diff --git a/server.js b/server.js index 69566a3..a26c997 100644 --- a/server.js +++ b/server.js @@ -3059,6 +3059,35 @@ const server = http.createServer(async (req, res) => { } // -- conversion pixel, InstantAdPay side (2026-10-23): its ad clicks arrive here (signed, upsert by id), and its members' // goals live on their LinkSpin account (same email): summary for the Campaigns rows, create / update / remove / test. + // -- Downline Banners "Use a LinkSpin Banner Spin" (Marty 2026-11-13). Downline Banners signs with NETWORK_SSO_SECRET (the secret + // all three sites share): X-Net-Ts + X-Net-Sig = hex HMAC-SHA256('net.v1.' + ts + newline + raw body), 5-minute window. + // { wallet } lists the 468x60 Banner Spins of the LinkSpin account holding that wallet. { wallet, spinId } also makes sure the + // account has a "Downline Banners" source (catalog template), tags the spin with it when the spin has no source yet, and + // answers the image + click URLs with the board's macros. Nothing else about the account is read or changed. + if (p === '/api/partner/bl/spins' && req.method === 'POST') { + let raw = ''; try { raw = await new Promise((ok, no) => { let d = ''; req.on('data', c => { d += c; if (d.length > 4096) { 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 w = String(b.wallet || '').trim().toLowerCase(); if (!/^0x[0-9a-f]{40}$/.test(w)) return json(res, 400, { error: 'wallet' }); + const a = await accounts.byAddress(w).catch(() => null); + if (!a || !a.email) return json(res, 200, { account: false }); + const spins = (await bannerspin.list(a.email)).filter(x => x.size === '468x60' && !x.adminOff); + const hs = domains.hosts(); const host = (hs[0] && hs[0].host) || SITE_HOST; + const out = { account: true, username: a.username || null, spins: spins.map(x => ({ id: x.id, name: x.name, code: x.code, paused: !!x.paused, banners: (x.banners || []).length, sourceId: x.sourceId || null })) }; + if (b.spinId != null) { + const sp = spins.find(x => x.id === Number(b.spinId)); if (!sp) return json(res, 404, { error: 'No such 468x60 Banner Spin on this LinkSpin account.' }); + let src = ((await sources.list(a.email)).sources || []).find(x => x.template === 'downlinebanners'); + if (!src) { const r = await sources.create(a.email, { template: 'downlinebanners', name: 'Downline Banners', site: 'https://downlinebanners.com' }); if (r.error) return json(res, 400, { error: r.error }); src = r.source || ((await sources.list(a.email)).sources || []).find(x => x.template === 'downlinebanners'); } + let tagged = false; + if (src && !sp.sourceId) { const r = await bannerspin.update(a.email, sp.id, { sourceId: src.id }); tagged = !r.error; } + out.use = { spinId: sp.id, name: sp.name, img: 'https://' + host + '/i/' + sp.code + '.gif', link: 'https://' + host + '/c/' + sp.code + '?clickid={clickid}&placement={spot}&s1={type}', + sourceId: src ? src.id : null, tagged, otherSource: !!(sp.sourceId && src && sp.sourceId !== src.id) }; + } + return json(res, 200, out, { '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