isInternalIP returns true if the IP of addr belongs to a private network IP range. addr must only be an IP or an IP:port combination.
(addr string)
| 620 | // belongs to a private network IP range. addr |
| 621 | // must only be an IP or an IP:port combination. |
| 622 | func isInternalIP(addr string) bool { |
| 623 | privateNetworks := []string{ |
| 624 | "127.0.0.0/8", // IPv4 loopback |
| 625 | "0.0.0.0/16", |
| 626 | "10.0.0.0/8", // RFC1918 |
| 627 | "172.16.0.0/12", // RFC1918 |
| 628 | "192.168.0.0/16", // RFC1918 |
| 629 | "169.254.0.0/16", // RFC3927 link-local |
| 630 | "::1/7", // IPv6 loopback |
| 631 | "fe80::/10", // IPv6 link-local |
| 632 | "fc00::/7", // IPv6 unique local addr |
| 633 | } |
| 634 | host := hostOnly(addr) |
| 635 | ip := net.ParseIP(host) |
| 636 | if ip == nil { |
| 637 | return false |
| 638 | } |
| 639 | for _, privateNetwork := range privateNetworks { |
| 640 | _, ipnet, _ := net.ParseCIDR(privateNetwork) |
| 641 | if ipnet.Contains(ip) { |
| 642 | return true |
| 643 | } |
| 644 | } |
| 645 | return false |
| 646 | } |
| 647 | |
| 648 | // hostOnly returns only the host portion of hostport. |
| 649 | // If there is no port or if there is an error splitting |
no test coverage detected
searching dependent graphs…