From 9be711a8e7aa66b40af3920a600f5902c52ada0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Fri, 24 Mar 2023 11:26:46 +0800 Subject: [PATCH] Fix action type --- gvisor.go | 4 ++-- route.go | 47 ++++++++++++++++++++++++++++++----------------- system.go | 12 ++++++------ 3 files changed, 38 insertions(+), 25 deletions(-) diff --git a/gvisor.go b/gvisor.go index 972302a..81e8efd 100644 --- a/gvisor.go +++ b/gvisor.go @@ -175,7 +175,7 @@ func (t *GVisor) Start() error { } }) switch actionType := action.(type) { - case *ActionReject: + case *ActionBlock: // TODO: send icmp unreachable return true case *ActionDirect: @@ -238,7 +238,7 @@ func (t *GVisor) Start() error { } }) switch actionType := action.(type) { - case *ActionReject: + case *ActionBlock: // TODO: send icmp unreachable return true case *ActionDirect: diff --git a/route.go b/route.go index 91c91a5..8fbfb7c 100644 --- a/route.go +++ b/route.go @@ -2,6 +2,7 @@ package tun import ( "net/netip" + "time" E "github.com/sagernet/sing/common/exceptions" ) @@ -9,8 +10,9 @@ import ( type ActionType = uint8 const ( - ActionTypeReturn ActionType = iota - ActionTypeReject + ActionTypeUnknown ActionType = iota + ActionTypeReturn + ActionTypeBlock ActionTypeDirect ) @@ -18,8 +20,8 @@ func ParseActionType(action string) (ActionType, error) { switch action { case "return": return ActionTypeReturn, nil - case "reject": - return ActionTypeReject, nil + case "block": + return ActionTypeBlock, nil case "direct": return ActionTypeDirect, nil default: @@ -27,16 +29,18 @@ func ParseActionType(action string) (ActionType, error) { } } -func ActionTypeName(actionType ActionType) string { +func ActionTypeName(actionType ActionType) (string, error) { switch actionType { + case ActionTypeUnknown: + return "", nil case ActionTypeReturn: - return "return" - case ActionTypeReject: - return "reject" + return "return", nil + case ActionTypeBlock: + return "block", nil case ActionTypeDirect: - return "direct" + return "direct", nil default: - return "unknown" + return "", E.New("unknown action: ", actionType) } } @@ -60,30 +64,39 @@ type RouteAction interface { Timeout() bool } -type ActionReturn struct{} +type ActionReturn struct { + Expire time.Time +} func (r *ActionReturn) ActionType() ActionType { return ActionTypeReturn } func (r *ActionReturn) Timeout() bool { - return false + return r.Expire != time.Time{} && time.Now().Before(r.Expire) } -type ActionReject struct{} +type ActionBlock struct { + Expire time.Time +} -func (r *ActionReject) ActionType() ActionType { - return ActionTypeReject +func (r *ActionBlock) ActionType() ActionType { + return ActionTypeBlock } -func (r *ActionReject) Timeout() bool { - return false +func (r *ActionBlock) Timeout() bool { + return r.Expire != time.Time{} && time.Now().Before(r.Expire) } type ActionDirect struct { + Expire time.Time DirectDestination } func (r *ActionDirect) ActionType() ActionType { return ActionTypeDirect } + +func (r *ActionDirect) Timeout() bool { + return r.Expire != time.Time{} && time.Now().Before(r.Expire) || r.DirectDestination.Timeout() +} diff --git a/system.go b/system.go index e3e77eb..a5359d5 100644 --- a/system.go +++ b/system.go @@ -257,7 +257,7 @@ func (s *System) processIPv4TCP(packet clashtcpip.IPv4Packet, header clashtcpip. return s.router.RouteConnection(session, &systemTCPDirectPacketWriter4{s.tun, source}) }) switch actionType := action.(type) { - case *ActionReject: + case *ActionBlock: // TODO: send ICMP unreachable return nil case *ActionDirect: @@ -298,7 +298,7 @@ func (s *System) processIPv6TCP(packet clashtcpip.IPv6Packet, header clashtcpip. return s.router.RouteConnection(session, &systemTCPDirectPacketWriter6{s.tun, source}) }) switch actionType := action.(type) { - case *ActionReject: + case *ActionBlock: // TODO: send RST return nil case *ActionDirect: @@ -336,7 +336,7 @@ func (s *System) processIPv4UDP(packet clashtcpip.IPv4Packet, header clashtcpip. return s.router.RouteConnection(routeSession, &systemUDPDirectPacketWriter4{s.tun, source}) }) switch actionType := action.(type) { - case *ActionReject: + case *ActionBlock: // TODO: send icmp unreachable return nil case *ActionDirect: @@ -374,7 +374,7 @@ func (s *System) processIPv6UDP(packet clashtcpip.IPv6Packet, header clashtcpip. return s.router.RouteConnection(routeSession, &systemUDPDirectPacketWriter6{s.tun, source}) }) switch actionType := action.(type) { - case *ActionReject: + case *ActionBlock: // TODO: send icmp unreachable return nil case *ActionDirect: @@ -407,7 +407,7 @@ func (s *System) processIPv4ICMP(packet clashtcpip.IPv4Packet, header clashtcpip return s.router.RouteConnection(routeSession, &systemICMPDirectPacketWriter4{s.tun, packet.SourceIP()}) }) switch actionType := action.(type) { - case *ActionReject: + case *ActionBlock: // TODO: send icmp unreachable return nil case *ActionDirect: @@ -435,7 +435,7 @@ func (s *System) processIPv6ICMP(packet clashtcpip.IPv6Packet, header clashtcpip return s.router.RouteConnection(routeSession, &systemICMPDirectPacketWriter6{s.tun, packet.SourceIP()}) }) switch actionType := action.(type) { - case *ActionReject: + case *ActionBlock: // TODO: send icmp unreachable return nil case *ActionDirect: