Create a helper for retrieving a user's actual IP (#1579)

This commit is contained in:
David Zhao
2023-04-04 19:32:49 -07:00
committed by GitHub
parent 793e61ac14
commit fc6a306031
4 changed files with 17 additions and 8 deletions
+16
View File
@@ -1,6 +1,7 @@
package service
import (
"net"
"net/http"
"regexp"
@@ -22,3 +23,18 @@ func IsValidDomain(domain string) bool {
domainRegexp := regexp.MustCompile(`^(?i)[a-z0-9-]+(\.[a-z0-9-]+)+\.?$`)
return domainRegexp.MatchString(domain)
}
func GetClientIP(r *http.Request) string {
// CF proxy typically is first thing the user reaches
if ip := r.Header.Get("CF-Connecting-IP"); ip != "" {
return ip
}
if ip := r.Header.Get("X-Forwarded-For"); ip != "" {
return ip
}
if ip := r.Header.Get("X-Real-IP"); ip != "" {
return ip
}
ip, _, _ := net.SplitHostPort(r.RemoteAddr)
return ip
}