// Pend Proxy — isolated proxy browser on Cloudflare Workers.
// Each profile = its own device + browser (User-Agent) + its own cookie jar.
// The browser keeps cookie jars in localStorage; the Worker only fetches.
const UI_HTML = `
Pend Proxy
Pend Proxy — isolated sessions, your device, your browser
Ready. Pick a profile, enter a URL, hit Go. Each profile is an isolated session with its own cookies.
`;
function isBlockedHost(hostname) {
const h = hostname.toLowerCase();
if (h === 'localhost' || h.endsWith('.localhost')) return true;
if (/^(127\.|10\.|192\.168\.|172\.(1[6-9]|2\d|3[01])\.|0\.0\.0\.0|::1)/.test(h)) return true;
return false;
}
async function handleFetch(request) {
let body;
try { body = await request.json(); } catch { return json({ error: 'Invalid JSON body' }, 400); }
const url = (body.url || '').trim();
const ua = (body.ua || '').trim().slice(0, 512);
const cookies = body.cookies && typeof body.cookies === 'object' ? body.cookies : {};
if (!/^https?:\/\//i.test(url)) return json({ error: 'Only http(s) URLs are allowed' }, 400);
let parsed;
try { parsed = new URL(url); } catch { return json({ error: 'Invalid URL' }, 400); }
if (isBlockedHost(parsed.hostname)) return json({ error: 'Blocked: private/local hosts are not allowed' }, 403);
const headers = new Headers();
if (ua) headers.set('User-Agent', ua);
headers.set('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8');
headers.set('Accept-Language', 'en-US,en;q=0.9');
const cookieHeader = Object.entries(cookies).map(([k, v]) => k + '=' + v).join('; ');
if (cookieHeader) headers.set('Cookie', cookieHeader);
let res;
try {
res = await fetch(url, { headers, redirect: 'follow' });
} catch (e) {
return json({ error: 'Fetch failed: ' + (e && e.message ? e.message : String(e)) }, 502);
}
const contentType = res.headers.get('content-type') || '';
const setCookies = [];
if (typeof res.headers.getSetCookie === 'function') {
setCookies.push(...res.headers.getSetCookie());
} else {
const sc = res.headers.get('set-cookie');
if (sc) setCookies.push(sc);
}
const text = await res.text();
return json({
status: res.status,
finalUrl: res.url,
contentType,
bytes: text.length,
setCookies,
body: text.slice(0, 2_000_000),
});
}
function json(obj, status = 200) {
return new Response(JSON.stringify(obj), {
status,
headers: { 'content-type': 'application/json; charset=utf-8' },
});
}
export default {
async fetch(request) {
const url = new URL(request.url);
if (request.method === 'POST' && url.pathname === '/api/fetch') return handleFetch(request);
if (request.method === 'GET' && url.pathname === '/') {
return new Response(UI_HTML, { headers: { 'content-type': 'text/html; charset=utf-8' } });
}
return new Response('Not found', { status: 404 });
},
};