v2fly/transport/internet/system_dialer.go

90 lines
2.3 KiB
Go
Raw Normal View History

2016-07-26 15:11:58 +00:00
package internet
import (
2017-02-26 13:38:41 +00:00
"context"
2018-09-06 08:06:57 +00:00
"syscall"
2016-07-26 15:11:58 +00:00
"time"
"v2ray.com/core/common/net"
2018-09-06 08:06:57 +00:00
"v2ray.com/core/common/session"
2016-07-26 15:11:58 +00:00
)
var (
2017-05-01 22:28:06 +00:00
effectiveSystemDialer SystemDialer = DefaultSystemDialer{}
2016-07-26 15:11:58 +00:00
)
type SystemDialer interface {
Dial(ctx context.Context, source net.Address, destination net.Destination, sockopt *SocketConfig) (net.Conn, error)
2016-07-26 15:11:58 +00:00
}
type DefaultSystemDialer struct {
}
func (DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest net.Destination, sockopt *SocketConfig) (net.Conn, error) {
2016-07-26 15:11:58 +00:00
dialer := &net.Dialer{
Timeout: time.Second * 60,
DualStack: true,
}
2018-09-06 08:06:57 +00:00
if sockopt != nil {
2018-09-06 08:06:57 +00:00
dialer.Control = func(network, address string, c syscall.RawConn) error {
return c.Control(func(fd uintptr) {
if err := applyOutboundSocketOptions(network, address, fd, sockopt); err != nil {
2018-09-06 08:06:57 +00:00
newError("failed to apply socket options").Base(err).WriteToLog(session.ExportIDToError(ctx))
}
if dest.Network == net.Network_UDP && len(sockopt.BindAddress) > 0 && sockopt.BindPort > 0 {
if err := bindAddr(fd, sockopt.BindAddress, sockopt.BindPort); err != nil {
newError("failed to bind source address to ", sockopt.BindAddress).Base(err).WriteToLog(session.ExportIDToError(ctx))
2018-09-17 13:12:58 +00:00
}
}
2018-09-06 08:06:57 +00:00
})
}
}
if src != nil && src != net.AnyIP {
2016-07-26 15:11:58 +00:00
var addr net.Addr
if dest.Network == net.Network_TCP {
2016-07-26 15:11:58 +00:00
addr = &net.TCPAddr{
IP: src.IP(),
Port: 0,
}
} else {
addr = &net.UDPAddr{
IP: src.IP(),
Port: 0,
}
}
dialer.LocalAddr = addr
}
2017-02-23 22:48:47 +00:00
return dialer.DialContext(ctx, dest.Network.SystemString(), dest.NetAddr())
2016-07-26 15:11:58 +00:00
}
type SystemDialerAdapter interface {
Dial(network string, address string) (net.Conn, error)
}
type SimpleSystemDialer struct {
adapter SystemDialerAdapter
}
2016-07-26 15:25:13 +00:00
func WithAdapter(dialer SystemDialerAdapter) SystemDialer {
return &SimpleSystemDialer{
adapter: dialer,
}
}
func (v *SimpleSystemDialer) Dial(ctx context.Context, src net.Address, dest net.Destination, sockopt *SocketConfig) (net.Conn, error) {
2016-11-27 20:39:09 +00:00
return v.adapter.Dial(dest.Network.SystemString(), dest.NetAddr())
2016-07-26 15:11:58 +00:00
}
2016-07-26 15:25:13 +00:00
// UseAlternativeSystemDialer replaces the current system dialer with a given one.
// Caller must ensure there is no race condition.
2018-12-03 21:44:42 +00:00
//
// v2ray:api:stable
2016-07-26 15:11:58 +00:00
func UseAlternativeSystemDialer(dialer SystemDialer) {
2017-04-30 21:37:30 +00:00
if dialer == nil {
2017-05-01 22:28:06 +00:00
effectiveSystemDialer = DefaultSystemDialer{}
2017-04-30 21:37:30 +00:00
}
2016-07-26 15:11:58 +00:00
effectiveSystemDialer = dialer
}