// — the destinations the swarm lets one agent reach through // its hive's egress proxy, beyond the base list every agent has: the entries // that hold for every agent and the ones for this agent alone, with an editor // to add and remove them. // // Reads `GET /api/egress/allowed-domains`, which answers every entry, and // shows the ones that apply to this agent. Adding POSTs one entry and removing // DELETEs one, each with the entry as the body, each answering the whole list. // The route's refusal (an IP address, a wildcard, a name the proxy already // allows everyone) shows as it arrives, next to the box. The hive reads a // change within about 30 seconds. // // An entry adds to the names the hive's own configuration allows, which stay // allowed. An agent that holds the unrestricted // internet flag reaches any name the proxy's private-range rules leave, so an // entry matters only to a restricted one. // // An entry the controller keeps from the agent's backend environment or a linked // account (`derived`) shows as automatic with no remove control; the route // refuses to remove it. // // Its state belongs to one agent: callers key it by agent name. import { useState } from "preact/hooks"; import { ApiErrorPanel } from "@hive/shared/api-error-panel.js"; import { readApiError, type ProblemDetails } from "@hive/shared/api-error.js"; import { Badge } from "@hive/shared/badge.js"; import { ActionRow } from "../../ui/action-row/ActionRow.js"; import { BadgePopover } from "../../ui/badge-popover/BadgePopover.js"; import { Button } from "../../ui/button/Button.js"; import { FormStack } from "../../ui/form-stack/FormStack.js"; import { CheckboxField } from "../../ui/checkbox-field/CheckboxField.js"; import { Dialog } from "../../ui/dialog/Dialog.js"; import { Panel } from "../../ui/panel/Panel.js"; import { TextField } from "../../ui/text-field/TextField.js"; import { ALLOWED_DOMAINS_URL, DENIED_DOMAINS_URL, entriesFor, entryBody, entryGroups, type EntryGroup, } from "./allowed-domains.js"; import { useAllowedDomains } from "./useAllowedDomains.js"; import { BadgeSkeleton } from "../../ui/skeleton/BadgeSkeleton.js"; import "../../ui/badge-row/BadgeRow.css"; import "./AgentLimits.css"; // What each source's badge says, and what its list holds. const GROUP_LABEL: Record = { "swarm-wide": "swarm-wide", own: "this agent", automatic: "automatic", }; function groupHelp(source: EntryGroup["source"], agent: string): string { switch (source) { case "swarm-wide": return "Names every agent in the swarm may reach through its hive's proxy."; case "own": return `Names only ${agent} may reach through its hive's proxy. Remove one to stop allowing it.`; case "automatic": return `Kept from ${agent}'s backend environment or a linked account; change or unlink that to change them.`; } } export function AllowedDomains({ agent }: { agent: string }) { const { entries, error, busy, remove, refetch } = useAllowedDomains(); const [adding, setAdding] = useState(false); // A list already loaded stays under a failed refetch, so a change that // worked is not hidden by a read that did not. const errorPanel = error ? ( <> ) : null; if (entries === null) { return ( errorPanel ?? ); } const shown = entriesFor(entries, agent); return (
{errorPanel}
{shown.length === 0 ? ( ) : null} {entryGroups(shown).map((g) => (

{groupHelp(g.source, agent)}

    {g.entries.map((e) => (
  • {" "} {e.derived === true ? null : ( void remove(e)} /> )}
  • ))}
))} setAdding(true)} />
setAdding(false)} label="add an allowed domain" plain > {/* Remounted per open, so a cancelled add leaves nothing behind. */} {adding ? ( setAdding(false)} onAdded={() => { setAdding(false); refetch(); }} /> ) : null}
); } // The add form of one allowed or denied domain: for `agent`, or for every // agent. With no `agent` (the swarm page) it can only add a swarm-wide entry. export function AddForm({ agent, kind = "allow", onClose, onAdded, }: { agent?: string; kind?: "allow" | "deny"; onClose: () => void; onAdded: () => void; }) { const deny = kind === "deny"; const [domain, setDomain] = useState(""); const [forSwarm, setForSwarm] = useState(agent === undefined); const [busy, setBusy] = useState(false); const [problem, setProblem] = useState(null); async function save(e: Event) { e.preventDefault(); setBusy(true); setProblem(null); try { const r = await fetch(deny ? DENIED_DOMAINS_URL : ALLOWED_DOMAINS_URL, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(entryBody(domain, forSwarm, agent ?? "")), }); if (!r.ok) { // The box stays as typed, so the operator can fix it and add again. setProblem(await readApiError(r)); return; } onAdded(); } catch (err: unknown) { setProblem({ detail: String(err) }); } finally { setBusy(false); } } return (

{deny ? agent === undefined ? "Stops every agent reaching one name through its hive's proxy." : `Stops ${agent} reaching one name through its hive's proxy, or every agent when the box below is ticked.` : agent === undefined ? "Lets every agent reach one more name through its hive's proxy." : `Lets ${agent} reach one more name through its hive's proxy, or every agent when the box below is ticked.`}{" "} A name needs at least two labels, such as github.com; a leading dot, as in .githubusercontent.com, also{" "} {deny ? "refuses" : "allows"} every subdomain. IP addresses and wildcards are refused.{" "} {deny ? "A denied name is refused in every egress mode, even when a swarm entry or a hive's own configuration allows it." : "A name that resolves to a private address stays blocked. The entry adds to the names each hive's own configuration allows."}{" "} The hive reads a change within about 30 seconds.

void save(e)}> {agent === undefined ? null : ( )} {problem ? ( ) : null}
); }