mirror of
https://github.com/movie-web/simple-proxy.git
synced 2025-09-13 16:43:26 +00:00
fix linter + make proxy work + remove temp files + fix typescript types
This commit is contained in:
22
src/routes/index.ts
Normal file
22
src/routes/index.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { getProxyHeaders, getAfterResponseHeaders } from '../utils/headers';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
// handle cors, if applicable
|
||||
if (isPreflightRequest(event)) return handleCors(event, {});
|
||||
|
||||
// parse destination URL
|
||||
const destination = getQuery<{ destination?: string }>(event).destination;
|
||||
if (!destination) throw new Error('invalid destination');
|
||||
|
||||
// proxy
|
||||
await proxyRequest(event, destination, {
|
||||
fetchOptions: {
|
||||
redirect: 'follow',
|
||||
headers: getProxyHeaders(event.headers),
|
||||
},
|
||||
onResponse(outputEvent, response) {
|
||||
const headers = getAfterResponseHeaders(response.headers, response.url);
|
||||
appendResponseHeaders(outputEvent, headers);
|
||||
},
|
||||
});
|
||||
});
|
46
src/utils/headers.ts
Normal file
46
src/utils/headers.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
function copyHeader(
|
||||
headers: Headers,
|
||||
outputHeaders: Headers,
|
||||
inputKey: string,
|
||||
outputKey: string,
|
||||
) {
|
||||
if (headers.has(inputKey))
|
||||
outputHeaders.set(outputKey, headers.get(inputKey) ?? '');
|
||||
}
|
||||
|
||||
export function getProxyHeaders(headers: Headers): Headers {
|
||||
const output = new Headers();
|
||||
|
||||
const headerMap: Record<string, string> = {
|
||||
'X-Cookie': 'Cookie',
|
||||
'X-Referer': 'Referer',
|
||||
'X-Origin': 'Origin',
|
||||
};
|
||||
Object.entries(headerMap).forEach((entry) => {
|
||||
copyHeader(headers, output, entry[0], entry[1]);
|
||||
});
|
||||
|
||||
output.set(
|
||||
'User-Agent',
|
||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0',
|
||||
);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
export function getAfterResponseHeaders(
|
||||
headers: Headers,
|
||||
finalUrl: string,
|
||||
): Record<string, string> {
|
||||
const output: Record<string, string> = {};
|
||||
|
||||
if (headers.has('Set-Cookie'))
|
||||
output['X-Set-Cookie'] = headers.get('Set-Cookie') ?? '';
|
||||
|
||||
return {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Expose-Headers': '*',
|
||||
Vary: 'Origin',
|
||||
'X-Final-Destination': finalUrl,
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user