a428075330
Edits (POST /api/my/spot/<id>, /api/my/default, uploads): still any number of times. Every change now gets the full check:
checkLink, then (LINK_PROBE on) the link must answer (safety.probeLink, up to 3 redirects each checked) and an image address must
download as a real 468x60 image of 150 KB or less (safety.probeImage; the address is kept, a Banner Spin image rotates). The
Banner Spin image + click link LinkSpin just handed the member are not fetched (that would count a LinkSpin click). safety.js gains
privateIp + safeLookup with the Node 20 fix VBB/ViralAdBlitz needed (honors opts.all, answers an array). Every real change is
logged on the spot (or the default): old and new link and image, time, via edit/upload; the owner sees the newest 10. 120 changes
an hour per connection (each one fetches the link).
Admin takedown (the Viral Banner Blitz way, 8e6d2f9): /admin (new page) finds a spot by #number, wallet or network username;
POST /api/admin/spots/<id>/takedown {reason (required, the owner sees it), credit}. Hidden everywhere at once: the board shows a
house tile in its place (nobody else moves), the gate gives its step to a house site, /go/<id> goes home, its Network Ad Space
banner is paused (approved = 0) and no new one is placed. No views or clicks count while down. My Spots shows "Taken down" with
the reason and no edit controls. Restore (POST .../restore) reverses it (NAS resumed). Admin ledger: GET /api/admin/actions (who,
when, reason, credit decision). The contract is never touched.
CREDIT BACK IS NOT BUILT: a spot is a $5 on-chain purchase and this site has no free replacement spot or post credit, so
credit: "back" is refused with a note until Marty decides what it means here; takedowns are "no credit".
Chatbot: CANNED takedown + banner answer (unlimited checked, logged edits) + FACTS EDITS/TAKEDOWN lines, KNOWLEDGE_DATE 2026-11-16.
line.css?v=9, my.js?v=z3.
Tests: smoke section 10 (the lookup shape, edit log, unlimited edits, refusals: unreachable, 404, private address, http redirect,
shortener, bad image size/404/unreachable; redirect followed; same link logs nothing; upload logged; default checked + logged;
takedown: token, reason, credit choice, credit back refused, held house tile, image nowhere, /go home, gate, no views, owner view,
no edits while down, ledger, restore, /admin) against a stand-in internet preloaded into the app (qa/out/probe-stub.cjs, nothing
leaves the machine), plus phone renders of My Spots taken down and /admin: 193/193 with UI. chatbot.test 181/181.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
142 lines
8.6 KiB
JavaScript
142 lines
8.6 KiB
JavaScript
// Link + image checks for My Spots. https only, a real public hostname, no tricks; uploads must be a real
|
|
// PNG/JPG/GIF at 468x60 (or 936x120 retina) and at most 150 KB.
|
|
const net = require('net');
|
|
const dns = require('dns');
|
|
const https = require('https');
|
|
|
|
const MAX_UPLOAD = 150 * 1024;
|
|
const BAD_HOST = /(^|\.)(localhost|local|internal|lan|home|corp|onion|test|invalid|example)$/i;
|
|
// shorteners and redirectors hide where a click goes; a member link must show its real destination
|
|
const REDIRECTORS = /(^|\.)(bit\.ly|tinyurl\.com|t\.co|goo\.gl|is\.gd|cutt\.ly|ow\.ly|rebrand\.ly|shorturl\.at|rb\.gy|s\.id|t\.ly|v\.gd|bl\.ink|tiny\.cc)$/i;
|
|
|
|
function checkLink(raw, what) {
|
|
what = what || 'Link';
|
|
const s = String(raw || '').trim();
|
|
if (!s) return { error: what + ' is empty.' };
|
|
if (s.length > 600) return { error: what + ' is too long (600 characters max).' };
|
|
if (/[\s<>"'`\\]/.test(s)) return { error: what + ' has spaces or characters a web address cannot have.' };
|
|
let u; try { u = new URL(s); } catch (e) { return { error: what + ' is not a valid web address.' }; }
|
|
if (u.protocol !== 'https:') return { error: what + ' must start with https://' };
|
|
if (u.username || u.password) return { error: what + ' cannot contain a username or password.' };
|
|
const h = u.hostname.toLowerCase().replace(/\.$/, '');
|
|
if (net.isIP(h.replace(/^\[|\]$/g, ''))) return { error: what + ' must use a domain name, not an IP address.' };
|
|
if (!h.includes('.') || BAD_HOST.test(h)) return { error: what + ' must point to a public website.' };
|
|
if (!/^[a-z0-9.-]+$/.test(h) && !/^xn--/.test(h)) return { error: what + ' has an unusual domain name.' };
|
|
if (REDIRECTORS.test(h)) return { error: 'Use the real destination, not a link shortener.' };
|
|
if (u.port && !['443', ''].includes(u.port)) return { error: what + ' must use the normal https port.' };
|
|
return { ok: true, url: u.toString() };
|
|
}
|
|
|
|
function imageInfo(buf) {
|
|
if (!buf || buf.length < 24) return null;
|
|
if (buf.slice(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])))
|
|
return { type: 'png', ext: '.png', w: buf.readUInt32BE(16), h: buf.readUInt32BE(20) };
|
|
if (buf.slice(0, 6).toString('latin1') === 'GIF87a' || buf.slice(0, 6).toString('latin1') === 'GIF89a')
|
|
return { type: 'gif', ext: '.gif', w: buf.readUInt16LE(6), h: buf.readUInt16LE(8) };
|
|
if (buf[0] === 0xff && buf[1] === 0xd8) {
|
|
let i = 2;
|
|
while (i + 9 < buf.length) {
|
|
if (buf[i] !== 0xff) { i++; continue; }
|
|
const m = buf[i + 1];
|
|
if (m >= 0xc0 && m <= 0xcf && m !== 0xc4 && m !== 0xc8 && m !== 0xcc) return { type: 'jpg', ext: '.jpg', h: buf.readUInt16BE(i + 5), w: buf.readUInt16BE(i + 7) };
|
|
if (m === 0xd8 || m === 0x01 || (m >= 0xd0 && m <= 0xd7)) { i += 2; continue; }
|
|
i += 2 + buf.readUInt16BE(i + 2);
|
|
}
|
|
return { type: 'jpg', ext: '.jpg', w: 0, h: 0 };
|
|
}
|
|
return null;
|
|
}
|
|
function checkImage(buf) {
|
|
if (!buf || !buf.length) return { error: 'No file received.' };
|
|
if (buf.length > MAX_UPLOAD) return { error: 'That file is ' + Math.ceil(buf.length / 1024) + ' KB. The limit is 150 KB.' };
|
|
const info = imageInfo(buf);
|
|
if (!info) return { error: 'Upload a PNG, JPG or GIF file.' };
|
|
const ok = (info.w === 468 && info.h === 60) || (info.w === 936 && info.h === 120);
|
|
if (!ok) return { error: 'The image is ' + info.w + 'x' + info.h + '. It must be 468x60 (or 936x120).' };
|
|
return { ok: true, info };
|
|
}
|
|
|
|
// ---- the network part of the check (2026-11-16: every edit gets it, any number of edits) ----
|
|
// Outbound requests only to public addresses: the DNS answer used for the connection is the one checked.
|
|
function privateIp(ip) {
|
|
if (net.isIPv4(ip)) {
|
|
const [a, b] = ip.split('.').map(Number);
|
|
return a === 0 || a === 10 || a === 127 || (a === 100 && b >= 64 && b <= 127) || (a === 169 && b === 254) || (a === 172 && b >= 16 && b <= 31)
|
|
|| (a === 192 && b === 168) || (a === 192 && b === 0) || (a === 198 && (b === 18 || b === 19)) || a >= 224;
|
|
}
|
|
const s = String(ip).toLowerCase();
|
|
if (s.startsWith('::ffff:')) return privateIp(s.slice(7));
|
|
return s === '::' || s === '::1' || s.startsWith('fc') || s.startsWith('fd') || s.startsWith('fe80') || s.startsWith('ff');
|
|
}
|
|
// Node 20's https agent asks for every address (opts.all, for happy eyeballs) and expects an array back; answering with one
|
|
// address broke every link check on VBB and ViralAdBlitz (fixed there 2026-11-15). Answer in the shape asked for, private refused.
|
|
function safeLookup(host, opts, cb) {
|
|
if (typeof opts === 'function') { cb = opts; opts = {}; }
|
|
dns.lookup(host, Object.assign({}, opts, { all: true }), (err, list) => {
|
|
if (err) return cb(err);
|
|
if (!list || !list.length || list.some(a => privateIp(a.address))) return cb(new Error('private address'));
|
|
if (opts && opts.all) return cb(null, list);
|
|
cb(null, list[0].address, list[0].family);
|
|
});
|
|
}
|
|
// GET with a size cap, a timeout and no redirects followed (a redirect is reported so the caller can decide)
|
|
function get(url, maxBytes, timeoutMs) {
|
|
return new Promise(resolve => {
|
|
let done = false; const fin = v => { if (!done) { done = true; resolve(v); } };
|
|
let req;
|
|
try {
|
|
req = https.get(url, { lookup: safeLookup, timeout: timeoutMs || 5000, headers: { 'User-Agent': 'Mozilla/5.0 (compatible; DLBCheck/1.0; +https://downlinebanners.com)', Accept: '*/*' } }, res => {
|
|
const out = { status: res.statusCode, headers: res.headers, location: res.headers.location || null };
|
|
if (!maxBytes) { res.destroy(); return fin(out); }
|
|
const ch = []; let n = 0;
|
|
res.on('data', c => { n += c.length; if (n > maxBytes) { res.destroy(); return fin(Object.assign(out, { tooBig: true })); } ch.push(c); });
|
|
res.on('end', () => fin(Object.assign(out, { body: Buffer.concat(ch) })));
|
|
res.on('error', () => fin(Object.assign(out, { error: 'read' })));
|
|
});
|
|
req.on('timeout', () => { req.destroy(); fin({ error: 'timeout' }); });
|
|
req.on('error', e => fin({ error: /private address/.test(e.message) ? 'private' : 'unreachable' }));
|
|
} catch (e) { fin({ error: 'unreachable' }); }
|
|
});
|
|
}
|
|
// the target link must answer (up to 3 redirects, each one checked like the link itself). Unlike VBB, a site that refuses frames
|
|
// is fine here: the gate opens it in a new tab instead (board.frameable).
|
|
async function probeLink(url) {
|
|
let cur = url;
|
|
for (let hop = 0; hop < 4; hop++) {
|
|
const r = await get(cur, 0, 6000);
|
|
if (r.error === 'private') return { error: 'That link points to a private network address.' };
|
|
if (r.error) return { error: 'We could not reach that link. Check it opens in your browser and try again.' };
|
|
if (r.status >= 300 && r.status < 400 && r.location) {
|
|
let next; try { next = new URL(r.location, cur).toString(); } catch (e) { return { error: 'That link redirects somewhere invalid.' }; }
|
|
const c = checkLink(next, 'The page your link redirects to'); if (c.error) return c;
|
|
cur = c.url; continue;
|
|
}
|
|
if (r.status >= 400) return { error: 'That link answered with an error (' + r.status + '). Use a page that loads.' };
|
|
return { ok: true };
|
|
}
|
|
return { error: 'That link redirects too many times.' };
|
|
}
|
|
// an https image address: downloaded once and checked like an upload (468x60 or 936x120, 150 KB). The spot keeps the address
|
|
// (a LinkSpin Banner Spin image rotates, so it is never copied). -> { ok, info } or { error }
|
|
async function probeImage(url) {
|
|
let cur = url, r = null;
|
|
for (let hop = 0; hop < 4; hop++) { // up to 3 redirects, each one checked like the address itself
|
|
r = await get(cur, MAX_UPLOAD, 6000);
|
|
if (!r.error && r.status >= 300 && r.status < 400 && r.location) {
|
|
let next; try { next = new URL(r.location, cur).toString(); } catch (e) { return { error: 'That image address redirects somewhere invalid.' }; }
|
|
const c = checkLink(next, 'The address your image redirects to'); if (c.error) return c;
|
|
cur = c.url; r = null; continue;
|
|
}
|
|
break;
|
|
}
|
|
if (!r) return { error: 'That image address redirects too many times.' };
|
|
if (r.error === 'private') return { error: 'That image address points to a private network address.' };
|
|
if (r.error) return { error: 'We could not download that image. Check the address or upload the file instead.' };
|
|
if (r.tooBig) return { error: 'That image is over 150 KB.' };
|
|
if (r.status !== 200) return { error: 'The image address answered ' + r.status + '. Use a direct link to the image file, or upload it.' };
|
|
const c = checkImage(r.body); if (c.error) return c;
|
|
return { ok: true, info: c.info };
|
|
}
|
|
|
|
module.exports = { checkLink, checkImage, imageInfo, privateIp, safeLookup, probeLink, probeImage, MAX_UPLOAD };
|