diff --git a/adapter/inbound/default.go b/adapter/inbound/default.go index 8ac84234..7bc03c67 100644 --- a/adapter/inbound/default.go +++ b/adapter/inbound/default.go @@ -181,6 +181,7 @@ func (a *myInboundAdapter) loopUDPInThreadSafe() { buffer := buf.NewPacket() n, addr, err := a.udpConn.ReadFromUDPAddrPort(buffer.FreeBytes()) if err != nil { + buffer.Release() return } buffer.Truncate(n) diff --git a/adapter/outbound.go b/adapter/outbound.go index d12b3e95..0b0ea6ba 100644 --- a/adapter/outbound.go +++ b/adapter/outbound.go @@ -11,7 +11,7 @@ import ( type Outbound interface { Type() string Tag() string + N.Dialer NewConnection(ctx context.Context, conn net.Conn, destination M.Socksaddr) error NewPacketConnection(ctx context.Context, conn N.PacketConn, destination M.Socksaddr) error - N.Dialer } diff --git a/adapter/outbound/builder.go b/adapter/outbound/builder.go index 08841f6c..6f45be65 100644 --- a/adapter/outbound/builder.go +++ b/adapter/outbound/builder.go @@ -24,6 +24,8 @@ func New(router adapter.Router, logger log.Logger, index int, options option.Out switch options.Type { case C.TypeDirect: return NewDirect(router, outboundLogger, options.Tag, options.DirectOptions), nil + case C.TypeSocks: + return NewSocks(router, outboundLogger, options.Tag, options.SocksOptions) case C.TypeShadowsocks: return NewShadowsocks(router, outboundLogger, options.Tag, options.ShadowsocksOptions) default: diff --git a/adapter/outbound/default.go b/adapter/outbound/default.go index ff246521..527e8223 100644 --- a/adapter/outbound/default.go +++ b/adapter/outbound/default.go @@ -44,7 +44,7 @@ func (d *defaultDialer) DialContext(ctx context.Context, network string, address return d.Dialer.DialContext(ctx, network, address.String()) } -func (d *defaultDialer) ListenPacket(ctx context.Context) (net.PacketConn, error) { +func (d *defaultDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) { return d.ListenConfig.ListenPacket(ctx, "udp", "") } @@ -105,12 +105,12 @@ func (d *lazyDialer) DialContext(ctx context.Context, network string, destinatio return dialer.DialContext(ctx, network, destination) } -func (d *lazyDialer) ListenPacket(ctx context.Context) (net.PacketConn, error) { +func (d *lazyDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) { dialer, err := d.Dialer() if err != nil { return nil, err } - return dialer.ListenPacket(ctx) + return dialer.ListenPacket(ctx, destination) } func CopyEarlyConn(ctx context.Context, conn net.Conn, serverConn net.Conn) error { diff --git a/adapter/outbound/direct.go b/adapter/outbound/direct.go index 5eef542d..3d4fbc36 100644 --- a/adapter/outbound/direct.go +++ b/adapter/outbound/direct.go @@ -63,9 +63,9 @@ func (d *Direct) DialContext(ctx context.Context, network string, destination M. return d.dialer.DialContext(ctx, network, destination) } -func (d *Direct) ListenPacket(ctx context.Context) (net.PacketConn, error) { +func (d *Direct) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) { d.logger.WithContext(ctx).Info("outbound packet connection") - return d.dialer.ListenPacket(ctx) + return d.dialer.ListenPacket(ctx, destination) } func (d *Direct) NewConnection(ctx context.Context, conn net.Conn, destination M.Socksaddr) error { @@ -77,7 +77,7 @@ func (d *Direct) NewConnection(ctx context.Context, conn net.Conn, destination M } func (d *Direct) NewPacketConnection(ctx context.Context, conn N.PacketConn, destination M.Socksaddr) error { - outConn, err := d.ListenPacket(ctx) + outConn, err := d.ListenPacket(ctx, destination) if err != nil { return err } diff --git a/adapter/outbound/shadowsocks.go b/adapter/outbound/shadowsocks.go index 84f1fd99..38a64d3a 100644 --- a/adapter/outbound/shadowsocks.go +++ b/adapter/outbound/shadowsocks.go @@ -47,22 +47,6 @@ func NewShadowsocks(router adapter.Router, logger log.Logger, tag string, option return outbound, nil } -func (o *Shadowsocks) NewConnection(ctx context.Context, conn net.Conn, destination M.Socksaddr) error { - serverConn, err := o.DialContext(ctx, "tcp", destination) - if err != nil { - return err - } - return CopyEarlyConn(ctx, conn, serverConn) -} - -func (o *Shadowsocks) NewPacketConnection(ctx context.Context, conn N.PacketConn, destination M.Socksaddr) error { - serverConn, err := o.ListenPacket(ctx) - if err != nil { - return err - } - return bufio.CopyPacketConn(ctx, conn, bufio.NewPacketConn(serverConn)) -} - func (o *Shadowsocks) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) { switch network { case C.NetworkTCP: @@ -84,11 +68,27 @@ func (o *Shadowsocks) DialContext(ctx context.Context, network string, destinati } } -func (o *Shadowsocks) ListenPacket(ctx context.Context) (net.PacketConn, error) { +func (o *Shadowsocks) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) { o.logger.WithContext(ctx).Info("outbound packet connection to ", o.serverAddr) - outConn, err := o.dialer.ListenPacket(ctx) + outConn, err := o.dialer.ListenPacket(ctx, destination) if err != nil { return nil, err } return o.method.DialPacketConn(&bufio.BindPacketConn{PacketConn: outConn, Addr: o.serverAddr.UDPAddr()}), nil } + +func (o *Shadowsocks) NewConnection(ctx context.Context, conn net.Conn, destination M.Socksaddr) error { + serverConn, err := o.DialContext(ctx, "tcp", destination) + if err != nil { + return err + } + return CopyEarlyConn(ctx, conn, serverConn) +} + +func (o *Shadowsocks) NewPacketConnection(ctx context.Context, conn N.PacketConn, destination M.Socksaddr) error { + serverConn, err := o.ListenPacket(ctx, destination) + if err != nil { + return err + } + return bufio.CopyPacketConn(ctx, conn, bufio.NewPacketConn(serverConn)) +} diff --git a/adapter/outbound/socks.go b/adapter/outbound/socks.go new file mode 100644 index 00000000..a9ebab75 --- /dev/null +++ b/adapter/outbound/socks.go @@ -0,0 +1,78 @@ +package outbound + +import ( + "context" + "net" + + "github.com/sagernet/sing-box/adapter" + C "github.com/sagernet/sing-box/constant" + "github.com/sagernet/sing-box/log" + "github.com/sagernet/sing-box/option" + "github.com/sagernet/sing/common/bufio" + M "github.com/sagernet/sing/common/metadata" + N "github.com/sagernet/sing/common/network" + "github.com/sagernet/sing/protocol/socks" +) + +var _ adapter.Outbound = (*Socks)(nil) + +type Socks struct { + myOutboundAdapter + client *socks.Client +} + +func NewSocks(router adapter.Router, logger log.Logger, tag string, options option.SocksOutboundOptions) (*Socks, error) { + dialer := NewDialer(router, options.DialerOptions) + var version socks.Version + var err error + if options.Version != "" { + version, err = socks.ParseVersion(options.Version) + } else { + version = socks.Version5 + } + if err != nil { + return nil, err + } + return &Socks{ + myOutboundAdapter{ + protocol: C.TypeSocks, + logger: logger, + tag: tag, + dialer: dialer, + }, + socks.NewClient(dialer, M.ParseSocksaddrHostPort(options.Server, options.ServerPort), version, options.Username, options.Password), + }, nil +} + +func (h *Socks) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) { + switch network { + case C.NetworkTCP: + h.logger.WithContext(ctx).Info("outbound connection to ", destination) + case C.NetworkUDP: + h.logger.WithContext(ctx).Info("outbound packet connection to ", destination) + default: + panic("unknown network " + network) + } + return h.client.DialContext(ctx, network, destination) +} + +func (h *Socks) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) { + h.logger.WithContext(ctx).Info("outbound packet connection to ", destination) + return h.client.ListenPacket(ctx, destination) +} + +func (h *Socks) NewConnection(ctx context.Context, conn net.Conn, destination M.Socksaddr) error { + outConn, err := h.DialContext(ctx, "tcp", destination) + if err != nil { + return err + } + return bufio.CopyConn(ctx, conn, outConn) +} + +func (h *Socks) NewPacketConnection(ctx context.Context, conn N.PacketConn, destination M.Socksaddr) error { + outConn, err := h.ListenPacket(ctx, destination) + if err != nil { + return err + } + return bufio.CopyPacketConn(ctx, conn, bufio.NewPacketConn(outConn)) +} diff --git a/go.mod b/go.mod index 4798f188..f66ef16f 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/goccy/go-json v0.9.8 github.com/logrusorgru/aurora v2.0.3+incompatible github.com/oschwald/geoip2-golang v1.7.0 - github.com/sagernet/sing v0.0.0-20220703025722-d002d5ba3ba5 + github.com/sagernet/sing v0.0.0-20220703051339-f128942ffe12 github.com/sagernet/sing-shadowsocks v0.0.0-20220701084835-2208da1d8649 github.com/sirupsen/logrus v1.8.1 github.com/spf13/cobra v1.5.0 diff --git a/go.sum b/go.sum index a7c131dc..9668b73c 100644 --- a/go.sum +++ b/go.sum @@ -20,8 +20,8 @@ github.com/oschwald/maxminddb-golang v1.9.0/go.mod h1:TK+s/Z2oZq0rSl4PSeAEoP0bgm github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/sagernet/sing v0.0.0-20220703025722-d002d5ba3ba5 h1:0oNnpN43Z6TXDdea5tbKIXXJ62yJqTpOW4IyQSgN3KY= -github.com/sagernet/sing v0.0.0-20220703025722-d002d5ba3ba5/go.mod h1:3ZmoGNg/nNJTyHAZFNRSPaXpNIwpDvyIiAUd0KIWV5c= +github.com/sagernet/sing v0.0.0-20220703051339-f128942ffe12 h1:HN3IoHyR2tpI4WwBVSDo5VJ0tKrQKqltkjlHTG9vbdo= +github.com/sagernet/sing v0.0.0-20220703051339-f128942ffe12/go.mod h1:3ZmoGNg/nNJTyHAZFNRSPaXpNIwpDvyIiAUd0KIWV5c= github.com/sagernet/sing-shadowsocks v0.0.0-20220701084835-2208da1d8649 h1:whNDUGOAX5GPZkSy4G3Gv9QyIgk5SXRyjkRuP7ohF8k= github.com/sagernet/sing-shadowsocks v0.0.0-20220701084835-2208da1d8649/go.mod h1:MuyT+9fEPjvauAv0fSE0a6Q+l0Tv2ZrAafTkYfnxBFw= github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= diff --git a/option/outbound.go b/option/outbound.go index 78a3ff4e..277e0a3b 100644 --- a/option/outbound.go +++ b/option/outbound.go @@ -10,6 +10,7 @@ type _Outbound struct { Tag string `json:"tag,omitempty"` Type string `json:"type,omitempty"` DirectOptions DirectOutboundOptions `json:"-"` + SocksOptions SocksOutboundOptions `json:"-"` ShadowsocksOptions ShadowsocksOutboundOptions `json:"-"` } @@ -20,6 +21,8 @@ func (h Outbound) MarshalJSON() ([]byte, error) { switch h.Type { case "direct": v = h.DirectOptions + case "socks": + v = h.SocksOptions case "shadowsocks": v = h.ShadowsocksOptions default: @@ -37,6 +40,8 @@ func (h *Outbound) UnmarshalJSON(bytes []byte) error { switch h.Type { case "direct": v = &h.DirectOptions + case "socks": + v = &h.SocksOptions case "shadowsocks": v = &h.ShadowsocksOptions default: @@ -73,6 +78,14 @@ func (o ServerOptions) Build() M.Socksaddr { return M.ParseSocksaddrHostPort(o.Server, o.ServerPort) } +type SocksOutboundOptions struct { + DialerOptions + ServerOptions + Version string `json:"version,omitempty"` + Username string `json:"username,omitempty"` + Password string `json:"password,omitempty"` +} + type ShadowsocksOutboundOptions struct { DialerOptions ServerOptions