diff --git a/go.mod b/go.mod index 1038a7f0..ced4643a 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index ccb5323c..189e10f2 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/service/rtcservice.go b/pkg/service/rtcservice.go index d712cd17..22592a4c 100644 --- a/pkg/service/rtcservice.go +++ b/pkg/service/rtcservice.go @@ -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 || diff --git a/pkg/service/utils.go b/pkg/service/utils.go index 07eca562..42f0c9c4 100644 --- a/pkg/service/utils.go +++ b/pkg/service/utils.go @@ -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 +}