diff --git a/go.mod b/go.mod index dff3f44..e4ca960 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/sagernet/sing go 1.18 require ( - github.com/cloudflare/cloudflare-go v0.37.0 + github.com/cloudflare/cloudflare-go v0.38.0 github.com/openacid/low v0.1.21 github.com/oschwald/geoip2-golang v1.7.0 github.com/seiflotfy/cuckoofilter v0.0.0-20220411075957-e3b120b3f5fb @@ -14,9 +14,9 @@ require ( github.com/v2fly/ss-bloomring v0.0.0-20210312155135-28617310f63e github.com/v2fly/v2ray-core/v5 v5.0.3 github.com/vishvananda/netlink v1.1.0 - golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 - golang.org/x/net v0.0.0-20220420153159-1850ba15e1be - golang.org/x/sys v0.0.0-20220412211240-33da011f77ad + golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f + golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 + golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 golang.zx2c4.com/wireguard v0.0.0-20220407013110-ef5c587f782d google.golang.org/protobuf v1.28.0 lukechampine.com/blake3 v1.1.7 diff --git a/go.sum b/go.sum index 213e31b..81212fe 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,6 @@ github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk= -github.com/cloudflare/cloudflare-go v0.37.0 h1:8LUyAtev5PZ92Wzxzsm9jOQj0ZTz3BBc5F1ISfwgxvk= -github.com/cloudflare/cloudflare-go v0.37.0/go.mod h1:EfqOgbgOTf9U+67Z5MR2t/MuVj5OjLlXs+dN6DBh6C8= +github.com/cloudflare/cloudflare-go v0.38.0 h1:Qf/oVlDmQgb4RfCxqkR0hJoEUQKHvDE4A09MHWssa/w= +github.com/cloudflare/cloudflare-go v0.38.0/go.mod h1:tWDVF03nA9VS9TvFRTrOeY78uSFQzS+n429EcBZszzg= github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -71,15 +71,15 @@ github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYp github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17pCcGlemwknint6hfoeCVQrEMVwxRLRjXpq+BU= github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74 h1:gga7acRE695APm9hlsSMoOoE65U4/TcqNj90mc69Rlg= github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0= -golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 h1:kUhD7nTDoI3fVd9G4ORWrbV5NY0liEs/Jg2pv5f+bBA= -golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/net v0.0.0-20220420153159-1850ba15e1be h1:yx80W7nvY5ySWpaU8UWaj5o9e23YgO9BRhQol7Lc+JI= -golang.org/x/net v0.0.0-20220420153159-1850ba15e1be/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= +golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f h1:OeJjE6G4dgCY4PIXvIRQbE8+RX+uXZyGhUy/ksMGJoc= +golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 h1:HVyaeDAYux4pnY+D/SiwmLOR36ewZ4iGQIIrtnuCjFA= +golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= golang.org/x/sys v0.0.0-20190606203320-7fc4e5ec1444/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200217220822-9197077df867/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20220412211240-33da011f77ad h1:ntjMns5wyP/fN65tdBD4g8J5w8n015+iIIs9rtjXkY0= -golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 h1:xHms4gcpe1YE7A3yIllJXP16CMAGuqwO2lX1mTyyRRc= +golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/time v0.0.0-20220411224347-583f2d630306 h1:+gHMid33q6pen7kv9xvT+JRinntgeXO2AeZVd0AWD3w= diff --git a/protocol/http/listener.go b/protocol/http/listener.go index f880a2b..cb6b46d 100644 --- a/protocol/http/listener.go +++ b/protocol/http/listener.go @@ -55,6 +55,7 @@ func HandleRequest(ctx context.Context, request *http.Request, conn net.Conn, au if err != nil { return E.Cause(err, "write http response") } + metadata.Protocol = "http" metadata.Destination = destination return handler.NewConnection(ctx, conn, metadata) } diff --git a/protocol/shadowsocks/none.go b/protocol/shadowsocks/none.go index 7bf1a60..276c52d 100644 --- a/protocol/shadowsocks/none.go +++ b/protocol/shadowsocks/none.go @@ -1,13 +1,16 @@ package shadowsocks import ( + "context" "io" "net" + "net/netip" "sync" "github.com/sagernet/sing/common" "github.com/sagernet/sing/common/buf" M "github.com/sagernet/sing/common/metadata" + "github.com/sagernet/sing/common/udpnat" "github.com/sagernet/sing/protocol/socks" ) @@ -149,3 +152,61 @@ func (c *nonePacketConn) WritePacket(buffer *buf.Buffer, addrPort *M.AddrPort) e buffer = buffer.WriteBufferAtFirst(header) return common.Error(buffer.WriteTo(c)) } + +type NoneService struct { + handler Handler + udp udpnat.Service[netip.AddrPort] +} + +func NewNoneService(udpTimeout int64, handler Handler) Service { + s := &NoneService{ + handler: handler, + } + s.udp = udpnat.New[netip.AddrPort](udpTimeout, s) + return s +} + +func (s *NoneService) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error { + destination, err := socks.AddressSerializer.ReadAddrPort(conn) + if err != nil { + return err + } + metadata.Protocol = "shadowsocks" + metadata.Destination = destination + return s.handler.NewConnection(ctx, conn, metadata) +} + +func (s *NoneService) NewPacket(conn socks.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error { + destination, err := socks.AddressSerializer.ReadAddrPort(buffer) + if err != nil { + return err + } + metadata.Protocol = "shadowsocks" + metadata.Destination = destination + s.udp.NewPacket(metadata.Source.AddrPort(), func() socks.PacketWriter { + return &nonePacketWriter{conn, metadata.Source} + }, buffer, metadata) + return nil +} + +type nonePacketWriter struct { + socks.PacketConn + sourceAddr *M.AddrPort +} + +func (s *nonePacketWriter) WritePacket(buffer *buf.Buffer, destination *M.AddrPort) error { + header := buf.With(buffer.ExtendHeader(socks.AddressSerializer.AddrPortLen(destination))) + err := socks.AddressSerializer.WriteAddrPort(header, destination) + if err != nil { + return err + } + return s.PacketConn.WritePacket(buffer, s.sourceAddr) +} + +func (s *NoneService) NewPacketConnection(conn socks.PacketConn, metadata M.Metadata) error { + return s.handler.NewPacketConnection(conn, metadata) +} + +func (s *NoneService) HandleError(err error) { + s.handler.HandleError(err) +} diff --git a/protocol/shadowsocks/service.go b/protocol/shadowsocks/service.go index 8a4ebd5..9b29055 100644 --- a/protocol/shadowsocks/service.go +++ b/protocol/shadowsocks/service.go @@ -1,14 +1,8 @@ package shadowsocks import ( - "context" - "net" - "net/netip" - - "github.com/sagernet/sing/common/buf" E "github.com/sagernet/sing/common/exceptions" M "github.com/sagernet/sing/common/metadata" - "github.com/sagernet/sing/common/udpnat" "github.com/sagernet/sing/protocol/socks" ) @@ -22,61 +16,3 @@ type Handler interface { socks.UDPConnectionHandler E.Handler } - -type NoneService struct { - handler Handler - udp udpnat.Service[netip.AddrPort] -} - -func NewNoneService(udpTimeout int64, handler Handler) Service { - s := &NoneService{ - handler: handler, - } - s.udp = udpnat.New[netip.AddrPort](udpTimeout, s) - return s -} - -func (s *NoneService) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error { - destination, err := socks.AddressSerializer.ReadAddrPort(conn) - if err != nil { - return err - } - metadata.Protocol = "shadowsocks" - metadata.Destination = destination - return s.handler.NewConnection(ctx, conn, metadata) -} - -func (s *NoneService) NewPacket(conn socks.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error { - destination, err := socks.AddressSerializer.ReadAddrPort(buffer) - if err != nil { - return err - } - metadata.Protocol = "shadowsocks" - metadata.Destination = destination - s.udp.NewPacket(metadata.Source.AddrPort(), func() socks.PacketWriter { - return &serverPacketWriter{conn, metadata.Source} - }, buffer, metadata) - return nil -} - -type serverPacketWriter struct { - socks.PacketConn - sourceAddr *M.AddrPort -} - -func (s *serverPacketWriter) WritePacket(buffer *buf.Buffer, destination *M.AddrPort) error { - header := buf.With(buffer.ExtendHeader(socks.AddressSerializer.AddrPortLen(destination))) - err := socks.AddressSerializer.WriteAddrPort(header, destination) - if err != nil { - return err - } - return s.PacketConn.WritePacket(buffer, s.sourceAddr) -} - -func (s *NoneService) NewPacketConnection(conn socks.PacketConn, metadata M.Metadata) error { - return s.handler.NewPacketConnection(conn, metadata) -} - -func (s *NoneService) HandleError(err error) { - s.handler.HandleError(err) -}