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
This commit is contained in:
@@ -1,25 +1,41 @@
|
||||
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 {
|
||||
var clientID 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 != "" {
|
||||
clientID = xForwardedFor
|
||||
} else {
|
||||
ipAddressString := req.RemoteAddr
|
||||
ipAddressComponents := strings.Split(ipAddressString, ":")
|
||||
ipAddressComponents[len(ipAddressComponents)-1] = ""
|
||||
clientID = strings.Join(ipAddressComponents, ":")
|
||||
ipAddressString = xForwardedFor
|
||||
}
|
||||
|
||||
// fmt.Println("IP address determined to be", ipAddress)
|
||||
ip, _, err := net.SplitHostPort(ipAddressString)
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
return ""
|
||||
}
|
||||
|
||||
return clientID + req.UserAgent()
|
||||
return ip
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user