Skip to content

Instantly share code, notes, and snippets.

@TerenceLiu98
Created May 7, 2025 11:41
Show Gist options
  • Save TerenceLiu98/277ccbca588ebc0444c14ddaec14c3ec to your computer and use it in GitHub Desktop.
Save TerenceLiu98/277ccbca588ebc0444c14ddaec14c3ec to your computer and use it in GitHub Desktop.
worker-http-proxy.js
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