mirror of
https://github.com/xjasonlyu/tun2socks.git
synced 2025-10-08 02:00:43 +08:00
Refactor: new dialer impl
This commit is contained in:
@@ -3,20 +3,56 @@ package dialer
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"syscall"
|
||||
|
||||
"go.uber.org/atomic"
|
||||
)
|
||||
|
||||
func Dial(network, address string) (net.Conn, error) {
|
||||
return DialContext(context.Background(), network, address)
|
||||
var (
|
||||
DefaultInterfaceName = atomic.NewString("")
|
||||
DefaultRoutingMark = atomic.NewInt32(0)
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
// InterfaceName is the name of interface/device to bind.
|
||||
// If a socket is bound to an interface, only packets received
|
||||
// from that particular interface are processed by the socket.
|
||||
InterfaceName string
|
||||
|
||||
// RoutingMark is the mark for each packet sent through this
|
||||
// socket. Changing the mark can be used for mark-based routing
|
||||
// without netfilter or for packet filtering.
|
||||
RoutingMark int
|
||||
}
|
||||
|
||||
func DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
d := &net.Dialer{}
|
||||
setControl(d)
|
||||
return DialContextWithOptions(ctx, network, address, &Options{
|
||||
InterfaceName: DefaultInterfaceName.Load(),
|
||||
RoutingMark: int(DefaultRoutingMark.Load()),
|
||||
})
|
||||
}
|
||||
|
||||
func DialContextWithOptions(ctx context.Context, network, address string, opts *Options) (net.Conn, error) {
|
||||
d := &net.Dialer{
|
||||
Control: func(network, address string, c syscall.RawConn) error {
|
||||
return setSocketOptions(network, address, c, opts)
|
||||
},
|
||||
}
|
||||
return d.DialContext(ctx, network, address)
|
||||
}
|
||||
|
||||
func ListenPacket(network, address string) (net.PacketConn, error) {
|
||||
lc := &net.ListenConfig{}
|
||||
setControl(lc)
|
||||
return ListenPacketWithOptions(network, address, &Options{
|
||||
InterfaceName: DefaultInterfaceName.Load(),
|
||||
RoutingMark: int(DefaultRoutingMark.Load()),
|
||||
})
|
||||
}
|
||||
|
||||
func ListenPacketWithOptions(network, address string, opts *Options) (net.PacketConn, error) {
|
||||
lc := &net.ListenConfig{
|
||||
Control: func(network, address string, c syscall.RawConn) error {
|
||||
return setSocketOptions(network, address, c, opts)
|
||||
},
|
||||
}
|
||||
return lc.ListenPacket(context.Background(), network, address)
|
||||
}
|
||||
|
Reference in New Issue
Block a user