From 18e3f43df31befd2a4f04a361f865a6d85a55c93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sun, 3 Jul 2022 20:59:25 +0800 Subject: [PATCH] Refactor struct & Add override dialer options --- adapter/inbound.go | 2 +- adapter/outbound/default.go | 138 ------------------ {adapter/inbound => inbound}/builder.go | 0 {adapter/inbound => inbound}/default.go | 14 +- {adapter/inbound => inbound}/direct.go | 0 {adapter/inbound => inbound}/http.go | 0 {adapter/inbound => inbound}/mixed.go | 0 {adapter/inbound => inbound}/shadowsocks.go | 0 {adapter/inbound => inbound}/socks.go | 0 option/network.go | 3 +- option/outbound.go | 20 ++- {adapter/outbound => outbound}/builder.go | 0 outbound/default.go | 54 +++++++ outbound/dialer/default.go | 47 ++++++ outbound/dialer/detour.go | 52 +++++++ outbound/dialer/dialer.go | 21 +++ outbound/dialer/override.go | 68 +++++++++ {adapter/outbound => outbound}/direct.go | 5 +- {adapter/outbound => outbound}/shadowsocks.go | 9 +- {adapter/outbound => outbound}/socks.go | 5 +- {adapter/route => route}/router.go | 0 {adapter/route => route}/rule.go | 0 {adapter/route => route}/rule_cidr.go | 0 {adapter/route => route}/rule_domain.go | 0 .../route => route}/rule_domain_keyword.go | 0 {adapter/route => route}/rule_geoip.go | 0 {adapter/route => route}/rule_inbound.go | 0 {adapter/route => route}/rule_ipversion.go | 0 {adapter/route => route}/rule_logical.go | 0 {adapter/route => route}/rule_network.go | 0 {adapter/route => route}/rule_port.go | 0 {adapter/route => route}/rule_protocol.go | 0 service.go | 10 +- 33 files changed, 282 insertions(+), 166 deletions(-) delete mode 100644 adapter/outbound/default.go rename {adapter/inbound => inbound}/builder.go (100%) rename {adapter/inbound => inbound}/default.go (94%) rename {adapter/inbound => inbound}/direct.go (100%) rename {adapter/inbound => inbound}/http.go (100%) rename {adapter/inbound => inbound}/mixed.go (100%) rename {adapter/inbound => inbound}/shadowsocks.go (100%) rename {adapter/inbound => inbound}/socks.go (100%) rename {adapter/outbound => outbound}/builder.go (100%) create mode 100644 outbound/default.go create mode 100644 outbound/dialer/default.go create mode 100644 outbound/dialer/detour.go create mode 100644 outbound/dialer/dialer.go create mode 100644 outbound/dialer/override.go rename {adapter/outbound => outbound}/direct.go (93%) rename {adapter/outbound => outbound}/shadowsocks.go (89%) rename {adapter/outbound => outbound}/socks.go (93%) rename {adapter/route => route}/router.go (100%) rename {adapter/route => route}/rule.go (100%) rename {adapter/route => route}/rule_cidr.go (100%) rename {adapter/route => route}/rule_domain.go (100%) rename {adapter/route => route}/rule_domain_keyword.go (100%) rename {adapter/route => route}/rule_geoip.go (100%) rename {adapter/route => route}/rule_inbound.go (100%) rename {adapter/route => route}/rule_ipversion.go (100%) rename {adapter/route => route}/rule_logical.go (100%) rename {adapter/route => route}/rule_network.go (100%) rename {adapter/route => route}/rule_port.go (100%) rename {adapter/route => route}/rule_protocol.go (100%) diff --git a/adapter/inbound.go b/adapter/inbound.go index 136f2730..7401f64c 100644 --- a/adapter/inbound.go +++ b/adapter/inbound.go @@ -22,5 +22,5 @@ type InboundContext struct { SourceGeoIPCode string GeoIPCode string - ProcessPath string + // ProcessPath string } diff --git a/adapter/outbound/default.go b/adapter/outbound/default.go deleted file mode 100644 index 527e8223..00000000 --- a/adapter/outbound/default.go +++ /dev/null @@ -1,138 +0,0 @@ -package outbound - -import ( - "context" - "net" - "runtime" - "sync" - "time" - - "github.com/database64128/tfo-go" - "github.com/sagernet/sing-box/adapter" - "github.com/sagernet/sing-box/log" - "github.com/sagernet/sing-box/option" - "github.com/sagernet/sing/common" - "github.com/sagernet/sing/common/buf" - "github.com/sagernet/sing/common/bufio" - "github.com/sagernet/sing/common/control" - E "github.com/sagernet/sing/common/exceptions" - M "github.com/sagernet/sing/common/metadata" - N "github.com/sagernet/sing/common/network" -) - -type myOutboundAdapter struct { - protocol string - logger log.Logger - tag string - dialer N.Dialer -} - -func (a *myOutboundAdapter) Type() string { - return a.protocol -} - -func (a *myOutboundAdapter) Tag() string { - return a.tag -} - -type defaultDialer struct { - tfo.Dialer - net.ListenConfig -} - -func (d *defaultDialer) DialContext(ctx context.Context, network string, address M.Socksaddr) (net.Conn, error) { - return d.Dialer.DialContext(ctx, network, address.String()) -} - -func (d *defaultDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) { - return d.ListenConfig.ListenPacket(ctx, "udp", "") -} - -func newDialer(options option.DialerOptions) N.Dialer { - var dialer net.Dialer - var listener net.ListenConfig - if options.BindInterface != "" { - dialer.Control = control.Append(dialer.Control, control.BindToInterface(options.BindInterface)) - listener.Control = control.Append(listener.Control, control.BindToInterface(options.BindInterface)) - } - if options.RoutingMark != 0 { - dialer.Control = control.Append(dialer.Control, control.RoutingMark(options.RoutingMark)) - listener.Control = control.Append(listener.Control, control.RoutingMark(options.RoutingMark)) - } - if options.ReuseAddr { - listener.Control = control.Append(listener.Control, control.ReuseAddr()) - } - if options.ConnectTimeout != 0 { - dialer.Timeout = time.Duration(options.ConnectTimeout) * time.Second - } - return &defaultDialer{tfo.Dialer{Dialer: dialer, DisableTFO: !options.TCPFastOpen}, listener} -} - -type lazyDialer struct { - router adapter.Router - options option.DialerOptions - dialer N.Dialer - initOnce sync.Once - initErr error -} - -func NewDialer(router adapter.Router, options option.DialerOptions) N.Dialer { - if options.Detour == "" { - return newDialer(options) - } - return &lazyDialer{ - router: router, - options: options, - } -} - -func (d *lazyDialer) Dialer() (N.Dialer, error) { - d.initOnce.Do(func() { - var loaded bool - d.dialer, loaded = d.router.Outbound(d.options.Detour) - if !loaded { - d.initErr = E.New("outbound detour not found: ", d.options.Detour) - } - }) - return d.dialer, d.initErr -} - -func (d *lazyDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) { - dialer, err := d.Dialer() - if err != nil { - return nil, err - } - return dialer.DialContext(ctx, network, destination) -} - -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, destination) -} - -func CopyEarlyConn(ctx context.Context, conn net.Conn, serverConn net.Conn) error { - _payload := buf.StackNew() - payload := common.Dup(_payload) - err := conn.SetReadDeadline(time.Now().Add(100 * time.Millisecond)) - if err != nil { - return err - } - _, err = payload.ReadFrom(conn) - if err != nil && !E.IsTimeout(err) { - return E.Cause(err, "read payload") - } - err = conn.SetReadDeadline(time.Time{}) - if err != nil { - payload.Release() - return err - } - _, err = serverConn.Write(payload.Bytes()) - if err != nil { - return E.Cause(err, "client handshake") - } - runtime.KeepAlive(_payload) - return bufio.CopyConn(ctx, conn, serverConn) -} diff --git a/adapter/inbound/builder.go b/inbound/builder.go similarity index 100% rename from adapter/inbound/builder.go rename to inbound/builder.go diff --git a/adapter/inbound/default.go b/inbound/default.go similarity index 94% rename from adapter/inbound/default.go rename to inbound/default.go index 7bc03c67..f17a5983 100644 --- a/adapter/inbound/default.go +++ b/inbound/default.go @@ -59,9 +59,9 @@ func (a *myInboundAdapter) Start() error { var tcpListener *net.TCPListener var err error if !a.listenOptions.TCPFastOpen { - tcpListener, err = net.ListenTCP(M.NetworkFromNetAddr("tcp", bindAddr.Addr), bindAddr.TCPAddr()) + tcpListener, err = net.ListenTCP(M.NetworkFromNetAddr(C.NetworkTCP, bindAddr.Addr), bindAddr.TCPAddr()) } else { - tcpListener, err = tfo.ListenTCP(M.NetworkFromNetAddr("tcp", bindAddr.Addr), bindAddr.TCPAddr()) + tcpListener, err = tfo.ListenTCP(M.NetworkFromNetAddr(C.NetworkTCP, bindAddr.Addr), bindAddr.TCPAddr()) } if err != nil { return err @@ -71,7 +71,7 @@ func (a *myInboundAdapter) Start() error { listenAddr = tcpListener.Addr() } if common.Contains(a.network, C.NetworkUDP) { - udpConn, err := net.ListenUDP(M.NetworkFromNetAddr("udp", bindAddr.Addr), bindAddr.UDPAddr()) + udpConn, err := net.ListenUDP(M.NetworkFromNetAddr(C.NetworkUDP, bindAddr.Addr), bindAddr.UDPAddr()) if err != nil { return err } @@ -136,7 +136,7 @@ func (a *myInboundAdapter) loopTCPIn() { ctx := log.ContextWithID(a.ctx) var metadata adapter.InboundContext metadata.Inbound = a.tag - metadata.Network = "tcp" + metadata.Network = C.NetworkTCP metadata.Source = M.SocksaddrFromNet(conn.RemoteAddr()) a.logger.WithContext(ctx).Info("inbound connection from ", metadata.Source) hErr := a.connHandler.NewConnection(ctx, conn, metadata) @@ -165,7 +165,7 @@ func (a *myInboundAdapter) loopUDPIn() { buffer.Truncate(n) var metadata adapter.InboundContext metadata.Inbound = a.tag - metadata.Network = "udp" + metadata.Network = C.NetworkUDP metadata.Source = M.SocksaddrFromNetIP(addr) err = a.packetHandler.NewPacket(a.ctx, packetService, buffer, metadata) if err != nil { @@ -187,7 +187,7 @@ func (a *myInboundAdapter) loopUDPInThreadSafe() { buffer.Truncate(n) var metadata adapter.InboundContext metadata.Inbound = a.tag - metadata.Network = "udp" + metadata.Network = C.NetworkUDP metadata.Source = M.SocksaddrFromNetIP(addr) err = a.packetHandler.NewPacket(a.ctx, packetService, buffer, metadata) if err != nil { @@ -235,7 +235,7 @@ func (a *myInboundAdapter) NewError(ctx context.Context, err error) { func (a *myInboundAdapter) writePacket(buffer *buf.Buffer, destination M.Socksaddr) error { defer buffer.Release() if destination.IsFqdn() { - udpAddr, err := net.ResolveUDPAddr("udp", destination.String()) + udpAddr, err := net.ResolveUDPAddr(C.NetworkUDP, destination.String()) if err != nil { return err } diff --git a/adapter/inbound/direct.go b/inbound/direct.go similarity index 100% rename from adapter/inbound/direct.go rename to inbound/direct.go diff --git a/adapter/inbound/http.go b/inbound/http.go similarity index 100% rename from adapter/inbound/http.go rename to inbound/http.go diff --git a/adapter/inbound/mixed.go b/inbound/mixed.go similarity index 100% rename from adapter/inbound/mixed.go rename to inbound/mixed.go diff --git a/adapter/inbound/shadowsocks.go b/inbound/shadowsocks.go similarity index 100% rename from adapter/inbound/shadowsocks.go rename to inbound/shadowsocks.go diff --git a/adapter/inbound/socks.go b/inbound/socks.go similarity index 100% rename from adapter/inbound/socks.go rename to inbound/socks.go diff --git a/option/network.go b/option/network.go index 6e66a672..8d7181c8 100644 --- a/option/network.go +++ b/option/network.go @@ -21,7 +21,8 @@ func (v *NetworkList) UnmarshalJSON(data []byte) error { } for _, networkName := range networkList { switch networkName { - case "tcp", "udp": + case C.NetworkTCP, C.NetworkUDP: + break default: return E.New("unknown network: " + networkName) } diff --git a/option/outbound.go b/option/outbound.go index 277e0a3b..1a940574 100644 --- a/option/outbound.go +++ b/option/outbound.go @@ -55,12 +55,20 @@ func (h *Outbound) UnmarshalJSON(bytes []byte) error { } type DialerOptions struct { - Detour string `json:"detour,omitempty"` - BindInterface string `json:"bind_interface,omitempty"` - RoutingMark int `json:"routing_mark,omitempty"` - ReuseAddr bool `json:"reuse_addr,omitempty"` - ConnectTimeout int `json:"connect_timeout,omitempty"` - TCPFastOpen bool `json:"tcp_fast_open,omitempty"` + Detour string `json:"detour,omitempty"` + BindInterface string `json:"bind_interface,omitempty"` + RoutingMark int `json:"routing_mark,omitempty"` + ReuseAddr bool `json:"reuse_addr,omitempty"` + ConnectTimeout int `json:"connect_timeout,omitempty"` + TCPFastOpen bool `json:"tcp_fast_open,omitempty"` + OverrideOptions *OverrideStreamOptions `json:"override,omitempty"` +} + +type OverrideStreamOptions struct { + TLS bool `json:"tls,omitempty"` + TLSServerName string `json:"tls_servername,omitempty"` + TLSInsecure bool `json:"tls_insecure,omitempty"` + UDPOverTCP bool `json:"udp_over_tcp,omitempty"` } type DirectOutboundOptions struct { diff --git a/adapter/outbound/builder.go b/outbound/builder.go similarity index 100% rename from adapter/outbound/builder.go rename to outbound/builder.go diff --git a/outbound/default.go b/outbound/default.go new file mode 100644 index 00000000..ffd751d3 --- /dev/null +++ b/outbound/default.go @@ -0,0 +1,54 @@ +package outbound + +import ( + "context" + "net" + "runtime" + "time" + + "github.com/sagernet/sing-box/log" + "github.com/sagernet/sing/common" + "github.com/sagernet/sing/common/buf" + "github.com/sagernet/sing/common/bufio" + E "github.com/sagernet/sing/common/exceptions" + N "github.com/sagernet/sing/common/network" +) + +type myOutboundAdapter struct { + protocol string + logger log.Logger + tag string + dialer N.Dialer +} + +func (a *myOutboundAdapter) Type() string { + return a.protocol +} + +func (a *myOutboundAdapter) Tag() string { + return a.tag +} + +func CopyEarlyConn(ctx context.Context, conn net.Conn, serverConn net.Conn) error { + _payload := buf.StackNew() + payload := common.Dup(_payload) + err := conn.SetReadDeadline(time.Now().Add(100 * time.Millisecond)) + if err != nil { + return err + } + _, err = payload.ReadFrom(conn) + if err != nil && !E.IsTimeout(err) { + return E.Cause(err, "read payload") + } + err = conn.SetReadDeadline(time.Time{}) + if err != nil { + payload.Release() + return err + } + _, err = serverConn.Write(payload.Bytes()) + if err != nil { + return E.Cause(err, "client handshake") + } + runtime.KeepAlive(_payload) + return bufio.CopyConn(ctx, conn, serverConn) +} diff --git a/outbound/dialer/default.go b/outbound/dialer/default.go new file mode 100644 index 00000000..8e11ca1e --- /dev/null +++ b/outbound/dialer/default.go @@ -0,0 +1,47 @@ +package dialer + +import ( + "context" + "net" + "time" + + "github.com/database64128/tfo-go" + C "github.com/sagernet/sing-box/constant" + "github.com/sagernet/sing-box/option" + "github.com/sagernet/sing/common/control" + M "github.com/sagernet/sing/common/metadata" + N "github.com/sagernet/sing/common/network" +) + +type defaultDialer struct { + tfo.Dialer + net.ListenConfig +} + +func newDefault(options option.DialerOptions) N.Dialer { + var dialer net.Dialer + var listener net.ListenConfig + if options.BindInterface != "" { + dialer.Control = control.Append(dialer.Control, control.BindToInterface(options.BindInterface)) + listener.Control = control.Append(listener.Control, control.BindToInterface(options.BindInterface)) + } + if options.RoutingMark != 0 { + dialer.Control = control.Append(dialer.Control, control.RoutingMark(options.RoutingMark)) + listener.Control = control.Append(listener.Control, control.RoutingMark(options.RoutingMark)) + } + if options.ReuseAddr { + listener.Control = control.Append(listener.Control, control.ReuseAddr()) + } + if options.ConnectTimeout != 0 { + dialer.Timeout = time.Duration(options.ConnectTimeout) * time.Second + } + return &defaultDialer{tfo.Dialer{Dialer: dialer, DisableTFO: !options.TCPFastOpen}, listener} +} + +func (d *defaultDialer) DialContext(ctx context.Context, network string, address M.Socksaddr) (net.Conn, error) { + return d.Dialer.DialContext(ctx, network, address.String()) +} + +func (d *defaultDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) { + return d.ListenConfig.ListenPacket(ctx, C.NetworkUDP, "") +} diff --git a/outbound/dialer/detour.go b/outbound/dialer/detour.go new file mode 100644 index 00000000..4080574b --- /dev/null +++ b/outbound/dialer/detour.go @@ -0,0 +1,52 @@ +package dialer + +import ( + "context" + "net" + "sync" + + "github.com/sagernet/sing-box/adapter" + "github.com/sagernet/sing-box/option" + E "github.com/sagernet/sing/common/exceptions" + M "github.com/sagernet/sing/common/metadata" + N "github.com/sagernet/sing/common/network" +) + +type detourDialer struct { + router adapter.Router + options option.DialerOptions + dialer N.Dialer + initOnce sync.Once + initErr error +} + +func newDetour(router adapter.Router, options option.DialerOptions) N.Dialer { + return &detourDialer{router: router, options: options} +} + +func (d *detourDialer) Dialer() (N.Dialer, error) { + d.initOnce.Do(func() { + var loaded bool + d.dialer, loaded = d.router.Outbound(d.options.Detour) + if !loaded { + d.initErr = E.New("outbound detour not found: ", d.options.Detour) + } + }) + return d.dialer, d.initErr +} + +func (d *detourDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) { + dialer, err := d.Dialer() + if err != nil { + return nil, err + } + return dialer.DialContext(ctx, network, destination) +} + +func (d *detourDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) { + dialer, err := d.Dialer() + if err != nil { + return nil, err + } + return dialer.ListenPacket(ctx, destination) +} diff --git a/outbound/dialer/dialer.go b/outbound/dialer/dialer.go new file mode 100644 index 00000000..b71c62dc --- /dev/null +++ b/outbound/dialer/dialer.go @@ -0,0 +1,21 @@ +package dialer + +import ( + "github.com/sagernet/sing-box/adapter" + "github.com/sagernet/sing-box/option" + "github.com/sagernet/sing/common" + N "github.com/sagernet/sing/common/network" +) + +func New(router adapter.Router, options option.DialerOptions) N.Dialer { + var dialer N.Dialer + if options.Detour == "" { + dialer = newDefault(options) + } else { + dialer = newDetour(router, options) + } + if options.OverrideOptions != nil { + dialer = newOverride(dialer, common.PtrValueOrDefault(options.OverrideOptions)) + } + return dialer +} diff --git a/outbound/dialer/override.go b/outbound/dialer/override.go new file mode 100644 index 00000000..bc38f283 --- /dev/null +++ b/outbound/dialer/override.go @@ -0,0 +1,68 @@ +package dialer + +import ( + "context" + "crypto/tls" + "net" + + C "github.com/sagernet/sing-box/constant" + "github.com/sagernet/sing-box/option" + M "github.com/sagernet/sing/common/metadata" + N "github.com/sagernet/sing/common/network" + "github.com/sagernet/sing/common/uot" +) + +var _ N.Dialer = (*overrideDialer)(nil) + +type overrideDialer struct { + upstream N.Dialer + tlsEnabled bool + tlsConfig tls.Config + uotEnabled bool +} + +func newOverride(upstream N.Dialer, options option.OverrideStreamOptions) N.Dialer { + if !options.TLS && !options.UDPOverTCP { + return upstream + } + return &overrideDialer{ + upstream, + options.TLS, + tls.Config{ + ServerName: options.TLSServerName, + InsecureSkipVerify: options.TLSInsecure, + }, + options.UDPOverTCP, + } +} + +func (d *overrideDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) { + switch network { + case C.NetworkTCP: + conn, err := d.upstream.DialContext(ctx, C.NetworkTCP, destination) + if err != nil { + return nil, err + } + return tls.Client(conn, &d.tlsConfig), nil + case C.NetworkUDP: + if d.uotEnabled { + tcpConn, err := d.upstream.DialContext(ctx, C.NetworkTCP, destination) + if err != nil { + return nil, err + } + return uot.NewClientConn(tcpConn), nil + } + } + return d.upstream.DialContext(ctx, network, destination) +} + +func (d *overrideDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) { + if d.uotEnabled { + tcpConn, err := d.upstream.DialContext(ctx, C.NetworkTCP, destination) + if err != nil { + return nil, err + } + return uot.NewClientConn(tcpConn), nil + } + return d.upstream.ListenPacket(ctx, destination) +} diff --git a/adapter/outbound/direct.go b/outbound/direct.go similarity index 93% rename from adapter/outbound/direct.go rename to outbound/direct.go index 3d4fbc36..de532d71 100644 --- a/adapter/outbound/direct.go +++ b/outbound/direct.go @@ -8,6 +8,7 @@ import ( C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/option" + "github.com/sagernet/sing-box/outbound/dialer" "github.com/sagernet/sing/common/bufio" M "github.com/sagernet/sing/common/metadata" N "github.com/sagernet/sing/common/network" @@ -27,7 +28,7 @@ func NewDirect(router adapter.Router, logger log.Logger, tag string, options opt protocol: C.TypeDirect, logger: logger, tag: tag, - dialer: NewDialer(router, options.DialerOptions), + dialer: dialer.New(router, options.DialerOptions), }, } if options.OverrideAddress != "" && options.OverridePort != 0 { @@ -69,7 +70,7 @@ func (d *Direct) ListenPacket(ctx context.Context, destination M.Socksaddr) (net } func (d *Direct) NewConnection(ctx context.Context, conn net.Conn, destination M.Socksaddr) error { - outConn, err := d.DialContext(ctx, "tcp", destination) + outConn, err := d.DialContext(ctx, C.NetworkTCP, destination) if err != nil { return err } diff --git a/adapter/outbound/shadowsocks.go b/outbound/shadowsocks.go similarity index 89% rename from adapter/outbound/shadowsocks.go rename to outbound/shadowsocks.go index 38a64d3a..f210f3c1 100644 --- a/adapter/outbound/shadowsocks.go +++ b/outbound/shadowsocks.go @@ -8,6 +8,7 @@ import ( C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/option" + "github.com/sagernet/sing-box/outbound/dialer" "github.com/sagernet/sing-shadowsocks" "github.com/sagernet/sing-shadowsocks/shadowimpl" "github.com/sagernet/sing/common/bufio" @@ -30,7 +31,7 @@ func NewShadowsocks(router adapter.Router, logger log.Logger, tag string, option protocol: C.TypeDirect, logger: logger, tag: tag, - dialer: NewDialer(router, options.DialerOptions), + dialer: dialer.New(router, options.DialerOptions), }, } var err error @@ -51,14 +52,14 @@ func (o *Shadowsocks) DialContext(ctx context.Context, network string, destinati switch network { case C.NetworkTCP: o.logger.WithContext(ctx).Info("outbound connection to ", destination) - outConn, err := o.dialer.DialContext(ctx, "tcp", o.serverAddr) + outConn, err := o.dialer.DialContext(ctx, C.NetworkTCP, o.serverAddr) if err != nil { return nil, err } return o.method.DialEarlyConn(outConn, destination), nil case C.NetworkUDP: o.logger.WithContext(ctx).Info("outbound packet connection to ", destination) - outConn, err := o.dialer.DialContext(ctx, "udp", o.serverAddr) + outConn, err := o.dialer.DialContext(ctx, C.NetworkUDP, o.serverAddr) if err != nil { return nil, err } @@ -78,7 +79,7 @@ func (o *Shadowsocks) ListenPacket(ctx context.Context, destination M.Socksaddr) } func (o *Shadowsocks) NewConnection(ctx context.Context, conn net.Conn, destination M.Socksaddr) error { - serverConn, err := o.DialContext(ctx, "tcp", destination) + serverConn, err := o.DialContext(ctx, C.NetworkTCP, destination) if err != nil { return err } diff --git a/adapter/outbound/socks.go b/outbound/socks.go similarity index 93% rename from adapter/outbound/socks.go rename to outbound/socks.go index a9ebab75..50b68f73 100644 --- a/adapter/outbound/socks.go +++ b/outbound/socks.go @@ -8,6 +8,7 @@ import ( C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/option" + "github.com/sagernet/sing-box/outbound/dialer" "github.com/sagernet/sing/common/bufio" M "github.com/sagernet/sing/common/metadata" N "github.com/sagernet/sing/common/network" @@ -22,7 +23,7 @@ type Socks struct { } func NewSocks(router adapter.Router, logger log.Logger, tag string, options option.SocksOutboundOptions) (*Socks, error) { - dialer := NewDialer(router, options.DialerOptions) + dialer := dialer.New(router, options.DialerOptions) var version socks.Version var err error if options.Version != "" { @@ -62,7 +63,7 @@ func (h *Socks) ListenPacket(ctx context.Context, destination M.Socksaddr) (net. } func (h *Socks) NewConnection(ctx context.Context, conn net.Conn, destination M.Socksaddr) error { - outConn, err := h.DialContext(ctx, "tcp", destination) + outConn, err := h.DialContext(ctx, C.NetworkTCP, destination) if err != nil { return err } diff --git a/adapter/route/router.go b/route/router.go similarity index 100% rename from adapter/route/router.go rename to route/router.go diff --git a/adapter/route/rule.go b/route/rule.go similarity index 100% rename from adapter/route/rule.go rename to route/rule.go diff --git a/adapter/route/rule_cidr.go b/route/rule_cidr.go similarity index 100% rename from adapter/route/rule_cidr.go rename to route/rule_cidr.go diff --git a/adapter/route/rule_domain.go b/route/rule_domain.go similarity index 100% rename from adapter/route/rule_domain.go rename to route/rule_domain.go diff --git a/adapter/route/rule_domain_keyword.go b/route/rule_domain_keyword.go similarity index 100% rename from adapter/route/rule_domain_keyword.go rename to route/rule_domain_keyword.go diff --git a/adapter/route/rule_geoip.go b/route/rule_geoip.go similarity index 100% rename from adapter/route/rule_geoip.go rename to route/rule_geoip.go diff --git a/adapter/route/rule_inbound.go b/route/rule_inbound.go similarity index 100% rename from adapter/route/rule_inbound.go rename to route/rule_inbound.go diff --git a/adapter/route/rule_ipversion.go b/route/rule_ipversion.go similarity index 100% rename from adapter/route/rule_ipversion.go rename to route/rule_ipversion.go diff --git a/adapter/route/rule_logical.go b/route/rule_logical.go similarity index 100% rename from adapter/route/rule_logical.go rename to route/rule_logical.go diff --git a/adapter/route/rule_network.go b/route/rule_network.go similarity index 100% rename from adapter/route/rule_network.go rename to route/rule_network.go diff --git a/adapter/route/rule_port.go b/route/rule_port.go similarity index 100% rename from adapter/route/rule_port.go rename to route/rule_port.go diff --git a/adapter/route/rule_protocol.go b/route/rule_protocol.go similarity index 100% rename from adapter/route/rule_protocol.go rename to route/rule_protocol.go diff --git a/service.go b/service.go index ead3b19e..34fefd8f 100644 --- a/service.go +++ b/service.go @@ -4,11 +4,11 @@ import ( "context" "github.com/sagernet/sing-box/adapter" - "github.com/sagernet/sing-box/adapter/inbound" - "github.com/sagernet/sing-box/adapter/outbound" - "github.com/sagernet/sing-box/adapter/route" + "github.com/sagernet/sing-box/inbound" "github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/option" + outbound2 "github.com/sagernet/sing-box/outbound" + "github.com/sagernet/sing-box/route" "github.com/sagernet/sing/common" E "github.com/sagernet/sing/common/exceptions" ) @@ -43,14 +43,14 @@ func NewService(ctx context.Context, options option.Options) (*Service, error) { } for i, outboundOptions := range options.Outbounds { var outboundService adapter.Outbound - outboundService, err = outbound.New(router, logger, i, outboundOptions) + outboundService, err = outbound2.New(router, logger, i, outboundOptions) if err != nil { return nil, E.Cause(err, "parse outbound[", i, "]") } outbounds = append(outbounds, outboundService) } if len(outbounds) == 0 { - outbounds = append(outbounds, outbound.NewDirect(nil, logger, "direct", option.DirectOutboundOptions{})) + outbounds = append(outbounds, outbound2.NewDirect(nil, logger, "direct", option.DirectOutboundOptions{})) } router.UpdateOutbounds(outbounds) return &Service{