isCIDRMatch returns true if urlHost matches an element of cidrs. urlHost is a URL.Host ("host:port" or "host") where the `host` part can be either a domain name or an IP address. If it is a domain name, then it will be resolved to IP addresses for matching. If resolution fails, false is returned.
(cidrs []*net.IPNet, urlHost string)
| 186 | // where the `host` part can be either a domain name or an IP address. If it is a domain name, then it will be |
| 187 | // resolved to IP addresses for matching. If resolution fails, false is returned. |
| 188 | func isCIDRMatch(cidrs []*net.IPNet, urlHost string) bool { |
| 189 | if len(cidrs) == 0 { |
| 190 | return false |
| 191 | } |
| 192 | |
| 193 | host, _, err := net.SplitHostPort(urlHost) |
| 194 | if err != nil { |
| 195 | // Assume urlHost is a host without port and go on. |
| 196 | host = urlHost |
| 197 | } |
| 198 | |
| 199 | var addresses []net.IP |
| 200 | if ip := net.ParseIP(host); ip != nil { |
| 201 | // Host is an IP-address. |
| 202 | addresses = append(addresses, ip) |
| 203 | } else { |
| 204 | // Try to resolve the host's IP-address. |
| 205 | addresses, err = lookupIP(host) |
| 206 | if err != nil { |
| 207 | // We failed to resolve the host; assume there's no match. |
| 208 | return false |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | for _, addr := range addresses { |
| 213 | for _, ipnet := range cidrs { |
| 214 | // check if the addr falls in the subnet |
| 215 | if ipnet.Contains(addr) { |
| 216 | return true |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | return false |
| 222 | } |
| 223 | |
| 224 | func normalizeIndexName(val string) string { |
| 225 | if val == "index.docker.io" { |
no test coverage detected
searching dependent graphs…