1
0
Fork 0
mirror of https://github.com/owncast/owncast.git synced 2024-10-28 18:19:39 +01:00

Fix geo details for viewers not showing on CDN connection (#3359)

* Added extraction of first IP address from X-FORWARDED-FOR header

* Added tests to the GetIPAddressFromRequest util method

---------

Co-authored-by: Aziz Rmadi <azizrmadi@Azizs-MacBook-Air.local>
This commit is contained in:
armadi1809 2023-10-15 17:43:07 -05:00 committed by GitHub
parent 77f23fdbf7
commit 3019995a6f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 1 deletions

View file

@ -5,6 +5,7 @@ import (
"encoding/hex" "encoding/hex"
"net" "net"
"net/http" "net/http"
"strings"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -24,7 +25,13 @@ func GetIPAddressFromRequest(req *http.Request) string {
ipAddressString := req.RemoteAddr ipAddressString := req.RemoteAddr
xForwardedFor := req.Header.Get("X-FORWARDED-FOR") xForwardedFor := req.Header.Get("X-FORWARDED-FOR")
if xForwardedFor != "" { if xForwardedFor != "" {
return xForwardedFor clientIpString := strings.Split(xForwardedFor, ", ")[0]
ip, _, err := net.SplitHostPort(clientIpString)
if err != nil {
log.Errorln(err)
return ""
}
return ip
} }
ip, _, err := net.SplitHostPort(ipAddressString) ip, _, err := net.SplitHostPort(ipAddressString)

27
utils/clientId_test.go Normal file
View file

@ -0,0 +1,27 @@
package utils
import (
"net/http/httptest"
"testing"
)
func TestGetIPAddressFromRequest(t *testing.T) {
expectedResult := "203.0.113.195"
request := httptest.NewRequest("GET", "/", nil)
request.RemoteAddr = "203.0.113.195:41237"
//First Test without X-FORWARDED-FOR header
result := GetIPAddressFromRequest(request)
if result != expectedResult {
t.Errorf("Remote address only test failed: Expected %s, got %s", expectedResult, result)
}
//Test with X-FORWARDED-FOR header
request.Header.Set("X-FORWARDED-FOR header", "203.0.113.195:41237, 198.51.100.100:38523, 140.248.67.176:12345")
result = GetIPAddressFromRequest(request)
if result != expectedResult {
t.Errorf("X-FORWARD-FOR header test failed: Expected %s, got %s", expectedResult, result)
}
}