Skip to content

Commit

Permalink
Don't overwrite old DNS results with empty list
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmaguire committed Oct 25, 2024
1 parent 3e6c755 commit a0327af
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lighthouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ func (lh *LightHouse) addStaticRemotes(i int, d time.Duration, network string, t
ctx := lh.ctx
lh.Unlock()

hr, err := NewHostnameResults(ctx, lh.l, d, network, timeout, toAddrs, func() {
hr, err := NewHostnamesResults(ctx, lh.l, d, vpnIp, network, timeout, toAddrs, func() {
// This callback runs whenever the DNS hostname resolver finds a different set of IP's
// in its resolution for hostnames.
am.Lock()
Expand Down
41 changes: 24 additions & 17 deletions remote_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ type hostnamesResults struct {
network string
lookupTimeout time.Duration
cancelFn func()
l *logrus.Logger
l logrus.FieldLogger
ips atomic.Pointer[map[netip.AddrPort]struct{}]
}

func NewHostnameResults(ctx context.Context, l *logrus.Logger, d time.Duration, network string, timeout time.Duration, hostPorts []string, onUpdate func()) (*hostnamesResults, error) {
func NewHostnamesResults(ctx context.Context, l logrus.FieldLogger, d time.Duration, vpnIp netip.Addr, network string, timeout time.Duration, hostPorts []string, onUpdate func()) (*hostnamesResults, error) {
l = l.WithField("vpnIp", vpnIp)
r := &hostnamesResults{
hostnames: make([]hostnamePort, len(hostPorts)),
network: network,
Expand Down Expand Up @@ -130,27 +131,33 @@ func NewHostnameResults(ctx context.Context, l *logrus.Logger, d time.Duration,
netipAddrs[netip.AddrPortFrom(a.Unmap(), hostPort.port)] = struct{}{}
}
}

origSet := r.ips.Load()
different := false
for a := range *origSet {
if _, ok := netipAddrs[a]; !ok {
different = true
break
}
}
if !different {
for a := range netipAddrs {
if _, ok := (*origSet)[a]; !ok {
if len(netipAddrs) == 0 && len(*origSet) != 0 {
l.WithFields(logrus.Fields{"hostnames": r.hostnames}).Info("No IPs resolved for hostnames, refusing to overwrite existing IPs")
} else {
different := false
for a := range *origSet {
if _, ok := netipAddrs[a]; !ok {
different = true
break
}
}
if !different {
for a := range netipAddrs {
if _, ok := (*origSet)[a]; !ok {
different = true
break
}
}
}
if different {
l.WithFields(logrus.Fields{"origSet": origSet, "newSet": netipAddrs}).Info("DNS results changed for host list")
r.ips.Store(&netipAddrs)
onUpdate()
}
}
if different {
l.WithFields(logrus.Fields{"origSet": origSet, "newSet": netipAddrs}).Info("DNS results changed for host list")
r.ips.Store(&netipAddrs)
onUpdate()
}

select {
case <-newCtx.Done():
return
Expand Down

0 comments on commit a0327af

Please sign in to comment.