diff --git a/.gitignore b/.gitignore index 8b59ef4..fda97e8 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ __pycache__ test.py binary tmp/ +scrap/ diff --git a/docker-compose.yml b/docker-compose.yml index 2401553..52849c2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,6 @@ services: website: + env_file: .env build: context: . dockerfile: Dockerfile diff --git a/go.mod b/go.mod index f3b8732..3b6bc2b 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module acidburnmonkey/acidarchon go 1.26.2 + +require github.com/joho/godotenv v1.5.1 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d61b19e --- /dev/null +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index 28771fa..78ae39e 100644 --- a/main.go +++ b/main.go @@ -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 +} diff --git a/static/scripts.js b/static/scripts.js deleted file mode 100644 index db95b4f..0000000 --- a/static/scripts.js +++ /dev/null @@ -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); - }); diff --git a/templates/home.html b/templates/home.html index f31e24e..f140e08 100644 --- a/templates/home.html +++ b/templates/home.html @@ -15,12 +15,10 @@ WHO R U?
Loading...
Loading...
Loading...
Loading...
Loading...
{{.IP}}
{{.Country}}
{{.Continent}}
{{.AsName}}