Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sebest/xff
A Golang Middleware to handle X-Forwarded-For Header
https://github.com/sebest/xff
Last synced: 3 months ago
JSON representation
A Golang Middleware to handle X-Forwarded-For Header
- Host: GitHub
- URL: https://github.com/sebest/xff
- Owner: sebest
- License: mit
- Created: 2014-12-22T10:29:05.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2022-01-18T20:54:49.000Z (almost 3 years ago)
- Last Synced: 2024-06-18T13:49:30.617Z (5 months ago)
- Language: Go
- Size: 13.7 KB
- Stars: 98
- Watchers: 3
- Forks: 25
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - xff - A Golang Middleware to handle X-Forwarded-For Header - ★ 68 (Web Frameworks)
- awesome-go-extra - xff - Forwarded-For Header|91|23|8|2014-12-22T10:29:05Z|2022-01-18T20:54:49Z| (Web Frameworks / Fail injection)
README
# X-Forwarded-For middleware fo Go [![godoc](http://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/sebest/xff) [![Build Status](https://travis-ci.org/sebest/xff.svg?branch=master)](https://travis-ci.org/sebest/xff)
Package `xff` is a `net/http` middleware/handler to parse [Forwarded HTTP Extension](http://tools.ietf.org/html/rfc7239) in Golang.
## Example usage
Install `xff`:
go get github.com/sebest/xff
Edit `server.go`:
```go
package mainimport (
"net/http""github.com/sebest/xff"
)func main() {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello from " + r.RemoteAddr + "\n"))
})xffmw, _ := xff.Default()
http.ListenAndServe(":8080", xffmw.Handler(handler))
}
```Then run your server:
go run server.go
The server now runs on `localhost:8080`:
$ curl -D - -H 'X-Forwarded-For: 42.42.42.42' http://localhost:8080/
HTTP/1.1 200 OK
Date: Fri, 20 Feb 2015 20:03:02 GMT
Content-Length: 29
Content-Type: text/plain; charset=utf-8hello from 42.42.42.42:52661