| 54 | }; |
| 55 | |
| 56 | function getLocalIP() { |
| 57 | const interfaces = os.networkInterfaces(); |
| 58 | for (const iface of Object.values(interfaces)) { |
| 59 | if (!iface) continue; |
| 60 | for (const addr of iface) { |
| 61 | if (addr.family === "IPv4" && !addr.internal) { |
| 62 | // Prefer 192.168.x.x or 10.x.x.x over other private ranges |
| 63 | if ( |
| 64 | addr.address.startsWith("192.168.") || |
| 65 | addr.address.startsWith("10.") |
| 66 | ) { |
| 67 | return addr.address; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | // Fallback: any non-internal IPv4 |
| 73 | for (const iface of Object.values(interfaces)) { |
| 74 | if (!iface) continue; |
| 75 | for (const addr of iface) { |
| 76 | if (addr.family === "IPv4" && !addr.internal) { |
| 77 | return addr.address; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | return "127.0.0.1"; |
| 82 | } |
| 83 | |
| 84 | function getFreePort() { |
| 85 | return new Promise((resolve, reject) => { |