Skip to content

Instantly share code, notes, and snippets.

@shagamemnon
shagamemnon / stick-session.js
Last active September 16, 2019 05:17
A Cloudflare Worker for session persistence
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
event.passThroughOnException()
})
const ORIGINS = [
'storage.googleapis.com',
'frank-io.herokuapp.com'
]
@shagamemnon
shagamemnon / getZones.sh
Last active January 17, 2020 10:26 — forked from cjus/jsonval.sh
Extract a JSON value from a BASH script
#!/bin/bash
function jsonval {
temp=`echo $json | sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed 's/\"//g' | grep -w $prop`
echo ${temp##*|}
}
json=`curl -s -X GET https://api.cloudflare.com/client/v4/zones?account.name=$ACCT_NAME&page=1&per_page=1000&order=status&direction=desc&match=all`
prop='profile_image_url'
picurl=`jsonval`
@shagamemnon
shagamemnon / cloudflare-enable-logs.sh
Created January 21, 2020 19:30
Enable log file retention for a list of Cloudflare (Enterprise) zones.
#!/bin/sh
# Enable log retention for a list of Cloudflare zones
AUTH_EMAIL="xxxx@example.com" # the account you use to
AUTH_KEY="xxxxxxxxxxxx" # https://dash.cloudflare.com/profile/api-tokens --> "Global API Key"
ZONE_NAMES="franktaylor.io cfiq.io valleynet.io" # space-separated list of zone names
for ZONE in $ZONE_NAMES
do

You can unzip POST request bodies in Workers using the pako library.

Demo

# Download a gzipped file to send as the body of a POST request
# The Worker running on this route will decompress the file and
# echo the human-readable response. Sweet!
wget https://storage.franktaylor.io/d06cef5527f329e519553f649b3a76e219f2c9d6/admin.php.gz
curl "POST" "https://workers-unzip.cflr.workers.dev/" --data-binary @admin.php.gz -H 'Content-Type: application/gzip'
addEventListener('fetch', event => {
event.respondWith(handleRequest(event))
})
async function timeout (ms) {
return new Promise(accept => {
setTimeout(() => {
accept('timeout')
}, ms)
})
@shagamemnon
shagamemnon / dig.md
Last active April 24, 2020 18:15 — forked from tuksik/dig_cs.sh
Frank's dig cheat sheet
# force recursion back to root nameservers (helpful for bypassing caches)
dig www.cloudflare.com A +trace

# get the address(es) for yahoo.com
dig yahoo.com A +noall +answer

# get a list of yahoo's mail servers
dig yahoo.com MX +noall +answer
@shagamemnon
shagamemnon / static_ip.sh
Last active October 5, 2020 02:17 — forked from RichardBronosky/static_ip.sh
Script to generate /etc/dhcpcd.conf for static IP on Raspbian Raspberry Pi
#!/bin/bash -e
sudo cp /etc/dhcpcd.conf /etc/dhcpcd.conf.backup
if [[ $# -ne 0 ]]; then
cat<<EOF
Usage:
$0
DEV=eth0 $0
<!-- <!DOCTYPE html> Required -->
<!DOCTYPE html>
<html lang="en">
<head>
<!-- <title> Required -->
<title>Error Page</title>
<!-- The HTML file must contain at least 100 characters. An easy way to do that is to add the universal normalize.css snippet -->
<style>
/*! normalize.css v8.0.1 | MIT License | github.com/necolas/normalize.css */html{line-height:1.15;-webkit-text-size-adjust:100%}body{margin:0}main{display:block}h1{font-size:2em;margin:.67em 0}hr{box-sizing:content-box;height:0;overflow:visible}pre{font-family:monospace,monospace;font-size:1em}a{background-color:transparent}abbr[title]{border-bottom:none;text-decoration:underline;text-decoration:underline dotted}b,strong{font-weight:bolder}code,kbd,samp{font-family:monospace,monospace;font-size:1em}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}img{border-style:none}button,input,optgroup,select,textarea{font-family:inherit;font-size:100%;line-height:1.15;
# Cache API Docs + Example
- https://developers.cloudflare.com/workers/about/using-cache/
- https://developers.cloudflare.com/workers/reference/apis/cache
```js
addEventListener('fetch', event => {
event.respondWith(handleRequest(event))
})
async function handleRequest(request) {