mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-08-10 13:20:33 +02:00
internal/clientinfo: only do self discover with client id
While at it, also ensure that client info table was initialized before doing any lookup.
This commit is contained in:
+1
-1
@@ -112,7 +112,7 @@ func (p *prog) run() {
|
|||||||
go uc.Ping()
|
go uc.Ping()
|
||||||
}
|
}
|
||||||
|
|
||||||
p.ciTable = clientinfo.NewTable(&cfg, defaultRouteIP())
|
p.ciTable = clientinfo.NewTable(&cfg, defaultRouteIP(), cdUID)
|
||||||
if leaseFile := p.cfg.Service.DHCPLeaseFile; leaseFile != "" {
|
if leaseFile := p.cfg.Service.DHCPLeaseFile; leaseFile != "" {
|
||||||
mainLog.Load().Debug().Msgf("watching custom lease file: %s", leaseFile)
|
mainLog.Load().Debug().Msgf("watching custom lease file: %s", leaseFile)
|
||||||
format := ctrld.LeaseFileFormat(p.cfg.Service.DHCPLeaseFileFormat)
|
format := ctrld.LeaseFileFormat(p.cfg.Service.DHCPLeaseFileFormat)
|
||||||
|
|||||||
@@ -4,9 +4,11 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Control-D-Inc/ctrld"
|
"github.com/Control-D-Inc/ctrld"
|
||||||
|
"github.com/Control-D-Inc/ctrld/internal/controld"
|
||||||
)
|
)
|
||||||
|
|
||||||
// IpResolver is the interface for retrieving IP from Mac.
|
// IpResolver is the interface for retrieving IP from Mac.
|
||||||
@@ -60,6 +62,7 @@ type Table struct {
|
|||||||
macResolvers []MacResolver
|
macResolvers []MacResolver
|
||||||
hostnameResolvers []HostnameResolver
|
hostnameResolvers []HostnameResolver
|
||||||
refreshers []refresher
|
refreshers []refresher
|
||||||
|
initOnce sync.Once
|
||||||
|
|
||||||
dhcp *dhcp
|
dhcp *dhcp
|
||||||
merlin *merlinDiscover
|
merlin *merlinDiscover
|
||||||
@@ -69,13 +72,15 @@ type Table struct {
|
|||||||
cfg *ctrld.Config
|
cfg *ctrld.Config
|
||||||
quitCh chan struct{}
|
quitCh chan struct{}
|
||||||
selfIP string
|
selfIP string
|
||||||
|
cdUID string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTable(cfg *ctrld.Config, selfIP string) *Table {
|
func NewTable(cfg *ctrld.Config, selfIP, cdUID string) *Table {
|
||||||
return &Table{
|
return &Table{
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
quitCh: make(chan struct{}),
|
quitCh: make(chan struct{}),
|
||||||
selfIP: selfIP,
|
selfIP: selfIP,
|
||||||
|
cdUID: cdUID,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,6 +93,7 @@ func (t *Table) AddLeaseFile(name string, format ctrld.LeaseFileFormat) {
|
|||||||
|
|
||||||
func (t *Table) RefreshLoop(stopCh chan struct{}) {
|
func (t *Table) RefreshLoop(stopCh chan struct{}) {
|
||||||
timer := time.NewTicker(time.Minute * 5)
|
timer := time.NewTicker(time.Minute * 5)
|
||||||
|
defer timer.Stop()
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-timer.C:
|
case <-timer.C:
|
||||||
@@ -102,6 +108,19 @@ func (t *Table) RefreshLoop(stopCh chan struct{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *Table) Init() {
|
func (t *Table) Init() {
|
||||||
|
t.initOnce.Do(t.init)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Table) init() {
|
||||||
|
if _, clientID := controld.ParseRawUID(t.cdUID); clientID != "" {
|
||||||
|
ctrld.ProxyLogger.Load().Debug().Msg("start self discovery")
|
||||||
|
t.dhcp = &dhcp{selfIP: t.selfIP}
|
||||||
|
t.dhcp.addSelf()
|
||||||
|
t.ipResolvers = append(t.ipResolvers, t.dhcp)
|
||||||
|
t.macResolvers = append(t.macResolvers, t.dhcp)
|
||||||
|
t.hostnameResolvers = append(t.hostnameResolvers, t.dhcp)
|
||||||
|
return
|
||||||
|
}
|
||||||
if t.discoverDHCP() || t.discoverARP() {
|
if t.discoverDHCP() || t.discoverARP() {
|
||||||
t.merlin = &merlinDiscover{}
|
t.merlin = &merlinDiscover{}
|
||||||
if err := t.merlin.refresh(); err != nil {
|
if err := t.merlin.refresh(); err != nil {
|
||||||
@@ -156,6 +175,7 @@ func (t *Table) Init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *Table) LookupIP(mac string) string {
|
func (t *Table) LookupIP(mac string) string {
|
||||||
|
t.initOnce.Do(t.init)
|
||||||
for _, r := range t.ipResolvers {
|
for _, r := range t.ipResolvers {
|
||||||
if ip := r.LookupIP(mac); ip != "" {
|
if ip := r.LookupIP(mac); ip != "" {
|
||||||
return ip
|
return ip
|
||||||
@@ -165,6 +185,7 @@ func (t *Table) LookupIP(mac string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *Table) LookupMac(ip string) string {
|
func (t *Table) LookupMac(ip string) string {
|
||||||
|
t.initOnce.Do(t.init)
|
||||||
for _, r := range t.macResolvers {
|
for _, r := range t.macResolvers {
|
||||||
if mac := r.LookupMac(ip); mac != "" {
|
if mac := r.LookupMac(ip); mac != "" {
|
||||||
return mac
|
return mac
|
||||||
@@ -174,6 +195,7 @@ func (t *Table) LookupMac(ip string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *Table) LookupHostname(ip, mac string) string {
|
func (t *Table) LookupHostname(ip, mac string) string {
|
||||||
|
t.initOnce.Do(t.init)
|
||||||
for _, r := range t.hostnameResolvers {
|
for _, r := range t.hostnameResolvers {
|
||||||
if name := r.LookupHostnameByIP(ip); name != "" {
|
if name := r.LookupHostnameByIP(ip); name != "" {
|
||||||
return name
|
return name
|
||||||
|
|||||||
Reference in New Issue
Block a user