@hmduc16031996/claude-mb-bridge 2.4.8 → 2.5.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/dist/index.js +22 -39
- package/dist/tunnel.d.ts +1 -10
- package/dist/tunnel.js +1 -53
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -2,89 +2,72 @@
|
|
|
2
2
|
import { Command } from 'commander';
|
|
3
3
|
import { CloudflareTunnel } from './tunnel.js';
|
|
4
4
|
import { startTerminalServer } from './server.js';
|
|
5
|
-
// ─── Built-in Cloudflare named tunnel (mobileideforusers.kanddlabs.com) ───────
|
|
6
|
-
// These are embedded at publish time. End-users don't need to supply them.
|
|
7
|
-
const BUILTIN_CF_TOKEN = 'eyJhIjoiZmM5YTQ4ZTU3NjY4NjI3MjY2OWE3YzBiMTliNTcyYzgiLCJ0IjoiMjgzM2E5NjMtNTBiYi00YThjLWJhMGMtNDI4NWE4ZTJkZmJjIiwicyI6IllqYzVaakF3T1RJdFpEWXdZeTAwWkRFNUxUZzVNR0l0WXpBeE1UazJNamc1T1dZeCJ9';
|
|
8
|
-
const BUILTIN_TUNNEL_URL = 'https://mobileideforusers.kanddlabs.com';
|
|
9
|
-
// ──────────────────────────────────────────────────────────────────────────────
|
|
10
5
|
const program = new Command();
|
|
11
6
|
program
|
|
12
7
|
.name('claude-mobile-bridge')
|
|
13
8
|
.description('Bridge Claude Code CLI to mobile via WebView')
|
|
14
|
-
.version('2.4.
|
|
9
|
+
.version('2.4.1')
|
|
15
10
|
.option('--token <token>', 'Pairing token from mobile app')
|
|
16
11
|
.option('--server <url>', 'Backend server URL', 'http://127.0.0.1:3110')
|
|
17
12
|
.option('--path <path>', 'Working directory', process.cwd())
|
|
18
13
|
.option('--port <port>', 'Local port for terminal server', '38473')
|
|
19
|
-
.option('--ide <type>', 'IDE type: claude_code
|
|
20
|
-
// Advanced: override the built-in tunnel with your own
|
|
21
|
-
.option('--cloudflare-token <cfToken>', 'Override built-in Cloudflare tunnel token')
|
|
22
|
-
.option('--tunnel-url <url>', 'Override built-in tunnel public URL')
|
|
23
|
-
// Escape hatch: fall back to ephemeral trycloudflare
|
|
24
|
-
.option('--quick-tunnel', 'Use ephemeral trycloudflare.com tunnel instead of the named tunnel')
|
|
14
|
+
.option('--ide <type>', 'IDE type: claude_code or cursor', 'claude_code')
|
|
25
15
|
.action(async (options) => {
|
|
26
|
-
const { token, server, path, port, ide
|
|
16
|
+
const { token, server, path, port, ide } = options;
|
|
27
17
|
if (!token) {
|
|
28
18
|
console.error('Error: --token is required');
|
|
29
19
|
process.exit(1);
|
|
30
20
|
}
|
|
31
|
-
//
|
|
32
|
-
const cfToken = options.cloudflareToken ?? BUILTIN_CF_TOKEN;
|
|
33
|
-
const tunnelUrl = options.tunnelUrl ?? BUILTIN_TUNNEL_URL;
|
|
34
|
-
const useNamedTunnel = !quickTunnel;
|
|
35
|
-
let tunnelResult = null;
|
|
36
|
-
let terminalServer = null;
|
|
37
|
-
// Cleanup
|
|
21
|
+
// Define cleanup function
|
|
38
22
|
const cleanup = async () => {
|
|
39
23
|
console.log('\n👋 Cleaning up session and shutting down...');
|
|
40
24
|
try {
|
|
25
|
+
// Optional: Notify server to end session
|
|
41
26
|
await fetch(`${server}/api/sessions/${token}/expire`, { method: 'POST' }).catch(() => { });
|
|
42
27
|
}
|
|
43
28
|
finally {
|
|
44
|
-
tunnelResult
|
|
45
|
-
terminalServer
|
|
29
|
+
tunnelResult.cleanup();
|
|
30
|
+
terminalServer.close();
|
|
46
31
|
process.exit(0);
|
|
47
32
|
}
|
|
48
33
|
};
|
|
49
34
|
// 1. Start local terminal server
|
|
50
35
|
console.log('📦 Starting terminal server...');
|
|
51
36
|
const localPort = parseInt(port, 10);
|
|
52
|
-
const { server:
|
|
53
|
-
terminalServer = ts;
|
|
37
|
+
const { server: terminalServer, actualPort } = await startTerminalServer(localPort, path, token, ide);
|
|
54
38
|
console.log(`✅ Terminal server started on port ${actualPort}`);
|
|
55
39
|
// 2. Start Cloudflare Tunnel
|
|
40
|
+
console.log('🌐 Establishing secure tunnel...');
|
|
56
41
|
const tunnel = new CloudflareTunnel();
|
|
57
|
-
|
|
58
|
-
console.log(`🌐 Connecting tunnel → ${tunnelUrl} ...`);
|
|
59
|
-
tunnelResult = await tunnel.startNamed(cfToken, tunnelUrl);
|
|
60
|
-
}
|
|
61
|
-
else {
|
|
62
|
-
console.log('🌐 Establishing ephemeral Cloudflare tunnel...');
|
|
63
|
-
tunnelResult = await tunnel.startQuick(actualPort);
|
|
64
|
-
}
|
|
42
|
+
const tunnelResult = await tunnel.start(actualPort);
|
|
65
43
|
if (!tunnelResult.url) {
|
|
66
44
|
console.error('❌ Failed to start Cloudflare Tunnel.');
|
|
67
45
|
process.exit(1);
|
|
68
46
|
}
|
|
69
|
-
console.log(
|
|
70
|
-
// 3. Validate token
|
|
47
|
+
console.log('✅ Secure tunnel established');
|
|
48
|
+
// 3. Validate token and report public URL to central server
|
|
71
49
|
console.log('🔑 Validating session token...');
|
|
72
50
|
try {
|
|
73
51
|
const res = await fetch(`${server}/api/sessions/${token}/validate`, {
|
|
74
52
|
method: 'POST',
|
|
75
|
-
headers: {
|
|
76
|
-
|
|
53
|
+
headers: {
|
|
54
|
+
'Content-Type': 'application/json'
|
|
55
|
+
},
|
|
56
|
+
body: JSON.stringify({
|
|
57
|
+
public_url: tunnelResult.url
|
|
58
|
+
})
|
|
77
59
|
});
|
|
78
|
-
if (!res.ok)
|
|
60
|
+
if (!res.ok) {
|
|
79
61
|
throw new Error('Invalid token or session expired');
|
|
62
|
+
}
|
|
80
63
|
console.log('✅ Session connected and authorized');
|
|
81
|
-
console.log(`📱 Mobile app can now connect via: ${tunnelResult.url}`);
|
|
82
64
|
}
|
|
83
65
|
catch (err) {
|
|
84
66
|
console.error(`❌ Validation failed: ${err.message}`);
|
|
85
67
|
console.error(' Ensure the central server is available');
|
|
86
|
-
|
|
68
|
+
cleanup();
|
|
87
69
|
}
|
|
70
|
+
// Handle graceful shutdown signals
|
|
88
71
|
process.on('SIGINT', cleanup);
|
|
89
72
|
process.on('SIGTERM', cleanup);
|
|
90
73
|
process.on('SIGHUP', cleanup);
|
package/dist/tunnel.d.ts
CHANGED
|
@@ -4,15 +4,6 @@ export interface TunnelResult {
|
|
|
4
4
|
}
|
|
5
5
|
export declare class CloudflareTunnel {
|
|
6
6
|
private process;
|
|
7
|
-
|
|
8
|
-
* Start a named Cloudflare tunnel using a pre-existing tunnel token.
|
|
9
|
-
* The public URL is known ahead of time (configured in Cloudflare dashboard).
|
|
10
|
-
*/
|
|
11
|
-
startNamed(tunnelToken: string, publicUrl: string): Promise<TunnelResult>;
|
|
12
|
-
/**
|
|
13
|
-
* Start an ephemeral quick tunnel (trycloudflare.com).
|
|
14
|
-
* The public URL is parsed from cloudflared stderr output.
|
|
15
|
-
*/
|
|
16
|
-
startQuick(port: number): Promise<TunnelResult>;
|
|
7
|
+
start(port: number): Promise<TunnelResult>;
|
|
17
8
|
private cleanup;
|
|
18
9
|
}
|
package/dist/tunnel.js
CHANGED
|
@@ -1,59 +1,7 @@
|
|
|
1
1
|
import { spawn } from 'child_process';
|
|
2
2
|
export class CloudflareTunnel {
|
|
3
3
|
process = null;
|
|
4
|
-
|
|
5
|
-
* Start a named Cloudflare tunnel using a pre-existing tunnel token.
|
|
6
|
-
* The public URL is known ahead of time (configured in Cloudflare dashboard).
|
|
7
|
-
*/
|
|
8
|
-
async startNamed(tunnelToken, publicUrl) {
|
|
9
|
-
return new Promise((resolve) => {
|
|
10
|
-
try {
|
|
11
|
-
const proc = spawn('cloudflared', ['tunnel', 'run', '--token', tunnelToken], {
|
|
12
|
-
stdio: ['ignore', 'pipe', 'pipe'],
|
|
13
|
-
});
|
|
14
|
-
this.process = proc;
|
|
15
|
-
// Give cloudflared a moment to connect; fail if it exits immediately
|
|
16
|
-
const timeout = setTimeout(() => {
|
|
17
|
-
// Still resolving with the known public URL — named tunnels don't print the URL
|
|
18
|
-
resolve({ url: publicUrl, cleanup: () => this.cleanup() });
|
|
19
|
-
}, 5000);
|
|
20
|
-
proc.stdout?.on('data', (data) => {
|
|
21
|
-
console.log('[cloudflared]', data.toString().trim());
|
|
22
|
-
});
|
|
23
|
-
proc.stderr?.on('data', (data) => {
|
|
24
|
-
const line = data.toString().trim();
|
|
25
|
-
if (line)
|
|
26
|
-
console.log('[cloudflared]', line);
|
|
27
|
-
// If we see a connection established log, resolve immediately
|
|
28
|
-
if (line.includes('Connection') || line.includes('registered')) {
|
|
29
|
-
clearTimeout(timeout);
|
|
30
|
-
resolve({ url: publicUrl, cleanup: () => this.cleanup() });
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
proc.on('error', (err) => {
|
|
34
|
-
clearTimeout(timeout);
|
|
35
|
-
console.error('[cloudflared] Failed to start:', err.message);
|
|
36
|
-
resolve({ url: null, cleanup: () => this.cleanup() });
|
|
37
|
-
});
|
|
38
|
-
proc.on('exit', (code) => {
|
|
39
|
-
clearTimeout(timeout);
|
|
40
|
-
if (code !== 0) {
|
|
41
|
-
console.error(`[cloudflared] Exited with code ${code}`);
|
|
42
|
-
}
|
|
43
|
-
this.process = null;
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
catch (err) {
|
|
47
|
-
console.error('[cloudflared] Exception:', err.message);
|
|
48
|
-
resolve({ url: null, cleanup: () => this.cleanup() });
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Start an ephemeral quick tunnel (trycloudflare.com).
|
|
54
|
-
* The public URL is parsed from cloudflared stderr output.
|
|
55
|
-
*/
|
|
56
|
-
async startQuick(port) {
|
|
4
|
+
async start(port) {
|
|
57
5
|
return new Promise((resolve) => {
|
|
58
6
|
try {
|
|
59
7
|
const proc = spawn('cloudflared', ['tunnel', '--url', `http://localhost:${port}`], {
|