implemented ip from backend

This commit is contained in:
Acid
2026-05-21 17:56:02 -04:00
parent 4f5106a824
commit 709ba501d3
7 changed files with 66 additions and 45 deletions
+1
View File
@@ -7,3 +7,4 @@ __pycache__
test.py
binary
tmp/
scrap/
+1
View File
@@ -1,5 +1,6 @@
services:
website:
env_file: .env
build:
context: .
dockerfile: Dockerfile
+2
View File
@@ -1,3 +1,5 @@
module acidburnmonkey/acidarchon
go 1.26.2
require github.com/joho/godotenv v1.5.1 // indirect
+2
View File
@@ -0,0 +1,2 @@
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
+56 -1
View File
@@ -1,15 +1,34 @@
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"os"
"text/template"
"github.com/joho/godotenv"
)
type ipResponse struct {
IP string `json:"ip"`
Asn string `json:"asn"`
AsName string `json:"as_name"`
AsDomain string `json:"as_domain"`
CountryCode string `json:"country_code"`
Country string `json:"country"`
ContinentCode string `json:"continent_code"`
Continent string `json:"continent"`
}
func main() {
mux := http.NewServeMux()
godotenv.Load()
// routes
mux.HandleFunc("/", handleRoot)
mux.HandleFunc("/robots.txt", serveRobots)
@@ -28,7 +47,18 @@ func handleRoot(w http.ResponseWriter, r *http.Request) {
return
}
templ.Execute(w, nil)
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
host = r.RemoteAddr
}
dox, err := callApi(host)
if err != nil {
http.Error(w, "api call failed", http.StatusInternalServerError)
return
}
templ.Execute(w, dox)
}
func serveRobots(w http.ResponseWriter, r *http.Request) {
@@ -40,3 +70,28 @@ func serveRobots(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, string(robots))
}
func callApi(ip string) (ipResponse, error) {
api := os.Getenv("IPAPI")
fmt.Printf("api: %v\n", api)
var values ipResponse
if api == " " {
return values, errors.New("No api key")
}
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")
}
defer request.Body.Close()
body, _ := io.ReadAll(request.Body)
err = json.Unmarshal(body, &values)
return values, nil
}
-38
View File
@@ -1,38 +0,0 @@
var requestOptions = {
method: 'GET',
headers: { 'Content-Type': 'application/json' },
};
fetch('/doxme/', requestOptions) //call api
.then(function (response) {
return response.json();
})
.then(function (jsonResponse) {
var user_ip = jsonResponse.ip_info.ip;
var user_country = jsonResponse.ip_info.country;
var user_region = jsonResponse.ip_info.country_code;
// Update the HTML elements with the fetched information
document.getElementById('ip').textContent = ''.concat(user_ip);
document.getElementById('country').textContent = ''.concat(user_country);
document.getElementById('region').textContent = ''.concat(user_region);
console.log('user_ip:', user_ip);
console.log('couintry:', user_country);
console.log('region:', user_region);
})
.catch(function (error) {
console.error('Error fetching IP information:', error);
});
fetch('http://ip-api.com/json/')
.then(function (response) {
return response.json();
})
.then(function (data) {
var user_city = data.city;
var user_isp = data.isp;
console.log('user_city', user_city);
console.log('user_isp', user_isp);
document.getElementById('city').textContent = ''.concat(user_city);
document.getElementById('isp').textContent = ''.concat(user_isp);
})
.catch(function (err) {
console.log('idk error', err);
});
+4 -6
View File
@@ -15,12 +15,10 @@
WHO R U?
</h1>
<div class="container">
<div class="info"><h2>IP Address:</h2><p id="ip">Loading...</p></div>
<div class="info"><h2>Country:</h2><p id="country">Loading...</p></div>
<div class="info"><h2>City:</h2><p id="city">Loading...</p></div>
<div class="info"><h2>Region:</h2><p id="region">Loading...</p></div>
<div class="info"><h2>Internet Provider:</h2><p id="isp">Loading...</p></div>
<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>
<script src="/static/scripts.js" defer></script>
</body>
</html>