1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-08-01 17:38:37 +00:00

Rewrite RealIP() to avoid returning an empty string

This commit is contained in:
Frédéric Guillot 2018-06-01 07:22:18 -07:00
parent cf7a7e25fb
commit 3b39f0883c
11 changed files with 117 additions and 255 deletions

View file

@ -6,8 +6,10 @@ package request
import (
"fmt"
"net"
"net/http"
"strconv"
"strings"
"github.com/gorilla/mux"
)
@ -88,3 +90,30 @@ func HasQueryParam(r *http.Request, param string) bool {
_, ok := values[param]
return ok
}
// RealIP returns client's real IP address.
func RealIP(r *http.Request) string {
headers := []string{"X-Forwarded-For", "X-Real-Ip"}
for _, header := range headers {
value := r.Header.Get(header)
if value != "" {
addresses := strings.Split(value, ",")
address := strings.TrimSpace(addresses[0])
if net.ParseIP(address) != nil {
return address
}
}
}
// Fallback to TCP/IP source IP address.
var remoteIP string
if strings.ContainsRune(r.RemoteAddr, ':') {
remoteIP, _, _ = net.SplitHostPort(r.RemoteAddr)
} else {
remoteIP = r.RemoteAddr
}
return remoteIP
}