1
0
Fork 0
mirror of https://github.com/owncast/owncast.git synced 2024-10-28 10:09:39 +01:00
owncast/utils/clientId.go
Gabe Kangas d7e355bce1
Connected clients admin API (#217)
* 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
2020-10-06 23:14:33 -07:00

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
}