From 19c6ecd7ff078e97d31675582d778d9344af3e67 Mon Sep 17 00:00:00 2001 From: Foster Hangdaan Date: Mon, 13 Nov 2023 18:31:54 -0500 Subject: [PATCH] Version 0.2.0: commit 54ef9b63c845b58ecce041ed8e189922f0383ee3 Author: Foster Hangdaan Date: Mon Nov 13 18:20:32 2023 -0500 fix: Minor update to app description commit 823ba6fa0a7a13ccd5e4c83f5f6825f4a53f5e2c Author: Foster Hangdaan Date: Mon Nov 13 18:12:41 2023 -0500 feat: add endpoint for obtaining IPv4 address commit 2e265aee602ccde515852b7a98bd1b92d9a42447 Author: Foster Hangdaan Date: Mon Nov 13 15:42:08 2023 -0500 Bump version to 0.2.0 --- Containerfile | 2 +- README.org | 2 +- main.ts | 30 +++++++++++++++++++++++++++--- tests/main.ts | 2 +- utils/app.ts | 2 +- version.ts | 2 +- 6 files changed, 32 insertions(+), 8 deletions(-) diff --git a/Containerfile b/Containerfile index f321f0a..24f9e91 100644 --- a/Containerfile +++ b/Containerfile @@ -1,4 +1,4 @@ -# IpMe - A self-hosted API for obtaining a public IP address. +# IpMe - A self-hosted API for obtaining your public IP address. # Copyright (C) 2023 Foster Hangdaan # This program is free software: you can redistribute it and/or modify diff --git a/README.org b/README.org index 4350f15..8e00d69 100644 --- a/README.org +++ b/README.org @@ -2,7 +2,7 @@ * About -A self-hosted API for obtaining public IP address. +A self-hosted API for obtaining your public IP address. * Contributing diff --git a/main.ts b/main.ts index b4dfe6c..da86fd7 100644 --- a/main.ts +++ b/main.ts @@ -1,5 +1,5 @@ /* - * IpMe - A self-hosted API for obtaining a public IP address. + * IpMe - A self-hosted API for obtaining your public IP address. * Copyright (C) 2023 Foster Hangdaan * * This program is free software: you can redistribute it and/or modify @@ -20,9 +20,33 @@ import { getVersion } from "./utils/app.ts"; const port = Number(Deno.env.get("PORT")) || 8000; -const handler = (_req: Request): Response => { - return new Response("Hello world"); +const ROOT_PATTERN = new URLPattern({ pathname: "/" }); + +const handler = (req: Request): Response => { + const rootMatch = ROOT_PATTERN.exec(req.url); + + if (rootMatch && req.method === "GET") { + const clientIpAddress = req.headers.get("x-forwarded-for")?.split(",").shift() || req.headers.get("host"); + const url = new URL(req.url); + const format = url.searchParams.get("format"); + + if (format === "json") { + return new Response(JSON.stringify({ ip: clientIpAddress }), { + headers: { + "content-type": "application/json", + }, + }); + } else { + return new Response(clientIpAddress); + } + } else { + return new Response("Not found", { status: 404 }); + } }; +console.log("IpMe - A self-hosted API for obtaining your public IP address.") console.log(`Version: ${getVersion()}`); +console.log("Source Code: https://code.fosterhangdaan.com/foster/ipme") +console.log("License: GNU AGPL (version 3 or later)") + Deno.serve({ port }, handler); diff --git a/tests/main.ts b/tests/main.ts index ffd31e0..15271d7 100644 --- a/tests/main.ts +++ b/tests/main.ts @@ -1,5 +1,5 @@ /* - * IpMe - A self-hosted API for obtaining a public IP address. + * IpMe - A self-hosted API for obtaining your public IP address. * Copyright (C) 2023 Foster Hangdaan * * This program is free software: you can redistribute it and/or modify diff --git a/utils/app.ts b/utils/app.ts index f9238ef..f5eb118 100644 --- a/utils/app.ts +++ b/utils/app.ts @@ -1,5 +1,5 @@ /* - * IpMe - Self-hosted API for obtaining a public IP address. + * IpMe - A self-hosted API for obtaining your public IP address. * Copyright (C) 2023 Foster Hangdaan * * This program is free software: you can redistribute it and/or modify diff --git a/version.ts b/version.ts index dc9ca6c..b559979 100644 --- a/version.ts +++ b/version.ts @@ -1,2 +1,2 @@ -const version = "0.1.0"; +const version = "0.2.0"; export default version;