2022-07-03 12:59:25 +00:00
|
|
|
package dialer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
2022-07-10 00:18:52 +00:00
|
|
|
"github.com/sagernet/sing-box/adapter"
|
2022-07-24 14:54:16 +00:00
|
|
|
"github.com/sagernet/sing-box/common/warning"
|
2022-07-06 07:01:09 +00:00
|
|
|
C "github.com/sagernet/sing-box/constant"
|
|
|
|
"github.com/sagernet/sing-box/option"
|
2022-07-08 15:03:57 +00:00
|
|
|
"github.com/sagernet/sing/common/control"
|
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
2022-07-29 16:29:22 +00:00
|
|
|
N "github.com/sagernet/sing/common/network"
|
2022-07-06 07:01:09 +00:00
|
|
|
|
|
|
|
"github.com/database64128/tfo-go"
|
2022-07-03 12:59:25 +00:00
|
|
|
)
|
|
|
|
|
2022-07-24 14:54:16 +00:00
|
|
|
var warnBindInterfaceOnUnsupportedPlatform = warning.New(
|
|
|
|
func() bool {
|
2022-08-05 08:55:46 +00:00
|
|
|
return !(C.IsLinux || C.IsWindows || C.IsDarwin)
|
2022-07-24 14:54:16 +00: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 13:47:21 +00:00
|
|
|
type DefaultDialer struct {
|
2022-07-03 12:59:25 +00:00
|
|
|
tfo.Dialer
|
|
|
|
net.ListenConfig
|
|
|
|
}
|
|
|
|
|
2022-07-10 00:18:52 +00:00
|
|
|
func NewDefault(router adapter.Router, options option.DialerOptions) *DefaultDialer {
|
2022-07-03 12:59:25 +00:00
|
|
|
var dialer net.Dialer
|
|
|
|
var listener net.ListenConfig
|
|
|
|
if options.BindInterface != "" {
|
2022-07-24 14:54:16 +00:00
|
|
|
warnBindInterfaceOnUnsupportedPlatform.Check()
|
2022-08-04 14:01:20 +00:00
|
|
|
bindFunc := control.BindToInterface(router.InterfaceBindManager(), options.BindInterface)
|
|
|
|
dialer.Control = control.Append(dialer.Control, bindFunc)
|
|
|
|
listener.Control = control.Append(listener.Control, bindFunc)
|
2022-07-10 00:18:52 +00:00
|
|
|
} else if router.AutoDetectInterface() {
|
2022-07-24 14:54:16 +00:00
|
|
|
if C.IsWindows {
|
2022-08-04 14:01:20 +00:00
|
|
|
bindFunc := control.BindToInterfaceIndexFunc(func() int {
|
|
|
|
return router.InterfaceMonitor().DefaultInterfaceIndex()
|
|
|
|
})
|
|
|
|
dialer.Control = control.Append(dialer.Control, bindFunc)
|
|
|
|
listener.Control = control.Append(listener.Control, bindFunc)
|
2022-07-14 15:06:03 +00:00
|
|
|
} else {
|
2022-08-04 14:01:20 +00:00
|
|
|
bindFunc := control.BindToInterfaceFunc(router.InterfaceBindManager(), func() string {
|
|
|
|
return router.InterfaceMonitor().DefaultInterfaceName()
|
|
|
|
})
|
|
|
|
dialer.Control = control.Append(dialer.Control, bindFunc)
|
|
|
|
listener.Control = control.Append(listener.Control, bindFunc)
|
2022-07-14 15:06:03 +00:00
|
|
|
}
|
2022-07-15 03:51:51 +00:00
|
|
|
} else if router.DefaultInterface() != "" {
|
2022-08-04 14:01:20 +00:00
|
|
|
bindFunc := control.BindToInterface(router.InterfaceBindManager(), router.DefaultInterface())
|
|
|
|
dialer.Control = control.Append(dialer.Control, bindFunc)
|
|
|
|
listener.Control = control.Append(listener.Control, bindFunc)
|
2022-07-03 12:59:25 +00:00
|
|
|
}
|
|
|
|
if options.RoutingMark != 0 {
|
2022-07-24 14:54:16 +00:00
|
|
|
warnRoutingMarkOnUnsupportedPlatform.Check()
|
2022-07-03 12:59:25 +00:00
|
|
|
dialer.Control = control.Append(dialer.Control, control.RoutingMark(options.RoutingMark))
|
|
|
|
listener.Control = control.Append(listener.Control, control.RoutingMark(options.RoutingMark))
|
2022-07-24 09:46:25 +00: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()))
|
2022-07-03 12:59:25 +00:00
|
|
|
}
|
|
|
|
if options.ReuseAddr {
|
2022-07-24 14:54:16 +00:00
|
|
|
warnReuseAdderOnUnsupportedPlatform.Check()
|
2022-07-03 12:59:25 +00:00
|
|
|
listener.Control = control.Append(listener.Control, control.ReuseAddr())
|
|
|
|
}
|
2022-07-06 06:45:56 +00:00
|
|
|
if options.ProtectPath != "" {
|
2022-07-24 14:54:16 +00:00
|
|
|
warnProtectPathOnNonAndroid.Check()
|
2022-07-11 10:44:59 +00:00
|
|
|
dialer.Control = control.Append(dialer.Control, control.ProtectPath(options.ProtectPath))
|
|
|
|
listener.Control = control.Append(listener.Control, control.ProtectPath(options.ProtectPath))
|
2022-07-06 06:45:56 +00:00
|
|
|
}
|
2022-07-03 12:59:25 +00:00
|
|
|
if options.ConnectTimeout != 0 {
|
2022-07-08 04:58:43 +00:00
|
|
|
dialer.Timeout = time.Duration(options.ConnectTimeout)
|
2022-07-18 12:40:14 +00:00
|
|
|
} else {
|
2022-07-25 14:02:39 +00:00
|
|
|
dialer.Timeout = C.TCPTimeout
|
2022-07-03 12:59:25 +00:00
|
|
|
}
|
2022-07-24 14:54:16 +00:00
|
|
|
if options.TCPFastOpen {
|
|
|
|
warnTFOOnUnsupportedPlatform.Check()
|
|
|
|
}
|
2022-07-07 13:47:21 +00:00
|
|
|
return &DefaultDialer{tfo.Dialer{Dialer: dialer, DisableTFO: !options.TCPFastOpen}, listener}
|
2022-07-03 12:59:25 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 13:47:21 +00:00
|
|
|
func (d *DefaultDialer) DialContext(ctx context.Context, network string, address M.Socksaddr) (net.Conn, error) {
|
2022-07-31 01:46:05 +00:00
|
|
|
return d.Dialer.DialContext(ctx, network, address.Unwrap().String())
|
2022-07-03 12:59:25 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 13:47:21 +00:00
|
|
|
func (d *DefaultDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
|
2022-07-29 16:29:22 +00:00
|
|
|
return d.ListenConfig.ListenPacket(ctx, N.NetworkUDP, "")
|
2022-07-03 12:59:25 +00:00
|
|
|
}
|
2022-07-07 13:47:21 +00:00
|
|
|
|
|
|
|
func (d *DefaultDialer) Upstream() any {
|
|
|
|
return &d.Dialer
|
|
|
|
}
|