-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
ip.go
55 lines (50 loc) · 1.44 KB
/
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
52
53
54
55
// Copyright 2021 Changkun Ou. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package utils
import (
"crypto/sha1"
"fmt"
"io"
"net"
"net/http"
"strings"
"changkun.de/x/redir/internal/config"
)
// ReadIP implements a best effort approach to return the real client IP,
// it parses X-Real-IP and X-Forwarded-For in order to work properly with
// reverse-proxies such us: nginx or haproxy. Use X-Forwarded-For before
// X-Real-Ip as nginx uses X-Real-Ip with the proxy's IP.
//
// The purpose of this function is to produce an identifier of visitor.
// It does not matter wheather it is an real IP or not. Depending on the
// configuration, the returned IP address might be an encrypted hash string.
//
// This implementation is derived from gin-gonic/gin.
func ReadIP(r *http.Request) (ip string) {
defer func() {
// GDPR friendly stuff
if config.Conf.Stats.HideIP {
hh := sha1.New()
io.WriteString(hh, ip)
ip = fmt.Sprintf("%x", hh.Sum(nil))
}
}()
ip = r.Header.Get("X-Forwarded-For")
ip = strings.TrimSpace(strings.Split(ip, ",")[0])
if ip == "" {
ip = strings.TrimSpace(r.Header.Get("X-Real-Ip"))
}
if ip != "" {
return ip
}
ip = r.Header.Get("X-Appengine-Remote-Addr")
if ip != "" {
return ip
}
ip, _, err := net.SplitHostPort(strings.TrimSpace(r.RemoteAddr))
if err != nil {
return "unknown" // use unknown to guarantee non empty string
}
return ip
}