-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
10ff9e4
commit f2b398e
Showing
5 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# go-realip-in-context | ||
|
||
Reads Real IP from request and saves in context. | ||
|
||
We use [realip](github.com/tomasen/realip) to find the client's real IP, then | ||
we save it to context. That allow us to get the client's IP in app code that | ||
doesn't have access to the request, only the context. | ||
|
||
We expose a middleware and a function. | ||
|
||
## Usage | ||
|
||
Examples use go-chi. | ||
|
||
|
||
```go | ||
package main | ||
|
||
import ( | ||
// ... | ||
"github.com/nrfta/go-realip-in-context" | ||
) | ||
|
||
// ... | ||
router := chi.NewRouter() | ||
|
||
router.Use(realip.Middleware) | ||
// ... | ||
``` | ||
|
||
```go | ||
func something(ctx context.Context) { | ||
ip := realip.GetRealIP(ctx) | ||
|
||
fmt.Println(ip) | ||
} | ||
``` | ||
## License | ||
|
||
This project is licensed under the [MIT License](LICENSE.md). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/nrfta/go-realip-in-context | ||
|
||
go 1.13 | ||
|
||
require github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce h1:fb190+cK2Xz/dvi9Hv8eCYJYvIGUTN2/KLq1pT6CjEc= | ||
github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce/go.mod h1:o8v6yHRoik09Xen7gje4m9ERNah1d1PPsVq1VEx9vE4= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package realip | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
tomasenRealip "github.com/tomasen/realip" | ||
) | ||
|
||
// Key to use when setting the Real IP. | ||
type ctxKeyRealIP int | ||
|
||
// RealIPKey is the key that holds the unique IP in a request context. | ||
const RealIPKey ctxKeyRealIP = 0 | ||
|
||
// RealIP is a middleware that sets a http.Request's RemoteAddr to the results | ||
// of parsing either the X-Forwarded-For header or the X-Real-IP header. | ||
// It also injects the IP into the context of each request. | ||
func Middleware(next http.Handler) http.Handler { | ||
fn := func(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
realIP := tomasenRealip.FromRequest(r) | ||
|
||
if realIP != "" { | ||
ctx = context.WithValue(ctx, RealIPKey, realIP) | ||
r.RemoteAddr = realIP | ||
} | ||
|
||
next.ServeHTTP(w, r.WithContext(ctx)) | ||
} | ||
return http.HandlerFunc(fn) | ||
} | ||
|
||
// GetRealIP returns a IP from the given context if one is present. | ||
// Returns the empty string if a IP cannot be found. | ||
func GetRealIP(ctx context.Context) string { | ||
if ctx == nil { | ||
return "" | ||
} | ||
if reqID, ok := ctx.Value(RealIPKey).(string); ok { | ||
return reqID | ||
} | ||
return "" | ||
} |