This commit is contained in:
2026-03-12 15:41:11 +01:00
parent b4aa34a109
commit 25f966250c
18 changed files with 30 additions and 23 deletions
+1 -1
View File
@@ -46,7 +46,7 @@ export default async function grabTtydServerInfo({
);
}
const available_port = getNextAvailablePort();
const available_port = await getNextAvailablePort();
let url = `/ttyd/${available_port}`;
+16 -9
View File
@@ -25,19 +25,26 @@ function makeSockaddr(port: number): Buffer {
return buf;
}
export default function getNextAvailablePort(
export default async function getNextAvailablePort(
startPort = AppData["DynamicPortStart"],
maxPort = 65535,
): number {
for (let port = startPort; port <= maxPort; port++) {
const fd = libc.symbols.socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) continue;
): Promise<number> {
const MAX_RETRIES = 5;
let retries = 0;
const addr = makeSockaddr(port);
const result = libc.symbols.bind(fd, ptr(addr), addr.byteLength);
libc.symbols.close(fd);
while (retries < MAX_RETRIES) {
for (let port = startPort; port <= maxPort; port++) {
const fd = libc.symbols.socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) continue;
if (result === 0) return port;
const addr = makeSockaddr(port);
const result = libc.symbols.bind(fd, ptr(addr), addr.byteLength);
libc.symbols.close(fd);
if (result === 0) return port;
await Bun.sleep(2000);
}
}
throw new Error(`No available port found in range ${startPort}-${maxPort}`);