/
client_ip.go
51 lines (41 loc) · 1.11 KB
/
client_ip.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package middleware
import (
"context"
"errors"
"net/http"
"github.com/apex/log"
"github.com/artem-malko/auth-and-go/api/response"
)
var clientIPKey = &contextKey{"client ip"}
const clientIPErrorMessage = "clientIP can not be empty"
func CreateClientIPMiddleware(logger log.Interface) Middleware {
return func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
xForwardedFor := r.Header.Get("X-Forwarded-For")
xRealIP := r.Header.Get("X-Real-IP")
remoteAddr := r.RemoteAddr
remoteIP := xForwardedFor
if xForwardedFor == "" {
remoteIP = xRealIP
}
if xRealIP == "" {
remoteIP = remoteAddr
}
if remoteIP == "" {
logger.Debugf("%s", errors.New(clientIPErrorMessage))
response.Error(w, http.StatusBadRequest, clientIPErrorMessage)
return
}
ctx = context.WithValue(ctx, clientIPKey, remoteIP)
next.ServeHTTP(w, r.WithContext(ctx))
}
return http.HandlerFunc(fn)
}
}
func GetClientIP(ctx context.Context) string {
if clientIP, ok := ctx.Value(clientIPKey).(string); ok {
return clientIP
}
return ""
}