Pend Proxy v1: isolated proxy browser on Cloudflare Workers — editable device + browser profiles, per-profile cookie jars
This commit is contained in:
commit
5dbf63b227
4 files changed
+328
No files matched your search
@@ -0,0 +1,271 @@
|
||||
// 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 = `<!DOCTYPE html>
|
||||
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Pend Proxy</title>
|
||||
<style>
|
||||
*{box-sizing:border-box} body{margin:0;font-family:system-ui,-apple-system,Segoe UI,Roboto,sans-serif;background:#0b1220;color:#e5e7eb}
|
||||
header{padding:12px 16px;background:#111827;border-bottom:1px solid #1f2937;display:flex;gap:10px;align-items:center;flex-wrap:wrap}
|
||||
header h1{font-size:18px;margin:0;color:#34d399}
|
||||
header h1 span{color:#e5e7eb;font-weight:400;font-size:13px}
|
||||
.bar{display:flex;gap:8px;padding:10px 16px;background:#111827;border-bottom:1px solid #1f2937;flex-wrap:wrap}
|
||||
.bar input[type=url]{flex:1;min-width:220px;padding:9px 12px;border-radius:8px;border:1px solid #374151;background:#0b1220;color:#fff;font-size:14px}
|
||||
select,input[type=text]{padding:9px 10px;border-radius:8px;border:1px solid #374151;background:#0b1220;color:#fff;font-size:14px}
|
||||
button{padding:9px 16px;border-radius:8px;border:0;background:#34d399;color:#06281c;font-weight:700;cursor:pointer}
|
||||
button.ghost{background:#1f2937;color:#e5e7eb}
|
||||
button.danger{background:#7f1d1d;color:#fee2e2}
|
||||
main{display:grid;grid-template-columns:300px 1fr;gap:0;height:calc(100vh - 118px)}
|
||||
aside{border-right:1px solid #1f2937;padding:12px;overflow-y:auto;background:#0f172a}
|
||||
aside h2{font-size:14px;margin:4px 0 10px;color:#34d399}
|
||||
.profile{border:1px solid #374151;border-radius:10px;padding:10px;margin-bottom:10px;cursor:pointer}
|
||||
.profile.active{border-color:#34d399;background:#12211b}
|
||||
.profile b{font-size:14px} .profile small{display:block;color:#9ca3af;margin-top:4px;word-break:break-all}
|
||||
.profile .row{display:flex;gap:6px;margin-top:8px}
|
||||
.profile .row button{padding:5px 9px;font-size:12px}
|
||||
form label{display:block;font-size:12px;color:#9ca3af;margin:10px 0 4px}
|
||||
form input,form select{width:100%}
|
||||
#uaPreview{background:#0b1220;border:1px solid #374151;border-radius:8px;padding:8px;font-size:11px;word-break:break-all;color:#a7f3d0;min-height:34px}
|
||||
#status{padding:8px 16px;font-size:13px;color:#9ca3af;border-bottom:1px solid #1f2937;background:#0f172a}
|
||||
#status b{color:#e5e7eb}
|
||||
section.viewer{display:flex;flex-direction:column;min-width:0}
|
||||
iframe{flex:1;width:100%;border:0;background:#fff}
|
||||
@media(max-width:760px){main{grid-template-columns:1fr}aside{border-right:0;border-bottom:1px solid #1f2937}}
|
||||
</style></head>
|
||||
<body>
|
||||
<header><h1>Pend Proxy <span>— isolated sessions, your device, your browser</span></h1></header>
|
||||
<div class="bar">
|
||||
<select id="profileSelect" title="Active profile / session"></select>
|
||||
<input type="url" id="urlInput" placeholder="https://example.com" value="https://example.com">
|
||||
<button id="goBtn">Go</button>
|
||||
<button class="ghost" id="clearCookiesBtn" title="Clear cookies for the active profile">Clear cookies</button>
|
||||
</div>
|
||||
<div id="status">Ready. Pick a profile, enter a URL, hit Go. Each profile is an isolated session with its own cookies.</div>
|
||||
<main>
|
||||
<aside>
|
||||
<h2>Profiles</h2>
|
||||
<div id="profileList"></div>
|
||||
<h2 id="formTitle">New profile</h2>
|
||||
<form id="profileForm">
|
||||
<label>Profile name<input type="text" id="fName" required placeholder="e.g. iPhone Safari"></label>
|
||||
<label>Device name<input type="text" id="fDevice" required placeholder="e.g. iPhone 15"></label>
|
||||
<label>Browser
|
||||
<select id="fBrowser">
|
||||
<option>Chrome</option><option>Firefox</option><option>Safari</option><option>Edge</option>
|
||||
</select>
|
||||
</label>
|
||||
<label>Browser version<input type="text" id="fVersion" placeholder="e.g. 131.0.0.0"></label>
|
||||
<label>Operating system
|
||||
<select id="fOS">
|
||||
<option value="win">Windows 10 / 11</option>
|
||||
<option value="mac">macOS 14</option>
|
||||
<option value="android">Android 14</option>
|
||||
<option value="ios">iOS 18</option>
|
||||
<option value="linux">Linux</option>
|
||||
</select>
|
||||
</label>
|
||||
<label>User-Agent (auto-built, editable)<input type="text" id="fUA"></label>
|
||||
<div id="uaPreview"></div>
|
||||
<div style="display:flex;gap:8px;margin-top:12px">
|
||||
<button type="submit" id="saveBtn">Save profile</button>
|
||||
<button type="button" class="ghost" id="resetFormBtn">New</button>
|
||||
</div>
|
||||
</form>
|
||||
</aside>
|
||||
<section class="viewer"><iframe id="viewer" sandbox="allow-scripts allow-same-origin allow-forms" title="page"></iframe></section>
|
||||
</main>
|
||||
<script>
|
||||
const $ = id => document.getElementById(id);
|
||||
const store = {
|
||||
load(){ try { return JSON.parse(localStorage.getItem('pend-proxy-profiles') || 'null') || null; } catch(e){ return null; } },
|
||||
save(p){ localStorage.setItem('pend-proxy-profiles', JSON.stringify(p)); },
|
||||
cookies(id){ try { return JSON.parse(localStorage.getItem('pend-proxy-cookies-' + id) || '{}'); } catch(e){ return {}; } },
|
||||
saveCookies(id, jar){ localStorage.setItem('pend-proxy-cookies-' + id, JSON.stringify(jar)); }
|
||||
};
|
||||
function defaultUA(b, v, os, dev){
|
||||
v = (v||'').trim();
|
||||
if (b === 'Chrome') {
|
||||
if (os === 'android') return 'Mozilla/5.0 (Linux; Android 14; ' + dev + ') AppleWebKit/537.36 (KHTML, like Gecko) Chrome/' + (v||'131.0.0.0') + ' Mobile Safari/537.36';
|
||||
if (os === 'ios') return 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/' + (v||'131.0.0.0') + ' Mobile/15E148 Safari/604.1';
|
||||
if (os === 'mac') return 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/' + (v||'131.0.0.0') + ' Safari/537.36';
|
||||
if (os === 'linux') return 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/' + (v||'131.0.0.0') + ' Safari/537.36';
|
||||
return 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/' + (v||'131.0.0.0') + ' Safari/537.36';
|
||||
}
|
||||
if (b === 'Firefox') {
|
||||
if (os === 'android') return 'Mozilla/5.0 (Android 14; Mobile; rv:' + (v||'133.0') + ') Gecko/' + (v||'133.0') + ' Firefox/' + (v||'133.0');
|
||||
if (os === 'ios') return 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) FxiOS/' + (v||'133.0') + ' Mobile/15E148 Safari/605.1.15';
|
||||
if (os === 'mac') return 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:' + (v||'133.0') + ') Gecko/20100101 Firefox/' + (v||'133.0');
|
||||
if (os === 'linux') return 'Mozilla/5.0 (X11; Linux x86_64; rv:' + (v||'133.0') + ') Gecko/20100101 Firefox/' + (v||'133.0');
|
||||
return 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:' + (v||'133.0') + ') Gecko/20100101 Firefox/' + (v||'133.0');
|
||||
}
|
||||
if (b === 'Safari') {
|
||||
if (os === 'ios') return 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/' + (v||'18.1') + ' Mobile/15E148 Safari/604.1';
|
||||
if (os === 'android') return 'Mozilla/5.0 (Linux; Android 14; ' + dev + ') AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/131.0.0.0 Mobile Safari/537.36';
|
||||
return 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/' + (v||'18.1') + ' Safari/605.1.15';
|
||||
}
|
||||
// Edge
|
||||
if (os === 'mac') return 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/' + (v||'131.0.0.0') + ' Safari/537.36 Edg/' + (v||'131.0.0.0');
|
||||
if (os === 'android') return 'Mozilla/5.0 (Linux; Android 14; ' + dev + ') AppleWebKit/537.36 (KHTML, like Gecko) Chrome/' + (v||'131.0.0.0') + ' Mobile Safari/537.36 EdgA/' + (v||'131.0.0.0');
|
||||
if (os === 'ios') return 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) EdgiOS/' + (v||'131.0.0.0') + ' Mobile/15E148 Safari/605.1.15';
|
||||
return 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/' + (v||'131.0.0.0') + ' Safari/537.36 Edg/' + (v||'131.0.0.0');
|
||||
}
|
||||
let profiles = store.load();
|
||||
if (!profiles) {
|
||||
profiles = [
|
||||
{id:'p1', name:'Windows Chrome', device:'Windows PC', browser:'Chrome', version:'131.0.0.0', os:'win', ua:''},
|
||||
{id:'p2', name:'iPhone Safari', device:'iPhone 15', browser:'Safari', version:'18.1', os:'ios', ua:''},
|
||||
{id:'p3', name:'Android Chrome', device:'SM-S921B', browser:'Chrome', version:'131.0.0.0', os:'android', ua:''},
|
||||
{id:'p4', name:'Mac Firefox', device:'MacBook Pro', browser:'Firefox', version:'133.0', os:'mac', ua:''}
|
||||
];
|
||||
profiles.forEach(p => p.ua = defaultUA(p.browser, p.version, p.os, p.device));
|
||||
store.save(profiles);
|
||||
}
|
||||
let activeId = localStorage.getItem('pend-proxy-active') || profiles[0].id;
|
||||
let editingId = null;
|
||||
function activeProfile(){ return profiles.find(p => p.id === activeId) || profiles[0]; }
|
||||
function renderProfiles(){
|
||||
const sel = $('profileSelect'); sel.innerHTML = '';
|
||||
profiles.forEach(p => { const o = document.createElement('option'); o.value = p.id; o.textContent = p.name + ' — ' + p.device + ' (' + p.browser + ')'; if (p.id === activeId) o.selected = true; sel.appendChild(o); });
|
||||
const list = $('profileList'); list.innerHTML = '';
|
||||
profiles.forEach(p => {
|
||||
const jar = store.cookies(p.id);
|
||||
const d = document.createElement('div');
|
||||
d.className = 'profile' + (p.id === activeId ? ' active' : '');
|
||||
d.innerHTML = '<b></b><small></small><small>🍪 ' + Object.keys(jar).length + ' cookies in this isolated session</small><div class="row"><button data-act="use">Use</button><button data-act="edit">Edit</button><button data-act="del" class="danger">Delete</button></div>';
|
||||
d.querySelector('b').textContent = p.name;
|
||||
d.querySelectorAll('small')[0].textContent = p.device + ' · ' + p.browser + ' ' + (p.version || '');
|
||||
d.querySelector('[data-act=use]').onclick = () => { activeId = p.id; localStorage.setItem('pend-proxy-active', activeId); renderProfiles(); };
|
||||
d.querySelector('[data-act=edit]').onclick = () => fillForm(p);
|
||||
d.querySelector('[data-act=del]').onclick = () => { if (profiles.length <= 1) { alert('Keep at least one profile.'); return; } profiles = profiles.filter(x => x.id !== p.id); localStorage.removeItem('pend-proxy-cookies-' + p.id); if (activeId === p.id) activeId = profiles[0].id; store.save(profiles); renderProfiles(); };
|
||||
list.appendChild(d);
|
||||
});
|
||||
}
|
||||
function fillForm(p){
|
||||
editingId = p.id; $('formTitle').textContent = 'Edit profile';
|
||||
$('fName').value = p.name; $('fDevice').value = p.device; $('fBrowser').value = p.browser;
|
||||
$('fVersion').value = p.version || ''; $('fOS').value = p.os; $('fUA').value = p.ua || '';
|
||||
$('uaPreview').textContent = p.ua || '';
|
||||
}
|
||||
function refreshUA(){
|
||||
const ua = $('fUA').value.trim() || defaultUA($('fBrowser').value, $('fVersion').value, $('fOS').value, $('fDevice').value);
|
||||
$('uaPreview').textContent = ua;
|
||||
}
|
||||
['fBrowser','fVersion','fOS','fDevice'].forEach(id => $(id).addEventListener('input', () => { if (!$('fUA').value.trim()) refreshUA(); }));
|
||||
$('fUA').addEventListener('input', refreshUA);
|
||||
$('resetFormBtn').onclick = () => { editingId = null; $('formTitle').textContent = 'New profile'; $('profileForm').reset(); $('fUA').value = ''; refreshUA(); };
|
||||
$('profileForm').addEventListener('submit', e => {
|
||||
e.preventDefault();
|
||||
const p = {
|
||||
id: editingId || ('p' + Date.now()),
|
||||
name: $('fName').value.trim(), device: $('fDevice').value.trim(),
|
||||
browser: $('fBrowser').value, version: $('fVersion').value.trim(), os: $('fOS').value,
|
||||
ua: $('fUA').value.trim() || defaultUA($('fBrowser').value, $('fVersion').value, $('fOS').value, $('fDevice').value)
|
||||
};
|
||||
if (editingId) profiles = profiles.map(x => x.id === editingId ? p : x);
|
||||
else profiles.push(p);
|
||||
store.save(profiles); editingId = null; $('formTitle').textContent = 'New profile'; $('profileForm').reset(); $('fUA').value = ''; renderProfiles(); refreshUA();
|
||||
});
|
||||
$('profileSelect').addEventListener('change', e => { activeId = e.target.value; localStorage.setItem('pend-proxy-active', activeId); renderProfiles(); });
|
||||
$('clearCookiesBtn').onclick = () => { store.saveCookies(activeId, {}); renderProfiles(); $('status').innerHTML = 'Cookies cleared for <b>' + activeProfile().name + '</b>.'; };
|
||||
function setStatus(html){ $('status').innerHTML = html; }
|
||||
async function go(url){
|
||||
const p = activeProfile();
|
||||
if (!url) url = $('urlInput').value.trim();
|
||||
if (!/^https?:\/\//i.test(url)) url = 'https://' + url;
|
||||
$('urlInput').value = url;
|
||||
setStatus('Loading <b>' + url.replace(/</g,'<') + '</b> as <b>' + p.device + ' · ' + p.browser + '</b> …');
|
||||
const jar = store.cookies(p.id);
|
||||
const t0 = Date.now();
|
||||
try {
|
||||
const r = await fetch('/api/fetch', { method:'POST', headers:{'content-type':'application/json'}, body: JSON.stringify({ url, ua: p.ua, cookies: jar }) });
|
||||
const data = await r.json();
|
||||
if (data.error) { setStatus('Error: <b>' + data.error.replace(/</g,'<') + '</b>'); return; }
|
||||
if (data.setCookies) { const j2 = store.cookies(p.id); data.setCookies.forEach(c => { const m = /^([^=]+)=([^;]*)/.exec(c); if (m) j2[m[1].trim()] = m[2]; }); store.saveCookies(p.id, j2); renderProfiles(); }
|
||||
setStatus('<b>' + data.status + '</b> · ' + data.finalUrl.replace(/</g,'<') + ' · ' + (data.contentType||'') + ' · ' + (data.bytes||0) + ' bytes · ' + (Date.now()-t0) + 'ms · as <b>' + p.device + ' · ' + p.browser + '</b>');
|
||||
const ct = (data.contentType || '').toLowerCase();
|
||||
if (ct.includes('text/html')) {
|
||||
let html = data.body || '';
|
||||
const inject = '<base href="' + data.finalUrl.replace(/"/g,'') + '"><script>document.addEventListener("click", function(e){ var a = e.target.closest("a"); if (a && a.href) { e.preventDefault(); parent.postMessage({type:"pend-proxy-nav", url:a.href}, "*"); } }, true);<\/script>';
|
||||
if (/<head[^>]*>/i.test(html)) html = html.replace(/<head[^>]*>/i, m => m + inject);
|
||||
else html = inject + html;
|
||||
$('viewer').srcdoc = html;
|
||||
} else {
|
||||
$('viewer').srcdoc = '<pre style="white-space:pre-wrap;word-break:break-all">' + (data.body || '').replace(/</g,'<') + '</pre>';
|
||||
}
|
||||
} catch (e) { setStatus('Error: <b>' + String(e).replace(/</g,'<') + '</b>'); }
|
||||
}
|
||||
$('goBtn').onclick = () => go();
|
||||
$('urlInput').addEventListener('keydown', e => { if (e.key === 'Enter') go(); });
|
||||
window.addEventListener('message', e => { if (e.data && e.data.type === 'pend-proxy-nav') go(e.data.url); });
|
||||
renderProfiles(); refreshUA();
|
||||
</script>
|
||||
</body></html>`;
|
||||
|
||||
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 });
|
||||
},
|
||||
};
|
||||
Reference in new issue
Block a user