From 6fc62da7ae5893b5a0ae573a5fda223735920732 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Sun, 4 Dec 2022 14:37:52 +0800 Subject: [PATCH] chore: change C.PacketAdapter from a struct to an interface --- adapter/inbound/packet.go | 17 +++- config/config.go | 125 +++++++++++++-------------- constant/adapters.go | 6 ++ constant/listener.go | 20 +---- hub/executor/executor.go | 9 +- hub/route/configs.go | 33 ++++--- hub/route/server.go | 2 +- listener/config/tuic.go | 23 +++++ listener/{sing_tun => config}/tun.go | 2 +- listener/http/server.go | 24 ++--- listener/inbound/base.go | 2 +- listener/inbound/http.go | 2 +- listener/inbound/mixed.go | 2 +- listener/inbound/redir.go | 2 +- listener/inbound/socks.go | 2 +- listener/inbound/tproxy.go | 2 +- listener/listener.go | 37 ++++---- listener/parse.go | 25 ++++-- listener/redir/tcp.go | 24 ++--- listener/shadowsocks/tcp.go | 2 +- listener/shadowsocks/udp.go | 4 +- listener/sing/sing.go | 2 +- listener/sing_shadowsocks/server.go | 2 +- listener/sing_tun/server.go | 15 ++-- listener/sing_vmess/server.go | 2 +- listener/socks/tcp.go | 36 ++++---- listener/socks/udp.go | 26 +++--- listener/tproxy/tproxy.go | 24 ++--- listener/tproxy/udp.go | 6 +- listener/tuic/server.go | 28 ++---- listener/tunnel/udp.go | 4 +- tunnel/tunnel.go | 8 +- 32 files changed, 267 insertions(+), 251 deletions(-) create mode 100644 listener/config/tuic.go rename listener/{sing_tun => config}/tun.go (99%) diff --git a/adapter/inbound/packet.go b/adapter/inbound/packet.go index 1b377d36..524c8f52 100644 --- a/adapter/inbound/packet.go +++ b/adapter/inbound/packet.go @@ -5,9 +5,18 @@ import ( "github.com/Dreamacro/clash/transport/socks5" ) +// PacketAdapter is a UDP Packet adapter for socks/redir/tun +type PacketAdapter struct { + C.UDPPacket + metadata *C.Metadata +} +// Metadata returns destination metadata +func (s *PacketAdapter) Metadata() *C.Metadata { + return s.metadata +} -func NewPacketWithInfos(target socks5.Addr, packet C.UDPPacket, source C.Type, inName , preferRulesName string) *C.PacketAdapter { +func NewPacketWithInfos(target socks5.Addr, packet C.UDPPacket, source C.Type, inName, preferRulesName string) C.PacketAdapter { metadata := parseSocksAddr(target) metadata.NetWork = C.UDP metadata.Type = source @@ -24,13 +33,13 @@ func NewPacketWithInfos(target socks5.Addr, packet C.UDPPacket, source C.Type, i } } - return C.NewPacketAdapter( + return &PacketAdapter{ packet, metadata, - ) + } } // NewPacket is PacketAdapter generator -func NewPacket(target socks5.Addr, packet C.UDPPacket, source C.Type) *C.PacketAdapter { +func NewPacket(target socks5.Addr, packet C.UDPPacket, source C.Type) C.PacketAdapter { return NewPacketWithInfos(target, packet, source, "", "") } diff --git a/config/config.go b/config/config.go index 5ac357b2..c44ac250 100644 --- a/config/config.go +++ b/config/config.go @@ -14,16 +14,11 @@ import ( "strings" "time" - "github.com/Dreamacro/clash/common/utils" - "github.com/Dreamacro/clash/listener/sing_tun" - "github.com/Dreamacro/clash/listener/tunnel" - R "github.com/Dreamacro/clash/rules" - RP "github.com/Dreamacro/clash/rules/provider" - L "github.com/Dreamacro/clash/listener" "github.com/Dreamacro/clash/adapter" "github.com/Dreamacro/clash/adapter/outbound" "github.com/Dreamacro/clash/adapter/outboundgroup" "github.com/Dreamacro/clash/adapter/provider" + "github.com/Dreamacro/clash/common/utils" "github.com/Dreamacro/clash/component/auth" "github.com/Dreamacro/clash/component/dialer" "github.com/Dreamacro/clash/component/fakeip" @@ -34,7 +29,12 @@ import ( providerTypes "github.com/Dreamacro/clash/constant/provider" snifferTypes "github.com/Dreamacro/clash/constant/sniffer" "github.com/Dreamacro/clash/dns" + L "github.com/Dreamacro/clash/listener" + LC "github.com/Dreamacro/clash/listener/config" + "github.com/Dreamacro/clash/listener/tunnel" "github.com/Dreamacro/clash/log" + R "github.com/Dreamacro/clash/rules" + RP "github.com/Dreamacro/clash/rules/provider" T "github.com/Dreamacro/clash/tunnel" "gopkg.in/yaml.v3" @@ -148,21 +148,21 @@ type Tun struct { AutoDetectInterface bool `yaml:"auto-detect-interface" json:"auto-detect-interface"` RedirectToTun []string `yaml:"-" json:"-"` - MTU uint32 `yaml:"mtu" json:"mtu,omitempty"` - Inet4Address []sing_tun.ListenPrefix `yaml:"inet4-address" json:"inet4-address,omitempty"` - Inet6Address []sing_tun.ListenPrefix `yaml:"inet6-address" json:"inet6-address,omitempty"` - StrictRoute bool `yaml:"strict-route" json:"strict-route,omitempty"` - Inet4RouteAddress []sing_tun.ListenPrefix `yaml:"inet4-route-address" json:"inet4-route-address,omitempty"` - Inet6RouteAddress []sing_tun.ListenPrefix `yaml:"inet6-route-address" json:"inet6-route-address,omitempty"` - IncludeUID []uint32 `yaml:"include-uid" json:"include-uid,omitempty"` - IncludeUIDRange []string `yaml:"include-uid-range" json:"include-uid-range,omitempty"` - ExcludeUID []uint32 `yaml:"exclude-uid" json:"exclude-uid,omitempty"` - ExcludeUIDRange []string `yaml:"exclude-uid-range" json:"exclude-uid-range,omitempty"` - IncludeAndroidUser []int `yaml:"include-android-user" json:"include-android-user,omitempty"` - IncludePackage []string `yaml:"include-package" json:"include-package,omitempty"` - ExcludePackage []string `yaml:"exclude-package" json:"exclude-package,omitempty"` - EndpointIndependentNat bool `yaml:"endpoint-independent-nat" json:"endpoint-independent-nat,omitempty"` - UDPTimeout int64 `yaml:"udp-timeout" json:"udp-timeout,omitempty"` + MTU uint32 `yaml:"mtu" json:"mtu,omitempty"` + Inet4Address []LC.ListenPrefix `yaml:"inet4-address" json:"inet4-address,omitempty"` + Inet6Address []LC.ListenPrefix `yaml:"inet6-address" json:"inet6-address,omitempty"` + StrictRoute bool `yaml:"strict-route" json:"strict-route,omitempty"` + Inet4RouteAddress []LC.ListenPrefix `yaml:"inet4-route-address" json:"inet4-route-address,omitempty"` + Inet6RouteAddress []LC.ListenPrefix `yaml:"inet6-route-address" json:"inet6-route-address,omitempty"` + IncludeUID []uint32 `yaml:"include-uid" json:"include-uid,omitempty"` + IncludeUIDRange []string `yaml:"include-uid-range" json:"include-uid-range,omitempty"` + ExcludeUID []uint32 `yaml:"exclude-uid" json:"exclude-uid,omitempty"` + ExcludeUIDRange []string `yaml:"exclude-uid-range" json:"exclude-uid-range,omitempty"` + IncludeAndroidUser []int `yaml:"include-android-user" json:"include-android-user,omitempty"` + IncludePackage []string `yaml:"include-package" json:"include-package,omitempty"` + ExcludePackage []string `yaml:"exclude-package" json:"exclude-package,omitempty"` + EndpointIndependentNat bool `yaml:"endpoint-independent-nat" json:"endpoint-independent-nat,omitempty"` + UDPTimeout int64 `yaml:"udp-timeout" json:"udp-timeout,omitempty"` } // IPTables config @@ -244,19 +244,19 @@ type RawTun struct { MTU uint32 `yaml:"mtu" json:"mtu,omitempty"` //Inet4Address []ListenPrefix `yaml:"inet4-address" json:"inet4_address,omitempty"` - Inet6Address []sing_tun.ListenPrefix `yaml:"inet6-address" json:"inet6_address,omitempty"` - StrictRoute bool `yaml:"strict-route" json:"strict_route,omitempty"` - Inet4RouteAddress []sing_tun.ListenPrefix `yaml:"inet4_route_address" json:"inet4_route_address,omitempty"` - Inet6RouteAddress []sing_tun.ListenPrefix `yaml:"inet6_route_address" json:"inet6_route_address,omitempty"` - IncludeUID []uint32 `yaml:"include-uid" json:"include_uid,omitempty"` - IncludeUIDRange []string `yaml:"include-uid-range" json:"include_uid_range,omitempty"` - ExcludeUID []uint32 `yaml:"exclude-uid" json:"exclude_uid,omitempty"` - ExcludeUIDRange []string `yaml:"exclude-uid-range" json:"exclude_uid_range,omitempty"` - IncludeAndroidUser []int `yaml:"include-android-user" json:"include_android_user,omitempty"` - IncludePackage []string `yaml:"include-package" json:"include_package,omitempty"` - ExcludePackage []string `yaml:"exclude-package" json:"exclude_package,omitempty"` - EndpointIndependentNat bool `yaml:"endpoint-independent-nat" json:"endpoint_independent_nat,omitempty"` - UDPTimeout int64 `yaml:"udp-timeout" json:"udp_timeout,omitempty"` + Inet6Address []LC.ListenPrefix `yaml:"inet6-address" json:"inet6_address,omitempty"` + StrictRoute bool `yaml:"strict-route" json:"strict_route,omitempty"` + Inet4RouteAddress []LC.ListenPrefix `yaml:"inet4_route_address" json:"inet4_route_address,omitempty"` + Inet6RouteAddress []LC.ListenPrefix `yaml:"inet6_route_address" json:"inet6_route_address,omitempty"` + IncludeUID []uint32 `yaml:"include-uid" json:"include_uid,omitempty"` + IncludeUIDRange []string `yaml:"include-uid-range" json:"include_uid_range,omitempty"` + ExcludeUID []uint32 `yaml:"exclude-uid" json:"exclude_uid,omitempty"` + ExcludeUIDRange []string `yaml:"exclude-uid-range" json:"exclude_uid_range,omitempty"` + IncludeAndroidUser []int `yaml:"include-android-user" json:"include_android_user,omitempty"` + IncludePackage []string `yaml:"include-package" json:"include_package,omitempty"` + ExcludePackage []string `yaml:"exclude-package" json:"exclude_package,omitempty"` + EndpointIndependentNat bool `yaml:"endpoint-independent-nat" json:"endpoint_independent_nat,omitempty"` + UDPTimeout int64 `yaml:"udp-timeout" json:"udp_timeout,omitempty"` } type RawTuicServer struct { @@ -272,34 +272,33 @@ type RawTuicServer struct { MaxUdpRelayPacketSize int `yaml:"max-udp-relay-packet-size" json:"max-udp-relay-packet-size,omitempty"` } - type RawConfig struct { - Port int `yaml:"port"` - SocksPort int `yaml:"socks-port"` - RedirPort int `yaml:"redir-port"` - TProxyPort int `yaml:"tproxy-port"` - MixedPort int `yaml:"mixed-port"` - ShadowSocksConfig string `yaml:"ss-config"` - VmessConfig string `yaml:"vmess-config"` - InboundTfo bool `yaml:"inbound-tfo"` - Authentication []string `yaml:"authentication"` - AllowLan bool `yaml:"allow-lan"` - BindAddress string `yaml:"bind-address"` - Mode T.TunnelMode `yaml:"mode"` - UnifiedDelay bool `yaml:"unified-delay"` - LogLevel log.LogLevel `yaml:"log-level"` - IPv6 bool `yaml:"ipv6"` - ExternalController string `yaml:"external-controller"` - ExternalControllerTLS string `yaml:"external-controller-tls"` - ExternalUI string `yaml:"external-ui"` - Secret string `yaml:"secret"` - Interface string `yaml:"interface-name"` - RoutingMark int `yaml:"routing-mark"` - Tunnels []tunnel.Tunnel `yaml:"tunnels"` - GeodataMode bool `yaml:"geodata-mode"` - GeodataLoader string `yaml:"geodata-loader"` - TCPConcurrent bool `yaml:"tcp-concurrent" json:"tcp-concurrent"` - EnableProcess bool `yaml:"enable-process" json:"enable-process"` + Port int `yaml:"port"` + SocksPort int `yaml:"socks-port"` + RedirPort int `yaml:"redir-port"` + TProxyPort int `yaml:"tproxy-port"` + MixedPort int `yaml:"mixed-port"` + ShadowSocksConfig string `yaml:"ss-config"` + VmessConfig string `yaml:"vmess-config"` + InboundTfo bool `yaml:"inbound-tfo"` + Authentication []string `yaml:"authentication"` + AllowLan bool `yaml:"allow-lan"` + BindAddress string `yaml:"bind-address"` + Mode T.TunnelMode `yaml:"mode"` + UnifiedDelay bool `yaml:"unified-delay"` + LogLevel log.LogLevel `yaml:"log-level"` + IPv6 bool `yaml:"ipv6"` + ExternalController string `yaml:"external-controller"` + ExternalControllerTLS string `yaml:"external-controller-tls"` + ExternalUI string `yaml:"external-ui"` + Secret string `yaml:"secret"` + Interface string `yaml:"interface-name"` + RoutingMark int `yaml:"routing-mark"` + Tunnels []tunnel.Tunnel `yaml:"tunnels"` + GeodataMode bool `yaml:"geodata-mode"` + GeodataLoader string `yaml:"geodata-loader"` + TCPConcurrent bool `yaml:"tcp-concurrent" json:"tcp-concurrent"` + EnableProcess bool `yaml:"enable-process" json:"enable-process"` Sniffer RawSniffer `yaml:"sniffer"` ProxyProvider map[string]map[string]any `yaml:"proxy-providers"` @@ -384,7 +383,7 @@ func UnmarshalRawConfig(buf []byte) (*RawConfig, error) { DNSHijack: []string{"0.0.0.0:53"}, // default hijack all dns query AutoRoute: true, AutoDetectInterface: true, - Inet6Address: []sing_tun.ListenPrefix{sing_tun.ListenPrefix(netip.MustParsePrefix("fdfe:dcba:9876::1/126"))}, + Inet6Address: []LC.ListenPrefix{LC.ListenPrefix(netip.MustParsePrefix("fdfe:dcba:9876::1/126"))}, }, TuicServer: RawTuicServer{ Enable: false, @@ -1232,7 +1231,7 @@ func parseTun(rawTun RawTun, general *General) error { RedirectToTun: rawTun.RedirectToTun, MTU: rawTun.MTU, - Inet4Address: []sing_tun.ListenPrefix{sing_tun.ListenPrefix(tunAddressPrefix)}, + Inet4Address: []LC.ListenPrefix{LC.ListenPrefix(tunAddressPrefix)}, Inet6Address: rawTun.Inet6Address, StrictRoute: rawTun.StrictRoute, Inet4RouteAddress: rawTun.Inet4RouteAddress, diff --git a/constant/adapters.go b/constant/adapters.go index d25023e2..f4b8ba4d 100644 --- a/constant/adapters.go +++ b/constant/adapters.go @@ -210,3 +210,9 @@ type UDPPacket interface { type UDPPacketInAddr interface { InAddr() net.Addr } + +// PacketAdapter is a UDP Packet adapter for socks/redir/tun +type PacketAdapter interface { + UDPPacket + Metadata() *Metadata +} diff --git a/constant/listener.go b/constant/listener.go index 370bf892..4edae67c 100644 --- a/constant/listener.go +++ b/constant/listener.go @@ -16,26 +16,8 @@ type AdvanceListener interface { type NewListener interface { Name() string - ReCreate(tcpIn chan<- ConnContext,udpIn chan<-*PacketAdapter) error + ReCreate(tcpIn chan<- ConnContext, udpIn chan<- PacketAdapter) error Close() error Address() string RawAddress() string } - -// PacketAdapter is a UDP Packet adapter for socks/redir/tun -type PacketAdapter struct { - UDPPacket - metadata *Metadata -} - -func NewPacketAdapter(udppacket UDPPacket,metadata *Metadata)*PacketAdapter{ -return &PacketAdapter{ - udppacket, - metadata, -} -} - -// Metadata returns destination metadata -func (s *PacketAdapter) Metadata() *Metadata { - return s.metadata -} \ No newline at end of file diff --git a/hub/executor/executor.go b/hub/executor/executor.go index 4a18b3c4..e1ac7f6e 100644 --- a/hub/executor/executor.go +++ b/hub/executor/executor.go @@ -8,6 +8,7 @@ import ( "sync" "github.com/Dreamacro/clash/adapter" + "github.com/Dreamacro/clash/adapter/inbound" "github.com/Dreamacro/clash/adapter/outboundgroup" "github.com/Dreamacro/clash/component/auth" "github.com/Dreamacro/clash/component/dialer" @@ -25,11 +26,9 @@ import ( "github.com/Dreamacro/clash/dns" "github.com/Dreamacro/clash/listener" authStore "github.com/Dreamacro/clash/listener/auth" - "github.com/Dreamacro/clash/adapter/inbound" + LC "github.com/Dreamacro/clash/listener/config" "github.com/Dreamacro/clash/listener/inner" - "github.com/Dreamacro/clash/listener/sing_tun" "github.com/Dreamacro/clash/listener/tproxy" - "github.com/Dreamacro/clash/listener/tuic" T "github.com/Dreamacro/clash/listener/tunnel" "github.com/Dreamacro/clash/log" "github.com/Dreamacro/clash/tunnel" @@ -281,7 +280,7 @@ func updateTun(general *config.General) { if general == nil { return } - listener.ReCreateTun(sing_tun.Tun(general.Tun), tunnel.TCPIn(), tunnel.UDPIn()) + listener.ReCreateTun(LC.Tun(general.Tun), tunnel.TCPIn(), tunnel.UDPIn()) listener.ReCreateRedirToTun(general.Tun.RedirectToTun) } @@ -367,7 +366,7 @@ func updateGeneral(general *config.General, force bool) { listener.ReCreateMixed(general.MixedPort, tcpIn, udpIn) listener.ReCreateShadowSocks(general.ShadowSocksConfig, tcpIn, udpIn) listener.ReCreateVmess(general.VmessConfig, tcpIn, udpIn) - listener.ReCreateTuic(tuic.TuicServer(general.TuicServer), tcpIn, udpIn) + listener.ReCreateTuic(LC.TuicServer(general.TuicServer), tcpIn, udpIn) } func updateUsers(users []auth.AuthUser) { diff --git a/hub/route/configs.go b/hub/route/configs.go index 4b3ca328..3699b9d3 100644 --- a/hub/route/configs.go +++ b/hub/route/configs.go @@ -13,8 +13,7 @@ import ( C "github.com/Dreamacro/clash/constant" "github.com/Dreamacro/clash/hub/executor" P "github.com/Dreamacro/clash/listener" - "github.com/Dreamacro/clash/listener/sing_tun" - "github.com/Dreamacro/clash/listener/tuic" + LC "github.com/Dreamacro/clash/listener/config" "github.com/Dreamacro/clash/log" "github.com/Dreamacro/clash/tunnel" @@ -69,19 +68,19 @@ type tunSchema struct { MTU *uint32 `yaml:"mtu" json:"mtu,omitempty"` //Inet4Address *[]config.ListenPrefix `yaml:"inet4-address" json:"inet4-address,omitempty"` - Inet6Address *[]sing_tun.ListenPrefix `yaml:"inet6-address" json:"inet6-address,omitempty"` - StrictRoute *bool `yaml:"strict-route" json:"strict-route,omitempty"` - Inet4RouteAddress *[]sing_tun.ListenPrefix `yaml:"inet4-route-address" json:"inet4-route-address,omitempty"` - Inet6RouteAddress *[]sing_tun.ListenPrefix `yaml:"inet6-route-address" json:"inet6-route-address,omitempty"` - IncludeUID *[]uint32 `yaml:"include-uid" json:"include-uid,omitempty"` - IncludeUIDRange *[]string `yaml:"include-uid-range" json:"include-uid-range,omitempty"` - ExcludeUID *[]uint32 `yaml:"exclude-uid" json:"exclude-uid,omitempty"` - ExcludeUIDRange *[]string `yaml:"exclude-uid-range" json:"exclude-uid-range,omitempty"` - IncludeAndroidUser *[]int `yaml:"include-android-user" json:"include-android-user,omitempty"` - IncludePackage *[]string `yaml:"include-package" json:"include-package,omitempty"` - ExcludePackage *[]string `yaml:"exclude-package" json:"exclude-package,omitempty"` - EndpointIndependentNat *bool `yaml:"endpoint-independent-nat" json:"endpoint-independent-nat,omitempty"` - UDPTimeout *int64 `yaml:"udp-timeout" json:"udp-timeout,omitempty"` + Inet6Address *[]LC.ListenPrefix `yaml:"inet6-address" json:"inet6-address,omitempty"` + StrictRoute *bool `yaml:"strict-route" json:"strict-route,omitempty"` + Inet4RouteAddress *[]LC.ListenPrefix `yaml:"inet4-route-address" json:"inet4-route-address,omitempty"` + Inet6RouteAddress *[]LC.ListenPrefix `yaml:"inet6-route-address" json:"inet6-route-address,omitempty"` + IncludeUID *[]uint32 `yaml:"include-uid" json:"include-uid,omitempty"` + IncludeUIDRange *[]string `yaml:"include-uid-range" json:"include-uid-range,omitempty"` + ExcludeUID *[]uint32 `yaml:"exclude-uid" json:"exclude-uid,omitempty"` + ExcludeUIDRange *[]string `yaml:"exclude-uid-range" json:"exclude-uid-range,omitempty"` + IncludeAndroidUser *[]int `yaml:"include-android-user" json:"include-android-user,omitempty"` + IncludePackage *[]string `yaml:"include-package" json:"include-package,omitempty"` + ExcludePackage *[]string `yaml:"exclude-package" json:"exclude-package,omitempty"` + EndpointIndependentNat *bool `yaml:"endpoint-independent-nat" json:"endpoint-independent-nat,omitempty"` + UDPTimeout *int64 `yaml:"udp-timeout" json:"udp-timeout,omitempty"` } type tuicServerSchema struct { @@ -118,7 +117,7 @@ func pointerOrDefaultString(p *string, def string) string { return def } -func pointerOrDefaultTun(p *tunSchema, def sing_tun.Tun) sing_tun.Tun { +func pointerOrDefaultTun(p *tunSchema, def LC.Tun) LC.Tun { if p != nil { def.Enable = p.Enable if p.Device != nil { @@ -176,7 +175,7 @@ func pointerOrDefaultTun(p *tunSchema, def sing_tun.Tun) sing_tun.Tun { return def } -func pointerOrDefaultTuicServer(p *tuicServerSchema, def tuic.TuicServer) tuic.TuicServer { +func pointerOrDefaultTuicServer(p *tuicServerSchema, def LC.TuicServer) LC.TuicServer { if p != nil { def.Enable = p.Enable if p.Listen != nil { diff --git a/hub/route/server.go b/hub/route/server.go index 00a33a08..2bef92c1 100644 --- a/hub/route/server.go +++ b/hub/route/server.go @@ -9,9 +9,9 @@ import ( "time" "github.com/Dreamacro/clash/adapter/inbound" + CN "github.com/Dreamacro/clash/common/net" C "github.com/Dreamacro/clash/constant" "github.com/Dreamacro/clash/log" - CN "github.com/Dreamacro/clash/common/net" "github.com/Dreamacro/clash/tunnel/statistic" "github.com/go-chi/chi/v5" diff --git a/listener/config/tuic.go b/listener/config/tuic.go new file mode 100644 index 00000000..78252fd3 --- /dev/null +++ b/listener/config/tuic.go @@ -0,0 +1,23 @@ +package config + +import ( + "encoding/json" +) + +type TuicServer struct { + Enable bool + Listen string + Token []string + Certificate string + PrivateKey string + CongestionController string + MaxIdleTime int + AuthenticationTimeout int + ALPN []string + MaxUdpRelayPacketSize int +} + +func (t TuicServer) String() string { + b, _ := json.Marshal(t) + return string(b) +} diff --git a/listener/sing_tun/tun.go b/listener/config/tun.go similarity index 99% rename from listener/sing_tun/tun.go rename to listener/config/tun.go index 95ff61ba..828ed488 100644 --- a/listener/sing_tun/tun.go +++ b/listener/config/tun.go @@ -1,4 +1,4 @@ -package sing_tun +package config import ( "encoding/json" diff --git a/listener/http/server.go b/listener/http/server.go index fceafb68..7bcc542f 100644 --- a/listener/http/server.go +++ b/listener/http/server.go @@ -9,10 +9,10 @@ import ( ) type Listener struct { - listener net.Listener - addr string - closed bool - name string + listener net.Listener + addr string + closed bool + name string preferRulesName string } @@ -33,14 +33,14 @@ func (l *Listener) Close() error { } func New(addr string, in chan<- C.ConnContext) (*Listener, error) { - return NewWithAuthenticate(addr,"DEFAULT-HTTP","", in, true) + return NewWithAuthenticate(addr, "DEFAULT-HTTP", "", in, true) } -func NewWithInfos(addr ,name ,preferRulesName string,in chan<-C.ConnContext)(*Listener,error){ - return NewWithAuthenticate(addr,name,preferRulesName,in,true) +func NewWithInfos(addr, name, preferRulesName string, in chan<- C.ConnContext) (*Listener, error) { + return NewWithAuthenticate(addr, name, preferRulesName, in, true) } -func NewWithAuthenticate(addr,name,preferRulesName string, in chan<- C.ConnContext, authenticate bool) (*Listener, error) { +func NewWithAuthenticate(addr, name, preferRulesName string, in chan<- C.ConnContext, authenticate bool) (*Listener, error) { l, err := inbound.Listen("tcp", addr) if err != nil { @@ -53,10 +53,10 @@ func NewWithAuthenticate(addr,name,preferRulesName string, in chan<- C.ConnConte } hl := &Listener{ - listener: l, - name: name, + listener: l, + name: name, preferRulesName: preferRulesName, - addr: addr, + addr: addr, } go func() { for { @@ -67,7 +67,7 @@ func NewWithAuthenticate(addr,name,preferRulesName string, in chan<- C.ConnConte } continue } - go HandleConn(hl.name,hl.preferRulesName,conn, in, c) + go HandleConn(hl.name, hl.preferRulesName, conn, in, c) } }() diff --git a/listener/inbound/base.go b/listener/inbound/base.go index 9fec28cd..a00ec5e6 100644 --- a/listener/inbound/base.go +++ b/listener/inbound/base.go @@ -52,7 +52,7 @@ func (b *Base) RawAddress() string { } // ReCreate implements constant.NewListener -func (*Base) ReCreate(tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) error { +func (*Base) ReCreate(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { return nil } diff --git a/listener/inbound/http.go b/listener/inbound/http.go index 1902109f..3b3de193 100644 --- a/listener/inbound/http.go +++ b/listener/inbound/http.go @@ -30,7 +30,7 @@ func (h *HTTP) Address() string { } // ReCreate implements constant.NewListener -func (h *HTTP) ReCreate(tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) error { +func (h *HTTP) ReCreate(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { var err error _ = h.Close() h.l, err = http.NewWithInfos(h.RawAddress(), h.name, h.preferRulesName, tcpIn) diff --git a/listener/inbound/mixed.go b/listener/inbound/mixed.go index 691b2503..087c69fe 100644 --- a/listener/inbound/mixed.go +++ b/listener/inbound/mixed.go @@ -39,7 +39,7 @@ func (m *Mixed) Address() string { } // ReCreate implements constant.NewListener -func (m *Mixed) ReCreate(tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) error { +func (m *Mixed) ReCreate(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { var err error _ = m.Close() m.l, err = mixed.NewWithInfos(m.RawAddress(), m.name, m.preferRulesName, tcpIn) diff --git a/listener/inbound/redir.go b/listener/inbound/redir.go index baf6c0f7..a5864464 100644 --- a/listener/inbound/redir.go +++ b/listener/inbound/redir.go @@ -31,7 +31,7 @@ func (r *Redir) Address() string { } // ReCreate implements constant.NewListener -func (r *Redir) ReCreate(tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) error { +func (r *Redir) ReCreate(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { var err error _ = r.Close() r.l, err = redir.NewWithInfos(r.Address(), r.name, r.preferRulesName, tcpIn) diff --git a/listener/inbound/socks.go b/listener/inbound/socks.go index 35e11297..a1b44a24 100644 --- a/listener/inbound/socks.go +++ b/listener/inbound/socks.go @@ -60,7 +60,7 @@ func (s *Socks) Address() string { } // ReCreate implements constant.NewListener -func (s *Socks) ReCreate(tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) error { +func (s *Socks) ReCreate(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { s.mux.Lock() defer s.mux.Unlock() var err error diff --git a/listener/inbound/tproxy.go b/listener/inbound/tproxy.go index acca5c8e..396117d2 100644 --- a/listener/inbound/tproxy.go +++ b/listener/inbound/tproxy.go @@ -38,7 +38,7 @@ func (t *TProxy) Address() string { } // ReCreate implements constant.NewListener -func (t *TProxy) ReCreate(tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) error { +func (t *TProxy) ReCreate(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { var err error _ = t.Close() t.lTCP, err = tproxy.NewWithInfos(t.RawAddress(), t.name, t.preferRulesName, tcpIn) diff --git a/listener/listener.go b/listener/listener.go index 4bf82369..836bfb2a 100644 --- a/listener/listener.go +++ b/listener/listener.go @@ -12,6 +12,7 @@ import ( "github.com/Dreamacro/clash/component/ebpf" C "github.com/Dreamacro/clash/constant" "github.com/Dreamacro/clash/listener/autoredir" + LC "github.com/Dreamacro/clash/listener/config" "github.com/Dreamacro/clash/listener/http" "github.com/Dreamacro/clash/listener/mixed" "github.com/Dreamacro/clash/listener/redir" @@ -64,8 +65,8 @@ var ( autoRedirMux sync.Mutex tcMux sync.Mutex - LastTunConf sing_tun.Tun - LastTuicConf tuic.TuicServer + LastTunConf LC.Tun + LastTuicConf LC.TuicServer ) type Ports struct { @@ -78,18 +79,18 @@ type Ports struct { VmessConfig string `json:"vmess-config"` } -func GetTunConf() sing_tun.Tun { +func GetTunConf() LC.Tun { if tunLister == nil { - return sing_tun.Tun{ + return LC.Tun{ Enable: false, } } return tunLister.Config() } -func GetTuicConf() tuic.TuicServer { +func GetTuicConf() LC.TuicServer { if tuicListener == nil { - return tuic.TuicServer{Enable: false} + return LC.TuicServer{Enable: false} } return tuicListener.Config() } @@ -144,7 +145,7 @@ func ReCreateHTTP(port int, tcpIn chan<- C.ConnContext) { log.Infoln("HTTP proxy listening at: %s", httpListener.Address()) } -func ReCreateSocks(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) { +func ReCreateSocks(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) { socksMux.Lock() defer socksMux.Unlock() @@ -203,7 +204,7 @@ func ReCreateSocks(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketA log.Infoln("SOCKS proxy listening at: %s", socksListener.Address()) } -func ReCreateRedir(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) { +func ReCreateRedir(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) { redirMux.Lock() defer redirMux.Unlock() @@ -249,7 +250,7 @@ func ReCreateRedir(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketA log.Infoln("Redirect proxy listening at: %s", redirListener.Address()) } -func ReCreateShadowSocks(shadowSocksConfig string, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) { +func ReCreateShadowSocks(shadowSocksConfig string, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) { ssMux.Lock() defer ssMux.Unlock() @@ -289,7 +290,7 @@ func ReCreateShadowSocks(shadowSocksConfig string, tcpIn chan<- C.ConnContext, u return } -func ReCreateVmess(vmessConfig string, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) { +func ReCreateVmess(vmessConfig string, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) { vmessMux.Lock() defer vmessMux.Unlock() @@ -329,7 +330,7 @@ func ReCreateVmess(vmessConfig string, tcpIn chan<- C.ConnContext, udpIn chan<- return } -func ReCreateTuic(config tuic.TuicServer, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) { +func ReCreateTuic(config LC.TuicServer, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) { tuicMux.Lock() defer func() { LastTuicConf = config @@ -371,7 +372,7 @@ func ReCreateTuic(config tuic.TuicServer, tcpIn chan<- C.ConnContext, udpIn chan return } -func ReCreateTProxy(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) { +func ReCreateTProxy(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) { tproxyMux.Lock() defer tproxyMux.Unlock() @@ -417,7 +418,7 @@ func ReCreateTProxy(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *C.Packet log.Infoln("TProxy server listening at: %s", tproxyListener.Address()) } -func ReCreateMixed(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) { +func ReCreateMixed(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) { mixedMux.Lock() defer mixedMux.Unlock() @@ -472,7 +473,7 @@ func ReCreateMixed(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketA log.Infoln("Mixed(http+socks) proxy listening at: %s", mixedListener.Address()) } -func ReCreateTun(tunConf sing_tun.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) { +func ReCreateTun(tunConf LC.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) { tunMux.Lock() defer func() { LastTunConf = tunConf @@ -536,7 +537,7 @@ func ReCreateRedirToTun(ifaceNames []string) { log.Infoln("Attached tc ebpf program to interfaces %v", tcProgram.RawNICs()) } -func ReCreateAutoRedir(ifaceNames []string, tcpIn chan<- C.ConnContext, _ chan<- *C.PacketAdapter) { +func ReCreateAutoRedir(ifaceNames []string, tcpIn chan<- C.ConnContext, _ chan<- C.PacketAdapter) { autoRedirMux.Lock() defer autoRedirMux.Unlock() @@ -592,7 +593,7 @@ func ReCreateAutoRedir(ifaceNames []string, tcpIn chan<- C.ConnContext, _ chan<- log.Infoln("Auto redirect proxy listening at: %s, attached tc ebpf program to interfaces %v", autoRedirListener.Address(), autoRedirProgram.RawNICs()) } -func PatchTunnel(tunnels []tunnel.Tunnel, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) { +func PatchTunnel(tunnels []tunnel.Tunnel, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) { tunnelMux.Lock() defer tunnelMux.Unlock() @@ -745,7 +746,7 @@ func genAddr(host string, port int, allowLan bool) string { return fmt.Sprintf("127.0.0.1:%d", port) } -func hasTunConfigChange(tunConf *sing_tun.Tun) bool { +func hasTunConfigChange(tunConf *LC.Tun) bool { if LastTunConf.Enable != tunConf.Enable || LastTunConf.Device != tunConf.Device || LastTunConf.Stack != tunConf.Stack || @@ -833,5 +834,5 @@ func Cleanup(wait bool) { tunLister.Close() tunLister = nil } - LastTunConf = sing_tun.Tun{} + LastTunConf = LC.Tun{} } diff --git a/listener/parse.go b/listener/parse.go index 026c7df6..ee85e26c 100644 --- a/listener/parse.go +++ b/listener/parse.go @@ -25,23 +25,38 @@ func ParseListener(mapping map[string]any) (C.NewListener, error) { switch proxyType { case "socks": socksOption := &IN.SocksOption{} - decoder.Decode(mapping, socksOption) + err = decoder.Decode(mapping, socksOption) + if err != nil { + return nil, err + } listener, err = IN.NewSocks(socksOption) case "http": httpOption := &IN.HTTPOption{} - decoder.Decode(mapping, httpOption) + err = decoder.Decode(mapping, httpOption) + if err != nil { + return nil, err + } listener, err = IN.NewHTTP(httpOption) case "tproxy": tproxyOption := &IN.TProxyOption{} - decoder.Decode(mapping, tproxyOption) + err = decoder.Decode(mapping, tproxyOption) + if err != nil { + return nil, err + } listener, err = IN.NewTProxy(tproxyOption) case "redir": redirOption := &IN.RedirOption{} - decoder.Decode(mapping, redirOption) + err = decoder.Decode(mapping, redirOption) + if err != nil { + return nil, err + } listener, err = IN.NewRedir(redirOption) case "mixed": mixedOption := &IN.MixedOption{} - decoder.Decode(mapping, mixedOption) + err = decoder.Decode(mapping, mixedOption) + if err != nil { + return nil, err + } listener, err = IN.NewMixed(mixedOption) default: return nil, fmt.Errorf("unsupport proxy type: %s", proxyType) diff --git a/listener/redir/tcp.go b/listener/redir/tcp.go index d55897cb..a9e9b5b8 100644 --- a/listener/redir/tcp.go +++ b/listener/redir/tcp.go @@ -8,10 +8,10 @@ import ( ) type Listener struct { - listener net.Listener - addr string - closed bool - name string + listener net.Listener + addr string + closed bool + name string preferRulesName string } @@ -32,18 +32,18 @@ func (l *Listener) Close() error { } func New(addr string, in chan<- C.ConnContext) (*Listener, error) { - return NewWithInfos(addr,"DEFAULT-REDIR","",in) + return NewWithInfos(addr, "DEFAULT-REDIR", "", in) } -func NewWithInfos(addr,name,preferRulesName string, in chan<- C.ConnContext) (*Listener, error) { +func NewWithInfos(addr, name, preferRulesName string, in chan<- C.ConnContext) (*Listener, error) { l, err := net.Listen("tcp", addr) if err != nil { return nil, err } rl := &Listener{ - listener: l, - addr: addr, - name: name, + listener: l, + addr: addr, + name: name, preferRulesName: preferRulesName, } @@ -56,18 +56,18 @@ func NewWithInfos(addr,name,preferRulesName string, in chan<- C.ConnContext) (*L } continue } - go handleRedir(rl.name,rl.preferRulesName,c, in) + go handleRedir(rl.name, rl.preferRulesName, c, in) } }() return rl, nil } -func handleRedir(name,preferRulesName string,conn net.Conn, in chan<- C.ConnContext) { +func handleRedir(name, preferRulesName string, conn net.Conn, in chan<- C.ConnContext) { target, err := parserPacket(conn) if err != nil { conn.Close() return } conn.(*net.TCPConn).SetKeepAlive(true) - in <- inbound.NewSocketWithInfos(target, conn, C.REDIR,name,preferRulesName) + in <- inbound.NewSocketWithInfos(target, conn, C.REDIR, name, preferRulesName) } diff --git a/listener/shadowsocks/tcp.go b/listener/shadowsocks/tcp.go index 74ffca59..ab235e18 100644 --- a/listener/shadowsocks/tcp.go +++ b/listener/shadowsocks/tcp.go @@ -21,7 +21,7 @@ type Listener struct { var _listener *Listener -func New(config string, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) (*Listener, error) { +func New(config string, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) (*Listener, error) { addr, cipher, password, err := ParseSSURL(config) if err != nil { return nil, err diff --git a/listener/shadowsocks/udp.go b/listener/shadowsocks/udp.go index 5ea17c6b..6357afa9 100644 --- a/listener/shadowsocks/udp.go +++ b/listener/shadowsocks/udp.go @@ -17,7 +17,7 @@ type UDPListener struct { closed bool } -func NewUDP(addr string, pickCipher core.Cipher, in chan<- *C.PacketAdapter) (*UDPListener, error) { +func NewUDP(addr string, pickCipher core.Cipher, in chan<- C.PacketAdapter) (*UDPListener, error) { l, err := net.ListenPacket("udp", addr) if err != nil { return nil, err @@ -53,7 +53,7 @@ func (l *UDPListener) Close() error { return l.packetConn.Close() } -func handleSocksUDP(pc net.PacketConn, in chan<- *C.PacketAdapter, buf []byte, addr net.Addr) { +func handleSocksUDP(pc net.PacketConn, in chan<- C.PacketAdapter, buf []byte, addr net.Addr) { tgtAddr := socks5.SplitAddr(buf) if tgtAddr == nil { // Unresolved UDP packet, return buffer to the pool diff --git a/listener/sing/sing.go b/listener/sing/sing.go index 4ff37f6a..5b605373 100644 --- a/listener/sing/sing.go +++ b/listener/sing/sing.go @@ -24,7 +24,7 @@ const UDPTimeout = 5 * time.Minute type ListenerHandler struct { TcpIn chan<- C.ConnContext - UdpIn chan<- *C.PacketAdapter + UdpIn chan<- C.PacketAdapter Type C.Type } diff --git a/listener/sing_shadowsocks/server.go b/listener/sing_shadowsocks/server.go index ecfc1df7..967d64fa 100644 --- a/listener/sing_shadowsocks/server.go +++ b/listener/sing_shadowsocks/server.go @@ -32,7 +32,7 @@ type Listener struct { var _listener *Listener -func New(config string, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) (C.AdvanceListener, error) { +func New(config string, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) (C.AdvanceListener, error) { addr, cipher, password, err := embedSS.ParseSSURL(config) if err != nil { return nil, err diff --git a/listener/sing_tun/server.go b/listener/sing_tun/server.go index 13437149..aeba8999 100644 --- a/listener/sing_tun/server.go +++ b/listener/sing_tun/server.go @@ -11,6 +11,7 @@ import ( "github.com/Dreamacro/clash/component/dialer" "github.com/Dreamacro/clash/component/iface" C "github.com/Dreamacro/clash/constant" + LC "github.com/Dreamacro/clash/listener/config" "github.com/Dreamacro/clash/listener/sing" "github.com/Dreamacro/clash/log" @@ -25,7 +26,7 @@ var InterfaceName = "Meta" type Listener struct { closed bool - options Tun + options LC.Tun handler *ListenerHandler tunName string @@ -63,7 +64,7 @@ func CalculateInterfaceName(name string) (tunName string) { return } -func New(options Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) (l *Listener, err error) { +func New(options LC.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) (l *Listener, err error) { tunName := options.Device if tunName == "" { tunName = CalculateInterfaceName(InterfaceName) @@ -161,12 +162,12 @@ func New(options Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) tunOptions := tun.Options{ Name: tunName, MTU: tunMTU, - Inet4Address: common.Map(options.Inet4Address, ListenPrefix.Build), - Inet6Address: common.Map(options.Inet6Address, ListenPrefix.Build), + Inet4Address: common.Map(options.Inet4Address, LC.ListenPrefix.Build), + Inet6Address: common.Map(options.Inet6Address, LC.ListenPrefix.Build), AutoRoute: options.AutoRoute, StrictRoute: options.StrictRoute, - Inet4RouteAddress: common.Map(options.Inet4RouteAddress, ListenPrefix.Build), - Inet6RouteAddress: common.Map(options.Inet6RouteAddress, ListenPrefix.Build), + Inet4RouteAddress: common.Map(options.Inet4RouteAddress, LC.ListenPrefix.Build), + Inet6RouteAddress: common.Map(options.Inet6RouteAddress, LC.ListenPrefix.Build), IncludeUID: includeUID, ExcludeUID: excludeUID, IncludeAndroidUser: options.IncludeAndroidUser, @@ -282,6 +283,6 @@ func (l *Listener) Close() { ) } -func (l *Listener) Config() Tun { +func (l *Listener) Config() LC.Tun { return l.options } diff --git a/listener/sing_vmess/server.go b/listener/sing_vmess/server.go index 4dd046c1..997fc6b0 100644 --- a/listener/sing_vmess/server.go +++ b/listener/sing_vmess/server.go @@ -24,7 +24,7 @@ type Listener struct { var _listener *Listener -func New(config string, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) (*Listener, error) { +func New(config string, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) (*Listener, error) { addr, username, password, err := parseVmessURL(config) if err != nil { return nil, err diff --git a/listener/socks/tcp.go b/listener/socks/tcp.go index 7ddc71ea..30174b61 100644 --- a/listener/socks/tcp.go +++ b/listener/socks/tcp.go @@ -13,11 +13,11 @@ import ( ) type Listener struct { - listener net.Listener - addr string - closed bool - preferRulesName string - name string + listener net.Listener + addr string + closed bool + preferRulesName string + name string } // RawAddress implements C.Listener @@ -37,19 +37,19 @@ func (l *Listener) Close() error { } func New(addr string, in chan<- C.ConnContext) (*Listener, error) { - return NewWithInfos(addr,"DEFAULT-SOCKS","",in) + return NewWithInfos(addr, "DEFAULT-SOCKS", "", in) } -func NewWithInfos(addr,name,preferRulesName string, in chan<- C.ConnContext) (*Listener, error) { +func NewWithInfos(addr, name, preferRulesName string, in chan<- C.ConnContext) (*Listener, error) { l, err := inbound.Listen("tcp", addr) if err != nil { return nil, err } sl := &Listener{ - listener: l, - addr: addr, - name: name, + listener: l, + addr: addr, + name: name, preferRulesName: preferRulesName, } go func() { @@ -61,14 +61,14 @@ func NewWithInfos(addr,name,preferRulesName string, in chan<- C.ConnContext) (*L } continue } - go handleSocks(sl.name,sl.preferRulesName,c, in) + go handleSocks(sl.name, sl.preferRulesName, c, in) } }() return sl, nil } -func handleSocks(name,preferRulesName string,conn net.Conn, in chan<- C.ConnContext) { +func handleSocks(name, preferRulesName string, conn net.Conn, in chan<- C.ConnContext) { conn.(*net.TCPConn).SetKeepAlive(true) bufConn := N.NewBufferedConn(conn) head, err := bufConn.Peek(1) @@ -79,24 +79,24 @@ func handleSocks(name,preferRulesName string,conn net.Conn, in chan<- C.ConnCont switch head[0] { case socks4.Version: - HandleSocks4(name,preferRulesName,bufConn, in) + HandleSocks4(name, preferRulesName, bufConn, in) case socks5.Version: - HandleSocks5(name,preferRulesName,bufConn, in) + HandleSocks5(name, preferRulesName, bufConn, in) default: conn.Close() } } -func HandleSocks4(name,preferRulesName string, conn net.Conn, in chan<- C.ConnContext) { +func HandleSocks4(name, preferRulesName string, conn net.Conn, in chan<- C.ConnContext) { addr, _, err := socks4.ServerHandshake(conn, authStore.Authenticator()) if err != nil { conn.Close() return } - in <- inbound.NewSocketWithInfos(socks5.ParseAddr(addr), conn, C.SOCKS4,name,preferRulesName) + in <- inbound.NewSocketWithInfos(socks5.ParseAddr(addr), conn, C.SOCKS4, name, preferRulesName) } -func HandleSocks5(name,preferRulesName string,conn net.Conn, in chan<- C.ConnContext) { +func HandleSocks5(name, preferRulesName string, conn net.Conn, in chan<- C.ConnContext) { target, command, err := socks5.ServerHandshake(conn, authStore.Authenticator()) if err != nil { conn.Close() @@ -107,5 +107,5 @@ func HandleSocks5(name,preferRulesName string,conn net.Conn, in chan<- C.ConnCon io.Copy(io.Discard, conn) return } - in <- inbound.NewSocketWithInfos(target, conn, C.SOCKS5,name,preferRulesName) + in <- inbound.NewSocketWithInfos(target, conn, C.SOCKS5, name, preferRulesName) } diff --git a/listener/socks/udp.go b/listener/socks/udp.go index 6adbd257..cde92c54 100644 --- a/listener/socks/udp.go +++ b/listener/socks/udp.go @@ -12,10 +12,10 @@ import ( ) type UDPListener struct { - packetConn net.PacketConn - addr string - closed bool - name string + packetConn net.PacketConn + addr string + closed bool + name string preferRulesName string } @@ -35,11 +35,11 @@ func (l *UDPListener) Close() error { return l.packetConn.Close() } -func NewUDP(addr string, in chan<- *C.PacketAdapter) (*UDPListener, error) { - return NewUDPWithInfos(addr,"DEFAULT-SOCKS","",in) +func NewUDP(addr string, in chan<- C.PacketAdapter) (*UDPListener, error) { + return NewUDPWithInfos(addr, "DEFAULT-SOCKS", "", in) } -func NewUDPWithInfos(addr,name ,preferRulesName string, in chan<- *C.PacketAdapter) (*UDPListener, error) { +func NewUDPWithInfos(addr, name, preferRulesName string, in chan<- C.PacketAdapter) (*UDPListener, error) { l, err := net.ListenPacket("udp", addr) if err != nil { return nil, err @@ -50,10 +50,10 @@ func NewUDPWithInfos(addr,name ,preferRulesName string, in chan<- *C.PacketAdapt } sl := &UDPListener{ - packetConn: l, - addr: addr, + packetConn: l, + addr: addr, preferRulesName: preferRulesName, - name: name, + name: name, } go func() { for { @@ -66,14 +66,14 @@ func NewUDPWithInfos(addr,name ,preferRulesName string, in chan<- *C.PacketAdapt } continue } - handleSocksUDP(sl.name,sl.preferRulesName,l, in, buf[:n], remoteAddr) + handleSocksUDP(sl.name, sl.preferRulesName, l, in, buf[:n], remoteAddr) } }() return sl, nil } -func handleSocksUDP(name,preferRulesName string,pc net.PacketConn, in chan<- *C.PacketAdapter, buf []byte, addr net.Addr) { +func handleSocksUDP(name, preferRulesName string, pc net.PacketConn, in chan<- C.PacketAdapter, buf []byte, addr net.Addr) { target, payload, err := socks5.DecodeUDPPacket(buf) if err != nil { // Unresolved UDP packet, return buffer to the pool @@ -87,7 +87,7 @@ func handleSocksUDP(name,preferRulesName string,pc net.PacketConn, in chan<- *C. bufRef: buf, } select { - case in <- inbound.NewPacketWithInfos(target, packet, C.SOCKS5,name,preferRulesName): + case in <- inbound.NewPacketWithInfos(target, packet, C.SOCKS5, name, preferRulesName): default: } } diff --git a/listener/tproxy/tproxy.go b/listener/tproxy/tproxy.go index c546008d..a1c53b53 100644 --- a/listener/tproxy/tproxy.go +++ b/listener/tproxy/tproxy.go @@ -9,10 +9,10 @@ import ( ) type Listener struct { - listener net.Listener - addr string - closed bool - name string + listener net.Listener + addr string + closed bool + name string preferRulesName string } @@ -32,17 +32,17 @@ func (l *Listener) Close() error { return l.listener.Close() } -func (l *Listener) handleTProxy(name,preferRulesName string ,conn net.Conn, in chan<- C.ConnContext) { +func (l *Listener) handleTProxy(name, preferRulesName string, conn net.Conn, in chan<- C.ConnContext) { target := socks5.ParseAddrToSocksAddr(conn.LocalAddr()) conn.(*net.TCPConn).SetKeepAlive(true) - in <- inbound.NewSocketWithInfos(target, conn, C.TPROXY,name,preferRulesName) + in <- inbound.NewSocketWithInfos(target, conn, C.TPROXY, name, preferRulesName) } func New(addr string, in chan<- C.ConnContext) (*Listener, error) { - return NewWithInfos(addr,"DEFAULT-TPROXY","",in) + return NewWithInfos(addr, "DEFAULT-TPROXY", "", in) } -func NewWithInfos(addr,name,preferRulesName string, in chan<- C.ConnContext) (*Listener, error) { +func NewWithInfos(addr, name, preferRulesName string, in chan<- C.ConnContext) (*Listener, error) { l, err := net.Listen("tcp", addr) if err != nil { return nil, err @@ -60,9 +60,9 @@ func NewWithInfos(addr,name,preferRulesName string, in chan<- C.ConnContext) (*L } rl := &Listener{ - listener: l, - addr: addr, - name: name, + listener: l, + addr: addr, + name: name, preferRulesName: preferRulesName, } @@ -75,7 +75,7 @@ func NewWithInfos(addr,name,preferRulesName string, in chan<- C.ConnContext) (*L } continue } - go rl.handleTProxy(rl.name,rl.preferRulesName,c, in) + go rl.handleTProxy(rl.name, rl.preferRulesName, c, in) } }() diff --git a/listener/tproxy/udp.go b/listener/tproxy/udp.go index f34e5525..9560a626 100644 --- a/listener/tproxy/udp.go +++ b/listener/tproxy/udp.go @@ -34,11 +34,11 @@ func (l *UDPListener) Close() error { return l.packetConn.Close() } -func NewUDP(addr string, in chan<- *C.PacketAdapter) (*UDPListener, error) { +func NewUDP(addr string, in chan<- C.PacketAdapter) (*UDPListener, error) { return NewUDPWithInfos(addr, "DEFAULT-TPROXY", "", in) } -func NewUDPWithInfos(addr, name, preferRulesName string, in chan<- *C.PacketAdapter) (*UDPListener, error) { +func NewUDPWithInfos(addr, name, preferRulesName string, in chan<- C.PacketAdapter) (*UDPListener, error) { l, err := net.ListenPacket("udp", addr) if err != nil { return nil, err @@ -90,7 +90,7 @@ func NewUDPWithInfos(addr, name, preferRulesName string, in chan<- *C.PacketAdap return rl, nil } -func handlePacketConn(name, preferRulesName string, pc net.PacketConn, in chan<- *C.PacketAdapter, buf []byte, lAddr, rAddr netip.AddrPort) { +func handlePacketConn(name, preferRulesName string, pc net.PacketConn, in chan<- C.PacketAdapter, buf []byte, lAddr, rAddr netip.AddrPort) { target := socks5.AddrFromStdAddrPort(rAddr) pkt := &packet{ pc: pc, diff --git a/listener/tuic/server.go b/listener/tuic/server.go index 565f258e..82f208cb 100644 --- a/listener/tuic/server.go +++ b/listener/tuic/server.go @@ -2,47 +2,29 @@ package tuic import ( "crypto/tls" - "encoding/json" "net" "strings" "time" "github.com/metacubex/quic-go" + "github.com/Dreamacro/clash/adapter/inbound" "github.com/Dreamacro/clash/common/sockopt" C "github.com/Dreamacro/clash/constant" - "github.com/Dreamacro/clash/adapter/inbound" + LC "github.com/Dreamacro/clash/listener/config" "github.com/Dreamacro/clash/log" "github.com/Dreamacro/clash/transport/socks5" "github.com/Dreamacro/clash/transport/tuic" ) -type TuicServer struct { - Enable bool - Listen string - Token []string - Certificate string - PrivateKey string - CongestionController string - MaxIdleTime int - AuthenticationTimeout int - ALPN []string - MaxUdpRelayPacketSize int -} - -func (t TuicServer) String() string { - b, _ := json.Marshal(t) - return string(b) -} - type Listener struct { closed bool - config TuicServer + config LC.TuicServer udpListeners []net.PacketConn servers []*tuic.Server } -func New(config TuicServer, tcpIn chan<- C.ConnContext, udpIn chan<- *C.PacketAdapter) (*Listener, error) { +func New(config LC.TuicServer, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) (*Listener, error) { cert, err := tls.LoadX509KeyPair(config.Certificate, config.PrivateKey) if err != nil { return nil, err @@ -140,6 +122,6 @@ func (l *Listener) Close() { } } -func (l *Listener) Config() TuicServer { +func (l *Listener) Config() LC.TuicServer { return l.config } diff --git a/listener/tunnel/udp.go b/listener/tunnel/udp.go index 2f8ddeb6..506e990f 100644 --- a/listener/tunnel/udp.go +++ b/listener/tunnel/udp.go @@ -34,7 +34,7 @@ func (l *PacketConn) Close() error { return l.conn.Close() } -func NewUDP(addr, target, proxy string, in chan<- *C.PacketAdapter) (*PacketConn, error) { +func NewUDP(addr, target, proxy string, in chan<- C.PacketAdapter) (*PacketConn, error) { l, err := net.ListenPacket("udp", addr) if err != nil { return nil, err @@ -69,7 +69,7 @@ func NewUDP(addr, target, proxy string, in chan<- *C.PacketAdapter) (*PacketConn return sl, nil } -func (l *PacketConn) handleUDP(pc net.PacketConn, in chan<- *C.PacketAdapter, buf []byte, addr net.Addr) { +func (l *PacketConn) handleUDP(pc net.PacketConn, in chan<- C.PacketAdapter, buf []byte, addr net.Addr) { packet := &packet{ pc: pc, rAddr: addr, diff --git a/tunnel/tunnel.go b/tunnel/tunnel.go index 6a0af92e..99fcb33b 100644 --- a/tunnel/tunnel.go +++ b/tunnel/tunnel.go @@ -26,7 +26,7 @@ import ( var ( tcpQueue = make(chan C.ConnContext, 200) - udpQueue = make(chan *C.PacketAdapter, 200) + udpQueue = make(chan C.PacketAdapter, 200) natTable = nat.New() rules []C.Rule subRules map[string][]C.Rule @@ -77,7 +77,7 @@ func TCPIn() chan<- C.ConnContext { } // UDPIn return fan-in udp queue -func UDPIn() chan<- *C.PacketAdapter { +func UDPIn() chan<- C.PacketAdapter { return udpQueue } @@ -217,7 +217,7 @@ func resolveMetadata(ctx C.PlainContext, metadata *C.Metadata) (proxy C.Proxy, r return } -func handleUDPConn(packet *C.PacketAdapter) { +func handleUDPConn(packet C.PacketAdapter) { metadata := packet.Metadata() if !metadata.Valid() { log.Warnln("[Metadata] not valid: %#v", metadata) @@ -325,7 +325,7 @@ func handleUDPConn(packet *C.PacketAdapter) { } oAddr := metadata.DstIP - go handleUDPToLocal(packet.UDPPacket, pc, key, oAddr, fAddr) + go handleUDPToLocal(packet, pc, key, oAddr, fAddr) natTable.Set(key, pc) handle()