Fixed linting/prettier errors

This commit is contained in:
Jonathan Barrow
2023-01-08 09:08:49 -05:00
parent ec4539f667
commit 13a4d0c8cc

View File

@@ -1,154 +1,172 @@
const corsHeaders = { const corsHeaders = {
'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,HEAD,POST,OPTIONS', 'Access-Control-Allow-Methods': 'GET,HEAD,POST,OPTIONS',
'Access-Control-Max-Age': '86400', 'Access-Control-Max-Age': '86400',
}; };
async function handleRequest(oRequest, destination, iteration = 0) { async function handleRequest(oRequest, destination, iteration = 0) {
console.log(`PROXYING ${destination}${iteration ? ' ON ITERATION ' + iteration : ''}`); console.log(
`PROXYING ${destination}${iteration ? ' ON ITERATION ' + iteration : ''}`,
);
// Create a new mutable request object for the destination // Create a new mutable request object for the destination
const request = new Request(destination, oRequest); const request = new Request(destination, oRequest);
request.headers.set('Origin', new URL(destination).origin); request.headers.set('Origin', new URL(destination).origin);
// TODO - Make cookie handling better. PHPSESSID overwrites all other cookie related headers // TODO - Make cookie handling better. PHPSESSID overwrites all other cookie related headers
// Add custom X headers from client // Add custom X headers from client
// These headers are usually forbidden to be set by fetch // These headers are usually forbidden to be set by fetch
if (oRequest.headers.has('X-Cookie')) { if (oRequest.headers.has('X-Cookie')) {
request.headers.set('Cookie', oRequest.headers.get('X-Cookie')); request.headers.set('Cookie', oRequest.headers.get('X-Cookie'));
request.headers.delete('X-Cookie'); request.headers.delete('X-Cookie');
} }
if (request.headers.has('X-Referer')) { if (request.headers.has('X-Referer')) {
request.headers.set('Referer', request.headers.get('X-Referer')); request.headers.set('Referer', request.headers.get('X-Referer'));
request.headers.delete('X-Referer'); request.headers.delete('X-Referer');
} }
if (request.headers.has('X-Origin')) { if (request.headers.has('X-Origin')) {
request.headers.set('Origin', request.headers.get('X-Origin')); request.headers.set('Origin', request.headers.get('X-Origin'));
request.headers.delete('X-Origin'); request.headers.delete('X-Origin');
} }
// Set PHPSESSID cookie // Set PHPSESSID cookie
if (request.headers.get('PHPSESSID')) { if (request.headers.get('PHPSESSID')) {
request.headers.set('Cookie', `PHPSESSID=${request.headers.get('PHPSESSID')}`); request.headers.set(
} 'Cookie',
`PHPSESSID=${request.headers.get('PHPSESSID')}`,
);
}
// Set User Agent, if not exists // Set User Agent, if not exists
const useragent = request.headers.get('User-Agent'); const useragent = request.headers.get('User-Agent');
if (!useragent) { if (!useragent) {
request.headers.set('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0'); request.headers.set(
} 'User-Agent',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0',
);
}
// Fetch the new resource // Fetch the new resource
const oResponse = await fetch(request); const oResponse = await fetch(request);
// If the server returned a redirect, follow it // If the server returned a redirect, follow it
if ( if (
(oResponse.status === 302 || oResponse.status === 301) && (oResponse.status === 302 || oResponse.status === 301) &&
oResponse.headers.get('location') oResponse.headers.get('location')
) { ) {
// Server tried to redirect too many times // Server tried to redirect too many times
if (iteration > 5) { if (iteration > 5) {
return event.respondWith( return event.respondWith(
new Response('418 Too many redirects', { new Response('418 Too many redirects', {
status: 418 status: 418,
}), }),
); );
} }
// Handle and return the request for the redirected destination // Handle and return the request for the redirected destination
return await handleRequest(request, oResponse.headers.get('location'), iteration + 1); return await handleRequest(
} request,
oResponse.headers.get('location'),
iteration + 1,
);
}
// Create mutable response using the original response as init // Create mutable response using the original response as init
const response = new Response(oResponse.body, oResponse); const response = new Response(oResponse.body, oResponse);
// Set CORS headers // Set CORS headers
response.headers.set('Access-Control-Allow-Origin', '*'); response.headers.set('Access-Control-Allow-Origin', '*');
response.headers.set('Access-Control-Expose-Headers', '*'); response.headers.set('Access-Control-Expose-Headers', '*');
const cookiesToSet = response.headers.get('Set-Cookie'); const cookiesToSet = response.headers.get('Set-Cookie');
// Transfer Set-Cookie to X-Set-Cookie // Transfer Set-Cookie to X-Set-Cookie
// Normally the Set-Cookie header is not accessible to fetch clients // Normally the Set-Cookie header is not accessible to fetch clients
if (cookiesToSet) { if (cookiesToSet) {
response.headers.set('X-Set-Cookie', response.headers.get('Set-Cookie')); response.headers.set('X-Set-Cookie', response.headers.get('Set-Cookie'));
} }
// Set PHPSESSID cookie // Set PHPSESSID cookie
if (cookiesToSet && cookiesToSet.includes('PHPSESSID') && cookiesToSet.includes(';')) { if (
let phpsessid = cookies.slice(cookies.search('PHPSESSID') + 10); cookiesToSet &&
phpsessid = phpsessid.slice(0, phpsessid.search(';')); cookiesToSet.includes('PHPSESSID') &&
cookiesToSet.includes(';')
) {
let phpsessid = cookies.slice(cookies.search('PHPSESSID') + 10);
phpsessid = phpsessid.slice(0, phpsessid.search(';'));
response.headers.set('PHPSESSID', phpsessid); response.headers.set('PHPSESSID', phpsessid);
} }
// Append to/Add Vary header so browser will cache response correctly // Append to/Add Vary header so browser will cache response correctly
response.headers.append('Vary', 'Origin'); response.headers.append('Vary', 'Origin');
return response; return response;
} }
function handleOptions(request) { function handleOptions(request) {
// Make sure the necessary headers are present // Make sure the necessary headers are present
// for this to be a valid pre-flight request // for this to be a valid pre-flight request
const headers = request.headers; const headers = request.headers;
let response = new Response(null, { let response = new Response(null, {
headers: { headers: {
Allow: 'GET, HEAD, POST, OPTIONS' Allow: 'GET, HEAD, POST, OPTIONS',
} },
}); });
if ( if (
headers.get('Origin') !== null && headers.get('Origin') !== null &&
headers.get('Access-Control-Request-Method') !== null && headers.get('Access-Control-Request-Method') !== null &&
headers.get('Access-Control-Request-Headers') !== null headers.get('Access-Control-Request-Headers') !== null
) { ) {
response = new Response(null, { response = new Response(null, {
headers: { headers: {
...corsHeaders, ...corsHeaders,
// Allow all future content Request headers to go back to browser // Allow all future content Request headers to go back to browser
// such as Authorization (Bearer) or X-Client-Name-Version // such as Authorization (Bearer) or X-Client-Name-Version
'Access-Control-Allow-Headers': request.headers.get('Access-Control-Request-Headers') 'Access-Control-Allow-Headers': request.headers.get(
} 'Access-Control-Request-Headers',
}); ),
} },
});
}
return response; return response;
} }
addEventListener('fetch', (event) => { addEventListener('fetch', (event) => {
const request = event.request; const request = event.request;
const url = new URL(request.url); const url = new URL(request.url);
const destination = url.searchParams.get('destination'); const destination = url.searchParams.get('destination');
console.log(`HTTP ${request.method} - ${request.url}`); console.log(`HTTP ${request.method} - ${request.url}`);
let response = new Response('404 Not Found', { let response = new Response('404 Not Found', {
status: 404 status: 404,
}); });
if (request.method === 'OPTIONS') { if (request.method === 'OPTIONS') {
// Handle CORS preflight requests // Handle CORS preflight requests
response = handleOptions(request); response = handleOptions(request);
} else if (!destination) { } else if (!destination) {
response = new Response('200 OK', { response = new Response('200 OK', {
status: 200, status: 200,
headers: { headers: {
Allow: 'GET, HEAD, POST, OPTIONS', Allow: 'GET, HEAD, POST, OPTIONS',
'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Origin': '*',
} },
}); });
} else if ( } else if (
request.method === 'GET' || request.method === 'GET' ||
request.method === 'HEAD' || request.method === 'HEAD' ||
request.method === 'POST' request.method === 'POST'
) { ) {
// Handle request // Handle request
response = handleRequest(request, destination); response = handleRequest(request, destination);
} }
event.respondWith(response); event.respondWith(response);
}); });