Skip to content

Commit 9be711a

Browse files
committed
Fix action type
1 parent f0c27d0 commit 9be711a

3 files changed

Lines changed: 38 additions & 25 deletions

File tree

gvisor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func (t *GVisor) Start() error {
175175
}
176176
})
177177
switch actionType := action.(type) {
178-
case *ActionReject:
178+
case *ActionBlock:
179179
// TODO: send icmp unreachable
180180
return true
181181
case *ActionDirect:
@@ -238,7 +238,7 @@ func (t *GVisor) Start() error {
238238
}
239239
})
240240
switch actionType := action.(type) {
241-
case *ActionReject:
241+
case *ActionBlock:
242242
// TODO: send icmp unreachable
243243
return true
244244
case *ActionDirect:

route.go

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,45 @@ package tun
22

33
import (
44
"net/netip"
5+
"time"
56

67
E "github.com/sagernet/sing/common/exceptions"
78
)
89

910
type ActionType = uint8
1011

1112
const (
12-
ActionTypeReturn ActionType = iota
13-
ActionTypeReject
13+
ActionTypeUnknown ActionType = iota
14+
ActionTypeReturn
15+
ActionTypeBlock
1416
ActionTypeDirect
1517
)
1618

1719
func ParseActionType(action string) (ActionType, error) {
1820
switch action {
1921
case "return":
2022
return ActionTypeReturn, nil
21-
case "reject":
22-
return ActionTypeReject, nil
23+
case "block":
24+
return ActionTypeBlock, nil
2325
case "direct":
2426
return ActionTypeDirect, nil
2527
default:
2628
return 0, E.New("unknown action: ", action)
2729
}
2830
}
2931

30-
func ActionTypeName(actionType ActionType) string {
32+
func ActionTypeName(actionType ActionType) (string, error) {
3133
switch actionType {
34+
case ActionTypeUnknown:
35+
return "", nil
3236
case ActionTypeReturn:
33-
return "return"
34-
case ActionTypeReject:
35-
return "reject"
37+
return "return", nil
38+
case ActionTypeBlock:
39+
return "block", nil
3640
case ActionTypeDirect:
37-
return "direct"
41+
return "direct", nil
3842
default:
39-
return "unknown"
43+
return "", E.New("unknown action: ", actionType)
4044
}
4145
}
4246

@@ -60,30 +64,39 @@ type RouteAction interface {
6064
Timeout() bool
6165
}
6266

63-
type ActionReturn struct{}
67+
type ActionReturn struct {
68+
Expire time.Time
69+
}
6470

6571
func (r *ActionReturn) ActionType() ActionType {
6672
return ActionTypeReturn
6773
}
6874

6975
func (r *ActionReturn) Timeout() bool {
70-
return false
76+
return r.Expire != time.Time{} && time.Now().Before(r.Expire)
7177
}
7278

73-
type ActionReject struct{}
79+
type ActionBlock struct {
80+
Expire time.Time
81+
}
7482

75-
func (r *ActionReject) ActionType() ActionType {
76-
return ActionTypeReject
83+
func (r *ActionBlock) ActionType() ActionType {
84+
return ActionTypeBlock
7785
}
7886

79-
func (r *ActionReject) Timeout() bool {
80-
return false
87+
func (r *ActionBlock) Timeout() bool {
88+
return r.Expire != time.Time{} && time.Now().Before(r.Expire)
8189
}
8290

8391
type ActionDirect struct {
92+
Expire time.Time
8493
DirectDestination
8594
}
8695

8796
func (r *ActionDirect) ActionType() ActionType {
8897
return ActionTypeDirect
8998
}
99+
100+
func (r *ActionDirect) Timeout() bool {
101+
return r.Expire != time.Time{} && time.Now().Before(r.Expire) || r.DirectDestination.Timeout()
102+
}

system.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ func (s *System) processIPv4TCP(packet clashtcpip.IPv4Packet, header clashtcpip.
257257
return s.router.RouteConnection(session, &systemTCPDirectPacketWriter4{s.tun, source})
258258
})
259259
switch actionType := action.(type) {
260-
case *ActionReject:
260+
case *ActionBlock:
261261
// TODO: send ICMP unreachable
262262
return nil
263263
case *ActionDirect:
@@ -298,7 +298,7 @@ func (s *System) processIPv6TCP(packet clashtcpip.IPv6Packet, header clashtcpip.
298298
return s.router.RouteConnection(session, &systemTCPDirectPacketWriter6{s.tun, source})
299299
})
300300
switch actionType := action.(type) {
301-
case *ActionReject:
301+
case *ActionBlock:
302302
// TODO: send RST
303303
return nil
304304
case *ActionDirect:
@@ -336,7 +336,7 @@ func (s *System) processIPv4UDP(packet clashtcpip.IPv4Packet, header clashtcpip.
336336
return s.router.RouteConnection(routeSession, &systemUDPDirectPacketWriter4{s.tun, source})
337337
})
338338
switch actionType := action.(type) {
339-
case *ActionReject:
339+
case *ActionBlock:
340340
// TODO: send icmp unreachable
341341
return nil
342342
case *ActionDirect:
@@ -374,7 +374,7 @@ func (s *System) processIPv6UDP(packet clashtcpip.IPv6Packet, header clashtcpip.
374374
return s.router.RouteConnection(routeSession, &systemUDPDirectPacketWriter6{s.tun, source})
375375
})
376376
switch actionType := action.(type) {
377-
case *ActionReject:
377+
case *ActionBlock:
378378
// TODO: send icmp unreachable
379379
return nil
380380
case *ActionDirect:
@@ -407,7 +407,7 @@ func (s *System) processIPv4ICMP(packet clashtcpip.IPv4Packet, header clashtcpip
407407
return s.router.RouteConnection(routeSession, &systemICMPDirectPacketWriter4{s.tun, packet.SourceIP()})
408408
})
409409
switch actionType := action.(type) {
410-
case *ActionReject:
410+
case *ActionBlock:
411411
// TODO: send icmp unreachable
412412
return nil
413413
case *ActionDirect:
@@ -435,7 +435,7 @@ func (s *System) processIPv6ICMP(packet clashtcpip.IPv6Packet, header clashtcpip
435435
return s.router.RouteConnection(routeSession, &systemICMPDirectPacketWriter6{s.tun, packet.SourceIP()})
436436
})
437437
switch actionType := action.(type) {
438-
case *ActionReject:
438+
case *ActionBlock:
439439
// TODO: send icmp unreachable
440440
return nil
441441
case *ActionDirect:

0 commit comments

Comments
 (0)