Skip to content

Commit 98084d7

Browse files
committed
dnsx: impl GetRelay
It returns the sole backing Proxy of a DNS transport, if any.
1 parent 4aa7510 commit 98084d7

12 files changed

Lines changed: 78 additions & 10 deletions

File tree

intra/backend/dnsx.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ type DNSTransport interface {
8989
P50() int64
9090
// Return the server host address used to initialize this transport.
9191
GetAddr() *Gostr
92+
// Return the proxy (relay) always used by this transport.
93+
// Returns nil if there isn't any.
94+
GetRelay() Proxy
9295
// State of the transport after previous query (see: queryerror.go)
9396
Status() int
9497
}
@@ -153,7 +156,8 @@ type ResolverListener interface {
153156
// OnDNSRemoved is called when a DNS transport with id is removed, except
154157
// when the transport is stopped, then OnDNSStopped is called instead.
155158
OnDNSRemoved(id *Gostr)
156-
// OnDNSStopped is called when the DNS transport is stopped. Note:
157-
// OnDNSRemoved is not called for each transport before this.
159+
// OnDNSStopped is called when all DNS transports are stopped. Note:
160+
// OnDNSRemoved is not called for each transport even if they are
161+
// being removed and not just stopped.
158162
OnDNSStopped()
159163
}

intra/bootstrap.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,10 @@ func (b *bootstrap) GetAddr() *x.Gostr {
288288
return x.StrOf(dnsx.NoDNS)
289289
}
290290

