From 0fcf8f4dce8a76007de66ca43fa3af7d3c637313 Mon Sep 17 00:00:00 2001 From: Meo597 <197331664+Meo597@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:56:42 +0800 Subject: [PATCH] refactor --- app/proxyman/outbound/handler.go | 3 +-- app/router/command/config.go | 5 ++--- common/session/context.go | 5 +++++ features/routing/context.go | 2 +- proxy/freedom/freedom.go | 13 +++++++------ transport/internet/dialer.go | 5 +---- 6 files changed, 17 insertions(+), 16 deletions(-) diff --git a/app/proxyman/outbound/handler.go b/app/proxyman/outbound/handler.go index 91952fd1d..5d49c92e1 100644 --- a/app/proxyman/outbound/handler.go +++ b/app/proxyman/outbound/handler.go @@ -181,8 +181,7 @@ func (h *Handler) Tag() string { func (h *Handler) Dispatch(ctx context.Context, link *transport.Link) { outbounds := session.OutboundsFromContext(ctx) ob := outbounds[len(outbounds)-1] - content := session.ContentFromContext(ctx) - if h.senderSettings != nil && h.senderSettings.TargetStrategy.HasStrategy() && ob.Target.Address.Family().IsDomain() && (content == nil || !content.SkipDNSResolve) { + if h.senderSettings != nil && h.senderSettings.TargetStrategy.HasStrategy() && ob.Target.Address.Family().IsDomain() && !session.SkipDNSResolveFromContext(ctx) { strategy := h.senderSettings.TargetStrategy if ob.Target.Network == net.Network_UDP && ob.OriginalTarget.Address != nil { strategy = strategy.GetDynamicStrategy(ob.OriginalTarget.Address.Family()) diff --git a/app/router/command/config.go b/app/router/command/config.go index 13a319b15..9631227bd 100644 --- a/app/router/command/config.go +++ b/app/router/command/config.go @@ -44,9 +44,8 @@ func (c routingContext) GetRuleTag() string { return "" } -// GetSkipDNSResolve is a mock implementation here to match the interface, -// SkipDNSResolve is set from dns module, no use if coming from a protobuf object? -// TODO: please confirm @Vigilans +// GetSkipDNSResolve implements routing.Context. +// The protobuf RoutingContext has no skip flag, so API contexts use false. func (c routingContext) GetSkipDNSResolve() bool { return false } diff --git a/common/session/context.go b/common/session/context.go index abe4219ca..e5c06bc27 100644 --- a/common/session/context.go +++ b/common/session/context.go @@ -77,6 +77,11 @@ func ContentFromContext(ctx context.Context) *Content { return nil } +func SkipDNSResolveFromContext(ctx context.Context) bool { + content := ContentFromContext(ctx) + return content != nil && content.SkipDNSResolve +} + func ContextWithIsReverseMux(ctx context.Context, isReverseMux bool) context.Context { return context.WithValue(ctx, isReverseMuxKey, isReverseMux) } diff --git a/features/routing/context.go b/features/routing/context.go index 6b38d175c..ce02769c9 100644 --- a/features/routing/context.go +++ b/features/routing/context.go @@ -47,6 +47,6 @@ type Context interface { // GetAttributes returns extra attributes from the conneciont content. GetAttributes() map[string]string - // GetSkipDNSResolve returns a flag switch for weather skip dns resolve during route pick. + // GetSkipDNSResolve reports whether built-in DNS resolution should be skipped during routing. GetSkipDNSResolve() bool } diff --git a/proxy/freedom/freedom.go b/proxy/freedom/freedom.go index 88f0ebbe0..cc54dcce9 100644 --- a/proxy/freedom/freedom.go +++ b/proxy/freedom/freedom.go @@ -278,12 +278,6 @@ func isValidAddress(addr *net.IPOrDomain) bool { // Process implements proxy.Outbound. func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer internet.Dialer) error { - if content := session.ContentFromContext(ctx); content != nil && content.SkipDNSResolve { - // Keep DNS connections from resolving their server through the built-in DNS. - handler := *h - handler.resolveStrategy = internet.DomainStrategy_AS_IS - h = &handler - } outbounds := session.OutboundsFromContext(ctx) ob := outbounds[len(outbounds)-1] if !ob.Target.IsValid() { @@ -291,6 +285,13 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte } ob.Name = "freedom" ob.CanSpliceCopy = 1 + + if session.SkipDNSResolveFromContext(ctx) { + handler := *h + handler.resolveStrategy = internet.DomainStrategy_AS_IS + h = &handler + } + inbound := session.InboundFromContext(ctx) var defaultRule *FinalRule if !h.usesDialerProxy { // freedom is not the final outbound, final rules do not apply (and the domain is not resolved) diff --git a/transport/internet/dialer.go b/transport/internet/dialer.go index d11db3776..a47e8832b 100644 --- a/transport/internet/dialer.go +++ b/transport/internet/dialer.go @@ -248,10 +248,7 @@ func DialSystem(ctx context.Context, dest net.Destination, sockopt *SocketConfig dest = *newDest } - content := session.ContentFromContext(ctx) - // Only Freedom's target resolution is skipped; proxy server resolution still follows sockopt. - skipDNSResolve := outboundName == "freedom" && content != nil && content.SkipDNSResolve - if sockopt.DomainStrategy.HasStrategy() && dest.Address.Family().IsDomain() && !skipDNSResolve { + if sockopt.DomainStrategy.HasStrategy() && dest.Address.Family().IsDomain() && !(outboundName == "freedom" && session.SkipDNSResolveFromContext(ctx)) { finalStrategy := sockopt.DomainStrategy if outboundName == "freedom" && dest.Network == net.Network_UDP && origTargetAddr != nil && src == nil { finalStrategy = finalStrategy.GetDynamicStrategy(origTargetAddr.Family())