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;