whatsrunning 0.4.0 → 0.6.0

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/README.md CHANGED
@@ -15,20 +15,31 @@ Or install it so `whatsrunning` is always on your PATH:
15
15
  npm install -g whatsrunning
16
16
  ```
17
17
 
18
+ > **0.6 known issues:** stop `spoof` with **Ctrl-C** and wait for "Network
19
+ > restored" — don't close the terminal while it runs, or the network restore
20
+ > can be skipped. On Linux with Docker/ufw, forwarded traffic may be dropped.
21
+ > Details: [Known issues](https://github.com/dmoney25632/whatsrunning#known-issues-in-06-read-before-running-spoof).
22
+
18
23
  ## Requires Python 3.9+
19
24
 
20
25
  whatsrunning's engine is Python — packet capture needs raw sockets and
21
26
  `libpcap`, which Node can't do cleanly. **This npm package is a launcher.** On
22
- the first run it finds Python 3, creates a private virtual environment under
23
- `~/.cache/whatsrunning`, installs the bundled engine into it (needs internet
24
- once), and from then on just runs it. You need:
25
-
26
- - **Python 3.9+** on your PATH (`brew install python`, or
27
- `sudo apt install python3 python3-venv`)
28
- - **macOS or Linux** (Windows isn't supported yet — it needs Npcap)
29
-
30
- Root is required for capture (`sudo`), the same as Wireshark/tcpdump. The first
31
- `sudo` run does its own one-time setup under root's home.
27
+ the first run it finds Python 3, creates a private virtual environment
28
+ (`~/.cache/whatsrunning` on macOS/Linux, `%LOCALAPPDATA%\whatsrunning\venv` on
29
+ Windows), installs the bundled engine into it (needs internet once), and from
30
+ then on just runs it. You need:
31
+
32
+ - **Python 3.9+** on your PATH (`brew install python`;
33
+ `sudo apt install python3 python3-venv`; or python.org on Windows with
34
+ "Add to PATH")
35
+ - **macOS, Linux or Windows 11**. On **Windows**, packet capture also needs
36
+ [Npcap](https://npcap.com) — install it with the default options (you do
37
+ **not** need "WinPcap API-compatible mode" or "Support raw 802.11 traffic").
38
+
39
+ Capture needs elevation, the same as Wireshark/tcpdump: `sudo` on macOS/Linux,
40
+ or an **Administrator** terminal on Windows. On macOS/Linux the first `sudo`
41
+ run does its own one-time setup; on Windows the setup lives under your user's
42
+ `%LOCALAPPDATA%`.
32
43
 
33
44
  ## What it does
34
45
 
@@ -10,34 +10,58 @@
10
10
  * run, installs the bundled engine wheel into it, then runs the real CLI and
11
11
  * passes your arguments straight through.
12
12
  *
13
- * Requires: Python 3.9+ on PATH. macOS / Linux only (Windows needs Npcap).
13
+ * Works on macOS, Linux and Windows 11. Requires Python 3.9+ on PATH. On
14
+ * Windows, packet capture additionally needs Npcap (https://npcap.com).
14
15
  */
15
16
 
16
- const { spawnSync } = require("child_process");
17
+ const { spawn, spawnSync } = require("child_process");
17
18
  const fs = require("fs");
18
19
  const os = require("os");
19
20
  const path = require("path");
20
21
 
22
+ const isWin = process.platform === "win32";
23
+ // Commands that capture packets — they need admin/root, and Npcap on Windows.
24
+ const CAPTURE_CMDS = new Set(["spoof", "discover"]);
25
+
21
26
  function log(msg) {
22
27
  process.stderr.write(`[whatsrunning] ${msg}\n`);
23
28
  }
24
29
 
30
+ // Candidate Python launchers, each as an argv prefix. On Windows the `py`
31
+ // launcher and the real python come before the Microsoft Store `python.exe`
32
+ // alias stub (which just opens the Store); we detect a working one by actually
33
+ // running it and checking the version prints.
34
+ function pythonCandidates() {
35
+ if (isWin) return [["py", "-3"], ["python"], ["python3"]];
36
+ return [["python3"], ["python"]];
37
+ }
38
+
25
39
  function findPython() {
26
- for (const cand of ["python3", "python"]) {
27
- const r = spawnSync(cand, ["-c", "import sys;print('%d.%d'%sys.version_info[:2])"],
28
- { encoding: "utf8" });
40
+ for (const prefix of pythonCandidates()) {
41
+ const r = spawnSync(prefix[0], [...prefix.slice(1),
42
+ "-c", "import sys;print('%d.%d' % sys.version_info[:2])"],
43
+ { encoding: "utf8" });
29
44
  if (r.status === 0 && r.stdout) {
30
45
  const [maj, min] = r.stdout.trim().split(".").map(Number);
31
- if (maj === 3 && min >= 9) return cand;
46
+ if (maj === 3 && min >= 9) return prefix;
32
47
  }
33
48
  }
34
49
  return null;
35
50
  }
36
51
 
37
- function venvPython(venvDir) {
38
- return process.platform === "win32"
39
- ? path.join(venvDir, "Scripts", "python.exe")
40
- : path.join(venvDir, "bin", "python");
52
+ function venvDir() {
53
+ if (isWin) {
54
+ const base = process.env.LOCALAPPDATA || path.join(os.homedir(), "AppData", "Local");
55
+ return path.join(base, "whatsrunning", "venv");
56
+ }
57
+ const cacheRoot = process.env.XDG_CACHE_HOME || path.join(os.homedir(), ".cache");
58
+ return path.join(cacheRoot, "whatsrunning", "venv");
59
+ }
60
+
61
+ function venvPython(dir) {
62
+ return isWin
63
+ ? path.join(dir, "Scripts", "python.exe")
64
+ : path.join(dir, "bin", "python");
41
65
  }
42
66
 
43
67
  function bundledWheel() {
@@ -53,29 +77,31 @@ function bundledWheel() {
53
77
  return path.join(vendor, whls[whls.length - 1]);
54
78
  }
55
79
 
56
- function ensureEngine(py) {
57
- const cacheRoot = process.env.XDG_CACHE_HOME || path.join(os.homedir(), ".cache");
58
- const venvDir = path.join(cacheRoot, "whatsrunning", "venv");
59
- const vpy = venvPython(venvDir);
80
+ function ensureEngine(pyPrefix) {
81
+ const dir = venvDir();
82
+ const vpy = venvPython(dir);
60
83
  const wheel = bundledWheel();
61
84
  if (!wheel) {
62
85
  log("internal error: bundled engine wheel is missing.");
63
86
  process.exit(1);
64
87
  }
65
- const marker = path.join(venvDir, ".installed");
88
+ const marker = path.join(dir, ".installed");
66
89
  const want = path.basename(wheel);
67
90
  const upToDate = fs.existsSync(vpy) && fs.existsSync(marker) &&
68
91
  fs.readFileSync(marker, "utf8").trim() === want;
69
92
 
70
93
  if (!upToDate) {
71
94
  log("first run: setting up the Python engine (one time, needs internet)...");
72
- fs.mkdirSync(path.dirname(venvDir), { recursive: true });
73
- let r = spawnSync(py, ["-m", "venv", venvDir], { stdio: "inherit" });
95
+ fs.mkdirSync(path.dirname(dir), { recursive: true });
96
+ let r = spawnSync(pyPrefix[0], [...pyPrefix.slice(1), "-m", "venv", dir],
97
+ { stdio: "inherit" });
74
98
  if (r.status !== 0) {
75
99
  log("could not create a Python virtual environment.");
76
100
  log("make sure the venv module is available (Debian/Ubuntu: sudo apt install python3-venv).");
77
101
  process.exit(1);
78
102
  }
103
+ // Upgrade pip FIRST: the pip bundled with Python 3.9 is too old to install
104
+ // some wheels/metadata cleanly. Only then install the engine wheel.
79
105
  spawnSync(vpy, ["-m", "pip", "install", "--quiet", "--upgrade", "pip"], { stdio: "inherit" });
80
106
  r = spawnSync(vpy, ["-m", "pip", "install", "--quiet", wheel], { stdio: "inherit" });
81
107
  if (r.status !== 0) {
@@ -88,22 +114,117 @@ function ensureEngine(py) {
88
114
  return vpy;
89
115
  }
90
116
 
91
- function main() {
92
- if (process.platform === "win32") {
93
- log("Windows is not supported yet (packet capture needs Npcap).");
94
- process.exit(1);
117
+ function npcapInstalled() {
118
+ const root = process.env.SystemRoot || "C:\\Windows";
119
+ return fs.existsSync(path.join(root, "System32", "Npcap", "wpcap.dll")) ||
120
+ fs.existsSync(path.join(root, "System32", "wpcap.dll"));
121
+ }
122
+
123
+ function isElevated() {
124
+ if (isWin) {
125
+ // `net session` succeeds only from an elevated prompt.
126
+ const r = spawnSync("net", ["session"], { stdio: "ignore" });
127
+ return r.status === 0;
95
128
  }
129
+ return typeof process.getuid === "function" && process.getuid() === 0;
130
+ }
131
+
132
+ function adminHint() {
133
+ if (isWin) {
134
+ return "This needs Administrator rights. Open Terminal as Administrator, then run: npx whatsrunning spoof";
135
+ }
136
+ return "This needs root. Re-run with: sudo npx whatsrunning spoof";
137
+ }
138
+
139
+ function main() {
96
140
  const py = findPython();
97
141
  if (!py) {
98
142
  log("needs Python 3.9+ on your PATH.");
99
143
  log(" macOS: brew install python");
100
144
  log(" Debian/Ubuntu: sudo apt install python3 python3-venv");
145
+ log(" Windows: install Python 3 from https://python.org (tick 'Add to PATH')");
101
146
  process.exit(1);
102
147
  }
103
- const vpy = ensureEngine(py);
148
+
104
149
  const args = process.argv.slice(2);
105
- const r = spawnSync(vpy, ["-m", "whatsrunning", ...args], { stdio: "inherit" });
106
- process.exit(r.status === null ? 1 : r.status);
150
+ const cmd = args.find((a) => !a.startsWith("-"));
151
+
152
+ if (CAPTURE_CMDS.has(cmd)) {
153
+ if (isWin && !npcapInstalled()) {
154
+ log("packet capture on Windows needs Npcap.");
155
+ log("install it from https://npcap.com (default options are fine; you do NOT");
156
+ log("need 'WinPcap API-compatible mode' or 'Support raw 802.11 traffic').");
157
+ process.exit(1);
158
+ }
159
+ if (!isElevated()) {
160
+ log(adminHint());
161
+ process.exit(1);
162
+ }
163
+ }
164
+
165
+ const vpy = ensureEngine(py);
166
+ runEngine(vpy, args, bundledWheel());
107
167
  }
108
168
 
109
- main();
169
+ // Which signals to pass on to the engine. The engine restores the network when
170
+ // it is interrupted, so it must (a) get the signal when only this launcher is
171
+ // signalled (`kill <pid>`, a supervisor, a closed pipe), (b) NOT get it twice
172
+ // when the terminal already delivered it to the whole foreground process group
173
+ // (Ctrl-C, terminal hang-up) — a second signal mid-restore is exactly what the
174
+ // engine guards against — and (c) be waited for, so the shell prompt only
175
+ // returns once the restore has finished.
176
+ function forwardedSignals(platform, interactive) {
177
+ if (platform === "win32") {
178
+ // Ctrl-C / Ctrl-Break / console close reach every process attached to the
179
+ // console, the engine included. Never re-send them; just stay alive.
180
+ return [];
181
+ }
182
+ // SIGTERM is never sent by the terminal: always forward it. SIGINT/SIGHUP
183
+ // come from the terminal to the whole group when we're interactive, so
184
+ // forward those only when not attached to a terminal.
185
+ return interactive ? ["SIGTERM"] : ["SIGINT", "SIGTERM", "SIGHUP"];
186
+ }
187
+
188
+ function runEngine(vpy, args, wheel) {
189
+ // Lets `schedule add` reuse this exact wheel for the root-owned engine.
190
+ const env = Object.assign({}, process.env);
191
+ if (wheel) env.WHATSRUNNING_WHEEL = wheel;
192
+ const child = spawn(vpy, ["-m", "whatsrunning", ...args], { stdio: "inherit", env });
193
+
194
+ const interactive = Boolean(process.stdin.isTTY);
195
+ const forwarded = forwardedSignals(process.platform, interactive);
196
+ const handlers = {};
197
+ for (const sig of forwarded) {
198
+ handlers[sig] = () => {
199
+ try { child.kill(sig); } catch (e) { /* already gone */ }
200
+ };
201
+ }
202
+ // Signals we don't forward must still not kill the launcher before the
203
+ // engine is done (it's restoring the network); keep a no-op handler.
204
+ const all = process.platform === "win32" ? ["SIGINT", "SIGBREAK", "SIGHUP"]
205
+ : ["SIGINT", "SIGTERM", "SIGHUP"];
206
+ for (const sig of all) {
207
+ if (!handlers[sig]) handlers[sig] = () => {};
208
+ }
209
+ for (const sig of Object.keys(handlers)) {
210
+ try { process.on(sig, handlers[sig]); } catch (e) { /* unsupported here */ }
211
+ }
212
+
213
+ child.on("error", (e) => {
214
+ log(`could not start the engine: ${e.message}`);
215
+ process.exit(1);
216
+ });
217
+ child.on("exit", (code, signal) => {
218
+ for (const sig of Object.keys(handlers)) process.removeListener(sig, handlers[sig]);
219
+ if (signal) {
220
+ // Die the same way the engine did, so callers see the real reason.
221
+ try { process.kill(process.pid, signal); } catch (e) { /* fall through */ }
222
+ process.exit(1);
223
+ }
224
+ process.exit(code === null ? 1 : code);
225
+ });
226
+ }
227
+
228
+ module.exports = { forwardedSignals, runEngine };
229
+
230
+ if (require.main === module) main();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "whatsrunning",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "See what every device on your own home network is talking to. (npm launcher for the Python engine.)",
5
5
  "bin": {
6
6
  "whatsrunning": "bin/whatsrunning.js"
@@ -33,7 +33,8 @@
33
33
  },
34
34
  "os": [
35
35
  "darwin",
36
- "linux"
36
+ "linux",
37
+ "win32"
37
38
  ],
38
39
  "preferGlobal": true
39
40
  }