Created
May 7, 2025 11:41
-
-
Save TerenceLiu98/277ccbca588ebc0444c14ddaec14c3ec to your computer and use it in GitHub Desktop.
worker-http-proxy.js
This file contains hidden or 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
const PASSWORD = 'helloworld'; | |
export default { | |
async fetch(request) { | |
const { searchParams } = new URL(request.url); | |
const target = searchParams.get('url'); | |
const key = searchParams.get('psw'); | |
if (PASSWORD && key !== PASSWORD) { | |
return new Response('Unauthorized', { status: 401 }); | |
} | |
if (!target || !/^https?:\/\//.test(target)) { | |
return new Response('Invalid target URL', { status: 400 }); | |
} | |
try { | |
const resp = await fetch(target, { | |
method: 'GET', | |
headers: { | |
'User-Agent': request.headers.get('User-Agent') || 'Mozilla/5.0', | |
}, | |
}); | |
const headers = new Headers(resp.headers); | |
headers.set('access-control-allow-origin', '*'); // CORS | |
return new Response(resp.body, { | |
status: resp.status, | |
headers | |
}); | |
} catch (err) { | |
return new Response('Fetch error: ' + err.message, { status: 502 }); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment