@auxiora/tailscale 1.0.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/dist/types.js ADDED
@@ -0,0 +1,7 @@
1
+ export const DEFAULT_TAILSCALE_CONFIG = {
2
+ enabled: false,
3
+ mode: 'serve',
4
+ localPort: 3000,
5
+ https: true,
6
+ };
7
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAiBA,MAAM,CAAC,MAAM,wBAAwB,GAAoB;IACvD,OAAO,EAAE,KAAK;IACd,IAAI,EAAE,OAAO;IACb,SAAS,EAAE,IAAI;IACf,KAAK,EAAE,IAAI;CACZ,CAAC"}
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "@auxiora/tailscale",
3
+ "version": "1.0.0",
4
+ "description": "Tailscale Serve/Funnel integration for exposing Auxiora without manual port forwarding",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ }
13
+ },
14
+ "dependencies": {
15
+ "@auxiora/logger": "1.0.0"
16
+ },
17
+ "engines": {
18
+ "node": ">=22.0.0"
19
+ },
20
+ "scripts": {
21
+ "build": "tsc",
22
+ "clean": "rm -rf dist",
23
+ "typecheck": "tsc --noEmit"
24
+ }
25
+ }
@@ -0,0 +1,224 @@
1
+ import { describe, it, expect, beforeEach } from 'vitest';
2
+ import type { CommandExecutor } from '../types.js';
3
+ import { TailscaleManager } from '../manager.js';
4
+
5
+ function makeMockRunner(): CommandExecutor & {
6
+ calls: Array<{ command: string; args: string[] }>;
7
+ setResponse(stdout: string, stderr?: string, exitCode?: number): void;
8
+ setResponseForArgs(args: string[], stdout: string, stderr?: string, exitCode?: number): void;
9
+ } {
10
+ const responses = new Map<string, { stdout: string; stderr: string; exitCode: number }>();
11
+ let defaultResponse = { stdout: '', stderr: '', exitCode: 0 };
12
+ const calls: Array<{ command: string; args: string[] }> = [];
13
+
14
+ return {
15
+ calls,
16
+ setResponse(stdout: string, stderr = '', exitCode = 0) {
17
+ defaultResponse = { stdout, stderr, exitCode };
18
+ },
19
+ setResponseForArgs(args: string[], stdout: string, stderr = '', exitCode = 0) {
20
+ responses.set(args.join(' '), { stdout, stderr, exitCode });
21
+ },
22
+ async run(command: string, args: string[]) {
23
+ calls.push({ command, args });
24
+ const key = args.join(' ');
25
+ return responses.get(key) ?? defaultResponse;
26
+ },
27
+ };
28
+ }
29
+
30
+ const TAILSCALE_STATUS_JSON = JSON.stringify({
31
+ BackendState: 'Running',
32
+ Self: {
33
+ HostName: 'my-machine',
34
+ DNSName: 'my-machine.tail1234.ts.net.',
35
+ TailscaleIPs: ['100.64.0.1', 'fd7a:115c::1'],
36
+ },
37
+ CurrentTailnet: {
38
+ Name: 'tail1234.ts.net',
39
+ },
40
+ });
41
+
42
+ describe('TailscaleManager', () => {
43
+ let runner: ReturnType<typeof makeMockRunner>;
44
+ let manager: TailscaleManager;
45
+
46
+ beforeEach(() => {
47
+ runner = makeMockRunner();
48
+ manager = new TailscaleManager({ enabled: true, localPort: 3000 }, runner);
49
+ });
50
+
51
+ it('requires a CommandExecutor', () => {
52
+ expect(() => new TailscaleManager()).toThrow('CommandExecutor is required');
53
+ });
54
+
55
+ describe('detect', () => {
56
+ it('returns true when tailscale CLI is found', async () => {
57
+ runner.setResponse('1.62.0\n go1.22');
58
+ const result = await manager.detect();
59
+ expect(result).toBe(true);
60
+ expect(runner.calls[0]).toEqual({ command: 'tailscale', args: ['version'] });
61
+ });
62
+
63
+ it('returns false when tailscale CLI is not found', async () => {
64
+ runner.setResponse('', 'command not found', 127);
65
+ const result = await manager.detect();
66
+ expect(result).toBe(false);
67
+ expect(manager.getStatus()).toBe('not-installed');
68
+ });
69
+ });
70
+
71
+ describe('getInfo', () => {
72
+ it('returns not-installed if tailscale is missing', async () => {
73
+ runner.setResponse('', 'not found', 127);
74
+ const info = await manager.getInfo();
75
+ expect(info.status).toBe('not-installed');
76
+ });
77
+
78
+ it('returns not-running if status command fails', async () => {
79
+ runner.setResponseForArgs(['version'], '1.62.0', '', 0);
80
+ runner.setResponseForArgs(['status', '--json'], '', 'daemon not running', 1);
81
+ const info = await manager.getInfo();
82
+ expect(info.status).toBe('not-running');
83
+ });
84
+
85
+ it('returns ready with machine info when running', async () => {
86
+ runner.setResponseForArgs(['version'], '1.62.0', '', 0);
87
+ runner.setResponseForArgs(['status', '--json'], TAILSCALE_STATUS_JSON, '', 0);
88
+ const info = await manager.getInfo();
89
+ expect(info.status).toBe('ready');
90
+ expect(info.hostname).toBe('my-machine');
91
+ expect(info.tailnet).toBe('tail1234.ts.net');
92
+ expect(info.ipAddress).toBe('100.64.0.1');
93
+ });
94
+
95
+ it('returns not-logged-in when backend needs login', async () => {
96
+ runner.setResponseForArgs(['version'], '1.62.0', '', 0);
97
+ runner.setResponseForArgs(
98
+ ['status', '--json'],
99
+ JSON.stringify({ BackendState: 'NeedsLogin' }),
100
+ '',
101
+ 0,
102
+ );
103
+ const info = await manager.getInfo();
104
+ expect(info.status).toBe('not-logged-in');
105
+ });
106
+
107
+ it('returns error on invalid JSON', async () => {
108
+ runner.setResponseForArgs(['version'], '1.62.0', '', 0);
109
+ runner.setResponseForArgs(['status', '--json'], 'not-json', '', 0);
110
+ const info = await manager.getInfo();
111
+ expect(info.status).toBe('error');
112
+ });
113
+ });
114
+
115
+ describe('serve', () => {
116
+ it('starts tailscale serve and returns info', async () => {
117
+ runner.setResponseForArgs(['serve', 'https://localhost:3000'], '', '', 0);
118
+ runner.setResponseForArgs(['version'], '1.62.0', '', 0);
119
+ runner.setResponseForArgs(['status', '--json'], TAILSCALE_STATUS_JSON, '', 0);
120
+
121
+ const info = await manager.serve();
122
+ expect(info.status).toBe('serving');
123
+ expect(info.serveUrl).toBe('https://my-machine.tail1234.ts.net');
124
+ expect(manager.getActiveMode()).toBe('serve');
125
+ });
126
+
127
+ it('uses custom port', async () => {
128
+ runner.setResponseForArgs(['serve', 'https://localhost:8080'], '', '', 0);
129
+ runner.setResponseForArgs(['version'], '1.62.0', '', 0);
130
+ runner.setResponseForArgs(['status', '--json'], TAILSCALE_STATUS_JSON, '', 0);
131
+
132
+ await manager.serve(8080);
133
+ expect(runner.calls[0]).toEqual({
134
+ command: 'tailscale',
135
+ args: ['serve', 'https://localhost:8080'],
136
+ });
137
+ });
138
+
139
+ it('throws on failure', async () => {
140
+ runner.setResponse('', 'access denied', 1);
141
+ await expect(manager.serve()).rejects.toThrow('Tailscale serve failed: access denied');
142
+ });
143
+ });
144
+
145
+ describe('funnel', () => {
146
+ it('starts tailscale funnel and returns info', async () => {
147
+ runner.setResponseForArgs(['funnel', 'https://localhost:3000'], '', '', 0);
148
+ runner.setResponseForArgs(['version'], '1.62.0', '', 0);
149
+ runner.setResponseForArgs(['status', '--json'], TAILSCALE_STATUS_JSON, '', 0);
150
+
151
+ const info = await manager.funnel();
152
+ expect(info.status).toBe('serving');
153
+ expect(info.publicUrl).toBe('https://my-machine.tail1234.ts.net');
154
+ expect(manager.getActiveMode()).toBe('funnel');
155
+ });
156
+
157
+ it('throws on failure', async () => {
158
+ runner.setResponse('', 'funnel not enabled', 1);
159
+ await expect(manager.funnel()).rejects.toThrow('Tailscale funnel failed: funnel not enabled');
160
+ });
161
+ });
162
+
163
+ describe('stop', () => {
164
+ it('does nothing when no active mode', async () => {
165
+ await manager.stop();
166
+ expect(runner.calls).toHaveLength(0);
167
+ });
168
+
169
+ it('stops active serve', async () => {
170
+ runner.setResponseForArgs(['serve', 'https://localhost:3000'], '', '', 0);
171
+ runner.setResponseForArgs(['version'], '1.62.0', '', 0);
172
+ runner.setResponseForArgs(['status', '--json'], TAILSCALE_STATUS_JSON, '', 0);
173
+ await manager.serve();
174
+
175
+ runner.calls.length = 0;
176
+ runner.setResponse('', '', 0);
177
+
178
+ await manager.stop();
179
+ expect(runner.calls[0]).toEqual({
180
+ command: 'tailscale',
181
+ args: ['serve', '--remove'],
182
+ });
183
+ expect(manager.getActiveMode()).toBeNull();
184
+ expect(manager.getStatus()).toBe('ready');
185
+ });
186
+
187
+ it('stops active funnel', async () => {
188
+ runner.setResponseForArgs(['funnel', 'https://localhost:3000'], '', '', 0);
189
+ runner.setResponseForArgs(['version'], '1.62.0', '', 0);
190
+ runner.setResponseForArgs(['status', '--json'], TAILSCALE_STATUS_JSON, '', 0);
191
+ await manager.funnel();
192
+
193
+ runner.calls.length = 0;
194
+ runner.setResponse('', '', 0);
195
+
196
+ await manager.stop();
197
+ expect(runner.calls[0]).toEqual({
198
+ command: 'tailscale',
199
+ args: ['funnel', '--remove'],
200
+ });
201
+ });
202
+ });
203
+
204
+ describe('config', () => {
205
+ it('uses http when https is disabled', async () => {
206
+ const mgr = new TailscaleManager({ https: false, localPort: 3000 }, runner);
207
+ runner.setResponse('', '', 0);
208
+ runner.setResponseForArgs(['version'], '1.62.0', '', 0);
209
+ runner.setResponseForArgs(['status', '--json'], TAILSCALE_STATUS_JSON, '', 0);
210
+
211
+ await mgr.serve();
212
+ expect(runner.calls[0]).toEqual({
213
+ command: 'tailscale',
214
+ args: ['serve', 'http://localhost:3000'],
215
+ });
216
+ });
217
+
218
+ it('returns a copy of config', () => {
219
+ const config = manager.getConfig();
220
+ expect(config.localPort).toBe(3000);
221
+ expect(config.enabled).toBe(true);
222
+ });
223
+ });
224
+ });
package/src/index.ts ADDED
@@ -0,0 +1,9 @@
1
+ export type {
2
+ TailscaleConfig,
3
+ TailscaleMode,
4
+ TailscaleStatus,
5
+ TailscaleInfo,
6
+ CommandExecutor,
7
+ } from './types.js';
8
+ export { DEFAULT_TAILSCALE_CONFIG } from './types.js';
9
+ export { TailscaleManager } from './manager.js';
package/src/manager.ts ADDED
@@ -0,0 +1,179 @@
1
+ import { getLogger } from '@auxiora/logger';
2
+ import type {
3
+ TailscaleConfig,
4
+ TailscaleInfo,
5
+ TailscaleMode,
6
+ TailscaleStatus,
7
+ CommandExecutor,
8
+ } from './types.js';
9
+ import { DEFAULT_TAILSCALE_CONFIG } from './types.js';
10
+
11
+ const logger = getLogger('tailscale');
12
+
13
+ /**
14
+ * Manages Tailscale Serve and Funnel for exposing Auxiora
15
+ * to the local tailnet or the public internet.
16
+ */
17
+ export class TailscaleManager {
18
+ private config: TailscaleConfig;
19
+ private runner: CommandExecutor;
20
+ private currentStatus: TailscaleStatus = 'not-installed';
21
+ private activeMode: TailscaleMode | null = null;
22
+
23
+ constructor(config?: Partial<TailscaleConfig>, runner?: CommandExecutor) {
24
+ this.config = { ...DEFAULT_TAILSCALE_CONFIG, ...config };
25
+ if (!runner) {
26
+ throw new Error('CommandExecutor is required — use ProcessCommandRunner for production');
27
+ }
28
+ this.runner = runner;
29
+ }
30
+
31
+ /** Detect whether the Tailscale CLI is installed and reachable. */
32
+ async detect(): Promise<boolean> {
33
+ const result = await this.runner.run('tailscale', ['version']);
34
+ if (result.exitCode !== 0) {
35
+ this.currentStatus = 'not-installed';
36
+ logger.debug('Tailscale CLI not found');
37
+ return false;
38
+ }
39
+ logger.info('Tailscale CLI detected', { version: result.stdout.trim().split('\n')[0] });
40
+ return true;
41
+ }
42
+
43
+ /** Get detailed status information from Tailscale. */
44
+ async getInfo(): Promise<TailscaleInfo> {
45
+ const installed = await this.detect();
46
+ if (!installed) {
47
+ return { status: 'not-installed' };
48
+ }
49
+
50
+ const result = await this.runner.run('tailscale', ['status', '--json']);
51
+ if (result.exitCode !== 0) {
52
+ this.currentStatus = 'not-running';
53
+ return { status: 'not-running' };
54
+ }
55
+
56
+ try {
57
+ const status = JSON.parse(result.stdout) as {
58
+ Self?: {
59
+ HostName?: string;
60
+ DNSName?: string;
61
+ TailscaleIPs?: string[];
62
+ };
63
+ CurrentTailnet?: { Name?: string };
64
+ BackendState?: string;
65
+ };
66
+
67
+ if (status.BackendState !== 'Running') {
68
+ this.currentStatus = status.BackendState === 'NeedsLogin' ? 'not-logged-in' : 'not-running';
69
+ return { status: this.currentStatus };
70
+ }
71
+
72
+ const hostname = status.Self?.HostName;
73
+ const dnsName = status.Self?.DNSName;
74
+ const tailnet = status.CurrentTailnet?.Name;
75
+ const ipAddress = status.Self?.TailscaleIPs?.[0];
76
+
77
+ this.currentStatus = this.activeMode ? 'serving' : 'ready';
78
+
79
+ const info: TailscaleInfo = {
80
+ status: this.currentStatus,
81
+ hostname,
82
+ tailnet,
83
+ ipAddress,
84
+ };
85
+
86
+ if (dnsName && this.activeMode === 'serve') {
87
+ info.serveUrl = `https://${dnsName.replace(/\.$/, '')}`;
88
+ }
89
+ if (dnsName && this.activeMode === 'funnel') {
90
+ info.publicUrl = `https://${dnsName.replace(/\.$/, '')}`;
91
+ }
92
+
93
+ return info;
94
+ } catch {
95
+ this.currentStatus = 'error';
96
+ return { status: 'error' };
97
+ }
98
+ }
99
+
100
+ /** Start Tailscale Serve — expose Auxiora on the local tailnet. */
101
+ async serve(port?: number): Promise<TailscaleInfo> {
102
+ const targetPort = port ?? this.config.localPort;
103
+ logger.info('Starting Tailscale Serve', { port: targetPort });
104
+
105
+ const proto = this.config.https ? 'https' : 'http';
106
+ const result = await this.runner.run('tailscale', [
107
+ 'serve',
108
+ `${proto}://localhost:${targetPort}`,
109
+ ]);
110
+
111
+ if (result.exitCode !== 0) {
112
+ logger.error('Failed to start Tailscale Serve', { error: new Error(result.stderr) });
113
+ throw new Error(`Tailscale serve failed: ${result.stderr}`);
114
+ }
115
+
116
+ this.activeMode = 'serve';
117
+ this.currentStatus = 'serving';
118
+ logger.info('Tailscale Serve started');
119
+ return this.getInfo();
120
+ }
121
+
122
+ /** Start Tailscale Funnel — expose Auxiora to the public internet. */
123
+ async funnel(port?: number): Promise<TailscaleInfo> {
124
+ const targetPort = port ?? this.config.localPort;
125
+ logger.info('Starting Tailscale Funnel', { port: targetPort });
126
+
127
+ const proto = this.config.https ? 'https' : 'http';
128
+ const result = await this.runner.run('tailscale', [
129
+ 'funnel',
130
+ `${proto}://localhost:${targetPort}`,
131
+ ]);
132
+
133
+ if (result.exitCode !== 0) {
134
+ logger.error('Failed to start Tailscale Funnel', { error: new Error(result.stderr) });
135
+ throw new Error(`Tailscale funnel failed: ${result.stderr}`);
136
+ }
137
+
138
+ this.activeMode = 'funnel';
139
+ this.currentStatus = 'serving';
140
+ logger.info('Tailscale Funnel started');
141
+ return this.getInfo();
142
+ }
143
+
144
+ /** Stop any active Tailscale Serve or Funnel. */
145
+ async stop(): Promise<void> {
146
+ if (!this.activeMode) {
147
+ logger.debug('No active Tailscale serve/funnel to stop');
148
+ return;
149
+ }
150
+
151
+ logger.info('Stopping Tailscale serve/funnel', { mode: this.activeMode });
152
+
153
+ const command = this.activeMode === 'funnel' ? 'funnel' : 'serve';
154
+ const result = await this.runner.run('tailscale', [command, '--remove']);
155
+
156
+ if (result.exitCode !== 0) {
157
+ logger.warn('Failed to cleanly stop Tailscale', { error: new Error(result.stderr) });
158
+ }
159
+
160
+ this.activeMode = null;
161
+ this.currentStatus = 'ready';
162
+ logger.info('Tailscale stopped');
163
+ }
164
+
165
+ /** Get the current status. */
166
+ getStatus(): TailscaleStatus {
167
+ return this.currentStatus;
168
+ }
169
+
170
+ /** Get the active mode (serve, funnel, or null). */
171
+ getActiveMode(): TailscaleMode | null {
172
+ return this.activeMode;
173
+ }
174
+
175
+ /** Get the current config. */
176
+ getConfig(): TailscaleConfig {
177
+ return { ...this.config };
178
+ }
179
+ }
package/src/types.ts ADDED
@@ -0,0 +1,52 @@
1
+ /** Tailscale exposure mode. */
2
+ export type TailscaleMode = 'serve' | 'funnel';
3
+
4
+ /** Configuration for Tailscale integration. */
5
+ export interface TailscaleConfig {
6
+ /** Whether Tailscale integration is enabled. */
7
+ enabled: boolean;
8
+ /** Exposure mode: serve (local tailnet) or funnel (public internet). */
9
+ mode: TailscaleMode;
10
+ /** Custom hostname for the Tailscale machine (optional). */
11
+ hostname?: string;
12
+ /** Local port to proxy traffic to. */
13
+ localPort: number;
14
+ /** Whether to use HTTPS on the Tailscale side. */
15
+ https: boolean;
16
+ }
17
+
18
+ export const DEFAULT_TAILSCALE_CONFIG: TailscaleConfig = {
19
+ enabled: false,
20
+ mode: 'serve',
21
+ localPort: 3000,
22
+ https: true,
23
+ };
24
+
25
+ /** Status of the Tailscale integration. */
26
+ export type TailscaleStatus =
27
+ | 'not-installed'
28
+ | 'not-running'
29
+ | 'not-logged-in'
30
+ | 'ready'
31
+ | 'serving'
32
+ | 'error';
33
+
34
+ /** Information about the current Tailscale state. */
35
+ export interface TailscaleInfo {
36
+ status: TailscaleStatus;
37
+ /** The Tailscale hostname (e.g., "my-machine"). */
38
+ hostname?: string;
39
+ /** The tailnet domain (e.g., "tail1234.ts.net"). */
40
+ tailnet?: string;
41
+ /** The public URL when serving via funnel. */
42
+ publicUrl?: string;
43
+ /** The local tailnet URL when serving via serve. */
44
+ serveUrl?: string;
45
+ /** The Tailscale IP address. */
46
+ ipAddress?: string;
47
+ }
48
+
49
+ /** Interface for running CLI commands (injectable for testing). */
50
+ export interface CommandExecutor {
51
+ run(command: string, args: string[]): Promise<{ stdout: string; stderr: string; exitCode: number }>;
52
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ "rootDir": "./src"
6
+ },
7
+ "include": ["src/**/*"],
8
+ "references": [
9
+ { "path": "../logger" }
10
+ ]
11
+ }