internal/clientinfo: use default route IP as self client info

This commit is contained in:
Cuong Manh Le
2023-08-09 23:54:23 +07:00
committed by Cuong Manh Le
parent 59a895bfe2
commit d6768c4c39
4 changed files with 49 additions and 4 deletions
+8 -3
View File
@@ -49,10 +49,15 @@ type Table struct {
mdns *mdns
cfg *ctrld.Config
quitCh chan struct{}
selfIP string
}
func NewTable(cfg *ctrld.Config) *Table {
return &Table{cfg: cfg, quitCh: make(chan struct{})}
func NewTable(cfg *ctrld.Config, selfIP string) *Table {
return &Table{
cfg: cfg,
quitCh: make(chan struct{}),
selfIP: selfIP,
}
}
func (t *Table) AddLeaseFile(name string, format ctrld.LeaseFileFormat) {
@@ -88,7 +93,7 @@ func (t *Table) Init() {
}
}
if t.discoverDHCP() {
t.dhcp = &dhcp{}
t.dhcp = &dhcp{selfIP: t.selfIP}
ctrld.ProxyLog.Debug().Msg("start dhcp discovery")
if err := t.dhcp.refresh(); err != nil {
ctrld.ProxyLog.Error().Err(err).Msg("could not init DHCP discover")
+9
View File
@@ -25,6 +25,7 @@ type dhcp struct {
mac sync.Map // ip => mac
watcher *fsnotify.Watcher
selfIP string
}
func (d *dhcp) refresh() error {
@@ -229,6 +230,7 @@ func (d *dhcp) addSelf() {
hostname = normalizeHostname(hostname)
d.ip2name.Store("127.0.0.1", hostname)
d.ip2name.Store("::1", hostname)
found := false
interfaces.ForeachInterface(func(i interfaces.Interface, prefixes []netip.Prefix) {
mac := i.HardwareAddr.String()
// Skip loopback interfaces, info was stored above.
@@ -237,6 +239,9 @@ func (d *dhcp) addSelf() {
}
addrs, _ := i.Addrs()
for _, addr := range addrs {
if found {
return
}
ipNet, ok := addr.(*net.IPNet)
if !ok {
continue
@@ -251,6 +256,10 @@ func (d *dhcp) addSelf() {
}
d.mac2name.Store(mac, hostname)
d.ip2name.Store(ip.String(), hostname)
// If we have self IP set, and this IP is it, use this IP only.
if ip.String() == d.selfIP {
found = true
}
}
})
}