mirror of
https://github.com/Termix-SSH/Termix.git
synced 2026-10-28 14:15:24 +00:00
63 lines
1.6 KiB
TypeScript
63 lines
1.6 KiB
TypeScript
import type { Client } from "ssh2";
|
|
import { usesIssuedCertificate } from "../../../src/backend/hosts/issued-certificate-auth.js";
|
|
|
|
export type StatsCapableHost = {
|
|
connectionType?: string;
|
|
authType?: string;
|
|
};
|
|
|
|
export type TcpPingStatsConfig = {
|
|
statusCheckEnabled: boolean;
|
|
disableTcpPing?: boolean;
|
|
};
|
|
|
|
export function supportsMetrics(host: StatsCapableHost): boolean {
|
|
const connectionType = host.connectionType || "ssh";
|
|
if (connectionType !== "ssh") return false;
|
|
if (host.authType === "none" || usesIssuedCertificate(host.authType))
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
export function isTcpPingEnabled(statsConfig: TcpPingStatsConfig): boolean {
|
|
return statsConfig.statusCheckEnabled && !statsConfig.disableTcpPing;
|
|
}
|
|
|
|
export function parseStatusHostIds(value: unknown): Set<number> | null {
|
|
if (value === undefined) return null;
|
|
if (typeof value !== "string") return new Set();
|
|
|
|
return new Set(
|
|
value
|
|
.split(",")
|
|
.map(Number)
|
|
.filter((id) => Number.isSafeInteger(id) && id > 0),
|
|
);
|
|
}
|
|
|
|
export function tcpPingThroughJumpHost(
|
|
jumpClient: Pick<Client, "forwardOut" | "end">,
|
|
host: string,
|
|
port: number,
|
|
timeoutMs = 5000,
|
|
): Promise<boolean> {
|
|
return new Promise((resolve) => {
|
|
let settled = false;
|
|
|
|
const finish = (result: boolean) => {
|
|
if (settled) return;
|
|
settled = true;
|
|
clearTimeout(timeout);
|
|
jumpClient.end();
|
|
resolve(result);
|
|
};
|
|
|
|
const timeout = setTimeout(() => finish(false), timeoutMs);
|
|
|
|
jumpClient.forwardOut("127.0.0.1", 0, host, port, (error, stream) => {
|
|
stream?.destroy();
|
|
finish(!error && !!stream);
|
|
});
|
|
});
|
|
}
|