initial commit
This commit is contained in:
commit
1c0ace8ca3
11 changed files with 435 additions and 0 deletions
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
result
|
||||
result-*
|
||||
|
||||
.DS_Store
|
10
LICENSE
Normal file
10
LICENSE
Normal file
|
@ -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.
|
||||
|
54
assets/index.html
Normal file
54
assets/index.html
Normal file
|
@ -0,0 +1,54 @@
|
|||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<meta name="description" content="Ny4 IP Checker" />
|
||||
<title>What is my IP?</title>
|
||||
<link href="static/style.css" rel="stylesheet" />
|
||||
<script src="static/script.js" defer></script>
|
||||
</head>
|
||||
<body>
|
||||
<div class="center">
|
||||
<p>Ny4 IP checker!</p>
|
||||
<!-- FIXME: this is completely not responsive -->
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>source</th>
|
||||
<th>ip</th>
|
||||
<th>location</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<th>local</th>
|
||||
<th>{{.sourceIP}}</th>
|
||||
<th>N/A</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>ip.sb</th>
|
||||
<th id="check-ipsb-ip">Loading...</th>
|
||||
<th id="check-ipsb-location">Loading...</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>ipapi.is</th>
|
||||
<th id="check-ipapiis-ip">Loading...</th>
|
||||
<th id="check-ipapiis-location">Loading...</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>ip-api.com</th>
|
||||
<th id="check-speedtestcn-ip">Loading...</th>
|
||||
<th id="check-speedtestcn-location">Loading...</th>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<footer>
|
||||
(c) 2024 Guanran Wang, source code licenced under MIT.
|
||||
|
||||
<a href="https://git.ny4.dev/nyancat/ip-checker">Git Repo</a>
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
54
assets/static/script.js
Normal file
54
assets/static/script.js
Normal file
|
@ -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();
|
||||
};
|
65
assets/static/style.css
Normal file
65
assets/static/style.css
Normal file
|
@ -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%);
|
||||
}
|
85
flake.lock
Normal file
85
flake.lock
Normal file
|
@ -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
|
||||
}
|
45
flake.nix
Normal file
45
flake.nix
Normal file
|
@ -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
|
||||
];
|
||||
};
|
||||
}
|
||||
);
|
||||
}
|
3
go.mod
Normal file
3
go.mod
Normal file
|
@ -0,0 +1,3 @@
|
|||
module ip-checker
|
||||
|
||||
go 1.22.5
|
86
main.go
Normal file
86
main.go
Normal file
|
@ -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
|
||||
}
|
15
package.nix
Normal file
15
package.nix
Normal file
|
@ -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;
|
||||
};
|
||||
}
|
14
treefmt.nix
Normal file
14
treefmt.nix
Normal file
|
@ -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;
|
||||
}
|
Loading…
Reference in a new issue