Refactor struct & Add override dialer options
This commit is contained in:
47
outbound/dialer/default.go
Normal file
47
outbound/dialer/default.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package dialer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/database64128/tfo-go"
|
||||
C "github.com/sagernet/sing-box/constant"
|
||||
"github.com/sagernet/sing-box/option"
|
||||
"github.com/sagernet/sing/common/control"
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
N "github.com/sagernet/sing/common/network"
|
||||
)
|
||||
|
||||
type defaultDialer struct {
|
||||
tfo.Dialer
|
||||
net.ListenConfig
|
||||
}
|
||||
|
||||
func newDefault(options option.DialerOptions) N.Dialer {
|
||||
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())
|
||||
}
|
||||
if options.ConnectTimeout != 0 {
|
||||
dialer.Timeout = time.Duration(options.ConnectTimeout) * time.Second
|
||||
}
|
||||
return &defaultDialer{tfo.Dialer{Dialer: dialer, DisableTFO: !options.TCPFastOpen}, listener}
|
||||
}
|
||||
|
||||
func (d *defaultDialer) DialContext(ctx context.Context, network string, address M.Socksaddr) (net.Conn, error) {
|
||||
return d.Dialer.DialContext(ctx, network, address.String())
|
||||
}
|
||||
|
||||
func (d *defaultDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
|
||||
return d.ListenConfig.ListenPacket(ctx, C.NetworkUDP, "")
|
||||
}
|
||||
52
outbound/dialer/detour.go
Normal file
52
outbound/dialer/detour.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package dialer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"sync"
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
"github.com/sagernet/sing-box/option"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
N "github.com/sagernet/sing/common/network"
|
||||
)
|
||||
|
||||
type detourDialer struct {
|
||||
router adapter.Router
|
||||
options option.DialerOptions
|
||||
dialer N.Dialer
|
||||
initOnce sync.Once
|
||||
initErr error
|
||||
}
|
||||
|
||||
func newDetour(router adapter.Router, options option.DialerOptions) N.Dialer {
|
||||
return &detourDialer{router: router, options: options}
|
||||
}
|
||||
|
||||
func (d *detourDialer) Dialer() (N.Dialer, error) {
|
||||
d.initOnce.Do(func() {
|
||||
var loaded bool
|
||||
d.dialer, loaded = d.router.Outbound(d.options.Detour)
|
||||
if !loaded {
|
||||
d.initErr = E.New("outbound detour not found: ", d.options.Detour)
|
||||
}
|
||||
})
|
||||
return d.dialer, d.initErr
|
||||
}
|
||||
|
||||
func (d *detourDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
|
||||
dialer, err := d.Dialer()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return dialer.DialContext(ctx, network, destination)
|
||||
}
|
||||
|
||||
func (d *detourDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
|
||||
dialer, err := d.Dialer()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return dialer.ListenPacket(ctx, destination)
|
||||
}
|
||||
21
outbound/dialer/dialer.go
Normal file
21
outbound/dialer/dialer.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package dialer
|
||||
|
||||
import (
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
"github.com/sagernet/sing-box/option"
|
||||
"github.com/sagernet/sing/common"
|
||||
N "github.com/sagernet/sing/common/network"
|
||||
)
|
||||
|
||||
func New(router adapter.Router, options option.DialerOptions) N.Dialer {
|
||||
var dialer N.Dialer
|
||||
if options.Detour == "" {
|
||||
dialer = newDefault(options)
|
||||
} else {
|
||||
dialer = newDetour(router, options)
|
||||
}
|
||||
if options.OverrideOptions != nil {
|
||||
dialer = newOverride(dialer, common.PtrValueOrDefault(options.OverrideOptions))
|
||||
}
|
||||
return dialer
|
||||
}
|
||||
68
outbound/dialer/override.go
Normal file
68
outbound/dialer/override.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package dialer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"net"
|
||||
|
||||
C "github.com/sagernet/sing-box/constant"
|
||||
"github.com/sagernet/sing-box/option"
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
N "github.com/sagernet/sing/common/network"
|
||||
"github.com/sagernet/sing/common/uot"
|
||||
)
|
||||
|
||||
var _ N.Dialer = (*overrideDialer)(nil)
|
||||
|
||||
type overrideDialer struct {
|
||||
upstream N.Dialer
|
||||
tlsEnabled bool
|
||||
tlsConfig tls.Config
|
||||
uotEnabled bool
|
||||
}
|
||||
|
||||
func newOverride(upstream N.Dialer, options option.OverrideStreamOptions) N.Dialer {
|
||||
if !options.TLS && !options.UDPOverTCP {
|
||||
return upstream
|
||||
}
|
||||
return &overrideDialer{
|
||||
upstream,
|
||||
options.TLS,
|
||||
tls.Config{
|
||||
ServerName: options.TLSServerName,
|
||||
InsecureSkipVerify: options.TLSInsecure,
|
||||
},
|
||||
options.UDPOverTCP,
|
||||
}
|
||||
}
|
||||
|
||||
func (d *overrideDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
|
||||
switch network {
|
||||
case C.NetworkTCP:
|
||||
conn, err := d.upstream.DialContext(ctx, C.NetworkTCP, destination)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return tls.Client(conn, &d.tlsConfig), nil
|
||||
case C.NetworkUDP:
|
||||
if d.uotEnabled {
|
||||
tcpConn, err := d.upstream.DialContext(ctx, C.NetworkTCP, destination)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return uot.NewClientConn(tcpConn), nil
|
||||
}
|
||||
}
|
||||
return d.upstream.DialContext(ctx, network, destination)
|
||||
}
|
||||
|
||||
func (d *overrideDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
|
||||
if d.uotEnabled {
|
||||
tcpConn, err := d.upstream.DialContext(ctx, C.NetworkTCP, destination)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return uot.NewClientConn(tcpConn), nil
|
||||
}
|
||||
return d.upstream.ListenPacket(ctx, destination)
|
||||
}
|
||||
Reference in New Issue
Block a user