Skip to content

Commit 1b40e7c

Browse files
committed
dnsx: In Loopback, let Default be blocked & proxied
1 parent 5a0c0c9 commit 1b40e7c

3 files changed

Lines changed: 44 additions & 16 deletions

File tree

intra/dns53/upstream.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ func NewTransportFrom(ctx context.Context, id string, ipp netip.AddrPort, px ipn
144144

145145
func (t *transport) pxdial(network, pid string) (*dns.Conn, string, uintptr, error) {
146146
if t.id == dnsx.Bootstrap || t.id == dnsx.System { // bootstrap/default never be proxied
147+
// never proxy dns53 transport with "bootstrap" id is a clone of dnsx.System
147148
pid = dnsx.NetBaseProxy
148149
} else if len(t.relay) > 0 { // relay takes precedence
149150
pid = t.relay

intra/dnsx/transport.go

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -484,12 +484,7 @@ func (r *resolver) LocalLookup(q []byte) ([]byte, string, error) {
484484
}
485485

486486
loopingBack := settings.Loopingback.Load()
487-
defaultIsSystemDNS := false
488-
if dtr, _ := r.Get(x.StrOf(Default)); dtr != nil {
489-
// todo: a better way to determine whether Default is SystemDNS
490-
// Default is usually SystemDNS if it is of type DNS53
491-
defaultIsSystemDNS = dtr.Type().V() == DNS53
492-
}
487+
defaultIsSystemDNS := r.isDefaultSystemDNS()
493488

494489
// including dns64 and/or alg
495490
ans, tid, err := r.forward(q, protect.UidSelf, Default)
@@ -1352,10 +1347,10 @@ func CanUseProxy(id string) bool {
13521347
switch id {
13531348
case Goos, CT + Goos, Local, CT + Local, System, CT + System:
13541349
return false
1355-
case Bootstrap, CT + Bootstrap, Default, CT + Default:
1356-
return false
13571350
case Preset, CT + Preset:
13581351
return false
1352+
case Default, CT + Default, Bootstrap, CT + Bootstrap:
1353+
return canProxyDefault()
13591354
}
13601355
return true
13611356
}
@@ -1371,10 +1366,12 @@ func overrideProxyIfNeeded(pid string, ids ...string) string {
13711366
return NetExitProxy
13721367
case CT + Goos, CT + Local: // exit
13731368
return NetExitProxy
1374-
case Bootstrap, Default, System, Preset: // base
1369+
case System, Preset: // base
13751370
return NetBaseProxy
1376-
case CT + Bootstrap, CT + Default, CT + System, CT + Preset: // base
1371+
case CT + System, CT + Preset: // base
13771372
return NetBaseProxy
1373+
case Default, CT + Default, Bootstrap, CT + Bootstrap: // may be proxy
1374+
return proxyForDefault(pid)
13781375
}
13791376
}
13801377
return pid // as-is
@@ -1386,15 +1383,44 @@ func skipBlock(tr ...Transport) bool {
13861383
continue
13871384
}
13881385
switch idstr(t) { // Plus/CT+Plus to skip blocks conditionally?
1389-
case Default, BlockFree, Alg, Bootstrap:
1386+
case BlockFree, Alg:
13901387
return true
1391-
case CT + Default, CT + BlockFree, CT + Alg, CT + Bootstrap:
1388+
case CT + BlockFree, CT + Alg:
13921389
return true
1390+
case Default, CT + Default, Bootstrap, CT + Bootstrap:
1391+
return canBlockDefault()
13931392
}
13941393
}
13951394
return false
13961395
}
13971396

1397+
func (r *resolver) isDefaultSystemDNS() (y bool) {
1398+
if dtr, _ := r.GetInternal(Default); dtr != nil {
1399+
// todo: a better way to determine whether Default is SystemDNS
1400+
// Default is usually SystemDNS if it is of type DNS53
1401+
y = dtr.Type().V() == DNS53
1402+
}
1403+
return
1404+
}
1405+
1406+
func canBlockDefault() bool {
1407+
// TODO: check for gateway.split?
1408+
return settings.Loopingback.Load()
1409+
}
1410+
1411+
func canProxyDefault() bool {
1412+
// TODO: check for gateway.split?
1413+
// TODO: do not allow proxying when Default is mapped to Goos/System
1414+
return settings.Loopingback.Load()
1415+
}
1416+
1417+
func proxyForDefault(pid string) string {
1418+
if canProxyDefault() {
1419+
return pid
1420+
}
1421+
return NetBaseProxy
1422+
}
1423+
13981424
func skipInternalCache(tids ...string) bool {
13991425
return isAnyBlockAll(tids...)
14001426
}

intra/doh/doh.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func newTransport(ctx context.Context, typ, id, rawurl, otargeturl string, addrs
130130

131131
var renewed bool
132132
var relay string
133-
if id != dnsx.Bootstrap && px != nil {
133+
if px != nil {
134134
if p, _ := px.ProxyFor(id); p != nil {
135135
relay = p.ID().V()
136136
}
@@ -781,7 +781,8 @@ func (t *transport) Query(network string, q *dns.Msg, smm *x.DNSSummary) (r *dns
781781
var elapsed time.Duration
782782
var qerr *dnsx.QueryError
783783

784-
if t.id == dnsx.Bootstrap { // bootstrap/default never be proxied
784+
canproxy := dnsx.CanUseProxy(t.id)
785+
if !canproxy { // bootstrap/default may not be proxied
785786
pid = dnsx.NetBaseProxy
786787
} else if r := t.relay; len(r) > 0 {
787788
pid = t.chooseProxy(r)
@@ -829,8 +830,8 @@ func (t *transport) Query(network string, q *dns.Msg, smm *x.DNSSummary) (r *dns
829830
smm.Msg = err.Error()
830831
}
831832
if settings.Debug {
832-
log.V("doh: (p/px/via %s/%s/%s); a:%d/sz:%d/pad:%d, q: %s:%d, data: %s, code: %d, via: %s, err? %v",
833-
network, pid, rpid, xdns.Len(r), xdns.Size(r), xdns.EDNS0PadLen(r), smm.QName, smm.QType, smm.RData, smm.RCode, smm.PID, err)
833+
log.V("doh: (p/px/via/can? %s/%s/%s/%t); a:%d/sz:%d/pad:%d, q: %s:%d, data: %s, code: %d, via: %s, err? %v",
834+
network, pid, rpid, canproxy, xdns.Len(r), xdns.Size(r), xdns.EDNS0PadLen(r), smm.QName, smm.QType, smm.RData, smm.RCode, smm.PID, err)
834835
}
835836
return r, err
836837
}

0 commit comments

Comments
 (0)