Add bind address to outbound options

This commit is contained in:
世界 2022-08-22 14:28:23 +08:00
parent 3a442347a5
commit 7ba0a14e97
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
13 changed files with 71 additions and 9 deletions

View file

@ -3,6 +3,7 @@ package dialer
import ( import (
"context" "context"
"net" "net"
"net/netip"
"time" "time"
"github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/adapter"
@ -54,6 +55,7 @@ var warnTFOOnUnsupportedPlatform = warning.New(
type DefaultDialer struct { type DefaultDialer struct {
tfo.Dialer tfo.Dialer
net.ListenConfig net.ListenConfig
bindUDPAddr string
} }
func NewDefault(router adapter.Router, options option.DialerOptions) *DefaultDialer { 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 { if options.TCPFastOpen {
warnTFOOnUnsupportedPlatform.Check() 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) { 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) { 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 { func (d *DefaultDialer) Upstream() any {

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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