Partner endpoint for DownlineHydra tracked program links (Marty 2026-11-15): POST /api/partner/dh/links, net.v1-signed with NETWORK_SSO_SECRET. op ensure makes or updates one link per program on the member's own account (by email, else network username), tagged with a new 'DownlineHydra' network source and the program id as sub-id (?s1=&placement=), clean link host from domains.hosts(); op stats answers clicks per program. rotator.ensurePartner / partnerStats; catalog template downlinehydra; NET_SRC dh. qa/partner-dhlinks.test.mjs 13/13, partner-spins 13/13, netsrc 16/16. LOCAL ONLY, not pushed or deployed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -3129,6 +3129,42 @@ const server = http.createServer(async (req, res) => {
|
||||
}
|
||||
return json(res, 200, out, { 'Cache-Control': 'no-store' });
|
||||
}
|
||||
// -- DownlineHydra tracked program links (Marty 2026-11-15). Same net.v1 signature as the Banner Spins call above. A DownlineHydra
|
||||
// member saved a referral link for an outside program (a wild head or a my head); their downline sees a LinkSpin link for it
|
||||
// on this member's own account, tagged with the account's "DownlineHydra" source and the program id as a sub-id
|
||||
// (?s1=<program>&placement=<program>, so Reports split clicks by program). Only clean link domains (domains.hosts()).
|
||||
// { email, username?, op: 'ensure', links: [{ program, name, url }] } -> { account, username, host, links: { program: { code, link } }, errors }
|
||||
// { email, username?, op: 'stats' } -> { account, host, stats: { program: { code, link, hits, hits30, unique30 } } }
|
||||
// Nothing else about the account is read or changed.
|
||||
if (p === '/api/partner/dh/links' && req.method === 'POST') {
|
||||
let raw = ''; try { raw = await new Promise((ok, no) => { let d = ''; req.on('data', c => { d += c; if (d.length > 65536) { req.destroy(); no(new Error('too big')); } }); req.on('end', () => ok(d)); req.on('error', no); }); } catch (e) { return json(res, 400, { error: 'bad body' }); }
|
||||
const sec = String(process.env.NETWORK_SSO_SECRET || '').trim(); const ts = String(req.headers['x-net-ts'] || ''); const sig = String(req.headers['x-net-sig'] || '');
|
||||
if (sec.length < 32 || !/^\d{13}$/.test(ts) || Math.abs(Date.now() - Number(ts)) > 300000) return json(res, 401, { error: 'unauthorized' });
|
||||
const want = crypto.createHmac('sha256', sec).update('net.v1.' + ts + '\n' + raw).digest('hex');
|
||||
if (sig.length !== want.length || !crypto.timingSafeEqual(Buffer.from(sig), Buffer.from(want))) return json(res, 401, { error: 'unauthorized' });
|
||||
let b; try { b = JSON.parse(raw || '{}'); } catch (e) { return json(res, 400, { error: 'bad json' }); }
|
||||
const email = String(b.email || '').trim().toLowerCase();
|
||||
let a = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(email) ? await accounts.byEmail(email).catch(() => null) : null;
|
||||
if ((!a || !a.email) && b.username && /^[A-Za-z0-9_.-]{2,40}$/.test(String(b.username))) a = await accounts.byUsername(String(b.username)).catch(() => null);
|
||||
if (!a || !a.email) return json(res, 200, { account: false });
|
||||
const hs = domains.hosts(); if (!hs.length) return json(res, 503, { error: 'No clean link domain right now.' });
|
||||
const host = hs[0].host; const PID = /^[a-z0-9_-]{1,40}$/i;
|
||||
const linkOf = (code, pid) => 'https://' + host + '/r/' + code + '?s1=' + encodeURIComponent(pid) + '&placement=' + encodeURIComponent(pid);
|
||||
if (b.op === 'stats') {
|
||||
const st = rotator.partnerStats(a.email, 'dh:'); const out = {};
|
||||
for (const [k, v] of Object.entries(st)) { const pid = k.slice(3); out[pid] = { code: v.code, link: linkOf(v.code, pid), hits: v.hits, hits30: v.hits30, unique30: v.unique30 }; }
|
||||
return json(res, 200, { account: true, username: a.username || null, host, stats: out }, { 'Cache-Control': 'no-store' });
|
||||
}
|
||||
if (b.op !== 'ensure' || !Array.isArray(b.links) || !b.links.length || b.links.length > 60) return json(res, 400, { error: 'op' });
|
||||
const src = sources.networkSource(a.email, 'downlinehydra');
|
||||
const links = {}, errors = {};
|
||||
for (const x of b.links) {
|
||||
const pid = String((x && x.program) || ''); if (!PID.test(pid)) { errors[pid.slice(0, 40) || '?'] = 'program'; continue; }
|
||||
const r = rotator.ensurePartner(a.email, 'dh:' + pid, 'DownlineHydra: ' + String(x.name || pid).slice(0, 60), String(x.url || ''), src ? src.id : null);
|
||||
if (r.error) errors[pid] = r.error; else links[pid] = { code: r.code, link: linkOf(r.code, pid), created: r.created };
|
||||
}
|
||||
return json(res, 200, { account: true, username: a.username || null, host, sourceId: src ? src.id : null, links, errors }, { 'Cache-Control': 'no-store' });
|
||||
}
|
||||
if (p === '/api/partner/clicks' && req.method === 'POST') {
|
||||
const sg = await readPartnerSigned(req, 2e6); if (!sg.body) return json(res, sg.status, { error: sg.error });
|
||||
const owners = convtrack.importOwners(sg.body.owners); // InstantAdPay members' own soft keys first, so their own clicks import as tests
|
||||
|
||||
Reference in New Issue
Block a user