@profullstack/threatcrush 0.15.0 → 0.15.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/daemon.js CHANGED
@@ -17997,6 +17997,7 @@ function defaultGateways() {
17997
17997
  }
17998
17998
 
17999
17999
  // src/core/threat-feeds/index.ts
18000
+ var CARRY_MAX_MS = 24 * 36e5;
18000
18001
  var FEEDS = {
18001
18002
  /** POST `{ query: 'get_iocs', days }` with an `Auth-Key` header. */
18002
18003
  threatfoxApi: "https://threatfox-api.abuse.ch/api/v1/",
@@ -18220,11 +18221,14 @@ function buildFeedSet(parts, allow, sources, now = /* @__PURE__ */ new Date()) {
18220
18221
  }
18221
18222
  return { fetched_at: now.toISOString(), ips, domains, sources, skipped_shared: skipped };
18222
18223
  }
18224
+ var FETCH_ATTEMPTS = 3;
18225
+ var FatalFeedError = class extends Error {
18226
+ };
18223
18227
  function redact(message, key) {
18224
18228
  if (!key) return message;
18225
18229
  return message.split(key).join("<auth-key>").split(encodeURIComponent(key)).join("<auth-key>");
18226
18230
  }
18227
- async function fetchFeeds(settings, fetchImpl = fetch) {
18231
+ async function fetchFeeds(settings, fetchImpl = fetch, retryDelayMs = 5e3, previous = null, now = /* @__PURE__ */ new Date()) {
18228
18232
  const sources = [];
18229
18233
  const parts = [];
18230
18234
  const key = settings.authKey?.trim() || void 0;
@@ -18236,17 +18240,31 @@ async function fetchFeeds(settings, fetchImpl = fetch) {
18236
18240
  }
18237
18241
  try {
18238
18242
  const extra = request2.init?.headers ?? {};
18239
- const res = await fetchImpl(request2.url, { ...request2.init, signal: AbortSignal.timeout(6e4), headers: { ...ua, ...extra } });
18240
- if (res.status === 401 || res.status === 403) throw new Error(`HTTP ${res.status}: abuse.ch refused the Auth-Key`);
18241
- if (!res.ok) throw new Error(`HTTP ${res.status}`);
18242
- const text = await res.text();
18243
- const type = res.headers.get("content-type") ?? "";
18244
- if (type.includes("text/html") || /^\s*<(!doctype|html)/i.test(text)) throw new Error(`HTTP ${res.status} but an HTML page, not feed data`);
18245
- const part = parse(text);
18243
+ let part;
18244
+ for (let attempt = 1; ; attempt++) {
18245
+ try {
18246
+ const res = await fetchImpl(request2.url, { ...request2.init, signal: AbortSignal.timeout(6e4), headers: { ...ua, ...extra } });
18247
+ if (res.status === 401 || res.status === 403) throw new FatalFeedError(`HTTP ${res.status}: abuse.ch refused the Auth-Key`);
18248
+ if (res.status >= 400 && res.status < 500) throw new FatalFeedError(`HTTP ${res.status}`);
18249
+ if (!res.ok) throw new Error(`HTTP ${res.status}`);
18250
+ const text = await res.text();
18251
+ const type = res.headers.get("content-type") ?? "";
18252
+ if (type.includes("text/html") || /^\s*<(!doctype|html)/i.test(text)) throw new FatalFeedError(`HTTP ${res.status} but an HTML page, not feed data`);
18253
+ part = parse(text);
18254
+ break;
18255
+ } catch (err) {
18256
+ const retryable = !(err instanceof FatalFeedError) && !/^ThreatFox API: /.test(err.message);
18257
+ if (!retryable || attempt >= FETCH_ATTEMPTS) throw err;
18258
+ await new Promise((r) => setTimeout(r, retryDelayMs * attempt));
18259
+ }
18260
+ }
18246
18261
  const count = (part.ips?.size ?? 0) + (part.domains?.size ?? 0);
18247
18262
  if (count === 0 && !emptyOk) throw new Error("no entries in the response");
18263
+ for (const map of [part.ips, part.domains]) {
18264
+ for (const [k, ind] of map ?? []) map.set(k, { ...ind, source: feed });
18265
+ }
18248
18266
  parts.push(part);
18249
- sources.push({ feed, ok: true, count });
18267
+ sources.push({ feed, ok: true, count, last_ok_at: now.toISOString() });
18250
18268
  } catch (err) {
18251
18269
  sources.push({ feed, ok: false, count: 0, error: redact(err.message, key) });
18252
18270
  }
@@ -18266,8 +18284,26 @@ async function fetchFeeds(settings, fetchImpl = fetch) {
18266
18284
  // Feodo has been near-empty since 2025; an empty list there is real.
18267
18285
  get2("feodo", { url: FEEDS.feodoIps }, (t) => ({ ips: parseIpList(t, "feodo") }), true)
18268
18286
  ]);
18287
+ carryForward(previous, sources, parts, now);
18269
18288
  sources.sort((a, b) => a.feed.localeCompare(b.feed));
18270
- return buildFeedSet(parts, settings.allow, sources);
18289
+ return buildFeedSet(parts, settings.allow, sources, now);
18290
+ }
18291
+ function carryForward(previous, sources, parts, now) {
18292
+ if (!previous) return;
18293
+ for (const s of sources) {
18294
+ if (s.ok || s.error === NEEDS_KEY) continue;
18295
+ const prev = previous.sources?.find((p) => p.feed === s.feed);
18296
+ const lastOk = prev?.last_ok_at;
18297
+ if (!lastOk || now.getTime() - Date.parse(lastOk) > CARRY_MAX_MS) continue;
18298
+ const part = { ips: /* @__PURE__ */ new Map(), domains: /* @__PURE__ */ new Map() };
18299
+ for (const [ip, ind] of Object.entries(previous.ips)) if (ind.source === s.feed) part.ips.set(ip, ind);
18300
+ for (const [d, ind] of Object.entries(previous.domains)) if (ind.source === s.feed) part.domains.set(d, ind);
18301
+ const kept = part.ips.size + part.domains.size;
18302
+ s.last_ok_at = lastOk;
18303
+ if (!kept) continue;
18304
+ parts.push(part);
18305
+ s.carried = kept;
18306
+ }
18271
18307
  }
