import { useState } from "react"; import { ChevronDown } from "lucide-react"; import { invoke } from "@tauri-apps/api/core"; import type { RouterItem } from "../types/router"; import { AlertTriangle, CheckCircle2, Router, Search, ShieldCheck, XCircle, } from "lucide-react"; type DetectionResult = { detected: boolean; ip: string | null; method: string; ssh_reachable: boolean; message: string; }; type PrepareResult = { success: boolean; hostname_updated: boolean; password_updated: boolean; message: string; logs: string; }; type CompatibilityResult = { compatible: boolean; version: string | null; target: string | null; arch: string | null; reason: string | null; }; type ReadinessResult = { internet_ok: boolean; dns_ok: boolean; opkg_ok: boolean; openvpn_installed: boolean; free_space: string | null; free_memory: string | null; }; type SshResult = { connected: boolean; host: string; hostname: string | null; openwrt_release: string | null; compatibility: CompatibilityResult | null; readiness: ReadinessResult | null; message: string; }; type Props = { routers: RouterItem[]; }; export function OpenWrtConfigPage({ routers }: Props) { const [detecting, setDetecting] = useState(false); const [connecting, setConnecting] = useState(false); const [detection, setDetection] = useState(null); const [sshResult, setSshResult] = useState(null); const [host, setHost] = useState(""); const [username, setUsername] = useState("root"); const [password, setPassword] = useState(""); const [selectedRouterId, setSelectedRouterId] = useState(""); const [newRootPassword, setNewRootPassword] = useState(""); const [confirmRootPassword, setConfirmRootPassword] = useState(""); const [routerDropdownOpen, setRouterDropdownOpen] = useState(false); const [preparing, setPreparing] = useState(false); const [prepareResult, setPrepareResult] = useState(null); const compatibility = sshResult?.compatibility; async function detectRouter() { try { setDetecting(true); setSshResult(null); const result = await invoke("detect_openwrt_router"); setDetection(result); if (result.ip) { setHost(result.ip); } } finally { setDetecting(false); } } const sshAuthFailed = sshResult && !sshResult.connected && sshResult.message.toLowerCase().includes("permission denied"); async function prepareRouter() { if (!selectedRouter || !targetHostname) return; try { setPreparing(true); setPrepareResult(null); const result = await invoke("prepare_openwrt_router", { host, username, password, targetHostname, newPassword: newRootPassword, }); setPrepareResult(result); if (result.success) { setPassword(newRootPassword); setNewRootPassword(""); setConfirmRootPassword(""); const refreshed = await invoke("test_openwrt_ssh", { host, username, password: newRootPassword, }); setSshResult(refreshed); } } finally { setPreparing(false); } } async function connectSsh() { try { setConnecting(true); const result = await invoke("test_openwrt_ssh", { host, username, password, }); setSshResult(result); } finally { setConnecting(false); } } const selectedRouter = routers.find((router) => router.id === selectedRouterId); const vpnIpParts = selectedRouter?.vpnIp?.split(".") ?? []; const targetHostname = selectedRouter?.vpnIp ? `Litoral_Regas_${vpnIpParts[vpnIpParts.length - 1]}` : ""; return (

Configuração OpenWRT

Detetar router local, testar SSH e validar compatibilidade

Deteção do Router

Procura pelo gateway local ou pelo IP padrão OpenWRT

{detection ? detection.message : "Ainda não foi executada nenhuma deteção."}

{detection?.ip && (
)}

Ligação SSH

Introduza as credenciais do router OpenWRT

{sshResult && (

Resultado da Ligação

Estado da sessão SSH com o router

{!sshResult.connected && (

{sshAuthFailed ? "Não foi possível autenticar. Verifique a password SSH." : sshResult.message}

)}
)} {sshResult?.connected && compatibility && (

Compatibilidade OpenWRT

Validação da versão e arquitetura do firmware

{compatibility.compatible ? "Compatível" : "Não compatível"}
{compatibility.compatible ? ( ) : ( )}

{compatibility.compatible ? "Este router cumpre os requisitos mínimos para configuração OpenVPN." : compatibility.reason || "Este router não cumpre os requisitos mínimos."}

{sshResult.openwrt_release && (
{sshResult.openwrt_release}
)}
)} {sshResult?.connected && sshResult.readiness && (

Estado do Router

Verificações antes da configuração OpenVPN

)} {sshResult?.connected && sshResult.readiness && (

Preparação do Router

Associe este router físico a um router da plataforma

{selectedRouter && (
)}
)} {prepareResult && (

Resultado da Preparação

Estado da configuração inicial do router

{prepareResult.success ? "Sucesso" : "Falhou"}
{prepareResult.success ? : }

{prepareResult.message}

{prepareResult.logs && (
{prepareResult.logs}
)}
)}
); } function DetailRow({ label, value }: { label: string; value: string }) { return (
{label} {value}
); }