diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..129cdf9 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 Jake Jarvis + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index 7ca2782..0526058 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,43 @@ -simpip.com -========= -A very simple IP utility that shows the user's IP address–and only the user's IP address–on a plaintext page using PHP. \ No newline at end of file +# 🌎 [simpip.com](https://simpip.com/) + +**⚡ Now powered purely by [Cloudflare Workers](https://www.cloudflare.com/products/cloudflare-workers/) — [try this code on the playground!](https://cloudflareworkers.com/#12bf2207fc352f52ebb27a041753c03d:https://tutorial.cloudflareworkers.com/)** The ancient PHP version is [archived here](https://github.com/jakejarvis/simpip/tree/php). + +A very, *very* "simple" web server that returns the visitor's IP address in plaintext...and **literally nothing** else. Perfect for CLI usage via `curl` or for automated tasks. + +This returns your IPv6 address by default, but to choose one or the other you can use [`curl`](https://curl.haxx.se/docs/manpage.html) flags: + +```bash +curl simpip.com # returns IPv6 *OR* IPv4 +curl -4 simpip.com # returns IPv4 +curl -6 simpip.com # returns IPv6, or fails to connect if network is incompatible +``` + + +## Example + +In [my terminal's dotfiles](https://github.com/jakejarvis/dotfiles), I have three aliases:`ip4` and `ip6` which are self-explanatory, and simply `ip` which returns **both** addresses *iff* your network supports IPv6 — otherwise, IPv6 sliently fails and only your IPv4 address is shown. + +```bash +alias ip4="curl -4 simpip.com --max-time 1 --proto-default https --silent" +alias ip6="curl -6 simpip.com --max-time 1 --proto-default https --silent" +alias ip="ip6; ip4" +``` + +Timeout is set to 1 second via `--max-time 1` (otherwise we will get stuck indefinitely trying to connect via IPv6 even if our network doesn't support it) and a secure connection preference is set using `--proto-default https`. Connection errors (particularly for IPv6) are silenced using `--silent`, so that the output of `ip` contains nothing but IP addresses, like so: + +```bash +jake@macbook:~$ ip4 +1.1.1.1 + +jake@macbook:~$ ip6 +2606:4700:4700::1111 + +jake@macbook:~$ ip +2606:4700:4700::1111 +1.1.1.1 +``` + + +## License + +This project is distributed under the [MIT license](LICENSE.md). diff --git a/index.js b/index.js new file mode 100644 index 0000000..23fb277 --- /dev/null +++ b/index.js @@ -0,0 +1,22 @@ +addEventListener('fetch', event => { + event.respondWith(handle(event.request)) +}) + +async function handle(request) { + return new Response(request.headers.get("cf-connecting-ip") + "\n", { + status: 200, + statusText: "OK", + headers: { + "Content-Type": "text/plain", + "Content-Security-Policy": "default-src 'none';", + "X-Content-Type-Options": "nosniff", + "X-XSS-Protection": "1; mode=block", + "X-Frame-Options": "SAMEORIGIN", + "Referrer-Policy": "no-referrer", + "Cache-Control": "max-age=0, no-cache, no-store, must-revalidate", + "Pragma": "no-cache", + "Expires": "Wed, 11 Jan 1984 05:00:00 GMT", + "X-Did-You-Know": "You can visit ipv4.simpip.com or ipv6.simpip.com to get either address!" + } + }); +} diff --git a/index.php b/index.php deleted file mode 100644 index c123d3e..0000000 --- a/index.php +++ /dev/null @@ -1,12 +0,0 @@ - \ No newline at end of file