2022-09-28 09:01:17 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
const UnknownIP = "0.0.0.0"
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// ClientIP returns the client IP address from the request context or a placeholder if it is unknown.
|
|
|
|
func ClientIP(c *gin.Context) (ip string) {
|
|
|
|
if c == nil {
|
|
|
|
// Should never happen.
|
2022-10-02 11:38:30 +02:00
|
|
|
return UnknownIP
|
2022-10-11 22:44:11 +02:00
|
|
|
} else if ip = c.ClientIP(); ip != "" {
|
|
|
|
return ip
|
|
|
|
} else if ip = c.RemoteIP(); ip != "" {
|
|
|
|
return ip
|
2022-09-28 09:01:17 +02:00
|
|
|
}
|
|
|
|
|
2022-10-11 22:44:11 +02:00
|
|
|
// Tests may not specify an IP address.
|
|
|
|
return UnknownIP
|
2022-09-28 09:01:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// UserAgent returns the user agent from the request context or an empty string if it is unknown.
|
|
|
|
func UserAgent(c *gin.Context) string {
|
|
|
|
if c == nil {
|
|
|
|
// Should never happen.
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Request.UserAgent()
|
|
|
|
}
|