feat: use new properties to override values from headers
This commit is contained in:
parent
a978d4921d
commit
07ef234cac
4 changed files with 69 additions and 136 deletions
|
|
@ -6,6 +6,7 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
|
@ -83,7 +84,6 @@ func sendRequestAndParse(ctx context.Context, url string, body interface{}, head
|
|||
return nil
|
||||
}
|
||||
|
||||
// opts the port from the host.
|
||||
func parseDomainFromHost(host string) string {
|
||||
// check if the host has a port
|
||||
if strings.Contains(host, ":") {
|
||||
|
|
@ -103,3 +103,36 @@ func parseAcceptLanguage(acceptLanguage string) string {
|
|||
}
|
||||
return matches[0][1]
|
||||
}
|
||||
|
||||
func extractRemoteIP(req *http.Request) string {
|
||||
if ip := req.Header.Get("CF-Connecting-IP"); ip != "" {
|
||||
return ip
|
||||
}
|
||||
|
||||
if ip := req.Header.Get("x-vercel-ip"); ip != "" {
|
||||
return ip
|
||||
}
|
||||
|
||||
// Standard proxy headers
|
||||
if xff := req.Header.Get("X-Forwarded-For"); xff != "" {
|
||||
ips := strings.Split(xff, ",")
|
||||
if len(ips) > 0 {
|
||||
return strings.TrimSpace(ips[0])
|
||||
}
|
||||
}
|
||||
|
||||
if xrip := req.Header.Get("X-Real-IP"); xrip != "" {
|
||||
return xrip
|
||||
}
|
||||
|
||||
// Direct connection
|
||||
if req.RemoteAddr != "" {
|
||||
ip, _, err := net.SplitHostPort(req.RemoteAddr)
|
||||
if err == nil {
|
||||
return ip
|
||||
}
|
||||
return req.RemoteAddr
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue