@termix-ssh/cli 1.1.2 → 1.2.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
@@ -33,7 +33,7 @@ The Termix CLI talks to your [Termix](https://github.com/Termix-SSH/Termix) serv
33
33
  - Interactive SSH terminal with `termix ssh`, over the same connection the web UI uses
34
34
  - One-off remote commands with `termix exec`, exiting with the remote exit code
35
35
  - File browsing and transfer over SFTP
36
- - SSH tunnel and Docker container control
36
+ - SSH tunnel, WireGuard gateway and Docker container control
37
37
  - Fleets, including running a command across every host at once
38
38
  - Host, credential and snippet management, with import and export
39
39
  - API keys and host enrollment for scripts, CI and AI agents
package/dist/index.js CHANGED
@@ -29307,7 +29307,7 @@ function unavailableFor(pluginId, reason) {
29307
29307
  }
29308
29308
 
29309
29309
  // src/version.ts
29310
- var CLI_VERSION = true ? "1.1.2" : "0.0.0-dev";
29310
+ var CLI_VERSION = true ? "1.2.0" : "0.0.0-dev";
29311
29311
 
29312
29312
  // src/core/http.ts
29313
29313
  var MAX_ATTEMPTS = 3;
@@ -31332,6 +31332,224 @@ function resolveLocalTarget(localPath, remotePath) {
31332
31332
 
31333
31333
  // src/commands/tunnel.ts
31334
31334
  import { setTimeout as sleep2 } from "timers/promises";
31335
+
31336
+ // src/commands/gateway.ts
31337
+ var GATEWAY_COLUMNS = [
31338
+ { header: "id", value: (g) => g.id, align: "right" },
31339
+ { header: "name", value: (g) => g.name },
31340
+ { header: "state", value: (g) => g.status.state },
31341
+ { header: "private host", value: (g) => g.privateHostId, align: "right" },
31342
+ { header: "public host", value: (g) => g.publicHostId, align: "right" },
31343
+ {
31344
+ header: "ports on",
31345
+ value: (g) => g.ports.filter((p) => p.enabled).length,
31346
+ align: "right"
31347
+ }
31348
+ ];
31349
+ var PORT_COLUMNS = [
31350
+ { header: "id", value: (p) => p.id, align: "right" },
31351
+ { header: "public", value: (p) => `${p.proto} ${p.publicPort}` },
31352
+ { header: "target", value: (p) => `${p.targetHost || "-"}:${p.targetPort}` },
31353
+ { header: "on", value: (p) => p.enabled ? "yes" : "no" },
31354
+ { header: "from", value: (p) => p.source },
31355
+ { header: "label", value: (p) => p.label }
31356
+ ];
31357
+ function gatewayPath(id, sub = "") {
31358
+ return pluginPath("tunnels", `/gateways/${id}${sub}`);
31359
+ }
31360
+ function parsePort2(value, flag) {
31361
+ const port = Number(value);
31362
+ if (!Number.isInteger(port) || port < 1 || port > 65535) {
31363
+ throw new UsageError(`Invalid ${flag}: "${value}" (expected 1-65535).`);
31364
+ }
31365
+ return port;
31366
+ }
31367
+ function buildPortBody(opts) {
31368
+ if (opts.publicPort === void 0) {
31369
+ throw new UsageError("A port needs --public-port.");
31370
+ }
31371
+ const publicPort = parsePort2(opts.publicPort, "--public-port");
31372
+ const proto = opts.proto ?? "tcp";
31373
+ if (!["tcp", "udp", "both"].includes(proto)) {
31374
+ throw new UsageError(
31375
+ `Invalid --proto: "${proto}" (expected tcp, udp or both).`
31376
+ );
31377
+ }
31378
+ return {
31379
+ proto,
31380
+ publicPort,
31381
+ targetPort: opts.targetPort === void 0 ? publicPort : parsePort2(opts.targetPort, "--target-port"),
31382
+ targetHost: opts.targetHost ?? "",
31383
+ label: opts.label ?? "",
31384
+ force: opts.force === true ? true : void 0
31385
+ };
31386
+ }
31387
+ function registerGatewayCommands(tunnel) {
31388
+ const gateway = tunnel.command("gateway").description(
31389
+ "Publish services on a private host through a public host over WireGuard."
31390
+ );
31391
+ gateway.command("list", { isDefault: true }).description("List gateways.").action(async function() {
31392
+ await run(async () => {
31393
+ const { client, requireFeature: requireFeature2 } = await createContext(this);
31394
+ await requireFeature2("tunnels");
31395
+ const rows = await client.request({
31396
+ method: "GET",
31397
+ path: pluginPath("tunnels", "/gateways")
31398
+ });
31399
+ printList(rows ?? [], GATEWAY_COLUMNS, {
31400
+ quietField: "id",
31401
+ jsonWrapper: (list) => ({ count: list.length, gateways: list })
31402
+ });
31403
+ });
31404
+ });
31405
+ gateway.command("show <gatewayId>").description("Show a gateway's state and ports.").action(async function(idArg) {
31406
+ await run(async () => {
31407
+ const id = parseId(idArg);
31408
+ const { client, requireFeature: requireFeature2 } = await createContext(this);
31409
+ await requireFeature2("tunnels");
31410
+ const found = await client.request({
31411
+ method: "GET",
31412
+ path: gatewayPath(id)
31413
+ });
31414
+ printList(found.ports ?? [], PORT_COLUMNS, {
31415
+ quietField: "id",
31416
+ jsonWrapper: () => found
31417
+ });
31418
+ });
31419
+ });
31420
+ gateway.command("create").description("Create a gateway and set it up on both hosts.").requiredOption("--name <name>", "Gateway name").requiredOption("--private-host <id>", "Host with the services").requiredOption("--public-host <id>", "Host with a public address").requiredOption(
31421
+ "--endpoint <address>",
31422
+ "Public address of the public host (an IP or domain)"
31423
+ ).option("--port <port>", "WireGuard UDP port (picked for you by default)").action(async function(opts) {
31424
+ await run(async () => {
31425
+ const { client, requireFeature: requireFeature2 } = await createContext(this);
31426
+ await requireFeature2("tunnels");
31427
+ const created = await client.request({
31428
+ method: "POST",
31429
+ path: pluginPath("tunnels", "/gateways"),
31430
+ data: {
31431
+ name: opts.name,
31432
+ privateHostId: parseId(opts.privateHost),
31433
+ publicHostId: parseId(opts.publicHost),
31434
+ endpoint: opts.endpoint,
31435
+ listenPort: opts.port === void 0 ? void 0 : parsePort2(opts.port, "--port")
31436
+ }
31437
+ });
31438
+ printResult(`Created gateway ${created.id}.`, {
31439
+ id: created.id,
31440
+ name: created.name
31441
+ });
31442
+ });
31443
+ });
31444
+ for (const [name, sub, verb] of [
31445
+ ["apply", "/apply", "Applied"],
31446
+ ["check", "/check", "Checked"]
31447
+ ]) {
31448
+ gateway.command(`${name} <gatewayId>`).description(
31449
+ name === "apply" ? "Write the tunnel and rules to both hosts again." : "Read the tunnel and rule state from both hosts."
31450
+ ).action(async function(idArg) {
31451
+ await run(async () => {
31452
+ const id = parseId(idArg);
31453
+ const { client, requireFeature: requireFeature2 } = await createContext(this);
31454
+ await requireFeature2("tunnels");
31455
+ const result = await client.request({
31456
+ method: "POST",
31457
+ path: gatewayPath(id, sub)
31458
+ });
31459
+ printResult(`${verb} gateway ${id}: ${result.status.state}.`, {
31460
+ id,
31461
+ state: result.status.state,
31462
+ message: result.status.message
31463
+ });
31464
+ });
31465
+ });
31466
+ }
31467
+ gateway.command("scan <gatewayId>").description("Look for new listening ports now.").action(async function(idArg) {
31468
+ await run(async () => {
31469
+ const id = parseId(idArg);
31470
+ const { client, requireFeature: requireFeature2 } = await createContext(this);
31471
+ await requireFeature2("tunnels");
31472
+ const result = await client.request({
31473
+ method: "POST",
31474
+ path: gatewayPath(id, "/scan")
31475
+ });
31476
+ printResult(`Found ${result.found} new port(s).`, {
31477
+ id,
31478
+ found: result.found
31479
+ });
31480
+ });
31481
+ });
31482
+ gateway.command("remove <gatewayId>").description("Take a gateway off both hosts and delete it.").action(async function(idArg) {
31483
+ await run(async () => {
31484
+ const id = parseId(idArg);
31485
+ const { client, requireFeature: requireFeature2 } = await createContext(this);
31486
+ await requireFeature2("tunnels");
31487
+ await client.request({ method: "DELETE", path: gatewayPath(id) });
31488
+ printResult(`Removed gateway ${id}.`, { id });
31489
+ });
31490
+ });
31491
+ const port = gateway.command("port").description("Manage a gateway's ports.");
31492
+ port.command("add <gatewayId>").description("Forward a public port. It starts on.").option("--public-port <port>", "Port on the public host").option("--target-port <port>", "Port on the target (default: same)").option("--proto <proto>", "tcp, udp or both (default: tcp)").option(
31493
+ "--target-host <ip>",
31494
+ "IPv4 address of the target (default: the private host)"
31495
+ ).option("--label <text>", "A note for the port").option("--force", "Use the port even if the public host listens on it").action(async function(idArg, opts) {
31496
+ await run(async () => {
31497
+ const id = parseId(idArg);
31498
+ const body = buildPortBody(opts);
31499
+ const { client, requireFeature: requireFeature2 } = await createContext(this);
31500
+ await requireFeature2("tunnels");
31501
+ const created = await client.request({
31502
+ method: "POST",
31503
+ path: gatewayPath(id, "/ports"),
31504
+ data: body
31505
+ });
31506
+ printResult(`Added port ${created.id} to gateway ${id}.`, {
31507
+ id: created.id,
31508
+ gatewayId: id
31509
+ });
31510
+ });
31511
+ });
31512
+ for (const [name, enabled] of [
31513
+ ["enable", true],
31514
+ ["disable", false]
31515
+ ]) {
31516
+ port.command(`${name} <gatewayId> <portId>`).description(`Turn a forwarded port ${enabled ? "on" : "off"}.`).option("--force", "Use the port even if the public host listens on it").action(async function(idArg, portArg, opts) {
31517
+ await run(async () => {
31518
+ const id = parseId(idArg);
31519
+ const portId = parseId(portArg);
31520
+ const { client, requireFeature: requireFeature2 } = await createContext(this);
31521
+ await requireFeature2("tunnels");
31522
+ await client.request({
31523
+ method: "PATCH",
31524
+ path: gatewayPath(id, `/ports/${portId}`),
31525
+ data: { enabled, force: opts.force === true ? true : void 0 }
31526
+ });
31527
+ printResult(
31528
+ `Turned port ${portId} ${enabled ? "on" : "off"} on gateway ${id}.`,
31529
+ { id: portId, gatewayId: id, enabled }
31530
+ );
31531
+ });
31532
+ });
31533
+ }
31534
+ port.command("remove <gatewayId> <portId>").description("Delete a forwarded port.").action(async function(idArg, portArg) {
31535
+ await run(async () => {
31536
+ const id = parseId(idArg);
31537
+ const portId = parseId(portArg);
31538
+ const { client, requireFeature: requireFeature2 } = await createContext(this);
31539
+ await requireFeature2("tunnels");
31540
+ await client.request({
31541
+ method: "DELETE",
31542
+ path: gatewayPath(id, `/ports/${portId}`)
31543
+ });
31544
+ printResult(`Removed port ${portId} from gateway ${id}.`, {
31545
+ id: portId,
31546
+ gatewayId: id
31547
+ });
31548
+ });
31549
+ });
31550
+ }
31551
+
31552
+ // src/commands/tunnel.ts
31335
31553
  var TUNNEL_COLUMNS = [
31336
31554
  { header: "name", value: (t) => t.displayName ?? t.name },
31337
31555
  { header: "status", value: (t) => t.status },
@@ -31379,6 +31597,15 @@ function parseConnections(host) {
31379
31597
  }
31380
31598
  return [];
31381
31599
  }
31600
+ function parseIndex(value) {
31601
+ const index = Number(value);
31602
+ if (!Number.isInteger(index) || index < 0) {
31603
+ throw new UsageError(
31604
+ `Invalid tunnel index: "${value}" (expected a non-negative integer).`
31605
+ );
31606
+ }
31607
+ return index;
31608
+ }
31382
31609
  function tunnelMode(connection) {
31383
31610
  return connection.mode ?? connection.tunnelType ?? "local";
31384
31611
  }
@@ -31429,6 +31656,95 @@ async function loadHost(client, hostId) {
31429
31656
  });
31430
31657
  return { ...host, id: hostId };
31431
31658
  }
