// Website read: our own headless Chromium opens the public page (no sign-in, no clicks on wallet buttons, no forms), takes a // screenshot, keeps the visible text, and looks for contract addresses and chain hints in the page, its links and its scripts. // Read-only and public: the browser never connects a wallet and never submits anything. // Private network targets are refused (no localhost, no 10/8, 172.16/12, 192.168/16, 169.254/16, ::1, fc00::/7). 'use strict'; const dns = require('dns').promises; const net = require('net'); const EXPLORERS = { 'bscscan.com': 'bsc', 'polygonscan.com': 'polygon', 'etherscan.io': 'eth', 'basescan.org': 'base' }; const CHAIN_WORDS = { bsc: /\b(bnb ?(smart )?chain|bsc|binance smart chain|bep-?20|pancakeswap)\b/gi, polygon: /\b(polygon|matic|\bpol\b|polygonscan)\b/gi, eth: /\b(ethereum|erc-?20|etherscan|mainnet eth)\b/gi, base: /\b(base chain|on base|base network|basescan|coinbase base)\b/gi }; const ADDR_RE = /0x[a-fA-F0-9]{40}(?![a-fA-F0-9])/g; const COMMON = new Set(['0x0000000000000000000000000000000000000000', '0x000000000000000000000000000000000000dead', '0x55d398326f99059ff775485246999027b3197955', '0x8ac76a51cc950d9822d68b83fe1ad97b32cd580d', '0xe9e7cea3dcd96aee6a24b63ae6d6e53fba8a9b56', '0xc2132d05d31c914a87c6611c10748aeb04b58e8f', '0x3c499c542cef5e3811e1192ce70d8cc03d5c3359', '0x2791bca1f2de4661ed88a30c99a7a9449aa84174', '0xdac17f958d2ee523a2206206994597c13d831ec7', '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48', '0x833589fcd6edb6e08f4c7c32d4f71b54bda02913', '0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c', '0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270', '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', '0x4200000000000000000000000000000000000006', '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', '0x10ed43c718714eb63d5aa57b78b54704e256024e', '0xca11bde05977b3631167028862be2a173976ca11']); function privateIp(ip) { if (net.isIPv4(ip)) { const p = ip.split('.').map(Number); return p[0] === 10 || p[0] === 127 || p[0] === 0 || (p[0] === 172 && p[1] >= 16 && p[1] <= 31) || (p[0] === 192 && p[1] === 168) || (p[0] === 169 && p[1] === 254) || (p[0] === 100 && p[1] >= 64 && p[1] <= 127) || p[0] >= 224; } const l = ip.toLowerCase(); return l === '::1' || l === '::' || l.startsWith('fc') || l.startsWith('fd') || l.startsWith('fe80') || l.startsWith('::ffff:127.') || l.startsWith('::ffff:10.') || l.startsWith('::ffff:192.168.'); } const dnsCache = new Map(); async function hostOk(host) { host = String(host || '').toLowerCase(); if (!host || host === 'localhost' || host.endsWith('.local') || host.endsWith('.internal')) return false; if (net.isIP(host)) return !privateIp(host); if (dnsCache.has(host)) return dnsCache.get(host); let ok = false; try { const a = await dns.lookup(host, { all: true }); ok = a.length > 0 && a.every(x => !privateIp(x.address)); } catch (e) { ok = false; } dnsCache.set(host, ok); if (dnsCache.size > 5000) dnsCache.clear(); return ok; } function normalizeUrl(u) { u = String(u || '').trim(); if (!u) return null; if (!/^https?:\/\//i.test(u)) u = 'https://' + u; try { const x = new URL(u); if (!/^https?:$/.test(x.protocol)) return null; x.hash = ''; return x.toString(); } catch (e) { return null; } } // human-readable ABI strings in the site's own code ("function name(uint256 a) view returns (address)") -> ABI JSON function parseHumanAbi(src) { const out = []; const seen = new Set(); const re = /["'`](function|event) ([A-Za-z_][A-Za-z0-9_]*)\(([^)"'`]*)\)([^"'`]{0,200})["'`]/g; let m; const types = s => s.split(',').map(x => x.trim()).filter(Boolean).map(x => ({ type: x.split(/\s+/)[0].replace(/^uint$/, 'uint256').replace(/^int$/, 'int256'), indexed: /\bindexed\b/.test(x) })); while ((m = re.exec(src)) && out.length < 400) { const key = m[1] + m[2] + m[3]; if (seen.has(key)) continue; seen.add(key); const ins = types(m[3]); if (ins.some(t => !/^(address|bool|string|bytes\d*|u?int\d*|tuple|\w+\[\d*\])$/.test(t.type))) continue; const f = { type: m[1], name: m[2], inputs: ins.map(t => m[1] === 'event' ? { type: t.type, indexed: t.indexed } : { type: t.type }) }; if (m[1] === 'function') { const r = /returns\s*\(([^)]*)\)/.exec(m[4] || ''); f.outputs = r ? types(r[1]).map(t => ({ type: t.type })) : []; } out.push(f); } return out; } async function scan(url, opts) { const { chromium } = require('playwright-core'); const res = { url, ok: false, title: null, text: '', addresses: [], chainHints: {}, explorerLinks: [], abi: [], notes: [], screenshot: null }; const u = new URL(url); if (!await hostOk(u.hostname)) { res.notes.push('The website address is not a public internet host, so it was not opened.'); return res; } const browser = await chromium.launch({ executablePath: process.env.CHROMIUM_PATH || '/usr/bin/chromium', args: ['--no-sandbox', '--disable-dev-shm-usage', '--disable-gpu'] }); let scripts = ''; let scriptBytes = 0; try { const ctx = await browser.newContext({ viewport: { width: 1280, height: 860 }, userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0 Safari/537.36 WebWorthIt-check', javaScriptEnabled: true, acceptDownloads: false, permissions: [] }); await ctx.route('**/*', async route => { try { const ru = new URL(route.request().url()); if (!/^https?:$/.test(ru.protocol) || !await hostOk(ru.hostname)) return route.abort(); const t = route.request().resourceType(); if (t === 'media' || t === 'font') return route.abort(); return route.continue(); } catch (e) { return route.abort(); } }); const page = await ctx.newPage(); page.on('dialog', d => d.dismiss().catch(() => {})); page.on('response', async r => { try { if (r.request().resourceType() !== 'script' || scriptBytes > 6e6) return; const b = await r.text(); scriptBytes += b.length; scripts += '\n' + b.slice(0, 2e6); } catch (e) {} }); let resp = null; try { resp = await page.goto(url, { waitUntil: 'domcontentloaded', timeout: 30000 }); } catch (e) { res.notes.push('The page did not finish loading in 30 seconds: ' + String(e.message).split('\n')[0].slice(0, 120)); } await page.waitForLoadState('networkidle', { timeout: 8000 }).catch(() => {}); await page.waitForTimeout(1500); res.status = resp ? resp.status() : null; res.finalUrl = page.url(); res.title = (await page.title().catch(() => '')).slice(0, 200); res.text = String(await page.evaluate(() => document.body ? document.body.innerText : '').catch(() => '')).replace(/\n{3,}/g, '\n\n').slice(0, 20000); const html = String(await page.content().catch(() => '')).slice(0, 3e6); const links = await page.evaluate(() => Array.from(document.querySelectorAll('a[href]')).map(a => a.href).slice(0, 2000)).catch(() => []); const shot = await page.screenshot({ type: 'jpeg', quality: 72, fullPage: false }).catch(() => null); if (shot) res.screenshot = shot; // addresses: where they appear decides how likely each one is the project contract const score = new Map(); const bump = (a, n, why) => { a = a.toLowerCase(); if (COMMON.has(a)) return; const s = score.get(a) || { address: a, score: 0, seen: new Set() }; s.score += n; s.seen.add(why); score.set(a, s); }; for (const l of links) { let lu; try { lu = new URL(l); } catch (e) { continue; } const host = lu.hostname.replace(/^www\./, ''); const ch = EXPLORERS[host]; const m = lu.pathname.match(/\/(address|token)\/(0x[a-fA-F0-9]{40})/); if (ch && m) { res.explorerLinks.push({ chain: ch, address: m[2].toLowerCase(), url: l.slice(0, 300) }); bump(m[2], 6, 'explorer link (' + host + ')'); res.chainHints[ch] = (res.chainHints[ch] || 0) + 3; } } for (const a of res.text.match(ADDR_RE) || []) bump(a, 3, 'visible text'); for (const a of html.match(ADDR_RE) || []) bump(a, 1, 'page HTML'); // in the site's own code, the words just before an address say what it is (NEXT_PUBLIC_..._ADDRESS, contract: 0x..) or that // it is wallet-library plumbing (paymaster, multicall, permit2, router) that is never the project contract { const re = new RegExp(ADDR_RE.source, 'g'); let m, n = 0; while ((m = re.exec(scripts)) && n++ < 4000) { const ctx = scripts.slice(Math.max(0, m.index - 90), m.index); let s = 0.5; if (/(_ADDRESS|_ADDR|CONTRACT|contract|[Vv]ault|[Pp]roxy|[Pp]ool)[^,;]{0,40}$/.test(ctx)) s += 3; if (/(paymaster|wallet_|multicall|[Pp]ermit2?|[Rr]outer|WETH|[Ff]actory|[Ee]ntry[Pp]oint|[Ee]ns|safe[A-Z_]|chainlink|[Oo]racle)[^,;]{0,40}$/.test(ctx)) s -= 2; bump(m[0], s, s > 2 ? 'site code, named as the contract' : 'site scripts'); } } // nothing on the front page: open up to 2 of the site's own pages named like a contract / ledger / proof page (public pages only) if (!res.explorerLinks.length && ![...score.values()].some(s => s.score >= 3)) { const base = new URL(page.url()); const seenP = new Set([base.pathname]); const subs = links.filter(l => { try { const x = new URL(l); return x.hostname === base.hostname && /contract|ledger|proof|verify|audit|transparen|smart/i.test(x.pathname) && !seenP.has(x.pathname) && seenP.add(x.pathname); } catch (e) { return false; } }).slice(0, 2); for (const sl of subs) { try { await page.goto(sl, { waitUntil: 'domcontentloaded', timeout: 20000 }); await page.waitForLoadState('networkidle', { timeout: 6000 }).catch(() => {}); const t2 = String(await page.evaluate(() => document.body ? document.body.innerText : '').catch(() => '')).slice(0, 8000); const l2 = await page.evaluate(() => Array.from(document.querySelectorAll('a[href]')).map(a => a.href).slice(0, 1000)).catch(() => []); const pth = new URL(sl).pathname; for (const l of l2) { let lu; try { lu = new URL(l); } catch (e) { continue; } const host = lu.hostname.replace(/^www\./, ''); const ch = EXPLORERS[host]; const m = lu.pathname.match(/\/(address|token)\/(0x[a-fA-F0-9]{40})/); if (ch && m) { res.explorerLinks.push({ chain: ch, address: m[2].toLowerCase(), url: l.slice(0, 300), page: pth }); bump(m[2], 6, 'explorer link on ' + pth); res.chainHints[ch] = (res.chainHints[ch] || 0) + 3; } } for (const a of t2.match(ADDR_RE) || []) bump(a, 3, 'visible text on ' + pth); res.text += '\n\n[Page ' + pth + ']\n' + t2.slice(0, 5000); res.pagesRead = (res.pagesRead || []).concat([pth]); } catch (e) {} } } for (const [ch, re] of Object.entries(CHAIN_WORDS)) { const n = (res.text.match(re) || []).length; if (n) res.chainHints[ch] = (res.chainHints[ch] || 0) + n; } res.addresses = [...score.values()].sort((a, b) => b.score - a.score).slice(0, 25).map(s => ({ address: s.address, score: Math.round(s.score * 10) / 10, seen: [...s.seen] })); res.abi = parseHumanAbi(scripts + html); res.ok = true; } finally { await browser.close().catch(() => {}); } return res; } module.exports = { scan, normalizeUrl, hostOk, parseHumanAbi, COMMON };