Compare commits

...

2 Commits

Author SHA1 Message Date
Acid 5288323831 added map 2026-05-26 18:04:08 -04:00
Acid a3ec2fada7 modified: main.go 2026-05-26 15:59:06 -04:00
3 changed files with 122 additions and 30 deletions
+58 -19
View File
@@ -2,9 +2,9 @@ package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"net"
"net/http"
"os"
@@ -24,6 +24,28 @@ type ipResponse struct {
Continent string `json:"continent"`
}
type IPInfo struct {
Query string `json:"query"`
Status string `json:"status"`
Country string `json:"country"`
CountryCode string `json:"countryCode"`
Region string `json:"region"`
RegionName string `json:"regionName"`
City string `json:"city"`
Zip string `json:"zip"`
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
Timezone string `json:"timezone"`
ISP string `json:"isp"`
Org string `json:"org"`
AS string `json:"as"`
}
type TemolateData struct {
Ip ipResponse
IP2 IPInfo
}
func main() {
mux := http.NewServeMux()
@@ -40,6 +62,7 @@ func main() {
http.ListenAndServe(":8080", mux)
}
// Routes
func handleRoot(w http.ResponseWriter, r *http.Request) {
templ, err := template.ParseFiles("templates/home.html")
if err != nil {
@@ -58,40 +81,35 @@ func handleRoot(w http.ResponseWriter, r *http.Request) {
host, _, _ = net.SplitHostPort(r.RemoteAddr)
}
dox, err := callApi(host)
if err != nil {
http.Error(w, "api call failed", http.StatusInternalServerError)
return
}
dox := callApi(host)
dox2 := secondApi(host)
templ.Execute(w, dox)
templ.Execute(w, TemolateData{Ip: dox, IP2: *dox2})
}
func serveRobots(w http.ResponseWriter, r *http.Request) {
robots, err := os.ReadFile("templates/robots.txt")
if err != nil {
fmt.Fprintf(w, "sucks to suck")
return
}
fmt.Fprint(w, string(robots))
http.ServeFile(w, r, ("templates/robots.txt"))
}
func callApi(ip string) (ipResponse, error) {
// utils
func callApi(ip string) ipResponse {
api := os.Getenv("IPAPI")
fmt.Printf("api: %v\n", api)
slog.Info("client called", "ip", ip)
var values ipResponse
if api == " " {
return values, errors.New("No api key")
slog.Error("No api provided")
return values
}
url := fmt.Sprintf("https://api.ipinfo.io/lite/%s?token=%s", ip, api)
request, err := http.Get(url)
if err != nil {
return values, errors.New("request failed")
slog.Error("callApi Error")
return values
}
defer request.Body.Close()
@@ -99,5 +117,26 @@ func callApi(ip string) (ipResponse, error) {
err = json.Unmarshal(body, &values)
return values, nil
return values
}
func secondApi(ip string) *IPInfo {
url := fmt.Sprintf("http://ip-api.com/json/%s", ip)
var res IPInfo
response, err := http.Get(url)
if err != nil {
slog.Error("secondApi err")
return &res
}
defer response.Body.Close()
body, _ := io.ReadAll(response.Body)
json.Unmarshal(body, &res)
slog.Debug("secondApi", "city", res.City, "lat", res.Lat, "lon", res.Lon, "status", res.Status)
return &res
}
+17
View File
@@ -13,6 +13,23 @@ h1 {
font-family: GlitchGoblin;
}
.layout {
display: flex;
gap: 40px;
align-items: flex-start;
padding: 0 40px;
}
.container {
flex-shrink: 0;
}
#map {
width: 400px;
height: 400px;
flex-shrink: 0;
}
.info {
margin-bottom: 20px;
}
+46 -10
View File
@@ -1,24 +1,60 @@
<!doctype html>
<html>
<head>
<head>
<link rel="icon" type="image/x-icon" href="/static/favicon.ico" />
<link rel="stylesheet" href="/static/styles.css" />
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta charset="UTF-8">
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<meta charset="UTF-8" />
<title>Who are you?</title>
</head>
<body>
</head>
<body>
<h1>
U HAVE THREAD <br />
UPON MY DOMAIN <br />
& MUST SUFFER <br />
WHO R U?
</h1>
<div class="layout">
<div class="container">
<div class="info"><h2>IP Address:</h2><p>{{.IP}}</p></div>
<div class="info"><h2>Country:</h2><p>{{.Country}}</p></div>
<div class="info"><h2>Continent:</h2><p>{{.Continent}}</p></div>
<div class="info"><h2>Internet Provider:</h2><p>{{.AsName}}</p></div>
<div class="info">
<h2>IP Address:</h2>
<p>{{.Ip.IP}}</p>
</div>
</body>
<div class="info">
<h2>Country:</h2>
<p>{{.Ip.Country}}</p>
</div>
<div class="info">
<h2>Continent:</h2>
<p>{{.Ip.Continent}}</p>
</div>
<div class="info">
<h2>Internet Provider:</h2>
<p>{{.Ip.AsName}}</p>
</div>
<div class="info">
<h2>City:</h2>
<p>{{.IP2.City}}</p>
</div>
<div class="info">
<h2>Coordinates:</h2>
<p>{{.IP2.Lat}}, {{.IP2.Lon}}</p>
</div>
</div>
<div id="map"></div>
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
<script>
var map = L.map('map', { maxZoom: 10 }).setView([{{.IP2.Lat}}, {{.IP2.Lon}}], 8);
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© OpenStreetMap'
}).addTo(map);
L.marker([{{.IP2.Lat}}, {{.IP2.Lon}}]).addTo(map);
</script>
</div>
</body>
</html>