diff --git a/app/(main)/ix/ix-linux-clients.tsx b/app/(main)/ix/ix-linux-clients.tsx index 03c85e95..4d4be6fb 100644 --- a/app/(main)/ix/ix-linux-clients.tsx +++ b/app/(main)/ix/ix-linux-clients.tsx @@ -9,20 +9,23 @@ import { Badge } from "@/components/reui/badge" import { dataGridFeatures, type DataGridFeatures } from "@/components/reui/data-grid" import { Frame, FrameHeader, FramePanel, FrameTitle } from "@/components/reui/frame" import { Button } from "@/components/ui/button" +import { ButtonGroup } from "@/components/ui/button-group" import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu" -import { Field, FieldDescription, FieldLabel } from "@/components/ui/field" +import { Field, FieldDescription, FieldLabel, FieldTitle } from "@/components/ui/field" import { Input } from "@/components/ui/input" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select" import { Sheet, SheetContent, SheetDescription, SheetFooter, SheetHeader, SheetTitle } from "@/components/ui/sheet" import { Textarea } from "@/components/ui/textarea" +import { isIpv4String } from "@/lib/gre-endpoint-resolve" import { publicEndpoint, transitName } from "./ix-exits" import { nodeLabel, useIxNodeInventory, type IxNode } from "./ix-view" type Binding = IxSite["interfaces"][number] +type ClientSelection = { site: IxSite; binding: Binding; addProtocol?: boolean } type FormState = { serverId: number name: string - kind: IxLinuxClientRequest["kind"] + kind: IxLinuxClientRequest["kind"] | "both" hub: string peer: string key: string @@ -43,35 +46,41 @@ function groupLabel(group: IxSite["groups"][number], site: IxSite, nodes: IxNode } function ClientForm({ segment, edit, nodes, backendUrl, busy, save, cancel }: { - segment: IxSegment; edit: { site: IxSite; binding: Binding } | null; nodes: IxNode[]; backendUrl: string + segment: IxSegment; edit: ClientSelection | null; nodes: IxNode[]; backendUrl: string busy: boolean; save: (input: IxLinuxClientRequest) => Promise; cancel: () => void }) { - const binding = edit?.binding + const existing = edit?.binding + const companion = edit?.addProtocol ? existing : undefined + const binding = companion ? undefined : existing const [form, setForm] = useState(() => ({ serverId: edit?.site.serverId ?? segment.configuration.sites[0].serverId, - name: binding?.linux?.name ?? "", kind: binding?.kind === "gre-ipsec" ? "gre-ipsec" : "wireguard", - hub: binding?.localEndpoint ?? binding?.provisionWireguard?.hubEndpoint ?? "", - peer: binding?.linux?.peerEndpoint ?? binding?.endpoint ?? "", + name: existing?.linux?.name ?? "", + kind: companion ? (companion.kind === "wireguard" ? "gre-ipsec" : "wireguard") + : binding?.kind === "gre-ipsec" ? "gre-ipsec" : "wireguard", + hub: existing?.localEndpoint ?? existing?.provisionWireguard?.hubEndpoint ?? "", + peer: existing?.linux?.peerEndpoint ?? existing?.endpoint ?? "", key: binding?.provisionWireguard?.credentialId ? "" : binding?.provisionWireguard?.peerPublicKey ?? "", localAddress: (binding?.provisionWireguard ?? binding?.provisionGre)?.localAddress ?? "", peerAddress: (binding?.provisionWireguard ?? binding?.provisionGre)?.peerAddress ?? "", prefixes: binding?.prefixes.join("\n") ?? "", destinations: binding?.linux?.destinations.join("\n") ?? "", exit: binding?.linux?.exit ?? "isolated", enabled: binding?.linux?.enabled ?? true, })) + const wireguard = form.kind !== "gre-ipsec" + const gre = form.kind !== "wireguard" const [rotate, setRotate] = useState(false) const origin = useIxNodeInventory(backendUrl, form.serverId, segment.id) const [transitLocal, setTransitLocal] = useState("") const [transitRemote, setTransitRemote] = useState("") const site = segment.configuration.sites.find(site => site.serverId === form.serverId) const suggested = publicEndpoint(origin.inventory) + const hub = form.hub.trim() || suggested const targetId = form.exit.startsWith("remote:") ? Number(form.exit.slice(7)) : 0 const target = useIxNodeInventory(backendUrl, targetId, segment.id) const localEndpoint = transitLocal.trim() || suggested || (form.hub.includes(":") ? "" : form.hub.trim()) const remoteEndpoint = transitRemote.trim() || publicEndpoint(target.inventory) - const family = form.peerAddress.includes(":") ? 6 : 4 + const family = form.kind !== "both" && form.peerAddress.includes(":") ? 6 : 4 const patch = (fields: Partial) => setForm(value => ({ ...value, ...fields })) const nodeChoices = segment.configuration.sites.map(site => ({ value: String(site.serverId), label: nodeLabel(nodes, site.serverId) })) - const transports = [{ value: "wireguard", label: "WireGuard" }, { value: "gre-ipsec", label: "GRE/IPsec (IKEv2)" }] const groups = site?.groups.filter(group => group.onFailure === "block" && group.gateway.includes(":") === (family === 6)) ?? [] const local = groups.find(group => group.id === "ix-wan" + family && group.primary.viaMain) const targets = nodes.filter(node => node.enabled && node.id !== form.serverId && ["ix-node", "exit-node"].includes(node.type ?? "")) @@ -82,7 +91,7 @@ function ClientForm({ segment, edit, nodes, backendUrl, busy, save, cancel }: { ...targets.filter(node => !groups.some(group => site?.interfaces.some(link => link.name === group.primary.interface && link.peerServerId === node.id))) .map(node => ({ value: "remote:" + node.id, label: "Через " + node.name + (node.type === "exit-node" ? " · ExitNode" : " · IX") })), ] - const valid = !!form.name.trim() && !!(form.hub || suggested) && (form.kind === "wireguard" || !!form.peer) && + const valid = !!form.name.trim() && !!hub && (!gre || isIpv4String(form.peer.trim()) && isIpv4String(hub)) && (!targetId || !!localEndpoint && !!remoteEndpoint) && exits.some(exit => exit.value === form.exit) const destinationsField = Разрешённые сети IX