mirror of
https://github.com/owncast/owncast.git
synced 2024-11-25 06:12:23 +03:00
d7e355bce1
* Add support for ending the inbound stream. Closes #191 * Add a simple success response to API requests * Connected clients API with geo details * Post-rebase cleanup * Make setting and reading geo details separate operations to unblock and speed up * Rename file * Fire geoip api call behind goroutine * Add comment * Post-rebase fixes * Add support for the MaxMind GeoLite2 GeoIP database
41 lines
1,021 B
Go
41 lines
1,021 B
Go
package utils
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"encoding/hex"
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
//GenerateClientIDFromRequest generates a client id from the provided request
|
|
func GenerateClientIDFromRequest(req *http.Request) string {
|
|
ipAddress := GetIPAddressFromRequest(req)
|
|
ipAddressComponents := strings.Split(ipAddress, ":")
|
|
ipAddressComponents[len(ipAddressComponents)-1] = ""
|
|
clientID := strings.Join(ipAddressComponents, ":") + req.UserAgent()
|
|
|
|
// Create a MD5 hash of this ip + useragent
|
|
hasher := md5.New()
|
|
hasher.Write([]byte(clientID))
|
|
return hex.EncodeToString(hasher.Sum(nil))
|
|
}
|
|
|
|
// GetIPAddressFromRequest returns the IP address from a http request
|
|
func GetIPAddressFromRequest(req *http.Request) string {
|
|
ipAddressString := req.RemoteAddr
|
|
xForwardedFor := req.Header.Get("X-FORWARDED-FOR")
|
|
if xForwardedFor != "" {
|
|
ipAddressString = xForwardedFor
|
|
}
|
|
|
|
ip, _, err := net.SplitHostPort(ipAddressString)
|
|
if err != nil {
|
|
log.Errorln(err)
|
|
return ""
|
|
}
|
|
|
|
return ip
|
|
}
|