mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-08-23 01:56:48 +08:00
Some refines related to direct/freedom and targetStrategy
; More intelligent "useIP"/"ForceIP", enhance "origin" functionality (#5030)
https://github.com/XTLS/Xray-core/pull/5009#issuecomment-3194264277
This commit is contained in:
@@ -24,11 +24,11 @@ type Dialer interface {
|
||||
// Dial dials a system connection to the given destination.
|
||||
Dial(ctx context.Context, destination net.Destination) (stat.Connection, error)
|
||||
|
||||
// Address returns the address used by this Dialer. Maybe nil if not known.
|
||||
Address() net.Address
|
||||
|
||||
// DestIpAddress returns the ip of proxy server. It is useful in case of Android client, which prepare an IP before proxy connection is established
|
||||
DestIpAddress() net.IP
|
||||
|
||||
// SetOutboundGateway set outbound gateway
|
||||
SetOutboundGateway(ctx context.Context, ob *session.Outbound)
|
||||
}
|
||||
|
||||
// dialFunc is an interface to dial network connection to a specific destination.
|
||||
@@ -91,8 +91,8 @@ func LookupForIP(domain string, strategy DomainStrategy, localAddr net.Address)
|
||||
}
|
||||
|
||||
ips, _, err := dnsClient.LookupIP(domain, dns.IPOption{
|
||||
IPv4Enable: (localAddr == nil || localAddr.Family().IsIPv4()) && strategy.PreferIP4(),
|
||||
IPv6Enable: (localAddr == nil || localAddr.Family().IsIPv6()) && strategy.PreferIP6(),
|
||||
IPv4Enable: (localAddr == nil && strategy.PreferIP4()) || (localAddr != nil && localAddr.Family().IsIPv4() && (strategy.PreferIP4() || strategy.FallbackIP4())),
|
||||
IPv6Enable: (localAddr == nil && strategy.PreferIP6()) || (localAddr != nil && localAddr.Family().IsIPv6() && (strategy.PreferIP6() || strategy.FallbackIP6())),
|
||||
})
|
||||
{ // Resolve fallback
|
||||
if (len(ips) == 0 || err != nil) && strategy.HasFallback() && localAddr == nil {
|
||||
@@ -109,13 +109,6 @@ func LookupForIP(domain string, strategy DomainStrategy, localAddr net.Address)
|
||||
return ips, err
|
||||
}
|
||||
|
||||
func canLookupIP(dst net.Destination, sockopt *SocketConfig) bool {
|
||||
if dst.Address.Family().IsIP() {
|
||||
return false
|
||||
}
|
||||
return sockopt.DomainStrategy.HasStrategy()
|
||||
}
|
||||
|
||||
func redirect(ctx context.Context, dst net.Destination, obt string, h outbound.Handler) net.Conn {
|
||||
errors.LogInfo(ctx, "redirecting request "+dst.String()+" to "+obt)
|
||||
outbounds := session.OutboundsFromContext(ctx)
|
||||
@@ -235,9 +228,15 @@ func checkAddressPortStrategy(ctx context.Context, dest net.Destination, sockopt
|
||||
func DialSystem(ctx context.Context, dest net.Destination, sockopt *SocketConfig) (net.Conn, error) {
|
||||
var src net.Address
|
||||
outbounds := session.OutboundsFromContext(ctx)
|
||||
var outboundName string
|
||||
var origTargetAddr net.Address
|
||||
if len(outbounds) > 0 {
|
||||
ob := outbounds[len(outbounds)-1]
|
||||
src = ob.Gateway
|
||||
if sockopt == nil || len(sockopt.DialerProxy) == 0 {
|
||||
src = ob.Gateway
|
||||
}
|
||||
outboundName = ob.Name
|
||||
origTargetAddr = ob.OriginalTarget.Address
|
||||
}
|
||||
if sockopt == nil {
|
||||
return effectiveSystemDialer.Dial(ctx, src, dest, sockopt)
|
||||
@@ -248,8 +247,12 @@ func DialSystem(ctx context.Context, dest net.Destination, sockopt *SocketConfig
|
||||
dest = *newDest
|
||||
}
|
||||
|
||||
if canLookupIP(dest, sockopt) {
|
||||
ips, err := LookupForIP(dest.Address.String(), sockopt.DomainStrategy, src)
|
||||
if sockopt.DomainStrategy.HasStrategy() && dest.Address.Family().IsDomain() {
|
||||
finalStrategy := sockopt.DomainStrategy
|
||||
if outboundName == "freedom" && dest.Network == net.Network_UDP && origTargetAddr != nil && src == nil {
|
||||
finalStrategy = finalStrategy.GetDynamicStrategy(origTargetAddr.Family())
|
||||
}
|
||||
ips, err := LookupForIP(dest.Address.Domain(), finalStrategy, src)
|
||||
if err != nil {
|
||||
errors.LogErrorInner(ctx, err, "failed to resolve ip")
|
||||
if sockopt.DomainStrategy.ForceIP() {
|
||||
|
Reference in New Issue
Block a user