sing-box/common/dialer/default.go

133 lines
4.7 KiB
Go
Raw Normal View History

package dialer
import (
"context"
"net"
"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-24 21:29:39 +08:00
"github.com/sagernet/sing/common"
2022-07-08 23:03:57 +08:00
"github.com/sagernet/sing/common/control"
M "github.com/sagernet/sing/common/metadata"
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 {
return !(C.IsLinux || C.IsWindows)
},
"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 {
tfo.Dialer
net.ListenConfig
}
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-07-14 23:06:03 +08:00
dialer.Control = control.Append(dialer.Control, control.BindToInterface(router.InterfaceBindManager(), options.BindInterface))
listener.Control = control.Append(listener.Control, control.BindToInterface(router.InterfaceBindManager(), options.BindInterface))
} else if router.AutoDetectInterface() {
2022-07-24 22:54:16 +08:00
if C.IsWindows {
2022-07-14 23:06:03 +08:00
dialer.Control = control.Append(dialer.Control, control.BindToInterfaceIndexFunc(func() int {
2022-07-15 11:51:51 +08:00
return router.AutoDetectInterfaceIndex()
2022-07-14 23:06:03 +08:00
}))
listener.Control = control.Append(listener.Control, control.BindToInterfaceIndexFunc(func() int {
2022-07-15 11:51:51 +08:00
return router.AutoDetectInterfaceIndex()
2022-07-14 23:06:03 +08:00
}))
} else {
dialer.Control = control.Append(dialer.Control, control.BindToInterfaceFunc(router.InterfaceBindManager(), func() string {
2022-07-15 11:51:51 +08:00
return router.AutoDetectInterfaceName()
2022-07-14 23:06:03 +08:00
}))
listener.Control = control.Append(listener.Control, control.BindToInterfaceFunc(router.InterfaceBindManager(), func() string {
2022-07-15 11:51:51 +08:00
return router.AutoDetectInterfaceName()
2022-07-14 23:06:03 +08:00
}))
}
2022-07-15 11:51:51 +08:00
} else if router.DefaultInterface() != "" {
dialer.Control = control.Append(dialer.Control, control.BindToInterface(router.InterfaceBindManager(), router.DefaultInterface()))
listener.Control = control.Append(listener.Control, control.BindToInterface(router.InterfaceBindManager(), router.DefaultInterface()))
}
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 {
dialer.Timeout = C.DefaultTCPTimeout
}
2022-07-24 22:54:16 +08:00
if options.TCPFastOpen {
warnTFOOnUnsupportedPlatform.Check()
}
2022-07-07 21:47:21 +08:00
return &DefaultDialer{tfo.Dialer{Dialer: dialer, DisableTFO: !options.TCPFastOpen}, listener}
}
2022-07-07 21:47:21 +08:00
func (d *DefaultDialer) DialContext(ctx context.Context, network string, address M.Socksaddr) (net.Conn, error) {
2022-07-24 21:29:39 +08:00
conn, err := d.Dialer.DialContext(ctx, network, address.Unwrap().String())
if err != nil {
return nil, err
}
if tcpConn, isTCP := common.Cast[*net.TCPConn](conn); isTCP {
tcpConn.SetKeepAlive(true)
tcpConn.SetKeepAlivePeriod(C.DefaultTCPKeepAlivePeriod)
}
return conn, nil
}
2022-07-07 21:47:21 +08:00
func (d *DefaultDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
return d.ListenConfig.ListenPacket(ctx, C.NetworkUDP, "")
}
2022-07-07 21:47:21 +08:00
func (d *DefaultDialer) Upstream() any {
return &d.Dialer
}