Skip to content

Commit

Permalink
Support using custom resolver when dialing domain address
Browse files Browse the repository at this point in the history
  • Loading branch information
Vigilans authored and xiaokangwang committed Feb 19, 2023
1 parent 44be94a commit b1d38db
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions common/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 19 additions & 1 deletion transport/internet/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,33 @@ 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
}

if transportLayerOutgoingTag := session.GetTransportLayerProxyTagFromContext(ctx); transportLayerOutgoingTag != "" {
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)
}

Expand Down

0 comments on commit b1d38db

Please sign in to comment.