Compare commits

...
20 changed files with 2646 additions and 68 deletions

No files matched your search

+9 -3
View File
@@ -29,6 +29,7 @@ type ChatAgent struct {
tx *transcript.Store
window int
proxyAddr string
proxyAddrFn func() string
proxyCACert string
webSearch WebSearchOpts
guard *guard.Guard // optional; nil disables intercept hooks for chat
@@ -65,7 +66,8 @@ func (c *ChatAgent) maxTokens() int {
// SetProxy points the chat agent's WebFetch/Bash at the recording proxy plus the
// CA cert it trusts (empty addr = direct). Kept for parity with the other agents.
func (c *ChatAgent) SetProxy(addr, caCert string) { c.proxyAddr, c.proxyCACert = addr, caCert }
func (c *ChatAgent) SetProxy(addr, caCert string) { c.proxyAddr, c.proxyCACert = addr, caCert }
func (c *ChatAgent) SetProxyProvider(fn func() string) { c.proxyAddrFn = fn }
// SetWebSearch selects the web_search backend for the chat agent (off by default).
func (c *ChatAgent) SetWebSearch(o WebSearchOpts) { c.webSearch = o }
@@ -124,6 +126,10 @@ func (c *ChatAgent) Chat(ctx context.Context, agentKey, sessionID, message strin
defer cleanup()
system, boundary := deferredSystem(chatSystem(agentKey, c.workDir, sessionWorkDir), def)
proxyAddr := c.proxyAddr
if c.proxyAddrFn != nil {
proxyAddr = c.proxyAddrFn()
}
opts := agentcore.Options{
Provider: c.prov,
SystemPrompt: system,
@@ -133,7 +139,7 @@ func (c *ChatAgent) Chat(ctx context.Context, agentKey, sessionID, message strin
UnlockSet: def.Unlock,
PermissionMode: permission.ModeBypass,
EnableWebFetch: true, // 走记录代理留痕;载入代理 CA 验证 MITM 重签的 HTTPS 证书
WebFetchProxy: c.proxyAddr,
WebFetchProxy: proxyAddr,
WebFetchCACert: c.proxyCACert,
// 联网搜索(可选)。ddgs 无需 key;brave-free 需 BraveKey;tavily 需 TavilyKey。
// WebSearchProxy 是独立出口代理(http/https/socks5),与记录流量的 MITM 代理无关;空则直连。
@@ -145,7 +151,7 @@ func (c *ChatAgent) Chat(ctx context.Context, agentKey, sessionID, message strin
DeepSeekSearchAPIKey: ws.DeepSeekAPIKey,
DeepSeekSearchModel: ws.DeepSeekModel,
WebSearchProxy: ws.Proxy,
BashEnv: proxyEnv(c.proxyAddr, c.proxyCACert), // Bash 子命令默认走代理+信任 CA
BashEnv: proxyEnv(proxyAddr, c.proxyCACert), // Bash 子命令默认走代理+信任 CA
WorkingDir: sessionWorkDir,
MaxTurns: maxTurns,
MaxDuration: maxDuration,
+15 -9
View File
@@ -21,11 +21,12 @@ type MainAgent struct {
findingRecorder FindingRecorder
prov llm.Provider
model string
tx *transcript.Store // raw LLM conversation persistence (nil = off)
window int // context window in tokens (for compaction)
windowFn func() int // optional dynamic task-chain minimum
maxTurns int // max agent turns per run (0 = unlimited)
proxyAddr string // recording proxy for WebFetch (empty = direct)
tx *transcript.Store // raw LLM conversation persistence (nil = off)
window int // context window in tokens (for compaction)
windowFn func() int // optional dynamic task-chain minimum
maxTurns int // max agent turns per run (0 = unlimited)
proxyAddr string // recording proxy for WebFetch (empty = direct)
proxyAddrFn func() string
proxyCACert string // recording proxy's CA cert path (HTTPS verify)
webSearch WebSearchOpts // web_search tool backend selection (off by default)
workDir string // shared work dir (surfaced in prompt as artifact-output target)
@@ -72,7 +73,8 @@ func (m *MainAgent) compactionWindow() int {
// SetProxy points the main agent's WebFetch at the recording proxy plus the CA
// cert it trusts to verify HTTPS through it (empty addr = direct).
func (m *MainAgent) SetProxy(addr, caCert string) { m.proxyAddr, m.proxyCACert = addr, caCert }
func (m *MainAgent) SetProxy(addr, caCert string) { m.proxyAddr, m.proxyCACert = addr, caCert }
func (m *MainAgent) SetProxyProvider(fn func() string) { m.proxyAddrFn = fn }
// SetWebSearch selects the web_search backend for the main agent (off by default).
func (m *MainAgent) SetWebSearch(o WebSearchOpts) { m.webSearch = o }
@@ -132,6 +134,10 @@ func (m *MainAgent) Chat(ctx context.Context, taskID int64, mainSeg int, as *db.
mainDir := ensureRunDir(m.workDir, taskID, 0)
ctx = intercept.WithReviewWorkingDirectory(ctx, mainDir)
system, boundary := deferredSystem(mainAgentSystem(goal, m.workDir, mainDir), def)
proxyAddr := m.proxyAddr
if m.proxyAddrFn != nil {
proxyAddr = m.proxyAddrFn()
}
opts := agentcore.Options{
Provider: m.prov,
SystemPrompt: system,
@@ -141,7 +147,7 @@ func (m *MainAgent) Chat(ctx context.Context, taskID int64, mainSeg int, as *db.
UnlockSet: def.Unlock,
PermissionMode: permission.ModeBypass,
EnableWebFetch: true, // 走记录代理留痕;载入代理 CA 验证 MITM 重签的 HTTPS 证书
WebFetchProxy: m.proxyAddr,
WebFetchProxy: proxyAddr,
WebFetchCACert: m.proxyCACert,
// 联网搜索(可选)。ddgs 无需 key;brave-free 需 BraveKey;tavily 需 TavilyKey。
// WebSearchProxy 是独立出口代理(http/https/socks5),与记录流量的 MITM 代理无关;空则直连。
@@ -153,8 +159,8 @@ func (m *MainAgent) Chat(ctx context.Context, taskID int64, mainSeg int, as *db.
DeepSeekSearchAPIKey: m.webSearch.DeepSeekAPIKey,
DeepSeekSearchModel: m.webSearch.DeepSeekModel,
WebSearchProxy: m.webSearch.Proxy,
BashEnv: proxyEnv(m.proxyAddr, m.proxyCACert), // Bash 子命令默认走代理+信任 CA
WorkingDir: mainDir, // 本任务工作目录 <workDir>/tasks/<taskID>
BashEnv: proxyEnv(proxyAddr, m.proxyCACert), // Bash 子命令默认走代理+信任 CA
WorkingDir: mainDir, // 本任务工作目录 <workDir>/tasks/<taskID>
ToolOutputDir: cmdOutDir(mainDir),
MaxTurns: m.maxTurns, // 0 = unlimited (configurable in agent management)
Compaction: compactionConfig(m.compactionWindow()), // long chats stay within the window
+18 -12
View File
@@ -31,14 +31,15 @@ type Planner struct {
killWork func(intentID int64) error // engine callback to terminate a running work (nil = off)
steerWork func(intentID int64, msg string) error // engine callback to steer a running work mid-run (nil = off)
proxyAddr string // recording proxy for WebFetch (empty = direct)
proxyCACert string // recording proxy's CA cert path (HTTPS verify)
webSearch WebSearchOpts // web_search tool backend selection (off by default)
workDir string // shared work dir (surfaced in prompt as artifact-output target)
injectConstraints func() bool // resolver: inject task operation constraints into system prompt? (nil = yes)
nonStreamingFn func() bool // resolver: use non-streaming (Complete) path? (nil = streaming)
noaEnabledFn func() bool // resolver: use experimental noa compaction? (nil = off)
maxTokensFn func() int // resolver: per-reply output cap (nil/0 = send no cap)
compactor *Compactor // cold-node compaction (§7); nil = disabled
proxyAddrFn func() string
proxyCACert string // recording proxy's CA cert path (HTTPS verify)
webSearch WebSearchOpts // web_search tool backend selection (off by default)
workDir string // shared work dir (surfaced in prompt as artifact-output target)
injectConstraints func() bool // resolver: inject task operation constraints into system prompt? (nil = yes)
nonStreamingFn func() bool // resolver: use non-streaming (Complete) path? (nil = streaming)
noaEnabledFn func() bool // resolver: use experimental noa compaction? (nil = off)
maxTokensFn func() int // resolver: per-reply output cap (nil/0 = send no cap)
compactor *Compactor // cold-node compaction (§7); nil = disabled
// todos keeps ONE plan-scratchpad per task (keyed by exploration id) so the
// planner's multi-step plan survives across wake-ups — each Plan() is a fresh
@@ -90,7 +91,8 @@ func (p *Planner) compactionWindow() int {
// SetProxy points the planner's WebFetch at the recording proxy plus the CA cert
// it trusts to verify HTTPS through it (empty addr = direct).
func (p *Planner) SetProxy(addr, caCert string) { p.proxyAddr, p.proxyCACert = addr, caCert }
func (p *Planner) SetProxy(addr, caCert string) { p.proxyAddr, p.proxyCACert = addr, caCert }
func (p *Planner) SetProxyProvider(fn func() string) { p.proxyAddrFn = fn }
// SetWebSearch selects the web_search backend for the planner (off by default).
func (p *Planner) SetWebSearch(o WebSearchOpts) { p.webSearch = o }
@@ -402,6 +404,10 @@ func (p *Planner) Plan(ctx context.Context, taskID int64, as *db.AssetStore, ts
if tc.DeadlineUnix > 0 {
settle = wrapupSettlementForTask("planner", nil, clamped)
}
proxyAddr := p.proxyAddr
if p.proxyAddrFn != nil {
proxyAddr = p.proxyAddrFn()
}
opts := agentcore.Options{
Provider: p.prov,
SystemPrompt: system,
@@ -411,7 +417,7 @@ func (p *Planner) Plan(ctx context.Context, taskID int64, as *db.AssetStore, ts
UnlockSet: def.Unlock,
PermissionMode: permission.ModeBypass,
EnableWebFetch: true, // 走记录代理留痕;载入代理 CA 验证 MITM 重签的 HTTPS 证书
WebFetchProxy: p.proxyAddr,
WebFetchProxy: proxyAddr,
WebFetchCACert: p.proxyCACert,
// 联网搜索(可选)。ddgs 无需 key;brave-free 需 BraveKey;tavily 需 TavilyKey。
// WebSearchProxy 是独立出口代理(http/https/socks5),与记录流量的 MITM 代理无关;空则直连。
@@ -423,8 +429,8 @@ func (p *Planner) Plan(ctx context.Context, taskID int64, as *db.AssetStore, ts
DeepSeekSearchAPIKey: p.webSearch.DeepSeekAPIKey,
DeepSeekSearchModel: p.webSearch.DeepSeekModel,
WebSearchProxy: p.webSearch.Proxy,
BashEnv: proxyEnv(p.proxyAddr, p.proxyCACert), // Bash 子命令默认走代理+信任 CA
WorkingDir: taskDir, // 本任务工作目录 <workDir>/tasks/<taskID>
BashEnv: proxyEnv(proxyAddr, p.proxyCACert), // Bash 子命令默认走代理+信任 CA
WorkingDir: taskDir, // 本任务工作目录 <workDir>/tasks/<taskID>
ToolOutputDir: cmdOutDir(taskDir),
MaxTurns: p.maxTurns, // 0 = unlimited (configurable in agent management)
MaxDuration: maxDur, // 0=不限;有 deadline 时=距 deadline 剩余
+10 -3
View File
@@ -57,6 +57,7 @@ type Worker struct {
model string
workDir string
proxyAddr string
proxyAddrFn func() string
proxyCACert string // recording proxy's CA cert path (for WebFetch HTTPS verify)
webSearch WebSearchOpts // web_search tool backend selection (off by default)
tx *transcript.Store // raw LLM conversation persistence (nil = off)
@@ -188,6 +189,8 @@ func (w *Worker) compactionWindow() int {
// that MITM proxy. Empty addr disables the hint.
func (w *Worker) SetProxy(addr, caCert string) { w.proxyAddr, w.proxyCACert = addr, caCert }
func (w *Worker) SetProxyProvider(fn func() string) { w.proxyAddrFn = fn }
// SetWebSearch selects the web_search backend for this worker (off by default).
func (w *Worker) SetWebSearch(o WebSearchOpts) { w.webSearch = o }
@@ -393,7 +396,11 @@ func (w *Worker) execute(ctx context.Context, name string, taskID int64, as *db.
// inherit a parent run's background into the action reviewer.
ctx = intercept.WithReviewContext(ctx, runDir, intercept.ReviewBackground{})
overview := renderWorkerGraphOverview(tsx.graphOverviewData())
sysBody := workerSystem(w.proxyAddr, w.proxyCACert, w.workDir, runDir)
proxyAddr := w.proxyAddr
if w.proxyAddrFn != nil {
proxyAddr = w.proxyAddrFn()
}
sysBody := workerSystem(proxyAddr, w.proxyCACert, w.workDir, runDir)
if w.wantConstraints() {
sysBody += constraintBlock(ts) // 操作约束(若有)注入系统提示,worker 执行时严格遵守
}
@@ -437,7 +444,7 @@ func (w *Worker) execute(ctx context.Context, name string, taskID int64, as *db.
// WebFetch 走记录代理,其 HTTP 与 curl 一样被留痕;载入代理 CA 让经 MITM
// 重签的 HTTPS 证书能【正常校验通过】(而非关掉校验)。proxy 空则直连。
EnableWebFetch: true,
WebFetchProxy: w.proxyAddr,
WebFetchProxy: proxyAddr,
WebFetchCACert: w.proxyCACert,
// 联网搜索(可选)。ddgs 无需 key;brave-free 需 BraveKey;tavily 需 TavilyKey。
// WebSearchProxy 是独立的出口代理(http/https/socks5),与记录流量的 MITM 代理无关;空则直连。
@@ -450,7 +457,7 @@ func (w *Worker) execute(ctx context.Context, name string, taskID int64, as *db.
DeepSeekSearchModel: w.webSearch.DeepSeekModel,
WebSearchProxy: w.webSearch.Proxy,
// Bash 子命令的 HTTP 默认走记录代理 + 信任其 CA(工具无需 -x/-k)。
BashEnv: proxyEnv(w.proxyAddr, w.proxyCACert),
BashEnv: proxyEnv(proxyAddr, w.proxyCACert),
WorkingDir: runDir,
MaxTurns: w.maxTurns, // 0 = unlimited (configurable in agent management)
// 墙钟预算,轮边界判,不打断半路;0 = 不限。有任务级 deadline 时夹逼到 min(自身预算,
+1
View File
@@ -113,6 +113,7 @@ func run() int {
}
log.Printf("[config] skill 目录: %s", skillDir)
srv := server.New(ctx, mgr, skillDir, *dataDir, config.BaseDir())
defer srv.Close()
httpSrv := &http.Server{
Addr: *addr,
Handler: srv.Handler(),
+3 -3
View File
@@ -368,7 +368,7 @@ func (s *Server) runScriptTool(ctx context.Context, key string, execRaw json.Raw
}
body, err := execPython(ctx, interp, key, spec.Code, params, workDir, sessionEnv, timeoutOr(spec.TimeoutMs, 120000))
if err != nil {
return actool.Errorf(err.Error()), nil
return actool.Errorf(maskSecrets(err.Error())), nil
}
return actool.Text(actool.Capture(tc, body)), nil
}
@@ -436,7 +436,7 @@ func (s *Server) runHTTPTool(ctx context.Context, execRaw json.RawMessage, param
defer cancel()
req, err := http.NewRequestWithContext(runCtx, method, rawURL, bodyReader)
if err != nil {
return actool.Errorf(err.Error()), nil
return actool.Errorf(maskSecrets(err.Error())), nil
}
for k, v := range spec.Headers {
req.Header.Set(k, renderTemplate(v, params, identity))
@@ -450,7 +450,7 @@ func (s *Server) runHTTPTool(ctx context.Context, execRaw json.RawMessage, param
if spec.UseRecordingProxy || strings.TrimSpace(spec.Proxy) == strings.TrimSpace(s.m.GlobalProxy()) {
s.handleProxyToolFailure(runCtx, err)
}
return actool.Errorf("请求失败: " + err.Error()), nil
return actool.Errorf("请求失败: " + maskSecrets(err.Error())), nil
}
defer resp.Body.Close()
respBody, _ := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
+15 -9
View File
@@ -341,6 +341,7 @@ func (s *Server) testEgressProxy(w http.ResponseWriter, r *http.Request) {
var req struct {
GlobalProxy string `json:"global_proxy"`
ProbeURL string `json:"probe_url"` // 可选:覆盖默认探测地址
Resume bool `json:"resume"` // manual settings probe may release paused_by_proxy tasks
}
// 请求体只有一个地址,给个上限,免得被塞大 payload。
r.Body = http.MaxBytesReader(w, r.Body, 64<<10)
@@ -366,16 +367,21 @@ func (s *Server) testEgressProxy(w http.ResponseWriter, r *http.Request) {
agentCA := s.m.ProxyCACert() // 捕获开时是 MITM 的 CA 路径;关时为空
savedProxy := s.m.GlobalProxy() // 已保存的 global_proxy
// 表单值 ≠ 已保存值时,② 仍按表单值测,但 agent 走的是已保存值——必须提示。
// 归一化再比:尾斜杠不算"未保存"。
pending := normalizeProxy(req.GlobalProxy) != normalizeProxy(savedProxy)
// 整体上限 40s:单次 Timeout 只兜一次,不兜总量;前端 fetch 没有超时。
ctx, cancel := context.WithTimeout(r.Context(), 40*time.Second)
defer cancel()
proxyGuards := s.snapshotProxyReconnectGuards(false)
var proxyGuards map[string]proxyReconnectGuard
if req.Resume {
proxyGuards = s.snapshotProxyReconnectGuardsWithExhausted(false, true)
}
configuredProxy := strings.TrimSpace(req.GlobalProxy)
if strings.Contains(configuredProxy, "***") || strings.Contains(configuredProxy, "******") {
configuredProxy = s.m.GlobalProxy()
}
// 表单值 ≠ 已保存值时,② 仍按表单值测,但 agent 走的是已保存值——必须提示。
// 归一化再比:尾斜杠不算"未保存"。
pending := normalizeProxy(configuredProxy) != normalizeProxy(savedProxy)
rep := egressReport{
Capture: capture,
Configured: maskSecrets(configuredProxy),
@@ -418,10 +424,10 @@ func (s *Server) testEgressProxy(w http.ResponseWriter, r *http.Request) {
rep.OK = judgeEgress(rep)
rep.Warnings = egressWarnings(rep, pending, savedProxy)
if rep.OK && !pending {
// The settings page uses this endpoint for its manual "测试代理" action.
// A successful probe is the same signal as an automatic reconnect success,
// but it must never consume an automatic attempt.
if req.Resume && rep.OK && !pending {
// The navbar uses the same probe endpoint for display-only polling but
// omits resume. Only an explicit settings-page test can release the
// guarded paused_by_proxy tasks.
s.proxyReconnectSuccessWithGuards(proxyGuards, nil)
}
writeJSON(w, 200, rep)
+294
View File
@@ -0,0 +1,294 @@
package server
import (
"bufio"
"context"
"crypto/tls"
"fmt"
"io"
"net"
"net/http"
"net/url"
"strings"
"sync"
"time"
"golang.org/x/net/proxy"
)
// dynamicForwardProxy is a credential-free, stable local proxy. Each request
// resolves the current upstream, so pool switches do not rebuild agents or MCP.
type dynamicForwardProxy struct {
ln net.Listener
addr string
current func() string
direct func() bool
server *http.Server
once sync.Once
mu sync.Mutex
conns map[net.Conn]struct{}
ctx context.Context
cancel context.CancelFunc
}
func newDynamicForwardProxy(current func() string) (*dynamicForwardProxy, error) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return nil, err
}
ctx, cancel := context.WithCancel(context.Background())
p := &dynamicForwardProxy{ln: ln, addr: "http://" + ln.Addr().String(), current: current, direct: func() bool { return true }, conns: map[net.Conn]struct{}{}, ctx: ctx, cancel: cancel}
p.server = &http.Server{Handler: http.HandlerFunc(p.serveHTTP), BaseContext: func(net.Listener) context.Context { return ctx }}
go p.server.Serve(ln)
return p, nil
}
func (p *dynamicForwardProxy) Addr() string {
if p == nil {
return ""
}
return p.addr
}
func (p *dynamicForwardProxy) Close() error {
if p == nil {
return nil
}
p.once.Do(func() {
p.cancel()
p.mu.Lock()
for c := range p.conns {
_ = c.Close()
}
p.conns = nil
p.mu.Unlock()
_ = p.server.Close()
})
return nil
}
func (p *dynamicForwardProxy) track(c net.Conn) func() {
if c == nil {
return func() {}
}
p.mu.Lock()
if p.conns != nil {
p.conns[c] = struct{}{}
}
p.mu.Unlock()
return func() { p.mu.Lock(); delete(p.conns, c); p.mu.Unlock() }
}
func (p *dynamicForwardProxy) serveHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodConnect {
p.serveConnect(w, r)
return
}
up := strings.TrimSpace(p.current())
if up == "" && p.direct != nil && !p.direct() {
http.Error(w, "proxy unavailable", 502)
return
}
tr := &http.Transport{Proxy: nil}
if up != "" {
u, err := url.Parse(up)
if err != nil {
http.Error(w, "proxy unavailable", 502)
return
}
if strings.HasPrefix(strings.ToLower(u.Scheme), "socks5") {
d, err := socksDialer(u)
if err != nil {
http.Error(w, "proxy unavailable", 502)
return
}
tr.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
return dialProxyContext(ctx, d, network, addr)
}
} else {
tr.Proxy = http.ProxyURL(u)
}
}
tr.TLSClientConfig = &tls.Config{MinVersion: tls.VersionTLS12}
defer tr.CloseIdleConnections()
rr := r.Clone(r.Context())
if rr.URL.IsAbs() == false {
rr.URL.Scheme = "http"
rr.URL.Host = r.Host
}
rr.RequestURI = ""
for k := range rr.Header {
if strings.EqualFold(k, "Proxy-Authorization") {
rr.Header.Del(k)
}
}
resp, err := tr.RoundTrip(rr)
if err != nil {
http.Error(w, "proxy unavailable", 502)
return
}
defer resp.Body.Close()
for k, vv := range resp.Header {
for _, v := range vv {
w.Header().Add(k, v)
}
}
w.WriteHeader(resp.StatusCode)
_, _ = io.Copy(w, resp.Body)
}
func (p *dynamicForwardProxy) serveConnect(w http.ResponseWriter, r *http.Request) {
up := strings.TrimSpace(p.current())
if up == "" && p.direct != nil && !p.direct() {
http.Error(w, "proxy unavailable", 502)
return
}
target := r.Host
var conn net.Conn
var err error
if up == "" {
conn, err = (&net.Dialer{Timeout: 10 * time.Second}).DialContext(r.Context(), "tcp", target)
} else {
u, e := url.Parse(up)
if e != nil {
err = e
} else if strings.HasPrefix(strings.ToLower(u.Scheme), "socks5") {
d, e := socksDialer(u)
if e != nil {
err = e
} else {
conn, err = dialProxyContext(r.Context(), d, "tcp", target)
}
} else {
conn, err = dialHTTPConnect(r.Context(), u, target)
}
}
if err != nil {
http.Error(w, "proxy unavailable", 502)
return
}
h, ok := w.(http.Hijacker)
if !ok {
http.Error(w, "proxy unavailable", 500)
return
}
client, rw, err := h.Hijack()
if err != nil {
return
}
removeClient := p.track(client)
defer removeClient()
removeUpstream := p.track(conn)
defer removeUpstream()
defer client.Close()
defer conn.Close()
_, _ = rw.WriteString("HTTP/1.1 200 Connection Established\r\n\r\n")
_ = rw.Flush()
clientReader := io.Reader(client)
if rw.Reader != nil && rw.Reader.Buffered() > 0 {
clientReader = io.MultiReader(rw.Reader, client)
}
done := make(chan struct{}, 2)
go func() { _, _ = io.Copy(conn, clientReader); _ = conn.Close(); done <- struct{}{} }()
go func() { _, _ = io.Copy(client, conn); _ = client.Close(); done <- struct{}{} }()
<-done
}
func socksDialer(u *url.URL) (proxy.Dialer, error) {
var auth *proxy.Auth
if u.User != nil {
pw, _ := u.User.Password()
auth = &proxy.Auth{User: u.User.Username(), Password: pw}
}
return proxy.SOCKS5("tcp", u.Host, auth, &net.Dialer{Timeout: 10 * time.Second})
}
func dialProxyContext(ctx context.Context, d proxy.Dialer, network, addr string) (net.Conn, error) {
if cd, ok := d.(proxy.ContextDialer); ok {
return cd.DialContext(ctx, network, addr)
}
type result struct {
c net.Conn
err error
}
ch := make(chan result, 1)
go func() { c, err := d.Dial(network, addr); ch <- result{c, err} }()
select {
case r := <-ch:
return r.c, r.err
case <-ctx.Done():
go func() {
r := <-ch
if r.c != nil {
_ = r.c.Close()
}
}()
return nil, ctx.Err()
}
}
type bufferedConn struct {
net.Conn
reader *bufio.Reader
}
func (c *bufferedConn) Read(p []byte) (int, error) { return c.reader.Read(p) }
func dialHTTPConnect(ctx context.Context, u *url.URL, target string) (net.Conn, error) {
d := &net.Dialer{Timeout: 10 * time.Second}
addr := u.Host
if !strings.Contains(addr, ":") {
addr += ":80"
}
conn, err := d.DialContext(ctx, "tcp", addr)
if err != nil {
return nil, err
}
if strings.EqualFold(u.Scheme, "https") {
conn = tls.Client(conn, &tls.Config{ServerName: u.Hostname(), MinVersion: tls.VersionTLS12})
if err := conn.(*tls.Conn).HandshakeContext(ctx); err != nil {
_ = conn.Close()
return nil, err
}
}
br := bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn))
fmt.Fprintf(br, "CONNECT %s HTTP/1.1\r\nHost: %s\r\n", target, target)
if u.User != nil {
req := &http.Request{Header: http.Header{}}
req.SetBasicAuth(u.User.Username(), func() string { p, _ := u.User.Password(); return p }())
fmt.Fprintf(br, "Proxy-Authorization: %s\r\n", req.Header.Get("Authorization"))
}
fmt.Fprint(br, "\r\n")
if err := br.Flush(); err != nil {
_ = conn.Close()
return nil, err
}
readCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
type responseResult struct {
resp *http.Response
err error
}
readDone := make(chan responseResult, 1)
go func() {
resp, err := http.ReadResponse(br.Reader, &http.Request{Method: http.MethodConnect})
readDone <- responseResult{resp: resp, err: err}
}()
var rr responseResult
select {
case rr = <-readDone:
case <-readCtx.Done():
_ = conn.Close()
<-readDone
return nil, readCtx.Err()
}
resp, err := rr.resp, rr.err
if err != nil {
_ = conn.Close()
return nil, err
}
if resp.StatusCode/100 != 2 {
_ = conn.Close()
return nil, fmt.Errorf("upstream CONNECT: %s", resp.Status)
}
return &bufferedConn{Conn: conn, reader: br.Reader}, nil
}
+47 -5
View File
@@ -13,6 +13,7 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/Autumn-27/artex/agent"
@@ -282,7 +283,10 @@ type Manager struct {
// (http/https/socks5, optional user:pass). Empty = direct. When traffic
// capture is on it becomes the MITM's upstream; when capture is off it is
// injected into agent bash env / WebFetch directly. See ProxyAddr.
globalProxy string
globalProxy string
proxyPool *proxyPoolState
forwardProxy *dynamicForwardProxy
proxyPoolConfigured atomic.Bool
}
// SetTaskControlMutex lets lifecycle operations that originate in Manager
@@ -582,6 +586,14 @@ func NewManager(dir, proxyAddr string) (*Manager, error) {
log.Printf("[llmusage] create table: %v", err)
}
m := &Manager{dir: dir, pg: pg, assets: pg.Assets(), tasks: map[string]*Task{}, interceptor: intercept.New(pg)}
if fp, ferr := newDynamicForwardProxy(func() string { return m.GlobalProxy() }); ferr == nil {
m.forwardProxy = fp
// An empty runtime route means direct egress, even when the persisted
// pool still contains disabled or blank entries.
fp.direct = func() bool { return strings.TrimSpace(m.GlobalProxy()) == "" }
} else {
log.Printf("[proxy] local forwarder disabled: %v", ferr)
}
if proxyAddr != "" {
tr, err := traffic.Open(filepath.Join(dir, "traffic"), proxyAddr)
if err != nil {
@@ -642,6 +654,7 @@ func NewManager(dir, proxyAddr string) (*Manager, error) {
if v, ok, _ := pg.GetSetting(settingGlobalProxy); ok {
m.globalProxy = strings.TrimSpace(v)
}
m.initProxyPool()
if m.traffic != nil {
if err := m.traffic.SetUpstreamProxy(m.globalProxy); err != nil {
log.Printf("[proxy] 全局代理 %q 无效,已忽略: %v", m.globalProxy, err)
@@ -1016,8 +1029,12 @@ func (m *Manager) ProxyAddr() string {
return m.traffic.ProxyAddr()
}
m.mu.RLock()
defer m.mu.RUnlock()
return m.globalProxy
global := m.globalProxy
m.mu.RUnlock()
if (global != "" || m.proxyPoolConfigured.Load()) && m.forwardProxy != nil {
return m.forwardProxy.Addr()
}
return maskSecrets(global)
}
// ProxyCACert returns the CA cert path agents must trust to verify HTTPS through
@@ -1047,7 +1064,30 @@ func (m *Manager) SetGlobalProxy(raw string) error {
raw = strings.TrimSpace(raw)
if raw != "" {
if _, err := traffic.ValidateProxyURL(raw); err != nil {
return err
return fmt.Errorf("%s", maskSecrets(err.Error()))
}
}
if m.proxyPool != nil {
m.proxyPool.mu.Lock()
managed := len(m.proxyPool.entries) > 0
if !managed && raw != "" {
m.proxyPool.entries = []proxyPoolEntry{{ProxyPoolEntry: ProxyPoolEntry{ID: "proxy-legacy", URL: maskSecrets(raw), Enabled: true, Health: "healthy"}, RawURL: raw}}
m.proxyPool.active = "proxy-legacy"
m.proxyPoolConfigured.Store(true)
if err := m.applyActiveProxyLocked("proxy-legacy", "manual"); err != nil {
m.proxyPool.entries = nil
m.proxyPool.active = ""
m.proxyPool.mu.Unlock()
return err
}
m.proxyPool.mu.Unlock()
return nil
}
m.proxyPool.mu.Unlock()
if managed {
// The legacy field is a read-only echo once a pool exists. Pool API
// operations are the sole authority for health, enablement and switching.
return nil
}
}
if err := m.pg.SetSetting(settingGlobalProxy, raw); err != nil {
@@ -1061,12 +1101,14 @@ func (m *Manager) SetGlobalProxy(raw string) error {
return err
}
}
// Keep the browser MCP's egress in sync with the new global proxy too.
m.syncBrowserMCPProxy()
return nil
}
func (m *Manager) Close() error {
if m.forwardProxy != nil {
_ = m.forwardProxy.Close()
}
m.mu.Lock()
defer m.mu.Unlock()
if m.traffic != nil {
+32 -3
View File
@@ -65,6 +65,10 @@ type proxyReconnectGuard struct {
}
func (s *Server) snapshotProxyReconnectGuards(checkAttempt bool) map[string]proxyReconnectGuard {
return s.snapshotProxyReconnectGuardsWithExhausted(checkAttempt, false)
}
func (s *Server) snapshotProxyReconnectGuardsWithExhausted(checkAttempt, includeExhausted bool) map[string]proxyReconnectGuard {
guards := make(map[string]proxyReconnectGuard)
if s == nil || s.m == nil {
return guards
@@ -74,6 +78,9 @@ func (s *Server) snapshotProxyReconnectGuards(checkAttempt bool) map[string]prox
if st.PauseReason != "paused_by_proxy" {
continue
}
if st.ReconnectExhausted && !includeExhausted {
continue
}
guards[t.ID] = proxyReconnectGuard{
generation: st.ReconnectGeneration,
attempt: st.ReconnectAttempt,
@@ -135,7 +142,10 @@ func (s *Server) handleProxyFailure(taskID string, cause error) {
if strings.TrimSpace(s.m.GlobalProxy()) == "" {
return
}
if probe := probeEgress(context.Background(), s.m.GlobalProxy(), "", defaultEgressProbes, 10*time.Second); probe.OK {
if s.handleProxyPoolFailure(cause) {
return
}
if probe := s.runProxyProbe(s.ctx, s.m.GlobalProxy(), "", defaultEgressProbes, 10*time.Second); probe.OK {
return
}
s.pauseProxyTask(taskID, cause)
@@ -182,6 +192,9 @@ func (s *Server) pauseProxyTask(taskID string, cause error) {
}
func (s *Server) handleProxyTrafficFailure(cause error) {
if s.handleProxyPoolFailure(cause) {
return
}
if !isGlobalProxyDialFailure(cause) || strings.TrimSpace(s.m.GlobalProxy()) == "" {
return
}
@@ -200,6 +213,9 @@ func (s *Server) handleProxyTrafficFailure(cause error) {
}
func (s *Server) handleProxyToolFailure(ctx context.Context, cause error) {
if s.handleProxyPoolFailure(cause) {
return
}
if !isGlobalProxyDialFailure(cause) || strings.TrimSpace(s.m.GlobalProxy()) == "" {
return
}
@@ -215,7 +231,7 @@ func truncateProxyError(err error) string {
if err == nil {
return ""
}
s := err.Error()
s := maskSecrets(err.Error())
if len(s) > 240 {
return s[:240] + "…"
}
@@ -239,8 +255,21 @@ func (s *Server) runProxyReconnect(ctx context.Context) {
func (s *Server) proxyReconnectTick() {
if strings.TrimSpace(s.m.GlobalProxy()) == "" {
// Clearing the runtime route is a successful direct transition. Reuse the
// generation gate so only eligible, non-exhausted proxy pauses resume.
s.proxyReconnectSuccess()
return
}
if s.m.proxyPool != nil && len(s.m.proxyPoolEntries()) > 0 {
// A pool tick may confirm the active route or switch to another available
// member. If it cannot confirm either, retain the original #106 per-task
// attempt/backoff/exhaustion loop (especially for a one-member legacy
// pool).
outcome := s.proxyPoolTick()
if outcome.confirmed || outcome.switched {
return
}
}
now := time.Now().Unix()
max, interval := s.proxyReconnectConfig()
for _, t := range s.m.List() {
@@ -396,7 +425,7 @@ func (s *Server) testTaskProxy(w http.ResponseWriter, r *http.Request) {
return
}
proxy := strings.TrimSpace(s.m.GlobalProxy())
guards := s.snapshotProxyReconnectGuards(false)
guards := s.snapshotProxyReconnectGuardsWithExhausted(false, true)
if proxy == "" {
writeJSON(w, 200, map[string]any{"ok": true, "direct": true})
s.proxyReconnectSuccessWithGuards(guards, nil)
+1160
View File
File diff suppressed because it is too large. Load diff
+791
View File
@@ -0,0 +1,791 @@
package server
import (
"bufio"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"
"github.com/Autumn-27/artex/agent"
"github.com/Autumn-27/artex/traffic"
)
func TestParseProxyBatchValidationAndDedup(t *testing.T) {
entries, errs := ParseProxyBatch("http://user:[email protected]:8080|one\nhttp://user:[email protected]:8080\nftp://bad:1\n# comment\n")
if len(entries) != 1 || len(errs) != 2 {
t.Fatalf("entries=%d errs=%d", len(entries), len(errs))
}
if entries[0].URL == "" || !strings.Contains(entries[0].URL, "pass") {
t.Fatalf("parser lost stored credential")
}
}
func TestProxyPoolSnapshotMasksCredentialsAndDisabledHealth(t *testing.T) {
m := &Manager{proxyPool: &proxyPoolState{entries: []proxyPoolEntry{{ProxyPoolEntry: ProxyPoolEntry{ID: "1", Enabled: false, Health: "healthy"}, RawURL: "http://user:secret@host:8080"}}, logs: []ProxySwitchLog{{From: "http://user:secret@host:8080", To: "http://user:secret2@host:8081", Reason: "timed"}}}}
entries, _, logs := m.ProxyPoolSnapshot()
if strings.Contains(entries[0].URL, "secret") || entries[0].Health != "disabled" {
t.Fatalf("entry leaked or wrong state: %+v", entries[0])
}
if strings.Contains(logs[0].From, "secret") || strings.Contains(logs[0].To, "secret2") {
t.Fatalf("switch log leaked credentials: %+v", logs[0])
}
}
func TestProxyPoolSequentialSkipsUnhealthyAndDisabled(t *testing.T) {
m := &Manager{proxyPool: &proxyPoolState{active: "a", entries: []proxyPoolEntry{
{ProxyPoolEntry: ProxyPoolEntry{ID: "a", Enabled: true, Health: "healthy"}, RawURL: "http://a"},
{ProxyPoolEntry: ProxyPoolEntry{ID: "b", Enabled: false, Health: "healthy"}, RawURL: "http://b"},
{ProxyPoolEntry: ProxyPoolEntry{ID: "c", Enabled: true, Health: "unhealthy-cooling"}, RawURL: "http://c"},
{ProxyPoolEntry: ProxyPoolEntry{ID: "d", Enabled: true, Health: "healthy"}, RawURL: "http://d"},
}}}
if got := m.selectHealthyLocked("a"); got != "d" {
t.Fatalf("selected %q", got)
}
}
func TestProxyPoolIntervalLowerBound(t *testing.T) {
if minProxyRotateInterval >= 5 {
t.Fatalf("bound must allow the documented 5s setting")
}
if minProxyRotateInterval != 3 {
t.Fatalf("bound=%d, rationale expects 3s", minProxyRotateInterval)
}
}
func TestProxyPoolPersistenceKeepsRawURL(t *testing.T) {
b, err := json.Marshal(proxyPoolPersist{Entries: []proxyPoolEntry{{ProxyPoolEntry: ProxyPoolEntry{ID: "1", URL: "http://***@host:1"}, RawURL: "http://user:pass@host:1"}}})
if err != nil || !strings.Contains(string(b), "raw_url") || !strings.Contains(string(b), "pass") {
t.Fatalf("bad persistence encoding: %s", b)
}
}
func TestProxyPoolURLKeyNormalizesHostCase(t *testing.T) {
a, _ := traffic.ValidateProxyURL("http://user:pass@HOST:8080")
b, _ := traffic.ValidateProxyURL("http://user:pass@host:8080")
if proxyURLKey(a) != proxyURLKey(b) {
t.Fatalf("keys differ: %q %q", proxyURLKey(a), proxyURLKey(b))
}
}
func TestProxyPoolHealthThresholdCooldownAndRecovery(t *testing.T) {
m := &Manager{proxyPool: &proxyPoolState{entries: []proxyPoolEntry{{ProxyPoolEntry: ProxyPoolEntry{ID: "a", Enabled: true, Health: "healthy"}, RawURL: "http://a"}}}}
fail := egressProbeResult{Error: "http://user:pass@proxy: connection refused"}
for i := 0; i < defaultProxyHealthThreshold-1; i++ {
m.markProxyProbe("a", fail)
if got := m.proxyPool.entries[0].Health; got != "healthy" {
t.Fatalf("failure %d moved to %s before threshold", i+1, got)
}
}
m.markProxyProbe("a", fail)
if got := m.proxyPool.entries[0]; got.Health != "unhealthy-cooling" || got.ConsecutiveFailures != defaultProxyHealthThreshold {
t.Fatalf("threshold state=%+v", got)
}
if got := m.proxyPool.entries[0].LastError; strings.Contains(got, "pass") || !strings.Contains(got, "***@") {
t.Fatalf("failure was not masked: %q", got)
}
m.markProxyProbe("a", egressProbeResult{OK: true, ExitIP: "203.0.113.9"})
if got := m.proxyPool.entries[0]; got.Health != "healthy" || got.ConsecutiveFailures != 0 || got.EgressIP != "203.0.113.9" {
t.Fatalf("recovery state=%+v", got)
}
}
func TestProxyPoolUncheckedMemberDoesNotPauseBeforeFailureThreshold(t *testing.T) {
m := proxyTestManager(t)
task, err := m.CreateTask("unchecked proxy", "goal", nil, 0, 0)
if err != nil {
t.Fatal(err)
}
defer func() { _, _ = m.DeleteTask(task.ID, DeleteTaskOptions{}) }()
m.globalProxy = "http://a"
m.proxyPool = &proxyPoolState{active: "a", entries: []proxyPoolEntry{{
ProxyPoolEntry: ProxyPoolEntry{ID: "a", Enabled: true, Health: "unknown"}, RawURL: "http://a",
}}}
s := &Server{m: m, ctx: context.Background(), proxyPoolProbeSem: make(chan struct{}, proxyPoolMaxProbeConcurrency)}
s.proxyProbe = func(context.Context, string, string, []string, time.Duration) egressProbeResult {
return egressProbeResult{Error: "proxy down"}
}
s.proxyPoolTickLocked(true)
if st := task.lifecycleSnapshot(); st.Paused || st.PauseReason == "paused_by_proxy" {
t.Fatalf("unchecked member paused the task: %+v", st)
}
if got := m.proxyPool.entries[0].Health; got != "unknown" {
t.Fatalf("single failure changed unchecked member to %q", got)
}
}
func TestProxyPoolCoolingWindowSkipsProbeUntilExpiry(t *testing.T) {
m := proxyTestManager(t)
m.globalProxy = "http://a"
m.proxyPool = &proxyPoolState{active: "a", entries: []proxyPoolEntry{{
ProxyPoolEntry: ProxyPoolEntry{ID: "a", Enabled: true, Health: "unhealthy-cooling", ConsecutiveFailures: 3, LastCheckedAt: time.Now()}, RawURL: "http://a",
}}}
s := &Server{m: m, ctx: context.Background(), proxyPoolProbeSem: make(chan struct{}, proxyPoolMaxProbeConcurrency)}
calls := 0
s.proxyProbe = func(context.Context, string, string, []string, time.Duration) egressProbeResult {
calls++
return egressProbeResult{OK: true, ExitIP: "203.0.113.5"}
}
s.proxyPoolTickLocked(false)
if calls != 0 {
t.Fatalf("cooling member was probed before cooldown expiry: calls=%d", calls)
}
m.proxyPool.entries[0].LastCheckedAt = time.Now().Add(-2 * time.Minute)
s.proxyPoolTickLocked(false)
if calls != 1 || m.proxyPool.entries[0].Health != "healthy" {
t.Fatalf("expired cooling member did not recover: calls=%d entry=%+v", calls, m.proxyPool.entries[0])
}
}
func TestProxyPoolTimedRotationUsesAvailableMembersAndRecoversTasks(t *testing.T) {
m := proxyTestManager(t)
if err := m.SetProxyPoolSettings(true, minProxyRotateInterval, true, "sequential", 3, 60, 30); err != nil {
t.Fatal(err)
}
m.globalProxy = "http://a"
m.proxyPool = &proxyPoolState{active: "a", lastTimed: time.Now().Add(-time.Minute), lastHealth: time.Now(), entries: []proxyPoolEntry{
{ProxyPoolEntry: ProxyPoolEntry{ID: "a", Enabled: true, Health: "healthy"}, RawURL: "http://a"},
{ProxyPoolEntry: ProxyPoolEntry{ID: "b", Enabled: true, Health: "healthy"}, RawURL: "http://b"},
{ProxyPoolEntry: ProxyPoolEntry{ID: "c", Enabled: true, Health: "unhealthy-cooling"}, RawURL: "http://c"},
}}
s := &Server{m: m, ctx: context.Background()}
s.proxyPoolTickLocked(false)
if got := m.GlobalProxy(); got != "http://b" {
t.Fatalf("timed rotation selected %q", got)
}
if len(m.proxyPool.logs) == 0 || m.proxyPool.logs[len(m.proxyPool.logs)-1].Reason != "timed" {
t.Fatalf("timed rotation was not logged: %+v", m.proxyPool.logs)
}
}
func TestProxyPoolFailureSwitchRecoversOnlyEligibleTasks(t *testing.T) {
m := proxyTestManager(t)
if err := m.SetProxyPoolSettings(false, minProxyRotateInterval, true, "sequential", 3, 60, 30); err != nil {
t.Fatal(err)
}
m.globalProxy = "http://a"
m.proxyPool = &proxyPoolState{active: "a", entries: []proxyPoolEntry{
{ProxyPoolEntry: ProxyPoolEntry{ID: "a", Enabled: true, Health: "healthy"}, RawURL: "http://a"},
{ProxyPoolEntry: ProxyPoolEntry{ID: "b", Enabled: true, Health: "healthy"}, RawURL: "http://b"},
}}
eligible, err := m.CreateTask("eligible", "goal", nil, 0, 0)
if err != nil {
t.Fatal(err)
}
exhausted, err := m.CreateTask("exhausted", "goal", nil, 0, 0)
if err != nil {
t.Fatal(err)
}
defer func() {
_, _ = m.DeleteTask(eligible.ID, DeleteTaskOptions{})
_, _ = m.DeleteTask(exhausted.ID, DeleteTaskOptions{})
}()
for _, task := range []*Task{eligible, exhausted} {
if err := m.ApplyProxyPause(task.ID); err != nil {
t.Fatal(err)
}
task.updateLifecycle(func(v *taskLifecycleState) { v.ReconnectGeneration = 7; v.ReconnectMax = 3 })
}
exhausted.updateLifecycle(func(v *taskLifecycleState) { v.ReconnectAttempt, v.ReconnectExhausted = 3, true })
s := newAdmissionTestServer(m, nil)
m.SetTaskControlMutex(&s.concMu)
s.proxyPoolWake = make(chan struct{}, 1)
s.proxyPoolProbeSem = make(chan struct{}, proxyPoolMaxProbeConcurrency)
s.proxyProbe = func(context.Context, string, string, []string, time.Duration) egressProbeResult {
return egressProbeResult{Error: "proxy down"}
}
s.engine.Pause(eligible.ID, agent.AbortPausedByProxy)
s.engine.Pause(exhausted.ID, agent.AbortPausedByProxy)
if !s.handleProxyPoolFailure(errors.New("proxyconnect tcp: connection refused")) {
t.Fatal("confirmed failure did not switch to the available member")
}
if got := m.GlobalProxy(); got != "http://b" {
t.Fatalf("failure switch selected %q", got)
}
if st := eligible.lifecycleSnapshot(); st.PauseReason != "" || st.Paused {
t.Fatalf("eligible task did not recover: %+v", st)
}
if st := exhausted.lifecycleSnapshot(); st.PauseReason != "paused_by_proxy" || !st.ReconnectExhausted {
t.Fatalf("exhausted task was automatically resumed: %+v", st)
}
}
func TestProxyPoolReconnectKeepsAttemptsAndDoesNotAutoResumeExhausted(t *testing.T) {
m := proxyTestManager(t)
if err := m.pg.SetSetting(settingProxyReconnectMax, "2"); err != nil {
t.Fatal(err)
}
if err := m.pg.SetSetting(settingProxyReconnectInterval, "1"); err != nil {
t.Fatal(err)
}
m.globalProxy = "http://legacy"
m.proxyPool = &proxyPoolState{active: "legacy", entries: []proxyPoolEntry{{
ProxyPoolEntry: ProxyPoolEntry{ID: "legacy", Enabled: true, Health: "healthy"}, RawURL: "http://legacy",
}}}
task, err := m.CreateTask("legacy reconnect", "goal", nil, 0, 0)
if err != nil {
t.Fatal(err)
}
defer func() { _, _ = m.DeleteTask(task.ID, DeleteTaskOptions{}) }()
if err := m.ApplyProxyPause(task.ID); err != nil {
t.Fatal(err)
}
task.updateLifecycle(func(v *taskLifecycleState) { v.ReconnectGeneration = 3 })
s := &Server{m: m, ctx: context.Background(), proxyPoolProbeSem: make(chan struct{}, proxyPoolMaxProbeConcurrency)}
s.proxyProbe = func(context.Context, string, string, []string, time.Duration) egressProbeResult {
return egressProbeResult{Error: "proxy down"}
}
s.proxyReconnectTick()
first := task.lifecycleSnapshot()
if first.ReconnectAttempt != 1 || first.ReconnectExhausted {
t.Fatalf("first pool reconnect attempt=%+v", first)
}
task.updateLifecycle(func(v *taskLifecycleState) { v.ReconnectNextAt = 0 })
s.proxyReconnectTick()
second := task.lifecycleSnapshot()
if second.ReconnectAttempt != 2 || !second.ReconnectExhausted || second.PauseReason != "paused_by_proxy" {
t.Fatalf("second pool reconnect did not exhaust: %+v", second)
}
s.proxyReconnectTick()
third := task.lifecycleSnapshot()
if third.ReconnectAttempt != second.ReconnectAttempt || !third.ReconnectExhausted || third.PauseReason != "paused_by_proxy" {
t.Fatalf("automatic tick revived exhausted task: %+v", third)
}
guards := s.snapshotProxyReconnectGuardsWithExhausted(false, true)
s.proxyReconnectSuccessWithGuards(guards, nil)
if got := task.lifecycleSnapshot(); got.PauseReason != "" {
t.Fatalf("manual guarded resume did not release exhausted task: %+v", got)
}
}
func TestProxyPoolDisableOrClearActiveRoutesAway(t *testing.T) {
newServer := func(entries []proxyPoolEntry) (*Server, *Manager) {
m := &Manager{globalProxy: "http://a", proxyPool: &proxyPoolState{active: "a", entries: entries}}
return &Server{m: m}, m
}
t.Run("disable switches to healthy member", func(t *testing.T) {
s, m := newServer([]proxyPoolEntry{
{ProxyPoolEntry: ProxyPoolEntry{ID: "a", Enabled: true, Health: "healthy"}, RawURL: "http://a"},
{ProxyPoolEntry: ProxyPoolEntry{ID: "b", Enabled: true, Health: "healthy"}, RawURL: "http://b"},
})
r := httptest.NewRequest(http.MethodPatch, "/api/settings/proxy-pool/a", strings.NewReader(`{"enabled":false}`))
r.SetPathValue("id", "a")
w := httptest.NewRecorder()
s.updateProxyPool(w, r)
if w.Code != http.StatusOK || m.GlobalProxy() != "http://b" || m.proxyPoolActiveID() != "b" {
t.Fatalf("disable route status=%d proxy=%q active=%q body=%s", w.Code, m.GlobalProxy(), m.proxyPoolActiveID(), w.Body.String())
}
})
t.Run("clear last member clears runtime", func(t *testing.T) {
s, m := newServer([]proxyPoolEntry{{
ProxyPoolEntry: ProxyPoolEntry{ID: "a", Enabled: true, Health: "healthy"}, RawURL: "http://a",
}})
r := httptest.NewRequest(http.MethodPatch, "/api/settings/proxy-pool/a", strings.NewReader(`{"url":""}`))
r.SetPathValue("id", "a")
w := httptest.NewRecorder()
s.updateProxyPool(w, r)
if w.Code != http.StatusOK || m.GlobalProxy() != "" || m.proxyPoolActiveID() != "" {
t.Fatalf("clear route status=%d proxy=%q active=%q body=%s", w.Code, m.GlobalProxy(), m.proxyPoolActiveID(), w.Body.String())
}
})
}
func TestProxyPoolClearOrDeleteLastMemberRecoversOnlyEligibleProxyPauses(t *testing.T) {
run := func(t *testing.T, deleteLast bool) {
m := proxyTestManager(t)
m.globalProxy = "http://a"
m.proxyPool = &proxyPoolState{active: "a", entries: []proxyPoolEntry{{
ProxyPoolEntry: ProxyPoolEntry{ID: "a", Enabled: true, Health: "healthy"}, RawURL: "http://a",
}}}
eligible, err := m.CreateTask("eligible", "goal", nil, 0, 0)
if err != nil {
t.Fatal(err)
}
exhausted, err := m.CreateTask("exhausted", "goal", nil, 0, 0)
if err != nil {
t.Fatal(err)
}
userPaused, err := m.CreateTask("user paused", "goal", nil, 0, 0)
if err != nil {
t.Fatal(err)
}
defer func() {
_, _ = m.DeleteTask(eligible.ID, DeleteTaskOptions{})
_, _ = m.DeleteTask(exhausted.ID, DeleteTaskOptions{})
_, _ = m.DeleteTask(userPaused.ID, DeleteTaskOptions{})
}()
for _, task := range []*Task{eligible, exhausted} {
if err := m.ApplyProxyPause(task.ID); err != nil {
t.Fatal(err)
}
task.updateLifecycle(func(v *taskLifecycleState) { v.ReconnectGeneration = 7 })
}
exhausted.updateLifecycle(func(v *taskLifecycleState) {
v.ReconnectAttempt, v.ReconnectExhausted = 3, true
})
if err := m.ApplyTaskPauseOrigin(userPaused.ID, false); err != nil {
t.Fatal(err)
}
s := newAdmissionTestServer(m, nil)
for _, task := range []*Task{eligible, exhausted, userPaused} {
s.engine.Pause(task.ID, agent.AbortPausedByProxy)
}
if deleteLast {
r := httptest.NewRequest(http.MethodDelete, "/api/settings/proxy-pool/a", nil)
r.SetPathValue("id", "a")
w := httptest.NewRecorder()
s.deleteProxyPool(w, r)
if w.Code != http.StatusOK {
t.Fatalf("delete status=%d body=%s", w.Code, w.Body.String())
}
} else {
r := httptest.NewRequest(http.MethodPatch, "/api/settings/proxy-pool/a", strings.NewReader(`{"url":""}`))
r.SetPathValue("id", "a")
w := httptest.NewRecorder()
s.updateProxyPool(w, r)
if w.Code != http.StatusOK {
t.Fatalf("clear status=%d body=%s", w.Code, w.Body.String())
}
}
if got := eligible.lifecycleSnapshot(); got.PauseReason != "" || got.Paused {
t.Fatalf("eligible proxy pause was not recovered: %+v", got)
}
if got := exhausted.lifecycleSnapshot(); got.PauseReason != "paused_by_proxy" || !got.ReconnectExhausted {
t.Fatalf("exhausted proxy pause was recovered: %+v", got)
}
if got := userPaused.lifecycleSnapshot(); got.PauseReason != "paused_by_user" {
t.Fatalf("user pause was recovered: %+v", got)
}
}
t.Run("clear", func(t *testing.T) { run(t, false) })
t.Run("delete", func(t *testing.T) { run(t, true) })
}
func TestProxyPoolCaptureOffUsesLocalForwarder(t *testing.T) {
fp, err := newDynamicForwardProxy(func() string { return "" })
if err != nil {
t.Fatal(err)
}
defer fp.Close()
m := &Manager{forwardProxy: fp}
m.proxyPoolConfigured.Store(true)
got := m.ProxyAddr()
if !strings.HasPrefix(got, "http://127.0.0.1:") {
t.Fatalf("capture-off proxy address=%q, want local forwarder", got)
}
}
func TestProxyPoolClearedRuntimeForwarderFallsBackToDirect(t *testing.T) {
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = io.WriteString(w, "direct-ok")
}))
defer target.Close()
m := proxyTestManager(t)
// A configured pool can remain persisted after its last usable member is
// disabled or cleared. The runtime route is still direct in that state.
m.proxyPoolConfigured.Store(true)
proxyURL, err := url.Parse(m.ProxyAddr())
if err != nil {
t.Fatal(err)
}
client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)}}
resp, err := client.Get(target.URL)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK || string(body) != "direct-ok" {
t.Fatalf("cleared runtime route status=%d body=%q", resp.StatusCode, body)
}
}
func TestProxyReconnectGuardsGateExhaustedAutomaticResume(t *testing.T) {
task := &Task{ID: "exhausted", PauseReason: "paused_by_proxy", ReconnectGeneration: 4, ReconnectAttempt: 3, ReconnectExhausted: true}
m := &Manager{tasks: map[string]*Task{task.ID: task}}
s := &Server{m: m}
if got := s.snapshotProxyReconnectGuards(false); len(got) != 0 {
t.Fatalf("automatic guards included exhausted task: %+v", got)
}
if got := s.snapshotProxyReconnectGuardsWithExhausted(false, true); len(got) != 1 {
t.Fatalf("manual guards did not include exhausted task: %+v", got)
}
}
func TestProxyPoolSettingsRejectTooShortInterval(t *testing.T) {
m := &Manager{}
if err := m.SetProxyPoolSettings(true, minProxyRotateInterval-1, true, "sequential", 3, 60, 30); err == nil {
t.Fatal("interval below lower bound was accepted")
}
}
func TestProxyPoolRandomSelectionStillExcludesDisabledAndCooling(t *testing.T) {
m := proxyTestManager(t)
if err := m.pg.SetSetting(settingProxyRotateStrategy, "random"); err != nil {
t.Fatal(err)
}
m.proxyPool = &proxyPoolState{entries: []proxyPoolEntry{
{ProxyPoolEntry: ProxyPoolEntry{ID: "disabled", Enabled: false, Health: "healthy"}, RawURL: "http://disabled"},
{ProxyPoolEntry: ProxyPoolEntry{ID: "cooling", Enabled: true, Health: "unhealthy-cooling"}, RawURL: "http://cooling"},
{ProxyPoolEntry: ProxyPoolEntry{ID: "empty", Enabled: true, Health: "healthy"}},
{ProxyPoolEntry: ProxyPoolEntry{ID: "good", Enabled: true, Health: "healthy"}, RawURL: "http://good"},
}}
for i := 0; i < 20; i++ {
if got := m.selectHealthyLocked(""); got != "good" {
t.Fatalf("random selected %q", got)
}
}
}
func TestProxyPoolProbeSkipsEnabledEmptyURL(t *testing.T) {
m := proxyTestManager(t)
m.globalProxy = "http://good"
m.proxyPool = &proxyPoolState{active: "good", entries: []proxyPoolEntry{
{ProxyPoolEntry: ProxyPoolEntry{ID: "empty", Enabled: true, Health: "healthy"}},
{ProxyPoolEntry: ProxyPoolEntry{ID: "good", Enabled: true, Health: "healthy"}, RawURL: "http://good"},
}}
s := &Server{m: m, ctx: context.Background(), proxyPoolProbeSem: make(chan struct{}, proxyPoolMaxProbeConcurrency)}
calls := 0
s.proxyProbe = func(context.Context, string, string, []string, time.Duration) egressProbeResult {
calls++
return egressProbeResult{OK: true}
}
s.proxyPoolTickLocked(true)
if calls != 1 {
t.Fatalf("probe calls=%d, want only non-empty enabled member", calls)
}
}
func TestProxyPoolFailureMarksOriginalMemberAfterConcurrentSwitch(t *testing.T) {
m := &Manager{globalProxy: "http://a", proxyPool: &proxyPoolState{active: "a", entries: []proxyPoolEntry{
{ProxyPoolEntry: ProxyPoolEntry{ID: "a", Enabled: true, Health: "healthy"}, RawURL: "http://a"},
{ProxyPoolEntry: ProxyPoolEntry{ID: "b", Enabled: true, Health: "healthy"}, RawURL: "http://b"},
}}}
s := &Server{m: m, ctx: context.Background(), proxyPoolWake: make(chan struct{}, 1)}
s.proxyProbe = func(context.Context, string, string, []string, time.Duration) egressProbeResult {
m.mu.Lock()
m.globalProxy = "http://b"
m.mu.Unlock()
return egressProbeResult{Error: "proxy connection refused"}
}
if !s.handleProxyPoolFailure(fmt.Errorf("proxyconnect tcp: connection refused")) {
t.Fatal("failure after concurrent switch was not consumed")
}
if got := m.proxyPool.entries[0].ConsecutiveFailures; got != 1 {
t.Fatalf("original member failure count=%d", got)
}
}
func TestProxyPoolAllMembersUnhealthyKeepsProxyConfigured(t *testing.T) {
m := &Manager{globalProxy: "http://a", proxyPool: &proxyPoolState{active: "a", entries: []proxyPoolEntry{
{ProxyPoolEntry: ProxyPoolEntry{ID: "a", Enabled: true, Health: "unhealthy-cooling", ConsecutiveFailures: defaultProxyHealthThreshold, LastCheckedAt: time.Now().Add(-2 * time.Minute)}, RawURL: "http://a"},
{ProxyPoolEntry: ProxyPoolEntry{ID: "b", Enabled: true, Health: "unhealthy-cooling", ConsecutiveFailures: defaultProxyHealthThreshold, LastCheckedAt: time.Now().Add(-2 * time.Minute)}, RawURL: "http://b"},
}}}
s := &Server{m: m, ctx: context.Background(), proxyPoolProbeSem: make(chan struct{}, proxyPoolMaxProbeConcurrency)}
s.proxyProbe = func(context.Context, string, string, []string, time.Duration) egressProbeResult {
return egressProbeResult{Error: "proxy down"}
}
s.proxyPoolTickLocked(true)
if got := m.GlobalProxy(); got != "http://a" {
t.Fatalf("all unhealthy members fell back to direct: %q", got)
}
if got := m.proxyPoolActiveID(); got != "a" {
t.Fatalf("active member changed without a healthy replacement: %q", got)
}
}
func TestProxyPoolMaskedEditHotCutsCoolingActiveMember(t *testing.T) {
m := &Manager{globalProxy: "http://old:[email protected]:8080", proxyPool: &proxyPoolState{active: "a", entries: []proxyPoolEntry{{
ProxyPoolEntry: ProxyPoolEntry{ID: "a", Enabled: true, Health: "unhealthy-cooling", ConsecutiveFailures: 3},
RawURL: "http://old:[email protected]:8080",
}}}}
s := &Server{m: m}
r := httptest.NewRequest(http.MethodPatch, "/api/settings/proxy-pool/a", strings.NewReader(`{"url":"http://***@new.example:9090"}`))
r.SetPathValue("id", "a")
w := httptest.NewRecorder()
s.updateProxyPool(w, r)
if w.Code != http.StatusOK {
t.Fatalf("status=%d body=%s", w.Code, w.Body.String())
}
if got := m.GlobalProxy(); got != "http://old:[email protected]:9090" {
t.Fatalf("active proxy=%q", got)
}
if got := m.proxyPool.entries[0].Health; got != "healthy" {
t.Fatalf("edited active member health=%q", got)
}
}
func TestProxyPoolDeleteNeverRetainsDeletedRuntimeURL(t *testing.T) {
m := &Manager{globalProxy: "http://a", proxyPool: &proxyPoolState{active: "a", entries: []proxyPoolEntry{
{ProxyPoolEntry: ProxyPoolEntry{ID: "a", Enabled: true, Health: "healthy"}, RawURL: "http://a"},
{ProxyPoolEntry: ProxyPoolEntry{ID: "b", Enabled: true, Health: "healthy"}, RawURL: "http://b"},
}}}
s := &Server{m: m}
deleteEntry := func(id string) {
r := httptest.NewRequest(http.MethodDelete, "/api/settings/proxy-pool/"+id, nil)
r.SetPathValue("id", id)
w := httptest.NewRecorder()
s.deleteProxyPool(w, r)
if w.Code != http.StatusOK {
t.Fatalf("delete %s status=%d body=%s", id, w.Code, w.Body.String())
}
}
deleteEntry("a")
if got := m.GlobalProxy(); got != "http://b" {
t.Fatalf("surviving pool member was not activated: %q", got)
}
deleteEntry("b")
if got := m.GlobalProxy(); got != "" || m.proxyPool.active != "" {
t.Fatalf("deleted last member left runtime proxy=%q active=%q", got, m.proxyPool.active)
}
}
func TestSetGlobalProxyIsReadOnlyWhenPoolManaged(t *testing.T) {
m := &Manager{globalProxy: "http://pool.example:8080", proxyPool: &proxyPoolState{entries: []proxyPoolEntry{{
ProxyPoolEntry: ProxyPoolEntry{ID: "pool", Enabled: true, Health: "healthy"}, RawURL: "http://pool.example:8080",
}}}}
if err := m.SetGlobalProxy("http://other.example:8080"); err != nil {
t.Fatal(err)
}
if got := m.GlobalProxy(); got != "http://pool.example:8080" {
t.Fatalf("managed pool runtime changed through legacy setting: %q", got)
}
}
func TestProxyPoolEmptyPersistenceDoesNotResurrectLegacyProxy(t *testing.T) {
dir := t.TempDir()
m, err := NewManager(dir, "")
if err != nil {
t.Skipf("postgres unavailable (%v) - skipping", err)
}
m.proxyPool = &proxyPoolState{active: "a", entries: []proxyPoolEntry{{
ProxyPoolEntry: ProxyPoolEntry{ID: "a", Enabled: true, Health: "healthy"}, RawURL: "http://deleted.example:8080",
}}}
m.globalProxy = "http://deleted.example:8080"
if err := m.persistProxyPoolLocked(); err != nil {
_ = m.Close()
t.Fatal(err)
}
s := &Server{m: m}
r := httptest.NewRequest(http.MethodDelete, "/api/settings/proxy-pool/a", nil)
r.SetPathValue("id", "a")
w := httptest.NewRecorder()
s.deleteProxyPool(w, r)
if w.Code != http.StatusOK {
_ = m.Close()
t.Fatalf("delete status=%d body=%s", w.Code, w.Body.String())
}
if got := m.GlobalProxy(); got != "" {
t.Fatalf("delete left runtime proxy=%q", got)
}
if err := m.Close(); err != nil {
t.Fatal(err)
}
m2, err := NewManager(dir, "")
if err != nil {
t.Fatal(err)
}
defer m2.Close()
if got := m2.GlobalProxy(); got != "" || len(m2.proxyPoolEntries()) != 0 {
t.Fatalf("empty persisted pool resurrected proxy=%q entries=%d", got, len(m2.proxyPoolEntries()))
}
}
func TestMergeMaskedProxyURLPreservesOnlyPassword(t *testing.T) {
got, err := mergeMaskedProxyURL("http://***@NEW.example:9090", "http://old:[email protected]:8080")
if err != nil {
t.Fatal(err)
}
if got != "http://old:[email protected]:9090" {
t.Fatalf("got %q", got)
}
}
func TestMergeMaskedProxyURLErrorIsMasked(t *testing.T) {
_, err := mergeMaskedProxyURL("http://user:secret@", "")
if err == nil || strings.Contains(err.Error(), "secret") {
t.Fatalf("unmasked error: %v", err)
}
}
func TestDynamicForwardProxyUsesCurrentUpstream(t *testing.T) {
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _, _ = w.Write([]byte("ok")) }))
defer target.Close()
up := ""
fp, err := newDynamicForwardProxy(func() string { return up })
if err != nil {
t.Fatal(err)
}
defer fp.Close()
pu, _ := url.Parse(fp.Addr())
c := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(pu)}}
resp, err := c.Get(target.URL)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
t.Fatalf("status %d", resp.StatusCode)
}
}
func TestDynamicForwardProxyConnectAuthenticatedUpstreamAndBufferedBytes(t *testing.T) {
const user, pass = "alice", "s3cret"
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodConnect {
http.Error(w, "connect required", http.StatusMethodNotAllowed)
return
}
want := "Basic " + base64.StdEncoding.EncodeToString([]byte(user+":"+pass))
if r.Header.Get("Proxy-Authorization") != want {
w.Header().Set("Proxy-Authenticate", `Basic realm="test"`)
w.WriteHeader(http.StatusProxyAuthRequired)
return
}
h, ok := w.(http.Hijacker)
if !ok {
http.Error(w, "no hijack", 500)
return
}
c, rw, err := h.Hijack()
if err != nil {
return
}
defer c.Close()
_, _ = rw.WriteString("HTTP/1.1 200 Connection Established\r\n\r\nupstream-buffer")
_ = rw.Flush()
}))
defer upstream.Close()
up, _ := url.Parse(upstream.URL)
up.User = url.UserPassword(user, pass)
fp, err := newDynamicForwardProxy(func() string { return up.String() })
if err != nil {
t.Fatal(err)
}
defer fp.Close()
c, err := net.Dial("tcp", strings.TrimPrefix(fp.Addr(), "http://"))
if err != nil {
t.Fatal(err)
}
defer c.Close()
_, _ = fmt.Fprintf(c, "CONNECT example.test:443 HTTP/1.1\r\nHost: example.test:443\r\n\r\n")
rd := bufio.NewReader(c)
resp, err := http.ReadResponse(rd, &http.Request{Method: http.MethodConnect})
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("local CONNECT status %s", resp.Status)
}
b, err := io.ReadAll(io.LimitReader(rd, int64(len("upstream-buffer"))))
if err != nil || string(b) != "upstream-buffer" {
t.Fatalf("buffered bytes=%q err=%v", b, err)
}
}
func TestDynamicForwardProxyConnectForwardsHijackBufferedClientBytes(t *testing.T) {
targetLn, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer targetLn.Close()
go func() {
c, err := targetLn.Accept()
if err != nil {
return
}
defer c.Close()
buf := make([]byte, len("hello"))
if _, err := io.ReadFull(c, buf); err == nil {
_, _ = c.Write([]byte("target"))
}
}()
proxyServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodConnect {
http.Error(w, "connect required", 405)
return
}
c, err := net.Dial("tcp", targetLn.Addr().String())
if err != nil {
http.Error(w, err.Error(), 502)
return
}
h, _ := w.(http.Hijacker)
client, rw, err := h.Hijack()
if err != nil {
_ = c.Close()
return
}
defer client.Close()
defer c.Close()
_, _ = rw.WriteString("HTTP/1.1 200 Connection Established\r\n\r\n")
_ = rw.Flush()
buf := make([]byte, len("hello"))
if _, err := io.ReadFull(rw.Reader, buf); err != nil {
return
}
_, _ = c.Write(buf)
response := make([]byte, len("target"))
if _, err := io.ReadFull(c, response); err == nil {
_, _ = client.Write(response)
}
}))
defer proxyServer.Close()
fp, err := newDynamicForwardProxy(func() string { return proxyServer.URL })
if err != nil {
t.Fatal(err)
}
defer fp.Close()
c, err := net.Dial("tcp", strings.TrimPrefix(fp.Addr(), "http://"))
if err != nil {
t.Fatal(err)
}
defer c.Close()
_, _ = fmt.Fprintf(c, "CONNECT %s HTTP/1.1\r\nHost: %s\r\n\r\nhello", targetLn.Addr().String(), targetLn.Addr().String())
rd := bufio.NewReader(c)
resp, err := http.ReadResponse(rd, &http.Request{Method: http.MethodConnect})
if err != nil || resp.StatusCode != http.StatusOK {
t.Fatalf("CONNECT response=%v status=%v", err, resp)
}
_ = c.SetReadDeadline(time.Now().Add(2 * time.Second))
b, err := io.ReadAll(io.LimitReader(rd, int64(len("target"))))
if err != nil || string(b) != "target" {
t.Fatalf("target response=%q err=%v", b, err)
}
}
func TestDialHTTPConnectBlackholeHonorsContext(t *testing.T) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer ln.Close()
go func() {
c, err := ln.Accept()
if err == nil {
defer c.Close()
_, _ = io.Copy(io.Discard, c)
}
}()
u, _ := url.Parse("http://" + ln.Addr().String())
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
started := time.Now()
_, err = dialHTTPConnect(ctx, u, "example.test:443")
if err == nil {
t.Fatal("blackholed CONNECT unexpectedly succeeded")
}
if elapsed := time.Since(started); elapsed > time.Second {
t.Fatalf("context cancellation took %s", elapsed)
}
}
+104 -19
View File
@@ -125,10 +125,14 @@ type Server struct {
// Cold task archives run through one persistent FIFO worker. The buffered wake
// channel coalesces enqueue bursts; the database remains the source of truth.
archiveWake chan struct{}
archiveWG sync.WaitGroup
credentialWake chan struct{}
proxyReconnect proxyReconnectSupervisor
archiveWake chan struct{}
archiveWG sync.WaitGroup
credentialWake chan struct{}
proxyReconnect proxyReconnectSupervisor
proxyPoolWake chan struct{}
proxyPoolWG sync.WaitGroup
proxyPoolProbeSem chan struct{}
proxyPoolTickMu sync.Mutex
// proxyProbe is injectable for deterministic reconnect race tests. Production
// servers leave it nil and use probeEgress directly.
proxyProbe func(context.Context, string, string, []string, time.Duration) egressProbeResult
@@ -163,7 +167,7 @@ func New(ctx context.Context, m *Manager, skillDir string, dataDir string, keyDi
}
s := &Server{m: m, engine: NewEngine(m), ctx: ctx, skillDir: skillDir, dataDir: dataDir, jwtKey: key, chatBusy: map[string]bool{},
chatCancel: map[string]context.CancelCauseFunc{}, triggerQ: map[string][]triggeredRun{},
triggerActive: map[string]int{}, triggerCfg: map[string]triggerBehavior{},
triggerActive: map[string]int{}, triggerCfg: map[string]triggerBehavior{}, proxyPoolWake: make(chan struct{}, 1), proxyPoolProbeSem: make(chan struct{}, proxyPoolMaxProbeConcurrency),
profChatAgents: map[int64]*agent.ChatAgent{},
provByProfile: map[int64]*provEntry{}, llmHealth: newLLMHealthRegistry(m.pg),
taskAgents: map[string]*taskAgentBundle{}, archiveWake: make(chan struct{}, 1), credentialWake: make(chan struct{}, 1)}
@@ -290,6 +294,8 @@ func New(ctx context.Context, m *Manager, skillDir string, dataDir string, keyDi
}
s.restoreTaskRuntimes()
s.ensureProxySupervisor()
s.proxyPoolWG.Add(1)
go func() { defer s.proxyPoolWG.Done(); s.runProxyPoolSupervisor(s.ctx) }()
go s.reconcileConcurrency()
s.startTaskArchiveWorker()
if m.pg != nil {
@@ -306,6 +312,13 @@ func New(ctx context.Context, m *Manager, skillDir string, dataDir string, keyDi
return s
}
// Close waits for proxy pool supervision to stop before Manager closes the DB.
func (s *Server) Close() {
if s != nil {
s.proxyPoolWG.Wait()
}
}
// Restored deadline and worker loops must inherit the same context as new tasks,
// including the side-question checkpoint publisher installed during startup.
func (s *Server) restoreTaskRuntimes() {
@@ -519,6 +532,7 @@ func (s *Server) applyLLM(cfg agent.Config) error {
// (backend/key) and gates Enabled per-conversation-agent at Chat time. 对话始终用激活配置。
s.chatAgent = agent.NewChatAgent(prov, cfg.Model, s.m.dir, tx, win) // chat page runner
s.chatAgent.SetProxy(s.m.ProxyAddr(), s.m.ProxyCACert())
s.chatAgent.SetProxyProvider(s.m.ProxyAddr)
s.chatAgent.SetWebSearch(s.m.WebSearchOpts())
s.chatAgent.SetGuard(s.chatGuard())
s.chatAgent.SetNonStreaming(nonStreamingResolver(cfg))
@@ -681,6 +695,7 @@ func (s *Server) chatAgentForProfile(id int64) *agent.ChatAgent {
tx := transcript.NewStore(filepath.Join(s.m.dir, "transcripts"))
ca := agent.NewChatAgent(s.poolForBinding(id, prov, cfg), cfg.Model, s.m.dir, tx, cfg.CompactionWindow())
ca.SetProxy(s.m.ProxyAddr(), s.m.ProxyCACert())
ca.SetProxyProvider(s.m.ProxyAddr)
ca.SetWebSearch(s.m.WebSearchOpts())
ca.SetGuard(s.chatGuard())
ca.SetNonStreaming(nonStreamingResolver(cfg))
@@ -937,6 +952,13 @@ func (s *Server) Handler() http.Handler {
mux.HandleFunc("POST /api/settings/web-search/test", s.testWebSearch)
// 出口代理连通性测试:发真实请求验证 global_proxy 是否可用,并回显出口 IP。
mux.HandleFunc("POST /api/settings/proxy/test", s.testEgressProxy)
mux.HandleFunc("GET /api/settings/proxy-pool", s.getProxyPool)
mux.HandleFunc("POST /api/settings/proxy-pool/import", s.importProxyPool)
mux.HandleFunc("PUT /api/settings/proxy-pool/{id}", s.updateProxyPool)
mux.HandleFunc("DELETE /api/settings/proxy-pool/{id}", s.deleteProxyPool)
mux.HandleFunc("POST /api/settings/proxy-pool/{id}/test", s.testProxyPoolEntry)
mux.HandleFunc("POST /api/settings/proxy-pool/test-all", s.testAllProxyPool)
mux.HandleFunc("POST /api/settings/proxy-pool/{id}/activate", s.activateProxyPool)
mux.HandleFunc("POST /api/tasks/{id}/proxy/test", s.testTaskProxy)
mux.HandleFunc("GET /api/report", s.getReport)
mux.HandleFunc("GET /api/chat/mentions", s.searchChatMentions)
@@ -3549,16 +3571,32 @@ func (s *Server) settingsPayload() map[string]any {
"web_search_backend": backend,
"brave_key_set": strings.TrimSpace(braveKey) != "",
"tavily_key_set": strings.TrimSpace(tavilyKey) != "",
"web_search_proxy": proxy, // 独立出口代理(http/https/socks5),空=直连
"global_proxy": s.m.GlobalProxy(), // 全局出口代理(http/https/socks5),所有目标流量走它,空=直连
"proxy_reconnect_max_attempts": func() int { n, _ := s.proxyReconnectConfig(); return n }(),
"proxy_reconnect_interval_seconds": func() int { _, d := s.proxyReconnectConfig(); return int(d / time.Second) }(),
"show_egress_ip": s.m.pg.GetBool(settingShowEgressIP, false),
"python_interpreter": strings.TrimSpace(pyStored), // 用户/自动设的值(空=用运行时检测)
"workers": s.m.Workers(), // 并发工作 agent 数(默认3);对之后启动的任务生效
"worker_pools": s.m.WorkerPools(), // 非空时按池顺序绑定 worker 会话
"task_concurrency_enabled": concOn, // 任务并发上限开关(默认关)
"task_concurrency_limit": concLimit, // 同时运行任务上限(开启后默认5)
"web_search_proxy": maskSecrets(proxy), // 独立出口代理(http/https/socks5),空=直连
"global_proxy": maskSecrets(s.m.GlobalProxy()), // 全局出口代理(http/https/socks5),所有目标流量走它,空=直连
"proxy_pool": func() []ProxyPoolEntry { e, _, _ := s.m.ProxyPoolSnapshot(); return e }(),
"proxy_pool_active_id": func() string { _, a, _ := s.m.ProxyPoolSnapshot(); return a }(),
"proxy_pool_switch_log": func() []ProxySwitchLog { _, _, l := s.m.ProxyPoolSnapshot(); return l }(),
"proxy_rotate_timed_enabled": s.m.pg.GetBool(settingProxyRotateTimed, false),
"proxy_rotate_interval_seconds": proxyIntSetting(s.m.pg, settingProxyRotateInterval, defaultProxyRotateInterval),
"proxy_rotate_on_failure": s.m.pg.GetBool(settingProxyRotateFailure, false),
"proxy_rotate_strategy": func() string {
v, ok, _ := s.m.pg.GetSetting(settingProxyRotateStrategy)
if ok && (v == "random" || v == "sequential") {
return v
}
return "sequential"
}(),
"proxy_health_fail_threshold": proxyIntSetting(s.m.pg, settingProxyHealthThreshold, defaultProxyHealthThreshold),
"proxy_health_cooldown_seconds": proxyIntSetting(s.m.pg, settingProxyHealthCooldown, defaultProxyHealthCooldown),
"proxy_health_check_interval_seconds": proxyIntSetting(s.m.pg, settingProxyHealthInterval, defaultProxyHealthInterval),
"proxy_reconnect_max_attempts": func() int { n, _ := s.proxyReconnectConfig(); return n }(),
"proxy_reconnect_interval_seconds": func() int { _, d := s.proxyReconnectConfig(); return int(d / time.Second) }(),
"show_egress_ip": s.m.pg.GetBool(settingShowEgressIP, false),
"python_interpreter": strings.TrimSpace(pyStored), // 用户/自动设的值(空=用运行时检测)
"workers": s.m.Workers(), // 并发工作 agent 数(默认3);对之后启动的任务生效
"worker_pools": s.m.WorkerPools(), // 非空时按池顺序绑定 worker 会话
"task_concurrency_enabled": concOn, // 任务并发上限开关(默认关)
"task_concurrency_limit": concLimit, // 同时运行任务上限(开启后默认5)
// LLM 轮询(故障转移)。默认关;开启后走全局激活配置的 agent 在当前配置不可用时
// 自动切到下一个配置。bind_fallback 仅在轮询开启时有意义(默认关)。
"llm_pool_enabled": s.m.LLMPoolEnabled(),
@@ -3635,6 +3673,13 @@ func (s *Server) putSettings(w http.ResponseWriter, r *http.Request) {
TavilyKey *string `json:"tavily_search_api_key"`
WebSearchProxy *string `json:"web_search_proxy"` // 独立出口代理(http/https/socks5);null=不改,""=清空
GlobalProxy *string `json:"global_proxy"` // 全局出口代理(http/https/socks5);null=不改,""=清空(直连)
ProxyRotateTimed *bool `json:"proxy_rotate_timed_enabled"`
ProxyRotateInterval *int `json:"proxy_rotate_interval_seconds"`
ProxyRotateFailure *bool `json:"proxy_rotate_on_failure"`
ProxyRotateStrategy *string `json:"proxy_rotate_strategy"`
ProxyHealthThreshold *int `json:"proxy_health_fail_threshold"`
ProxyHealthCooldown *int `json:"proxy_health_cooldown_seconds"`
ProxyHealthInterval *int `json:"proxy_health_check_interval_seconds"`
ProxyReconnectMax *int `json:"proxy_reconnect_max_attempts"`
ProxyReconnectInterval *int `json:"proxy_reconnect_interval_seconds"`
ShowEgressIP *bool `json:"show_egress_ip"` // 在顶栏显示直连/全局代理出口 IP;默认关
@@ -3851,17 +3896,20 @@ func (s *Server) putSettings(w http.ResponseWriter, r *http.Request) {
changed = true
}
if req.GlobalProxy != nil {
globalProxyValue := strings.TrimSpace(*req.GlobalProxy)
if strings.Contains(globalProxyValue, "***") || strings.Contains(globalProxyValue, "******") {
globalProxyValue = s.m.GlobalProxy()
}
var proxyGuards map[string]proxyReconnectGuard
if strings.TrimSpace(*req.GlobalProxy) == "" {
if globalProxyValue == "" {
proxyGuards = s.snapshotProxyReconnectGuards(false)
}
// Validation failure (bad scheme/host) is a client error, not a 500.
if err := s.m.SetGlobalProxy(*req.GlobalProxy); err != nil {
if err := s.m.SetGlobalProxy(globalProxyValue); err != nil {
writeErr(w, 400, err.Error())
return
}
changed = true // capture-off egress is baked into agents at build time → rebuild
if strings.TrimSpace(*req.GlobalProxy) == "" {
if globalProxyValue == "" && (s.m.proxyPool == nil || len(s.m.proxyPoolEntries()) == 0) {
s.proxyReconnectSuccessWithGuards(proxyGuards, nil)
}
}
@@ -3871,6 +3919,39 @@ func (s *Server) putSettings(w http.ResponseWriter, r *http.Request) {
return
}
}
if req.ProxyRotateTimed != nil || req.ProxyRotateInterval != nil || req.ProxyRotateFailure != nil || req.ProxyRotateStrategy != nil || req.ProxyHealthThreshold != nil || req.ProxyHealthCooldown != nil || req.ProxyHealthInterval != nil {
timed, interval, onFailure, strategy := s.m.proxyRotateConfig()
threshold, cooldown, healthInterval := s.m.proxyHealthConfig()
if req.ProxyRotateTimed != nil {
timed = *req.ProxyRotateTimed
}
if req.ProxyRotateInterval != nil {
interval = time.Duration(*req.ProxyRotateInterval) * time.Second
}
if req.ProxyRotateFailure != nil {
onFailure = *req.ProxyRotateFailure
}
if req.ProxyRotateStrategy != nil {
strategy = strings.TrimSpace(*req.ProxyRotateStrategy)
}
if req.ProxyHealthThreshold != nil {
threshold = *req.ProxyHealthThreshold
}
if req.ProxyHealthCooldown != nil {
cooldown = time.Duration(*req.ProxyHealthCooldown) * time.Second
}
if req.ProxyHealthInterval != nil {
healthInterval = time.Duration(*req.ProxyHealthInterval) * time.Second
}
if err := s.m.SetProxyPoolSettings(timed, int(interval/time.Second), onFailure, strategy, threshold, int(cooldown/time.Second), int(healthInterval/time.Second)); err != nil {
writeErr(w, 400, err.Error())
return
}
select {
case s.proxyPoolWake <- struct{}{}:
default:
}
}
if req.ShowEgressIP != nil {
if err := s.m.pg.SetBool(settingShowEgressIP, *req.ShowEgressIP); err != nil {
writeErr(w, 500, err.Error())
@@ -3886,6 +3967,10 @@ func (s *Server) putSettings(w http.ResponseWriter, r *http.Request) {
if req.WebSearchBackend != nil {
backend = *req.WebSearchBackend
}
if req.WebSearchProxy != nil && (strings.Contains(*req.WebSearchProxy, "***") || strings.Contains(*req.WebSearchProxy, "******")) {
_, _, _, _, savedSearchProxy := s.m.WebSearch()
req.WebSearchProxy = &savedSearchProxy
}
if err := s.m.SetWebSearch(on, backend, req.BraveKey, req.TavilyKey, req.WebSearchProxy); err != nil {
writeErr(w, 500, err.Error())
return
+4
View File
@@ -592,6 +592,7 @@ func (s *Server) agentsForTask(t *Task) *taskAgentBundle {
wk.SetNoaEnabled(s.m.NoaCompactionEnabled) // 实验功能:noa 上下文压缩(平台级开关,每 run 读)
wk.SetRunTimeout(time.Duration(s.agentRunSeconds("worker")) * time.Second)
wk.SetProxy(s.m.ProxyAddr(), s.m.ProxyCACert())
wk.SetProxyProvider(s.m.ProxyAddr)
wk.SetWebSearch(s.webSearchFor("worker"))
wk.SetConstraintInject(s.constraintInjectWorker) // 操作约束注入 worker(可配置,默认开)
pl := agent.NewPlanner(plannerRuntime, "task-router", s.m.dir, tx, plannerRuntime.CompactionWindow(), s.agentMaxTurns("planner"))
@@ -603,6 +604,7 @@ func (s *Server) agentsForTask(t *Task) *taskAgentBundle {
pl.SetKillWork(s.engine.KillWork)
pl.SetSteerWork(s.engine.SteerWork)
pl.SetProxy(s.m.ProxyAddr(), s.m.ProxyCACert())
pl.SetProxyProvider(s.m.ProxyAddr)
pl.SetWebSearch(s.webSearchFor("planner"))
pl.SetConstraintInject(s.constraintInjectPlanner) // 操作约束注入 planner(可配置,默认开)
// cold-digest §7: 冷节点后台压缩。引擎经权威解析器实际驱动的就是这套 per-task planner
@@ -616,6 +618,7 @@ func (s *Server) agentsForTask(t *Task) *taskAgentBundle {
main.SetMaxTokens(mainRuntime.maxTokens)
main.SetNoaEnabled(s.m.NoaCompactionEnabled) // 实验功能:noa 上下文压缩(平台级开关,每 run 读)
main.SetProxy(s.m.ProxyAddr(), s.m.ProxyCACert())
main.SetProxyProvider(s.m.ProxyAddr)
main.SetWebSearch(s.webSearchFor("mainagent"))
main.SetSteerWork(s.engine.SteerWork) // steer_work:人对运行中 work 实时纠偏
bundle := &taskAgentBundle{
@@ -662,6 +665,7 @@ func (s *Server) workerForTaskSlot(t *Task, slot WorkerSlot) (*agent.Worker, err
worker.SetNoaEnabled(s.m.NoaCompactionEnabled)
worker.SetRunTimeout(time.Duration(s.agentRunSeconds("worker")) * time.Second)
worker.SetProxy(s.m.ProxyAddr(), s.m.ProxyCACert())
worker.SetProxyProvider(s.m.ProxyAddr)
worker.SetWebSearch(s.webSearchFor("worker"))
worker.SetConstraintInject(s.constraintInjectWorker)
bundle.pinnedWorkers[id] = worker
@@ -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 });
+84 -1
View File
@@ -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
View File
@@ -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 推送 ----
+9
View File
@@ -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,
+6
View File
@@ -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" };
+26
View File
@@ -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;