@ds-hu/dsh-tailcat-remote 0.1.0 → 0.1.1

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/src/parse.js CHANGED
@@ -117,7 +117,7 @@ export function buildServeArgs(input) {
117
117
  const allow = Array.isArray(input.allow) ? input.allow.filter((key) => typeof key === 'string' && key !== '') : [];
118
118
  if (allow.length > 0) argv.push(`--allow=${allow.join(',')}`);
119
119
  if (input.listen === 'tcp') {
120
- argv.push(String(input.port));
120
+ argv.push(`${input.port}:127.0.0.1:${input.port}`);
121
121
  } else {
122
122
  if (!input.socketPath) throw new Error('buildServeArgs: socketPath is required in socket mode');
123
123
  argv.push(`${input.port}:${input.socketPath}`);
package/src/paths.js CHANGED
@@ -1,3 +1,4 @@
1
+ import fs from 'node:fs';
1
2
  import os from 'node:os';
2
3
  import path from 'node:path';
3
4
 
@@ -7,34 +8,94 @@ export function dshHome() {
7
8
  return configured ? path.resolve(configured) : path.join(os.homedir(), '.dsh');
8
9
  }
9
10
 
10
- /** Plugin-owned runtime-data directory (device allow-list only). */
11
+ /** Plugin-owned runtime data (devices and the managed tailcat copy). */
11
12
  export function dataDir() {
12
13
  return path.join(dshHome(), 'tailcat-remote');
13
14
  }
14
15
 
16
+ /** Directory holding the plugin-managed tailcat executable. */
17
+ export function managedBinaryDir() {
18
+ return path.join(dataDir(), 'bin');
19
+ }
20
+
21
+ /** Absolute path of the plugin-managed tailcat executable. */
22
+ export function managedBinaryPath(platform = process.platform) {
23
+ return path.join(managedBinaryDir(), platform === 'win32' ? 'tailcat.exe' : 'tailcat');
24
+ }
25
+
15
26
  /** Default Unix-domain door socket inside the harness data directory. */
16
27
  export function defaultSocketPath() {
17
28
  return path.join(dshHome(), 'tailcat-remote.sock');
18
29
  }
19
30
 
20
- /** The platform user-config directory tailcat uses for saved keys. */
21
- export function userConfigDir() {
22
- const configured = process.env.XDG_CONFIG_HOME?.trim();
23
- return configured ? path.resolve(configured) : path.join(os.homedir(), '.config');
31
+ /** @param {string} value - possibly relative config root. @param {string} home - tailcat cwd. @returns {string} absolute path. */
32
+ function resolveFromHome(value, home) {
33
+ return path.isAbsolute(value) ? value : path.resolve(home, value);
34
+ }
35
+
36
+ /**
37
+ * The platform user-config directory tailcat uses for saved keys. This must
38
+ * match Go's os.UserConfigDir: macOS uses Application Support and Windows
39
+ * uses %AppData%, even when XDG_CONFIG_HOME is set.
40
+ * @param {string} [platform] - Node platform.
41
+ * @param {NodeJS.ProcessEnv} [env] - environment.
42
+ * @param {string} [home] - home directory.
43
+ * @returns {string} absolute config root.
44
+ */
45
+ export function userConfigDir(platform = process.platform, env = process.env, home = os.homedir()) {
46
+ if (platform === 'win32') {
47
+ const configured = env.APPDATA?.trim();
48
+ return configured ? resolveFromHome(configured, home) : path.join(home, 'AppData', 'Roaming');
49
+ }
50
+ if (platform === 'darwin') return path.join(home, 'Library', 'Application Support');
51
+ const configured = env.XDG_CONFIG_HOME?.trim();
52
+ return configured ? resolveFromHome(configured, home) : path.join(home, '.config');
53
+ }
54
+
55
+ /** Common package-manager locations for a user-installed tailcat. */
56
+ export function knownTailcatPaths(platform = process.platform) {
57
+ if (platform === 'darwin') return ['/opt/homebrew/bin/tailcat', '/usr/local/bin/tailcat'];
58
+ if (platform === 'linux') {
59
+ return ['/usr/local/bin/tailcat', '/usr/bin/tailcat', '/bin/tailcat', '/snap/bin/tailcat', '/home/linuxbrew/.linuxbrew/bin/tailcat'];
60
+ }
61
+ return [];
62
+ }
63
+
64
+ /**
65
+ * Find an executable tailcat in a well-known package-manager location.
66
+ * @param {string} [platform] - Node platform.
67
+ * @returns {string | null} absolute path when present.
68
+ */
69
+ export function findKnownTailcat(platform = process.platform) {
70
+ for (const candidate of knownTailcatPaths(platform)) {
71
+ try {
72
+ if (fs.statSync(candidate).isFile()) {
73
+ fs.accessSync(candidate, fs.constants.X_OK);
74
+ return candidate;
75
+ }
76
+ } catch {
77
+ // Keep looking.
78
+ }
79
+ }
80
+ return null;
24
81
  }
25
82
 
26
83
  /**
27
84
  * Resolve a tailcat key name or path to the saved-key file tailcat reads.
28
85
  * A name maps to `<user config>/tailcat/keys/<name>.private.json`; a value
29
86
  * carrying a separator is treated as a path, matching tailcat's own rule.
87
+ * Relative paths resolve from the supervisor's cwd (the home directory).
30
88
  * @param {string} keyName - configured key name or path.
89
+ * @param {string} [platform] - Node platform.
90
+ * @param {NodeJS.ProcessEnv} [env] - environment.
91
+ * @param {string} [home] - home directory.
31
92
  * @returns {string} absolute key file path.
32
93
  */
33
- export function tailcatKeyPath(keyName) {
34
- if (keyName.includes('/') || keyName.includes(path.sep)) {
35
- return path.isAbsolute(keyName) ? keyName : path.resolve(os.homedir(), keyName);
94
+ export function tailcatKeyPath(keyName, platform = process.platform, env = process.env, home = os.homedir()) {
95
+ if (keyName.includes('/') || keyName.includes('\\')) {
96
+ return path.isAbsolute(keyName) ? keyName : path.resolve(home, keyName);
36
97
  }
37
- return path.join(userConfigDir(), 'tailcat', 'keys', `${keyName}.private.json`);
98
+ return path.join(userConfigDir(platform, env, home), 'tailcat', 'keys', `${keyName}.private.json`);
38
99
  }
39
100
 
40
101
  /**
package/src/remote.js CHANGED
@@ -5,6 +5,7 @@ import { createDeviceStore } from './devices.js';
5
5
  import { Door } from './door.js';
6
6
  import { buildForwardCommand } from './parse.js';
7
7
  import { qrMatrix } from './qr.js';
8
+ import { TailcatInstaller } from './installer.js';
8
9
  import { TailcatSupervisor } from './tailcat.js';
9
10
 
10
11
  const LOCAL_CLIENT_PORT = 18_080;
@@ -25,7 +26,16 @@ export class RemoteAccess {
25
26
  this.config = config;
26
27
  this.logger = ctx.logger;
27
28
  this.devices = createDeviceStore({ logger: ctx.logger });
28
- this.tailcat = new TailcatSupervisor(ctx, config, { devices: this.devices, logger: ctx.logger });
29
+ this.installer = new TailcatInstaller(ctx, {
30
+ logger: ctx.logger,
31
+ releaseTag: `v${plugin.version}`,
32
+ requestedBinary: config.binaryPath,
33
+ });
34
+ this.tailcat = new TailcatSupervisor(ctx, config, {
35
+ devices: this.devices,
36
+ logger: ctx.logger,
37
+ installer: this.installer,
38
+ });
29
39
  this.door = new Door(ctx, config, ctx.logger);
30
40
  this.ops = Promise.resolve();
31
41
  this.startPromise = null;
@@ -54,6 +64,11 @@ export class RemoteAccess {
54
64
  this.door.lastError = errorMessage(error);
55
65
  this.logger.warn(`tailcat-remote: door unavailable: ${errorMessage(error)}`);
56
66
  }
67
+ try {
68
+ await this.installer.detectSystem();
69
+ } catch (error) {
70
+ this.logger.warn(`tailcat-remote: could not inspect an existing tailcat: ${errorMessage(error)}`);
71
+ }
57
72
  if (this.config.autoStart && this.door.phase === 'listening') {
58
73
  try {
59
74
  await this.tailcat.start();
@@ -68,6 +83,7 @@ export class RemoteAccess {
68
83
  startTunnel() {
69
84
  return this.enqueue(async () => {
70
85
  await this.ensureDoor();
86
+ await this.installer.detectSystem();
71
87
  await this.tailcat.start();
72
88
  return this.snapshot();
73
89
  });
@@ -85,6 +101,7 @@ export class RemoteAccess {
85
101
  restartTunnel() {
86
102
  return this.enqueue(async () => {
87
103
  await this.ensureDoor();
104
+ await this.installer.detectSystem();
88
105
  await this.tailcat.restart();
89
106
  return this.snapshot();
90
107
  });
@@ -94,6 +111,7 @@ export class RemoteAccess {
94
111
  rotateAddress() {
95
112
  return this.enqueue(async () => {
96
113
  await this.ensureDoor();
114
+ await this.installer.detectSystem();
97
115
  await this.tailcat.rotate();
98
116
  return this.snapshot();
99
117
  });
@@ -110,6 +128,7 @@ export class RemoteAccess {
110
128
  this.devices.replace(allow);
111
129
  if (this.tailcat.desired || this.tailcat.handle) {
112
130
  try {
131
+ await this.installer.detectSystem();
113
132
  await this.tailcat.restart();
114
133
  } catch (error) {
115
134
  this.logger.warn(`tailcat-remote: allow-list saved but tailcat did not restart: ${errorMessage(error)}`);
@@ -119,9 +138,53 @@ export class RemoteAccess {
119
138
  });
120
139
  }
121
140
 
141
+ /**
142
+ * Install the plugin-managed tailcat, then restore the previous desired
143
+ * tunnel state. A pre-existing user-installed tailcat is never modified.
144
+ * @param {{force?: boolean}} [options] - reinstall even when a managed copy exists.
145
+ */
146
+ installTailcat(options = {}) {
147
+ return this.enqueue(async () => {
148
+ if (this.config.binaryPath !== 'tailcat') {
149
+ throw new RemoteAccessError(
150
+ 'tailcat-binary-overridden',
151
+ `binaryPath is set to "${this.config.binaryPath}", so the managed install would not be used. Set binaryPath back to "tailcat" or install there manually.`,
152
+ 409,
153
+ );
154
+ }
155
+ await this.installer.detectSystem();
156
+ const wasDesired = this.tailcat.desired || Boolean(this.tailcat.handle);
157
+ if (this.tailcat.handle) await this.tailcat.stop();
158
+ try {
159
+ await this.installer.install({ force: options.force === true });
160
+ } catch (error) {
161
+ if (wasDesired && !this.disposed) {
162
+ try {
163
+ await this.ensureDoor();
164
+ await this.tailcat.start();
165
+ } catch (restartError) {
166
+ this.logger.warn(`tailcat-remote: could not restart the previous tailcat: ${errorMessage(restartError)}`);
167
+ }
168
+ }
169
+ throw error;
170
+ }
171
+ this.tailcat.reloadBinary();
172
+ if (wasDesired && !this.disposed) {
173
+ try {
174
+ await this.ensureDoor();
175
+ await this.tailcat.start();
176
+ } catch (error) {
177
+ this.logger.warn(`tailcat-remote: installed tailcat but could not start it: ${errorMessage(error)}`);
178
+ }
179
+ }
180
+ return this.snapshot();
181
+ });
182
+ }
183
+
122
184
  /** Stop both halves and release the plugin's resources. */
123
185
  async dispose() {
124
186
  this.disposed = true;
187
+ this.installer.dispose();
125
188
  try {
126
189
  await this.ops;
127
190
  } catch {
@@ -158,6 +221,7 @@ export class RemoteAccess {
158
221
  config: configView(this.config),
159
222
  door: this.door.snapshot(),
160
223
  tailcat,
224
+ installer: this.installer.snapshot(),
161
225
  devices: this.devices.snapshot(),
162
226
  pairing,
163
227
  safety: {
package/src/tailcat.js CHANGED
@@ -4,7 +4,7 @@ import os from 'node:os';
4
4
  import path from 'node:path';
5
5
  import readline from 'node:readline';
6
6
  import { RemoteAccessError, errorMessage } from './errors.js';
7
- import { doorEndpoint, tailcatKeyPath } from './paths.js';
7
+ import { doorEndpoint, findKnownTailcat, tailcatKeyPath } from './paths.js';
8
8
  import {
9
9
  buildGenkeyArgs,
10
10
  buildServeArgs,
@@ -27,13 +27,14 @@ export class TailcatSupervisor {
27
27
  /**
28
28
  * @param {object} ctx - Host plugin context.
29
29
  * @param {ReturnType<import('./config.js').resolveConfig>} config - resolved config.
30
- * @param {{ devices: ReturnType<import('./devices.js').createDeviceStore>, logger: any }} options - collaborators.
30
+ * @param {{ devices: ReturnType<import('./devices.js').createDeviceStore>, logger: any, installer?: import('./installer.js').TailcatInstaller }} options - collaborators.
31
31
  */
32
- constructor(ctx, config, { devices, logger }) {
32
+ constructor(ctx, config, { devices, logger, installer }) {
33
33
  this.ctx = ctx;
34
34
  this.config = config;
35
35
  this.devices = devices;
36
36
  this.logger = logger;
37
+ this.installer = installer;
37
38
  this.events = new EventEmitter();
38
39
  this.events.setMaxListeners(50);
39
40
 
@@ -43,7 +44,7 @@ export class TailcatSupervisor {
43
44
  this.keyMode = config.addressMode === 'ephemeral' ? 'ephemeral' : 'saved';
44
45
  this.keyFile = null;
45
46
  this.region = null;
46
- this.binary = { requested: config.binaryPath, resolved: null, version: null, error: null };
47
+ this.binary = { requested: config.binaryPath, resolved: null, source: null, version: null, error: null };
47
48
  this.restarts = 0;
48
49
  this.lastError = null;
49
50
  this.lastExit = null;
@@ -182,6 +183,7 @@ export class TailcatSupervisor {
182
183
  binary: {
183
184
  requested: this.binary.requested,
184
185
  resolved: this.binary.resolved,
186
+ source: this.binary.source,
185
187
  version: this.binary.version,
186
188
  error: this.binary.error,
187
189
  },
@@ -267,19 +269,54 @@ export class TailcatSupervisor {
267
269
  }
268
270
  }
269
271
 
272
+ /** Forget the resolved executable so the next start picks up an install. */
273
+ reloadBinary() {
274
+ this.binary.resolved = null;
275
+ this.binary.source = null;
276
+ this.binary.version = null;
277
+ this.binary.error = null;
278
+ this.versionPromise = null;
279
+ }
280
+
270
281
  /** @private */
271
282
  async ensureBinary() {
272
283
  if (this.binary.resolved) return;
284
+ const requested = this.config.binaryPath;
285
+ const managed = this.installer?.binaryPath;
286
+ if (requested === 'tailcat' && managed && this.installer?.hasInstalled()) {
287
+ this.binary.resolved = managed;
288
+ this.binary.source = 'managed';
289
+ this.binary.error = null;
290
+ this.logger.info(`tailcat-remote: using managed tailcat at ${managed}`);
291
+ void this.loadVersion();
292
+ return;
293
+ }
294
+ if (requested === 'tailcat' && this.installer?.system?.compatible === false) {
295
+ throw new RemoteAccessError(
296
+ 'tailcat-version-too-old',
297
+ `The installed tailcat ${this.installer.system.version ?? ''} is too old for remote access; install the managed version from Remote Access or upgrade tailcat to 0.8.0 or newer.`,
298
+ 503,
299
+ );
300
+ }
273
301
  try {
274
- const resolved = await this.ctx.subprocess.resolveExecutable(this.config.binaryPath, process.env);
302
+ let resolved = null;
303
+ try {
304
+ resolved = await this.ctx.subprocess.resolveExecutable(requested, process.env);
305
+ } catch (error) {
306
+ if (requested !== 'tailcat') throw error;
307
+ resolved =
308
+ this.installer?.system?.status === 'found' ? this.installer.system.path : findKnownTailcat(process.platform);
309
+ if (!resolved) throw error;
310
+ }
275
311
  this.binary.resolved = resolved;
312
+ this.binary.source = requested === 'tailcat' ? 'path' : 'config';
276
313
  this.binary.error = null;
277
314
  this.logger.info(`tailcat-remote: using ${resolved}`);
278
315
  } catch (error) {
279
316
  this.binary.error = errorMessage(error);
280
317
  throw new RemoteAccessError(
281
318
  'tailcat-not-found',
282
- `Cannot find the tailcat executable "${this.config.binaryPath}". Install tailcat, put it on PATH, or set binaryPath in the plugin config. (${errorMessage(error)})`,
319
+ `Cannot find the tailcat executable "${requested}". Use the install button in Remote Access, put tailcat on PATH, or set binaryPath in the plugin config. (${errorMessage(error)})`,
283
320
  503,
284
321
  );
285
322
  }
@@ -638,7 +675,22 @@ export class TailcatSupervisor {
638
675
  /** Environment needed by tailcat's key path and DERP cache discovery. */
639
676
  function childEnv() {
640
677
  const env = { HOME: os.homedir() };
641
- for (const name of ['XDG_CONFIG_HOME', 'XDG_CACHE_HOME', 'XDG_DATA_HOME']) {
678
+ // tailcat resolves saved keys through Go's os.UserConfigDir: XDG on Linux,
679
+ // Application Support on macOS, and %AppData% on Windows. Forward the
680
+ // platform roots it needs, plus the usual temp locations for its caches.
681
+ for (const name of [
682
+ 'PATH',
683
+ 'APPDATA',
684
+ 'LOCALAPPDATA',
685
+ 'USERPROFILE',
686
+ 'SYSTEMROOT',
687
+ 'TEMP',
688
+ 'TMP',
689
+ 'TMPDIR',
690
+ 'XDG_CONFIG_HOME',
691
+ 'XDG_CACHE_HOME',
692
+ 'XDG_DATA_HOME',
693
+ ]) {
642
694
  const value = process.env[name];
643
695
  if (value) env[name] = value;
644
696
  }