1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-17 13:05:24 +00:00
v2fly/transport/internet/dialer.go

107 lines
3.4 KiB
Go
Raw Normal View History

2016-06-14 20:54:08 +00:00
package internet
import (
"context"
core "github.com/v2fly/v2ray-core/v4"
"github.com/v2fly/v2ray-core/v4/features/routing"
2017-01-03 13:53:59 +00:00
2021-02-16 20:31:50 +00:00
"github.com/v2fly/v2ray-core/v4/common/net"
"github.com/v2fly/v2ray-core/v4/common/session"
2016-06-14 20:54:08 +00:00
)
2018-10-22 20:12:50 +00: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)
// Address returns the address used by this Dialer. Maybe nil if not known.
Address() net.Address
2018-10-22 20:12:50 +00:00
}
// dialFunc is an interface to dial network connection to a specific destination.
type dialFunc func(ctx context.Context, dest net.Destination, streamSettings *MemoryStreamConfig) (Connection, error)
2016-06-14 20:54:08 +00:00
var (
2018-10-22 20:12:50 +00:00
transportDialerCache = make(map[string]dialFunc)
2016-06-14 20:54:08 +00:00
)
2018-10-14 06:23:49 +00:00
// RegisterTransportDialer registers a Dialer with given name.
2018-10-22 20:12:50 +00:00
func RegisterTransportDialer(protocol string, dialer dialFunc) 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
}
transportDialerCache[protocol] = dialer
2017-01-03 14:16:48 +00:00
return nil
}
2018-04-17 21:33:39 +00:00
// Dial dials a internet connection towards the given destination.
func Dial(ctx context.Context, dest net.Destination, streamSettings *MemoryStreamConfig) (Connection, error) {
if dest.Network == net.Network_TCP {
2018-09-07 13:00:46 +00:00
if streamSettings == nil {
s, err := ToMemoryStreamConfig(nil)
if err != nil {
2018-09-07 13:00:46 +00:00
return nil, newError("failed to create default stream settings").Base(err)
}
2018-09-07 13:00:46 +00:00
streamSettings = s
}
2018-09-07 13:00:46 +00:00
2018-09-07 13:18:08 +00:00
protocol := streamSettings.ProtocolName
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
}
return dialer(ctx, dest, streamSettings)
2016-06-14 20:54:08 +00:00
}
2018-09-15 19:35:32 +00: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, streamSettings)
2017-01-03 14:16:48 +00:00
}
2018-09-15 19:35:32 +00:00
return nil, newError("unknown network ", dest.Network)
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.
func DialSystem(ctx context.Context, dest net.Destination, sockopt *SocketConfig) (net.Conn, error) {
var src net.Address
if outbound := session.OutboundFromContext(ctx); outbound != nil {
src = outbound.Gateway
}
if transportLayerOutgoingTag := session.GetTransportLayerProxyTagFromContext(ctx); transportLayerOutgoingTag != "" {
return DialTaggedOutbound(ctx, dest, transportLayerOutgoingTag)
}
return effectiveSystemDialer.Dial(ctx, src, dest, sockopt)
2016-06-14 20:54:08 +00:00
}
func DialTaggedOutbound(ctx context.Context, dest net.Destination, tag string) (net.Conn, error) {
var dispatcher routing.Dispatcher
if err := core.RequireFeatures(ctx, func(dispatcherInstance routing.Dispatcher) {
dispatcher = dispatcherInstance
}); err != nil {
return nil, newError("Required Feature dispatcher not resolved").Base(err)
}
content := new(session.Content)
content.SkipDNSResolve = true
session.SetForcedOutboundTagToContext(ctx, tag)
ctx = session.ContextWithContent(ctx, content)
r, err := dispatcher.Dispatch(ctx, dest)
if err != nil {
return nil, err
}
var readerOpt net.ConnectionOption
if dest.Network == net.Network_TCP {
readerOpt = net.ConnectionOutputMulti(r.Reader)
} else {
readerOpt = net.ConnectionOutputMultiUDP(r.Reader)
}
return net.NewConnection(net.ConnectionInputMulti(r.Writer), readerOpt), nil
}