https://github.com/kataras/realip
Extract the real HTTP client's Remote IP Address
https://github.com/kataras/realip
go golang http iris realip
Last synced: 6 months ago
JSON representation
Extract the real HTTP client's Remote IP Address
- Host: GitHub
- URL: https://github.com/kataras/realip
- Owner: kataras
- License: mit
- Created: 2020-07-14T09:37:02.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-12-24T06:22:31.000Z (about 3 years ago)
- Last Synced: 2025-03-25T01:51:10.569Z (10 months ago)
- Topics: go, golang, http, iris, realip
- Language: Go
- Homepage:
- Size: 12.7 KB
- Stars: 8
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Real IP
[](https://github.com/kataras/realip/actions) [](https://goreportcard.com/report/github.com/kataras/realip) [](https://pkg.go.dev/github.com/kataras/realip)
Extract the real HTTP client's Remote IP Address.
## Installation
The only requirement is the [Go Programming Language](https://golang.org/dl).
```sh
$ go get github.com/kataras/realip
```
## Getting Started
The main function is `Get`, it makes use of the `Default` options to extract the request's remote address.
```go
package main
import (
"fmt"
"net/http"
"github.com/kataras/realip"
)
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
func handler(w http.ResponseWriter, r *http.Request) {
ip := realip.Get(r)
fmt.Fprintf(w, "Your Public IPv4 is: %s", ip)
}
```
> The `Get(r)` function calls the `Default.Get(r)` method
### Options
Here are the default values:
```go
var Default = Options{
Headers: []string{
"X-Real-Ip",
"X-Forwarded-For",
"CF-Connecting-IP",
},
PrivateSubnets: []Range{
{
Start: net.ParseIP("10.0.0.0"),
End: net.ParseIP("10.255.255.255"),
},
{
Start: net.ParseIP("100.64.0.0"),
End: net.ParseIP("100.127.255.255"),
},
{
Start: net.ParseIP("172.16.0.0"),
End: net.ParseIP("172.31.255.255"),
},
{
Start: net.ParseIP("192.0.0.0"),
End: net.ParseIP("192.0.0.255"),
},
{
Start: net.ParseIP("192.168.0.0"),
End: net.ParseIP("192.168.255.255"),
},
{
Start: net.ParseIP("198.18.0.0"),
End: net.ParseIP("198.19.255.255"),
},
},
}
```
Use the `AddRange` method helper to add an IP range in **custom** options:
```go
func main() {
myOptions := &realip.Options{Headers: []string{"X-Forwarded-For"}}
myOptions.AddRange("192.168.0.0", "192.168.255.255")
// [...]
http.HandleFunc("/", handler(myOptions))
}
func handler(opts *realip.Options) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request){
ip := opts.Get(r)
// [...]
}
}
```
Please navigate through [_examples](_examples) directory for more.
## License
This software is licensed under the [MIT License](LICENSE).