Control Room: shared shell with in-shell sign-in, Reports, Team and Wallet screens; postback rails (click ids, macros, lsid attribution, conversions, outbound + inbound postbacks with SSRF guards)
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
+45
-8
@@ -8,15 +8,15 @@ const fs = require('fs');
|
||||
const path = require('path');
|
||||
const crypto = require('crypto');
|
||||
|
||||
let DATA_DIR = null, geo = null;
|
||||
let DATA_DIR = null, geo = null, OWN_HOSTS = () => [];
|
||||
const DAY = 86400000;
|
||||
const J = {
|
||||
db: null,
|
||||
FILE: () => path.join(DATA_DIR, 'rotator.json'),
|
||||
load() { try { this.db = JSON.parse(fs.readFileSync(this.FILE(), 'utf8')); } catch (e) { this.db = { v: 1, rotations: [], hits: [] }; } },
|
||||
load() { try { this.db = JSON.parse(fs.readFileSync(this.FILE(), 'utf8')); } catch (e) { this.db = { v: 1, rotations: [], hits: [], conv: [] }; } if (!this.db.conv) this.db.conv = []; },
|
||||
save() { try { fs.writeFileSync(this.FILE(), JSON.stringify(this.db)); } catch (e) {} }
|
||||
};
|
||||
function init(opts) { DATA_DIR = opts.dataDir; geo = opts.geo || null; J.load(); }
|
||||
function init(opts) { DATA_DIR = opts.dataDir; geo = opts.geo || null; OWN_HOSTS = opts.ownHosts || (() => []); J.load(); }
|
||||
const norm = e => String(e || '').trim().toLowerCase();
|
||||
const newCode = () => { const a = 'abcdefghjkmnpqrstuvwxyz23456789'; let s = ''; for (let i = 0; i < 6; i++) s += a[crypto.randomInt(a.length)]; return s; };
|
||||
const BOT = /bot|crawl|spider|slurp|facebookexternalhit|preview|telegrambot|whatsapp|curl|wget|python-requests|headless/i;
|
||||
@@ -27,7 +27,8 @@ function pub(r) {
|
||||
const since7 = Date.now() - 7 * DAY;
|
||||
const perDest = {};
|
||||
for (const h of hits) { const d = perDest[h.d] = perDest[h.d] || { hits: 0, hits7: 0, uniques: new Set() }; d.hits++; if (h.ts >= since7) d.hits7++; d.uniques.add(h.v); }
|
||||
return { id: r.id, code: r.code, name: r.name, owner: r.owner, created: r.created, fallback: r.fallback || '', paused: !!r.paused,
|
||||
const conv = J.db.conv.filter(c => c.r === r.id); const convBy = {}; for (const c of conv) convBy[c.ev] = (convBy[c.ev] || 0) + 1;
|
||||
return { id: r.id, code: r.code, name: r.name, owner: r.owner, created: r.created, fallback: r.fallback || '', paused: !!r.paused, conversions: conv.length, convBy,
|
||||
destinations: r.destinations.map(d => Object.assign({}, d, { hits: (perDest[d.id] || { hits: 0 }).hits, hits7: (perDest[d.id] || { hits7: 0 }).hits7, uniques: (perDest[d.id] || { uniques: new Set() }).uniques.size })),
|
||||
hits: hits.length, hits7: hits.filter(h => h.ts >= since7).length, uniques: new Set(hits.map(h => h.v)).size, bots: hits.filter(h => h.bot).length };
|
||||
}
|
||||
@@ -86,10 +87,43 @@ async function resolve(code, req) {
|
||||
let country = ''; try { country = geo && geo.countryOf ? (geo.countryOf(ip) || '') : ''; } catch (e) {}
|
||||
const ref = String(req.headers.referer || ''); let src = 'direct'; try { if (ref) src = new URL(ref).hostname.replace(/^www\./, ''); } catch (e) {}
|
||||
const device = /mobile|android|iphone|ipad/i.test(ua) ? 'mobile' : 'desktop';
|
||||
J.db.hits.push({ r: r.id, d: d ? d.id : 0, ts: Date.now(), v, c: country, s: src, dv: device, bot: bot ? 1 : 0 });
|
||||
// click ids and sub ids that rode in on the link (tracker / traffic-network params), kept per click
|
||||
const q = {}; try { const sp = new URL(req.url, 'http://x').searchParams; let n = 0; for (const [k, val] of sp) { const key = String(k).toLowerCase(); if (n >= 20) break; if (/^(clickid|click_id|cid|subid|sub_id|s[1-5]|sub[1-5]|t[1-5]|aff_sub[1-5]?|utm_(source|medium|campaign|term|content)|src|source|campaign|zone|placement)$/.test(key)) { q[key] = String(val).slice(0, 200); n++; } } } catch (e) {}
|
||||
const id = crypto.randomBytes(6).toString('hex').slice(0, 10);
|
||||
J.db.hits.push({ id, r: r.id, d: d ? d.id : 0, ts: Date.now(), v, c: country, s: src, dv: device, bot: bot ? 1 : 0, q: Object.keys(q).length ? q : undefined });
|
||||
if (J.db.hits.length > 200000) J.db.hits = J.db.hits.slice(-150000);
|
||||
J.save();
|
||||
return { url, dest: d, rotation: r };
|
||||
let out = url;
|
||||
if (out) {
|
||||
const vals = { clickid: q.clickid || q.cid || q.click_id || '', subid: q.subid || q.sub_id || q.s1 || '', s1: q.s1 || q.sub1 || '', s2: q.s2 || q.sub2 || '', s3: q.s3 || q.sub3 || '', s4: q.s4 || q.sub4 || '', s5: q.s5 || q.sub5 || '', source: src, country, device, lsid: id, code: r.code };
|
||||
for (const [k, val] of Object.entries(q)) if (vals[k] == null) vals[k] = val;
|
||||
out = out.replace(/\{([a-z0-9_]{1,24})\}/gi, (m, k) => encodeURIComponent(vals[k.toLowerCase()] == null ? '' : vals[k.toLowerCase()]));
|
||||
// our own pages get the click id automatically, so a join can be matched back to this click without any macro
|
||||
try { const t = new URL(out); if (OWN_HOSTS().includes(t.hostname.toLowerCase()) && !t.searchParams.has('lsid')) { t.searchParams.set('lsid', id); out = t.toString(); } } catch (e) {}
|
||||
}
|
||||
return { url: out, dest: d, rotation: r, lsid: id };
|
||||
}
|
||||
// conversions: record an event against the click that brought the person (dedupe per click + event)
|
||||
async function convert(lsid, ev, facts) {
|
||||
const h = J.db.hits.find(x => x.id === String(lsid)); if (!h) return null;
|
||||
const r = J.db.rotations.find(x => x.id === h.r); if (!r) return null;
|
||||
if (!J.db.conv.some(c => c.lsid === h.id && c.ev === ev)) { J.db.conv.push({ lsid: h.id, r: r.id, d: h.d, ev, ts: Date.now(), value: Number(facts && facts.payout || 0) || 0, label: facts && facts.label ? String(facts.label).slice(0, 40) : undefined }); if (J.db.conv.length > 100000) J.db.conv = J.db.conv.slice(-80000); J.save(); }
|
||||
return { owner: r.owner, code: r.code, rotationId: r.id, q: h.q || {}, s: h.s, c: h.c, dv: h.dv };
|
||||
}
|
||||
const ctDay = ts => new Date(ts).toLocaleDateString('en-CA', { timeZone: 'America/Chicago' });
|
||||
// one view across everything a member owns, for Reports
|
||||
async function summary(owner, days) {
|
||||
const mine = J.db.rotations.filter(r => r.owner === norm(owner)); const ids = new Set(mine.map(r => r.id));
|
||||
const since = Date.now() - (Number(days) || 30) * DAY;
|
||||
const all = J.db.hits.filter(h => ids.has(h.r) && h.ts >= since); const hits = all.filter(h => !h.bot);
|
||||
const by = key => { const m = {}; for (const h of hits) { const k = h[key] || '(none)'; m[k] = (m[k] || 0) + 1; } return Object.entries(m).sort((a, b) => b[1] - a[1]).slice(0, 12).map(([k, n]) => ({ k, n })); };
|
||||
const dayMap = {}; for (const h of all) { const k = ctDay(h.ts); const d = dayMap[k] = dayMap[k] || { day: k, u: new Set(), b: 0 }; if (h.bot) d.b++; else d.u.add(h.v); }
|
||||
const dlist = Object.values(dayMap).sort((a, b) => a.day < b.day ? -1 : 1).map(d => ({ day: d.day, u: d.u.size, b: d.b }));
|
||||
const hours = new Array(24).fill(0); for (const h of hits) hours[new Date(h.ts).getUTCHours()]++;
|
||||
const conv = J.db.conv.filter(c => ids.has(c.r) && c.ts >= since); const convBy = {}; for (const c of conv) convBy[c.ev] = (convBy[c.ev] || 0) + 1;
|
||||
const top = mine.map(r => { const hs = hits.filter(h => h.r === r.id); return { id: r.id, name: r.name, code: r.code, hits: hs.length, unique: new Set(hs.map(h => h.v)).size, conv: conv.filter(c => c.r === r.id).length }; }).sort((a, b) => b.unique - a.unique).slice(0, 10);
|
||||
return { links: mine.length, totals: { served: hits.length, unique: new Set(hits.map(h => h.v)).size, bots: all.length - hits.length, joins: convBy.join || 0, wallets: convBy.wallet || 0, purchases: convBy.purchase || 0, external: convBy.external || 0 },
|
||||
days: dlist, source: by('s'), country: by('c'), device: by('dv'), hours, top, conversions: Object.entries(convBy).map(([k, n]) => ({ k, n })).sort((a, b) => b.n - a.n) };
|
||||
}
|
||||
async function stats(owner, id) {
|
||||
const r = J.db.rotations.find(r => r.id === Number(id) && r.owner === norm(owner)); if (!r) return null;
|
||||
@@ -97,8 +131,11 @@ async function stats(owner, id) {
|
||||
const by = key => { const m = {}; for (const h of hits) { const k = h[key] || '(none)'; m[k] = (m[k] || 0) + 1; } return Object.entries(m).sort((a, b) => b[1] - a[1]).slice(0, 12).map(([k, n]) => ({ k, n })); };
|
||||
const days = {}; for (const h of hits) { const k = new Date(h.ts).toISOString().slice(0, 10); days[k] = (days[k] || 0) + 1; }
|
||||
const hours = new Array(24).fill(0); for (const h of hits) hours[new Date(h.ts).getUTCHours()]++;
|
||||
return { rotation: pub(r), country: by('c'), source: by('s'), device: by('dv'), days: Object.entries(days).sort().slice(-30).map(([k, n]) => ({ day: k, n })), hours, bots: J.db.hits.filter(h => h.r === r.id && h.bot).length };
|
||||
const subs = {}; for (const h of hits) if (h.q) for (const [k, v] of Object.entries(h.q)) { const key = k + '=' + v; subs[key] = (subs[key] || 0) + 1; }
|
||||
const conv = J.db.conv.filter(c => c.r === r.id); const convBy = {}; for (const c of conv) { const k = c.ev + (c.label ? ' · ' + c.label : ''); convBy[k] = (convBy[k] || 0) + 1; }
|
||||
return { rotation: pub(r), country: by('c'), source: by('s'), device: by('dv'), days: Object.entries(days).sort().slice(-30).map(([k, n]) => ({ day: k, n })), hours, bots: J.db.hits.filter(h => h.r === r.id && h.bot).length,
|
||||
subs: Object.entries(subs).sort((a, b) => b[1] - a[1]).slice(0, 12).map(([k, n]) => ({ k, n })), conversions: Object.entries(convBy).sort((a, b) => b[1] - a[1]).map(([k, n]) => ({ k, n })) };
|
||||
}
|
||||
function totals() { return { rotations: J.db.rotations.length, hits: J.db.hits.length }; }
|
||||
|
||||
module.exports = { init, list, get, create, update, remove, addDest, updateDest, resolve, stats, totals };
|
||||
module.exports = { init, list, get, create, update, remove, addDest, updateDest, resolve, stats, totals, convert, summary };
|
||||
|
||||
Reference in New Issue
Block a user