2022-07-03 12:59:25 +00:00
|
|
|
package dialer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/sagernet/sing/common/control"
|
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
2022-07-06 07:01:09 +00:00
|
|
|
|
|
|
|
C "github.com/sagernet/sing-box/constant"
|
|
|
|
"github.com/sagernet/sing-box/option"
|
|
|
|
|
|
|
|
"github.com/database64128/tfo-go"
|
2022-07-03 12:59:25 +00:00
|
|
|
)
|
|
|
|
|
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-07 13:47:21 +00:00
|
|
|
func NewDefault(options option.DialerOptions) *DefaultDialer {
|
2022-07-03 12:59:25 +00:00
|
|
|
var dialer net.Dialer
|
|
|
|
var listener net.ListenConfig
|
|
|
|
if options.BindInterface != "" {
|
|
|
|
dialer.Control = control.Append(dialer.Control, control.BindToInterface(options.BindInterface))
|
|
|
|
listener.Control = control.Append(listener.Control, control.BindToInterface(options.BindInterface))
|
|
|
|
}
|
|
|
|
if options.RoutingMark != 0 {
|
|
|
|
dialer.Control = control.Append(dialer.Control, control.RoutingMark(options.RoutingMark))
|
|
|
|
listener.Control = control.Append(listener.Control, control.RoutingMark(options.RoutingMark))
|
|
|
|
}
|
|
|
|
if options.ReuseAddr {
|
|
|
|
listener.Control = control.Append(listener.Control, control.ReuseAddr())
|
|
|
|
}
|
2022-07-06 06:45:56 +00:00
|
|
|
if options.ProtectPath != "" {
|
|
|
|
dialer.Control = control.Append(dialer.Control, ProtectPath(options.ProtectPath))
|
|
|
|
listener.Control = control.Append(listener.Control, ProtectPath(options.ProtectPath))
|
|
|
|
}
|
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-03 12:59:25 +00:00
|
|
|
}
|
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-03 12:59:25 +00:00
|
|
|
return d.Dialer.DialContext(ctx, network, address.String())
|
|
|
|
}
|
|
|
|
|
2022-07-07 13:47:21 +00:00
|
|
|
func (d *DefaultDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
|
2022-07-03 12:59:25 +00:00
|
|
|
return d.ListenConfig.ListenPacket(ctx, C.NetworkUDP, "")
|
|
|
|
}
|
2022-07-07 13:47:21 +00:00
|
|
|
|
|
|
|
func (d *DefaultDialer) Upstream() any {
|
|
|
|
return &d.Dialer
|
|
|
|
}
|