58 lines
1.6 KiB
Go
Raw Normal View History

2016-06-14 22:54:08 +02:00
package internet
import (
"net"
2017-01-03 14:53:59 +01:00
2016-12-04 09:10:47 +01:00
"v2ray.com/core/common/errors"
2016-11-19 22:40:06 +01:00
"v2ray.com/core/common/log"
2016-08-20 20:55:45 +02:00
v2net "v2ray.com/core/common/net"
2016-06-14 22:54:08 +02:00
)
2016-09-30 16:53:40 +02:00
type DialerOptions struct {
2016-11-10 23:41:28 +01:00
Stream *StreamConfig
Proxy *ProxyConfig
2016-09-30 16:53:40 +02:00
}
type Dialer func(src v2net.Address, dest v2net.Destination, options DialerOptions) (Connection, error)
2016-06-14 22:54:08 +02:00
var (
transportDialerCache = make(map[TransportProtocol]Dialer)
2017-01-03 15:16:48 +01:00
2017-01-03 14:53:59 +01:00
ProxyDialer Dialer
2016-06-14 22:54:08 +02:00
)
func RegisterTransportDialer(protocol TransportProtocol, dialer Dialer) error {
if _, found := transportDialerCache[protocol]; found {
return errors.New("Internet|Dialer: ", protocol, " dialer already registered.")
2017-01-03 15:16:48 +01:00
}
transportDialerCache[protocol] = dialer
2017-01-03 15:16:48 +01:00
return nil
}
2016-11-09 00:17:09 +01:00
func Dial(src v2net.Address, dest v2net.Destination, options DialerOptions) (Connection, error) {
2016-11-10 23:41:28 +01:00
if options.Proxy.HasTag() && ProxyDialer != nil {
2016-11-19 22:40:06 +01:00
log.Info("Internet: Proxying outbound connection through: ", options.Proxy.Tag)
2016-11-09 00:17:09 +01:00
return ProxyDialer(src, dest, options)
}
2016-08-13 21:42:56 +08:00
2016-09-20 11:53:05 +02:00
if dest.Network == v2net.Network_TCP {
protocol := options.Stream.GetEffectiveProtocol()
dialer := transportDialerCache[protocol]
2017-01-03 15:16:48 +01:00
if dialer == nil {
return nil, errors.New("Internet|Dialer: ", options.Stream.Protocol, " dialer not registered.")
2016-06-14 22:54:08 +02:00
}
2017-01-03 15:16:48 +01:00
return dialer(src, dest, options)
2016-06-14 22:54:08 +02:00
}
udpDialer := transportDialerCache[TransportProtocol_UDP]
2017-01-03 15:16:48 +01:00
if udpDialer == nil {
return nil, errors.New("Internet|Dialer: UDP dialer not registered.")
}
return udpDialer(src, dest, options)
2016-06-14 22:54:08 +02:00
}
2017-01-03 15:16:48 +01:00
// DialSystem calls system dialer to create a network connection.
func DialSystem(src v2net.Address, dest v2net.Destination) (net.Conn, error) {
2016-07-26 17:11:58 +02:00
return effectiveSystemDialer.Dial(src, dest)
2016-06-14 22:54:08 +02:00
}