Skip to content

Commit

Permalink
Update TunnelService.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
DFanso committed Dec 8, 2024
1 parent 1c04de4 commit a6494d4
Showing 1 changed file with 70 additions and 23 deletions.
93 changes: 70 additions & 23 deletions src/services/TunnelService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,29 +141,76 @@ export class TunnelService extends EventEmitter {
}

private async validateTargetService(target: string, port: number): Promise<boolean> {
return new Promise((resolve) => {
const socket = new net.Socket();

socket.setTimeout(2000); // 2 second timeout

socket.on('connect', () => {
socket.destroy();
resolve(true);
});

socket.on('timeout', () => {
socket.destroy();
resolve(false);
});

socket.on('error', () => {
socket.destroy();
resolve(false);
});

const [host] = target.split(':');
socket.connect(port, host.replace(/^https?:\/\//, ''));
});
const maxRetries = 3;
const retryDelay = 1000; // 1 second

for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
logger.info(`Validating target service (attempt ${attempt}/${maxRetries})`, {
target,
port
});

const isAvailable = await new Promise<boolean>((resolve) => {
const socket = new net.Socket();

socket.setTimeout(2000); // 2 second timeout

socket.on('connect', () => {
logger.info(`Successfully connected to target service`, {
target,
port,
attempt
});
socket.destroy();
resolve(true);
});

socket.on('timeout', () => {
logger.warn(`Connection attempt timed out`, {
target,
port,
attempt
});
socket.destroy();
resolve(false);
});

socket.on('error', (err) => {
logger.warn(`Connection attempt failed`, {
target,
port,
attempt,
error: err.message
});
socket.destroy();
resolve(false);
});

const host = target.replace(/^https?:\/\//, '').split(':')[0];
logger.info(`Attempting to connect to ${host}:${port}`);
socket.connect(port, host);
});

if (isAvailable) {
return true;
}

if (attempt < maxRetries) {
logger.info(`Retrying connection after ${retryDelay}ms...`);
await new Promise(resolve => setTimeout(resolve, retryDelay));
}
} catch (error) {
logger.error(`Error during service validation`, {
target,
port,
attempt,
error
});
}
}

return false;
}

public async proxyRequestWrapper(req: IncomingMessage, res: ServerResponse): Promise<void> {
Expand Down

0 comments on commit a6494d4

Please sign in to comment.