1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-18 09:57:04 -04:00
v2fly/transport/internet/dialer.go

72 lines
2.1 KiB
Go
Raw Normal View History

2016-06-14 16:54:08 -04:00
package internet
import (
"context"
2017-01-03 08:53:59 -05:00
"v2ray.com/core/common/net"
2018-10-14 02:23:49 -04:00
"v2ray.com/core/common/session"
2016-06-14 16:54:08 -04:00
)
2018-10-22 16:12:50 -04:00
// Dialer is the interface for dialing outbound connections.
type Dialer interface {
// Dial dials a system connection to the given destination.
Dial(ctx context.Context, destination net.Destination) (Connection, error)
}
// dialFunc is an interface to dial network connection to a specific destination.
type dialFunc func(ctx context.Context, dest net.Destination) (Connection, error)
2016-06-14 16:54:08 -04:00
var (
2018-10-22 16:12:50 -04:00
transportDialerCache = make(map[string]dialFunc)
2016-06-14 16:54:08 -04:00
)
2018-10-14 02:23:49 -04:00
// RegisterTransportDialer registers a Dialer with given name.
2018-10-22 16:12:50 -04:00
func RegisterTransportDialer(protocol string, dialer dialFunc) error {
if _, found := transportDialerCache[protocol]; found {
2017-04-08 19:43:25 -04:00
return newError(protocol, " dialer already registered").AtError()
2017-01-03 09:16:48 -05:00
}
transportDialerCache[protocol] = dialer
2017-01-03 09:16:48 -05:00
return nil
}
2018-04-17 17:33:39 -04:00
// Dial dials a internet connection towards the given destination.
func Dial(ctx context.Context, dest net.Destination) (Connection, error) {
if dest.Network == net.Network_TCP {
2017-02-26 08:38:41 -05:00
streamSettings := StreamSettingsFromContext(ctx)
2018-09-07 09:00:46 -04:00
if streamSettings == nil {
s, err := ToMemoryStreamConfig(nil)
if err != nil {
2018-09-07 09:00:46 -04:00
return nil, newError("failed to create default stream settings").Base(err)
}
2018-09-07 09:00:46 -04:00
streamSettings = s
ctx = ContextWithStreamSettings(ctx, streamSettings)
}
2018-09-07 09:00:46 -04:00
2018-09-07 09:18:08 -04:00
protocol := streamSettings.ProtocolName
dialer := transportDialerCache[protocol]
2017-01-03 09:16:48 -05:00
if dialer == nil {
2017-04-08 19:43:25 -04:00
return nil, newError(protocol, " dialer not registered").AtError()
2016-06-14 16:54:08 -04:00
}
return dialer(ctx, dest)
2016-06-14 16:54:08 -04:00
}
2018-09-15 15:35:32 -04:00
if dest.Network == net.Network_UDP {
udpDialer := transportDialerCache["udp"]
if udpDialer == nil {
return nil, newError("UDP dialer not registered").AtError()
}
return udpDialer(ctx, dest)
2017-01-03 09:16:48 -05:00
}
2018-09-15 15:35:32 -04:00
return nil, newError("unknown network ", dest.Network)
2016-06-14 16:54:08 -04:00
}
2017-01-03 09:16:48 -05:00
// DialSystem calls system dialer to create a network connection.
func DialSystem(ctx context.Context, dest net.Destination) (net.Conn, error) {
var src net.Address
if outbound := session.OutboundFromContext(ctx); outbound != nil {
src = outbound.Gateway
}
2017-02-23 17:48:47 -05:00
return effectiveSystemDialer.Dial(ctx, src, dest)
2016-06-14 16:54:08 -04:00
}