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