Foster Hangdaan
9418eb04c5
Squashed commit of the following: commit 50f699bb966a014c92ecbb05f96a05fe515ff6d8 Author: Foster Hangdaan <foster@hangdaan.email> Date: Tue Nov 14 20:58:51 2023 -0500 doc: Update README.org - Added a table of API URLs with their respective response type and sample output. - Place the `curl` examples in their own section. commit 28d1bdc4e457cfc44830c8a20013a3f588dafca4 Author: Foster Hangdaan <foster@hangdaan.email> Date: Tue Nov 14 20:46:59 2023 -0500 feat: Added `jsonp` format Accepts the `callback` query param for changing the name of the callback function. commit 126455a3fd96ec3a998847e29cbdcefbd18b16ac Author: Foster Hangdaan <foster@hangdaan.email> Date: Tue Nov 14 20:35:30 2023 -0500 refactor: Convert `if...else` to `switch` commit 8945673d5e0644ab6aea8dc268cbaeb12530649c Author: Foster Hangdaan <foster@hangdaan.email> Date: Tue Nov 14 05:50:20 2023 -0500 doc: Update README.org - Added new sections. - Moved copyright text into its own section. - Some minor edits here and there. commit f5a6804a9ea57ad52a5f000643661ffd3908dcaa Author: Foster Hangdaan <foster@hangdaan.email> Date: Tue Nov 14 05:11:47 2023 -0500 Bump version to 0.3.0
64 lines
2.3 KiB
TypeScript
64 lines
2.3 KiB
TypeScript
/*
|
|
* IpMe - A self-hosted API for obtaining your public IP address.
|
|
* Copyright (C) 2023 Foster Hangdaan <foster@hangdaan.email>
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
* License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
import { getVersion } from "./utils/app.ts";
|
|
|
|
const port = Number(Deno.env.get("PORT")) || 8000;
|
|
|
|
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");
|
|
|
|
switch(format) {
|
|
case "json": {
|
|
return new Response(JSON.stringify({ ip: clientIpAddress }), {
|
|
headers: {
|
|
"content-type": "application/json",
|
|
},
|
|
});
|
|
}
|
|
case "jsonp": {
|
|
const parameter = JSON.stringify({ ip: clientIpAddress });
|
|
const callback = url.searchParams.get("callback") || "callback";
|
|
return new Response(`${callback}(${parameter});`, {
|
|
headers: {
|
|
"content-type": "application/javascript",
|
|
},
|
|
});
|
|
}
|
|
default: {
|
|
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);
|