claude-relay 1.2.8 → 1.2.9

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 (2) hide show
  1. package/bin/cli.js +39 -12
  2. package/package.json +1 -1
package/bin/cli.js CHANGED
@@ -305,8 +305,32 @@ function promptToggle(title, desc, defaultValue, callback) {
305
305
  });
306
306
  }
307
307
 
308
+ // --- Port availability check ---
309
+ var net = require("net");
310
+
311
+ function isPortFree(p) {
312
+ return new Promise(function (resolve) {
313
+ var srv = net.createServer();
314
+ srv.once("error", function () { resolve(false); });
315
+ srv.once("listening", function () { srv.close(function () { resolve(true); }); });
316
+ srv.listen(p);
317
+ });
318
+ }
319
+
320
+ async function findAvailablePort(startPort) {
321
+ var p = startPort;
322
+ var maxAttempts = 20;
323
+ for (var i = 0; i < maxAttempts; i++) {
324
+ var httpFree = await isPortFree(p);
325
+ var httpsFree = await isPortFree(p + 1);
326
+ if (httpFree && httpsFree) return p;
327
+ p += 2;
328
+ }
329
+ return null;
330
+ }
331
+
308
332
  // --- Server start ---
309
- function start(pin) {
333
+ async function start(pin) {
310
334
  var ip = getLocalIP();
311
335
  var tlsOptions = null;
312
336
  var caRoot = null;
@@ -326,28 +350,31 @@ function start(pin) {
326
350
  }
327
351
  }
328
352
 
353
+ var actualPort = await findAvailablePort(port);
354
+ if (actualPort === null) {
355
+ log(a.red + "No available port found (tried " + port + " to " + (port + 38) + ")." + a.reset);
356
+ process.exit(1);
357
+ return;
358
+ }
359
+ if (actualPort !== port) {
360
+ log(sym.warn + " " + a.yellow + "Port " + port + " in use" + a.reset + a.dim + " · using " + actualPort + a.reset);
361
+ log(sym.bar);
362
+ }
363
+ port = actualPort;
364
+
329
365
  var result = createServer(cwd, tlsOptions, caRoot, pin, port);
330
366
  var entryServer = result.entryServer;
331
367
  var httpsServer = result.httpsServer;
332
368
 
333
369
  entryServer.on("error", function (err) {
334
- if (err.code === "EADDRINUSE") {
335
- log(a.red + "Port " + port + " is already in use." + a.reset);
336
- log(a.dim + "Run: claude-relay -p <port>" + a.reset);
337
- } else {
338
- log(a.red + "Server error: " + err.message + a.reset);
339
- }
370
+ log(a.red + "Server error: " + err.message + a.reset);
340
371
  process.exit(1);
341
372
  });
342
373
 
343
374
  var httpsPort = port + 1;
344
375
  if (httpsServer) {
345
376
  httpsServer.on("error", function (err) {
346
- if (err.code === "EADDRINUSE") {
347
- log(a.red + "HTTPS port " + httpsPort + " is already in use." + a.reset);
348
- } else {
349
- log(a.red + "HTTPS error: " + err.message + a.reset);
350
- }
377
+ log(a.red + "HTTPS error: " + err.message + a.reset);
351
378
  process.exit(1);
352
379
  });
353
380
  httpsServer.listen(httpsPort);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-relay",
3
- "version": "1.2.8",
3
+ "version": "1.2.9",
4
4
  "description": "Access Claude Code on your machine, from anywhere. One command, no config.",
5
5
  "bin": {
6
6
  "claude-relay": "./bin/cli.js"