cloudflare-link-masker/worker.js

49 lines
1.6 KiB
JavaScript
Raw Normal View History

2023-08-07 00:24:59 +01:00
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
const url = new URL(request.url);
// Define the base hostname and path for the mappings
const baseHostname = 'YOURWORKER.YOURUSER.workers.dev';
const baseMappingPath = '/examplebase/';
const testFilesPath = '/testfiles/';
// Check if the path matches the desired format
// Add ifs as needed
if (url.hostname === baseHostname && (url.pathname.startsWith(baseMappingPath) || url.pathname.startsWith(testFilesPath))) {
const subpath = url.pathname.startsWith(baseMappingPath)
? url.pathname.substring(baseMappingPath.length) // Extract the subpath for mappings
: url.pathname.substring(testFilesPath.length); // Extract the subpath for test files
// Define the mapping of subpaths to redirect URLs
const baseTargetUrls = {
'x86_64.iso': 'https://example.com/x86_64.iso',
'i686.iso': 'https://example.com/i686.iso',
// Add more mappings here if needed
};
// Check if the subpath exists in the mapping
if (baseTargetUrls[subpath]) {
const targetUrl = baseTargetUrls[subpath];
return fetch(targetUrl, request);
}
// Check if the subpath exists in test files
const testFileUrls = {
'example': 'https://example.com'
// Add more test file mappings here if needed
};
if (testFileUrls[subpath]) {
const testFileUrl = testFileUrls[subpath];
return fetch(testFileUrl, request);
}
}
// Return a 404 response for other requests
return new Response('Not Found', { status: 404 });
}