aboutsummaryrefslogtreecommitdiffstats
path: root/lib/notifier.ts
blob: adc094570c91f61f39d3b67e134159dd52108f29 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import config from "$config";
import dns from "node:dns";

// POST /confirm credits deposits, so only the wallet-notify scripts may call
// it. They run inside the bitcoin and liquid node containers and connect to
// us directly, so the TCP peer is one of those nodes (or ourselves, for the
// catch-up sweep in lib/payments.ts). Checking the peer address, rather than
// the absence of proxy headers, also stops requests relayed by another
// internal service such as the UI's server-side forwarding routes.
const hosts = [config.bitcoin?.host, config.liquid?.host].filter(
  (h): h is string => typeof h === "string" && h.length > 0,
);

let notifiers = new Set<string>();
const resolve = async () => {
  const next = new Set<string>();
  for (const host of hosts) {
    try {
      for (const { address } of await dns.promises.lookup(host.split(":")[0], {
        all: true,
      }))
        next.add(address);
    } catch {}
  }
  if (next.size) notifiers = next;
};
await resolve();
setInterval(resolve, 30000).unref();

const loopback = (ip: string) => ip === "::1" || ip.startsWith("127.");

export const isNotifier = (req): boolean => {
  const peer = (req.socket?.remoteAddress || "").replace(/^::ffff:/, "");
  return loopback(peer) || notifiers.has(peer);
};