getRemoteAddress extracts the IP address from the given string. If the string contains commas, it assumes that the first part is the original address.
(address string)
| 189 | // the string contains commas, it assumes that the first part is the |
| 190 | // original address. |
| 191 | func getRemoteAddress(address string) net.IP { |
| 192 | // X-Forwarded-For may contain multiple addresses, in case the |
| 193 | // proxies are chained; the first value is the client address |
| 194 | i := strings.IndexByte(address, ',') |
| 195 | if i == -1 { |
| 196 | i = len(address) |
| 197 | } |
| 198 | |
| 199 | // If the address contains a port, remove it |
| 200 | firstAddress := address[:i] |
| 201 | host, _, err := net.SplitHostPort(firstAddress) |
| 202 | if err != nil { |
| 203 | // This will error if there is no port, so try to parse the address |
| 204 | return net.ParseIP(firstAddress) |
| 205 | } |
| 206 | return net.ParseIP(host) |
| 207 | } |
| 208 | |
| 209 | // isContainedIn checks that the given address is contained in the given |
| 210 | // network. |
no outgoing calls
no test coverage detected