2022-07-03 20:59:25 +08:00
|
|
|
package dialer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
2022-07-10 08:18:52 +08:00
|
|
|
"github.com/sagernet/sing-box/adapter"
|
2023-03-03 19:26:54 +08:00
|
|
|
"github.com/sagernet/sing-box/common/dialer/conntrack"
|
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-03 20:59:25 +08:00
|
|
|
)
|
|
|
|
|
2022-07-07 21:47:21 +08:00
|
|
|
type DefaultDialer struct {
|
2023-08-16 17:47:24 +08:00
|
|
|
dialer4 tcpDialer
|
|
|
|
dialer6 tcpDialer
|
2022-11-03 11:51:09 +08:00
|
|
|
udpDialer4 net.Dialer
|
|
|
|
udpDialer6 net.Dialer
|
2022-08-22 14:35:05 +08:00
|
|
|
udpListener net.ListenConfig
|
2022-11-03 11:51:09 +08:00
|
|
|
udpAddr4 string
|
|
|
|
udpAddr6 string
|
2022-07-03 20:59:25 +08:00
|
|
|
}
|
|
|
|
|
2023-08-08 16:14:03 +08:00
|
|
|
func NewDefault(router adapter.Router, options option.DialerOptions) (*DefaultDialer, error) {
|
2022-07-03 20:59:25 +08:00
|
|
|
var dialer net.Dialer
|
|
|
|
var listener net.ListenConfig
|
|
|
|
if options.BindInterface != "" {
|
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)
|
2022-07-10 08:18:52 +08:00
|
|
|
} else if router.AutoDetectInterface() {
|
2022-10-25 12:55:00 +08:00
|
|
|
bindFunc := router.AutoDetectInterfaceFunc()
|
2022-09-14 12:46:02 +08:00
|
|
|
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)
|
2022-07-03 20:59:25 +08:00
|
|
|
}
|
|
|
|
if options.RoutingMark != 0 {
|
|
|
|
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()))
|
2022-07-03 20:59:25 +08:00
|
|
|
}
|
|
|
|
if options.ReuseAddr {
|
|
|
|
listener.Control = control.Append(listener.Control, control.ReuseAddr())
|
|
|
|
}
|
2022-07-06 14:45:56 +08:00
|
|
|
if options.ProtectPath != "" {
|
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
|
|
|
}
|
2022-07-03 20:59:25 +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-03 20:59:25 +08:00
|
|
|
}
|
2022-09-21 15:32:32 +08:00
|
|
|
var udpFragment bool
|
|
|
|
if options.UDPFragment != nil {
|
|
|
|
udpFragment = *options.UDPFragment
|
|
|
|
} else {
|
|
|
|
udpFragment = options.UDPFragmentDefault
|
|
|
|
}
|
|
|
|
if !udpFragment {
|
2022-09-06 00:54:57 +08:00
|
|
|
dialer.Control = control.Append(dialer.Control, control.DisableUDPFragment())
|
|
|
|
listener.Control = control.Append(listener.Control, control.DisableUDPFragment())
|
|
|
|
}
|
2022-11-03 11:51:09 +08:00
|
|
|
var (
|
|
|
|
dialer4 = dialer
|
|
|
|
udpDialer4 = dialer
|
|
|
|
udpAddr4 string
|
|
|
|
)
|
|
|
|
if options.Inet4BindAddress != nil {
|
|
|
|
bindAddr := options.Inet4BindAddress.Build()
|
|
|
|
dialer4.LocalAddr = &net.TCPAddr{IP: bindAddr.AsSlice()}
|
|
|
|
udpDialer4.LocalAddr = &net.UDPAddr{IP: bindAddr.AsSlice()}
|
|
|
|
udpAddr4 = M.SocksaddrFrom(bindAddr, 0).String()
|
2022-08-25 14:49:49 +08:00
|
|
|
}
|
2022-11-03 11:51:09 +08:00
|
|
|
var (
|
|
|
|
dialer6 = dialer
|
|
|
|
udpDialer6 = dialer
|
|
|
|
udpAddr6 string
|
|
|
|
)
|
|
|
|
if options.Inet6BindAddress != nil {
|
|
|
|
bindAddr := options.Inet6BindAddress.Build()
|
|
|
|
dialer6.LocalAddr = &net.TCPAddr{IP: bindAddr.AsSlice()}
|
|
|
|
udpDialer6.LocalAddr = &net.UDPAddr{IP: bindAddr.AsSlice()}
|
|
|
|
udpAddr6 = M.SocksaddrFrom(bindAddr, 0).String()
|
|
|
|
}
|
2023-08-08 16:14:03 +08:00
|
|
|
if options.TCPMultiPath {
|
2023-08-16 17:47:24 +08:00
|
|
|
if !go121Available {
|
2023-08-08 16:14:03 +08:00
|
|
|
return nil, E.New("MultiPath TCP requires go1.21, please recompile your binary.")
|
|
|
|
}
|
|
|
|
setMultiPathTCP(&dialer4)
|
|
|
|
}
|
2023-08-16 17:47:24 +08:00
|
|
|
tcpDialer4, err := newTCPDialer(dialer4, options.TCPFastOpen)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tcpDialer6, err := newTCPDialer(dialer6, options.TCPFastOpen)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-11-03 11:51:09 +08:00
|
|
|
return &DefaultDialer{
|
2023-08-16 17:47:24 +08:00
|
|
|
tcpDialer4,
|
|
|
|
tcpDialer6,
|
2022-11-03 11:51:09 +08:00
|
|
|
udpDialer4,
|
|
|
|
udpDialer6,
|
|
|
|
listener,
|
|
|
|
udpAddr4,
|
|
|
|
udpAddr6,
|
2023-08-08 16:14:03 +08:00
|
|
|
}, nil
|
2022-07-03 20:59:25 +08:00
|
|
|
}
|
|
|
|
|
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:
|
2022-11-03 11:51:09 +08:00
|
|
|
if !address.IsIPv6() {
|
2023-03-31 18:40:08 +08:00
|
|
|
return trackConn(d.udpDialer4.DialContext(ctx, network, address.String()))
|
2022-11-03 11:51:09 +08:00
|
|
|
} else {
|
2023-03-31 18:40:08 +08:00
|
|
|
return trackConn(d.udpDialer6.DialContext(ctx, network, address.String()))
|
2022-11-03 11:51:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !address.IsIPv6() {
|
2023-03-03 19:26:54 +08:00
|
|
|
return trackConn(DialSlowContext(&d.dialer4, ctx, network, address))
|
2022-11-03 11:51:09 +08:00
|
|
|
} else {
|
2023-03-03 19:26:54 +08:00
|
|
|
return trackConn(DialSlowContext(&d.dialer6, ctx, network, address))
|
2022-08-22 14:35:05 +08:00
|
|
|
}
|
2022-07-03 20:59:25 +08:00
|
|
|
}
|
|
|
|
|
2022-07-07 21:47:21 +08:00
|
|
|
func (d *DefaultDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
|
2022-11-28 11:47:56 +08:00
|
|
|
if !destination.IsIPv6() {
|
2023-03-03 19:26:54 +08:00
|
|
|
return trackPacketConn(d.udpListener.ListenPacket(ctx, N.NetworkUDP, d.udpAddr4))
|
2022-11-03 11:51:09 +08:00
|
|
|
} else {
|
2023-03-03 19:26:54 +08:00
|
|
|
return trackPacketConn(d.udpListener.ListenPacket(ctx, N.NetworkUDP, d.udpAddr6))
|
2022-11-03 11:51:09 +08:00
|
|
|
}
|
2022-07-07 21:47:21 +08:00
|
|
|
}
|
2023-03-03 19:26:54 +08:00
|
|
|
|
|
|
|
func trackConn(conn net.Conn, err error) (net.Conn, error) {
|
|
|
|
if !conntrack.Enabled || err != nil {
|
|
|
|
return conn, err
|
|
|
|
}
|
2023-03-16 10:55:06 +08:00
|
|
|
return conntrack.NewConn(conn)
|
2023-03-03 19:26:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func trackPacketConn(conn net.PacketConn, err error) (net.PacketConn, error) {
|
|
|
|
if !conntrack.Enabled || err != nil {
|
|
|
|
return conn, err
|
|
|
|
}
|
2023-03-16 10:55:06 +08:00
|
|
|
return conntrack.NewPacketConn(conn)
|
2023-03-03 19:26:54 +08:00
|
|
|
}
|