(logger slog.Logger, proxyHealth *proxyhealth.ProxyHealth)
| 1172 | ) |
| 1173 | |
| 1174 | func derpMapper(logger slog.Logger, proxyHealth *proxyhealth.ProxyHealth) func(*tailcfg.DERPMap) *tailcfg.DERPMap { |
| 1175 | return func(derpMap *tailcfg.DERPMap) *tailcfg.DERPMap { |
| 1176 | derpMap = derpMap.Clone() |
| 1177 | |
| 1178 | // Find the starting region ID that we'll use for proxies. This must be |
| 1179 | // deterministic based on the derp map. |
| 1180 | startingRegionID, largestRegionID := getProxyDERPStartingRegionID(derpMap) |
| 1181 | if largestRegionID >= 1<<32 { |
| 1182 | // Enforce an upper bound on the region ID. This shouldn't be hit in |
| 1183 | // practice, but it's a good sanity check. |
| 1184 | lastDerpConflictMutex.Lock() |
| 1185 | shouldLog := lastDerpConflictLog.IsZero() || time.Since(lastDerpConflictLog) > time.Minute |
| 1186 | if shouldLog { |
| 1187 | lastDerpConflictLog = time.Now() |
| 1188 | } |
| 1189 | lastDerpConflictMutex.Unlock() |
| 1190 | if shouldLog { |
| 1191 | logger.Warn( |
| 1192 | context.Background(), |
| 1193 | "existing DERP region IDs are too large, proxy region IDs will not be populated in the derp map. Please ensure that all DERP region IDs are less than 2^32", |
| 1194 | slog.F("largest_region_id", largestRegionID), |
| 1195 | slog.F("max_region_id", int64(1<<32-1)), |
| 1196 | ) |
| 1197 | return derpMap |
| 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | // Add all healthy proxies to the DERP map. |
| 1202 | statusMap := proxyHealth.HealthStatus() |
| 1203 | statusLoop: |
| 1204 | for _, status := range statusMap { |
| 1205 | if status.Status != proxyhealth.Healthy || !status.Proxy.DerpEnabled { |
| 1206 | // Only add healthy proxies with DERP enabled to the DERP map. |
| 1207 | continue |
| 1208 | } |
| 1209 | |
| 1210 | u, err := url.Parse(status.Proxy.Url) |
| 1211 | if err != nil { |
| 1212 | // Not really any need to log, the proxy should be unreachable |
| 1213 | // anyways and filtered out by the above condition. |
| 1214 | continue |
| 1215 | } |
| 1216 | port := u.Port() |
| 1217 | if port == "" { |
| 1218 | port = "80" |
| 1219 | if u.Scheme == "https" { |
| 1220 | port = "443" |
| 1221 | } |
| 1222 | } |
| 1223 | portInt, err := strconv.Atoi(port) |
| 1224 | if err != nil { |
| 1225 | // Not really any need to log, the proxy should be unreachable |
| 1226 | // anyways and filtered out by the above condition. |
| 1227 | continue |
| 1228 | } |
| 1229 | |
| 1230 | // Sanity check that the region ID and code is unique. |
| 1231 | // |
no test coverage detected