@webmux/agent 0.1.2 → 0.1.4
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/cli.js +56 -14
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
import fs2 from "fs";
|
|
5
5
|
import os2 from "os";
|
|
6
6
|
import path2 from "path";
|
|
7
|
-
import { execSync } from "child_process";
|
|
7
|
+
import { execSync as execSync2 } from "child_process";
|
|
8
8
|
import { Command } from "commander";
|
|
9
9
|
|
|
10
10
|
// src/credentials.ts
|
|
@@ -198,6 +198,7 @@ function isTmuxEmptyStateMessage(message) {
|
|
|
198
198
|
}
|
|
199
199
|
|
|
200
200
|
// src/connection.ts
|
|
201
|
+
import { execSync } from "child_process";
|
|
201
202
|
import WebSocket from "ws";
|
|
202
203
|
|
|
203
204
|
// src/terminal.ts
|
|
@@ -253,6 +254,7 @@ async function createTerminalBridge(options) {
|
|
|
253
254
|
}
|
|
254
255
|
|
|
255
256
|
// src/connection.ts
|
|
257
|
+
var AGENT_VERSION = "0.1.4";
|
|
256
258
|
var HEARTBEAT_INTERVAL_MS = 3e4;
|
|
257
259
|
var SESSION_SYNC_INTERVAL_MS = 15e3;
|
|
258
260
|
var INITIAL_RECONNECT_DELAY_MS = 1e3;
|
|
@@ -301,7 +303,7 @@ var AgentConnection = class {
|
|
|
301
303
|
ws.on("open", () => {
|
|
302
304
|
console.log("[agent] WebSocket connected, authenticating...");
|
|
303
305
|
this.reconnectDelay = INITIAL_RECONNECT_DELAY_MS;
|
|
304
|
-
this.sendMessage({ type: "auth", agentId: this.agentId, agentSecret: this.agentSecret });
|
|
306
|
+
this.sendMessage({ type: "auth", agentId: this.agentId, agentSecret: this.agentSecret, version: AGENT_VERSION });
|
|
305
307
|
});
|
|
306
308
|
ws.on("message", (raw) => {
|
|
307
309
|
let msg;
|
|
@@ -325,6 +327,11 @@ var AgentConnection = class {
|
|
|
325
327
|
switch (msg.type) {
|
|
326
328
|
case "auth-ok":
|
|
327
329
|
console.log("[agent] Authenticated successfully");
|
|
330
|
+
if (msg.latestVersion && msg.latestVersion !== AGENT_VERSION) {
|
|
331
|
+
console.log(`[agent] Update available: ${AGENT_VERSION} \u2192 ${msg.latestVersion}`);
|
|
332
|
+
this.selfUpdate(msg.latestVersion);
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
328
335
|
this.startHeartbeat();
|
|
329
336
|
this.startSessionSync();
|
|
330
337
|
this.syncSessions();
|
|
@@ -450,6 +457,22 @@ var AgentConnection = class {
|
|
|
450
457
|
this.sendMessage({ type: "command-result", requestId, ok: false, error: message });
|
|
451
458
|
}
|
|
452
459
|
}
|
|
460
|
+
selfUpdate(targetVersion) {
|
|
461
|
+
console.log(`[agent] Installing @webmux/agent@${targetVersion}...`);
|
|
462
|
+
try {
|
|
463
|
+
execSync(`npm install -g @webmux/agent@${targetVersion}`, { stdio: "inherit" });
|
|
464
|
+
console.log("[agent] Update installed. Restarting...");
|
|
465
|
+
} catch (err) {
|
|
466
|
+
console.error("[agent] Update failed:", err instanceof Error ? err.message : err);
|
|
467
|
+
console.log("[agent] Continuing with current version");
|
|
468
|
+
this.startHeartbeat();
|
|
469
|
+
this.startSessionSync();
|
|
470
|
+
this.syncSessions();
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
this.stop();
|
|
474
|
+
process.exit(0);
|
|
475
|
+
}
|
|
453
476
|
sendMessage(msg) {
|
|
454
477
|
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
|
455
478
|
this.ws.send(JSON.stringify(msg));
|
|
@@ -599,7 +622,7 @@ program.command("status").description("Show agent status and credentials info").
|
|
|
599
622
|
console.log(`Agent ID: ${creds.agentId}`);
|
|
600
623
|
console.log(`Credentials File: ${credentialsPath()}`);
|
|
601
624
|
try {
|
|
602
|
-
const result =
|
|
625
|
+
const result = execSync2(`systemctl --user is-active ${SERVICE_NAME} 2>/dev/null`, { encoding: "utf-8" }).trim();
|
|
603
626
|
console.log(`Service: ${result}`);
|
|
604
627
|
} catch {
|
|
605
628
|
console.log(`Service: not installed`);
|
|
@@ -619,6 +642,7 @@ service.command("install").description("Install and start the agent as a systemd
|
|
|
619
642
|
}
|
|
620
643
|
const serviceDir = path2.join(os2.homedir(), ".config", "systemd", "user");
|
|
621
644
|
const servicePath = path2.join(serviceDir, `${SERVICE_NAME}.service`);
|
|
645
|
+
const npmPath = findBinary("npm") ?? "npm";
|
|
622
646
|
const unit = `[Unit]
|
|
623
647
|
Description=Webmux Agent (${creds.name})
|
|
624
648
|
After=network-online.target
|
|
@@ -626,9 +650,10 @@ Wants=network-online.target
|
|
|
626
650
|
|
|
627
651
|
[Service]
|
|
628
652
|
Type=simple
|
|
629
|
-
|
|
653
|
+
ExecStartPre=${npmPath} install -g @webmux/agent@latest
|
|
654
|
+
ExecStart=${findBinary("webmux-agent") ?? `${npxPath} -y @webmux/agent`} start
|
|
630
655
|
Restart=always
|
|
631
|
-
RestartSec=
|
|
656
|
+
RestartSec=10
|
|
632
657
|
Environment=HOME=${os2.homedir()}
|
|
633
658
|
Environment=PATH=${process.env.PATH}
|
|
634
659
|
WorkingDirectory=${os2.homedir()}
|
|
@@ -640,10 +665,10 @@ WantedBy=default.target
|
|
|
640
665
|
fs2.writeFileSync(servicePath, unit);
|
|
641
666
|
console.log(`[agent] Service file created: ${servicePath}`);
|
|
642
667
|
try {
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
668
|
+
execSync2("systemctl --user daemon-reload", { stdio: "inherit" });
|
|
669
|
+
execSync2(`systemctl --user enable ${SERVICE_NAME}`, { stdio: "inherit" });
|
|
670
|
+
execSync2(`systemctl --user start ${SERVICE_NAME}`, { stdio: "inherit" });
|
|
671
|
+
execSync2(`loginctl enable-linger ${os2.userInfo().username}`, { stdio: "inherit" });
|
|
647
672
|
console.log(``);
|
|
648
673
|
console.log(`[agent] Service installed and started!`);
|
|
649
674
|
console.log(`[agent] It will auto-start on boot.`);
|
|
@@ -663,8 +688,8 @@ WantedBy=default.target
|
|
|
663
688
|
service.command("uninstall").description("Stop and remove the systemd user service").action(() => {
|
|
664
689
|
const servicePath = path2.join(os2.homedir(), ".config", "systemd", "user", `${SERVICE_NAME}.service`);
|
|
665
690
|
try {
|
|
666
|
-
|
|
667
|
-
|
|
691
|
+
execSync2(`systemctl --user stop ${SERVICE_NAME} 2>/dev/null`, { stdio: "inherit" });
|
|
692
|
+
execSync2(`systemctl --user disable ${SERVICE_NAME} 2>/dev/null`, { stdio: "inherit" });
|
|
668
693
|
} catch {
|
|
669
694
|
}
|
|
670
695
|
if (fs2.existsSync(servicePath)) {
|
|
@@ -672,21 +697,38 @@ service.command("uninstall").description("Stop and remove the systemd user servi
|
|
|
672
697
|
console.log(`[agent] Service file removed: ${servicePath}`);
|
|
673
698
|
}
|
|
674
699
|
try {
|
|
675
|
-
|
|
700
|
+
execSync2("systemctl --user daemon-reload", { stdio: "inherit" });
|
|
676
701
|
} catch {
|
|
677
702
|
}
|
|
678
703
|
console.log(`[agent] Service uninstalled.`);
|
|
679
704
|
});
|
|
680
705
|
service.command("status").description("Show systemd service status").action(() => {
|
|
681
706
|
try {
|
|
682
|
-
|
|
707
|
+
execSync2(`systemctl --user status ${SERVICE_NAME}`, { stdio: "inherit" });
|
|
683
708
|
} catch {
|
|
684
709
|
console.log(`[agent] Service is not installed or not running.`);
|
|
685
710
|
}
|
|
686
711
|
});
|
|
712
|
+
service.command("upgrade").description("Upgrade agent to latest version and restart service").action(() => {
|
|
713
|
+
console.log("[agent] Upgrading @webmux/agent to latest...");
|
|
714
|
+
try {
|
|
715
|
+
execSync2("npm install -g @webmux/agent@latest", { stdio: "inherit" });
|
|
716
|
+
} catch {
|
|
717
|
+
console.error("[agent] Failed to upgrade. Try manually: npm install -g @webmux/agent@latest");
|
|
718
|
+
process.exit(1);
|
|
719
|
+
}
|
|
720
|
+
console.log("[agent] Restarting service...");
|
|
721
|
+
try {
|
|
722
|
+
execSync2(`systemctl --user restart ${SERVICE_NAME}`, { stdio: "inherit" });
|
|
723
|
+
console.log("[agent] Upgrade complete!");
|
|
724
|
+
} catch {
|
|
725
|
+
console.log("[agent] Package upgraded. Service not installed or restart failed.");
|
|
726
|
+
console.log("[agent] If running manually, restart with: npx @webmux/agent@latest start");
|
|
727
|
+
}
|
|
728
|
+
});
|
|
687
729
|
function findBinary(name) {
|
|
688
730
|
try {
|
|
689
|
-
return
|
|
731
|
+
return execSync2(`which ${name} 2>/dev/null`, { encoding: "utf-8" }).trim();
|
|
690
732
|
} catch {
|
|
691
733
|
return null;
|
|
692
734
|
}
|