31659
+ async function loadConnections(client, host) {
31660
+ try {
31661
+ const saved = await client.request({ method: "GET", path: pluginPath("tunnels", "/saved") });
31662
+ if (Array.isArray(saved)) {
31663
+ return saved.find((entry) => entry.hostId === host.id)?.connections ?? [];
31664
+ }
31665
+ } catch (error) {
31666
+ if (!(error instanceof TermixApiError && error.status === 404)) throw error;
31667
+ }
31668
+ return parseConnections(host);
31669
+ }
31670
+ async function saveConnections(client, hostId, connections) {
31671
+ try {
31672
+ await client.request({
31673
+ method: "PUT",
31674
+ path: pluginPath("tunnels", `/saved/${hostId}`),
31675
+ data: { connections }
31676
+ });
31677
+ } catch (error) {
31678
+ if (error instanceof TermixApiError && error.status === 404) {
31679
+ throw new Error(
31680
+ "This server's Tunnels plugin is older than 1.2.0, so tunnels can't be changed from the CLI. Update the plugin."
31681
+ );
31682
+ }
31683
+ throw error;
31684
+ }
31685
+ }
31686
+ function parsePort3(value, flag) {
31687
+ const port = Number(value);
31688
+ if (!Number.isInteger(port) || port < 1 || port > 65535) {
31689
+ throw new UsageError(`Invalid ${flag}: "${value}" (expected 1-65535).`);
31690
+ }
31691
+ return port;
31692
+ }
31693
+ function parseCount(value, flag) {
31694
+ const count = Number(value);
31695
+ if (!Number.isInteger(count) || count < 0) {
31696
+ throw new UsageError(
31697
+ `Invalid ${flag}: "${value}" (expected a whole number).`
31698
+ );
31699
+ }
31700
+ return count;
31701
+ }
31702
+ function buildConnection(opts, base) {
31703
+ const next = base ? { ...base } : {
31704
+ scope: "s2s",
31705
+ mode: "local",
31706
+ bindHost: "127.0.0.1",
31707
+ maxRetries: 3,
31708
+ retryInterval: 10,
31709
+ autoStart: false
31710
+ };
31711
+ if (opts.mode !== void 0) {
31712
+ if (!["local", "remote", "dynamic"].includes(opts.mode)) {
31713
+ throw new UsageError(
31714
+ `Invalid --mode: "${opts.mode}" (expected local, remote or dynamic).`
31715
+ );
31716
+ }
31717
+ next.mode = opts.mode;
31718
+ next.tunnelType = opts.mode === "remote" ? "remote" : "local";
31719
+ }
31720
+ if (opts.sourcePort !== void 0) {
31721
+ next.sourcePort = parsePort3(opts.sourcePort, "--source-port");
31722
+ }
31723
+ if (opts.endpointHost !== void 0) next.endpointHost = opts.endpointHost;
31724
+ if (opts.endpointPort !== void 0) {
31725
+ next.endpointPort = parsePort3(opts.endpointPort, "--endpoint-port");
31726
+ }
31727
+ if (opts.bindHost !== void 0) next.bindHost = opts.bindHost;
31728
+ if (opts.maxRetries !== void 0) {
31729
+ next.maxRetries = parseCount(opts.maxRetries, "--max-retries");
31730
+ }
31731
+ if (opts.retryInterval !== void 0) {
31732
+ next.retryInterval = parseCount(opts.retryInterval, "--retry-interval");
31733
+ }
31734
+ if (opts.autoStart !== void 0) next.autoStart = opts.autoStart;
31735
+ if (next.sourcePort === void 0) {
31736
+ throw new UsageError("A tunnel needs --source-port.");
31737
+ }
31738
+ if (tunnelMode(next) !== "dynamic" && next.endpointPort === void 0) {
31739
+ throw new UsageError(
31740
+ "A tunnel needs --endpoint-port unless it is dynamic."
31741
+ );
31742
+ }
31743
+ return next;
31744
+ }
31745
+ function withTunnelOptions(command) {
31746
+ return command.option("--mode <mode>", "local, remote or dynamic").option("--source-port <port>", "Port the tunnel listens on").option("--endpoint-host <host>", "Where the tunnel goes").option("--endpoint-port <port>", "Port on the endpoint").option("--bind-host <address>", "Address the tunnel listens on").option("--max-retries <count>", "How many times to retry").option("--retry-interval <seconds>", "Seconds between retries").option("--auto-start", "Start the tunnel when Termix starts").option("--no-auto-start", "Do not start it when Termix starts");
31747
+ }
31432
31748
  function registerTunnelCommands(program3) {
31433
31749
  const tunnel = program3.command("tunnel").description("Manage SSH tunnels configured on your hosts.");
31434
31750
  tunnel.command("list", { isDefault: true }).description("Show the status of every tunnel.").action(async function() {
@@ -31453,14 +31769,16 @@ function registerTunnelCommands(program3) {
31453
31769
  const { client, requireFeature: requireFeature2 } = await createContext(this);
31454
31770
  await requireFeature2("tunnels");
31455
31771
  const host = await loadHost(client, hostId);
31456
- const rows = parseConnections(host).map((connection, index) => ({
31457
- index,
31458
- name: buildTunnelName(host, index, connection),
31459
- mode: tunnelMode(connection),
31460
- sourcePort: connection.sourcePort,
31461
- endpointHost: endpointHostOf(connection),
31462
- endpointPort: connection.endpointPort
31463
- }));
31772
+ const rows = (await loadConnections(client, host)).map(
31773
+ (connection, index) => ({
31774
+ index,
31775
+ name: buildTunnelName(host, index, connection),
31776
+ mode: tunnelMode(connection),
31777
+ sourcePort: connection.sourcePort,
31778
+ endpointHost: endpointHostOf(connection),
31779
+ endpointPort: connection.endpointPort
31780
+ })
31781
+ );
31464
31782
  printList(
31465
31783
  rows,
31466
31784
  [
@@ -31492,7 +31810,7 @@ function registerTunnelCommands(program3) {
31492
31810
  const { client, requireFeature: requireFeature2 } = await createContext(this);
31493
31811
  await requireFeature2("tunnels");
31494
31812
  const host = await loadHost(client, hostId);
31495
- const connection = parseConnections(host)[index];
31813
+ const connection = (await loadConnections(client, host))[index];
31496
31814
  if (!connection) {
31497
31815
  throw new UsageError(
31498
31816
  `Host ${hostId} has no tunnel at index ${index}. Run \`termix tunnel show ${hostId}\`.`
@@ -31517,6 +31835,82 @@ function registerTunnelCommands(program3) {
31517
31835
  );
31518
31836
  });
31519
31837
  });
31838
+ withTunnelOptions(
31839
+ tunnel.command("add <hostId>").description(
31840
+ "Add a tunnel to a host. Tunnels must be turned on for the host."
31841
+ )
31842
+ ).action(async function(hostIdArg, opts) {
31843
+ await run(async () => {
31844
+ const hostId = parseId(hostIdArg);
31845
+ const connection = buildConnection(opts);
31846
+ const { client, requireFeature: requireFeature2 } = await createContext(this);
31847
+ await requireFeature2("tunnels");
31848
+ const host = await loadHost(client, hostId);
31849
+ if (host.pluginSettings?.tunnels?.enableTunnel !== true) {
31850
+ throw new UsageError(
31851
+ `Tunnels are off for host ${hostId}. Turn on Enable Tunneling for it first.`
31852
+ );
31853
+ }
31854
+ const connections = await loadConnections(client, host);
31855
+ await saveConnections(client, hostId, [...connections, connection]);
31856
+ const index = connections.length;
31857
+ printResult(`Added tunnel ${index} to host ${hostId}.`, {
31858
+ hostId,
31859
+ index
31860
+ });
31861
+ });
31862
+ });
31863
+ withTunnelOptions(
31864
+ tunnel.command("edit <hostId> <index>").description("Change a tunnel. Only the options you give are changed.")
31865
+ ).action(async function(hostIdArg, indexArg, opts) {
31866
+ await run(async () => {
31867
+ const hostId = parseId(hostIdArg);
31868
+ const index = parseIndex(indexArg);
31869
+ const { client, requireFeature: requireFeature2 } = await createContext(this);
31870
+ await requireFeature2("tunnels");
31871
+ const host = await loadHost(client, hostId);
31872
+ const connections = await loadConnections(client, host);
31873
+ const current = connections[index];
31874
+ if (!current) {
31875
+ throw new UsageError(
31876
+ `Host ${hostId} has no tunnel at index ${index}. Run \`termix tunnel show ${hostId}\`.`
31877
+ );
31878
+ }
31879
+ const next = connections.map(
31880
+ (c, i) => i === index ? buildConnection(opts, current) : c
31881
+ );
31882
+ await saveConnections(client, hostId, next);
31883
+ printResult(`Changed tunnel ${index} on host ${hostId}.`, {
31884
+ hostId,
31885
+ index
31886
+ });
31887
+ });
31888
+ });
31889
+ tunnel.command("remove <hostId> <index>").description("Delete a tunnel from a host.").action(async function(hostIdArg, indexArg) {
31890
+ await run(async () => {
31891
+ const hostId = parseId(hostIdArg);
31892
+ const index = parseIndex(indexArg);
31893
+ const { client, requireFeature: requireFeature2 } = await createContext(this);
31894
+ await requireFeature2("tunnels");
31895
+ const host = await loadHost(client, hostId);
31896
+ const connections = await loadConnections(client, host);
31897
+ if (!connections[index]) {
31898
+ throw new UsageError(
31899
+ `Host ${hostId} has no tunnel at index ${index}. Run \`termix tunnel show ${hostId}\`.`
31900
+ );
31901
+ }
31902
+ await saveConnections(
31903
+ client,
31904
+ hostId,
31905
+ connections.filter((_, i) => i !== index)
31906
+ );
31907
+ printResult(`Removed tunnel ${index} from host ${hostId}.`, {
31908
+ hostId,
31909
+ index
31910
+ });
31911
+ });
31912
+ });
31913
+ registerGatewayCommands(tunnel);
31520
31914
  tunnel.command("stop <name>").description("Stop a running tunnel by its full name (see `tunnel list`).").action(async function(name) {
31521
31915
  await run(async () => {
31522
31916
  const { client, requireFeature: requireFeature2 } = await createContext(this);