sing-box/common/dialer/default.go

149 lines
5.1 KiB
Go
Raw Normal View History

package dialer
import (
"context"
"net"
2022-08-22 14:28:23 +08:00
"net/netip"
"time"
"github.com/sagernet/sing-box/adapter"
2022-07-24 22:54:16 +08:00
"github.com/sagernet/sing-box/common/warning"
2022-07-06 15:01:09 +08:00
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/option"
2022-07-08 23:03:57 +08:00
"github.com/sagernet/sing/common/control"
2022-08-29 19:58:58 +08:00
E "github.com/sagernet/sing/common/exceptions"
2022-07-08 23:03:57 +08:00
M "github.com/sagernet/sing/common/metadata"
2022-07-30 00:29:22 +08:00
N "github.com/sagernet/sing/common/network"
2022-07-06 15:01:09 +08:00
"github.com/database64128/tfo-go"
)
2022-07-24 22:54:16 +08:00
var warnBindInterfaceOnUnsupportedPlatform = warning.New(
func() bool {
2022-08-05 16:55:46 +08:00
return !(C.IsLinux || C.IsWindows || C.IsDarwin)
2022-07-24 22:54:16 +08:00
},
"outbound option `bind_interface` is only supported on Linux and Windows",
)
var warnRoutingMarkOnUnsupportedPlatform = warning.New(
func() bool {
return !C.IsLinux
},
"outbound option `routing_mark` is only supported on Linux",
)
var warnReuseAdderOnUnsupportedPlatform = warning.New(
func() bool {
return !(C.IsDarwin || C.IsDragonfly || C.IsFreebsd || C.IsLinux || C.IsNetbsd || C.IsOpenbsd || C.IsSolaris || C.IsWindows)
},
"outbound option `reuse_addr` is unsupported on current platform",
)
var warnProtectPathOnNonAndroid = warning.New(
func() bool {
return !C.IsAndroid
},
"outbound option `protect_path` is only supported on Android",
)
var warnTFOOnUnsupportedPlatform = warning.New(
func() bool {
return !(C.IsDarwin || C.IsFreebsd || C.IsLinux || C.IsWindows)
},
"outbound option `tcp_fast_open` is unsupported on current platform",
)
2022-07-07 21:47:21 +08:00
type DefaultDialer struct {
2022-08-22 14:35:05 +08:00
dialer tfo.Dialer
udpDialer net.Dialer
udpListener net.ListenConfig
2022-08-22 14:28:23 +08:00
bindUDPAddr string
}
func NewDefault(router adapter.Router, options option.DialerOptions) *DefaultDialer {
var dialer net.Dialer
var listener net.ListenConfig
if options.BindInterface != "" {
2022-07-24 22:54:16 +08:00
warnBindInterfaceOnUnsupportedPlatform.Check()
2022-09-14 12:46:02 +08:00
bindFunc := control.BindToInterface(router.InterfaceFinder(), options.BindInterface, -1)
2022-08-04 22:01:20 +08:00
dialer.Control = control.Append(dialer.Control, bindFunc)
listener.Control = control.Append(listener.Control, bindFunc)
} else if router.AutoDetectInterface() {
2022-09-14 12:46:02 +08:00
const useInterfaceName = C.IsLinux
bindFunc := control.BindToInterfaceFunc(router.InterfaceFinder(), func(network string, address string) (interfaceName string, interfaceIndex int) {
remoteAddr := M.ParseSocksaddr(address).Addr
if C.IsLinux {
return router.InterfaceMonitor().DefaultInterfaceName(remoteAddr), -1
} else {
return "", router.InterfaceMonitor().DefaultInterfaceIndex(remoteAddr)
}
})
dialer.Control = control.Append(dialer.Control, bindFunc)
listener.Control = control.Append(listener.Control, bindFunc)
2022-07-15 11:51:51 +08:00
} else if router.DefaultInterface() != "" {
2022-09-14 12:46:02 +08:00
bindFunc := control.BindToInterface(router.InterfaceFinder(), router.DefaultInterface(), -1)
2022-08-04 22:01:20 +08:00
dialer.Control = control.Append(dialer.Control, bindFunc)
listener.Control = control.Append(listener.Control, bindFunc)
}
if options.RoutingMark != 0 {
2022-07-24 22:54:16 +08:00
warnRoutingMarkOnUnsupportedPlatform.Check()
dialer.Control = control.Append(dialer.Control, control.RoutingMark(options.RoutingMark))
listener.Control = control.Append(listener.Control, control.RoutingMark(options.RoutingMark))
2022-07-24 17:46:25 +08:00
} else if router.DefaultMark() != 0 {
dialer.Control = control.Append(dialer.Control, control.RoutingMark(router.DefaultMark()))
listener.Control = control.Append(listener.Control, control.RoutingMark(router.DefaultMark()))
}
if options.ReuseAddr {
2022-07-24 22:54:16 +08:00
warnReuseAdderOnUnsupportedPlatform.Check()
listener.Control = control.Append(listener.Control, control.ReuseAddr())
}
2022-07-06 14:45:56 +08:00
if options.ProtectPath != "" {
2022-07-24 22:54:16 +08:00
warnProtectPathOnNonAndroid.Check()
2022-07-11 18:44:59 +08:00
dialer.Control = control.Append(dialer.Control, control.ProtectPath(options.ProtectPath))
listener.Control = control.Append(listener.Control, control.ProtectPath(options.ProtectPath))
2022-07-06 14:45:56 +08:00
}
if options.ConnectTimeout != 0 {
2022-07-08 12:58:43 +08:00
dialer.Timeout = time.Duration(options.ConnectTimeout)
2022-07-18 20:40:14 +08:00
} else {
2022-07-25 22:02:39 +08:00
dialer.Timeout = C.TCPTimeout
}
2022-07-24 22:54:16 +08:00
if options.TCPFastOpen {
warnTFOOnUnsupportedPlatform.Check()
}
2022-09-06 00:54:57 +08:00
if !options.UDPFragment {
dialer.Control = control.Append(dialer.Control, control.DisableUDPFragment())
listener.Control = control.Append(listener.Control, control.DisableUDPFragment())
}
2022-08-22 14:28:23 +08:00
var bindUDPAddr string
2022-08-22 14:35:05 +08:00
udpDialer := dialer
2022-08-25 14:49:49 +08:00
var bindAddress netip.Addr
if options.BindAddress != nil {
bindAddress = options.BindAddress.Build()
}
2022-08-22 14:28:23 +08:00
if bindAddress.IsValid() {
dialer.LocalAddr = &net.TCPAddr{
IP: bindAddress.AsSlice(),
}
2022-08-22 14:35:05 +08:00
udpDialer.LocalAddr = &net.UDPAddr{
IP: bindAddress.AsSlice(),
}
2022-08-22 14:28:23 +08:00
bindUDPAddr = M.SocksaddrFrom(bindAddress, 0).String()
}
2022-08-22 14:35:05 +08:00
return &DefaultDialer{tfo.Dialer{Dialer: dialer, DisableTFO: !options.TCPFastOpen}, udpDialer, listener, bindUDPAddr}
}
2022-07-07 21:47:21 +08:00
func (d *DefaultDialer) DialContext(ctx context.Context, network string, address M.Socksaddr) (net.Conn, error) {
2022-08-29 19:58:58 +08:00
if !address.IsValid() {
return nil, E.New("invalid address")
}
2022-08-22 14:35:05 +08:00
switch N.NetworkName(network) {
case N.NetworkUDP:
return d.udpDialer.DialContext(ctx, network, address.String())
}
return d.dialer.DialContext(ctx, network, address.Unwrap().String())
}
2022-07-07 21:47:21 +08:00
func (d *DefaultDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
2022-08-22 14:35:05 +08:00
return d.udpListener.ListenPacket(ctx, N.NetworkUDP, d.bindUDPAddr)
2022-07-07 21:47:21 +08:00
}