Skip to content

Commit c74a168

Browse files
committed
boostrap: m refactor
1 parent 829307e commit c74a168

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

intra/bootstrap.go

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/celzero/firestack/intra/ipn"
1919
"github.com/celzero/firestack/intra/log"
2020
"github.com/celzero/firestack/intra/protect"
21+
"github.com/celzero/firestack/intra/xdns"
2122
)
2223

2324
const (
@@ -43,8 +44,8 @@ type bootstrap struct {
4344
proxies ipn.Proxies // never nil if underlying transport is set
4445
bridge Bridge // never nil if underlying transport is set
4546
typ string // DOH or DNS53
46-
ipports string // never empty
47-
url string // never empty
47+
ipports string // never empty for DNS53
48+
url string // never empty for DOH
4849
hostname string // never empty
4950
}
5051

@@ -76,35 +77,40 @@ func newDefaultTransport(ipcsv string, p ipn.Proxies, g Bridge) (dnsx.Transport,
7677
return nil, errCannotStart
7778
}
7879

79-
func (b *bootstrap) reinit(trtype, u, ipcsv string) error {
80-
if len(ipcsv) <= 0 {
81-
log.E("dns: default: reinit: empty url %s / ips %s", u, ipcsv)
82-
return dnsx.ErrNotDefaultTransport
83-
}
84-
if trtype != dnsx.DOH && trtype != dnsx.DNS53 {
85-
log.E("dns: default: reinit: unknown type %s", trtype)
86-
return dnsx.ErrNotDefaultTransport
87-
}
88-
if len(u) <= 0 {
89-
u = protect.UidSelf
90-
}
91-
b.url = u // may be localhost or protect.UidSelf; see: ipmap.LookupNetIP
92-
b.typ = trtype
93-
b.ipports = ipcsv
94-
ips := strings.Split(ipcsv, ",")
95-
if len(ips) <= 0 {
96-
log.E("dns: default: reinit: zero valid ipports in %s (url? %s)", ipcsv, b.url)
80+
func (b *bootstrap) reinit(trtype, ippOrUrl, ipcsv string) error {
81+
if len(ippOrUrl) <= 0 {
82+
log.E("dns: default: reinit: empty url %s! ips? %s", ippOrUrl, ipcsv)
9783
return dnsx.ErrNotDefaultTransport
9884
}
9985

100-
b.hostname = b.url // may be a special name like protect.UidSelf
101-
if parsed, err := url.Parse(b.url); err == nil {
86+
if parsed, err := url.Parse(ippOrUrl); err == nil { // ippOrUrl is a url?
87+
if trtype != dnsx.DOH {
88+
log.E("dns: default: reinit: url %s for %s", ippOrUrl, trtype)
89+
return dnsx.ErrNotDefaultTransport
90+
}
91+
b.url = ippOrUrl
10292
b.hostname = parsed.Hostname()
93+
b.ipports = ipcsv // may be empty
94+
b.typ = dnsx.DOH
95+
} else { // ippOrUrl is an ipport?
96+
if trtype != dnsx.DNS53 {
97+
log.E("dns: default: reinit: ipport %s for %s", ippOrUrl, trtype)
98+
return dnsx.ErrNotDefaultTransport
99+
}
100+
if ipport, err := xdns.DnsIPPort(ippOrUrl); err == nil {
101+
b.url = ""
102+
b.hostname = protect.UidSelf
103+
b.ipports = ipport.String()
104+
b.typ = dnsx.DNS53
105+
} else {
106+
return err
107+
}
103108
}
109+
104110
// hydrate ipmap with the new ips against incoming hostname
105-
ok := dialers.Renew(b.hostname, ips)
111+
ok := dialers.Renew(b.hostname, strings.Split(ipcsv, ","))
106112

107-
log.I("dns: default: %s reinit %s %s w/ %s; resolved? %t", trtype, b.url, b.hostname, ips, ok)
113+
log.I("dns: default: %s reinit %s %s w/ %s; resolved? %t", trtype, b.url, b.hostname, ipcsv, ok)
108114

109115
// if proxies and bridges are set, restart to create new transport
110116
if b.proxies != nil && b.bridge != nil {

0 commit comments

Comments
 (0)