18272
18308
  function readFeedCache(path) {
18273
18309
  try {
@@ -26911,7 +26947,7 @@ var ThreatFeeds = class {
26911
26947
  }
26912
26948
  async refresh() {
26913
26949
  try {
26914
- const set = await fetchFeeds(this.opts.settings, this.opts.fetchImpl);
26950
+ const set = await fetchFeeds(this.opts.settings, this.opts.fetchImpl, this.opts.retryDelayMs, this.set, new Date(this.now()));
26915
26951
  const failed = set.sources.filter((s) => !s.ok);
26916
26952
  if (failed.length === set.sources.length) {
26917
26953
  this.opts.log(`[threat-feeds] refresh failed, keeping previous list: ${failed.map((s) => `${s.feed}: ${s.error}`).join("; ")}`);
@@ -26939,7 +26975,7 @@ var ThreatFeeds = class {
26939
26975
  this.blocking = ok;
26940
26976
  mode = ok ? "blocking egress" : "detecting (nft load failed)";
26941
26977
  }
26942
- const failed = (set.sources ?? []).filter((s) => !s.ok).map((s) => s.feed);
26978
+ const failed = (set.sources ?? []).filter((s) => !s.ok).map((s) => s.carried ? `${s.feed}: kept ${s.carried} from ${s.last_ok_at?.slice(11, 16)} UTC` : s.feed);
26943
26979
  const failing = failed.length ? `; ${failed.length}/${set.sources.length} feeds failing (${failed.join(", ")}), see \`threatcrush feeds\`` : "";
26944
26980
  const detail = `${ips.length} IPs, ${domains} domains, ${mode}${this.writeResolverFiles(set)}${failing}`;
26945
26981
  this.opts.log(`[threat-feeds] ${from === "cache" ? "loaded cached" : "refreshed"} list: ${detail} (${set.skipped_shared} shared/protected entries skipped)`);