Skip to content

Commit 293d578

Browse files
committed
feat: use all agents list for coordinate-based DNS fallback
- Add FallbackAgentInfo struct and Agents field to Config - Update WebSocketConfig to receive agents list from Core - Update ConfigManager with GetAgents() method - Update ConfigManagerInterface with GetAgents() method - Update findBestAgentIP to use allAgents for coordinate-based fallback - Update all calls to findBestAgentIP with agents list - DNS server now matches IP Check page logic exactly
1 parent 214f689 commit 293d578

4 files changed

Lines changed: 73 additions & 43 deletions

File tree

config/manager.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,15 @@ func (cm *ConfigManager) GetProxies() []Proxy {
428428
return proxies
429429
}
430430

431+
func (cm *ConfigManager) GetAgents() []FallbackAgentInfo {
432+
cm.mu.RLock()
433+
defer cm.mu.RUnlock()
434+
435+
agents := make([]FallbackAgentInfo, len(cm.config.Agents))
436+
copy(agents, cm.config.Agents)
437+
return agents
438+
}
439+
431440
func (cm *ConfigManager) GetStats() Stats {
432441
cm.stats.mu.RLock()
433442
defer cm.stats.mu.RUnlock()

config/types.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,22 @@ import (
55
)
66

77
type Config struct {
8-
Domains []Domain `json:"domains"`
9-
Proxies []Proxy `json:"proxies"`
8+
Domains []Domain `json:"domains"`
9+
Agents []FallbackAgentInfo `json:"agents"` // All agents for coordinate-based fallback
10+
Proxies []Proxy `json:"proxies"`
1011
LastUpdate time.Time
1112
}
1213

14+
// FallbackAgentInfo represents agent information for coordinate-based fallback
15+
type FallbackAgentInfo struct {
16+
AgentId string `json:"agentId"`
17+
AgentName string `json:"agentName"`
18+
AgentIp string `json:"agentIp"`
19+
LoadScore float64 `json:"loadScore"`
20+
CountryCode string `json:"countryCode"`
21+
City string `json:"city"`
22+
}
23+
1324
type Domain struct {
1425
ID string `json:"id,omitempty"` // MongoDB ObjectId домена
1526
Domain string `json:"domain"`

config/websocket.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ type WebSocketMessage struct {
2121

2222
// WebSocketConfig represents the config sent via WebSocket
2323
type WebSocketConfig struct {
24-
AgentId string `json:"agentId"`
25-
Domains []Domain `json:"domains"`
26-
Proxies []Proxy `json:"proxies"`
27-
GeoCode string `json:"geoCode,omitempty"`
28-
Bans []BanInfo `json:"bans"`
29-
Hash int `json:"hash,omitempty"` // Config version hash
24+
AgentId string `json:"agentId"`
25+
Domains []Domain `json:"domains"`
26+
Proxies []Proxy `json:"proxies"`
27+
Agents []FallbackAgentInfo `json:"agents"` // All agents for coordinate-based fallback
28+
GeoCode string `json:"geoCode,omitempty"`
29+
Bans []BanInfo `json:"bans"`
30+
Hash int `json:"hash,omitempty"` // Config version hash
3031
}
3132

3233
// BanInfo represents ban information
@@ -287,6 +288,7 @@ func (w *WebSocketClient) convertConfig(wsConfig *WebSocketConfig) *Config {
287288
config := &Config{
288289
Domains: wsConfig.Domains,
289290
Proxies: wsConfig.Proxies,
291+
Agents: wsConfig.Agents, // All agents for coordinate-based fallback
290292
LastUpdate: time.Now(),
291293
}
292294

dns/server.go

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type DNSServer struct {
2121
type ConfigManagerInterface interface {
2222
GetDomain(domain string) *config.Domain
2323
GetAgentIP() string
24+
GetAgents() []config.FallbackAgentInfo
2425
}
2526

2627
type 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

Comments
 (0)