Skip to content

Commit

Permalink
✨ Add missing keep-alive config options
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Dvorak <[email protected]>
  • Loading branch information
Tomas2D committed Sep 18, 2023
1 parent c9e2bb5 commit 4bd02d3
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/UltraVNCRepeater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ export class UltraVNCRepeater extends EventEmitter {
port: this._options.serverPort,
bufferSize: this._options.bufferSize,
socketTimeout: this._options.socketTimeout,
keepAlive: this._options.keepAlive,
},
this._logger,
);
Expand Down Expand Up @@ -386,6 +387,7 @@ export class UltraVNCRepeater extends EventEmitter {
port: this._options.clientPort,
bufferSize: this._options.bufferSize,
socketTimeout: this._options.socketTimeout,
keepAlive: this._options.keepAlive,
},
this._logger,
);
Expand Down
18 changes: 18 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const EnvName = createEnumLowerCase([
"REPEATER_REFUSE",
"REPEATER_LOGFILE",
"REPEATER_LOG_LEVEL",
"REPEATER_SOCKET_TIMEOUT",
"REPEATER_SOCKET_KEEPALIVE",
] as const);

export async function run() {
Expand Down Expand Up @@ -82,6 +84,22 @@ export async function run() {
default: getEnv(EnvName.REPEATER_LOG_LEVEL, { type: "string" }),
demandOption: false,
},
socketTimeout: {
describe: `Provide socket timeout in seconds (or use '${EnvName.REPEATER_SOCKET_TIMEOUT}' env)`,
type: "number",
default: getEnv(EnvName.REPEATER_SOCKET_TIMEOUT, {
type: "number",
}),
demandOption: false,
},
keepAlive: {
describe: `Provide keep-alive interval in seconds (or use '${EnvName.REPEATER_SOCKET_KEEPALIVE}' env)`,
type: "number",
default: getEnv(EnvName.REPEATER_SOCKET_KEEPALIVE, {
type: "number",
}),
demandOption: false,
},
})
.parseAsync();

Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const DefaultServerOptions: VNCRepeaterOptions = {
logger: undefined,
socketTimeout: 30 * 60,
killTimeout: 30,
keepAlive: 10,
} as const;

export const EventInternal = createEnumLowerCase([
Expand Down
19 changes: 12 additions & 7 deletions src/gateway/BaseGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ export abstract class BaseGateway extends EventEmitter {
protected _logger: Logger;

protected constructor(
protected readonly _options: Pick<VNCRepeaterOptions, "socketTimeout"> & {
protected readonly _options: Pick<
VNCRepeaterOptions,
"socketTimeout" | "keepAlive"
> & {
port: number;
},
logger: Logger,
Expand All @@ -30,7 +33,7 @@ export abstract class BaseGateway extends EventEmitter {

this._server = net.createServer(
{
keepAlive: true,
keepAlive: Boolean(this._options.keepAlive),
allowHalfOpen: false,
pauseOnConnect: false,
},
Expand Down Expand Up @@ -135,12 +138,14 @@ export abstract class BaseGateway extends EventEmitter {
);
socket.setTimeout(this._options.socketTimeout * 1000);
}
socket.setKeepAlive(true, 1000);
setKeepAliveInterval(socket, 1000);
setKeepAliveProbes(socket, 1);
if (this._options.keepAlive) {
socket.setKeepAlive(true, this._options.keepAlive * 1000);
setKeepAliveInterval(socket, this._options.keepAlive * 1000);
setKeepAliveProbes(socket, 1);
}
socket.on("error", (e) => {
this._logger.error(e, "Socket error");
closeSocket(socket);
this._logger.error(e, "Socket has occurred an error");
closeSocket(socket, true);
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/gateway/ClientGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class ClientGateway extends BaseGateway {
constructor(
protected readonly _options: Pick<
VNCRepeaterOptions,
"noRFB" | "bufferSize" | "refuse" | "socketTimeout"
"noRFB" | "bufferSize" | "refuse" | "socketTimeout" | "keepAlive"
> & {
port: number;
},
Expand Down
2 changes: 1 addition & 1 deletion src/gateway/ServerGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class ServerGateway extends BaseGateway {
constructor(
protected readonly _options: Pick<
VNCRepeaterOptions,
"bufferSize" | "refuse" | "socketTimeout"
"bufferSize" | "refuse" | "socketTimeout" | "keepAlive"
> & { port: number },
logger: Logger,
) {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface VNCRepeaterOptions {
logger?: Logger;
logLevel?: LogLevel;
socketTimeout?: number;
keepAlive?: number;
killTimeout?: number;
}

Expand Down

0 comments on commit 4bd02d3

Please sign in to comment.