"use client"; import { useCallback, useEffect, useRef, useState } from "react"; import { Terminal, useTerminal } from "@wterm/react"; import "@wterm/react/css"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { TerminalSquare, Loader2, LogOut } from "lucide-react"; type AuthMethod = "password" | "privateKey"; type ConnectionState = "disconnected" | "connecting" | "connected" | "error"; interface ConnectionParams { host: string; port: number; username: string; password?: string; privateKey?: string; } export default function SSHClient() { const { ref, write } = useTerminal(); const wsRef = useRef(null); const [state, setState] = useState("disconnected"); const [error, setError] = useState(""); const [host, setHost] = useState("cmd.ai"); const [port, setPort] = useState("22"); const [username, setUsername] = useState(""); // Restore saved username from localStorage after hydration — // synchronous setState is intentional to avoid a flash of empty input. useEffect(() => { const saved = localStorage.getItem("ssh:username"); // eslint-disable-next-line react-hooks/set-state-in-effect if (saved) setUsername(saved); }, []); const [authMethod, setAuthMethod] = useState("privateKey"); const [password, setPassword] = useState(""); const [privateKey, setPrivateKey] = useState(""); const connect = useCallback(() => { if (!host || !username) return; setState("connecting"); setError(""); const proto = window.location.protocol === "https:" ? "wss:" : "ws:"; const wsUrl = `${proto}//${window.location.host}/api/ssh`; const ws = new WebSocket(wsUrl); wsRef.current = ws; ws.binaryType = "arraybuffer"; ws.onopen = () => { const params: ConnectionParams = { host, port: parseInt(port, 10) || 22, username, }; if (authMethod === "privateKey" && privateKey) { params.privateKey = privateKey; } else if (password) { params.password = password; } ws.send(JSON.stringify(params)); localStorage.setItem("ssh:username", username); setState("connected"); }; ws.onmessage = (event: MessageEvent) => { if (event.data instanceof ArrayBuffer) { const text = new TextDecoder("latin1").decode(event.data); write(text); } else { const data = event.data as string; if (data.startsWith("{")) { try { const msg = JSON.parse(data); if (msg.error) { setError(msg.error); setState("error"); return; } } catch { /* not JSON, treat as terminal data */ } } write(data); } }; ws.onclose = () => { setState((current) => { if (current === "connecting") { setError("Connection failed"); return "error"; } return "disconnected"; }); wsRef.current = null; }; ws.onerror = () => { setError("WebSocket connection failed"); setState("error"); }; }, [host, port, username, authMethod, password, privateKey, write]); const disconnect = useCallback(() => { wsRef.current?.close(); wsRef.current = null; setState("disconnected"); }, []); const handleData = useCallback((data: string) => { if (wsRef.current?.readyState === WebSocket.OPEN) { wsRef.current.send(data); } }, []); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); connect(); }; if (state === "connected") { return (
{username}@{host}:{port}
); } return (

SSH Client

Connect to a remote server via SSH

setHost(e.target.value)} required />
setPort(e.target.value)} />
setUsername(e.target.value)} required />
{authMethod === "password" ? (
setPassword(e.target.value)} placeholder="••••••••" />
) : (