From 1c0ace8ca3e68a6f2d2080cec105197a4455eef3 Mon Sep 17 00:00:00 2001 From: Guanran Wang Date: Tue, 27 Aug 2024 06:01:24 +0800 Subject: [PATCH] initial commit --- .gitignore | 4 ++ LICENSE | 10 +++++ assets/index.html | 54 ++++++++++++++++++++++++++ assets/static/script.js | 54 ++++++++++++++++++++++++++ assets/static/style.css | 65 +++++++++++++++++++++++++++++++ flake.lock | 85 ++++++++++++++++++++++++++++++++++++++++ flake.nix | 45 +++++++++++++++++++++ go.mod | 3 ++ main.go | 86 +++++++++++++++++++++++++++++++++++++++++ package.nix | 15 +++++++ treefmt.nix | 14 +++++++ 11 files changed, 435 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 assets/index.html create mode 100644 assets/static/script.js create mode 100644 assets/static/style.css create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 go.mod create mode 100644 main.go create mode 100644 package.nix create mode 100644 treefmt.nix diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..94d7698 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +result +result-* + +.DS_Store diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d19a24e --- /dev/null +++ b/LICENSE @@ -0,0 +1,10 @@ +MIT License + +Copyright (c) 2024 Guanran Wang + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/assets/index.html b/assets/index.html new file mode 100644 index 0000000..cf116a9 --- /dev/null +++ b/assets/index.html @@ -0,0 +1,54 @@ + + + + + + + What is my IP? + + + + +
+

