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
+1 -5
View File
@@ -12,7 +12,6 @@ import (
"time"
"github.com/gorilla/websocket"
"github.com/sebest/xff"
"github.com/ua-parser/uap-go/uaparser"
"github.com/livekit/livekit-server/pkg/utils"
@@ -404,10 +403,7 @@ func (s *RTCService) ParseClientInfo(r *http.Request) *livekit.ClientInfo {
ci.DeviceModel = values.Get("device_model")
ci.Network = values.Get("network")
// get real address (forwarded http header) - check Cloudflare headers first, fall back to X-Forwarded-For
ci.Address = r.Header.Get("CF-Connecting-IP")
if len(ci.Address) == 0 {
ci.Address = xff.GetRemoteAddr(r)
}
ci.Address = GetClientIP(r)
// attempt to parse types for SDKs that support browser as a platform
if ci.Sdk == livekit.ClientInfo_JS ||
+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
}