@elizaos/plugin-tunnel 2.0.0-beta.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.
Files changed (53) hide show
  1. package/README.md +39 -0
  2. package/dist/__tests__/TunnelTestSuite.d.ts +6 -0
  3. package/dist/__tests__/TunnelTestSuite.d.ts.map +1 -0
  4. package/dist/__tests__/TunnelTestSuite.js +47 -0
  5. package/dist/__tests__/TunnelTestSuite.js.map +1 -0
  6. package/dist/actions/get-tunnel-status.d.ts +3 -0
  7. package/dist/actions/get-tunnel-status.d.ts.map +1 -0
  8. package/dist/actions/get-tunnel-status.js +43 -0
  9. package/dist/actions/get-tunnel-status.js.map +1 -0
  10. package/dist/actions/start-tunnel.d.ts +3 -0
  11. package/dist/actions/start-tunnel.d.ts.map +1 -0
  12. package/dist/actions/start-tunnel.js +96 -0
  13. package/dist/actions/start-tunnel.js.map +1 -0
  14. package/dist/actions/stop-tunnel.d.ts +3 -0
  15. package/dist/actions/stop-tunnel.d.ts.map +1 -0
  16. package/dist/actions/stop-tunnel.js +41 -0
  17. package/dist/actions/stop-tunnel.js.map +1 -0
  18. package/dist/actions/tunnel.d.ts +20 -0
  19. package/dist/actions/tunnel.d.ts.map +1 -0
  20. package/dist/actions/tunnel.js +159 -0
  21. package/dist/actions/tunnel.js.map +1 -0
  22. package/dist/environment.d.ts +18 -0
  23. package/dist/environment.d.ts.map +1 -0
  24. package/dist/environment.js +66 -0
  25. package/dist/environment.js.map +1 -0
  26. package/dist/index.d.ts +21 -0
  27. package/dist/index.d.ts.map +1 -0
  28. package/dist/index.js +46 -0
  29. package/dist/index.js.map +1 -0
  30. package/dist/providers/tunnel-state.d.ts +12 -0
  31. package/dist/providers/tunnel-state.d.ts.map +1 -0
  32. package/dist/providers/tunnel-state.js +43 -0
  33. package/dist/providers/tunnel-state.js.map +1 -0
  34. package/dist/services/LocalTunnelService.d.ts +37 -0
  35. package/dist/services/LocalTunnelService.d.ts.map +1 -0
  36. package/dist/services/LocalTunnelService.js +186 -0
  37. package/dist/services/LocalTunnelService.js.map +1 -0
  38. package/dist/types.d.ts +47 -0
  39. package/dist/types.d.ts.map +1 -0
  40. package/dist/types.js +34 -0
  41. package/dist/types.js.map +1 -0
  42. package/package.json +59 -0
  43. package/src/__tests__/TunnelTestSuite.ts +46 -0
  44. package/src/__tests__/unit/local-tunnel-service.test.ts +23 -0
  45. package/src/actions/get-tunnel-status.ts +60 -0
  46. package/src/actions/start-tunnel.ts +117 -0
  47. package/src/actions/stop-tunnel.ts +58 -0
  48. package/src/actions/tunnel.ts +193 -0
  49. package/src/environment.ts +74 -0
  50. package/src/index.ts +59 -0
  51. package/src/providers/tunnel-state.ts +46 -0
  52. package/src/services/LocalTunnelService.ts +228 -0
  53. package/src/types.ts +62 -0