291+
func (b *bootstrap) GetRelay() x.Proxy {
292+
return nil
293+
}
294+
291295
func (b *bootstrap) IPPorts() []netip.AddrPort {
292296
if tr := b.tr; tr != nil {
293297
return tr.IPPorts()

intra/dns53/dot.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,14 @@ func (t *dot) GetAddr() *x.Gostr {
367367
return x.StrOf(t.getAddr())
368368
}
369369

370+
func (t *dot) GetRelay() x.Proxy {
371+
if r := t.relay; len(r) > 0 {
372+
px, _ := t.proxies.ProxyFor(r)
373+
return px
374+
}
375+
return nil
376+
}
377+
370378
func (t *dot) getAddr() (addr string) {
371379
if t.c3 != nil {
372380
addr = dnsx.EchPrefix + t.addrport

intra/dns53/errorer.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type errorer struct {
2727

2828
var _ dnsx.Transport = (*errorer)(nil)
2929

30-
// NewGroundedTransport returns a DNS transport that blocks all DNS queries.
30+
// NewErrorerTransport returns a DNS transport that always errors out on queries.
3131
func NewErrorerTransport(id string) *errorer {
3232
t := &errorer{
3333
id: id, // typically, dnsx.Fixed
@@ -65,6 +65,10 @@ func (t *errorer) GetAddr() *x.Gostr {
6565
return x.StrOf(t.ipport)
6666
}
6767

68+
func (t *errorer) GetRelay() x.Proxy {
69+
return nil
70+
}
71+
6872
func (t *errorer) IPPorts() []netip.AddrPort {
6973
return dnsx.NoIPPort
7074
}

intra/dns53/goos.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ func (t *goosr) getAddr() string {
197197
return protect.Localhost + ":53" // dummy
198198
}
199199

200+
func (t *goosr) GetRelay() x.Proxy {
201+
return nil
202+
}
203+
200204
func (t *goosr) IPPorts() []netip.AddrPort {
201205
return []netip.AddrPort{netip.AddrPortFrom(netip.IPv6Loopback(), uint16(53))}
202206
}

intra/dns53/grounded.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ func (t *grounded) GetAddr() *x.Gostr {
7474
return x.StrOf(t.ipport)
7575
}
7676

77+
func (t *grounded) GetRelay() x.Proxy {
78+
return nil
79+
}
80+
7781
func (t *grounded) IPPorts() []netip.AddrPort {
7882
return dnsx.NoIPPort
7983
}

intra/dns53/mdns.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ func (t *dnssd) GetAddr() *x.Gostr {
203203
return x.StrOf(t.ipport)
204204
}
205205

206+
func (t *dnssd) GetRelay() x.Proxy {
207+
return nil
208+
}
209+
206210
func (t *dnssd) IPPorts() []netip.AddrPort {
207211
return []netip.AddrPort{
208212
xdns.MDNSAddr4.AddrPort(),

intra/dns53/upstream.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,14 @@ func (t *transport) getAddr() string {
345345
return addr
346346
}
347347

348+
func (t *transport) GetRelay() x.Proxy {
349+
if r := t.relay; len(r) > 0 {
350+
px, _ := t.proxies.ProxyFor(r)
351+
return px
352+
}
353+
return nil
354+
}
355+
348356
func (t *transport) IPPorts() (ipps []netip.AddrPort) {
349357
for _, ip := range dialers.For(t.addrport) {
350358
ipps = append(ipps, netip.AddrPortFrom(ip, t.port))

intra/dnscrypt/multiserver.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,8 @@ func resolve(network string, data *dns.Msg, si *serverinfo, smm *x.DNSSummary) (
310310
useudp := proto == dnsx.NetTypeUDP
311311
pid := dnsx.NetNoProxy
312312
if si != nil {
313-
if r := si.relay; r != nil {
314-
pid = si.chooseProxy([]string{r.ID().V()})
313+
if r := si.relay; len(r) > 0 {
314+
pid = si.chooseProxy([]string{r})
315315
} else {
316316
pid = si.chooseProxy(pids)
317317
}
@@ -659,6 +659,10 @@ func (p *DcMulti) getAddr() string {
659659
return p.lastAddr
660660
}
661661

662+
func (p *DcMulti) GetRelay() x.Proxy {
663+
return nil
664+
}
665+
662666
func (p *DcMulti) IPPorts() []netip.AddrPort {
663667
// TODO: get ipports from all servers
664668
return dnsx.NoIPPort

intra/dnscrypt/servers.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ type serverinfo struct {
5858
UDPAddr *net.UDPAddr
5959
TCPAddr *net.TCPAddr
6060
proxies ipn.ProxyProvider // proxy-provider, may be nil
61-
relay ipn.Proxy // proxy relay to use, may be nil
61+
relay string // proxy relay to use, may be nil
6262
est core.P2QuantileEstimator
6363

6464
// fields below are mutable
@@ -260,7 +260,7 @@ func fetchDNSCryptServerInfo(proxy *DcMulti, name string, stamp stamps.ServerSta
260260
RelayTCPAddrs: core.NewZeroVolatile[[]*net.TCPAddr](), // populated later; see proxy.refreshRoutes()
261261
RelayUDPAddrs: core.NewZeroVolatile[[]*net.UDPAddr](), // populated later; see proxy.refreshRoutes()
262262
proxies: px,
263-
relay: relay,
263+
relay: relay.ID().V(),
264264
est: core.NewP50Estimator(ctx),
265265
status: core.NewVolatile(dnsx.Start),
266266
}
@@ -393,6 +393,18 @@ func (s *serverinfo) getAddr() string {
393393
return s.HostName
394394
}
395395

396+
func (s *serverinfo) GetRelay() x.Proxy {
397+
return s.getRelay()
398+
}
399+
400+
func (s *serverinfo) getRelay() ipn.Proxy {
401+
if r := s.relay; len(r) > 0 {
402+
px, _ := s.proxies.ProxyFor(r)
403+
return px
404+
}
405+
return nil
406+
}
407+
396408
func (s *serverinfo) IPPorts() []netip.AddrPort {
397409
if relay := s.RelayUDPAddrs.Load(); relay != nil {
398410
return addr2ipp(relay...)
@@ -413,7 +425,7 @@ func (s *serverinfo) Stop() error {
413425
}
414426

415427
func (s *serverinfo) dialudp(pid string, addr *net.UDPAddr) (net.Conn, error) {
416-
userelay := s.relay != nil
428+
userelay := s.GetRelay() != nil
417429
useproxy := len(pid) != 0 // pid == dnsx.NetNoProxy => ipn.Base
418430
if userelay || useproxy {
419431
return s.dialpx(pid, "udp", addr.String())
@@ -422,7 +434,7 @@ func (s *serverinfo) dialudp(pid string, addr *net.UDPAddr) (net.Conn, error) {
422434
}
423435

424436
func (s *serverinfo) dialtcp(pid string, addr *net.TCPAddr) (net.Conn, error) {
425-
userelay := s.relay != nil
437+
userelay := s.GetRelay() != nil
426438
useproxy := len(pid) != 0 // pid == dnsx.NetNoProxy => ipn.Base
427439
if userelay || useproxy {
428440
return s.dialpx(pid, "tcp", addr.String())
@@ -431,7 +443,7 @@ func (s *serverinfo) dialtcp(pid string, addr *net.TCPAddr) (net.Conn, error) {
431443
}
432444

433445
func (s *serverinfo) dialpx(pid, proto string, addr string) (net.Conn, error) {
434-
relay := s.relay
446+
relay := s.getRelay()
435447
if relay != nil {
436448
// addr is always ip:port; hence protect.dialers are not needed
437449
return relay.Dialer().Dial(proto, addr)

0 commit comments

Comments
 (0)