diff --git a/dns/dns_proxy.go b/dns/dns_proxy.go index 7a69f53..9451ba8 100644 --- a/dns/dns_proxy.go +++ b/dns/dns_proxy.go @@ -745,6 +745,7 @@ func (p *DNSProxy) SetJITHandler(handler func(siteId int)) { // domain should be a domain name (e.g., "example.com" or "example.com.") // ip should be a valid IPv4 or IPv6 address func (p *DNSProxy) AddDNSRecord(domain string, ip net.IP, siteId int) error { + logger.Debug("Adding dns record for domain %s with IP %s (siteId=%d)", domain, ip.String(), siteId) return p.recordStore.AddRecord(domain, ip, siteId) } diff --git a/dns/dns_records.go b/dns/dns_records.go index c52c08e..270bae6 100644 --- a/dns/dns_records.go +++ b/dns/dns_records.go @@ -75,8 +75,18 @@ func (s *DNSRecordStore) AddRecord(domain string, ip net.IP, siteId int) error { } rs := m[domain] if isV4 { + for _, existing := range rs.A { + if existing.Equal(ip) { + return nil + } + } rs.A = append(rs.A, ip) } else { + for _, existing := range rs.AAAA { + if existing.Equal(ip) { + return nil + } + } rs.AAAA = append(rs.AAAA, ip) } @@ -87,6 +97,7 @@ func (s *DNSRecordStore) AddRecord(domain string, ip net.IP, siteId int) error { return nil } + // AddPTRRecord adds a PTR record mapping an IP address to a domain name // ip should be a valid IPv4 or IPv6 address // domain should be in FQDN format (e.g., "example.com.") diff --git a/olm/connect.go b/olm/connect.go index f5a0ccd..3a2000c 100644 --- a/olm/connect.go +++ b/olm/connect.go @@ -175,21 +175,19 @@ func (o *Olm) handleConnect(msg websocket.WSMessage) { for i := range wgData.Sites { site := wgData.Sites[i] - if site.PublicKey == "" { - logger.Warn("Skipping site %d (%s): no public key available (site may not be connected)", site.SiteId, site.Name) - continue + if site.PublicKey != "" { + var siteEndpoint string + // here we are going to take the relay endpoint if it exists which means we requested a relay for this peer + if site.RelayEndpoint != "" { + siteEndpoint = site.RelayEndpoint + } else { + siteEndpoint = site.Endpoint + } + + o.apiServer.AddPeerStatus(site.SiteId, site.Name, false, 0, siteEndpoint, false) } - var siteEndpoint string - // here we are going to take the relay endpoint if it exists which means we requested a relay for this peer - if site.RelayEndpoint != "" { - siteEndpoint = site.RelayEndpoint - } else { - siteEndpoint = site.Endpoint - } - - o.apiServer.AddPeerStatus(site.SiteId, site.Name, false, 0, siteEndpoint, false) - + // we still call this to add the aliases for jit lookup but we just do that then pass inside. need to skip the above so we dont add to the api if err := o.peerManager.AddPeer(site); err != nil { logger.Error("Failed to add peer: %v", err) return @@ -311,12 +309,12 @@ func (o *Olm) handleTerminate(msg websocket.WSMessage) { logger.Error("Error unmarshaling terminate error data: %v", err) } else { logger.Info("Terminate reason (code: %s): %s", errorData.Code, errorData.Message) - + if errorData.Code == "TERMINATED_INACTIVITY" { logger.Info("Ignoring...") return } - + // Set the olm error in the API server so it can be exposed via status o.apiServer.SetOlmError(errorData.Code, errorData.Message) } diff --git a/peers/manager.go b/peers/manager.go index c5bb291..9cc1e75 100644 --- a/peers/manager.go +++ b/peers/manager.go @@ -110,6 +110,19 @@ func (pm *PeerManager) GetAllPeers() []SiteConfig { func (pm *PeerManager) AddPeer(siteConfig SiteConfig) error { pm.mu.Lock() defer pm.mu.Unlock() + + for _, alias := range siteConfig.Aliases { + address := net.ParseIP(alias.AliasAddress) + if address == nil { + continue + } + pm.dnsProxy.AddDNSRecord(alias.Alias, address, siteConfig.SiteId) + } + + if siteConfig.PublicKey == "" { + logger.Debug("Skip adding site %d because no pub key", siteConfig.SiteId) + return nil + } // build the allowed IPs list from the remote subnets and aliases and add them to the peer allowedIPs := make([]string, 0, len(siteConfig.RemoteSubnets)+len(siteConfig.Aliases)) @@ -143,14 +156,7 @@ func (pm *PeerManager) AddPeer(siteConfig SiteConfig) error { if err := network.AddRoutes(siteConfig.RemoteSubnets, pm.interfaceName); err != nil { logger.Error("Failed to add routes for remote subnets: %v", err) } - for _, alias := range siteConfig.Aliases { - address := net.ParseIP(alias.AliasAddress) - if address == nil { - continue - } - pm.dnsProxy.AddDNSRecord(alias.Alias, address, siteConfig.SiteId) - } - + monitorAddress := strings.Split(siteConfig.ServerIP, "/")[0] monitorPeer := net.JoinHostPort(monitorAddress, strconv.Itoa(int(siteConfig.ServerPort+1))) // +1 for the monitor port