mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-08-10 13:20:33 +02:00
internal/clientinfo: use default route IP as self client info
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user