Created
January 11, 2022 00:16
-
-
Save miguelmota/638507e780e33ff419f2d79cc8aea0fd to your computer and use it in GitHub Desktop.
TypeScript Cloudflare worker proxy IPFS cloudflare-ipfs.com example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SETINGS ///////////////////////////////////// | |
const upstream = { | |
domain: 'cloudflare-ipfs.com', | |
protocol: 'https', | |
} | |
const dns = { | |
domain: 'cloudflare-dns.com', | |
protocol: 'https', | |
path: '/dns-query', | |
query: '?ct=application/dns-json&type=TXT&name=_dnslink.' | |
} | |
const blockedCountries = ['BY', 'CU', 'IR', 'IQ', 'CI', 'LR', 'KP', 'SD', 'SY', 'ZW'] | |
const headers = { | |
'X-Frame-Options': 'DENY', | |
'X-XSS-Protection': '1; mode=block', | |
'Referrer-Policy': 'no-referrer', | |
'X-Content-Type-Options': 'nosniff', | |
'Strict-Transport-Security': 'max-age=15780000; includeSubDomains; preload', | |
'Feature-Policy': "geolocation 'none'; midi 'none'; sync-xhr 'none'; microphone 'none'; camera 'none'; magnetometer 'none'; gyroscope 'none'; fullscreen 'none'; payment 'none'" | |
} | |
// PROXY /////////////////////////////////////// | |
async function getResponse (origRequest: Request) { | |
try { | |
const isBlocked = blockedCountries.includes(origRequest.cf!.country) | |
if (isBlocked) { | |
throw new Error('Unavailable For Legal Reasons') | |
} | |
const url = new URL(origRequest.url) | |
const dnsUrl = `${dns.protocol}://${dns.domain}${dns.path}${dns.query}${url.hostname}` | |
const data = await fetch(dnsUrl) | |
const json: any = await data.json() | |
const txtValue = json.Answer[0].data | |
const ipfsPath = txtValue.replace(/.*dnslink=(.*)"/, '$1/') | |
url.pathname = ipfsPath + url.pathname | |
url.hostname = upstream.domain | |
url.protocol = upstream.protocol | |
const request = new Request(url.href, { | |
body: origRequest.body, | |
method: origRequest.method, | |
headers: origRequest.headers, | |
}) | |
let response = await fetch(request); | |
if (response.status === 404) { | |
const cloneURL = new URL(url.href) | |
cloneURL.pathname = cloneURL.pathname.replace(/.*(\/ipfs\/\w+\/).*/, '$1') | |
const cloneRequest = new Request(cloneURL.href, { | |
body: request.body, | |
method: request.method, | |
headers: request.headers, | |
}) | |
response = await fetch(cloneRequest); | |
} | |
const responseHeaders = new Headers(response.headers) | |
for (const [key, value] of Object.entries(headers)) { | |
responseHeaders.set(key, value) | |
} | |
responseHeaders.delete('Set-Cookie') | |
return new Response(response.body, { | |
status: response.status, | |
statusText: response.statusText, | |
headers: responseHeaders | |
}) | |
} catch (err: any) { | |
return new Response(err.message, {status: 400}) | |
} | |
} | |
function handleRequest(event: any) { | |
const response = getResponse(event.request) | |
event.respondWith(response) | |
} | |
addEventListener('fetch', handleRequest) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment