isLoopback returns true if the host (with or without port) is a loopback address. This allows internal health checks and agent connections that bypass the proxy.
(hostPort string)
| 192 | // isLoopback returns true if the host (with or without port) is a loopback address. |
| 193 | // This allows internal health checks and agent connections that bypass the proxy. |
| 194 | func isLoopback(hostPort string) bool { |
| 195 | host := hostPort |
| 196 | if idx := strings.LastIndex(hostPort, ":"); idx != -1 { |
| 197 | // Handle IPv6 [::1]:port |
| 198 | if strings.Contains(hostPort, "]") { |
| 199 | host = strings.TrimPrefix(hostPort[:strings.Index(hostPort, "]")+1], "[") |
| 200 | host = strings.TrimSuffix(host, "]") |
| 201 | } else { |
| 202 | host = hostPort[:idx] |
| 203 | } |
| 204 | } |
| 205 | host = strings.TrimPrefix(host, "[") |
| 206 | host = strings.TrimSuffix(host, "]") |
| 207 | return host == "localhost" || host == "127.0.0.1" || host == "::1" || strings.HasPrefix(host, "127.") |
| 208 | } |