From 2873974ad62e997b177134125f64a4710436dc9d Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Mon, 13 Sep 2021 21:33:42 -0700 Subject: [PATCH] Add IP worker --- README.md | 18 ++++++++++++++++++ ip/index.js | 3 +++ ip/package.json | 19 +++++++++++++++++++ ip/wrangler.toml.example | 5 +++++ package.json | 3 ++- 5 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 ip/index.js create mode 100644 ip/package.json create mode 100644 ip/wrangler.toml.example diff --git a/README.md b/README.md index e724787..d98b15c 100644 --- a/README.md +++ b/README.md @@ -117,3 +117,21 @@ Generates a QR code for the value passed to the endpoint. Note: set the `type` p ### Usage Same as `proxy`. + +## ip + +Returns the requester's IP address. Uses the [Cloudflare header](https://developers.cloudflare.com/workers/runtime-apis/headers#cloudflare-headers) `CF-Connecting-IP` to get this value. Inspired by and works like [ifconfig.me](https://ifconfig.me) and [ifconfig.co](https://ifconfig.co). + +### Usage + +In bash: +``` +curl https://x.y.workers.dev +> +``` + +In PowerShell: +``` +(Invoke-WebRequest https://x.y.workers.dev).Content +> +``` diff --git a/ip/index.js b/ip/index.js new file mode 100644 index 0000000..d573cb8 --- /dev/null +++ b/ip/index.js @@ -0,0 +1,3 @@ +addEventListener('fetch', event => { + event.respondWith(new Response(event.request.headers.get('CF-Connecting-IP'))); +}) diff --git a/ip/package.json b/ip/package.json new file mode 100644 index 0000000..84cdf42 --- /dev/null +++ b/ip/package.json @@ -0,0 +1,19 @@ +{ + "name": "ip", + "version": "0.1.0", + "description": "Cloudflare Worker which returns the requester's IP address.", + "private": true, + "main": "index.js", + "scripts": { + "login": "wrangler login", + "preview": "wrangler preview", + "build": "wrangler build", + "publish": "wrangler publish" + }, + "author": "Ardis Lu", + "license": "ISC", + "repository": { + "type": "git", + "url": "https://github.com/ardislu/cloudflare-workers.git" + } +} diff --git a/ip/wrangler.toml.example b/ip/wrangler.toml.example new file mode 100644 index 0000000..9ff38c2 --- /dev/null +++ b/ip/wrangler.toml.example @@ -0,0 +1,5 @@ +name = 'ip' +type = 'javascript' +account_id = '' +workers_dev = true +route = 'example.com/*' diff --git a/package.json b/package.json index 8956dc3..468272e 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "instagram", "cors", "pwned", - "qr" + "qr", + "ip" ] } -- 2.47.3