@@ -0,0 +1,228 @@
1
+ import { spawn } from 'node:child_process';
2
+ import { elizaLogger, type IAgentRuntime, Service } from '@elizaos/core';
3
+ import { z } from 'zod';
4
+ import { validateTunnelConfig } from '../environment';
5
+ import type { ITunnelService, TunnelStatus } from '../types';
6
+
7
+ const tailscaleStatusPeerSchema = z.object({
8
+ DNSName: z.string().optional(),
9
+ Online: z.boolean().optional(),
10
+ });
11
+
12
+ const tailscaleStatusSchema = z.object({
13
+ Self: z
14
+ .object({
15
+ DNSName: z.string().optional(),
16
+ })
17
+ .optional(),
18
+ MagicDNSSuffix: z.string().optional(),
19
+ Peer: z.record(z.string(), tailscaleStatusPeerSchema).optional(),
20
+ });
21
+
22
+ type TailscaleStatus = z.infer<typeof tailscaleStatusSchema>;
23
+
24
+ interface SpawnResult {
25
+ code: number | null;
26
+ stdout: string;
27
+ stderr: string;
28
+ }
29
+
30
+ function runCommand(cmd: string, args: string[]): Promise<SpawnResult> {
31
+ return new Promise((resolve, reject) => {
32
+ const child = spawn(cmd, args, { stdio: ['ignore', 'pipe', 'pipe'] });
33
+ const out: Buffer[] = [];
34
+ const err: Buffer[] = [];
35
+ child.stdout?.on('data', (chunk: Buffer) => out.push(chunk));
36
+ child.stderr?.on('data', (chunk: Buffer) => err.push(chunk));
37
+ child.on('error', reject);
38
+ child.on('exit', (code) =>
39
+ resolve({
40
+ code,
41
+ stdout: Buffer.concat(out).toString('utf8'),
42
+ stderr: Buffer.concat(err).toString('utf8'),
43
+ })
44
+ );
45
+ });
46
+ }
47
+
48
+ export function checkTailscaleInstalled(): Promise<boolean> {
49
+ return new Promise((resolve) => {
50
+ const proc = spawn('which', ['tailscale']);
51
+ proc.on('exit', (code) => resolve(code === 0));
52
+ proc.on('error', () => resolve(false));
53
+ });
54
+ }
55
+
56
+ function parseTailscaleStatus(stdout: string): TailscaleStatus | null {
57
+ let raw: unknown;
58
+ try {
59
+ raw = JSON.parse(stdout);
60
+ } catch {
61
+ return null;
62
+ }
63
+ const result = tailscaleStatusSchema.safeParse(raw);
64
+ return result.success ? result.data : null;
65
+ }
66
+
67
+ /**
68
+ * Tunnel service backed by the locally-installed `tailscale` CLI.
69
+ *
70
+ * The user is responsible for `tailscale up` (i.e. authenticating with
71
+ * Tailscale's coordination server, OR with a self-hosted headscale via
72
+ * `--login-server`). This service just calls `tailscale serve` / `tailscale
73
+ * funnel` to expose a port and reads `tailscale status --json` to learn the
74
+ * tailnet DNS name.
75
+ *
76
+ * Coexists with `@elizaos/plugin-elizacloud`'s cloud tunnel and
77
+ * `@elizaos/plugin-ngrok` — only the first one to register `serviceType="tunnel"`
78
+ * wins. This service registers itself only if the `tailscale` binary is on
79
+ * `PATH`, gating out machines that have no Tailscale install.
80
+ */
81
+ export class LocalTunnelService extends Service implements ITunnelService {
82
+ static override serviceType = 'tunnel';
83
+ readonly capabilityDescription =
84
+ 'Tunnel via the locally-installed `tailscale` CLI (serve / funnel). User authenticates separately with `tailscale up`.';
85
+
86
+ private tunnelUrl: string | null = null;
87
+ private tunnelPort: number | null = null;
88
+ private startedAt: Date | null = null;
89
+ private isShuttingDown = false;
90
+ private useFunnel = false;
91
+
92
+ static override async start(runtime: IAgentRuntime): Promise<Service> {
93
+ const service = new LocalTunnelService(runtime);
94
+ await service.start();
95
+ return service;
96
+ }
97
+
98
+ async start(): Promise<void> {
99
+ elizaLogger.info('[LocalTunnelService] starting');
100
+ const installed = await checkTailscaleInstalled();
101
+ if (!installed) {
102
+ throw new Error(
103
+ 'tailscale is not installed. Install from https://tailscale.com/download or run: brew install tailscale'
104
+ );
105
+ }
106
+ }
107
+
108
+ async stop(): Promise<void> {
109
+ await this.stopTunnel();
110
+ }
111
+
112
+ async startTunnel(port?: number): Promise<string | undefined> {
113
+ if (this.isActive()) {
114
+ elizaLogger.warn('[LocalTunnelService] tunnel already running');
115
+ return this.tunnelUrl ?? undefined;
116
+ }
117
+
118
+ if (port === undefined || port === null) {
119
+ elizaLogger.warn(
120
+ '[LocalTunnelService] startTunnel called without a port — service active but no tunnel started'
121
+ );
122
+ return;
123
+ }
124
+
125
+ if (port < 1 || port > 65535) {
126
+ throw new Error('Invalid port number');
127
+ }
128
+
129
+ const config = await validateTunnelConfig(this.runtime);
130
+ this.useFunnel = config.TUNNEL_FUNNEL;
131
+
132
+ elizaLogger.info(
133
+ `[LocalTunnelService] starting tunnel on port ${port} (funnel=${this.useFunnel})`
134
+ );
135
+
136
+ if (this.useFunnel) {
137
+ const result = await runCommand('tailscale', ['funnel', String(port)]);
138
+ if (result.code !== 0) {
139
+ throw new Error(
140
+ `tailscale funnel exited with code ${result.code}: ${result.stderr.trim()}`
141
+ );
142
+ }
143
+ } else {
144
+ const result = await runCommand('tailscale', [
145
+ 'serve',
146
+ '--bg',
147
+ '--https=443',
148
+ `localhost:${port}`,
149
+ ]);
150
+ if (result.code !== 0) {
151
+ throw new Error(`tailscale serve exited with code ${result.code}: ${result.stderr.trim()}`);
152
+ }
153
+ }
154
+
155
+ const dnsName = await this.fetchSelfDnsName();
156
+ if (!dnsName) {
157
+ throw new Error(
158
+ 'tailscale serve started but no DNSName resolved from `tailscale status --json`'
159
+ );
160
+ }
161
+ this.tunnelUrl = `https://${dnsName}`;
162
+ this.tunnelPort = port;
163
+ this.startedAt = new Date();
164
+ elizaLogger.info(`[LocalTunnelService] tunnel started: ${this.tunnelUrl}`);
165
+ return this.tunnelUrl;
166
+ }
167
+
168
+ async stopTunnel(): Promise<void> {
169
+ if (!this.isActive()) {
170
+ elizaLogger.warn('[LocalTunnelService] no active tunnel to stop');
171
+ return;
172
+ }
173
+ this.isShuttingDown = true;
174
+ elizaLogger.info('[LocalTunnelService] stopping tunnel');
175
+
176
+ if (this.useFunnel) {
177
+ await runCommand('tailscale', ['funnel', 'reset']);
178
+ } else {
179
+ await runCommand('tailscale', ['serve', 'reset']);
180
+ }
181
+
182
+ this.cleanup();
183
+ this.isShuttingDown = false;
184
+ elizaLogger.info('[LocalTunnelService] tunnel stopped');
185
+ }
186
+
187
+ getUrl(): string | null {
188
+ return this.tunnelUrl;
189
+ }
190
+
191
+ isActive(): boolean {
192
+ return this.tunnelUrl !== null && !this.isShuttingDown;
193
+ }
194
+
195
+ getStatus(): TunnelStatus {
196
+ return {
197
+ active: this.isActive(),
198
+ url: this.tunnelUrl,
199
+ port: this.tunnelPort,
200
+ startedAt: this.startedAt,
201
+ provider: 'tailscale',
202
+ backend: 'local-cli',
203
+ };
204
+ }
205
+
206
+ private async fetchSelfDnsName(): Promise<string | null> {
207
+ const result = await runCommand('tailscale', ['status', '--json']);
208
+ if (result.code !== 0) {
209
+ elizaLogger.error(`[LocalTunnelService] tailscale status failed: ${result.stderr.trim()}`);
210
+ return null;
211
+ }
212
+ const status = parseTailscaleStatus(result.stdout);
213
+ if (!status) {
214
+ elizaLogger.error('[LocalTunnelService] tailscale status returned malformed JSON');
215
+ return null;
216
+ }
217
+ const raw = status.Self?.DNSName;
218
+ if (!raw) return null;
219
+ return raw.replace(/\.$/, '');
220
+ }
221
+
222
+ private cleanup(): void {
223
+ this.tunnelUrl = null;
224
+ this.tunnelPort = null;
225
+ this.startedAt = null;
226
+ this.useFunnel = false;
227
+ }
228
+ }
package/src/types.ts ADDED
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Tunnel-service contract shared across all elizaOS tunnel plugins.
3
+ *
4
+ * All tunnel plugins (`@elizaos/plugin-tunnel`, `@elizaos/plugin-elizacloud`'s
5
+ * cloud tunnel, `@elizaos/plugin-ngrok`) register under
6
+ * `serviceType = "tunnel"` so consumers stay backend-agnostic via
7
+ * `runtime.getService("tunnel")`. The runtime returns the FIRST registered
8
+ * service for a given type, so plugins coordinate via conditional
9
+ * registration: each plugin's `init` only registers if its credentials are
10
+ * present and no other tunnel service has already claimed the slot.
11
+ */
12
+
13
+ import type { IAgentRuntime, Service } from '@elizaos/core';
14
+
15
+ declare module '@elizaos/core' {
16
+ interface ServiceTypeRegistry {
17
+ TUNNEL: 'tunnel';
18
+ }
19
+ }
20
+
21
+ export type TunnelProvider = 'tailscale' | 'headscale' | 'ngrok';
22
+
23
+ export interface TunnelStatus {
24
+ active: boolean;
25
+ url: string | null;
26
+ port: number | null;
27
+ startedAt: Date | null;
28
+ provider: TunnelProvider;
29
+ /** Optional human label distinguishing backend variants (e.g. "local-cli", "eliza-cloud-headscale"). */
30
+ backend?: string;
31
+ }
32
+
33
+ export interface ITunnelService {
34
+ startTunnel(port?: number): Promise<string | undefined>;
35
+ stopTunnel(): Promise<void>;
36
+ getUrl(): string | null;
37
+ isActive(): boolean;
38
+ getStatus(): TunnelStatus;
39
+ }
40
+
41
+ /**
42
+ * Backend-agnostic accessor. Returns the first registered service that
43
+ * implements the tunnel contract; returns null if nothing is registered or
44
+ * the registered service doesn't satisfy the shape (defensive guard against
45
+ * an unrelated service registering under "tunnel").
46
+ */
47
+ export function getTunnelService(runtime: IAgentRuntime): ITunnelService | null {
48
+ const service = runtime.getService('tunnel');
49
+ if (!service) return null;
50
+ if (typeof (service as Partial<ITunnelService>).startTunnel !== 'function') {
51
+ return null;
52
+ }
53
+ return service as Service & ITunnelService;
54
+ }
55
+
56
+ /**
57
+ * True iff no tunnel service has claimed `serviceType="tunnel"` yet.
58
+ * Used by tunnel plugins' `init` hooks to coordinate "first active wins".
59
+ */
60
+ export function tunnelSlotIsFree(runtime: IAgentRuntime): boolean {
61
+ return runtime.getService('tunnel') === null;
62
+ }