mirror of
https://gitcode.com/JianFeeeee/webui4frpc.git
synced 2026-09-26 04:12:55 +00:00
- Cluster: forwardToNext offline detection (leader+non-leader), WatchLeader 1s heartbeat fallback, 409 for standalone nodes, Node.NodeKey key exchange via token ring, ClusterPeers persistence + auto-rejoin, Forward delegates to forwardToNext (bugfix) - Auth: Basic Auth (flag-creds fast path) + bcrypt users (admin/viewer) + Bearer API keys (read/write/admin scope) - Frontend: UsersView (accounts+API keys), ClusterView (ring/nodeKey/tasks/topology/log), StatusView (group management, per-proxy status), CanvasView (edge toggle/group), PortEdge (disabled/group labels) - API: handlers split (canvas/forwards/users/logs), canvas export/import, forwards group start/stop/assign/delete, cluster endpoints - Docs: comprehensive README rewrite (all flags/APIs/auth/cluster), docs/cluster-api.md (cluster management API reference) - Deploy: run-cluster.sh now 4-node ring + 1 isolated standalone, test-forward.sh updated for 4 nodes - Removed plan.md (design notes consolidated into README + API docs)
209 lines
7.8 KiB
TypeScript
209 lines
7.8 KiB
TypeScript
// HTTP client and API functions for webui4frpc.
|
|
import type {
|
|
ApiKey,
|
|
ApiKeyCreated,
|
|
BinaryStatus,
|
|
CacheResp,
|
|
CanvasData,
|
|
CanvasExportEnvelope,
|
|
ClusterNodesResp,
|
|
InstallResult,
|
|
MeResp,
|
|
Remote,
|
|
RingSnapshot,
|
|
Settings,
|
|
StatusResp,
|
|
User,
|
|
WorkerLogBundle,
|
|
} from "./types";
|
|
|
|
class HTTPError extends Error {
|
|
status: number;
|
|
constructor(status: number, message: string) {
|
|
super(message);
|
|
this.status = status;
|
|
}
|
|
}
|
|
|
|
async function request<T>(url: string, options: RequestInit = {}): Promise<T> {
|
|
const response = await fetch(url, {
|
|
credentials: "same-origin",
|
|
...options,
|
|
});
|
|
if (!response.ok) {
|
|
throw new HTTPError(response.status, `HTTP ${response.status}`);
|
|
}
|
|
const ct = response.headers.get("content-type") || "";
|
|
if (ct.includes("application/json")) {
|
|
return response.json() as Promise<T>;
|
|
}
|
|
return response.text() as unknown as Promise<T>;
|
|
}
|
|
|
|
const json = (body: unknown): RequestInit => ({
|
|
method: "PUT",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(body),
|
|
});
|
|
|
|
export const api = {
|
|
status: () => request<StatusResp>("/api/manager/status"),
|
|
|
|
canvas: () => request<CanvasData>("/api/manager/canvas"),
|
|
saveCanvas: (data: CanvasData) =>
|
|
request<CanvasData>("/api/manager/canvas", json(data)),
|
|
|
|
settings: () => request<Settings>("/api/manager/settings"),
|
|
saveSettings: (s: Settings) =>
|
|
request<Settings>("/api/manager/settings", json(s)),
|
|
|
|
binaryStatus: () => request<BinaryStatus>("/api/manager/binary/status"),
|
|
installBinary: (version?: string) =>
|
|
request<InstallResult>("/api/manager/binary/install", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: version ? JSON.stringify({ version }) : undefined,
|
|
}),
|
|
|
|
saveRemote: (remote: Remote) =>
|
|
request<Remote>("/api/manager/remotes", json(remote)),
|
|
deleteRemote: (name: string) =>
|
|
request<void>(`/api/manager/remotes/${encodeURIComponent(name)}`, {
|
|
method: "DELETE",
|
|
}),
|
|
|
|
// Per-forward start/stop (forwards page). local-only forwards toggle the
|
|
// local frpc worker; cluster forwards submit/revoke via the ring.
|
|
forwardStart: (local: string, remote: string, remotePort: number) =>
|
|
request<{ ok: boolean }>("/api/manager/forwards/start", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ local, remote, remotePort }),
|
|
}),
|
|
forwardStop: (local: string, remote: string, remotePort: number) =>
|
|
request<{ ok: boolean }>("/api/manager/forwards/stop", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ local, remote, remotePort }),
|
|
}),
|
|
groupStart: (group: string) =>
|
|
request<{ ok: boolean }>("/api/manager/forwards/group/start", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ group }),
|
|
}),
|
|
groupStop: (group: string) =>
|
|
request<{ ok: boolean }>("/api/manager/forwards/group/stop", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ group }),
|
|
}),
|
|
// assignGroup changes a single forward's group label (status page chip).
|
|
// Empty group clears the assignment (移出分组). Pure DB update, no worker.
|
|
assignGroup: (local: string, remote: string, remotePort: number, group: string) =>
|
|
request<{ ok: boolean }>("/api/manager/forwards/assign", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ local, remote, remotePort, group }),
|
|
}),
|
|
// deleteGroup dissolves a group: all members moved to 未分组. The group is
|
|
// just a label on links, so clearing all members is the complete delete.
|
|
deleteGroup: (group: string) =>
|
|
request<{ ok: boolean }>("/api/manager/forwards/group/delete", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ group }),
|
|
}),
|
|
|
|
profileStart: (name: string) =>
|
|
request<void>(`/api/manager/profiles/${encodeURIComponent(name)}/start`, {
|
|
method: "POST",
|
|
}),
|
|
profileStop: (name: string) =>
|
|
request<void>(`/api/manager/profiles/${encodeURIComponent(name)}/stop`, {
|
|
method: "POST",
|
|
}),
|
|
profileRestart: (name: string) =>
|
|
request<void>(`/api/manager/profiles/${encodeURIComponent(name)}/restart`, {
|
|
method: "POST",
|
|
}),
|
|
profileConfig: (name: string) =>
|
|
request<string>(`/api/manager/profiles/${encodeURIComponent(name)}/config`),
|
|
profileLogs: (name: string) =>
|
|
request<string>(`/api/manager/profiles/${encodeURIComponent(name)}/logs`),
|
|
|
|
// M6: cluster nodes + binary cache management.
|
|
clusterNodes: () => request<ClusterNodesResp>("/api/manager/cluster/nodes"),
|
|
clusterCache: () => request<CacheResp>("/api/manager/cluster/cache"),
|
|
pruneCache: (keep: number) =>
|
|
request<CacheResp>("/api/manager/cluster/cache", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ keep }),
|
|
}),
|
|
|
|
// M6 token ring snapshot.
|
|
ring: () => request<RingSnapshot>("/api/manager/cluster/ring"),
|
|
|
|
// M6 cluster lifecycle (创建/加入/退出/移除).
|
|
// clusterCreate: reseed THIS node as a fresh standalone leader (创建集群).
|
|
clusterCreate: () =>
|
|
request<RingSnapshot>("/api/manager/cluster/create", { method: "POST" }),
|
|
// clusterJoinRing: THIS node joins the cluster at peer addr (加入集群).
|
|
// joinKey is the sponsor node's nodeKey — required for admission security.
|
|
clusterJoinRing: (addr: string, joinKey: string) =>
|
|
request<RingSnapshot>("/api/manager/cluster/join-ring", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ addr, joinKey }),
|
|
}),
|
|
// clusterRemoveNode: publish a node-removal command (移除节点 / 退出集群[self]).
|
|
clusterRemoveNode: (id: string) =>
|
|
request<{ task: unknown }>("/api/manager/cluster/node-remove", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ id }),
|
|
}),
|
|
|
|
// M7 auth/accounts/API keys/canvas export-import/worker logs.
|
|
me: () => request<MeResp>("/api/manager/me"),
|
|
listUsers: () => request<{ users: User[] }>("/api/manager/users"),
|
|
createUser: (username: string, password: string, role: 'admin' | 'viewer') =>
|
|
request<User>("/api/manager/users", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ username, password, role }),
|
|
}),
|
|
updateUser: (name: string, patch: { password?: string; role?: 'admin' | 'viewer'; enabled?: boolean }) =>
|
|
request<User>(`/api/manager/users/${encodeURIComponent(name)}`, {
|
|
method: "PUT",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(patch),
|
|
}),
|
|
deleteUser: (name: string) =>
|
|
request<void>(`/api/manager/users/${encodeURIComponent(name)}`, {
|
|
method: "DELETE",
|
|
}),
|
|
listApiKeys: () => request<{ apiKeys: ApiKey[] }>("/api/manager/apikeys"),
|
|
createApiKey: (userId: number, label: string, scope: 'read' | 'write' | 'admin') =>
|
|
request<ApiKeyCreated>("/api/manager/apikeys", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ userId, label, scope }),
|
|
}),
|
|
deleteApiKey: (id: number) =>
|
|
request<void>(`/api/manager/apikeys/${id}`, { method: "DELETE" }),
|
|
|
|
// Canvas export/import (转发表 备份/还原).
|
|
exportCanvas: () => request<CanvasExportEnvelope>("/api/manager/canvas/export"),
|
|
importCanvas: (data: CanvasData | CanvasExportEnvelope) =>
|
|
request<CanvasData>("/api/manager/canvas/import", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(data),
|
|
}),
|
|
|
|
// Worker-log bundle (HTTP fan-out across ring nodes; read-level/auditor).
|
|
exportWorkerLogs: () => request<WorkerLogBundle>("/api/manager/cluster/logs/export"),
|
|
};
|