@@ -934,6 +934,7 @@ final class RouteManager: ObservableObject {
934934
935935 // Collect all routes to add (for batch operation)
936936 var routesToAdd : [ ( destination: String , gateway: String , isNetwork: Bool , source: String ) ] = [ ]
937+ var seenDestinations : Set < String > = [ ] // Deduplicate by destination IP
937938
938939 while index < allDomains. count {
939940 let endIndex = min ( index + batchSize, allDomains. count)
@@ -964,7 +965,8 @@ final class RouteManager: ObservableObject {
964965 }
965966 dnsDiskCache [ result. domain] = ips // Update persistent cache
966967
967- for ip in ips {
968+ for ip in ips where !seenDestinations. contains ( ip) {
969+ seenDestinations. insert ( ip)
968970 routesToAdd. append ( ( destination: ip, gateway: gateway, isNetwork: false , source: result. source) )
969971 }
970972 } else if let cachedIPs = dnsDiskCache [ result. domain] , !cachedIPs. isEmpty {
@@ -973,7 +975,8 @@ final class RouteManager: ObservableObject {
973975 if let firstIP = cachedIPs. first {
974976 dnsCache [ result. domain] = firstIP
975977 }
976- for ip in cachedIPs {
978+ for ip in cachedIPs where !seenDestinations. contains ( ip) {
979+ seenDestinations. insert ( ip)
977980 routesToAdd. append ( ( destination: ip, gateway: gateway, isNetwork: false , source: result. source) )
978981 }
979982 } else {
@@ -991,7 +994,8 @@ final class RouteManager: ObservableObject {
991994
992995 // Collect IP ranges (these don't need DNS resolution)
993996 for service in config. services where service. enabled {
994- for range in service. ipRanges {
997+ for range in service. ipRanges where !seenDestinations. contains ( range) {
998+ seenDestinations. insert ( range)
995999 routesToAdd. append ( ( destination: range, gateway: gateway, isNetwork: true , source: service. name) )
9961000 }
9971001 }
@@ -1094,12 +1098,14 @@ final class RouteManager: ObservableObject {
10941098
10951099 var newRoutes : [ ActiveRoute ] = [ ]
10961100 var routesToAdd : [ ( destination: String , gateway: String , isNetwork: Bool , source: String ) ] = [ ]
1101+ var seenDestinations : Set < String > = [ ] // Deduplicate by destination IP
10971102
10981103 // Build routes from DNS cache
10991104 for service in config. services where service. enabled {
11001105 for domain in service. domains {
11011106 if let cachedIPs = dnsDiskCache [ domain] {
1102- for ip in cachedIPs {
1107+ for ip in cachedIPs where !seenDestinations. contains ( ip) {
1108+ seenDestinations. insert ( ip)
11031109 routesToAdd. append ( ( destination: ip, gateway: gateway, isNetwork: false , source: service. name) )
11041110 }
11051111 if let firstIP = cachedIPs. first {
@@ -1108,15 +1114,17 @@ final class RouteManager: ObservableObject {
11081114 }
11091115 }
11101116 // IP ranges don't need DNS
1111- for range in service. ipRanges {
1117+ for range in service. ipRanges where !seenDestinations. contains ( range) {
1118+ seenDestinations. insert ( range)
11121119 routesToAdd. append ( ( destination: range, gateway: gateway, isNetwork: true , source: service. name) )
11131120 }
11141121 }
11151122
11161123 // Custom domains
11171124 for domain in config. domains where domain. enabled {
11181125 if let cachedIPs = dnsDiskCache [ domain. domain] {
1119- for ip in cachedIPs {
1126+ for ip in cachedIPs where !seenDestinations. contains ( ip) {
1127+ seenDestinations. insert ( ip)
11201128 routesToAdd. append ( ( destination: ip, gateway: gateway, isNetwork: false , source: domain. domain) )
11211129 }
11221130 if let firstIP = cachedIPs. first {
@@ -1579,6 +1587,10 @@ final class RouteManager: ObservableObject {
15791587 var newRoutes : [ ActiveRoute ] = [ ]
15801588 var routesToAdd : [ ( destination: String , gateway: String , isNetwork: Bool ) ] = [ ]
15811589
1590+ // Track existing routes to avoid duplicates
1591+ let existingDestinations = Set ( activeRoutes. map { $0. destination } )
1592+ var seenDestinations : Set < String > = existingDestinations
1593+
15821594 // Capture DNS settings for parallel resolution
15831595 let userDNS = detectedDNSServer
15841596 let fallbackDNS = config. fallbackDNS
@@ -1608,14 +1620,16 @@ final class RouteManager: ObservableObject {
16081620 dnsCache [ domain] = firstIP
16091621 }
16101622
1611- for ip in ips {
1623+ for ip in ips where !seenDestinations. contains ( ip) {
1624+ seenDestinations. insert ( ip)
16121625 routesToAdd. append ( ( destination: ip, gateway: gateway, isNetwork: false ) )
16131626 newRoutes. append ( ActiveRoute ( destination: ip, gateway: gateway, source: service. name, timestamp: Date ( ) ) )
16141627 }
16151628 }
16161629
16171630 // Add IP ranges (no DNS needed)
1618- for range in service. ipRanges {
1631+ for range in service. ipRanges where !seenDestinations. contains ( range) {
1632+ seenDestinations. insert ( range)
16191633 routesToAdd. append ( ( destination: range, gateway: gateway, isNetwork: true ) )
16201634 newRoutes. append ( ActiveRoute ( destination: range, gateway: gateway, source: service. name, timestamp: Date ( ) ) )
16211635 }
0 commit comments