2016-06-14 16:54:08 -04:00
|
|
|
package internet
|
|
|
|
|
|
|
|
import (
|
2017-01-26 14:46:44 -05:00
|
|
|
"context"
|
2021-10-28 06:34:19 -04:00
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v5/common/net"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/session"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/transport/internet/tagged"
|
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)
|
2018-11-19 15:36:46 -05:00
|
|
|
|
|
|
|
// Address returns the address used by this Dialer. Maybe nil if not known.
|
|
|
|
Address() net.Address
|
2018-10-22 16:12:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// dialFunc is an interface to dial network connection to a specific destination.
|
2018-11-21 08:54:40 -05:00
|
|
|
type dialFunc func(ctx context.Context, dest net.Destination, streamSettings *MemoryStreamConfig) (Connection, error)
|
2016-06-14 16:54:08 -04:00
|
|
|
|
2021-05-19 17:28:52 -04:00
|
|
|
var 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 {
|
2017-01-12 06:54:34 -05:00
|
|
|
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
|
|
|
}
|
2017-01-12 06:54:34 -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.
|
2018-11-21 08:54:40 -05:00
|
|
|
func Dial(ctx context.Context, dest net.Destination, streamSettings *MemoryStreamConfig) (Connection, error) {
|
2017-08-29 06:56:57 -04:00
|
|
|
if dest.Network == net.Network_TCP {
|
2018-09-07 09:00:46 -04:00
|
|
|
if streamSettings == nil {
|
|
|
|
s, err := ToMemoryStreamConfig(nil)
|
2017-01-26 14:46:44 -05:00
|
|
|
if err != nil {
|
2018-09-07 09:00:46 -04:00
|
|
|
return nil, newError("failed to create default stream settings").Base(err)
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
2018-09-07 09:00:46 -04:00
|
|
|
streamSettings = s
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
2018-09-07 09:00:46 -04:00
|
|
|
|
2018-09-07 09:18:08 -04:00
|
|
|
protocol := streamSettings.ProtocolName
|
2021-09-07 11:53:09 -04:00
|
|
|
|
|
|
|
if originalProtocolName := getOriginalMessageName(streamSettings); originalProtocolName != "" {
|
|
|
|
protocol = originalProtocolName
|
|
|
|
}
|
|
|
|
|
2017-01-12 06:54:34 -05:00
|
|
|
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
|
|
|
}
|
2018-11-21 08:54:40 -05:00
|
|
|
return dialer(ctx, dest, streamSettings)
|
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()
|
|
|
|
}
|
2018-11-21 08:54:40 -05:00
|
|
|
return udpDialer(ctx, dest, streamSettings)
|
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.
|
2018-11-21 08:54:40 -05:00
|
|
|
func DialSystem(ctx context.Context, dest net.Destination, sockopt *SocketConfig) (net.Conn, error) {
|
2023-02-17 13:50:20 -05:00
|
|
|
outbound := session.OutboundFromContext(ctx)
|
|
|
|
|
2018-09-18 17:09:54 -04:00
|
|
|
var src net.Address
|
2023-02-17 13:50:20 -05:00
|
|
|
if outbound != nil {
|
2018-09-18 17:09:54 -04:00
|
|
|
src = outbound.Gateway
|
|
|
|
}
|
2021-02-28 13:47:31 -05:00
|
|
|
|
|
|
|
if transportLayerOutgoingTag := session.GetTransportLayerProxyTagFromContext(ctx); transportLayerOutgoingTag != "" {
|
|
|
|
return DialTaggedOutbound(ctx, dest, transportLayerOutgoingTag)
|
|
|
|
}
|
|
|
|
|
2023-02-17 13:50:20 -05:00
|
|
|
originalAddr := dest.Address
|
|
|
|
if outbound != nil && outbound.Resolver != nil && dest.Address.Family().IsDomain() {
|
|
|
|
if addr := outbound.Resolver(ctx, dest.Address.Domain()); addr != nil {
|
|
|
|
dest.Address = addr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case src != nil && dest.Address != originalAddr:
|
|
|
|
newError("dialing to ", dest, " resolved from ", originalAddr, " via ", src).WriteToLog(session.ExportIDToError(ctx))
|
|
|
|
case src != nil:
|
|
|
|
newError("dialing to ", dest, " via ", src).WriteToLog(session.ExportIDToError(ctx))
|
|
|
|
case dest.Address != originalAddr:
|
|
|
|
newError("dialing to ", dest, " resolved from ", originalAddr).WriteToLog(session.ExportIDToError(ctx))
|
|
|
|
}
|
|
|
|
|
2018-11-21 08:54:40 -05:00
|
|
|
return effectiveSystemDialer.Dial(ctx, src, dest, sockopt)
|
2016-06-14 16:54:08 -04:00
|
|
|
}
|
2021-02-28 13:47:31 -05:00
|
|
|
|
|
|
|
func DialTaggedOutbound(ctx context.Context, dest net.Destination, tag string) (net.Conn, error) {
|
2021-02-28 14:10:38 -05:00
|
|
|
if tagged.Dialer == nil {
|
|
|
|
return nil, newError("tagged dial not enabled")
|
2021-02-28 13:47:31 -05:00
|
|
|
}
|
2021-02-28 14:10:38 -05:00
|
|
|
return tagged.Dialer(ctx, dest, tag)
|
2021-02-28 13:47:31 -05:00
|
|
|
}
|