]> git.ardis.lu - cloudflare-workers/commitdiff
Add IP worker
authorArdis Lu <ardisglu@gmail.com>
Tue, 14 Sep 2021 04:33:42 +0000 (21:33 -0700)
committerArdis Lu <ardisglu@gmail.com>
Tue, 14 Sep 2021 04:33:42 +0000 (21:33 -0700)
README.md
ip/index.js [new file with mode: 0644]
ip/package.json [new file with mode: 0644]
ip/wrangler.toml.example [new file with mode: 0644]
package.json

index e724787eefaea29996407b3f1598b01eb94d1480..d98b15c5e8982f39020c7a778fe4349a67f268bd 100644 (file)
--- 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
+> <your IP address>
+```
+
+In PowerShell:
+```
+(Invoke-WebRequest https://x.y.workers.dev).Content
+> <your IP address>
+```
diff --git a/ip/index.js b/ip/index.js
new file mode 100644 (file)
index 0000000..d573cb8
--- /dev/null
@@ -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 (file)
index 0000000..84cdf42
--- /dev/null
@@ -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 (file)
index 0000000..9ff38c2
--- /dev/null
@@ -0,0 +1,5 @@
+name = 'ip'
+type = 'javascript'
+account_id = ''
+workers_dev = true
+route = 'example.com/*'
index 8956dc3d7e613f30c8a634da2b661eca8946a394..468272edef96a4c1b267d7c41fbbcd41e5854d96 100644 (file)
@@ -23,6 +23,7 @@
     "instagram",
     "cors",
     "pwned",
-    "qr"
+    "qr",
+    "ip"
   ]
 }
This page took 0.039709 seconds and 4 git commands to generate.