kaixuan1115 / notes

笔记收录
6 stars 0 forks source link

CloudFlare Workers加速Heroku #24

Closed xiaokaixuan closed 4 years ago

xiaokaixuan commented 4 years ago

用CF Workers加速Heroku

addEventListener('fetch', event => {
const url = new URL(event.request.url);
url.hostname = 'appname.herokuapp.com';
const request = new Request(url, event.request);
event.respondWith(fetch(request));
});


addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});

async function handleRequest(request) { const url = new URL(request.url); url.hostname = 'appname.herokuapp.com'; return fetch(new Request(url, request)); }


> **注意:** 免费版每日限制10万次请求。
xiaokaixuan commented 3 years ago

ss-v2ray-plugin 多合一


'use strict';

addEventListener('fetch', event => { event.respondWith(handleRequest(event.request)) });

async function handleRequest(request) { const url = new URL(request.url); if (url.hostname.startsWith('gost')) { url.hostname = 'gost-mws.herokuapp.com'; return fetch(new Request(url, request)); } if (url.hostname.startsWith('vless')) { url.hostname = 'vless-wss.herokuapp.com'; return fetch(new Request(url, request)); } if (url.pathname == '/') { return rootRequest(request); } if (url.hostname.startsWith('ss-v2p')) { url.hostname = 'ss-v2p.herokuapp.com'; return fetch(new Request(url, request)); } url.hostname = 'go-back.tk'; return fetch(new Request(url, request)); }

async function rootRequest(request) { const url = new URL(request.url); const hostname = url.hostname; const host_ips = await resolveToIps(hostname); const json = { hostname, host_ips, plugin: 'v2ray-plugin', 'plugin-opts': /ws;tls;host=${hostname} }; return new Response(JSON.stringify(json, null, 2), { status: 200 }); }

async function resolveToIps(hostname) { const dns_google = https://dns.google/resolve?name=${hostname}&type=A; const json = await fetch(dns_google).then(res => res.json()); const list = []; if (json.Answer) { json.Answer.forEach(it => it.data && list.push(it.data)); } return list; }

xiaokaixuan commented 2 years ago

用CF Workers代理下载


// https://proxy.kaixuan.workers.dev/{download_url}

addEventListener('fetch', event => { event.passThroughOnException();

return event.respondWith(handleRequest(event)); })

async function handleRequest(event) { const request = event.request;

var urlString = request.url.substr(8); urlString = decodeURIComponent(urlString.substr(urlString.indexOf('/') + 1)); urlString = urlString.replace(/^https:\/+/,'https://').replace(/^http:\/+/, 'http://'); if (!/^https?:\/\//.test(urlString)) urlString = "https://" + urlString;

const newRequest = new Request(new URL(urlString), request); const response = await fetch(newRequest, { redirect: "follow" }); const status = response.status, statusText = response.statusText, body = response.body; const headers = new Headers(response.headers); headers.set("Access-Control-Allow-Origin", request.headers.get("Origin") || "*"); headers.set("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS"); headers.set("Access-Control-Allow-Headers", request.headers.get("Access-Control-Allow-Headers") || "Accept, Authorization, Cache-Control, Content-Type, DNT, If-Modified-Since, Keep-Alive, Origin, User-Agent, X-Requested-With, Token, x-access-token, Notion-Version");

return new Response(body, { status, statusText, headers }); }

xiaokaixuan commented 2 years ago

包子漫画 代理访问


addEventListener('fetch', event => {
event.passThroughOnException();

return event.respondWith(handleRequest(event)); })

const PROXY_MAP = { 'baozimh.go-back.cf': 'www.baozimh.com', 'webmota.go-back.cf': 'www.webmota.com' };

const DOMAIN_MAP = { 'www.baozimh.com': 'baozimh.go-back.cf', 'cn.baozimh.com': 'baozimh.go-back.cf', 'www.webmota.com': 'webmota.go-back.cf', 'cn.webmota.com': 'webmota.go-back.cf' };

function replaceHostnames(content) { for (const hostname in DOMAIN_MAP) { const HOST_NAME = DOMAIN_MAP[hostname]; content = content.replace(new RegExp(hostname, 'g'), HOST_NAME); } return content; }

async function handleRequest(event) { const request = event.request; const url = new URL(request.url);

const HOST_NAME = PROXY_MAP[url.hostname] || PROXY_MAP['baozimh.go-back.cf']; url.hostname = HOST_NAME;

const newRequest = new Request(url, request); const response = await fetch(newRequest, { redirect: "follow" }); const status = response.status, statusText = response.statusText; const headers = new Headers(response.headers); headers.set("Access-Control-Allow-Origin", request.headers.get("Origin") || "*"); headers.set("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS"); headers.set("Access-Control-Allow-Headers", request.headers.get("Access-Control-Allow-Headers") || "Accept, Authorization, Cache-Control, Content-Type, DNT, If-Modified-Since, Keep-Alive, Origin, User-Agent, X-Requested-With, Token, x-access-token, Notion-Version");

const originalBody = await response.text(); const body = replaceHostnames(originalBody);

return new Response(body, { status, statusText, headers }); }