From 62474e0ed6e6a3898d1e2e4bf095362b300a1b27 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Sun, 4 Dec 2022 22:08:20 +0800 Subject: [PATCH] fix: correct C.Metadata --- adapter/inbound/http.go | 4 ++-- adapter/inbound/https.go | 4 ++-- adapter/inbound/packet.go | 4 ++-- adapter/inbound/socket.go | 4 ++-- constant/metadata.go | 34 +++++++++++++++++----------------- listener/autoredir/tcp.go | 26 +++++++++++++------------- listener/http/client.go | 4 ++-- listener/http/proxy.go | 8 ++++---- listener/http/server.go | 26 +++++++++++++------------- listener/http/upgrade.go | 4 ++-- listener/inbound/base.go | 20 ++++++++++---------- listener/inbound/http.go | 2 +- listener/inbound/mixed.go | 4 ++-- listener/inbound/redir.go | 2 +- listener/inbound/socks.go | 4 ++-- listener/inbound/tproxy.go | 4 ++-- listener/mixed/mixed.go | 34 +++++++++++++++++----------------- listener/redir/tcp.go | 26 +++++++++++++------------- listener/socks/tcp.go | 36 ++++++++++++++++++------------------ listener/socks/udp.go | 26 +++++++++++++------------- listener/tproxy/tproxy.go | 26 +++++++++++++------------- listener/tproxy/udp.go | 18 +++++++++--------- tunnel/tunnel.go | 2 +- 23 files changed, 161 insertions(+), 161 deletions(-) diff --git a/adapter/inbound/http.go b/adapter/inbound/http.go index 4557d470..0637b39a 100644 --- a/adapter/inbound/http.go +++ b/adapter/inbound/http.go @@ -13,12 +13,12 @@ func NewHTTP(target socks5.Addr, source net.Addr, conn net.Conn) *context.ConnCo return NewHTTPWithInfos(target, source, conn, "", "") } -func NewHTTPWithInfos(target socks5.Addr, source net.Addr, conn net.Conn, inName, preferRulesName string) *context.ConnContext { +func NewHTTPWithInfos(target socks5.Addr, source net.Addr, conn net.Conn, inName, specialRules string) *context.ConnContext { metadata := parseSocksAddr(target) metadata.NetWork = C.TCP metadata.Type = C.HTTP metadata.InName = inName - metadata.PreferRulesName = preferRulesName + metadata.SpecialRules = specialRules if ip, port, err := parseAddr(source.String()); err == nil { metadata.SrcIP = ip metadata.SrcPort = port diff --git a/adapter/inbound/https.go b/adapter/inbound/https.go index 882802ef..dbc17835 100644 --- a/adapter/inbound/https.go +++ b/adapter/inbound/https.go @@ -13,10 +13,10 @@ func NewHTTPS(request *http.Request, conn net.Conn) *context.ConnContext { return NewHTTPSWithInfos(request, conn, "", "") } -func NewHTTPSWithInfos(request *http.Request, conn net.Conn, inName, preferRulesName string) *context.ConnContext { +func NewHTTPSWithInfos(request *http.Request, conn net.Conn, inName, specialRules string) *context.ConnContext { metadata := parseHTTPAddr(request) metadata.Type = C.HTTPS - metadata.PreferRulesName = preferRulesName + metadata.SpecialRules = specialRules metadata.InName = inName if ip, port, err := parseAddr(conn.RemoteAddr().String()); err == nil { metadata.SrcIP = ip diff --git a/adapter/inbound/packet.go b/adapter/inbound/packet.go index 524c8f52..a84a3008 100644 --- a/adapter/inbound/packet.go +++ b/adapter/inbound/packet.go @@ -16,12 +16,12 @@ 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, specialRules string) C.PacketAdapter { metadata := parseSocksAddr(target) metadata.NetWork = C.UDP metadata.Type = source metadata.InName = inName - metadata.PreferRulesName = preferRulesName + metadata.SpecialRules = specialRules if ip, port, err := parseAddr(packet.LocalAddr().String()); err == nil { metadata.SrcIP = ip metadata.SrcPort = port diff --git a/adapter/inbound/socket.go b/adapter/inbound/socket.go index 6bcdbced..34f31cf3 100644 --- a/adapter/inbound/socket.go +++ b/adapter/inbound/socket.go @@ -9,11 +9,11 @@ import ( "github.com/Dreamacro/clash/transport/socks5" ) -func NewSocketWithInfos(target socks5.Addr, conn net.Conn, source C.Type, inName , preferRulesName string) *context.ConnContext { +func NewSocketWithInfos(target socks5.Addr, conn net.Conn, source C.Type, inName, specialRules string) *context.ConnContext { metadata := parseSocksAddr(target) metadata.NetWork = C.TCP metadata.Type = source - metadata.PreferRulesName = preferRulesName + metadata.SpecialRules = specialRules metadata.InName = inName remoteAddr := conn.RemoteAddr() diff --git a/constant/metadata.go b/constant/metadata.go index 0102ba74..278304bd 100644 --- a/constant/metadata.go +++ b/constant/metadata.go @@ -117,23 +117,23 @@ func (t Type) MarshalJSON() ([]byte, error) { // Metadata is used to store connection address type Metadata struct { - NetWork NetWork `json:"network"` - Type Type `json:"type"` - SrcIP netip.Addr `json:"sourceIP"` - DstIP netip.Addr `json:"destinationIP"` - SrcPort string `json:"sourcePort"` - DstPort string `json:"destinationPort"` - InIP netip.Addr `json:"inboundIP"` - InPort string `json:"inboundPort"` - Host string `json:"host"` - DNSMode DNSMode `json:"dnsMode"` - Uid *uint32 `json:"uid"` - Process string `json:"process"` - ProcessPath string `json:"processPath"` - SpecialProxy string `json:"specialProxy"` - RemoteDst string `json:"remoteDestination"` - InName string `jsson:"-"` - PreferRulesName string + NetWork NetWork `json:"network"` + Type Type `json:"type"` + SrcIP netip.Addr `json:"sourceIP"` + DstIP netip.Addr `json:"destinationIP"` + SrcPort string `json:"sourcePort"` + DstPort string `json:"destinationPort"` + InIP netip.Addr `json:"inboundIP"` + InPort string `json:"inboundPort"` + Host string `json:"host"` + DNSMode DNSMode `json:"dnsMode"` + Uid *uint32 `json:"uid"` + Process string `json:"process"` + ProcessPath string `json:"processPath"` + SpecialProxy string `json:"specialProxy"` + RemoteDst string `json:"remoteDestination"` + InName string `json:"inboundName"` + SpecialRules string `json:"specialRules"` } func (m *Metadata) RemoteAddress() string { diff --git a/listener/autoredir/tcp.go b/listener/autoredir/tcp.go index 936bffb5..5e675540 100644 --- a/listener/autoredir/tcp.go +++ b/listener/autoredir/tcp.go @@ -11,12 +11,12 @@ import ( ) type Listener struct { - listener net.Listener - addr string - closed bool - name string - preferRulesName string - lookupFunc func(netip.AddrPort) (socks5.Addr, error) + listener net.Listener + addr string + closed bool + name string + specialRules string + lookupFunc func(netip.AddrPort) (socks5.Addr, error) } // RawAddress implements C.Listener @@ -58,23 +58,23 @@ func (l *Listener) handleRedir(conn net.Conn, in chan<- C.ConnContext) { _ = conn.(*net.TCPConn).SetKeepAlive(true) - in <- inbound.NewSocketWithInfos(target, conn, C.REDIR,l.name,l.preferRulesName) + in <- inbound.NewSocketWithInfos(target, conn, C.REDIR, l.name, l.specialRules) } 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, specialRules 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, - preferRulesName: preferRulesName, + listener: l, + addr: addr, + name: name, + specialRules: specialRules, } go func() { diff --git a/listener/http/client.go b/listener/http/client.go index 8b21f98b..64208e17 100644 --- a/listener/http/client.go +++ b/listener/http/client.go @@ -12,7 +12,7 @@ import ( "github.com/Dreamacro/clash/transport/socks5" ) -func newClient(source net.Addr,name,preferRulesName string, in chan<- C.ConnContext) *http.Client { +func newClient(source net.Addr, name, specialRules string, in chan<- C.ConnContext) *http.Client { return &http.Client{ Transport: &http.Transport{ // from http.DefaultTransport @@ -32,7 +32,7 @@ func newClient(source net.Addr,name,preferRulesName string, in chan<- C.ConnCont left, right := net.Pipe() - in <- inbound.NewHTTPWithInfos(dstAddr, source, right,name,preferRulesName) + in <- inbound.NewHTTPWithInfos(dstAddr, source, right, name, specialRules) return left, nil }, diff --git a/listener/http/proxy.go b/listener/http/proxy.go index 74d9d77e..afded98e 100644 --- a/listener/http/proxy.go +++ b/listener/http/proxy.go @@ -14,8 +14,8 @@ import ( "github.com/Dreamacro/clash/log" ) -func HandleConn(name, preferRulesName string, c net.Conn, in chan<- C.ConnContext, cache *cache.LruCache[string, bool]) { - client := newClient(c.RemoteAddr(), name, preferRulesName, in) +func HandleConn(name, specialRules string, c net.Conn, in chan<- C.ConnContext, cache *cache.LruCache[string, bool]) { + client := newClient(c.RemoteAddr(), name, specialRules, in) defer client.CloseIdleConnections() conn := N.NewBufferedConn(c) @@ -48,7 +48,7 @@ func HandleConn(name, preferRulesName string, c net.Conn, in chan<- C.ConnContex break // close connection } - in <- inbound.NewHTTPSWithInfos(request, conn, name, preferRulesName) + in <- inbound.NewHTTPSWithInfos(request, conn, name, specialRules) return // hijack connection } @@ -61,7 +61,7 @@ func HandleConn(name, preferRulesName string, c net.Conn, in chan<- C.ConnContex request.RequestURI = "" if isUpgradeRequest(request) { - handleUpgrade(name, preferRulesName, conn, request, in) + handleUpgrade(name, specialRules, conn, request, in) return // hijack connection } diff --git a/listener/http/server.go b/listener/http/server.go index 7bcc542f..fa51ba28 100644 --- a/listener/http/server.go +++ b/listener/http/server.go @@ -9,11 +9,11 @@ import ( ) type Listener struct { - listener net.Listener - addr string - closed bool - name string - preferRulesName string + listener net.Listener + addr string + closed bool + name string + specialRules string } // RawAddress implements C.Listener @@ -36,11 +36,11 @@ func New(addr string, in chan<- C.ConnContext) (*Listener, error) { 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, specialRules string, in chan<- C.ConnContext) (*Listener, error) { + return NewWithAuthenticate(addr, name, specialRules, in, true) } -func NewWithAuthenticate(addr, name, preferRulesName string, in chan<- C.ConnContext, authenticate bool) (*Listener, error) { +func NewWithAuthenticate(addr, name, specialRules 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.ConnCon } hl := &Listener{ - listener: l, - name: name, - preferRulesName: preferRulesName, - addr: addr, + listener: l, + name: name, + specialRules: specialRules, + addr: addr, } go func() { for { @@ -67,7 +67,7 @@ func NewWithAuthenticate(addr, name, preferRulesName string, in chan<- C.ConnCon } continue } - go HandleConn(hl.name, hl.preferRulesName, conn, in, c) + go HandleConn(hl.name, hl.specialRules, conn, in, c) } }() diff --git a/listener/http/upgrade.go b/listener/http/upgrade.go index b0f1e389..a4601995 100644 --- a/listener/http/upgrade.go +++ b/listener/http/upgrade.go @@ -25,7 +25,7 @@ func isUpgradeRequest(req *http.Request) bool { return false } -func handleUpgrade(name,preferRulesName string,conn net.Conn, request *http.Request, in chan<- C.ConnContext) { +func handleUpgrade(name, specialRules string, conn net.Conn, request *http.Request, in chan<- C.ConnContext) { defer conn.Close() removeProxyHeaders(request.Header) @@ -43,7 +43,7 @@ func handleUpgrade(name,preferRulesName string,conn net.Conn, request *http.Requ left, right := net.Pipe() - in <- inbound.NewHTTPWithInfos(dstAddr, conn.RemoteAddr(), right,name,preferRulesName) + in <- inbound.NewHTTPWithInfos(dstAddr, conn.RemoteAddr(), right, name, specialRules) var bufferedLeft *N.BufferedConn if request.TLS != nil { diff --git a/listener/inbound/base.go b/listener/inbound/base.go index eee3cdce..7428e716 100644 --- a/listener/inbound/base.go +++ b/listener/inbound/base.go @@ -10,11 +10,11 @@ import ( ) type Base struct { - config *BaseOption - name string - preferRulesName string - listenAddr netip.Addr - port int + config *BaseOption + name string + specialRules string + listenAddr netip.Addr + port int } func NewBase(options *BaseOption) (*Base, error) { @@ -26,11 +26,11 @@ func NewBase(options *BaseOption) (*Base, error) { return nil, err } return &Base{ - name: options.Name(), - listenAddr: addr, - preferRulesName: options.PreferRulesName, - port: options.Port, - config: options, + name: options.Name(), + listenAddr: addr, + specialRules: options.PreferRulesName, + port: options.Port, + config: options, }, nil } diff --git a/listener/inbound/http.go b/listener/inbound/http.go index cf373ed7..b6edb479 100644 --- a/listener/inbound/http.go +++ b/listener/inbound/http.go @@ -44,7 +44,7 @@ func (h *HTTP) Address() string { // Listen implements constant.InboundListener func (h *HTTP) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { var err error - h.l, err = http.NewWithInfos(h.RawAddress(), h.name, h.preferRulesName, tcpIn) + h.l, err = http.NewWithInfos(h.RawAddress(), h.name, h.specialRules, tcpIn) if err != nil { return err } diff --git a/listener/inbound/mixed.go b/listener/inbound/mixed.go index 222de4c5..f29caebb 100644 --- a/listener/inbound/mixed.go +++ b/listener/inbound/mixed.go @@ -52,12 +52,12 @@ func (m *Mixed) Address() string { // Listen implements constant.InboundListener func (m *Mixed) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { var err error - m.l, err = mixed.NewWithInfos(m.RawAddress(), m.name, m.preferRulesName, tcpIn) + m.l, err = mixed.NewWithInfos(m.RawAddress(), m.name, m.specialRules, tcpIn) if err != nil { return err } if m.udp { - m.lUDP, err = socks.NewUDPWithInfos(m.Address(), m.name, m.preferRulesName, udpIn) + m.lUDP, err = socks.NewUDPWithInfos(m.Address(), m.name, m.specialRules, udpIn) if err != nil { return err } diff --git a/listener/inbound/redir.go b/listener/inbound/redir.go index 1809c3bc..9f0f35b4 100644 --- a/listener/inbound/redir.go +++ b/listener/inbound/redir.go @@ -44,7 +44,7 @@ func (r *Redir) Address() string { // Listen implements constant.InboundListener func (r *Redir) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { var err error - r.l, err = redir.NewWithInfos(r.Address(), r.name, r.preferRulesName, tcpIn) + r.l, err = redir.NewWithInfos(r.Address(), r.name, r.specialRules, tcpIn) if err != nil { return err } diff --git a/listener/inbound/socks.go b/listener/inbound/socks.go index 10df3568..9727350b 100644 --- a/listener/inbound/socks.go +++ b/listener/inbound/socks.go @@ -70,11 +70,11 @@ func (s *Socks) Address() string { // Listen implements constant.InboundListener func (s *Socks) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { var err error - if s.stl, err = socks.NewWithInfos(s.RawAddress(), s.name, s.preferRulesName, tcpIn); err != nil { + if s.stl, err = socks.NewWithInfos(s.RawAddress(), s.name, s.specialRules, tcpIn); err != nil { return err } if s.udp { - if s.sul, err = socks.NewUDPWithInfos(s.RawAddress(), s.name, s.preferRulesName, udpIn); err != nil { + if s.sul, err = socks.NewUDPWithInfos(s.RawAddress(), s.name, s.specialRules, udpIn); err != nil { return err } } diff --git a/listener/inbound/tproxy.go b/listener/inbound/tproxy.go index ff5893be..c3e34033 100644 --- a/listener/inbound/tproxy.go +++ b/listener/inbound/tproxy.go @@ -51,13 +51,13 @@ func (t *TProxy) Address() string { // Listen implements constant.InboundListener func (t *TProxy) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { var err error - t.lTCP, err = tproxy.NewWithInfos(t.RawAddress(), t.name, t.preferRulesName, tcpIn) + t.lTCP, err = tproxy.NewWithInfos(t.RawAddress(), t.name, t.specialRules, tcpIn) if err != nil { return err } if t.udp { if t.lUDP != nil { - t.lUDP, err = tproxy.NewUDPWithInfos(t.Address(), t.name, t.preferRulesName, udpIn) + t.lUDP, err = tproxy.NewUDPWithInfos(t.Address(), t.name, t.specialRules, udpIn) if err != nil { return err } diff --git a/listener/mixed/mixed.go b/listener/mixed/mixed.go index fe42a14e..34b4b1af 100644 --- a/listener/mixed/mixed.go +++ b/listener/mixed/mixed.go @@ -14,12 +14,12 @@ import ( ) type Listener struct { - listener net.Listener - addr string - name string - preferRulesName string - cache *cache.LruCache[string, bool] - closed bool + listener net.Listener + addr string + name string + specialRules string + cache *cache.LruCache[string, bool] + closed bool } // RawAddress implements C.Listener @@ -42,18 +42,18 @@ func New(addr string, in chan<- C.ConnContext) (*Listener, error) { return NewWithInfos(addr, "DEFAULT-MIXED", "", in) } -func NewWithInfos(addr, name, preferRulesName string, in chan<- C.ConnContext) (*Listener, error) { +func NewWithInfos(addr, name, specialRules string, in chan<- C.ConnContext) (*Listener, error) { l, err := inbound.Listen("tcp", addr) if err != nil { return nil, err } ml := &Listener{ - listener: l, - addr: addr, - name: name, - preferRulesName: preferRulesName, - cache: cache.New[string, bool](cache.WithAge[string, bool](30)), + listener: l, + addr: addr, + name: name, + specialRules: specialRules, + cache: cache.New[string, bool](cache.WithAge[string, bool](30)), } go func() { for { @@ -64,14 +64,14 @@ func NewWithInfos(addr, name, preferRulesName string, in chan<- C.ConnContext) ( } continue } - go handleConn(ml.name, ml.preferRulesName, c, in, ml.cache) + go handleConn(ml.name, ml.specialRules, c, in, ml.cache) } }() return ml, nil } -func handleConn(name, preferRulesName string, conn net.Conn, in chan<- C.ConnContext, cache *cache.LruCache[string, bool]) { +func handleConn(name, specialRules string, conn net.Conn, in chan<- C.ConnContext, cache *cache.LruCache[string, bool]) { conn.(*net.TCPConn).SetKeepAlive(true) bufConn := N.NewBufferedConn(conn) @@ -82,10 +82,10 @@ func handleConn(name, preferRulesName string, conn net.Conn, in chan<- C.ConnCon switch head[0] { case socks4.Version: - socks.HandleSocks4(name, preferRulesName, bufConn, in) + socks.HandleSocks4(name, specialRules, bufConn, in) case socks5.Version: - socks.HandleSocks5(name, preferRulesName, bufConn, in) + socks.HandleSocks5(name, specialRules, bufConn, in) default: - http.HandleConn(name, preferRulesName, bufConn, in, cache) + http.HandleConn(name, specialRules, bufConn, in, cache) } } diff --git a/listener/redir/tcp.go b/listener/redir/tcp.go index a9e9b5b8..150f1da5 100644 --- a/listener/redir/tcp.go +++ b/listener/redir/tcp.go @@ -8,11 +8,11 @@ import ( ) type Listener struct { - listener net.Listener - addr string - closed bool - name string - preferRulesName string + listener net.Listener + addr string + closed bool + name string + specialRules string } // RawAddress implements C.Listener @@ -35,16 +35,16 @@ func New(addr string, in chan<- C.ConnContext) (*Listener, error) { return NewWithInfos(addr, "DEFAULT-REDIR", "", in) } -func NewWithInfos(addr, name, preferRulesName string, in chan<- C.ConnContext) (*Listener, error) { +func NewWithInfos(addr, name, specialRules 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, - preferRulesName: preferRulesName, + listener: l, + addr: addr, + name: name, + specialRules: specialRules, } go func() { @@ -56,18 +56,18 @@ func NewWithInfos(addr, name, preferRulesName string, in chan<- C.ConnContext) ( } continue } - go handleRedir(rl.name, rl.preferRulesName, c, in) + go handleRedir(rl.name, rl.specialRules, c, in) } }() return rl, nil } -func handleRedir(name, preferRulesName string, conn net.Conn, in chan<- C.ConnContext) { +func handleRedir(name, specialRules 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, specialRules) } diff --git a/listener/socks/tcp.go b/listener/socks/tcp.go index 30174b61..dc0aff8f 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 + specialRules string + name string } // RawAddress implements C.Listener @@ -40,17 +40,17 @@ func New(addr string, in chan<- C.ConnContext) (*Listener, error) { return NewWithInfos(addr, "DEFAULT-SOCKS", "", in) } -func NewWithInfos(addr, name, preferRulesName string, in chan<- C.ConnContext) (*Listener, error) { +func NewWithInfos(addr, name, specialRules 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, - preferRulesName: preferRulesName, + listener: l, + addr: addr, + name: name, + specialRules: specialRules, } go func() { for { @@ -61,14 +61,14 @@ func NewWithInfos(addr, name, preferRulesName string, in chan<- C.ConnContext) ( } continue } - go handleSocks(sl.name, sl.preferRulesName, c, in) + go handleSocks(sl.name, sl.specialRules, c, in) } }() return sl, nil } -func handleSocks(name, preferRulesName string, conn net.Conn, in chan<- C.ConnContext) { +func handleSocks(name, specialRules 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.ConnCo switch head[0] { case socks4.Version: - HandleSocks4(name, preferRulesName, bufConn, in) + HandleSocks4(name, specialRules, bufConn, in) case socks5.Version: - HandleSocks5(name, preferRulesName, bufConn, in) + HandleSocks5(name, specialRules, bufConn, in) default: conn.Close() } } -func HandleSocks4(name, preferRulesName string, conn net.Conn, in chan<- C.ConnContext) { +func HandleSocks4(name, specialRules 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, specialRules) } -func HandleSocks5(name, preferRulesName string, conn net.Conn, in chan<- C.ConnContext) { +func HandleSocks5(name, specialRules 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.ConnC io.Copy(io.Discard, conn) return } - in <- inbound.NewSocketWithInfos(target, conn, C.SOCKS5, name, preferRulesName) + in <- inbound.NewSocketWithInfos(target, conn, C.SOCKS5, name, specialRules) } diff --git a/listener/socks/udp.go b/listener/socks/udp.go index cde92c54..c7232d9d 100644 --- a/listener/socks/udp.go +++ b/listener/socks/udp.go @@ -12,11 +12,11 @@ import ( ) type UDPListener struct { - packetConn net.PacketConn - addr string - closed bool - name string - preferRulesName string + packetConn net.PacketConn + addr string + closed bool + name string + specialRules string } // RawAddress implements C.Listener @@ -39,7 +39,7 @@ 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, specialRules 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, - preferRulesName: preferRulesName, - name: name, + packetConn: l, + addr: addr, + specialRules: specialRules, + 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.specialRules, 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, specialRules 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, specialRules): default: } } diff --git a/listener/tproxy/tproxy.go b/listener/tproxy/tproxy.go index a1c53b53..857eb5e0 100644 --- a/listener/tproxy/tproxy.go +++ b/listener/tproxy/tproxy.go @@ -9,11 +9,11 @@ import ( ) type Listener struct { - listener net.Listener - addr string - closed bool - name string - preferRulesName string + listener net.Listener + addr string + closed bool + name string + specialRules string } // RawAddress implements C.Listener @@ -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, specialRules 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, specialRules) } func New(addr string, in chan<- C.ConnContext) (*Listener, error) { return NewWithInfos(addr, "DEFAULT-TPROXY", "", in) } -func NewWithInfos(addr, name, preferRulesName string, in chan<- C.ConnContext) (*Listener, error) { +func NewWithInfos(addr, name, specialRules string, in chan<- C.ConnContext) (*Listener, error) { l, err := net.Listen("tcp", addr) if err != nil { return nil, err @@ -60,10 +60,10 @@ func NewWithInfos(addr, name, preferRulesName string, in chan<- C.ConnContext) ( } rl := &Listener{ - listener: l, - addr: addr, - name: name, - preferRulesName: preferRulesName, + listener: l, + addr: addr, + name: name, + specialRules: specialRules, } go func() { @@ -75,7 +75,7 @@ func NewWithInfos(addr, name, preferRulesName string, in chan<- C.ConnContext) ( } continue } - go rl.handleTProxy(rl.name, rl.preferRulesName, c, in) + go rl.handleTProxy(rl.name, rl.specialRules, c, in) } }() diff --git a/listener/tproxy/udp.go b/listener/tproxy/udp.go index 9560a626..c25bf103 100644 --- a/listener/tproxy/udp.go +++ b/listener/tproxy/udp.go @@ -11,11 +11,11 @@ import ( ) type UDPListener struct { - packetConn net.PacketConn - addr string - closed bool - name string - preferRulesName string + packetConn net.PacketConn + addr string + closed bool + name string + specialRules string } // RawAddress implements C.Listener @@ -38,7 +38,7 @@ 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, specialRules string, in chan<- C.PacketAdapter) (*UDPListener, error) { l, err := net.ListenPacket("udp", addr) if err != nil { return nil, err @@ -83,14 +83,14 @@ func NewUDPWithInfos(addr, name, preferRulesName string, in chan<- C.PacketAdapt // try to unmap 4in6 address lAddr = netip.AddrPortFrom(lAddr.Addr().Unmap(), lAddr.Port()) } - handlePacketConn(rl.name, rl.preferRulesName, l, in, buf[:n], lAddr, rAddr) + handlePacketConn(rl.name, rl.specialRules, l, in, buf[:n], lAddr, rAddr) } }() return rl, nil } -func handlePacketConn(name, preferRulesName string, pc net.PacketConn, in chan<- C.PacketAdapter, buf []byte, lAddr, rAddr netip.AddrPort) { +func handlePacketConn(name, specialRules string, pc net.PacketConn, in chan<- C.PacketAdapter, buf []byte, lAddr, rAddr netip.AddrPort) { target := socks5.AddrFromStdAddrPort(rAddr) pkt := &packet{ pc: pc, @@ -98,7 +98,7 @@ func handlePacketConn(name, preferRulesName string, pc net.PacketConn, in chan<- buf: buf, } select { - case in <- inbound.NewPacketWithInfos(target, pkt, C.TPROXY, name, preferRulesName): + case in <- inbound.NewPacketWithInfos(target, pkt, C.TPROXY, name, specialRules): default: } } diff --git a/tunnel/tunnel.go b/tunnel/tunnel.go index 943bd917..64ed0325 100644 --- a/tunnel/tunnel.go +++ b/tunnel/tunnel.go @@ -508,7 +508,7 @@ func match(metadata *C.Metadata) (C.Proxy, C.Rule, error) { } func getRules(metadata *C.Metadata) []C.Rule { - if sr, ok := subRules[metadata.PreferRulesName]; ok { + if sr, ok := subRules[metadata.SpecialRules]; ok { return sr } else { return rules