diff options
Diffstat (limited to 'names/server.js')
| -rw-r--r-- | names/server.js | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/names/server.js b/names/server.js index f12b08f..d49c390 100644 --- a/names/server.js +++ b/names/server.js @@ -27,7 +27,7 @@ import { HDKey } from '@scure/bip32'; import { mnemonicToSeedSync } from '@scure/bip39'; import { lnBackend } from '../bridge/ln.js'; import { ArkManager } from '../coinosv3/src/ark/manager.js'; -import { fetchPreview } from './preview.js'; +import { fetchPreview, fetchLnurlPay } from './preview.js'; import { dnsUpdate } from './dns-update.js'; import { translate, translateDeepl, languages } from './translate.js'; import { decodeAddress, arkIdFromServerPubkey } from '../coinosv3/src/ark/proto.js'; @@ -1317,6 +1317,7 @@ function rateOk(ip, limit = 10) { // HTTP // --------------------------------------------------------------------------- +const lnurlFetches = new Map(); // /lnurlp-fetch: url → { d, until } const json = (body, status = 200) => { if (status >= 400) log(`→ ${status} ${JSON.stringify(body).slice(0, 120)}`); return new Response(JSON.stringify(body), { @@ -1460,6 +1461,26 @@ Bun.serve({ // A link in a post, as the card its page describes for itself (Open // Graph). Cached a day either way: a page with nothing to show is asked // again only after an hour. Public data; the CDN may cache it too. + // A Lightning address's pay parameters fetched for the wallet when the + // browser can't (a redirect without CORS: speed.app). GET ?url=https:// + // host/.well-known/lnurlp/name → the host's JSON, or 502. + if (url.pathname === '/lnurlp-fetch' && req.method === 'GET') { + if (!rateOk(ip, 60)) return json({ error: 'rate limited' }, 429); + const target = (url.searchParams.get('url') || '').trim(); + if (!/^https:\/\/\S{4,300}$/.test(target)) return json({ error: 'bad url' }, 400); + let hit = lnurlFetches.get(target); + if (!hit || Date.now() > hit.until) { + const d = await fetchLnurlPay(target).catch(() => null); + hit = { d, until: Date.now() + (d?.tag ? 300_000 : 30_000) }; + lnurlFetches.set(target, hit); + if (lnurlFetches.size > 2000) lnurlFetches.delete(lnurlFetches.keys().next().value); + } + return new Response(JSON.stringify(hit.d || { status: 'ERROR', reason: "couldn't reach that address" }), { + status: hit.d?.tag ? 200 : hit.d ? 404 : 502, + headers: { 'content-type': 'application/json', 'access-control-allow-origin': '*', 'cache-control': 'no-store' }, + }); + } + if (url.pathname === '/preview' && req.method === 'GET') { if (!rateOk(ip, 120)) return json({ error: 'rate limited' }, 429); const target = (url.searchParams.get('url') || '').trim(); |
