@@ -21,6 +21,7 @@ type DNSServer struct {
2121type ConfigManagerInterface interface {
2222 GetDomain (domain string ) * config.Domain
2323 GetAgentIP () string
24+ GetAgents () []config.FallbackAgentInfo
2425}
2526
2627type DNSStats struct {
@@ -178,7 +179,9 @@ func (s *DNSServer) handleGeoDNSQuery(w dns.ResponseWriter, r *dns.Msg, domainCo
178179 log .Printf ("[DNS] GeoDNS Fallback Map: %+v" , domainConfig .GeoDnsFallbackMap )
179180 }
180181
181- agentIP := findBestAgentIP (domainConfig .GeoDNSMap , domainConfig .GeoDnsFallbackMap , clientLocation , domainConfig .HTTPProxy .Enabled )
182+ // Get agents list for coordinate-based fallback
183+ allAgents := s .configMgr .GetAgents ()
184+ agentIP := findBestAgentIP (domainConfig .GeoDNSMap , domainConfig .GeoDnsFallbackMap , clientLocation , domainConfig .HTTPProxy .Enabled , allAgents )
182185 if agentIP == "" {
183186 log .Printf ("[DNS] No agent IP found for location: %s (HTTP Proxy: %v)" , clientLocation , domainConfig .HTTPProxy .Enabled )
184187 s .sendNXDOMAIN (w , r )
@@ -280,7 +283,9 @@ func (s *DNSServer) handleRegularDNSQuery(w dns.ResponseWriter, r *dns.Msg, doma
280283 clientLocation = detectedLocation
281284 }
282285 }
283- agentIP = findBestAgentIP (domainConfig .GeoDNSMap , domainConfig .GeoDnsFallbackMap , clientLocation , cnameRecord .HTTPProxyEnabled )
286+ // Get agents list for coordinate-based fallback
287+ allAgents := s .configMgr .GetAgents ()
288+ agentIP = findBestAgentIP (domainConfig .GeoDNSMap , domainConfig .GeoDnsFallbackMap , clientLocation , cnameRecord .HTTPProxyEnabled , allAgents )
284289 log .Printf ("[DNS] CNAME Flattening: Using GeoDNS agent selection: %s (location: %s) → %s" , queryName , clientLocation , agentIP )
285290 }
286291
@@ -444,7 +449,9 @@ func (s *DNSServer) handleRegularDNSQuery(w dns.ResponseWriter, r *dns.Msg, doma
444449 clientLocation = detectedLocation
445450 }
446451 }
447- agentIP = findBestAgentIP (domainConfig .GeoDNSMap , domainConfig .GeoDnsFallbackMap , clientLocation , record .HTTPProxyEnabled )
452+ // Get agents list for coordinate-based fallback
453+ allAgents := s .configMgr .GetAgents ()
454+ agentIP = findBestAgentIP (domainConfig .GeoDNSMap , domainConfig .GeoDnsFallbackMap , clientLocation , record .HTTPProxyEnabled , allAgents )
448455 log .Printf ("[DNS] Using GeoDNS agent selection for proxied record: %s (location: %s) → %s" , queryName , clientLocation , agentIP )
449456 }
450457
@@ -598,7 +605,7 @@ func extractClientIP(addr net.Addr) string {
598605 }
599606}
600607
601- func findBestAgentIP (geoDNSMap map [string ]string , fallbackMap map [string ]string , clientLocation string , httpProxyEnabled bool ) string {
608+ func findBestAgentIP (geoDNSMap map [string ]string , fallbackMap map [string ]string , clientLocation string , httpProxyEnabled bool , allAgents []config. FallbackAgentInfo ) string {
602609 // Try exact match first
603610 if ip , ok := geoDNSMap [clientLocation ]; ok {
604611 return ip
@@ -612,44 +619,45 @@ func findBestAgentIP(geoDNSMap map[string]string, fallbackMap map[string]string,
612619 }
613620 }
614621
615- // If no exact match and no Core fallback, use coordinate-based fallback
616- // Find nearest agent by geographic distance (with political restrictions)
617- clientCoords , ok := LOCATION_COORDINATES [clientLocation ]
618- if ok && len ( geoDNSMap ) > 0 {
619- var nearestAgent string
620- var minDistance float64 = - 1
622+ // If no exact match and no Core fallback, use coordinate-based fallback with all agents
623+ if len ( allAgents ) > 0 {
624+ clientCoords , ok := LOCATION_COORDINATES [clientLocation ]
625+ if ok {
626+ var nearestAgent string
627+ var minDistance float64 = - 1
621628
622- for locationCode , agentIP := range geoDNSMap {
623- // Skip non-location entries like "default"
624- if locationCode == "default " {
625- continue
626- }
629+ for _ , agent := range allAgents {
630+ agentCountryCode := strings . ToLower ( agent . CountryCode )
631+ if agentCountryCode == "" {
632+ continue
633+ }
627634
628- agentCoords , ok := LOCATION_COORDINATES [ locationCode ]
629- if ! ok {
630- continue
631- }
635+ // Check political restrictions
636+ if isRoutingRestricted ( clientLocation , agentCountryCode ) {
637+ continue
638+ }
632639
633- // Check political restrictions
634- if isRoutingRestricted ( clientLocation , locationCode ) {
635- continue
636- }
640+ agentCoords , ok := LOCATION_COORDINATES [ agentCountryCode ]
641+ if ! ok {
642+ continue
643+ }
637644
638- dist := calculateHaversineDistance (
639- clientCoords .Lat , clientCoords .Lon ,
640- agentCoords .Lat , agentCoords .Lon ,
641- )
645+ dist := calculateHaversineDistance (
646+ clientCoords .Lat , clientCoords .Lon ,
647+ agentCoords .Lat , agentCoords .Lon ,
648+ )
642649
643- if minDistance == - 1 || dist < minDistance {
644- minDistance = dist
645- nearestAgent = agentIP
650+ if minDistance == - 1 || dist < minDistance {
651+ minDistance = dist
652+ nearestAgent = agent .AgentIp
653+ }
646654 }
647- }
648655
649- if nearestAgent != "" {
650- log .Printf ("[GeoDNS] No exact match for '%s', using nearest agent: %s (distance: %.0f km)" ,
651- clientLocation , nearestAgent , minDistance )
652- return nearestAgent
656+ if nearestAgent != "" {
657+ log .Printf ("[GeoDNS] No exact match for '%s', using nearest agent: %s (distance: %.0f km)" ,
658+ clientLocation , nearestAgent , minDistance )
659+ return nearestAgent
660+ }
653661 }
654662 }
655663
0 commit comments