feat: ✨ added api key support #13
+4
-1
@@ -26,4 +26,7 @@ MYSQL_POOL_SIZE=5
|
||||
MAX_CONCURRENT_SSH=32
|
||||
|
||||
# Paramiko SSH keepalive interval (seconds); set 0 to disable.
|
||||
SSH_KEEPALIVE_INTERVAL=30
|
||||
SSH_KEEPALIVE_INTERVAL=15
|
||||
|
||||
# WebSocket keepalive interval (seconds); server sends traffic to avoid proxy idle timeouts.
|
||||
WS_KEEPALIVE_INTERVAL=25
|
||||
@@ -644,7 +644,26 @@ def _close_ssh_entry(entry: dict[str, Any]) -> None:
|
||||
|
||||
|
||||
MAX_CONCURRENT_SSH = int(os.getenv("MAX_CONCURRENT_SSH", "32"))
|
||||
SSH_KEEPALIVE_INTERVAL = int(os.getenv("SSH_KEEPALIVE_INTERVAL", "30"))
|
||||
SSH_KEEPALIVE_INTERVAL = int(os.getenv("SSH_KEEPALIVE_INTERVAL", "15"))
|
||||
WS_KEEPALIVE_INTERVAL = int(os.getenv("WS_KEEPALIVE_INTERVAL", "25"))
|
||||
|
||||
|
||||
def _apply_ssh_keepalive(client: paramiko.SSHClient) -> None:
|
||||
if SSH_KEEPALIVE_INTERVAL <= 0:
|
||||
return
|
||||
transport = client.get_transport()
|
||||
if transport is not None:
|
||||
transport.set_keepalive(SSH_KEEPALIVE_INTERVAL)
|
||||
|
||||
|
||||
def _ssh_transports_alive(
|
||||
client: paramiko.SSHClient, jump_clients: list[paramiko.SSHClient] | None
|
||||
) -> bool:
|
||||
for ssh_client in (client, *(jump_clients or [])):
|
||||
transport = ssh_client.get_transport()
|
||||
if transport is None or not transport.is_active():
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
class GeventWsAppResponse(Response):
|
||||
@@ -795,9 +814,7 @@ def _connect_paramiko(host_row: dict, sock=None) -> tuple[paramiko.SSHClient, pa
|
||||
)
|
||||
|
||||
if SSH_KEEPALIVE_INTERVAL > 0:
|
||||
transport = client.get_transport()
|
||||
if transport is not None:
|
||||
transport.set_keepalive(SSH_KEEPALIVE_INTERVAL)
|
||||
_apply_ssh_keepalive(client)
|
||||
|
||||
chan = client.invoke_shell(term="xterm-256color", width=120, height=40)
|
||||
chan.setblocking(True)
|
||||
@@ -1839,8 +1856,8 @@ def ws_terminal():
|
||||
height=int(o.get("rows", 40)),
|
||||
)
|
||||
return True
|
||||
elif o.get("type") == "ping":
|
||||
# Ping message to keep connection alive, ignore without sending to channel
|
||||
if o.get("type") == "ping":
|
||||
sock.send(json.dumps({"type": "pong"}))
|
||||
return True
|
||||
except (json.JSONDecodeError, TypeError, ValueError):
|
||||
pass
|
||||
@@ -1889,6 +1906,7 @@ def ws_terminal():
|
||||
)
|
||||
)
|
||||
|
||||
last_ws_keepalive = time.monotonic()
|
||||
while not stop.is_set():
|
||||
drained_eof = False
|
||||
try:
|
||||
@@ -1898,16 +1916,31 @@ def ws_terminal():
|
||||
drained_eof = True
|
||||
break
|
||||
sock.send(item)
|
||||
last_ws_keepalive = time.monotonic()
|
||||
except queue.Empty:
|
||||
pass
|
||||
if drained_eof:
|
||||
break
|
||||
|
||||
now = time.monotonic()
|
||||
if (
|
||||
WS_KEEPALIVE_INTERVAL > 0
|
||||
and now - last_ws_keepalive >= WS_KEEPALIVE_INTERVAL
|
||||
):
|
||||
if not _ssh_transports_alive(client, jump_clients):
|
||||
break
|
||||
try:
|
||||
sock.send(json.dumps({"type": "keepalive"}))
|
||||
except Exception:
|
||||
break
|
||||
last_ws_keepalive = now
|
||||
|
||||
msg = sock.receive(timeout=0.15)
|
||||
if msg is None:
|
||||
if use_gevent_wsgi and getattr(sock, "_gw", None) is not None and sock._gw.closed:
|
||||
break
|
||||
continue
|
||||
last_ws_keepalive = time.monotonic()
|
||||
if not handle_ws_inbound(msg):
|
||||
break
|
||||
except ConnectionClosed:
|
||||
|
||||
@@ -24,7 +24,7 @@ let ws: WebSocket | null = null;
|
||||
let term: Terminal | null = null;
|
||||
let fit: FitAddon | null = null;
|
||||
let ro: ResizeObserver | null = null;
|
||||
let pingInterval: NodeJS.Timeout | null = null;
|
||||
let visibilityHandler: (() => void) | null = null;
|
||||
|
||||
function wsUrl(hostId: number): string {
|
||||
const proto = location.protocol === "https:" ? "wss:" : "ws:";
|
||||
@@ -37,6 +37,12 @@ function sendResize() {
|
||||
ws.send(JSON.stringify({ type: "resize", ...dims }));
|
||||
}
|
||||
|
||||
function sendPing() {
|
||||
if (ws && ws.readyState === WebSocket.OPEN) {
|
||||
ws.send(JSON.stringify({ type: "ping" }));
|
||||
}
|
||||
}
|
||||
|
||||
function fitAndResize() {
|
||||
if (!fit || !term || !props.visible) return;
|
||||
try {
|
||||
@@ -47,6 +53,25 @@ function fitAndResize() {
|
||||
}
|
||||
}
|
||||
|
||||
function isControlMessage(raw: string): boolean {
|
||||
try {
|
||||
const o = JSON.parse(raw) as { type?: string; conn_id?: string };
|
||||
if (o.type === "ready" && o.conn_id) {
|
||||
connId.value = o.conn_id;
|
||||
status.value = "";
|
||||
fitAndResize();
|
||||
term?.focus();
|
||||
return true;
|
||||
}
|
||||
if (o.type === "keepalive" || o.type === "pong") {
|
||||
return true;
|
||||
}
|
||||
} catch {
|
||||
/* not JSON control traffic */
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
await nextTick();
|
||||
if (!termEl.value) return;
|
||||
@@ -87,29 +112,12 @@ onMounted(async () => {
|
||||
ws.onopen = () => {
|
||||
status.value = "Handshaking…";
|
||||
sendResize();
|
||||
// Send ping every 60 seconds to keep connection alive
|
||||
pingInterval = setInterval(() => {
|
||||
if (ws && ws.readyState === WebSocket.OPEN) {
|
||||
ws.send(JSON.stringify({ type: "ping" }));
|
||||
}
|
||||
}, 60000);
|
||||
};
|
||||
|
||||
ws.onmessage = (ev) => {
|
||||
if (!term) return;
|
||||
if (typeof ev.data === "string") {
|
||||
try {
|
||||
const o = JSON.parse(ev.data) as { type?: string; conn_id?: string };
|
||||
if (o.type === "ready" && o.conn_id) {
|
||||
connId.value = o.conn_id;
|
||||
status.value = "";
|
||||
fitAndResize();
|
||||
term.focus();
|
||||
return;
|
||||
}
|
||||
} catch {
|
||||
/* fall through */
|
||||
}
|
||||
if (isControlMessage(ev.data)) return;
|
||||
term.write(ev.data);
|
||||
return;
|
||||
}
|
||||
@@ -122,22 +130,26 @@ onMounted(async () => {
|
||||
};
|
||||
|
||||
ws.onclose = () => {
|
||||
if (pingInterval) {
|
||||
clearInterval(pingInterval);
|
||||
pingInterval = null;
|
||||
}
|
||||
if (!connId.value) {
|
||||
status.value = "Disconnected";
|
||||
} else {
|
||||
status.value = "Session ended";
|
||||
}
|
||||
};
|
||||
|
||||
visibilityHandler = () => {
|
||||
if (document.visibilityState === "visible") {
|
||||
sendPing();
|
||||
fitAndResize();
|
||||
}
|
||||
};
|
||||
document.addEventListener("visibilitychange", visibilityHandler);
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
if (pingInterval) {
|
||||
clearInterval(pingInterval);
|
||||
pingInterval = null;
|
||||
if (visibilityHandler) {
|
||||
document.removeEventListener("visibilitychange", visibilityHandler);
|
||||
visibilityHandler = null;
|
||||
}
|
||||
ro?.disconnect();
|
||||
ro = null;
|
||||
@@ -155,6 +167,7 @@ watch(
|
||||
await nextTick();
|
||||
fitAndResize();
|
||||
term?.focus();
|
||||
sendPing();
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user