|
| 1 | + |
| 2 | +package http |
| 3 | + |
| 4 | +import ( |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net" |
| 8 | + "net/http" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +var cidrs []*net.IPNet |
| 13 | + |
| 14 | +func init() { |
| 15 | + maxCidrBlocks := []string{ |
| 16 | + "127.0.0.1/8", // localhost |
| 17 | + "10.0.0.0/8", // 24-bit block |
| 18 | + "172.16.0.0/12", // 20-bit block |
| 19 | + "192.168.0.0/16", // 16-bit block |
| 20 | + "169.254.0.0/16", // link local address |
| 21 | + "::1/128", // localhost IPv6 |
| 22 | + "fc00::/7", // unique local address IPv6 |
| 23 | + "fe80::/10", // link local address IPv6 |
| 24 | + } |
| 25 | + |
| 26 | + cidrs = make([]*net.IPNet, len(maxCidrBlocks)) |
| 27 | + for i, maxCidrBlock := range maxCidrBlocks { |
| 28 | + _, cidr, err := net.ParseCIDR(maxCidrBlock) |
| 29 | + if err != nil { |
| 30 | + panic(fmt.Sprintf("failed to parse CIDR block %q: %v", maxCidrBlock, err)) |
| 31 | + } |
| 32 | + cidrs[i] = cidr |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// IPFromRequest return client's real public IP address from http request headers. |
| 37 | +func IPFromRequest(r *http.Request) string { |
| 38 | + // If we have it, return this first. |
| 39 | + // |
| 40 | + // https://developers.cloudflare.com/fundamentals/get-started/reference/http-request-headers/#cf-connecting-ip |
| 41 | + if ip := r.Header.Get("Cf-Connecting-Ip"); ip != "" { |
| 42 | + return ip |
| 43 | + } |
| 44 | + |
| 45 | + // If we have it, try to return the first global address in X-Forwarded-For |
| 46 | + for _, ip := range strings.Split(r.Header.Get("X-Forwarded-For"), ",") { |
| 47 | + ip = strings.TrimSpace(ip) |
| 48 | + isPrivate, err := isPrivateAddress(ip) |
| 49 | + if !isPrivate && err == nil { |
| 50 | + return ip |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // Check X-Real-Ip header next |
| 55 | + if ip := r.Header.Get("X-Real-Ip"); ip != "" { |
| 56 | + return ip |
| 57 | + } |
| 58 | + |
| 59 | + // If all else fails, return the remote address |
| 60 | + // |
| 61 | + // If there are colon in remote address, remove the port number |
| 62 | + // otherwise, return remote address as is |
| 63 | + var ip string |
| 64 | + if strings.ContainsRune(r.RemoteAddr, ':') { |
| 65 | + ip, _, _ = net.SplitHostPort(r.RemoteAddr) //nolint:errcheck |
| 66 | + } else { |
| 67 | + ip = r.RemoteAddr |
| 68 | + } |
| 69 | + return ip |
| 70 | +} |
| 71 | + |
| 72 | +// isPrivateAddress works by checking if the address is under private CIDR blocks. |
| 73 | +// List of private CIDR blocks can be seen on : |
| 74 | +// |
| 75 | +// https://en.wikipedia.org/wiki/Private_network |
| 76 | +// |
| 77 | +// https://en.wikipedia.org/wiki/Link-local_address |
| 78 | +func isPrivateAddress(address string) (bool, error) { |
| 79 | + ipAddress := net.ParseIP(address) |
| 80 | + if ipAddress == nil { |
| 81 | + return false, errors.New("address is not valid") |
| 82 | + } |
| 83 | + |
| 84 | + for i := range cidrs { |
| 85 | + if cidrs[i].Contains(ipAddress) { |
| 86 | + return true, nil |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return false, nil |
| 91 | +} |
0 commit comments