mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-11-23 01:21:51 +08:00
Merge pull request #107 from RuoJi6/feat/proxy-pool
feat: 全局代理池(健康检查、定时/失效轮换、热切换、脱敏)— Depends on #106
This commit is contained in:
1 parent
f0993424e5
commit
05dd5b40e8
20 files changed
+2646
-68
No files matched your search
@@ -61,6 +61,14 @@ export function EgressIpProvider({ children }: { children: React.ReactNode }) {
|
||||
};
|
||||
}, [probeForProxy]);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (!enabled) return;
|
||||
const timer = window.setInterval(() => {
|
||||
void api.settings().then((settings) => probeForProxy(settings.global_proxy ?? "")).catch(() => undefined);
|
||||
}, 10000);
|
||||
return () => window.clearInterval(timer);
|
||||
}, [enabled, probeForProxy]);
|
||||
|
||||
const setEnabled = React.useCallback(
|
||||
async (nextEnabled: boolean) => {
|
||||
const settings = await api.setSettings({ show_egress_ip: nextEnabled });
|
||||
|
||||
@@ -18,12 +18,13 @@ import { Button } from "@/components/ui/button";
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
import { Field, FieldContent, FieldDescription, FieldLabel } from "@/components/ui/field";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Textarea } from "@/components/ui/textarea";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
||||
import { Switch } from "@/components/ui/switch";
|
||||
import { api } from "@/lib/api";
|
||||
import { CHAT_SEND_MODE_OPTIONS, type ChatSendMode, setChatSendMode, useChatSendMode } from "@/lib/chat-send-mode";
|
||||
import type { EgressReport, LLMProfile, Settings, WorkerPool } from "@/lib/types";
|
||||
import type { EgressReport, LLMProfile, ProxyPoolEntry, Settings, WorkerPool } from "@/lib/types";
|
||||
|
||||
import { useEgressIp } from "../../_components/egress-ip-banner";
|
||||
import { BasicAuthCard } from "./_components/basic-auth-card";
|
||||
@@ -55,6 +56,17 @@ export default function SystemSettingsPage() {
|
||||
const [savingProxy, setSavingProxy] = React.useState(false);
|
||||
const [globalProxyInput, setGlobalProxyInput] = React.useState("");
|
||||
const [savingGlobalProxy, setSavingGlobalProxy] = React.useState(false);
|
||||
const [proxyPool, setProxyPool] = React.useState<ProxyPoolEntry[]>([]);
|
||||
const [proxyPoolActiveId, setProxyPoolActiveId] = React.useState("");
|
||||
const [proxyPoolText, setProxyPoolText] = React.useState("");
|
||||
const [proxyPoolTimed, setProxyPoolTimed] = React.useState(false);
|
||||
const [proxyPoolInterval, setProxyPoolInterval] = React.useState("30");
|
||||
const [proxyPoolFailure, setProxyPoolFailure] = React.useState(false);
|
||||
const [proxyPoolStrategy, setProxyPoolStrategy] = React.useState<"sequential" | "random">("sequential");
|
||||
const [proxyPoolThreshold, setProxyPoolThreshold] = React.useState("3");
|
||||
const [proxyPoolCooldown, setProxyPoolCooldown] = React.useState("60");
|
||||
const [proxyPoolHealthInterval, setProxyPoolHealthInterval] = React.useState("30");
|
||||
const [proxyPoolLogs, setProxyPoolLogs] = React.useState<Array<{ at: string; from: string; to: string; reason: string }>>([]);
|
||||
const [proxyReconnectMax, setProxyReconnectMax] = React.useState("5");
|
||||
const [proxyReconnectInterval, setProxyReconnectInterval] = React.useState("30");
|
||||
const [savingEgressVisibility, setSavingEgressVisibility] = React.useState(false);
|
||||
@@ -113,6 +125,16 @@ export default function SystemSettingsPage() {
|
||||
setProxyInput(s.web_search_proxy ?? "");
|
||||
const savedProxy = s.global_proxy ?? "";
|
||||
setGlobalProxyInput(savedProxy);
|
||||
setProxyPool(s.proxy_pool ?? []);
|
||||
setProxyPoolActiveId(s.proxy_pool_active_id ?? "");
|
||||
setProxyPoolTimed(!!s.proxy_rotate_timed_enabled);
|
||||
setProxyPoolInterval(String(s.proxy_rotate_interval_seconds ?? 30));
|
||||
setProxyPoolFailure(!!s.proxy_rotate_on_failure);
|
||||
setProxyPoolStrategy(s.proxy_rotate_strategy === "random" ? "random" : "sequential");
|
||||
setProxyPoolThreshold(String(s.proxy_health_fail_threshold ?? 3));
|
||||
setProxyPoolCooldown(String(s.proxy_health_cooldown_seconds ?? 60));
|
||||
setProxyPoolHealthInterval(String(s.proxy_health_check_interval_seconds ?? 30));
|
||||
setProxyPoolLogs(s.proxy_pool_switch_log ?? []);
|
||||
setProxyReconnectMax(String(s.proxy_reconnect_max_attempts ?? 5));
|
||||
setProxyReconnectInterval(String(s.proxy_reconnect_interval_seconds ?? 30));
|
||||
setPyInterp(s.python_interpreter ?? "");
|
||||
@@ -205,6 +227,11 @@ export default function SystemSettingsPage() {
|
||||
.then((s) => {
|
||||
apply(s);
|
||||
setSettingsLoaded(true);
|
||||
void api.proxyPool().then((pool) => {
|
||||
setProxyPool(pool.entries);
|
||||
setProxyPoolActiveId(pool.active_id);
|
||||
setProxyPoolLogs(pool.switch_log);
|
||||
}).catch(() => undefined);
|
||||
})
|
||||
.catch(() => {
|
||||
setSettingsLoaded(false);
|
||||
@@ -418,6 +445,10 @@ export default function SystemSettingsPage() {
|
||||
};
|
||||
|
||||
const saveGlobalProxy = () => {
|
||||
if (proxyPool.length > 0) {
|
||||
toast.info("代理池非空时,全局代理输入框仅用于回显,请在代理池中编辑或切换");
|
||||
return;
|
||||
}
|
||||
setSavingGlobalProxy(true);
|
||||
api
|
||||
.setSettings({ global_proxy: globalProxyInput.trim() })
|
||||
@@ -432,6 +463,23 @@ export default function SystemSettingsPage() {
|
||||
.finally(() => setSavingGlobalProxy(false));
|
||||
};
|
||||
|
||||
const importProxyPool = () => {
|
||||
api.importProxyPool({ text: proxyPoolText }).then((r) => { setProxyPool(r.entries); setProxyPoolActiveId(r.active_id); setProxyPoolLogs(r.switch_log); setProxyPoolText(""); if (r.errors.length) toast.error(r.errors.map((e) => `第${e.line}行:${e.error}`).join(";")); else toast.success("代理池已导入"); }).catch((e) => toast.error("导入失败:" + (e as Error).message));
|
||||
};
|
||||
const saveProxyPoolStrategy = () => {
|
||||
const interval = Number(proxyPoolInterval);
|
||||
if (!Number.isFinite(interval) || !Number.isInteger(interval) || interval < 3) {
|
||||
toast.error("定时轮换间隔必须是大于等于 3 秒的整数");
|
||||
return;
|
||||
}
|
||||
api.setSettings({ proxy_rotate_timed_enabled: proxyPoolTimed, proxy_rotate_interval_seconds: Number(proxyPoolInterval), proxy_rotate_on_failure: proxyPoolFailure, proxy_rotate_strategy: proxyPoolStrategy, proxy_health_fail_threshold: Number(proxyPoolThreshold), proxy_health_cooldown_seconds: Number(proxyPoolCooldown), proxy_health_check_interval_seconds: Number(proxyPoolHealthInterval) }).then((s) => { apply(s); toast.success("代理池轮换策略已保存"); }).catch((e) => toast.error("保存失败:" + (e as Error).message));
|
||||
};
|
||||
const testAllProxyPool = () => api.testAllProxyPool().then((r) => { setProxyPool(r.entries); setProxyPoolActiveId(r.active_id); setProxyPoolLogs(r.switch_log); toast.success("已完成全部代理探测"); }).catch((e) => toast.error("探测失败:" + (e as Error).message));
|
||||
const testOneProxyPool = (id: string) => api.testProxyPoolEntry(id).then(() => api.proxyPool()).then((r) => { setProxyPool(r.entries); setProxyPoolActiveId(r.active_id); setProxyPoolLogs(r.switch_log); }).catch((e) => toast.error("探测失败:" + (e as Error).message));
|
||||
const refreshProxyPool = () => api.proxyPool().then((r) => { setProxyPool(r.entries); setProxyPoolActiveId(r.active_id); setProxyPoolLogs(r.switch_log); });
|
||||
const toggleProxyPoolEntry = (entry: ProxyPoolEntry) => api.updateProxyPool(entry.id, { enabled: !entry.enabled }).then(() => refreshProxyPool()).catch((e) => toast.error("更新失败:" + (e as Error).message));
|
||||
const deleteProxyPoolEntry = (id: string) => api.deleteProxyPool(id).then(() => refreshProxyPool()).catch((e) => toast.error("删除失败:" + (e as Error).message));
|
||||
|
||||
const saveProxyReconnect = () => {
|
||||
api.setSettings({ proxy_reconnect_max_attempts: Number(proxyReconnectMax), proxy_reconnect_interval_seconds: Number(proxyReconnectInterval) })
|
||||
.then((s) => { apply(s); toast.success("已保存代理重连策略"); })
|
||||
@@ -446,6 +494,7 @@ export default function SystemSettingsPage() {
|
||||
api
|
||||
.testEgressProxy({
|
||||
global_proxy: globalProxyInput.trim(),
|
||||
resume: true,
|
||||
|
||||
...(egressProbeUrl.trim() ? { probe_url: egressProbeUrl.trim() } : {}),
|
||||
})
|
||||
@@ -879,6 +928,40 @@ export default function SystemSettingsPage() {
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
<div className="mt-3 rounded-md border p-3">
|
||||
<div className="flex flex-wrap items-center justify-between gap-2">
|
||||
<div>
|
||||
<p className="text-sm font-medium">代理池</p>
|
||||
<p className="text-muted-foreground text-xs">当前在用:{proxyPool.find((entry) => entry.id === proxyPoolActiveId)?.name ?? (proxyPoolActiveId ? "已选择代理" : "直连")} · 每行一个代理;可用 URL|名称|备注。账号密码只会以掩码返回。</p>
|
||||
</div>
|
||||
<Button type="button" variant="outline" size="sm" onClick={testAllProxyPool}>测试全部</Button>
|
||||
</div>
|
||||
<Textarea className="mt-2 font-mono text-xs" rows={3} placeholder="http://user:pass@host:8080|香港 1|备用" value={proxyPoolText} onChange={(e) => setProxyPoolText(e.target.value)} />
|
||||
<div className="mt-2 flex flex-wrap gap-2"><Button type="button" size="sm" onClick={importProxyPool} disabled={!proxyPoolText.trim()}>批量导入</Button></div>
|
||||
<div className="mt-3 space-y-2">
|
||||
{proxyPool.length === 0 ? <p className="text-muted-foreground text-xs">暂无代理池条目;单代理仍由上面的全局代理输入框兼容。</p> : proxyPool.map((entry) => (
|
||||
<div key={entry.id} className="flex flex-wrap items-center gap-2 rounded border px-2 py-2 text-xs">
|
||||
<Switch checked={entry.enabled} onCheckedChange={() => toggleProxyPoolEntry(entry)} aria-label="启用代理" />
|
||||
<span className="min-w-0 flex-1 font-mono">{entry.name ? `${entry.name} · ` : ""}{entry.url}{entry.id === proxyPoolActiveId ? " · 当前" : ""}</span>
|
||||
<span className={entry.health === "healthy" ? "text-emerald-600" : entry.health === "disabled" ? "text-muted-foreground" : "text-amber-600"}>{entry.enabled ? entry.health : "disabled"}</span>
|
||||
<span className="text-muted-foreground">{entry.egress_ip ?? "—"} · {entry.latency_ms ?? "—"}ms · 失败 {entry.consecutive_failures}{entry.last_error ? ` · ${entry.last_error}` : ""}</span>
|
||||
<Button type="button" variant="ghost" size="sm" onClick={() => testOneProxyPool(entry.id)}>测试</Button>
|
||||
<Button type="button" variant="ghost" size="sm" onClick={() => deleteProxyPoolEntry(entry.id)}>删除</Button>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
<div className="mt-3 grid grid-cols-1 gap-2 sm:grid-cols-3">
|
||||
<div className="flex items-center justify-between gap-2 rounded border px-2 py-2"><Label htmlFor="proxy-pool-timed">定时轮换</Label><Switch id="proxy-pool-timed" checked={proxyPoolTimed} onCheckedChange={setProxyPoolTimed} /></div>
|
||||
<div><Label htmlFor="proxy-pool-interval">间隔(秒,≥3)</Label><Input id="proxy-pool-interval" type="number" min={3} value={proxyPoolInterval} onChange={(e) => setProxyPoolInterval(e.target.value)} /></div>
|
||||
<div className="flex items-center justify-between gap-2 rounded border px-2 py-2"><Label htmlFor="proxy-pool-failure">失效轮换</Label><Switch id="proxy-pool-failure" checked={proxyPoolFailure} onCheckedChange={setProxyPoolFailure} /></div>
|
||||
<div><Label>策略</Label><Select value={proxyPoolStrategy} onValueChange={(v) => setProxyPoolStrategy(v as "sequential" | "random")}><SelectTrigger><SelectValue /></SelectTrigger><SelectContent><SelectItem value="sequential">顺序</SelectItem><SelectItem value="random">随机</SelectItem></SelectContent></Select></div>
|
||||
<div><Label htmlFor="proxy-pool-threshold">失败阈值</Label><Input id="proxy-pool-threshold" type="number" min={1} value={proxyPoolThreshold} onChange={(e) => setProxyPoolThreshold(e.target.value)} /></div>
|
||||
<div><Label htmlFor="proxy-pool-cooldown">冷却(秒)</Label><Input id="proxy-pool-cooldown" type="number" min={1} value={proxyPoolCooldown} onChange={(e) => setProxyPoolCooldown(e.target.value)} /></div>
|
||||
<div><Label htmlFor="proxy-pool-health-interval">健康检查(秒)</Label><Input id="proxy-pool-health-interval" type="number" min={1} value={proxyPoolHealthInterval} onChange={(e) => setProxyPoolHealthInterval(e.target.value)} /></div>
|
||||
<Button type="button" variant="outline" onClick={saveProxyPoolStrategy}>保存轮换策略</Button>
|
||||
</div>
|
||||
{proxyPoolLogs.length > 0 && <div className="mt-3 text-xs"><p className="font-medium">最近切换</p>{proxyPoolLogs.slice(-5).reverse().map((log) => <p key={`${log.at}-${log.to}`} className="text-muted-foreground">{new Date(log.at).toLocaleTimeString()} · {log.from || "直连"} → {log.to || "直连"} · {log.reason}</p>)}</div>}
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
|
||||
+10
-1
@@ -34,6 +34,8 @@ import type {
|
||||
DeleteTaskResult,
|
||||
Edge,
|
||||
EgressReport,
|
||||
ProxyPoolEntry,
|
||||
ProxySwitchLog,
|
||||
EvidenceBodyPreview,
|
||||
ExplorationNodePage,
|
||||
ExplorationNodeQuery,
|
||||
@@ -990,8 +992,15 @@ export const api = {
|
||||
}) => post<{ ok: boolean; error?: string; count?: number; backend?: string }>(`/settings/web-search/test`, patch),
|
||||
|
||||
// global_proxy 传空 = 测直连;probe_url 可覆盖默认探测地址(隔离网用)。
|
||||
testEgressProxy: (patch: { global_proxy?: string; probe_url?: string }) =>
|
||||
testEgressProxy: (patch: { global_proxy?: string; probe_url?: string; resume?: boolean }) =>
|
||||
post<EgressReport>(`/settings/proxy/test`, patch),
|
||||
proxyPool: () => get<{ entries: ProxyPoolEntry[]; active_id: string; active_proxy: string; switch_log: ProxySwitchLog[] }>(`/settings/proxy-pool`),
|
||||
importProxyPool: (payload: Record<string, unknown>) => post<{ entries: ProxyPoolEntry[]; active_id: string; switch_log: ProxySwitchLog[]; errors: Array<{ line: number; input: string; error: string }> }>(`/settings/proxy-pool/import`, payload),
|
||||
updateProxyPool: (id: string, payload: Partial<Pick<ProxyPoolEntry, "url" | "name" | "remark" | "enabled">>) => put<{ entry: ProxyPoolEntry }>(`/settings/proxy-pool/${encodeURIComponent(id)}`, payload),
|
||||
deleteProxyPool: (id: string) => del<{ ok: boolean }>(`/settings/proxy-pool/${encodeURIComponent(id)}`),
|
||||
testProxyPoolEntry: (id: string) => post<{ ok: boolean; result?: EgressReport["checks"][number]["result"] }>(`/settings/proxy-pool/${encodeURIComponent(id)}/test`, {}),
|
||||
testAllProxyPool: () => post<{ entries: ProxyPoolEntry[]; active_id: string; switch_log: ProxySwitchLog[] }>(`/settings/proxy-pool/test-all`, {}),
|
||||
activateProxyPool: (id: string) => post<{ ok: boolean; active_id: string; active_proxy: string }>(`/settings/proxy-pool/${encodeURIComponent(id)}/activate`, {}),
|
||||
testTaskProxy: (taskID: string) => post<{ ok: boolean; error?: string }>(`/tasks/${taskID}/proxy/test`, {}),
|
||||
|
||||
// ---- 漏洞 IM 推送 ----
|
||||
|
||||
@@ -3350,6 +3350,15 @@ export const settings: Settings = {
|
||||
tavily_key_set: true,
|
||||
web_search_proxy: "",
|
||||
global_proxy: "",
|
||||
proxy_pool: [],
|
||||
proxy_pool_active_id: "",
|
||||
proxy_rotate_timed_enabled: false,
|
||||
proxy_rotate_interval_seconds: 30,
|
||||
proxy_rotate_on_failure: false,
|
||||
proxy_rotate_strategy: "sequential",
|
||||
proxy_health_fail_threshold: 3,
|
||||
proxy_health_cooldown_seconds: 60,
|
||||
proxy_health_check_interval_seconds: 30,
|
||||
show_egress_ip: false,
|
||||
python_interpreter: "/usr/bin/python3",
|
||||
workers: 3,
|
||||
|
||||
@@ -2406,6 +2406,12 @@ function route(m: string, path: string, seg: string[], q: URLSearchParams, b: Re
|
||||
],
|
||||
warnings: [],
|
||||
};
|
||||
if (path === "/settings/proxy-pool" && m === "GET") return { entries: D.settings.proxy_pool ?? [], active_id: D.settings.proxy_pool_active_id ?? "", active_proxy: D.settings.global_proxy ?? "", switch_log: [] };
|
||||
if (path === "/settings/proxy-pool/import" && m === "POST") return { entries: D.settings.proxy_pool ?? [], active_id: D.settings.proxy_pool_active_id ?? "", switch_log: [], errors: [] };
|
||||
if (path === "/settings/proxy-pool/test-all" && m === "POST") return { entries: D.settings.proxy_pool ?? [], active_id: D.settings.proxy_pool_active_id ?? "", switch_log: [] };
|
||||
if (path.startsWith("/settings/proxy-pool/") && m === "PUT") return { entry: { ...(D.settings.proxy_pool ?? []).find((x) => x.id === path.split("/").at(-1)), ...b } };
|
||||
if (path.startsWith("/settings/proxy-pool/") && m === "DELETE") return { ok: true };
|
||||
if (path.startsWith("/settings/proxy-pool/") && m === "POST") return { ok: true, active_id: path.split("/").at(-1), active_proxy: "" };
|
||||
if (path === "/settings/python/detect") return { python_interpreter: "/usr/bin/python3" };
|
||||
if (path === "/chat")
|
||||
return { reply: "(demo)我已把该建议注入为一条高优意图,work agent 会尽快执行。", mode: "hint" };
|
||||
|
||||
@@ -1004,6 +1004,16 @@ export interface Settings {
|
||||
// 全局出口代理(http/https/socks5,可带 user:pass),所有目标流量走它。开启流量捕获时作为
|
||||
// MITM 上游;关闭捕获时直接注入 agent 的 bash/WebFetch。空=直连。
|
||||
global_proxy?: string;
|
||||
proxy_pool?: ProxyPoolEntry[];
|
||||
proxy_pool_active_id?: string;
|
||||
proxy_pool_switch_log?: ProxySwitchLog[];
|
||||
proxy_rotate_timed_enabled?: boolean;
|
||||
proxy_rotate_interval_seconds?: number;
|
||||
proxy_rotate_on_failure?: boolean;
|
||||
proxy_rotate_strategy?: "sequential" | "random";
|
||||
proxy_health_fail_threshold?: number;
|
||||
proxy_health_cooldown_seconds?: number;
|
||||
proxy_health_check_interval_seconds?: number;
|
||||
proxy_reconnect_max_attempts?: number;
|
||||
proxy_reconnect_interval_seconds?: number;
|
||||
show_egress_ip?: boolean; // 在顶栏显示直连 / 全局代理出口 IP;默认关闭
|
||||
@@ -1031,6 +1041,22 @@ export interface Settings {
|
||||
notify_digest_interval_min?: number; // 汇总模式周期(分钟),默认 30
|
||||
}
|
||||
|
||||
export interface ProxyPoolEntry {
|
||||
id: string;
|
||||
url: string;
|
||||
name?: string;
|
||||
remark?: string;
|
||||
enabled: boolean;
|
||||
egress_ip?: string;
|
||||
latency_ms?: number;
|
||||
last_error?: string;
|
||||
last_checked_at?: string;
|
||||
consecutive_failures: number;
|
||||
health: "healthy" | "unhealthy-cooling" | "disabled" | string;
|
||||
}
|
||||
|
||||
export interface ProxySwitchLog { at: string; from: string; to: string; reason: string }
|
||||
|
||||
export interface WorkerPool {
|
||||
profile_id: string;
|
||||
workers: number;
|
||||
|
||||
Reference in new issue
Block a user