Ny4 IP checker!

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
sourceiplocation
local{{.sourceIP}}N/A
ip.sbLoading...Loading...
ipapi.isLoading...Loading...
ip-api.comLoading...Loading...
+
+ + + + diff --git a/assets/static/script.js b/assets/static/script.js new file mode 100644 index 0000000..790952e --- /dev/null +++ b/assets/static/script.js @@ -0,0 +1,54 @@ +function main() { + fetchAsync("https://api.ip.sb/geoip").then( + function (value) { + v = value; + document.getElementById("check-ipsb-ip").innerHTML = v.ip; + document.getElementById("check-ipsb-location").innerHTML = [ + v.city, + v.country, + v.isp, + ].join(", "); + }, + function (error) { + console.log(error); + }, + ); + fetchAsync("https://api-v3.speedtest.cn/ip").then( + function (value) { + v = value.data; + document.getElementById("check-speedtestcn-ip").innerHTML = v.ip; + document.getElementById("check-speedtestcn-location").innerHTML = [ + v.city, + v.country, + v.isp, + ].join(", "); + }, + function (error) { + console.log(error); + }, + ); + fetchAsync("https://api.ipapi.is/").then( + function (value) { + v = value; + document.getElementById("check-ipapiis-ip").innerHTML = v.ip; + document.getElementById("check-ipapiis-location").innerHTML = [ + v.location.city, + v.location.country, + v.company.name, + ].join(", "); + }, + function (error) { + console.log(error); + }, + ); +} + +async function fetchAsync(url) { + let response = await fetch(url); + let data = await response.json(); + return data; +} + +window.onload = function () { + main(); +}; diff --git a/assets/static/style.css b/assets/static/style.css new file mode 100644 index 0000000..dd276f5 --- /dev/null +++ b/assets/static/style.css @@ -0,0 +1,65 @@ +@import url("https://fonts.googleapis.com/css2?family=Fira+Code:wght@500&display=swap"); + +/* https://tailwindcss.com/docs/customizing-colors#default-color-palette */ +:root { + --bg: #ede9fe; + --text: #2e1065; + --text-footer: #64748b; + --table: #c4b5fd; +} + +@media (prefers-color-scheme: dark) { + :root { + --text: #f1f5f9; + --bg: #0f172a; + --text-footer: #64748b; + --table: #334155; + } + + a:link { + color: #3b82f6; + } + + a:visited { + color: #8b5cf6; + } +} + +body { + font-family: "Fira Code", monospace; + font-optical-sizing: auto; + font-size: 16px; + font-style: normal; + font-weight: 500; + + background-color: var(--bg); + color: var(--text); +} + +table { + width: 100%; + border-collapse: collapse; +} + +th { + border: 2px var(--table) solid; + padding: 5px 20px; +} + +footer { + color: var(--text-footer); + font-size: 14px; + + position: absolute; + bottom: 0; + left: 50%; + transform: translate(-50%, -10px); +} + +.center { + margin: 0; + position: absolute; + top: 50%; + left: 50%; + transform: translate(-50%, -50%); +} diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..67815fc --- /dev/null +++ b/flake.lock @@ -0,0 +1,85 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": [ + "systems" + ] + }, + "locked": { + "lastModified": 1710146030, + "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1724395761, + "narHash": "sha256-zRkDV/nbrnp3Y8oCADf5ETl1sDrdmAW6/bBVJ8EbIdQ=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "ae815cee91b417be55d43781eb4b73ae1ecc396c", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs", + "systems": "systems", + "treefmt-nix": "treefmt-nix" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "treefmt-nix": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1724338379, + "narHash": "sha256-kKJtaiU5Ou+e/0Qs7SICXF22DLx4V/WhG1P6+k4yeOE=", + "owner": "numtide", + "repo": "treefmt-nix", + "rev": "070f834771efa715f3e74cd8ab93ecc96fabc951", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "treefmt-nix", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..1b37175 --- /dev/null +++ b/flake.nix @@ -0,0 +1,45 @@ +{ + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + + flake-utils = { + url = "github:numtide/flake-utils"; + inputs.systems.follows = "systems"; + }; + treefmt-nix = { + url = "github:numtide/treefmt-nix"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + + ### De-dupe flake dependencies + systems.url = "github:nix-systems/default"; + }; + + outputs = + inputs: + inputs.flake-utils.lib.eachDefaultSystem ( + system: + let + pkgs = inputs.nixpkgs.legacyPackages.${system}; + treefmtEval = inputs.treefmt-nix.lib.evalModule pkgs ./treefmt.nix; + in + { + ### nix {run,shell,build} + packages.default = pkgs.callPackage ./package.nix { }; + + ### nix fmt + formatter = treefmtEval.config.build.wrapper; + + ### nix flake check + checks.formatting = treefmtEval.config.build.check inputs.self; + + ### nix develop + devShells.default = pkgs.mkShell { + nativeBuildInputs = with pkgs; [ + go + nodePackages.prettier + ]; + }; + } + ); +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..a2f3241 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module ip-checker + +go 1.22.5 diff --git a/main.go b/main.go new file mode 100644 index 0000000..85aa0f6 --- /dev/null +++ b/main.go @@ -0,0 +1,86 @@ +package main + +import ( + "fmt" + "html/template" + "log" + "net/http" + "os" + "strings" +) + +type ServerConfig struct { + listen string +} + +func main() { + config := ServerConfig{ + listen: getEnvOr("IP_CHECKER_LISTEN", ":8080"), + } + + tmpl := template.Must(template.ParseFiles("assets/index.html")) + + http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./assets/static")))) + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + handleRequest(w, r, tmpl) + }) + + log.Printf("Starting server on %s", config.listen) + log.Fatal(http.ListenAndServe(config.listen, nil)) +} + +func handleRequest(w http.ResponseWriter, r *http.Request, tmpl *template.Template) { + sourceIP := strings.Split(r.RemoteAddr, ":")[0] + isHeadless := strings.HasPrefix(r.Header.Get("User-Agent"), "curl/") + + log.Printf("r.URL.Path: %s, sourceIP: %s, isHeadless: %t, User-Agent: %s", r.URL.Path, sourceIP, isHeadless, r.Header.Get("User-Agent")) + + // TODO: /favicon.ico + switch r.URL.Path { + case "/": + if isHeadless { + fmt.Fprintf(w, "%s\n", sourceIP) + } else { + csp := []string{ + "default-src 'none'", + "img-src 'self'", + "script-src-elem 'self'", + "style-src-elem 'self' fonts.googleapis.com", + "font-src fonts.gstatic.com", + "connect-src api.ip.sb api-v3.speedtest.cn api.ipapi.is", + } + w.Header().Set("Content-Security-Policy", strings.Join(csp, "; ")) + + data := map[string]string{ + "sourceIP": sourceIP, + } + + err := tmpl.Execute(w, data) + if err != nil { + http.Error(w, "Unable to load template", http.StatusInternalServerError) + log.Printf("Template execution error: %v", err) + return + } + } + + case "/robots.txt": + fmt.Fprintf(w, `User-Agent: * +Disallow: /harming/humans +Disallow: /ignoring/human/orders +Disallow: /harm/to/self +`) + + default: + log.Printf("sourceIP: %s, isHeadless: %t", sourceIP, isHeadless) + http.NotFound(w, r) + return + } +} + +func getEnvOr(key, defaultValue string) string { + if value, exists := os.LookupEnv(key); exists { + return value + } + return defaultValue +} diff --git a/package.nix b/package.nix new file mode 100644 index 0000000..0dbaab4 --- /dev/null +++ b/package.nix @@ -0,0 +1,15 @@ +{ lib, buildGoModule }: + +buildGoModule { + pname = "ip-checker"; + version = "dev"; + + src = ./.; + + vendorHash = null; + + meta = { + homepage = "https://git.ny4.dev/nyancat/ip-checker"; + license = lib.licenses.mit; + }; +} diff --git a/treefmt.nix b/treefmt.nix new file mode 100644 index 0000000..c32c576 --- /dev/null +++ b/treefmt.nix @@ -0,0 +1,14 @@ +{ + projectRootFile = "flake.nix"; + + ### nix + programs.deadnix.enable = true; + programs.statix.enable = true; + programs.nixfmt.enable = true; + + ### go + programs.gofmt.enable = true; + + ### misc + programs.prettier.enable = true; +}