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/dcarrillo/whatismyip/service"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
2021-11-30 16:21:01 +00:00
|
|
|
// JSONResponse maps data as json
|
2021-11-10 19:06:12 +00:00
|
|
|
type JSONResponse struct {
|
|
|
|
IP string `json:"ip"`
|
|
|
|
IPVersion byte `json:"ip_version"`
|
|
|
|
ClientPort string `json:"client_port"`
|
|
|
|
Country string `json:"country"`
|
|
|
|
CountryCode string `json:"country_code"`
|
|
|
|
City string `json:"city"`
|
|
|
|
Latitude float64 `json:"latitude"`
|
|
|
|
Longitude float64 `json:"longitude"`
|
|
|
|
PostalCode string `json:"postal_code"`
|
|
|
|
TimeZone string `json:"time_zone"`
|
|
|
|
ASN uint `json:"asn"`
|
|
|
|
ASNOrganization string `json:"asn_organization"`
|
|
|
|
Host string `json:"host"`
|
|
|
|
Headers http.Header `json:"headers"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRoot(ctx *gin.Context) {
|
2022-04-30 15:10:22 +00:00
|
|
|
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))
|
2022-04-30 15:10:22 +00:00
|
|
|
case gin.MIMEJSON:
|
|
|
|
getJSON(ctx)
|
|
|
|
default:
|
|
|
|
ctx.String(http.StatusOK, ctx.ClientIP()+"\n")
|
2021-11-10 19:06:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getClientPortAsString(ctx *gin.Context) {
|
2021-11-21 11:49:13 +00:00
|
|
|
_, port, _ := net.SplitHostPort(ctx.Request.RemoteAddr)
|
|
|
|
ctx.String(http.StatusOK, port+"\n")
|
2021-11-10 19:06:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getAllAsString(ctx *gin.Context) {
|
|
|
|
output := "IP: " + ctx.ClientIP() + "\n"
|
2021-11-21 11:49:13 +00:00
|
|
|
_, port, _ := net.SplitHostPort(ctx.Request.RemoteAddr)
|
|
|
|
output += "Client Port: " + port + "\n"
|
2021-11-10 19:06:12 +00:00
|
|
|
|
|
|
|
r := service.Geo{IP: net.ParseIP(ctx.ClientIP())}
|
|
|
|
if record := r.LookUpCity(); record != nil {
|
|
|
|
output += geoCityRecordToString(record) + "\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
if record := r.LookUpASN(); record != nil {
|
|
|
|
output += geoASNRecordToString(record) + "\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
h := ctx.Request.Header
|
|
|
|
h["Host"] = []string{ctx.Request.Host}
|
|
|
|
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 {
|
|
|
|
ip := service.Geo{IP: net.ParseIP(ctx.ClientIP())}
|
|
|
|
asnRecord := ip.LookUpASN()
|
|
|
|
cityRecord := ip.LookUpCity()
|
|
|
|
var version byte = 4
|
|
|
|
if p := net.ParseIP(ctx.ClientIP()).To4(); p == nil {
|
|
|
|
version = 6
|
|
|
|
}
|
|
|
|
|
2021-11-21 11:49:13 +00:00
|
|
|
_, port, _ := net.SplitHostPort(ctx.Request.RemoteAddr)
|
2021-11-10 19:06:12 +00:00
|
|
|
return JSONResponse{
|
|
|
|
IP: ctx.ClientIP(),
|
|
|
|
IPVersion: version,
|
2021-11-21 11:49:13 +00:00
|
|
|
ClientPort: port,
|
2021-11-10 19:06:12 +00:00
|
|
|
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,
|
|
|
|
Host: ctx.Request.Host,
|
|
|
|
Headers: ctx.Request.Header,
|
|
|
|
}
|
|
|
|
}
|