Skip to content

Commit b360eaf

Browse files
committed
ipmap,core: refactor undelegated domains
1 parent 6d6f395 commit b360eaf

3 files changed

Lines changed: 41 additions & 34 deletions

File tree

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,9 @@
44
// License, v. 2.0. If a copy of the MPL was not distributed with this
55
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
66

7-
package dnsx
7+
package core
88

9-
import (
10-
"strings"
11-
12-
x "github.com/celzero/firestack/intra/backend"
13-
"github.com/celzero/firestack/intra/settings"
14-
"github.com/celzero/firestack/intra/xdns"
15-
)
16-
17-
var undelegatedSet = []string{
9+
var UndelegatedDomains = []string{
1810
"0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa",
1911
"0.in-addr.arpa",
2012
"1",
@@ -161,24 +153,3 @@ var undelegatedSet = []string{
161153
".workgroup",
162154
".zghjccbob3n0",
163155
}
164-
165-
func newUndelegatedDomainsTrie() x.RadixTree {
166-
t := x.NewRadixTree()
167-
for _, domain := range undelegatedSet {
168-
t.Add(x.StrOf(domain))
169-
}
170-
return t
171-
}
172-
173-
func (r *resolver) requiresGoosOrLocal(qname string) (id string) {
174-
if strings.HasSuffix(qname, ".local") || xdns.IsMDNSQuery(qname) {
175-
id = Local
176-
} else if !settings.SystemDNSForUndelegatedDomains.Load() {
177-
// todo: remove this once we let users "pin" domains to resolvers
178-
// github.com/celzero/rethink-app/issues/1153
179-
// skip override when preventing DNS capture on port53 is turned off
180-
} else if len(qname) > 0 && r.localdomains.HasAny(x.StrOf(qname)) {
181-
id = Goos // system is primary; see: transport.go:determineTransports()
182-
}
183-
return
184-
}

intra/dnsx/transport.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func NewResolver(pctx context.Context, fakeaddrs string, dtr x.DNSTransport, l x
199199
listener: l,
200200
smms: make(chan *x.DNSSummary, 64),
201201
transports: make(map[string]Transport),
202-
localdomains: newUndelegatedDomainsTrie(),
202+
localdomains: ipmap.UndelegatedDomainsTrie,
203203
}
204204
r.loadaddrs(fakeaddrs)
205205
r.gateway = NewDNSGateway(ctx, r.dnsaddrs, r, pt)
@@ -1050,6 +1050,19 @@ func (r *resolver) preferencesFrom(qname string, qtyp uint16, s *x.DNSOpts, chos
10501050
return
10511051
}
10521052

1053+
func (r *resolver) requiresGoosOrLocal(qname string) (id string) {
1054+
if strings.HasSuffix(qname, ".local") || xdns.IsMDNSQuery(qname) {
1055+
id = Local
1056+
} else if !settings.SystemDNSForUndelegatedDomains.Load() {
1057+
// todo: remove this once we let users "pin" domains to resolvers
1058+
// github.com/celzero/rethink-app/issues/1153
1059+
// skip override when preventing DNS capture on port53 is turned off
1060+
} else if len(qname) > 0 && r.localdomains.HasAny(x.StrOf(qname)) {
1061+
id = Goos // system is primary; see: transport.go:determineTransports()
1062+
}
1063+
return
1064+
}
1065+
10531066
func (r *resolver) chooseOne(ids ...string) string {
10541067
if len(ids) <= 0 {
10551068
return ""

intra/protect/ipmap/ipmap.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/celzero/firestack/intra/core"
3636
"github.com/celzero/firestack/intra/log"
3737
"github.com/celzero/firestack/intra/protect"
38+
"github.com/celzero/firestack/intra/xdns"
3839
)
3940

4041
const maxFailLimit = 8
@@ -66,6 +67,16 @@ func (h IPSetType) String() string {
6667
}
6768
}
6869

70+
var UndelegatedDomainsTrie = newUndelegatedDomainTrie()
71+
72+
func newUndelegatedDomainTrie() x.RadixTree {
73+
t := x.NewRadixTree()
74+
for _, domain := range core.UndelegatedDomains {
75+
t.Add(x.StrOf(domain))
76+
}
77+
return t
78+
}
79+
6980
// IPMapper is an interface for resolving hostnames to IP addresses.
7081
// For internal used by firestack.
7182
type IPMapper interface {
@@ -263,12 +274,24 @@ func (m *ipmap) ReverseGetMany(n uint8) []string {
263274
m.RLock()
264275
defer m.RUnlock()
265276

277+
possiblyPublicHost := func(host string) bool {
278+
if xdns.IsMDNSQuery(host) {
279+
return false
280+
}
281+
if UndelegatedDomainsTrie.HasAny(x.StrOf(host)) {
282+
return false
283+
}
284+
if _, err := netip.ParseAddr(host); err == nil {
285+
return false // not a host, but an IP address
286+
}
287+
return strings.Contains(host, ".")
288+
}
266289
// TODO: use hosts with public prefixes
267290
for host := range m.m {
268291
if len(hosts) >= int(n) {
269292
break
270293
}
271-
if _, err := netip.ParseAddr(host); err != nil {
294+
if possiblyPublicHost(host) {
272295
// append if not an IP address
273296
hosts = append(hosts, host)
274297
}
@@ -277,7 +300,7 @@ func (m *ipmap) ReverseGetMany(n uint8) []string {
277300
if len(hosts) >= int(n) {
278301
break
279302
}
280-
if _, err := netip.ParseAddr(host); err != nil {
303+
if possiblyPublicHost(host) {
281304
// append if not an IP address
282305
hosts = append(hosts, host)
283306
}

0 commit comments

Comments
 (0)