Files
instantadpay/spamscan.js
T

146 lines
11 KiB
JavaScript

// Link-farm scan (Marty, 2026-10-30). BYTE-FOR-BYTE COPY in both repos (InstantAdPay site/ and LinkSpin site/): the two
// sites must flag the same accounts by the same rule; qa/profile-policy.test.cjs on each side hashes this file and
// prohibited.js against the sibling checkout when it is present. Edit one, copy to the other, never fork.
//
// Why: an SEO backlink farm opened 106 free accounts with casino and local-business bios plus a website link, and the public
// walls put that text in front of Google. The accounts are gone; this job finds the next batch before the crawler does.
// What: an in-app daily job (checked every few minutes, claimOnce per Central day, runs at 06:30 Central; replaces a server
// cron) lists accounts with memberId 0 and no wallet that carry a link (URL in the bio, or any social set) AND at least
// one farm signal: a prohibited term in the bio or socials, a sign-in that stopped within 15 minutes of the sign-up, or
// no sponsor at all. Emails on the site's spamScanAllow setting are skipped. The result lands in DATA_DIR/wall-spam-scan.json
// and, only when there are candidates, ONE Telegram goes to the private admin chat. The admin pane "Link-farm list"
// (Admin > Members) shows the rows with delete (the existing DELETE /api/admin/member, which refuses on-chain members) and
// allow (adds the email to spamScanAllow). Nothing is deleted automatically, and nothing cascades to the other site.
'use strict';
const fs = require('fs');
const path = require('path');
const P = require('./prohibited');
const TZ = 'America/Chicago';
const dayOf = ts => new Date(ts).toLocaleDateString('en-CA', { timeZone: TZ });
const minuteOf = ts => { const [h, m] = new Date(ts).toLocaleTimeString('en-GB', { timeZone: TZ, hour: '2-digit', minute: '2-digit', hour12: false }).split(':').map(Number); return h * 60 + m; };
const RUN_AT = 6 * 60 + 30; // 06:30 Central
const QUICK_MS = 15 * 60 * 1000;
const DEFAULT_ALLOW = ['cryptogoat76@gmail.com', 'csicsokaren@gmail.com', 'osamedefe2026@gmail.com'];
const SOCIAL_KEYS = ['facebook', 'twitter', 'youtube', 'instagram', 'tiktok', 'telegram', 'linkedin', 'website', 'video'];
const LINK_KEYS = ['website', 'twitter', 'youtube']; // the fields a farm comes for (quarantine rule at profile save)
let X = null, FILE = null, S = null, running = false;
// opts: { dataDir, site, siteUrl, accounts, siteConfig(), saveSite(patch), notify(text) async, marks?, alsoOn?(email) async -> true|false|null, otherSite }
function init(opts) { X = opts; FILE = path.join(opts.dataDir, 'wall-spam-scan.json'); S = load(); }
function load() { try { return JSON.parse(fs.readFileSync(FILE, 'utf8')); } catch (e) { return { day: null, at: 0, candidates: [], scanned: 0, skipped: 0 }; } }
function save() { try { fs.writeFileSync(FILE, JSON.stringify(S, null, 2)); } catch (e) {} }
async function claimOnce(key) { try { return X.marks ? await X.marks.claim(key) : true; } catch (e) { return true; } }
const norm = e => String(e || '').trim().toLowerCase();
function parseSocials(v) { if (!v) return null; if (typeof v === 'object') return v; try { const o = JSON.parse(v); return o && typeof o === 'object' ? o : null; } catch (e) { return null; } }
// every link a profile carries, socials first, then URLs picked out of the bio
function linksOf(a) {
const out = []; const soc = parseSocials(a.socials) || {};
for (const k of SOCIAL_KEYS) { const v = String(soc[k] || '').trim(); if (v) out.push(v); }
const m = String(a.bio || '').match(/https?:\/\/[^\s<>"']+|(?:www\.)?[a-z0-9-]+(?:\.[a-z0-9-]+)*\.[a-z]{2,24}(?:\/[^\s<>"']*)?/gi);
if (m) for (const u of m) if (!out.includes(u)) out.push(u);
return out;
}
// Allowlist: the site setting (array or comma/space separated string) plus the seeded three.
function allowlist() {
const c = (X && X.siteConfig && X.siteConfig()) || {};
const raw = c.spamScanAllow == null ? DEFAULT_ALLOW : c.spamScanAllow;
const arr = Array.isArray(raw) ? raw : String(raw).split(/[\s,;]+/);
return [...new Set(arr.map(norm).filter(Boolean))];
}
function setAllow(email, on) {
const list = allowlist(); const e = norm(email); if (!e) return list;
const next = on ? [...new Set(list.concat(e))] : list.filter(x => x !== e);
if (X.saveSite) X.saveSite({ spamScanAllow: next.join(', ') });
return next;
}
// The detector. Pure: account in, verdict out. `allow` is a Set of lower-cased emails (defaults to the site allowlist).
function classify(a, allow, now) {
now = now || Date.now();
const r = { email: a.email, candidate: false, reasons: [], links: [] };
if (!a || !a.email) return r;
if (Number(a.memberId) > 0) return r; // a buyer is a member, never a candidate
if (a.address) return r; // a wallet means they came to earn, not to plant a link
const bioUrl = P.hasUrl(a.bio);
const soc = parseSocials(a.socials); const socialsPresent = !!(soc && SOCIAL_KEYS.some(k => String(soc[k] || '').trim()));
if (!bioUrl && !socialsPresent && !a.profileQuarantined) return r;
r.links = linksOf(a);
const term = P.match(a.bio) || (soc ? SOCIAL_KEYS.map(k => P.match(soc[k])).find(Boolean) : null);
if (term) r.reasons.push('prohibited:' + term);
const seen = Number(a.lastSeen) || Number(a.created) || now;
if (Number(a.created) && seen - Number(a.created) < QUICK_MS) r.reasons.push('quick');
if (!a.sponsorRef) r.reasons.push('no-sponsor');
if (a.profileQuarantined) r.reasons.push('quarantined');
if (!r.reasons.length) return r;
if ((allow || new Set(allowlist())).has(norm(a.email))) { r.allowlisted = true; return r; }
r.candidate = true;
return r;
}
// Quarantine rule at profile save (Marty): a brand-new account (memberId 0, no wallet, no sponsor, younger than 24 h)
// whose FIRST profile save carries a URL in the bio or a website / X / YouTube social is saved with profileQuarantined.
function shouldQuarantine(acct, bio, socials, now) {
now = now || Date.now();
if (!acct || Number(acct.memberId) > 0 || acct.address || acct.sponsorRef) return false;
if (!(Number(acct.created) > 0) || now - Number(acct.created) >= 24 * 3600 * 1000) return false;
if (acct.bio || parseSocials(acct.socials)) return false; // not the first save
const soc = parseSocials(socials) || {};
return P.hasUrl(bio) || LINK_KEYS.some(k => String(soc[k] || '').trim());
}
const QUARANTINE_NOTICE = 'Your profile links will appear after your first ad package.';
// One pass over every account. Writes the result file; sends the Telegram when there is anything to review.
async function run(opts) {
if (running) return S; running = true;
try {
const now = Date.now(); const allow = new Set(allowlist());
const all = await X.accounts.listAll(50000, { shells: true });
const names = new Map(); for (const a of all) { if (a.username) names.set(norm(a.username), a); if (a.code) names.set(norm(a.code), a); }
const out = []; let skipped = 0;
for (const a of all) {
const c = classify(a, allow, now);
if (c.allowlisted) { skipped++; continue; }
if (!c.candidate) continue;
const sp = a.sponsorRef ? names.get(norm(a.sponsorRef)) : null;
let alsoOn = null; try { alsoOn = X.alsoOn ? await X.alsoOn(a.email, a) : null; } catch (e) { alsoOn = null; }
out.push({ email: a.email, username: a.username || null, created: Number(a.created) || 0, lastSeen: Number(a.lastSeen) || 0, sponsorRef: a.sponsorRef || '',
sponsorName: sp ? (sp.username ? '@' + sp.username : sp.email) : (a.sponsorRef || null), firstLink: c.links[0] || null, links: c.links.slice(0, 6), reasons: c.reasons,
bio: String(a.bio || '').slice(0, 160), quarantined: !!a.profileQuarantined, alsoOn });
}
out.sort((x, y) => y.created - x.created);
// watch list (Marty 2026-10-31: do not delete the candidates yet, watch whether they activate a wallet or do something real):
// every candidate carries firstSeen from the previous run; the Telegram goes out only when something CHANGED, listing the
// accounts that are new since the last run and the ones that left the list (linked a wallet, bought, or were deleted),
// with the count still being watched. A list that merely persists is silence.
const prev = new Map((S.candidates || []).map(c => [c.email, c]));
for (const c of out) { const p = prev.get(c.email); c.firstSeen = p && p.firstSeen ? p.firstSeen : (p ? p.created || now : now); }
const fresh = out.filter(c => !prev.has(c.email));
const nowSet = new Set(out.map(c => c.email));
const left = [...prev.values()].filter(c => !nowSet.has(c.email) && !allow.has(norm(c.email)));
S = { day: dayOf(now), at: now, candidates: out, scanned: all.length, skipped, manual: !!(opts && opts.manual), fresh: fresh.map(c => c.email), left: left.map(c => c.email) }; save();
if ((fresh.length || left.length) && !(opts && opts.quiet)) { try { await X.notify(telegramText(out, now, { fresh, left })); S.notified = now; save(); } catch (e) { console.error('spamscan notify', e.message); } }
console.log('spamscan', X.site, 'scanned', all.length, 'candidates', out.length, 'allowlisted', skipped);
return S;
} finally { running = false; }
}
function telegramText(out, now, changes) {
const row = c => (c.username ? '@' + c.username : c.email) + ' | joined ' + dayOf(c.created || now) + ' | sponsor ' + (c.sponsorName || 'none') + ' | ' + (c.firstLink || 'no link') + (c.alsoOn === true ? ' | also on ' + X.otherSite : '');
const fresh = changes && changes.fresh ? changes.fresh : out, left = changes && changes.left ? changes.left : [];
const watched = out.length - fresh.length;
const lines = [X.site + ' link-farm scan (' + dayOf(now) + ', ' + X.siteUrl.replace(/^https?:\/\//, '') + '): ' + fresh.length + ' new, ' + watched + ' still watched, ' + left.length + ' left the list'];
if (fresh.length) { lines.push('New since the last run:'); for (const c of fresh.slice(0, 15)) lines.push(row(c)); if (fresh.length > 15) lines.push('... and ' + (fresh.length - 15) + ' more'); }
if (left.length) { lines.push('Left the list (linked a wallet, bought, or were deleted):'); for (const c of left.slice(0, 10)) lines.push(c.username ? '@' + c.username : c.email); if (left.length > 10) lines.push('... and ' + (left.length - 10) + ' more'); }
lines.push('Review in Admin > Members > Link-farm list');
return lines.join('\n');
}
// checked every few minutes from server.js: once per Central day, from 06:30 on
async function tick(now) {
now = now || Date.now(); const day = dayOf(now);
if (minuteOf(now) < RUN_AT || S.day === day) return { skipped: true };
if (!await claimOnce('spamscan:' + day)) { S.day = day; save(); return { skipped: true, claimedElsewhere: true }; }
return run();
}
function view() { return { site: X ? X.site : null, otherSite: X ? X.otherSite : null, last: S, allow: allowlist(), running }; }
module.exports = { init, classify, shouldQuarantine, linksOf, allowlist, setAllow, run, tick, view, telegramText, QUARANTINE_NOTICE, DEFAULT_ALLOW, RUN_AT, QUICK_MS, dayOf, minuteOf };