Skip to content

Instantly share code, notes, and snippets.

@vdbelt
vdbelt / dump.sh
Created Jun 2, 2020
Mysql dump with ionice
View dump.sh
ionice -c2 nice -n19 mysqldump --opt --single-transaction --quick --hex-blob --events --force --user=$MYSQLUSER -p $db | ionice -c2 nice -n19 gzip > sql_dump.sql.gz
@vdbelt
vdbelt / cloudflare-worker-device-header-for-origin.js
Last active Oct 9, 2018
Send info about device back to origin
View cloudflare-worker-device-header-for-origin.js
addEventListener('fetch', event => {
event.respondWith(fetchAndApply(event.request))
})
function isMobile(userAgent) {
return userAgent.match(/iPhone|Android|webOS/i) ? 'Yes' : 'No';
}
async function fetchAndApply(request) {
let isMobileResult = isMobile(request.headers.get('user-agent'))
@vdbelt
vdbelt / cloudflare-ignore-query-string.js
Created Oct 3, 2018
Ignore query string for optimal caching
View cloudflare-ignore-query-string.js
addEventListener('fetch', event => {
event.respondWith(ignoreQueryString(event.request))
})
async function ignoreQueryString(request) {
let url = new URL(request.url)
url.search = ''
let modifiedRequest = new Request(url, request)
@vdbelt
vdbelt / cloudflare-hot-link-protection-with-whitelist.js
Last active Aug 30, 2020
Cloudflare service worker hot link protection with whitelist
View cloudflare-hot-link-protection-with-whitelist.js
@vdbelt
vdbelt / cloudflare-block-countries-worker.js
Created Aug 24, 2018
Blocks certain countries from accessing your page, while giving the flexibility to whitelist certain paths.
View cloudflare-block-countries-worker.js
addEventListener('fetch', event => {
event.respondWith(byPassOrBlock(event.request))
})
async function byPassOrBlock(request) {
let url = new URL(request.url)
let blockedCountries = ['XX']
let whitelistedPaths = ['/about*']
@vdbelt
vdbelt / cloudflare-purge-cache-service-worker.js
Created Aug 21, 2018
A CloudFlare service worker that proxies purge cache requests. Example: https://example.com/__purge_cache?zone=XX
View cloudflare-purge-cache-service-worker.js
addEventListener('fetch', event => {
event.respondWith(purgeCache(event.request))
})
async function purgeCache(request) {
const url = new URL(request.url)