Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set outbound tag of the selector for dns route #2264

Open
wants to merge 2 commits into
base: main-next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions outbound/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ func (s *Selector) NewPacketConnection(ctx context.Context, conn N.PacketConn, m
}

func RealTag(detour adapter.Outbound) string {
if s, ok := detour.(*Selector); ok {
return RealTag(s.selected)
}
if group, isGroup := detour.(adapter.OutboundGroup); isGroup {
return group.Now()
}
Expand Down
13 changes: 9 additions & 4 deletions route/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ import (
"github.com/sagernet/sing-box/common/process"
"github.com/sagernet/sing-box/common/sniff"
"github.com/sagernet/sing-box/common/taskmonitor"
"github.com/sagernet/sing-box/constant"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/experimental/libbox/platform"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-box/outbound"
"github.com/sagernet/sing-box/transport/fakeip"
"github.com/sagernet/sing-dns"
"github.com/sagernet/sing-mux"
"github.com/sagernet/sing-tun"
"github.com/sagernet/sing-vmess"
dns "github.com/sagernet/sing-dns"
mux "github.com/sagernet/sing-mux"
tun "github.com/sagernet/sing-tun"
vmess "github.com/sagernet/sing-vmess"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio"
Expand Down Expand Up @@ -1113,6 +1114,10 @@ func (r *Router) match(ctx context.Context, metadata *adapter.InboundContext, de
}
}
ctx = outbound.ContextWithTag(ctx, matchOutbound.Tag())
if matchOutbound.Type() == constant.TypeSelector {
tag := outbound.RealTag(matchOutbound)
metadata.Outbound = tag
}
return ctx, matchRule, matchOutbound, nil
}

Expand Down
24 changes: 23 additions & 1 deletion route/router_dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"time"

"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-dns"
"github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/outbound"
dns "github.com/sagernet/sing-dns"
"github.com/sagernet/sing/common/cache"
E "github.com/sagernet/sing/common/exceptions"
F "github.com/sagernet/sing/common/format"
Expand Down Expand Up @@ -127,6 +129,26 @@ func (r *Router) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.Msg, er
dnsCtx context.Context
addressLimit bool
)
mOutbound := r.defaultOutboundForConnection
for i, rule := range r.rules {
metadata.ResetRuleCache()
if rule.Match(metadata) {
detour := rule.Outbound()
r.logger.DebugContext(ctx, "match[", i, "] ", rule.String(), " => ", detour)
if matchOutbound, loaded := r.Outbound(detour); loaded {
if matchOutbound.Type() != constant.TypeDNS {
mOutbound = matchOutbound
break
} else {
r.logger.DebugContext(ctx, "skip the dns outbound ", detour, matchOutbound.Type(), matchOutbound.Tag(), rule.String())
continue
}
}
r.logger.ErrorContext(ctx, "outbound not found: ", detour)
}
}
tag := outbound.RealTag(mOutbound)
metadata.Outbound = tag
dnsCtx, transport, strategy, rule, ruleIndex = r.matchDNS(ctx, true, ruleIndex, isAddressQuery(message))
dnsCtx = adapter.OverrideContext(dnsCtx)
if rule != nil && rule.WithAddressLimit() {
Expand Down