@profullstack/threatcrush 0.12.1 → 0.12.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/index.js CHANGED
@@ -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)) {
@@ -26072,6 +26073,55 @@ function resolveBinPath() {
26072
26073
  function isRoot2() {
26073
26074
  return typeof process.getuid === "function" && process.getuid() === 0;
26074
26075
  }
26076
+ function reexecWithSudo(args) {
26077
+ if ((0, import_node_child_process9.spawnSync)("sudo", ["--version"], { stdio: "pipe" }).status !== 0) return false;
26078
+ console.log(source_default.dim(" This needs root. Re-running under sudo...\n"));
26079
+ const bin = resolveBinPath();
26080
+ const result = (0, import_node_child_process9.spawnSync)("sudo", [bin, ...args], { stdio: "inherit" });
26081
+ return result.status === 0;
26082
+ }
26083
+ function callerHome() {
26084
+ const user = process.env.SUDO_USER;
26085
+ if (!user) return null;
26086
+ try {
26087
+ const line = (0, import_node_child_process9.execSync)(`getent passwd ${user}`, { encoding: "utf-8" }).trim();
26088
+ const home = line.split(":")[5];
26089
+ return home && (0, import_node_fs30.existsSync)(home) ? home : null;
26090
+ } catch {
26091
+ return null;
26092
+ }
26093
+ }
26094
+ function stopUserModeDaemon() {
26095
+ const home = callerHome();
26096
+ if (!home) return;
26097
+ const runDir = (0, import_node_path21.join)(home, ".threatcrush", "run");
26098
+ const pidFile = (0, import_node_path21.join)(runDir, "threatcrushd.pid");
26099
+ const socket = (0, import_node_path21.join)(runDir, "threatcrushd.sock");
26100
+ if (!(0, import_node_fs30.existsSync)(pidFile) && !(0, import_node_fs30.existsSync)(socket)) return;
26101
+ let pid = null;
26102
+ try {
26103
+ pid = Number.parseInt((0, import_node_fs30.readFileSync)(pidFile, "utf-8").trim(), 10);
26104
+ } catch {
26105
+ pid = null;
26106
+ }
26107
+ if (pid && Number.isFinite(pid)) {
26108
+ try {
26109
+ process.kill(pid, "SIGTERM");
26110
+ console.log(source_default.dim(` Stopped the user-mode daemon (pid ${pid}).`));
26111
+ } catch {
26112
+ }
26113
+ }
26114
+ for (const path of [socket, pidFile]) {
26115
+ try {
26116
+ if ((0, import_node_fs30.existsSync)(path)) (0, import_node_fs30.rmSync)(path);
26117
+ } catch {
26118
+ }
26119
+ }
26120
+ }
26121
+ function unitIsActive() {
26122
+ const result = (0, import_node_child_process9.spawnSync)("systemctl", ["is-active", "--quiet", DAEMON_UNIT_NAME], { stdio: "pipe" });
26123
+ return result.status === 0;
26124
+ }
26075
26125
  async function installServiceCommand() {
26076
26126
  banner();
26077
26127
  if (process.platform !== "linux") {
@@ -26079,22 +26129,33 @@ async function installServiceCommand() {
26079
26129
  return;
26080
26130
  }
26081
26131
  if (!isRoot2()) {
26082
- console.log(source_default.red(" Must run as root (try `sudo threatcrush install-service`)."));
26132
+ if (reexecWithSudo(["install-service"])) return;
26133
+ console.log(source_default.red(" Needs root and sudo is unavailable or was declined."));
26134
+ console.log(source_default.dim(" Run this as root: threatcrush install-service"));
26083
26135
  return;
26084
26136
  }
26085
26137
  const unit = resolveTemplate().replace("{{BIN_PATH}}", resolveBinPath());
26086
26138
  (0, import_node_fs30.writeFileSync)(UNIT_PATH, unit, { mode: 420 });
26087
26139
  console.log(source_default.green(` \u2713 Installed unit file: ${UNIT_PATH}`));
26088
26140
  ensureSystemDirs();
26141
+ stopUserModeDaemon();
26089
26142
  try {
26090
26143
  (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"));
26144
+ (0, import_node_child_process9.execSync)(`systemctl enable --now ${DAEMON_UNIT_NAME}`, { stdio: "inherit" });
26095
26145
  } catch (err) {
26096
26146
  console.log(source_default.yellow(` ! systemctl error: ${err.message}`));
26147
+ console.log(source_default.dim(` Logs: journalctl -u ${DAEMON_UNIT_NAME} -n 50`));
26148
+ return;
26097
26149
  }
26150
+ if (unitIsActive()) {
26151
+ console.log(source_default.green(" \u2713 Service enabled on boot and running now, as root."));
26152
+ console.log(source_default.dim(" It can write firewall rules, so bans from the dashboard will apply."));
26153
+ console.log(source_default.dim(` Logs: journalctl -u ${DAEMON_UNIT_NAME} -f`));
26154
+ } else {
26155
+ console.log(source_default.yellow(" ! The unit is installed but did not come up."));
26156
+ console.log(source_default.dim(` Logs: journalctl -u ${DAEMON_UNIT_NAME} -n 50`));
26157
+ }
26158
+ console.log();
26098
26159
  }
26099
26160
  function ensureSystemDirs() {
26100
26161
  const dirs = [
@@ -26136,7 +26197,9 @@ async function uninstallServiceCommand() {
26136
26197
  return;
26137
26198
  }
26138
26199
  if (!isRoot2()) {
26139
- console.log(source_default.red(" Must run as root (try `sudo threatcrush uninstall-service`)."));
26200
+ if (reexecWithSudo(["uninstall-service"])) return;
26201
+ console.log(source_default.red(" Needs root and sudo is unavailable or was declined."));
26202
+ console.log(source_default.dim(" Run this as root: threatcrush uninstall-service"));
26140
26203
  return;
26141
26204
  }
26142
26205
  try {
@@ -27496,9 +27559,24 @@ async function refreshDaemonAfterUpgrade() {
27496
27559
  });
27497
27560
  if (plan.action === "none") return;
27498
27561
  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}
27562
+ console.log(source_default.dim(" Restarting the systemd service onto the new build...\n"));
27563
+ const restarted = restartSystemdDaemon();
27564
+ if (!restarted) {
27565
+ console.log(source_default.yellow(" ! Could not restart the service; it is still on the old build."));
27566
+ console.log(source_default.dim(` Restart it with: ${plan.command}
27501
27567
  `));
27568
+ return;
27569
+ }
27570
+ const after2 = await liveDaemon();
27571
+ const stale = staleDaemonWarning(PKG_VERSION, after2.version);
27572
+ if (stale) {
27573
+ console.log(source_default.yellow(` ${stale[0]}`));
27574
+ for (const line of stale.slice(1)) console.log(source_default.dim(` ${line}`));
27575
+ console.log();
27576
+ } else {
27577
+ console.log(source_default.green(` \u2713 threatcrushd restarted on ${after2.version ?? PKG_VERSION}.
27578
+ `));
27579
+ }
27502
27580
  return;
27503
27581
  }
27504
27582
  console.log(source_default.dim(" Restarting threatcrushd onto the new build...\n"));
@@ -27513,6 +27591,30 @@ async function refreshDaemonAfterUpgrade() {
27513
27591
  console.log();
27514
27592
  }
27515
27593
  }
27594
+ async function promoteUnprivilegedDaemon() {
27595
+ const client = new IpcClient();
27596
+ let warning;
27597
+ try {
27598
+ await client.connect();
27599
+ warning = (await client.blocklist()).warning;
27600
+ } catch {
27601
+ return;
27602
+ } finally {
27603
+ client.close();
27604
+ }
27605
+ if (!warning) return;
27606
+ console.log(source_default.yellow(` ! ${warning}`));
27607
+ console.log(source_default.dim(" Auto-defence is on, so installing the system service to give it that privilege.\n"));
27608
+ await installServiceCommand();
27609
+ }
27610
+ function restartSystemdDaemon() {
27611
+ const asRoot = typeof process.getuid === "function" && process.getuid() === 0;
27612
+ const argv = asRoot ? ["systemctl", "restart", DAEMON_UNIT] : ["sudo", "systemctl", "restart", DAEMON_UNIT];
27613
+ if (!asRoot && (0, import_node_child_process12.spawnSync)("sudo", ["--version"], { stdio: "pipe" }).status !== 0) return false;
27614
+ const [cmd, ...args] = argv;
27615
+ if ((0, import_node_child_process12.spawnSync)(cmd, args, { stdio: "inherit" }).status !== 0) return false;
27616
+ return (0, import_node_child_process12.spawnSync)("systemctl", ["is-active", "--quiet", DAEMON_UNIT], { stdio: "pipe" }).status === 0;
27617
+ }
27516
27618
  function execQuiet(cmd) {
27517
27619
  try {
27518
27620
  return (0, import_node_child_process12.execSync)(cmd, { encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"] });
@@ -27753,6 +27855,7 @@ program2.command("update").description("Update ThreatCrush CLI and installed bun
27753
27855
  }
27754
27856
  await reportIfStillStale();
27755
27857
  await refreshDaemonAfterUpgrade();
27858
+ await promoteUnprivilegedDaemon();
27756
27859
  console.log(source_default.dim(" If a `threatcrush monitor --tui` is open, quit and relaunch it.\n"));
27757
27860
  } catch (err) {
27758
27861
  console.log(source_default.red("\n \u2717 Update failed. Try manually:\n"));