├── .gitignore ├── LICENSE_MIT ├── README.md ├── index.js ├── package.json └── wrangler.toml /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /dist 3 | **/*.rs.bk 4 | Cargo.lock 5 | bin/ 6 | pkg/ 7 | wasm-pack.log 8 | worker/ 9 | node_modules/ 10 | .cargo-ok 11 | -------------------------------------------------------------------------------- /LICENSE_MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2018 Ashley Williams 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Cloudflare worker to work as a web proxy 2 | 3 | > wrangler publish 4 | 5 | > curl https://proxy.[your-subdomain].workers.dev/__url=https://example.com 6 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Cloudflare-worker-proxy 3 | * 4 | */ 5 | 6 | const cookiename = '__cfmainstream' 7 | const html = `
` + 8 | ` ` + 9 | `
` 10 | 11 | var request, url, mainstream 12 | 13 | 14 | function getCookie(cookies, name) { 15 | let match, re = /(.*?)=(.*?)(?:\s*;\s*|$)/g 16 | while (match = re.exec(cookies)) 17 | if(match[1] == name) 18 | return match[2] 19 | } 20 | 21 | 22 | async function getResponse() { 23 | let opts = await getOpts() 24 | let response = await fetch(mainstream.origin + url.pathname, opts) 25 | 26 | response = new Response(response.body, response) 27 | if(url.searchParams.get('__url')) 28 | response.headers.append('Set-Cookie', 29 | `${cookiename}=${mainstream.origin}; secure; httpOnly`) 30 | return response 31 | } 32 | 33 | 34 | async function getOpts() { 35 | let headers = {} 36 | let body = !['GET', 'HEAD'].includes(request.method) ? 37 | await request.text() : null 38 | 39 | for(const header of request.headers.entries()) { 40 | let [name, value] = header 41 | 42 | if(value.indexOf(url.hostname) != -1) 43 | value = value.replaceAll(url.hostname, mainstream.hostname) 44 | headers[name] = value 45 | } 46 | return {method: request.method, headers: headers, body: body} 47 | } 48 | 49 | 50 | addEventListener("fetch", event => { 51 | request = event.request 52 | url = new URL(request.url) 53 | mainstream = url.searchParams.get('__url') 54 | 55 | if(!mainstream) 56 | mainstream = getCookie(request.headers.get("Cookie"), cookiename) 57 | 58 | if(!mainstream || !/^https?:\/\//.test(mainstream)) 59 | return event.respondWith(new Response(html, 60 | {headers: {'Content-Type': 'text/html'}})) 61 | 62 | mainstream = new URL(mainstream) 63 | return event.respondWith(getResponse()) 64 | }) 65 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cloudflare-worker-proxy", 3 | "version": "1.0.0", 4 | "description": "Cloudflare worker to work as a proxy", 5 | "main": "index.js", 6 | "scripts": {}, 7 | "author": "anio ", 8 | "license": "MIT" 9 | } 10 | -------------------------------------------------------------------------------- /wrangler.toml: -------------------------------------------------------------------------------- 1 | name = "proxy" 2 | type = "javascript" 3 | account_id = "" 4 | workers_dev = true 5 | route = "" 6 | zone_id = "" 7 | --------------------------------------------------------------------------------