Skip to content

Instantly share code, notes, and snippets.

@doroved
Last active January 27, 2026 16:24
Show Gist options
  • Select an option

  • Save doroved/f3370bc1e4fb0f8bc4e54cf2e2b00bd0 to your computer and use it in GitHub Desktop.

Select an option

Save doroved/f3370bc1e4fb0f8bc4e54cf2e2b00bd0 to your computer and use it in GitHub Desktop.
Proxying ntc.party through CF workers
export default {
async fetch(request) {
const url = new URL(request.url);
const workerHost = url.host;
const targetHost = 'ntc.party';
// 1. Подменяем URL запроса
url.hostname = targetHost;
url.protocol = 'https:';
// 2. Копируем заголовки, но подменяем Host и Origin, чтобы сервер ntc.party думал, что мы "свои"
const newHeaders = new Headers(request.headers);
newHeaders.set('Host', targetHost);
newHeaders.set('Origin', `https://${targetHost}`);
newHeaders.set('Referer', `https://${targetHost}/`);
// 3. Создаем новый запрос
const newRequest = new Request(url, {
method: request.method,
headers: newHeaders,
body: request.body,
redirect: 'manual' // Не идем по редиректам автоматически, чтобы обработать их
});
// 4. Получаем ответ от ntc.party
const response = await fetch(newRequest);
// 5. Обрабатываем заголовки ответа (исправляем редиректы и куки)
const newResponseHeaders = new Headers(response.headers);
// Если сервер хочет нас перенаправить (301/302), меняем адрес перенаправления на наш воркер
// if (newResponseHeaders.has('Location')) {
// const location = newResponseHeaders.get('Location');
// newResponseHeaders.set('Location', location.replace(targetHost, workerHost));
// }
// 6. Самое главное: обработка ТЕЛА ответа
const contentType = newResponseHeaders.get('content-type');
// Если это HTML или JSON или JavaScript — читаем текст и меняем домены
if (contentType && (contentType.includes('text/html') || contentType.includes('application/json') || contentType.includes('javascript'))) {
let bodyText = await response.text();
// Глобальная замена всех вхождений ntc.party на ваш домен
bodyText = bodyText.replaceAll(`https://${targetHost}`, `https://${workerHost}`);
return new Response(bodyText, {
status: response.status,
statusText: response.statusText,
headers: newResponseHeaders
});
}
// 7. Если это картинка, шрифт или бинарный файл — просто отдаем как есть (stream)
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: newResponseHeaders
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment