cloudflare-link-masker/worker.js

87 lines
2.8 KiB
JavaScript

addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
const url = new URL(request.url);
// Define the base hostname and paths for the mappings
const baseHostname = 'YOURWORKER.YOURUSER.workers.dev';
const testFilesPath = '/testfiles/';
const v10MappingPath = '/v1.0/';
const v11MappingPath = '/v1.1/'; // New mapping path
const v12MappingPath = '/v1.2/'; // New mapping path
const v13MappingPath = '/v1.3/'; // New mapping path
// Check if the path matches any of the desired formats
if (url.hostname === baseHostname && (url.pathname.startsWith(v10MappingPath) || url.pathname.startsWith(testFilesPath) || url.pathname.startsWith(v11MappingPath) || url.pathname.startsWith(v12MappingPath) || url.pathname.startsWith(v13MappingPath))) {
let subpath = '';
if (url.pathname.startsWith(v10MappingPath)) {
subpath = url.pathname.substring(v10MappingPath.length);
} else if (url.pathname.startsWith(testFilesPath)) {
subpath = url.pathname.substring(testFilesPath.length);
} else if (url.pathname.startsWith(v11MappingPath)) {
subpath = url.pathname.substring(v11MappingPath.length);
} else if (url.pathname.startsWith(v12MappingPath)) {
subpath = url.pathname.substring(v12MappingPath.length);
} else if (url.pathname.startsWith(v13MappingPath)) {
subpath = url.pathname.substring(v13MappingPath.length);
}
// Define the mapping of subpaths to redirect URLs
const v10TargetUrls = {
'v1.0.zip': 'https://foo.bar/v1.0.zip',
'v1.0-i686.zip': 'https://foo.bar/v1.0.i686.zip',
};
const v11TargetUrls = {
'v1.1.zip': 'https://foo.bar/v1.1.zip',
'v1.1-i686.zip': 'https://foo.bar/v1.1.i686.zip',
};
const v12TargetUrls = {
'v1.2.zip': 'https://foo.bar/v1.2.zip',
'v1.2-i686.zip': 'https://foo.bar/v1.2.i686.zip',
};
const v13TargetUrls = {
'v1.3.zip': 'https://foo.bar/v1.3.zip',
'v1.3-i686.zip': 'https://foo.bar/v1.3.i686.zip',
};
if (v10TargetUrls[subpath]) {
const targetUrl = v10TargetUrls[subpath];
return fetch(targetUrl, request);
}
if (v11TargetUrls[subpath]) {
const targetUrl = v11TargetUrls[subpath];
return fetch(targetUrl, request);
}
if (v12TargetUrls[subpath]) {
const targetUrl = v12TargetUrls[subpath];
return fetch(targetUrl, request);
}
if (v13TargetUrls[subpath]) {
const targetUrl = v13TargetUrls[subpath];
return fetch(targetUrl, request);
}
const testFileUrls = {
'example.png': 'https://foo.bar/example.png',
};
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 });
}