Skip to content

Commit

Permalink
Initial Implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
josemarluedke committed Jan 15, 2020
1 parent 10ff9e4 commit f2b398e
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 1 deletion.
2 changes: 1 addition & 1 deletion LICENSE → LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 NRFTA LLC
Copyright (c) 2020 NRFTA

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
40 changes: 40 additions & 0 deletions README.md
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).
5 changes: 5 additions & 0 deletions go.mod
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
2 changes: 2 additions & 0 deletions go.sum
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=
44 changes: 44 additions & 0 deletions realip.go
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 ""
}

0 comments on commit f2b398e

Please sign in to comment.