Explorar o código

Add bind address to outbound options

世界 %!s(int64=3) %!d(string=hai) anos
pai
achega
7ba0a14e97

+ 12 - 2
common/dialer/default.go

@@ -3,6 +3,7 @@ package dialer
 import (
 	"context"
 	"net"
+	"net/netip"
 	"time"
 
 	"github.com/sagernet/sing-box/adapter"
@@ -54,6 +55,7 @@ var warnTFOOnUnsupportedPlatform = warning.New(
 type DefaultDialer struct {
 	tfo.Dialer
 	net.ListenConfig
+	bindUDPAddr string
 }
 
 func NewDefault(router adapter.Router, options option.DialerOptions) *DefaultDialer {
@@ -108,7 +110,15 @@ func NewDefault(router adapter.Router, options option.DialerOptions) *DefaultDia
 	if options.TCPFastOpen {
 		warnTFOOnUnsupportedPlatform.Check()
 	}
-	return &DefaultDialer{tfo.Dialer{Dialer: dialer, DisableTFO: !options.TCPFastOpen}, listener}
+	var bindUDPAddr string
+	bindAddress := netip.Addr(options.BindAddress)
+	if bindAddress.IsValid() {
+		dialer.LocalAddr = &net.TCPAddr{
+			IP: bindAddress.AsSlice(),
+		}
+		bindUDPAddr = M.SocksaddrFrom(bindAddress, 0).String()
+	}
+	return &DefaultDialer{tfo.Dialer{Dialer: dialer, DisableTFO: !options.TCPFastOpen}, listener, bindUDPAddr}
 }
 
 func (d *DefaultDialer) DialContext(ctx context.Context, network string, address M.Socksaddr) (net.Conn, error) {
@@ -116,7 +126,7 @@ func (d *DefaultDialer) DialContext(ctx context.Context, network string, address
 }
 
 func (d *DefaultDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
-	return d.ListenConfig.ListenPacket(ctx, N.NetworkUDP, "")
+	return d.ListenConfig.ListenPacket(ctx, N.NetworkUDP, d.bindUDPAddr)
 }
 
 func (d *DefaultDialer) Upstream() any {

+ 1 - 0
docs/changelog.md

@@ -1,6 +1,7 @@
 #### 2022/08/22
 
 * Add strategy setting for each [DNS server](/configuration/dns/server)
+* Add bind address to outbound options
 
 #### 2022/08/21
 

+ 5 - 0
docs/configuration/outbound/direct.md

@@ -14,6 +14,7 @@
 
       "detour": "upstream-out",
       "bind_interface": "en0",
+      "bind_address": "0.0.0.0",
       "routing_mark": 1234,
       "reuse_addr": false,
       "connect_timeout": "5s",
@@ -47,6 +48,10 @@ Other dial fields will be ignored when enabled.
 
 The network interface to bind to.
 
+#### bind_address
+
+The address to bind to.
+
 #### routing_mark
 
 !!! error ""

+ 5 - 0
docs/configuration/outbound/http.md

@@ -17,6 +17,7 @@
       
       "detour": "upstream-out",
       "bind_interface": "en0",
+      "bind_address": "0.0.0.0",
       "routing_mark": 1234,
       "reuse_addr": false,
       "connect_timeout": "5s",
@@ -66,6 +67,10 @@ Other dial fields will be ignored when enabled.
 
 The network interface to bind to.
 
+#### bind_address
+
+The address to bind to.
+
 #### routing_mark
 
 !!! error ""

+ 5 - 0
docs/configuration/outbound/hysteria.md

@@ -25,6 +25,7 @@
       
       "detour": "upstream-out",
       "bind_interface": "en0",
+      "bind_address": "0.0.0.0",
       "routing_mark": 1234,
       "reuse_addr": false,
       "connect_timeout": "5s",
@@ -134,6 +135,10 @@ Other dial fields will be ignored when enabled.
 
 The network interface to bind to.
 
+#### bind_address
+
+The address to bind to.
+
 #### routing_mark
 
 !!! error ""

+ 5 - 0
docs/configuration/outbound/shadowsocks.md

@@ -17,6 +17,7 @@
       
       "detour": "upstream-out",
       "bind_interface": "en0",
+      "bind_address": "0.0.0.0",
       "routing_mark": 1234,
       "reuse_addr": false,
       "connect_timeout": "5s",
@@ -106,6 +107,10 @@ Other dial fields will be ignored when enabled.
 
 The network interface to bind to.
 
+#### bind_address
+
+The address to bind to.
+
 #### routing_mark
 
 !!! error ""

+ 5 - 0
docs/configuration/outbound/socks.md

@@ -19,6 +19,7 @@
 
       "detour": "upstream-out",
       "bind_interface": "en0",
+      "bind_address": "0.0.0.0",
       "routing_mark": 1234,
       "reuse_addr": false,
       "connect_timeout": "5s",
@@ -82,6 +83,10 @@ Other dial fields will be ignored when enabled.
 
 The network interface to bind to.
 
+#### bind_address
+
+The address to bind to.
+
 #### routing_mark
 
 !!! error ""

+ 5 - 0
docs/configuration/outbound/ssh.md

@@ -19,6 +19,7 @@
       
       "detour": "upstream-out",
       "bind_interface": "en0",
+      "bind_address": "0.0.0.0",
       "routing_mark": 1234,
       "reuse_addr": false,
       "connect_timeout": "5s",
@@ -82,6 +83,10 @@ Other dial fields will be ignored when enabled.
 
 The network interface to bind to.
 
+#### bind_address
+
+The address to bind to.
+
 #### routing_mark
 
 !!! error ""

+ 5 - 0
docs/configuration/outbound/tor.md

@@ -16,6 +16,7 @@
       
       "detour": "upstream-out",
       "bind_interface": "en0",
+      "bind_address": "0.0.0.0",
       "routing_mark": 1234,
       "reuse_addr": false,
       "connect_timeout": "5s",
@@ -69,6 +70,10 @@ Other dial fields will be ignored when enabled.
 
 The network interface to bind to.
 
+#### bind_address
+
+The address to bind to.
+
 #### routing_mark
 
 !!! error ""

+ 5 - 0
docs/configuration/outbound/trojan.md

@@ -16,6 +16,7 @@
 
       "detour": "upstream-out",
       "bind_interface": "en0",
+      "bind_address": "0.0.0.0",
       "routing_mark": 1234,
       "reuse_addr": false,
       "connect_timeout": "5s",
@@ -75,6 +76,10 @@ Other dial fields will be ignored when enabled.
 
 The network interface to bind to.
 
+#### bind_address
+
+The address to bind to.
+
 #### routing_mark
 
 !!! error ""

+ 5 - 0
docs/configuration/outbound/vmess.md

@@ -20,6 +20,7 @@
 
       "detour": "upstream-out",
       "bind_interface": "en0",
+      "bind_address": "0.0.0.0",
       "routing_mark": 1234,
       "reuse_addr": false,
       "connect_timeout": "5s",
@@ -109,6 +110,10 @@ Other dial fields will be ignored when enabled.
 
 The network interface to bind to.
 
+#### bind_address
+
+The address to bind to.
+
 #### routing_mark
 
 !!! error ""

+ 5 - 0
docs/configuration/outbound/wireguard.md

@@ -21,6 +21,7 @@
       
       "detour": "upstream-out",
       "bind_interface": "en0",
+      "bind_address": "0.0.0.0",
       "routing_mark": 1234,
       "reuse_addr": false,
       "connect_timeout": "5s",
@@ -101,6 +102,10 @@ Other dial fields will be ignored when enabled.
 
 The network interface to bind to.
 
+#### bind_address
+
+The address to bind to.
+
 #### routing_mark
 
 !!! error ""

+ 8 - 7
option/outbound.go

@@ -100,13 +100,14 @@ func (h *Outbound) UnmarshalJSON(bytes []byte) error {
 }
 
 type DialerOptions struct {
-	Detour         string   `json:"detour,omitempty"`
-	BindInterface  string   `json:"bind_interface,omitempty"`
-	ProtectPath    string   `json:"protect_path,omitempty"`
-	RoutingMark    int      `json:"routing_mark,omitempty"`
-	ReuseAddr      bool     `json:"reuse_addr,omitempty"`
-	ConnectTimeout Duration `json:"connect_timeout,omitempty"`
-	TCPFastOpen    bool     `json:"tcp_fast_open,omitempty"`
+	Detour         string        `json:"detour,omitempty"`
+	BindInterface  string        `json:"bind_interface,omitempty"`
+	BindAddress    ListenAddress `json:"bind_address,omitempty"`
+	ProtectPath    string        `json:"protect_path,omitempty"`
+	RoutingMark    int           `json:"routing_mark,omitempty"`
+	ReuseAddr      bool          `json:"reuse_addr,omitempty"`
+	ConnectTimeout Duration      `json:"connect_timeout,omitempty"`
+	TCPFastOpen    bool          `json:"tcp_fast_open,omitempty"`
 }
 
 type OutboundDialerOptions struct {