@profullstack/threatcrush 0.12.1 → 0.12.3

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/index.js CHANGED
@@ -11246,7 +11246,7 @@ function bansPanel(parent, state, theme, options) {
11246
11246
  if (fw.warning) {
11247
11247
  panel.text("NOT PRIVILEGED", { fg: severityColors.critical, bold: true });
11248
11248
  panel.label(fw.warning, { fg: severityColors.high });
11249
- panel.label("sudo threatcrush install-service", { fg: theme.muted });
11249
+ panel.label("threatcrush install-service", { fg: theme.muted });
11250
11250
  panel.spacer(1);
11251
11251
  }
11252
11252
  if (state.bans.length === 0) {
@@ -25433,7 +25433,7 @@ function explainBlockFailure(err, backend) {
25433
25433
  const denied = /not permitted|EACCES|EPERM|permission denied|must be root/i.test(raw);
25434
25434
  if (!denied) return `${raw} (backend: ${backend})`;
25435
25435
  const who = typeof process.getuid === "function" && process.getuid() === 0 ? "as root" : `as uid ${typeof process.getuid === "function" ? process.getuid() : "?"}`;
25436
- return `threatcrushd is running ${who} and cannot manage ${backend}. A ban is written by the daemon, not by whoever asked for it \u2014 run the daemon as root: sudo threatcrush install-service && sudo systemctl enable --now threatcrushd`;
25436
+ return `threatcrushd is running ${who} and cannot manage ${backend}. A ban is written by the daemon, not by whoever asked for it. Fix it with one command, which asks for root itself: threatcrush install-service`;
25437
25437
  }
25438
25438
  var RemediationManager = class {
25439
25439
  constructor(adapter, bus2, config) {
@@ -26048,6 +26048,7 @@ var import_node_child_process9 = require("child_process");
26048
26048
  var import_node_fs30 = require("fs");
26049
26049
  var import_node_path21 = require("path");
26050
26050
  var UNIT_PATH = "/etc/systemd/system/threatcrushd.service";
26051
+ var DAEMON_UNIT_NAME = "threatcrushd.service";
26051
26052
  function resolveTemplate() {
26052
26053
  const templatePath = (0, import_node_path21.join)(__dirname, "systemd", "threatcrushd.service");
26053
26054
  if (!(0, import_node_fs30.existsSync)(templatePath)) {
@@ -26069,9 +26070,75 @@ function resolveBinPath() {
26069
26070
  return "threatcrush";
26070
26071
  }
26071
26072
  }
26073
+ function stableNodePath(execPath) {
26074
+ const stable = execPath.replace(/(\/installs\/node\/)[^/]+\//, "$1latest/");
26075
+ if (stable === execPath) return execPath;
26076
+ return (0, import_node_fs30.existsSync)(stable) ? stable : execPath;
26077
+ }
26078
+ function looksLikeNodeScript(path) {
26079
+ try {
26080
+ const head = (0, import_node_fs30.readFileSync)(path, "utf-8").slice(0, 120);
26081
+ return /^#!.*\bnode\b/.test(head);
26082
+ } catch {
26083
+ return false;
26084
+ }
26085
+ }
26086
+ function execStartCommand(binPath, nodePath, isNodeScript) {
26087
+ return isNodeScript ? `${nodePath} ${binPath} daemon` : `${binPath} daemon`;
26088
+ }
26072
26089
  function isRoot2() {
26073
26090
  return typeof process.getuid === "function" && process.getuid() === 0;
26074
26091
  }
26092
+ function reexecWithSudo(args) {
26093
+ if ((0, import_node_child_process9.spawnSync)("sudo", ["--version"], { stdio: "pipe" }).status !== 0) return false;
26094
+ console.log(source_default.dim(" This needs root \u2014 sudo will ask for your password.\n"));
26095
+ const bin = resolveBinPath();
26096
+ const argv = looksLikeNodeScript(bin) ? [stableNodePath(process.execPath), bin, ...args] : [bin, ...args];
26097
+ const result = (0, import_node_child_process9.spawnSync)("sudo", argv, { stdio: "inherit" });
26098
+ return result.status === 0;
26099
+ }
26100
+ function callerHome() {
26101
+ const user = process.env.SUDO_USER;
26102
+ if (!user) return null;
26103
+ try {
26104
+ const line = (0, import_node_child_process9.execSync)(`getent passwd ${user}`, { encoding: "utf-8" }).trim();
26105
+ const home = line.split(":")[5];
26106
+ return home && (0, import_node_fs30.existsSync)(home) ? home : null;
26107
+ } catch {
26108
+ return null;
26109
+ }
26110
+ }
26111
+ function stopUserModeDaemon() {
26112
+ const home = callerHome();
26113
+ if (!home) return;
26114
+ const runDir = (0, import_node_path21.join)(home, ".threatcrush", "run");
26115
+ const pidFile = (0, import_node_path21.join)(runDir, "threatcrushd.pid");
26116
+ const socket = (0, import_node_path21.join)(runDir, "threatcrushd.sock");
26117
+ if (!(0, import_node_fs30.existsSync)(pidFile) && !(0, import_node_fs30.existsSync)(socket)) return;
26118
+ let pid = null;
26119
+ try {
26120
+ pid = Number.parseInt((0, import_node_fs30.readFileSync)(pidFile, "utf-8").trim(), 10);
26121
+ } catch {
26122
+ pid = null;
26123
+ }
26124
+ if (pid && Number.isFinite(pid)) {
26125
+ try {
26126
+ process.kill(pid, "SIGTERM");
26127
+ console.log(source_default.dim(` Stopped the user-mode daemon (pid ${pid}).`));
26128
+ } catch {
26129
+ }
26130
+ }
26131
+ for (const path of [socket, pidFile]) {
26132
+ try {
26133
+ if ((0, import_node_fs30.existsSync)(path)) (0, import_node_fs30.rmSync)(path);
26134
+ } catch {
26135
+ }
26136
+ }
26137
+ }
26138
+ function unitIsActive() {
26139
+ const result = (0, import_node_child_process9.spawnSync)("systemctl", ["is-active", "--quiet", DAEMON_UNIT_NAME], { stdio: "pipe" });
26140
+ return result.status === 0;
26141
+ }
26075
26142
  async function installServiceCommand() {
26076
26143
  banner();
26077
26144
  if (process.platform !== "linux") {
@@ -26079,22 +26146,40 @@ async function installServiceCommand() {
26079
26146
  return;
26080
26147
  }
26081
26148
  if (!isRoot2()) {
26082
- console.log(source_default.red(" Must run as root (try `sudo threatcrush install-service`)."));
26149
+ if (reexecWithSudo(["install-service"])) return;
26150
+ console.log(source_default.red(" Needs root and sudo is unavailable or was declined."));
26151
+ console.log(source_default.dim(" Run this as root: threatcrush install-service"));
26083
26152
  return;
26084
26153
  }
26085
- const unit = resolveTemplate().replace("{{BIN_PATH}}", resolveBinPath());
26154
+ const binPath = resolveBinPath();
26155
+ const exec = execStartCommand(
26156
+ binPath,
26157
+ stableNodePath(process.execPath),
26158
+ looksLikeNodeScript(binPath)
26159
+ );
26160
+ const unit = resolveTemplate().replace("{{BIN_PATH}} daemon", exec).replace("{{BIN_PATH}}", binPath);
26086
26161
  (0, import_node_fs30.writeFileSync)(UNIT_PATH, unit, { mode: 420 });
26087
26162
  console.log(source_default.green(` \u2713 Installed unit file: ${UNIT_PATH}`));
26163
+ console.log(source_default.dim(` ExecStart=${exec}`));
26088
26164
  ensureSystemDirs();
26165
+ stopUserModeDaemon();
26089
26166
  try {
26090
26167
  (0, import_node_child_process9.execSync)("systemctl daemon-reload", { stdio: "inherit" });
26091
- (0, import_node_child_process9.execSync)("systemctl enable threatcrushd.service", { stdio: "inherit" });
26092
- console.log(source_default.green(" \u2713 Service enabled on boot."));
26093
- console.log(source_default.dim(" Start now with: systemctl start threatcrushd"));
26094
- console.log(source_default.dim(" View logs with: journalctl -u threatcrushd -f"));
26168
+ (0, import_node_child_process9.execSync)(`systemctl enable --now ${DAEMON_UNIT_NAME}`, { stdio: "inherit" });
26095
26169
  } catch (err) {
26096
26170
  console.log(source_default.yellow(` ! systemctl error: ${err.message}`));
26171
+ console.log(source_default.dim(` Logs: journalctl -u ${DAEMON_UNIT_NAME} -n 50`));
26172
+ return;
26097
26173
  }
26174
+ if (unitIsActive()) {
26175
+ console.log(source_default.green(" \u2713 Service enabled on boot and running now, as root."));
26176
+ console.log(source_default.dim(" It can write firewall rules, so bans from the dashboard will apply."));
26177
+ console.log(source_default.dim(` Logs: journalctl -u ${DAEMON_UNIT_NAME} -f`));
26178
+ } else {
26179
+ console.log(source_default.yellow(" ! The unit is installed but did not come up."));
26180
+ console.log(source_default.dim(` Logs: journalctl -u ${DAEMON_UNIT_NAME} -n 50`));
26181
+ }
26182
+ console.log();
26098
26183
  }
26099
26184
  function ensureSystemDirs() {
26100
26185
  const dirs = [
@@ -26136,7 +26221,9 @@ async function uninstallServiceCommand() {
26136
26221
  return;
26137
26222
  }
26138
26223
  if (!isRoot2()) {
26139
- console.log(source_default.red(" Must run as root (try `sudo threatcrush uninstall-service`)."));
26224
+ if (reexecWithSudo(["uninstall-service"])) return;
26225
+ console.log(source_default.red(" Needs root and sudo is unavailable or was declined."));
26226
+ console.log(source_default.dim(" Run this as root: threatcrush uninstall-service"));
26140
26227
  return;
26141
26228
  }
26142
26229
  try {
@@ -27496,9 +27583,24 @@ async function refreshDaemonAfterUpgrade() {
27496
27583
  });
27497
27584
  if (plan.action === "none") return;
27498
27585
  if (plan.action === "manual") {
27499
- console.log(source_default.yellow(" ! The daemon is managed by systemd and is still on the old build."));
27500
- console.log(source_default.dim(` Restart it with: ${plan.command}
27586
+ console.log(source_default.dim(" Restarting the systemd service onto the new build...\n"));
27587
+ const restarted = restartSystemdDaemon();
27588
+ if (!restarted) {
27589
+ console.log(source_default.yellow(" ! Could not restart the service; it is still on the old build."));
27590
+ console.log(source_default.dim(` Restart it with: ${plan.command}
27501
27591
  `));
27592
+ return;
27593
+ }
27594
+ const after2 = await liveDaemon();
27595
+ const stale = staleDaemonWarning(PKG_VERSION, after2.version);
27596
+ if (stale) {
27597
+ console.log(source_default.yellow(` ${stale[0]}`));
27598
+ for (const line of stale.slice(1)) console.log(source_default.dim(` ${line}`));
27599
+ console.log();
27600
+ } else {
27601
+ console.log(source_default.green(` \u2713 threatcrushd restarted on ${after2.version ?? PKG_VERSION}.
27602
+ `));
27603
+ }
27502
27604
  return;
27503
27605
  }
27504
27606
  console.log(source_default.dim(" Restarting threatcrushd onto the new build...\n"));
@@ -27513,6 +27615,30 @@ async function refreshDaemonAfterUpgrade() {
27513
27615
  console.log();
27514
27616
  }
27515
27617
  }
27618
+ async function promoteUnprivilegedDaemon() {
27619
+ const client = new IpcClient();
27620
+ let warning;
27621
+ try {
27622
+ await client.connect();
27623
+ warning = (await client.blocklist()).warning;
27624
+ } catch {
27625
+ return;
27626
+ } finally {
27627
+ client.close();
27628
+ }
27629
+ if (!warning) return;
27630
+ console.log(source_default.yellow(` ! ${warning}`));
27631
+ console.log(source_default.dim(" Auto-defence is on, so installing the system service to give it that privilege.\n"));
27632
+ await installServiceCommand();
27633
+ }
27634
+ function restartSystemdDaemon() {
27635
+ const asRoot = typeof process.getuid === "function" && process.getuid() === 0;
27636
+ const argv = asRoot ? ["systemctl", "restart", DAEMON_UNIT] : ["sudo", "systemctl", "restart", DAEMON_UNIT];
27637
+ if (!asRoot && (0, import_node_child_process12.spawnSync)("sudo", ["--version"], { stdio: "pipe" }).status !== 0) return false;
27638
+ const [cmd, ...args] = argv;
27639
+ if ((0, import_node_child_process12.spawnSync)(cmd, args, { stdio: "inherit" }).status !== 0) return false;
27640
+ return (0, import_node_child_process12.spawnSync)("systemctl", ["is-active", "--quiet", DAEMON_UNIT], { stdio: "pipe" }).status === 0;
27641
+ }
27516
27642
  function execQuiet(cmd) {
27517
27643
  try {
27518
27644
  return (0, import_node_child_process12.execSync)(cmd, { encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"] });
@@ -27753,6 +27879,7 @@ program2.command("update").description("Update ThreatCrush CLI and installed bun
27753
27879
  }
27754
27880
  await reportIfStillStale();
27755
27881
  await refreshDaemonAfterUpgrade();
27882
+ await promoteUnprivilegedDaemon();
27756
27883
  console.log(source_default.dim(" If a `threatcrush monitor --tui` is open, quit and relaunch it.\n"));
27757
27884
  } catch (err) {
27758
27885
  console.log(source_default.red("\n \u2717 Update failed. Try manually:\n"));