"use client"; import React, { useEffect, useState } from "react"; import Link from "next/link"; import { RefreshCw, Save } from "lucide-react"; import { api, errorText } from "@/lib/api"; import { fmtDate } from "@/lib/format"; import { StatusPill } from "@/components/servers"; /** * frps's config (central server) or frpc's (church server) from the Hub (2.11.0, agent 1.4.0): * where the file is, what's in it (secrets hidden on the server), and changing it. A change is * checked with `frp* verify` on the server, a backup is kept, and the old file goes back when frp * doesn't start with the new one. */ export type FrpRead = { service: "frps" | "frpc"; path: string | null; source: "hub" | "systemd" | "usual-place" | null; found: boolean; format: "ini" | "toml" | null; content: string | null; hidden: string[]; proxyBindAddr: string | null; allowPorts: string | null; serviceRunning: boolean | null; looked?: string[]; readAt: string; jobId?: string; }; export type FrpState = { service: "frps" | "frpc"; path: string | null; read: FrpRead | null; pending: { id: string; type: string; status: string } | null; last: { id: string; type: string; status: string; message: string | null; finishedAt: string | null } | null; minAgent: string; hidden: string; }; type Msg = { type: "ok" | "err"; text: string } | null; const SOURCE: Record = { hub: "set here", systemd: "from the systemd unit", "usual-place": "found in a usual place" }; /** Compares dotted versions: is `have` at least `need`? */ function atLeast(have: string | null, need: string): boolean { const a = (have || "0").split(".").map(Number); const b = need.split(".").map(Number); for (let i = 0; i < 3; i++) if ((a[i] || 0) !== (b[i] || 0)) return (a[i] || 0) > (b[i] || 0); return true; } export function FrpConfig({ serverId, agentVersion, frp, onChange }: { serverId: string; agentVersion: string | null; frp: FrpState; onChange: (m: Msg) => void }) { const read = frp.read; const [path, setPath] = useState(frp.path || read?.path || ""); const [content, setContent] = useState(read?.content ?? ""); const [restart, setRestart] = useState(true); const [totp, setTotp] = useState(""); const [busy, setBusy] = useState(false); const supported = atLeast(agentVersion, frp.minAgent); const edited = !!read?.found && content !== (read.content ?? ""); // A new read replaces what's in the editor. useEffect(() => { setContent(read?.content ?? ""); }, [read?.readAt, read?.content]); useEffect(() => { setPath(frp.path || read?.path || ""); }, [frp.path, read?.path]); const go = async (url: string, body: Record, ok: string) => { setBusy(true); try { await api(url, { method: "POST", body }); onChange({ type: "ok", text: ok }); setTotp(""); } catch (e) { onChange({ type: "err", text: errorText(e) }); } setBusy(false); }; const readNow = (withPath: boolean) => go(`/api/admin/servers/${serverId}/frp/read`, withPath ? { path: path.trim() } : {}, "Asked the agent to read the config: it shows here within a minute (reload the page)."); const save = (e: React.FormEvent) => { e.preventDefault(); if (!read?.path || !window.confirm(`Replace ${read.path} on the server${restart ? ` and restart ${frp.service}` : ""}? Every church site's tunnel drops for a few seconds while ${frp.service} restarts.`)) return; go(`/api/admin/servers/${serverId}/frp/write`, { content, restart, totp }, `Sent: the agent checks it with ${frp.service} verify, keeps a backup${restart ? `, restarts ${frp.service}` : ""}, and puts the old file back if ${frp.service} doesn't start.`); }; return (

frp config ({frp.service})

{frp.pending && {frp.pending.type === "frp-write" ? "A change" : "A read"} is on its way: {frp.pending.status}}
{!supported &&

The agent here is {agentVersion || "an unknown version"}; reading and changing frp's config needs agent {frp.minAgent} or later. Update the agent first.

}
setPath(e.target.value)} placeholder={`Found by the agent (e.g. /etc/frp/${frp.service}.toml)`} disabled={!supported} />

Empty: the agent finds it itself (the -c in {frp.service}'s systemd unit, else /etc/frp and other usual places). {frp.service === "frpc" && " Installs on this server add their tunnels to this file."}

{read && (
{read.found ? (

{read.path} ({read.format}, {SOURCE[read.source || ""] || "found"}) · read {fmtDate(read.readAt)} ·{" "} {read.service} {read.serviceRunning === null ? "unknown" : read.serviceRunning ? "running" : not running} {read.service === "frps" && <> · proxy_bind_addr {read.proxyBindAddr || not set} · allow_ports {read.allowPorts || "any"}}

) : (

No {read.service} config found{read.path ? <> at {read.path} : ""}. {read.looked?.length ? <> Looked in: {read.looked.join(", ")}. : null} Set where it is above.

)}
)} {frp.last && frp.last.status !== "succeeded" && (

Last {frp.last.type === "frp-write" ? "change" : "read"}: {frp.last.message} Open

)} {read?.found && (