mirror of
https://github.com/owncast/owncast.git
synced 2024-11-26 23:24:29 +03:00
4d2066a76d
* Start cleaning up linter errors. For #357 * Fix unmarshalling NullTime values * More linter fixes * Remove commented code * Move defer up * Consolidate error check lines * Move error check to make sure row iteration was successful * Cleaner error check + do not recreate pipe if it exists * Consolidate hashing to generate client id
40 lines
1,000 B
Go
40 lines
1,000 B
Go
package utils
|
|
|
|
import (
|
|
"crypto/md5" //nolint
|
|
"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
|
|
b := md5.Sum([]byte(clientID)) // nolint
|
|
return hex.EncodeToString(b[:])
|
|
}
|
|
|
|
// 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 != "" {
|
|
return xForwardedFor
|
|
}
|
|
|
|
ip, _, err := net.SplitHostPort(ipAddressString)
|
|
if err != nil {
|
|
log.Errorln(err)
|
|
return ""
|
|
}
|
|
|
|
return ip
|
|
}
|