whatismyip/router/generic.go

128 lines
3.3 KiB
Go
Raw Normal View History

2021-11-10 19:06:12 +00:00
package router
import (
"net"
"net/http"
"path/filepath"
"github.com/dcarrillo/whatismyip/internal/httputils"
"github.com/dcarrillo/whatismyip/internal/setting"
"github.com/gin-gonic/gin"
)
2025-01-02 19:13:41 +00:00
type GeoResponse struct {
Country string `json:"country,omitempty"`
CountryCode string `json:"country_code,omitempty"`
City string `json:"city,omitempty"`
Latitude float64 `json:"latitude,omitempty"`
Longitude float64 `json:"longitude,omitempty"`
PostalCode string `json:"postal_code,omitempty"`
TimeZone string `json:"time_zone,omitempty"`
ASN uint `json:"asn,omitempty"`
ASNOrganization string `json:"asn_organization,omitempty"`
}
2021-11-10 19:06:12 +00:00
type JSONResponse struct {
2025-01-02 19:13:41 +00:00
IP string `json:"ip"`
IPVersion byte `json:"ip_version"`
ClientPort string `json:"client_port"`
Host string `json:"host"`
Headers http.Header `json:"headers"`
GeoResponse
2021-11-10 19:06:12 +00:00
}
func getRoot(ctx *gin.Context) {
switch ctx.NegotiateFormat(gin.MIMEPlain, gin.MIMEHTML, gin.MIMEJSON) {
case gin.MIMEHTML:
2021-11-10 19:06:12 +00:00
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")
2021-11-10 19:06:12 +00:00
}
}
func getClientPort(ctx *gin.Context) string {
var port string
if setting.App.TrustedPortHeader == "" {
if setting.App.TrustedHeader != "" {
port = "unknown"
} else {
_, port, _ = net.SplitHostPort(ctx.Request.RemoteAddr)
}
} else {
port = ctx.GetHeader(setting.App.TrustedPortHeader)
if port == "" {
port = "unknown"
}
}
return port
}
2021-11-10 19:06:12 +00:00
func getClientPortAsString(ctx *gin.Context) {
ctx.String(http.StatusOK, getClientPort(ctx)+"\n")
2021-11-10 19:06:12 +00:00
}
func getAllAsString(ctx *gin.Context) {
2025-01-02 19:13:41 +00:00
ip := net.ParseIP(ctx.ClientIP())
2021-11-10 19:06:12 +00:00
2025-01-02 19:13:41 +00:00
output := "IP: " + ip.String() + "\n"
output += "Client Port: " + getClientPort(ctx) + "\n"
2021-11-10 19:06:12 +00:00
2025-01-02 19:13:41 +00:00
if geoSvc != nil {
output += geoCityRecordToString(geoSvc.LookUpCity(ip)) + "\n"
output += geoASNRecordToString(geoSvc.LookUpASN(ip)) + "\n"
2021-11-10 19:06:12 +00:00
}
h := httputils.GetHeadersWithoutTrustedHeaders(ctx)
h.Set("Host", ctx.Request.Host)
2021-11-10 19:06:12 +00:00
output += httputils.HeadersToSortedString(h)
ctx.String(http.StatusOK, output)
}
func getJSON(ctx *gin.Context) {
ctx.JSON(http.StatusOK, jsonOutput(ctx))
}
func jsonOutput(ctx *gin.Context) JSONResponse {
2025-01-02 19:13:41 +00:00
ip := net.ParseIP(ctx.ClientIP())
2021-11-10 19:06:12 +00:00
var version byte = 4
2025-01-02 19:13:41 +00:00
if p := ip.To4(); p == nil {
2021-11-10 19:06:12 +00:00
version = 6
}
2025-01-02 19:13:41 +00:00
geoResp := GeoResponse{}
if geoSvc != nil {
cityRecord := geoSvc.LookUpCity(ip)
asnRecord := geoSvc.LookUpASN(ip)
geoResp = GeoResponse{
Country: cityRecord.Country.Names["en"],
CountryCode: cityRecord.Country.ISOCode,
City: cityRecord.City.Names["en"],
Latitude: cityRecord.Location.Latitude,
Longitude: cityRecord.Location.Longitude,
PostalCode: cityRecord.Postal.Code,
TimeZone: cityRecord.Location.TimeZone,
ASN: asnRecord.AutonomousSystemNumber,
ASNOrganization: asnRecord.AutonomousSystemOrganization,
}
}
2021-11-10 19:06:12 +00:00
return JSONResponse{
2025-01-02 19:13:41 +00:00
IP: ip.String(),
IPVersion: version,
ClientPort: getClientPort(ctx),
Host: ctx.Request.Host,
Headers: httputils.GetHeadersWithoutTrustedHeaders(ctx),
GeoResponse: geoResp,
2021-11-10 19:06:12 +00:00
}
}