traforo 0.2.1 → 0.2.3
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/LICENSE +21 -0
- package/dist/lockfile.d.ts +33 -0
- package/dist/lockfile.js +66 -0
- package/dist/run-tunnel.js +73 -1
- package/package.json +13 -14
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
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.
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Port lockfile management for traforo tunnels.
|
|
3
|
+
*
|
|
4
|
+
* Stores one JSON file per active tunnel port in ~/.traforo/{port}.json.
|
|
5
|
+
* Used to detect port conflicts, show tunnel info in error messages,
|
|
6
|
+
* and let agents reuse existing tunnels instead of killing them.
|
|
7
|
+
*/
|
|
8
|
+
export type LockfileData = {
|
|
9
|
+
tunnelId: string;
|
|
10
|
+
tunnelUrl: string;
|
|
11
|
+
port: number;
|
|
12
|
+
/** PID of the traforo tunnel process (used for liveness checks) */
|
|
13
|
+
tunnelPid: number;
|
|
14
|
+
/** PID of the child server process, if any */
|
|
15
|
+
serverPid?: number;
|
|
16
|
+
command: string[] | undefined;
|
|
17
|
+
cwd: string;
|
|
18
|
+
startedAt: string;
|
|
19
|
+
};
|
|
20
|
+
export declare function writeLockfile(port: number, data: LockfileData): void;
|
|
21
|
+
export declare function readLockfile(port: number): LockfileData | null;
|
|
22
|
+
/**
|
|
23
|
+
* Remove lockfile only if it belongs to this traforo instance.
|
|
24
|
+
* Prevents a late-exiting old process from deleting a newer instance's lockfile.
|
|
25
|
+
*/
|
|
26
|
+
export declare function removeLockfile(port: number, expectedTunnelPid?: number): void;
|
|
27
|
+
/**
|
|
28
|
+
* Check if the tunnel process in a lockfile is still alive.
|
|
29
|
+
* Uses tunnelPid (the traforo process) not serverPid, because
|
|
30
|
+
* the tunnel URL is only valid while the traforo process is running.
|
|
31
|
+
* Returns true (stale) if the tunnel process no longer exists.
|
|
32
|
+
*/
|
|
33
|
+
export declare function isLockfileStale(lock: LockfileData): boolean;
|
package/dist/lockfile.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Port lockfile management for traforo tunnels.
|
|
3
|
+
*
|
|
4
|
+
* Stores one JSON file per active tunnel port in ~/.traforo/{port}.json.
|
|
5
|
+
* Used to detect port conflicts, show tunnel info in error messages,
|
|
6
|
+
* and let agents reuse existing tunnels instead of killing them.
|
|
7
|
+
*/
|
|
8
|
+
import fs from 'node:fs';
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import os from 'node:os';
|
|
11
|
+
const LOCKFILE_DIR = path.join(os.homedir(), '.traforo');
|
|
12
|
+
function lockfilePath(port) {
|
|
13
|
+
return path.join(LOCKFILE_DIR, `${port}.json`);
|
|
14
|
+
}
|
|
15
|
+
export function writeLockfile(port, data) {
|
|
16
|
+
try {
|
|
17
|
+
fs.mkdirSync(LOCKFILE_DIR, { recursive: true });
|
|
18
|
+
fs.writeFileSync(lockfilePath(port), JSON.stringify(data, null, 2) + '\n');
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
// Non-critical — don't crash if we can't write the lockfile
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
export function readLockfile(port) {
|
|
25
|
+
try {
|
|
26
|
+
const raw = fs.readFileSync(lockfilePath(port), 'utf-8');
|
|
27
|
+
return JSON.parse(raw);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Remove lockfile only if it belongs to this traforo instance.
|
|
35
|
+
* Prevents a late-exiting old process from deleting a newer instance's lockfile.
|
|
36
|
+
*/
|
|
37
|
+
export function removeLockfile(port, expectedTunnelPid) {
|
|
38
|
+
try {
|
|
39
|
+
if (expectedTunnelPid != null) {
|
|
40
|
+
const lock = readLockfile(port);
|
|
41
|
+
if (lock && lock.tunnelPid !== expectedTunnelPid) {
|
|
42
|
+
return; // not ours — leave it alone
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
fs.unlinkSync(lockfilePath(port));
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
// Already gone or never existed
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Check if the tunnel process in a lockfile is still alive.
|
|
53
|
+
* Uses tunnelPid (the traforo process) not serverPid, because
|
|
54
|
+
* the tunnel URL is only valid while the traforo process is running.
|
|
55
|
+
* Returns true (stale) if the tunnel process no longer exists.
|
|
56
|
+
*/
|
|
57
|
+
export function isLockfileStale(lock) {
|
|
58
|
+
try {
|
|
59
|
+
// signal 0 doesn't kill — just checks if process exists
|
|
60
|
+
process.kill(lock.tunnelPid, 0);
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
}
|
package/dist/run-tunnel.js
CHANGED
|
@@ -3,9 +3,10 @@ import { exec, spawn } from 'node:child_process';
|
|
|
3
3
|
import net from 'node:net';
|
|
4
4
|
import { promisify } from 'node:util';
|
|
5
5
|
import { TunnelClient } from './client.js';
|
|
6
|
+
import { writeLockfile, readLockfile, removeLockfile, isLockfileStale, } from './lockfile.js';
|
|
6
7
|
const execPromise = promisify(exec);
|
|
7
8
|
export const CLI_NAME = 'traforo';
|
|
8
|
-
const DEFAULT_TUNNEL_ID_BYTES =
|
|
9
|
+
const DEFAULT_TUNNEL_ID_BYTES = 10;
|
|
9
10
|
export function createRandomTunnelId({ port }) {
|
|
10
11
|
return `${crypto.randomBytes(DEFAULT_TUNNEL_ID_BYTES).toString('hex')}-${port}`;
|
|
11
12
|
}
|
|
@@ -167,6 +168,64 @@ export async function runTunnel(options) {
|
|
|
167
168
|
// Kill existing process on port if requested
|
|
168
169
|
if (options.kill) {
|
|
169
170
|
await killProcessOnPort(port);
|
|
171
|
+
removeLockfile(port); // no ownership check — --kill is intentional
|
|
172
|
+
// Verify the port actually freed up
|
|
173
|
+
if (await isPortInUse(port, localHost)) {
|
|
174
|
+
console.error(`Error: Port ${port} is still in use after --kill.`);
|
|
175
|
+
console.error(`The process may require elevated permissions to terminate.`);
|
|
176
|
+
process.exit(1);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
// Pre-flight: detect port conflict before spawning the child process
|
|
180
|
+
if (options.command && options.command.length > 0 && !options.kill) {
|
|
181
|
+
const portBusy = await isPortInUse(port, localHost);
|
|
182
|
+
if (portBusy) {
|
|
183
|
+
const lock = readLockfile(port);
|
|
184
|
+
if (lock && !isLockfileStale(lock)) {
|
|
185
|
+
const currentCwd = process.cwd();
|
|
186
|
+
const currentCmd = options.command;
|
|
187
|
+
const sameCwd = lock.cwd === currentCwd;
|
|
188
|
+
const sameCmd = lock.command &&
|
|
189
|
+
lock.command.length === currentCmd.length &&
|
|
190
|
+
lock.command.every((arg, i) => arg === currentCmd[i]);
|
|
191
|
+
if (sameCwd && sameCmd) {
|
|
192
|
+
// Same command in same directory — tell agent to reuse the tunnel
|
|
193
|
+
console.error(`Error: Port ${port} is already in use\n`);
|
|
194
|
+
console.error(` Tunnel: ${lock.tunnelUrl}`);
|
|
195
|
+
console.error(` ID: ${lock.tunnelId}`);
|
|
196
|
+
console.error(` Command: ${lock.command?.join(' ') ?? 'unknown'}`);
|
|
197
|
+
console.error(` Dir: ${lock.cwd}`);
|
|
198
|
+
console.error(` PID: ${lock.tunnelPid}`);
|
|
199
|
+
console.error(` Started: ${lock.startedAt}\n`);
|
|
200
|
+
console.error(`The same command in the same directory is already tunneled.`);
|
|
201
|
+
console.error(`Reuse the tunnel URL above instead of creating a new one.`);
|
|
202
|
+
process.exit(1);
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
// Different command or directory — suggest --kill or reuse
|
|
206
|
+
console.error(`Error: Port ${port} is already in use\n`);
|
|
207
|
+
console.error(` Tunnel: ${lock.tunnelUrl}`);
|
|
208
|
+
console.error(` ID: ${lock.tunnelId}`);
|
|
209
|
+
console.error(` Command: ${lock.command?.join(' ') ?? 'unknown'}`);
|
|
210
|
+
console.error(` Dir: ${lock.cwd}`);
|
|
211
|
+
console.error(` PID: ${lock.tunnelPid}`);
|
|
212
|
+
console.error(` Started: ${lock.startedAt}\n`);
|
|
213
|
+
console.error(`Use --kill to terminate the existing process and start fresh,`);
|
|
214
|
+
console.error(`or just reuse the tunnel URL above instead:\n`);
|
|
215
|
+
console.error(` traforo -p ${port} --kill -- ${options.command.join(' ')}`);
|
|
216
|
+
process.exit(1);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
// Port busy but no lockfile or stale lockfile — unknown process
|
|
221
|
+
if (lock)
|
|
222
|
+
removeLockfile(port);
|
|
223
|
+
console.error(`Error: Port ${port} is already in use by another process.\n`);
|
|
224
|
+
console.error(`Use --kill to terminate it before starting:`);
|
|
225
|
+
console.error(` traforo -p ${port} --kill -- ${options.command.join(' ')}`);
|
|
226
|
+
process.exit(1);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
170
229
|
}
|
|
171
230
|
let child = null;
|
|
172
231
|
// If command provided, spawn child process with PORT env
|
|
@@ -193,6 +252,7 @@ export async function runTunnel(options) {
|
|
|
193
252
|
});
|
|
194
253
|
spawnedChild.on('exit', (code) => {
|
|
195
254
|
console.log(`\nCommand exited with code ${code}`);
|
|
255
|
+
removeLockfile(port, process.pid);
|
|
196
256
|
process.exit(code || 0);
|
|
197
257
|
});
|
|
198
258
|
// Wait for port to be available before connecting tunnel
|
|
@@ -225,6 +285,7 @@ export async function runTunnel(options) {
|
|
|
225
285
|
// Handle shutdown
|
|
226
286
|
const cleanup = () => {
|
|
227
287
|
console.log('\nShutting down...');
|
|
288
|
+
removeLockfile(port, process.pid);
|
|
228
289
|
client.close();
|
|
229
290
|
if (child) {
|
|
230
291
|
child.kill();
|
|
@@ -235,6 +296,17 @@ export async function runTunnel(options) {
|
|
|
235
296
|
process.on('SIGTERM', cleanup);
|
|
236
297
|
try {
|
|
237
298
|
await client.connect();
|
|
299
|
+
// Write lockfile so other traforo instances can detect this tunnel
|
|
300
|
+
writeLockfile(port, {
|
|
301
|
+
tunnelId,
|
|
302
|
+
tunnelUrl: client.url,
|
|
303
|
+
port,
|
|
304
|
+
tunnelPid: process.pid,
|
|
305
|
+
serverPid: child?.pid,
|
|
306
|
+
command: options.command,
|
|
307
|
+
cwd: process.cwd(),
|
|
308
|
+
startedAt: new Date().toISOString(),
|
|
309
|
+
});
|
|
238
310
|
}
|
|
239
311
|
catch (err) {
|
|
240
312
|
console.error('Failed to connect:', err instanceof Error ? err.message : String(err));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "traforo",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
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,18 +28,6 @@
|
|
|
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
|
-
},
|
|
43
31
|
"devDependencies": {
|
|
44
32
|
"@cloudflare/workers-types": "^4.20250712.0",
|
|
45
33
|
"@types/http-cache-semantics": "^4.2.0",
|
|
@@ -57,5 +45,16 @@
|
|
|
57
45
|
"http-cache-semantics": "^4.2.0",
|
|
58
46
|
"string-dedent": "^3.0.2",
|
|
59
47
|
"ws": "^8.19.0"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"build": "tsc -p tsconfig.client.json && chmod +x dist/cli.js",
|
|
51
|
+
"dev": "wrangler dev",
|
|
52
|
+
"deploy": "wrangler deploy",
|
|
53
|
+
"deploy:preview": "wrangler deploy --env preview",
|
|
54
|
+
"typecheck": "tsc --noEmit",
|
|
55
|
+
"typecheck:client": "tsc --noEmit -p tsconfig.client.json",
|
|
56
|
+
"typecheck:test": "tsc --noEmit -p tsconfig.test.json",
|
|
57
|
+
"cli": "tsx src/cli.ts",
|
|
58
|
+
"test": "vitest --run"
|
|
60
59
|
}
|
|
61
|
-
}
|
|
60
|
+
}
|