mirror of
https://github.com/dcarrillo/whatismyip.git
synced 2025-07-03 12:29:25 +00:00
Use Accept request header instead of user agent to figure out non-browser clients. Funny fact, I borrowed the idea from a fork ('8a3e142cf3/router/generic.go (L33)
')
This commit is contained in:
@ -4,7 +4,6 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
|
||||
"github.com/dcarrillo/whatismyip/internal/httputils"
|
||||
"github.com/dcarrillo/whatismyip/internal/setting"
|
||||
@ -12,8 +11,6 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const userAgentPattern = `curl|wget|libwww-perl|python|ansible-httpget|HTTPie|WindowsPowerShell|http_request|Go-http-client|^$`
|
||||
|
||||
// JSONResponse maps data as json
|
||||
type JSONResponse struct {
|
||||
IP string `json:"ip"`
|
||||
@ -33,15 +30,17 @@ type JSONResponse struct {
|
||||
}
|
||||
|
||||
func getRoot(ctx *gin.Context) {
|
||||
reg := regexp.MustCompile(userAgentPattern)
|
||||
if reg.Match([]byte(ctx.Request.UserAgent())) {
|
||||
ctx.String(http.StatusOK, ctx.ClientIP())
|
||||
} else {
|
||||
switch ctx.NegotiateFormat(gin.MIMEPlain, gin.MIMEHTML, gin.MIMEJSON) {
|
||||
case gin.MIMEHTML:
|
||||
name := "home"
|
||||
if setting.App.TemplatePath != "" {
|
||||
name = filepath.Base(setting.App.TemplatePath)
|
||||
}
|
||||
ctx.HTML(http.StatusOK, name, jsonOutput(ctx))
|
||||
case gin.MIMEJSON:
|
||||
getJSON(ctx)
|
||||
default:
|
||||
ctx.String(http.StatusOK, ctx.ClientIP()+"\n")
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user