2016-06-14 20:54:08 +00:00
|
|
|
package internet
|
|
|
|
|
|
|
|
import (
|
2017-01-26 19:46:44 +00:00
|
|
|
"context"
|
2016-06-14 20:54:08 +00:00
|
|
|
"net"
|
2017-01-03 13:53:59 +00:00
|
|
|
|
2016-08-20 18:55:45 +00:00
|
|
|
v2net "v2ray.com/core/common/net"
|
2016-06-14 20:54:08 +00:00
|
|
|
)
|
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
type Dialer func(ctx context.Context, dest v2net.Destination) (Connection, error)
|
2016-06-14 20:54:08 +00:00
|
|
|
|
|
|
|
var (
|
2017-01-12 11:54:34 +00:00
|
|
|
transportDialerCache = make(map[TransportProtocol]Dialer)
|
2016-06-14 20:54:08 +00:00
|
|
|
)
|
|
|
|
|
2017-01-12 11:54:34 +00:00
|
|
|
func RegisterTransportDialer(protocol TransportProtocol, dialer Dialer) error {
|
|
|
|
if _, found := transportDialerCache[protocol]; found {
|
2017-04-08 23:43:25 +00:00
|
|
|
return newError(protocol, " dialer already registered").AtError()
|
2017-01-03 14:16:48 +00:00
|
|
|
}
|
2017-01-12 11:54:34 +00:00
|
|
|
transportDialerCache[protocol] = dialer
|
2017-01-03 14:16:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
func Dial(ctx context.Context, dest v2net.Destination) (Connection, error) {
|
2016-09-20 09:53:05 +00:00
|
|
|
if dest.Network == v2net.Network_TCP {
|
2017-02-26 13:38:41 +00:00
|
|
|
streamSettings := StreamSettingsFromContext(ctx)
|
2017-01-26 19:46:44 +00:00
|
|
|
protocol := streamSettings.GetEffectiveProtocol()
|
|
|
|
transportSettings, err := streamSettings.GetEffectiveTransportSettings()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ctx = ContextWithTransportSettings(ctx, transportSettings)
|
|
|
|
if streamSettings != nil && streamSettings.HasSecuritySettings() {
|
|
|
|
securitySettings, err := streamSettings.GetEffectiveSecuritySettings()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ctx = ContextWithSecuritySettings(ctx, securitySettings)
|
|
|
|
}
|
2017-01-12 11:54:34 +00:00
|
|
|
dialer := transportDialerCache[protocol]
|
2017-01-03 14:16:48 +00:00
|
|
|
if dialer == nil {
|
2017-04-08 23:43:25 +00:00
|
|
|
return nil, newError(protocol, " dialer not registered").AtError()
|
2016-06-14 20:54:08 +00:00
|
|
|
}
|
2017-01-26 19:46:44 +00:00
|
|
|
return dialer(ctx, dest)
|
2016-06-14 20:54:08 +00:00
|
|
|
}
|
|
|
|
|
2017-01-12 11:54:34 +00:00
|
|
|
udpDialer := transportDialerCache[TransportProtocol_UDP]
|
2017-01-03 14:16:48 +00:00
|
|
|
if udpDialer == nil {
|
2017-04-08 23:43:25 +00:00
|
|
|
return nil, newError("UDP dialer not registered").AtError()
|
2017-01-03 14:16:48 +00:00
|
|
|
}
|
2017-01-26 19:46:44 +00:00
|
|
|
return udpDialer(ctx, dest)
|
2016-06-14 20:54:08 +00:00
|
|
|
}
|
|
|
|
|
2017-01-03 14:16:48 +00:00
|
|
|
// DialSystem calls system dialer to create a network connection.
|
2017-02-23 22:48:47 +00:00
|
|
|
func DialSystem(ctx context.Context, src v2net.Address, dest v2net.Destination) (net.Conn, error) {
|
|
|
|
return effectiveSystemDialer.Dial(ctx, src, dest)
|
2016-06-14 20:54:08 +00:00
|
|
|
}
|