Dashboard VPN: per-browser VPN toggle with country picker

- manager/vpn-pool.js: free proxy pool from ProxyScrape JSON (no key),
  live-checked via curl, cached 30 min, grouped by country
- server.js: GET /api/vpn/pool, POST /api/vpn/refresh,
  POST /api/browsers/:id/vpn; vpn state stored in meta.json
- generate.js: 5th arg vpnJson, stored in meta.json
- index.html: VPN section in Settings (toggle + country + refresh),
  VPN badge on cells, manual proxy disabled while VPN is on
- README: VPN section + file list
Verified live on 49.128.189.254: 24 working proxies in 11 countries,
b1 VPN on (PH 180.232.127.250) showed the proxy IP, off restored direct
This commit is contained in:
maspend committed 2026-12-02 14:38:03 +07:00
1 parent df4ae5a17a
commit 26427401db
5 files changed
+426 -13

No files matched your search

+96 -8
View File
@@ -19,6 +19,7 @@ const { promisify } = require('util');
const execFileAsync = promisify(execFile);
const app = express();
const vpnPool = require('./vpn-pool');
const PORT = Number(process.env.PORT || 18080);
const HOST = process.env.HOST || '127.0.0.1';
@@ -105,6 +106,7 @@ function loadBrowser(id) {
uaKey: uaKey,
uaLabel: preset.label || uaKey,
proxy: meta.proxy || { type: 'direct' },
vpn: meta.vpn && typeof meta.vpn.on === 'boolean' ? meta.vpn : { on: false },
httpPort: base.httpPort,
udpStart: base.udpStart,
udpEnd: base.udpEnd,
@@ -336,8 +338,21 @@ app.post('/api/browsers/:id/wipe', async (req, res) => {
}
});
// Regenerate one browser's chromium.conf + meta.json and restart it.
// vpn is the dashboard VPN state stored in meta.json ({on:false} for a
// manual/direct setup; the /vpn endpoint passes the live VPN details).
async function applyBrowserConfig(id, uaKey, proxy, vpn) {
await execFileAsync(
process.execPath,
[GENERATE_SCRIPT, id, uaKey, JSON.stringify(proxy), JSON.stringify(vpn || { on: false })],
{ env: Object.assign({}, process.env, { FARM_ROOT: FARM_ROOT }) }
);
await dockerAction('restart', id);
}
// Change one browser's identity (UA preset + proxy), then restart it so the
// new Chromium flags take effect.
// new Chromium flags take effect. A manual proxy clears the dashboard VPN
// state (the proxy is now manual, not the VPN one).
app.post('/api/browsers/:id/config', async (req, res) => {
const id = req.params.id;
if (!validateId(id)) {
@@ -353,13 +368,7 @@ app.post('/api/browsers/:id/config', async (req, res) => {
return res.status(400).json({ ok: false, error: checked.error });
}
try {
// Regenerate chromium.conf + meta.json with the shared generator.
await execFileAsync(
process.execPath,
[GENERATE_SCRIPT, id, body.ua, JSON.stringify(checked.proxy)],
{ env: Object.assign({}, process.env, { FARM_ROOT: FARM_ROOT }) }
);
await dockerAction('restart', id);
await applyBrowserConfig(id, body.ua, checked.proxy, { on: false });
const info = loadBrowser(id);
const st = await dockerStatus(id);
res.json({
@@ -369,6 +378,7 @@ app.post('/api/browsers/:id/config', async (req, res) => {
uaKey: info.uaKey,
uaLabel: info.uaLabel,
proxy: info.proxy,
vpn: info.vpn,
status: st.status,
});
} catch (e) {
@@ -376,6 +386,79 @@ app.post('/api/browsers/:id/config', async (req, res) => {
}
});
// ---- Dashboard VPN ----
// The VPN toggle routes one browser through a free proxy server from the
// pool (another country), switched from the dashboard instead of the
// Browsec extension inside the browser. The proxy becomes the browser's
// --proxy-server; the vpn field in meta.json records that it came from
// the dashboard VPN, so the grid can show the badge.
app.get('/api/vpn/pool', (req, res) => {
const pool = vpnPool.getPool();
res.json({
ok: true,
updatedAt: pool.updatedAt,
refreshing: pool.refreshing,
total: pool.proxies.length,
countries: vpnPool.countries(),
});
});
app.post('/api/vpn/refresh', (req, res) => {
vpnPool.refreshPool().catch(() => {});
const pool = vpnPool.getPool();
res.json({ ok: true, refreshing: true, total: pool.proxies.length });
});
app.post('/api/browsers/:id/vpn', async (req, res) => {
const id = req.params.id;
if (!validateId(id)) {
return res.status(400).json({ ok: false, error: 'invalid browser id' });
}
const body = req.body || {};
const cfg = loadConfig();
const info = loadBrowser(id);
if (!info) return res.status(404).json({ ok: false, error: 'browser not found' });
const uaKey = body.ua && (cfg.uaPresets || {})[body.ua] ? body.ua : info.uaKey;
try {
if (body.on) {
let pool = vpnPool.getPool();
if (!pool.proxies.length) {
await vpnPool.refreshPool();
pool = vpnPool.getPool();
}
const picked = vpnPool.pickProxy(typeof body.country === 'string' ? body.country : '');
if (!picked) {
return res.status(503).json({
ok: false,
error: 'pool VPN belum siap atau tidak ada proxy untuk negara itu — refresh pool lalu ulangi',
});
}
const vpn = {
on: true,
country: picked.country,
countryCode: picked.countryCode,
type: picked.type,
host: picked.host,
port: picked.port,
};
await applyBrowserConfig(
id,
uaKey,
{ type: picked.type, host: picked.host, port: picked.port, user: '', pass: '' },
vpn
);
const st = await dockerStatus(id);
return res.json({ ok: true, id: id, vpn: vpn, status: st.status });
}
// VPN off: back to a direct connection.
await applyBrowserConfig(id, uaKey, { type: 'direct' }, { on: false });
const st = await dockerStatus(id);
return res.json({ ok: true, id: id, vpn: { on: false }, status: st.status });
} catch (e) {
res.status(500).json({ ok: false, error: String((e && e.message) || e) });
}
});
// Generic start/stop/restart. Defined AFTER the specific :id routes
// (/wipe and /config) so Express does not swallow those paths as an "action".
app.post('/api/browsers/:id/:action', async (req, res) => {
@@ -396,6 +479,11 @@ app.post('/api/browsers/:id/:action', async (req, res) => {
}
});
// Warm up the VPN pool in the background so the dashboard toggle has
// servers ready when the user opens it. Refreshes again automatically
// whenever /api/vpn/pool sees a stale cache.
vpnPool.refreshPool().catch(() => {});
app.listen(PORT, HOST, () => {
console.log('browser-farm manager listening on ' + HOST + ':' + PORT);
});