Skip to content

Commit

Permalink
dnsx/transport: Local overrides other transports if present
Browse files Browse the repository at this point in the history
  • Loading branch information
ignoramous committed Sep 29, 2023
1 parent 172dbbc commit b7753d6
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions intra/dnsx/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ func (r *resolver) Forward(q []byte) ([]byte, error) {
return nil, errNoSuchTransport
}
var t2 Transport
if r.useSecondary(id, sid) {
if len(sid) > 0 {
t2 = r.determineTransport(sid)
}

Expand All @@ -402,10 +402,7 @@ func (r *resolver) Forward(q []byte) ([]byte, error) {
summary.ID = t.ID()
var res2 []byte

netid := NetTypeUDP
if r.allowProxy(t, t2) {
netid = xdns.NetAndProxyID(netid, pid)
}
netid := xdns.NetAndProxyID(NetTypeUDP, pid)

// with t2 as the secondary transport, which could be nil
res2, err = gw.q(t, t2, netid, q, summary)
Expand Down Expand Up @@ -454,10 +451,6 @@ func (r *resolver) Serve(c Conn) {
r.accept(c)
}

func (r *resolver) useSecondary(id, _ string) bool {
return id != Local
}

func (r *resolver) determineTransport(id string) Transport {
r.RLock()
defer r.RUnlock()
Expand Down Expand Up @@ -533,7 +526,7 @@ func (r *resolver) forwardQuery(q []byte, c io.Writer) error {
return errNoSuchTransport
}
var t2 Transport = nil
if r.useSecondary(id, sid) {
if len(sid) > 0 {
t2 = r.determineTransport(sid)
}

Expand All @@ -557,10 +550,7 @@ func (r *resolver) forwardQuery(q []byte, c io.Writer) error {
summary.Type = t.Type()
summary.ID = t.ID()
var res2 []byte
netid := NetTypeTCP
if r.allowProxy(t, t2) {
netid = xdns.NetAndProxyID(netid, pid)
}
netid := xdns.NetAndProxyID(NetTypeTCP, pid)

// with t2 as secondary transport, which may be nil
res2, err = gw.q(t, t2, netid, q, summary)
Expand Down Expand Up @@ -669,14 +659,20 @@ func isReserved(id string) (ok bool) {
return id == Alg || id == DcProxy || id == BlockAll || id == Preferred || id == BlockFree || id == System
}

func (r *resolver) allowProxy(ts ...Transport) bool {
func overrideTransports(ids ...string) string {
for _, t := range ids {
if t == Local {
return Local
}
}
return ""
}

func allowProxy(ids ...string) bool {
allow := true
deny := false
for _, t := range ts {
if t == nil {
continue
}
if t.ID() == Default || t.ID() == System || t.ID() == Local {
for _, t := range ids {
if t == Default || t == System || t == Local {
return deny
}
}
Expand Down Expand Up @@ -779,7 +775,12 @@ func preferencesFrom(s *NsOpts) (id1, id2, pid, ips string) {
log.W("dns: pref: too many tids; upto 2, got %d", l)
id1, id2 = x[0], x[1] // ids for transport t1, t2
}
if len(s.PID) > 0 {
if sup := overrideTransports(id1, id2); len(sup) > 0 && l >= 2 {
log.D("dns: pref: %s overrides transports %s, %s", sup, id1, id2)
id1 = sup
id2 = ""
}
if len(s.PID) > 0 && allowProxy(id1, id2) {
pid = s.PID // id for proxy
} else {
pid = NetNoProxy
Expand Down

1 comment on commit b7753d6

@ignoramous
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.