1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 01:57:12 -05:00

Support using custom resolver when dialing domain address

This commit is contained in:
Vigilans 2023-02-18 02:50:20 +08:00 committed by Shelikhoo
parent 44be94a0e7
commit b1d38db30a
No known key found for this signature in database
GPG Key ID: C4D5E79D22B25316
2 changed files with 21 additions and 1 deletions

View File

@ -51,6 +51,8 @@ type Outbound struct {
Target net.Destination
// Gateway address
Gateway net.Address
// Domain resolver to use when dialing
Resolver func(ctx context.Context, domain string) net.Address
}
// SniffingRequest controls the behavior of content sniffing.

View File

@ -68,8 +68,10 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *MemoryStrea
// DialSystem calls system dialer to create a network connection.
func DialSystem(ctx context.Context, dest net.Destination, sockopt *SocketConfig) (net.Conn, error) {
outbound := session.OutboundFromContext(ctx)
var src net.Address
if outbound := session.OutboundFromContext(ctx); outbound != nil {
if outbound != nil {
src = outbound.Gateway
}
@ -77,6 +79,22 @@ func DialSystem(ctx context.Context, dest net.Destination, sockopt *SocketConfig
return DialTaggedOutbound(ctx, dest, transportLayerOutgoingTag)
}
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))
}
return effectiveSystemDialer.Dial(ctx, src, dest, sockopt)
}