traforo 0.1.0 → 0.2.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/cli.js +2 -0
- package/dist/client.d.ts +3 -0
- package/dist/client.js +33 -1
- package/dist/run-tunnel.d.ts +5 -0
- package/dist/run-tunnel.js +120 -4
- package/package.json +15 -13
- package/LICENSE +0 -21
package/dist/cli.js
CHANGED
|
@@ -11,6 +11,7 @@ cli
|
|
|
11
11
|
.option('-s, --server [url]', 'Tunnel server URL')
|
|
12
12
|
.option('-c, --cache [key]', 'Enable edge caching for static assets (optional key for cache partitioning, default: "default")')
|
|
13
13
|
.option('--password <password>', 'Protect the tunnel with a password (visitors must enter it to access)')
|
|
14
|
+
.option('-k, --kill', 'Kill any existing process on the port before starting')
|
|
14
15
|
.example(`${CLI_NAME} -p 3000`)
|
|
15
16
|
.example(`${CLI_NAME} -p 3000 -- next start`)
|
|
16
17
|
.example(`${CLI_NAME} -p 3000 -- pnpm dev`)
|
|
@@ -42,6 +43,7 @@ cli
|
|
|
42
43
|
command: command.length > 0 ? command : undefined,
|
|
43
44
|
cacheKey,
|
|
44
45
|
password: options.password,
|
|
46
|
+
kill: options.kill,
|
|
45
47
|
});
|
|
46
48
|
});
|
|
47
49
|
cli.help();
|
package/dist/client.d.ts
CHANGED
|
@@ -28,10 +28,13 @@ export declare class TunnelClient {
|
|
|
28
28
|
private ws;
|
|
29
29
|
private localWsConnections;
|
|
30
30
|
private closed;
|
|
31
|
+
private pingInterval;
|
|
31
32
|
constructor(options: TunnelClientOptions);
|
|
32
33
|
get url(): string;
|
|
33
34
|
connect(): Promise<void>;
|
|
34
35
|
close(): void;
|
|
36
|
+
private startPingInterval;
|
|
37
|
+
private stopPingInterval;
|
|
35
38
|
private handleMessage;
|
|
36
39
|
private handleHttpRequest;
|
|
37
40
|
private handleWsOpen;
|
package/dist/client.js
CHANGED
|
@@ -2,11 +2,19 @@
|
|
|
2
2
|
* Local tunnel client - runs on user's machine to expose a local server.
|
|
3
3
|
*/
|
|
4
4
|
import WebSocket from 'ws';
|
|
5
|
+
/**
|
|
6
|
+
* Interval (ms) between keepalive pings sent to the DO.
|
|
7
|
+
* Cloudflare's CDN has a ~100s inactivity timeout on WebSockets.
|
|
8
|
+
* The DO's setWebSocketAutoResponse handles these at the edge without
|
|
9
|
+
* waking the DO from hibernation, so this is zero-cost.
|
|
10
|
+
*/
|
|
11
|
+
const PING_INTERVAL_MS = 30_000;
|
|
5
12
|
export class TunnelClient {
|
|
6
13
|
options;
|
|
7
14
|
ws = null;
|
|
8
15
|
localWsConnections = new Map();
|
|
9
16
|
closed = false;
|
|
17
|
+
pingInterval = null;
|
|
10
18
|
constructor(options) {
|
|
11
19
|
const baseDomain = options.baseDomain || 'traforo.dev';
|
|
12
20
|
this.options = {
|
|
@@ -38,7 +46,8 @@ export class TunnelClient {
|
|
|
38
46
|
return new Promise((resolve, reject) => {
|
|
39
47
|
this.ws = new WebSocket(wsUrl);
|
|
40
48
|
this.ws.on('open', () => {
|
|
41
|
-
console.log(`Connected with Traforo
|
|
49
|
+
console.log(`Connected with Traforo!\n${this.url}`);
|
|
50
|
+
this.startPingInterval();
|
|
42
51
|
resolve();
|
|
43
52
|
});
|
|
44
53
|
this.ws.on('error', (err) => {
|
|
@@ -47,6 +56,7 @@ export class TunnelClient {
|
|
|
47
56
|
});
|
|
48
57
|
this.ws.on('close', (code, reason) => {
|
|
49
58
|
console.log(`Disconnected: ${code} ${reason.toString()}`);
|
|
59
|
+
this.stopPingInterval();
|
|
50
60
|
this.ws = null;
|
|
51
61
|
// Close all local WS connections
|
|
52
62
|
for (const [, localWs] of this.localWsConnections) {
|
|
@@ -71,6 +81,7 @@ export class TunnelClient {
|
|
|
71
81
|
}
|
|
72
82
|
close() {
|
|
73
83
|
this.closed = true;
|
|
84
|
+
this.stopPingInterval();
|
|
74
85
|
if (this.ws) {
|
|
75
86
|
this.ws.close();
|
|
76
87
|
this.ws = null;
|
|
@@ -83,6 +94,27 @@ export class TunnelClient {
|
|
|
83
94
|
}
|
|
84
95
|
this.localWsConnections.clear();
|
|
85
96
|
}
|
|
97
|
+
startPingInterval() {
|
|
98
|
+
this.stopPingInterval();
|
|
99
|
+
this.pingInterval = setInterval(() => {
|
|
100
|
+
const ws = this.ws;
|
|
101
|
+
if (!ws || ws.readyState !== WebSocket.OPEN) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
try {
|
|
105
|
+
ws.send('{"type":"ping"}');
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
// readyState can flip between check and send(); safe to ignore
|
|
109
|
+
}
|
|
110
|
+
}, PING_INTERVAL_MS);
|
|
111
|
+
}
|
|
112
|
+
stopPingInterval() {
|
|
113
|
+
if (this.pingInterval) {
|
|
114
|
+
clearInterval(this.pingInterval);
|
|
115
|
+
this.pingInterval = null;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
86
118
|
handleMessage(rawMessage) {
|
|
87
119
|
let msg;
|
|
88
120
|
try {
|
package/dist/run-tunnel.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
export declare const CLI_NAME = "traforo";
|
|
2
|
+
export declare function createRandomTunnelId({ port }: {
|
|
3
|
+
port: number;
|
|
4
|
+
}): string;
|
|
2
5
|
export type RunTunnelOptions = {
|
|
3
6
|
port: number;
|
|
4
7
|
tunnelId?: string;
|
|
@@ -10,6 +13,8 @@ export type RunTunnelOptions = {
|
|
|
10
13
|
cacheKey?: string;
|
|
11
14
|
/** Password to protect the tunnel */
|
|
12
15
|
password?: string;
|
|
16
|
+
/** Kill any existing process on the port before starting */
|
|
17
|
+
kill?: boolean;
|
|
13
18
|
};
|
|
14
19
|
/**
|
|
15
20
|
* Parse argv to extract command after `--` separator.
|
package/dist/run-tunnel.js
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
|
-
import
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import { exec, spawn } from 'node:child_process';
|
|
2
3
|
import net from 'node:net';
|
|
4
|
+
import { promisify } from 'node:util';
|
|
3
5
|
import { TunnelClient } from './client.js';
|
|
6
|
+
const execPromise = promisify(exec);
|
|
4
7
|
export const CLI_NAME = 'traforo';
|
|
5
|
-
const
|
|
8
|
+
const DEFAULT_TUNNEL_ID_BYTES = 16;
|
|
9
|
+
export function createRandomTunnelId({ port }) {
|
|
10
|
+
return `${crypto.randomBytes(DEFAULT_TUNNEL_ID_BYTES).toString('hex')}-${port}`;
|
|
11
|
+
}
|
|
6
12
|
/**
|
|
7
13
|
* Wait for a port to be available (accepting connections).
|
|
8
14
|
* Used when spawning a child process to wait for the server to start.
|
|
@@ -30,6 +36,113 @@ async function waitForPort(port, host = 'localhost', timeoutMs = 60_000) {
|
|
|
30
36
|
check();
|
|
31
37
|
});
|
|
32
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Check if a port is currently in use (something is listening).
|
|
41
|
+
*/
|
|
42
|
+
async function isPortInUse(port, host = 'localhost') {
|
|
43
|
+
return new Promise((resolve) => {
|
|
44
|
+
const socket = new net.Socket();
|
|
45
|
+
socket.once('connect', () => {
|
|
46
|
+
socket.destroy();
|
|
47
|
+
resolve(true);
|
|
48
|
+
});
|
|
49
|
+
socket.once('error', () => {
|
|
50
|
+
socket.destroy();
|
|
51
|
+
resolve(false);
|
|
52
|
+
});
|
|
53
|
+
socket.connect(port, host);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Kill any process listening on the given port.
|
|
58
|
+
* Cross-platform: uses lsof on macOS/Linux, netstat+taskkill on Windows.
|
|
59
|
+
* Never throws — silently succeeds if no process is found or kill fails.
|
|
60
|
+
*/
|
|
61
|
+
async function killProcessOnPort(port) {
|
|
62
|
+
try {
|
|
63
|
+
const inUse = await isPortInUse(port);
|
|
64
|
+
if (!inUse) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
console.log(`Killing process on port ${port}...`);
|
|
68
|
+
if (process.platform === 'win32') {
|
|
69
|
+
// Windows: parse netstat output to find PIDs listening on the port
|
|
70
|
+
const { stdout } = await execPromise(`netstat -ano | findstr :${port} | findstr LISTENING`);
|
|
71
|
+
const pids = new Set();
|
|
72
|
+
for (const line of stdout.trim().split('\n')) {
|
|
73
|
+
const parts = line.trim().split(/\s+/);
|
|
74
|
+
const pid = parseInt(parts[parts.length - 1], 10);
|
|
75
|
+
if (!isNaN(pid) && pid > 0) {
|
|
76
|
+
pids.add(pid);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
for (const pid of pids) {
|
|
80
|
+
try {
|
|
81
|
+
await execPromise(`taskkill /PID ${pid} /F`);
|
|
82
|
+
}
|
|
83
|
+
catch { }
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
// macOS / Linux: lsof returns PIDs listening on tcp port
|
|
88
|
+
const { stdout } = await execPromise(`lsof -ti tcp:${port}`);
|
|
89
|
+
const pids = stdout
|
|
90
|
+
.trim()
|
|
91
|
+
.split('\n')
|
|
92
|
+
.map((s) => {
|
|
93
|
+
return parseInt(s, 10);
|
|
94
|
+
})
|
|
95
|
+
.filter((n) => {
|
|
96
|
+
return !isNaN(n) && n > 0;
|
|
97
|
+
});
|
|
98
|
+
for (const pid of pids) {
|
|
99
|
+
try {
|
|
100
|
+
process.kill(pid, 'SIGTERM');
|
|
101
|
+
}
|
|
102
|
+
catch { }
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
// Brief wait for the port to actually free up
|
|
106
|
+
const maxWait = 3_000;
|
|
107
|
+
const start = Date.now();
|
|
108
|
+
while (Date.now() - start < maxWait) {
|
|
109
|
+
const stillInUse = await isPortInUse(port);
|
|
110
|
+
if (!stillInUse) {
|
|
111
|
+
console.log(`Port ${port} is now free`);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
await new Promise((r) => {
|
|
115
|
+
return setTimeout(r, 200);
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
// If SIGTERM didn't work on POSIX, try SIGKILL as last resort
|
|
119
|
+
if (process.platform !== 'win32') {
|
|
120
|
+
try {
|
|
121
|
+
const { stdout } = await execPromise(`lsof -ti tcp:${port}`);
|
|
122
|
+
const pids = stdout
|
|
123
|
+
.trim()
|
|
124
|
+
.split('\n')
|
|
125
|
+
.map((s) => {
|
|
126
|
+
return parseInt(s, 10);
|
|
127
|
+
})
|
|
128
|
+
.filter((n) => {
|
|
129
|
+
return !isNaN(n) && n > 0;
|
|
130
|
+
});
|
|
131
|
+
for (const pid of pids) {
|
|
132
|
+
try {
|
|
133
|
+
process.kill(pid, 'SIGKILL');
|
|
134
|
+
}
|
|
135
|
+
catch { }
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
catch { }
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
catch {
|
|
142
|
+
// Never crash — if anything fails, just continue and let the
|
|
143
|
+
// child process or port-wait logic surface the actual error
|
|
144
|
+
}
|
|
145
|
+
}
|
|
33
146
|
/**
|
|
34
147
|
* Parse argv to extract command after `--` separator.
|
|
35
148
|
* Returns the command array and remaining argv without the command.
|
|
@@ -48,10 +161,13 @@ export function parseCommandFromArgv(argv) {
|
|
|
48
161
|
* Run the tunnel, optionally spawning a child process first.
|
|
49
162
|
*/
|
|
50
163
|
export async function runTunnel(options) {
|
|
51
|
-
const tunnelId = options.tunnelId ||
|
|
52
|
-
crypto.randomUUID().replaceAll('-', '').slice(0, DEFAULT_TUNNEL_ID_LENGTH);
|
|
53
164
|
const localHost = options.localHost || 'localhost';
|
|
54
165
|
const port = options.port;
|
|
166
|
+
const tunnelId = options.tunnelId || createRandomTunnelId({ port });
|
|
167
|
+
// Kill existing process on port if requested
|
|
168
|
+
if (options.kill) {
|
|
169
|
+
await killProcessOnPort(port);
|
|
170
|
+
}
|
|
55
171
|
let child = null;
|
|
56
172
|
// If command provided, spawn child process with PORT env
|
|
57
173
|
if (options.command && options.command.length > 0) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "traforo",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "HTTP tunnel via Cloudflare Durable Objects and WebSockets. Edge caching and password protection.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -28,6 +28,18 @@
|
|
|
28
28
|
"import": "./dist/run-tunnel.js"
|
|
29
29
|
}
|
|
30
30
|
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsc -p tsconfig.client.json && chmod +x dist/cli.js",
|
|
33
|
+
"prepublishOnly": "pnpm build",
|
|
34
|
+
"dev": "wrangler dev",
|
|
35
|
+
"deploy": "wrangler deploy",
|
|
36
|
+
"deploy:preview": "wrangler deploy --env preview",
|
|
37
|
+
"typecheck": "tsc --noEmit",
|
|
38
|
+
"typecheck:client": "tsc --noEmit -p tsconfig.client.json",
|
|
39
|
+
"typecheck:test": "tsc --noEmit -p tsconfig.test.json",
|
|
40
|
+
"cli": "tsx src/cli.ts",
|
|
41
|
+
"test": "vitest --run"
|
|
42
|
+
},
|
|
31
43
|
"devDependencies": {
|
|
32
44
|
"@cloudflare/workers-types": "^4.20250712.0",
|
|
33
45
|
"@types/http-cache-semantics": "^4.2.0",
|
|
@@ -35,6 +47,7 @@
|
|
|
35
47
|
"@types/ws": "^8.18.1",
|
|
36
48
|
"tsx": "^4.20.5",
|
|
37
49
|
"typescript": "^5.7.0",
|
|
50
|
+
"undici-types": "~6.21.0",
|
|
38
51
|
"vite": "^7.1.4",
|
|
39
52
|
"vitest": "^3.2.4",
|
|
40
53
|
"wrangler": "^4.24.3"
|
|
@@ -44,16 +57,5 @@
|
|
|
44
57
|
"http-cache-semantics": "^4.2.0",
|
|
45
58
|
"string-dedent": "^3.0.2",
|
|
46
59
|
"ws": "^8.19.0"
|
|
47
|
-
},
|
|
48
|
-
"scripts": {
|
|
49
|
-
"build": "tsc -p tsconfig.client.json && chmod +x dist/cli.js",
|
|
50
|
-
"dev": "wrangler dev",
|
|
51
|
-
"deploy": "wrangler deploy",
|
|
52
|
-
"deploy:preview": "wrangler deploy --env preview",
|
|
53
|
-
"typecheck": "tsc --noEmit",
|
|
54
|
-
"typecheck:client": "tsc --noEmit -p tsconfig.client.json",
|
|
55
|
-
"typecheck:test": "tsc --noEmit -p tsconfig.test.json",
|
|
56
|
-
"cli": "tsx src/cli.ts",
|
|
57
|
-
"test": "vitest --run"
|
|
58
60
|
}
|
|
59
|
-
}
|
|
61
|
+
}
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2025 Kimaki
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|