diff --git a/option/types.go b/option/types.go index fe7d4b3d..10abbb92 100644 --- a/option/types.go +++ b/option/types.go @@ -12,6 +12,11 @@ import ( mDNS "github.com/miekg/dns" ) +var ( + DefaultNetworks = []string{N.NetworkTCP, N.NetworkUDP} + DefaultIPNetworks = []string{N.NetworkTCP, N.NetworkUDP, N.NetworkICMPv4, N.NetworkICMPv6} +) + type NetworkList string func (v *NetworkList) UnmarshalJSON(content []byte) error { @@ -37,9 +42,9 @@ func (v *NetworkList) UnmarshalJSON(content []byte) error { return nil } -func (v NetworkList) Build() []string { +func (v NetworkList) Build(defaultNetworks []string) []string { if v == "" { - return []string{N.NetworkTCP, N.NetworkUDP} + return defaultNetworks } return strings.Split(string(v), "\n") } diff --git a/protocol/direct/inbound.go b/protocol/direct/inbound.go index 93994761..bf124864 100644 --- a/protocol/direct/inbound.go +++ b/protocol/direct/inbound.go @@ -60,7 +60,7 @@ func NewInbound(ctx context.Context, router adapter.Router, logger log.ContextLo inbound.listener = listener.New(listener.Options{ Context: ctx, Logger: logger, - Network: options.Network.Build(), + Network: options.Network.Build(option.DefaultIPNetworks), Listen: options.ListenOptions, ConnectionHandler: inbound, PacketHandler: inbound, diff --git a/protocol/direct/outbound.go b/protocol/direct/outbound.go index 9b454f58..5af23fd8 100644 --- a/protocol/direct/outbound.go +++ b/protocol/direct/outbound.go @@ -2,6 +2,7 @@ package direct import ( "context" + "github.com/sagernet/sing-tun" "net" "net/netip" "time" @@ -27,6 +28,7 @@ func RegisterOutbound(registry *outbound.Registry) { var ( _ N.ParallelDialer = (*Outbound)(nil) _ dialer.ParallelNetworkDialer = (*Outbound)(nil) + _ adapter.DirectRouteOutbound = (*Outbound)(nil) ) type Outbound struct { @@ -50,7 +52,7 @@ func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextL return nil, err } outbound := &Outbound{ - Adapter: outbound.NewAdapterWithDialerOptions(C.TypeDirect, tag, []string{N.NetworkTCP, N.NetworkUDP}, options.DialerOptions), + Adapter: outbound.NewAdapterWithDialerOptions(C.TypeDirect, tag, option.DefaultIPNetworks, options.DialerOptions), logger: logger, //nolint:staticcheck domainStrategy: C.DomainStrategy(options.DomainStrategy), @@ -242,6 +244,10 @@ func (h *Outbound) ListenSerialNetworkPacket(ctx context.Context, destination M. return conn, newDestination, nil } +func (h *Outbound) NewDirectRouteConnection(metadata adapter.InboundContext, routeContext tun.DirectRouteContext) (tun.DirectRouteDestination, error) { + +} + /*func (h *Outbound) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error { if h.loopBack.CheckConn(metadata.Source.AddrPort(), M.AddrPortFromNet(conn.LocalAddr())) { return E.New("reject loopback connection to ", metadata.Destination) diff --git a/protocol/hysteria/outbound.go b/protocol/hysteria/outbound.go index d3035c71..07c99b07 100644 --- a/protocol/hysteria/outbound.go +++ b/protocol/hysteria/outbound.go @@ -52,7 +52,7 @@ func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextL if err != nil { return nil, err } - networkList := options.Network.Build() + networkList := options.Network.Build(option.DefaultIPNetworks) var password string if options.AuthString != "" { password = options.AuthString diff --git a/protocol/hysteria2/outbound.go b/protocol/hysteria2/outbound.go index c805f07e..a774ac5b 100644 --- a/protocol/hysteria2/outbound.go +++ b/protocol/hysteria2/outbound.go @@ -64,7 +64,7 @@ func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextL if err != nil { return nil, err } - networkList := options.Network.Build() + networkList := options.Network.Build(option.DefaultIPNetworks) client, err := hysteria2.NewClient(hysteria2.ClientOptions{ Context: ctx, Dialer: outboundDialer, diff --git a/protocol/naive/inbound.go b/protocol/naive/inbound.go index 5e77af43..27acb233 100644 --- a/protocol/naive/inbound.go +++ b/protocol/naive/inbound.go @@ -57,7 +57,7 @@ func NewInbound(ctx context.Context, router adapter.Router, logger log.ContextLo Listen: options.ListenOptions, }), networkIsDefault: options.Network == "", - network: options.Network.Build(), + network: options.Network.Build(option.DefaultIPNetworks), authenticator: auth.NewAuthenticator(options.Users), } if common.Contains(inbound.network, N.NetworkUDP) { diff --git a/protocol/redirect/tproxy.go b/protocol/redirect/tproxy.go index 7860e173..ec4b8b9a 100644 --- a/protocol/redirect/tproxy.go +++ b/protocol/redirect/tproxy.go @@ -53,7 +53,7 @@ func NewTProxy(ctx context.Context, router adapter.Router, logger log.ContextLog tproxy.listener = listener.New(listener.Options{ Context: ctx, Logger: logger, - Network: options.Network.Build(), + Network: options.Network.Build(option.DefaultIPNetworks), Listen: options.ListenOptions, ConnectionHandler: tproxy, OOBPacketHandler: tproxy, diff --git a/protocol/shadowsocks/inbound.go b/protocol/shadowsocks/inbound.go index 89af24ea..09f8ad48 100644 --- a/protocol/shadowsocks/inbound.go +++ b/protocol/shadowsocks/inbound.go @@ -84,7 +84,7 @@ func newInbound(ctx context.Context, router adapter.Router, logger log.ContextLo inbound.listener = listener.New(listener.Options{ Context: ctx, Logger: logger, - Network: options.Network.Build(), + Network: options.Network.Build(option.DefaultIPNetworks), Listen: options.ListenOptions, ConnectionHandler: inbound, PacketHandler: inbound, diff --git a/protocol/shadowsocks/inbound_multi.go b/protocol/shadowsocks/inbound_multi.go index 5b604365..129953a7 100644 --- a/protocol/shadowsocks/inbound_multi.go +++ b/protocol/shadowsocks/inbound_multi.go @@ -92,7 +92,7 @@ func newMultiInbound(ctx context.Context, router adapter.Router, logger log.Cont inbound.listener = listener.New(listener.Options{ Context: ctx, Logger: logger, - Network: options.Network.Build(), + Network: options.Network.Build(option.DefaultIPNetworks), Listen: options.ListenOptions, ConnectionHandler: inbound, PacketHandler: inbound, diff --git a/protocol/shadowsocks/inbound_relay.go b/protocol/shadowsocks/inbound_relay.go index 9760b2f0..c24c118e 100644 --- a/protocol/shadowsocks/inbound_relay.go +++ b/protocol/shadowsocks/inbound_relay.go @@ -77,7 +77,7 @@ func newRelayInbound(ctx context.Context, router adapter.Router, logger log.Cont inbound.listener = listener.New(listener.Options{ Context: ctx, Logger: logger, - Network: options.Network.Build(), + Network: options.Network.Build(option.DefaultIPNetworks), Listen: options.ListenOptions, ConnectionHandler: inbound, PacketHandler: inbound, diff --git a/protocol/shadowsocks/outbound.go b/protocol/shadowsocks/outbound.go index 875c9e69..8513a5d1 100644 --- a/protocol/shadowsocks/outbound.go +++ b/protocol/shadowsocks/outbound.go @@ -49,7 +49,7 @@ func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextL return nil, err } outbound := &Outbound{ - Adapter: outbound.NewAdapterWithDialerOptions(C.TypeShadowsocks, tag, options.Network.Build(), options.DialerOptions), + Adapter: outbound.NewAdapterWithDialerOptions(C.TypeShadowsocks, tag, options.Network.Build(option.DefaultIPNetworks), options.DialerOptions), logger: logger, dialer: outboundDialer, method: method, diff --git a/protocol/socks/outbound.go b/protocol/socks/outbound.go index 851412ff..3f2daa49 100644 --- a/protocol/socks/outbound.go +++ b/protocol/socks/outbound.go @@ -51,7 +51,7 @@ func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextL return nil, err } outbound := &Outbound{ - Adapter: outbound.NewAdapterWithDialerOptions(C.TypeSOCKS, tag, options.Network.Build(), options.DialerOptions), + Adapter: outbound.NewAdapterWithDialerOptions(C.TypeSOCKS, tag, options.Network.Build(option.DefaultIPNetworks), options.DialerOptions), dnsRouter: service.FromContext[adapter.DNSRouter](ctx), logger: logger, client: socks.NewClient(outboundDialer, options.ServerOptions.Build(), version, options.Username, options.Password), diff --git a/protocol/trojan/outbound.go b/protocol/trojan/outbound.go index 37a6933c..b9c67844 100644 --- a/protocol/trojan/outbound.go +++ b/protocol/trojan/outbound.go @@ -43,7 +43,7 @@ func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextL return nil, err } outbound := &Outbound{ - Adapter: outbound.NewAdapterWithDialerOptions(C.TypeTrojan, tag, options.Network.Build(), options.DialerOptions), + Adapter: outbound.NewAdapterWithDialerOptions(C.TypeTrojan, tag, options.Network.Build(option.DefaultIPNetworks), options.DialerOptions), logger: logger, dialer: outboundDialer, serverAddr: options.ServerOptions.Build(), diff --git a/protocol/tuic/outbound.go b/protocol/tuic/outbound.go index a31d4850..b2ff7e86 100644 --- a/protocol/tuic/outbound.go +++ b/protocol/tuic/outbound.go @@ -80,7 +80,7 @@ func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextL return nil, err } return &Outbound{ - Adapter: outbound.NewAdapterWithDialerOptions(C.TypeTUIC, tag, options.Network.Build(), options.DialerOptions), + Adapter: outbound.NewAdapterWithDialerOptions(C.TypeTUIC, tag, options.Network.Build(option.DefaultIPNetworks), options.DialerOptions), logger: logger, client: client, udpStream: options.UDPOverStream, diff --git a/protocol/vless/outbound.go b/protocol/vless/outbound.go index e0208be9..cb5f406b 100644 --- a/protocol/vless/outbound.go +++ b/protocol/vless/outbound.go @@ -46,7 +46,7 @@ func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextL return nil, err } outbound := &Outbound{ - Adapter: outbound.NewAdapterWithDialerOptions(C.TypeVLESS, tag, options.Network.Build(), options.DialerOptions), + Adapter: outbound.NewAdapterWithDialerOptions(C.TypeVLESS, tag, options.Network.Build(option.DefaultIPNetworks), options.DialerOptions), logger: logger, dialer: outboundDialer, serverAddr: options.ServerOptions.Build(), diff --git a/protocol/vmess/outbound.go b/protocol/vmess/outbound.go index be05990e..e9f550ba 100644 --- a/protocol/vmess/outbound.go +++ b/protocol/vmess/outbound.go @@ -46,7 +46,7 @@ func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextL return nil, err } outbound := &Outbound{ - Adapter: outbound.NewAdapterWithDialerOptions(C.TypeVMess, tag, options.Network.Build(), options.DialerOptions), + Adapter: outbound.NewAdapterWithDialerOptions(C.TypeVMess, tag, options.Network.Build(option.DefaultIPNetworks), options.DialerOptions), logger: logger, dialer: outboundDialer, serverAddr: options.ServerOptions.Build(), diff --git a/protocol/wireguard/outbound.go b/protocol/wireguard/outbound.go index edd8184c..eeb92552 100644 --- a/protocol/wireguard/outbound.go +++ b/protocol/wireguard/outbound.go @@ -45,7 +45,7 @@ func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextL deprecated.Report(ctx, deprecated.OptionWireGuardGSO) } outbound := &Outbound{ - Adapter: outbound.NewAdapterWithDialerOptions(C.TypeWireGuard, tag, []string{N.NetworkTCP, N.NetworkUDP}, options.DialerOptions), + Adapter: outbound.NewAdapterWithDialerOptions(C.TypeWireGuard, tag, option.DefaultNetworks, options.DialerOptions), ctx: ctx, dnsRouter: service.FromContext[adapter.DNSRouter](ctx), logger: logger,