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
View File
@@ -40,7 +40,6 @@ require (
github.com/prometheus/client_golang v1.14.0
github.com/redis/go-redis/v9 v9.0.3
github.com/rs/cors v1.8.3
github.com/sebest/xff v0.0.0-20210106013422-671bd2870b3a
github.com/stretchr/testify v1.8.2
github.com/thoas/go-funk v0.9.3
github.com/twitchtv/twirp v8.1.3+incompatible
-2
View File
@@ -377,8 +377,6 @@ github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sclevine/agouti v3.0.0+incompatible/go.mod h1:b4WX9W9L1sfQKXeJf1mUTLZKJ48R1S7H23Ji7oFO5Bw=
github.com/sclevine/spec v1.4.0 h1:z/Q9idDcay5m5irkZ28M7PtQM4aOISzOpj4bUPkDee8=
github.com/sebest/xff v0.0.0-20210106013422-671bd2870b3a h1:iLcLb5Fwwz7g/DLK89F+uQBDeAhHhwdzB5fSlVdhGcM=
github.com/sebest/xff v0.0.0-20210106013422-671bd2870b3a/go.mod h1:wozgYq9WEBQBaIJe4YZ0qTSFAMxmcwBhQH0fO0R34Z0=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
+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
}