Files
ARTEX-chixuweihu/server/proxy_tools.go
T
ji ruo 778e49317d feat(proxy,chat): add per-task/schedule/chat proxy pool routing and @task mentions
- Support pool auto-rotation mode alongside inherit/direct/node/custom proxy routes across gateway and MITM proxy
- Add GET /api/proxy/routes/{scope}/{owner_id}, task create/DTO proxy_route, and schedule proxy persistence + propagation
- Add list_proxy_pool and set_task_proxy chat agent tools and @task mention search/context injection
- Wire shared proxy/pool selectors into tasks, schedules, chat sandbox popover, and container routes UI
2026-12-03 00:10:50 +08:00

153 lines
5.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package server
import (
"context"
"encoding/json"
"fmt"
"log"
"strconv"
"strings"
actool "github.com/Autumn-27/norma/tool"
)
func parseTaskIDArg(raw json.RawMessage) string {
if len(raw) == 0 {
return ""
}
var s string
if json.Unmarshal(raw, &s) == nil {
return strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(s), "#"))
}
var n int64
if json.Unmarshal(raw, &n) == nil && n > 0 {
return strconv.FormatInt(n, 10)
}
return ""
}
func (s *Server) toolListProxyPool() actool.CoreTool {
return roTool("list_proxy_pool",
"查看当前全局出口代理、代理池节点列表(含 ID、名称、健康状态、出口 IP)以及所有任务/会话容器的自定义代理路由。",
objSchema(map[string]any{}),
func(context.Context, json.RawMessage) (actool.Result, error) {
entries, activeID, _ := s.m.ProxyPoolSnapshot()
rt := s.ensureRouteRuntime()
rt.mu.RLock()
routesCopy := make([]ContainerProxyRoute, 0, len(rt.routes))
for _, r := range rt.routes {
routesCopy = append(routesCopy, r)
}
rt.mu.RUnlock()
globalRaw := strings.TrimSpace(s.m.GlobalProxy())
globalDisplay := "direct"
if globalRaw != "" {
globalDisplay = maskSecrets(globalRaw)
}
routes := make([]map[string]any, 0, len(routesCopy))
for _, r := range routesCopy {
cp := r
eff, _ := s.effectiveProxyDisplay(&cp)
routes = append(routes, map[string]any{
"scope": r.Scope,
"owner_id": r.OwnerID,
"mode": r.Mode,
"proxy_pool_id": r.ProxyPoolID,
"proxy_url": maskSecrets(r.ProxyURL),
"effective_proxy": eff,
"note": r.Note,
"updated_at": r.UpdatedAt,
})
}
return jsonResult(map[string]any{
"global_proxy": globalDisplay,
"active_pool_id": activeID,
"entries": entries,
"routes": routes,
})
})
}
func (s *Server) toolSetTaskProxy() actool.CoreTool {
return wrTool("set_task_proxy",
"为指定任务的沙箱容器设置出口代理或代理池(保存并立即对透明网关与抓包代理热生效)。\n"+
"- mode=pool:使用代理池自动轮换(跟随当前健康活跃节点)\n"+
"- mode=proxy:绑定代理池指定节点(传 proxy_pool_id,支持节点 ID 或名称)或自定义代理(传 proxy_url)\n"+
"- mode=direct:强制直连(绕过全局代理/代理池)\n"+
"- mode=inherit:恢复继承全局出口代理设置",
objSchema(map[string]any{
"task_id": strParam("目标任务 ID(如 \"5\")"),
"mode": map[string]any{"type": "string", "enum": []string{"inherit", "direct", "pool", "proxy"}, "description": "代理模式:pool=代理池自动轮换,proxy=指定节点或自定义 URL,direct=直连,inherit=继承全局"},
"proxy_pool_id": strParam("可选(mode=proxy 时):代理池节点 ID 或名称,可用 list_proxy_pool 查看"),
"proxy_url": strParam("可选(mode=proxy 且未传 proxy_pool_id 时):自定义代理 URL,如 socks5://host:1080 或 http://user:pass@host:8080"),
"note": strParam("可选:路由备注"),
}, "task_id", "mode"),
func(_ context.Context, in json.RawMessage) (actool.Result, error) {
var a struct {
TaskID json.RawMessage `json:"task_id"`
Mode string `json:"mode"`
ProxyPoolID string `json:"proxy_pool_id"`
ProxyURL string `json:"proxy_url"`
Note string `json:"note"`
}
if err := json.Unmarshal(in, &a); err != nil {
return actool.Errorf("参数无效: " + err.Error()), nil
}
taskID := parseTaskIDArg(a.TaskID)
if taskID == "" {
return actool.Errorf("task_id 不能为空"), nil
}
t, ok := s.m.Task(taskID)
if !ok {
return actool.Errorf(fmt.Sprintf("任务 #%s 不存在", taskID)), nil
}
mode := strings.ToLower(strings.TrimSpace(a.Mode))
if mode == "" {
if strings.TrimSpace(a.ProxyPoolID) != "" || strings.TrimSpace(a.ProxyURL) != "" {
mode = "proxy"
} else {
return actool.Errorf("mode 不能为空"), nil
}
}
route, err := s.setContainerProxyRoute("task", t.ID, ProxyRouteInput{
Mode: mode,
ProxyPoolID: a.ProxyPoolID,
ProxyURL: a.ProxyURL,
Note: a.Note,
})
if err != nil {
return actool.Errorf("设置任务代理失败: " + err.Error()), nil
}
view, globalDisplay := s.containerProxyRouteView("task", t.ID)
return jsonResult(map[string]any{
"task_id": t.ID,
"task_name": t.lifecycleSnapshot().Name,
"scope": route.Scope,
"owner_id": route.OwnerID,
"mode": view.Mode,
"proxy_pool_id": view.ProxyPoolID,
"proxy_url": view.ProxyURL,
"effective_proxy": view.EffectiveProxy,
"global_proxy": globalDisplay,
"note": view.Note,
})
})
}
// seedProxyToolBindings adds list_proxy_pool and set_task_proxy bindings ONCE
// on existing DBs so chat agents can manage task proxies immediately.
func (s *Server) seedProxyToolBindings() {
const flag = "proxy_tool_bindings_v1"
if v, _, _ := s.m.pg.GetSetting(flag); v == "true" {
return
}
for _, agentKey := range []string{"auto", "mainagent", "pentest"} {
if err := s.m.pg.AddAgentToToolBinding(agentKey, []string{"list_proxy_pool", "set_task_proxy"}); err != nil {
log.Printf("[%s] 代理工具默认绑定失败: %v", agentKey, err)
return
}
}
_ = s.m.pg.SetSetting(flag, "true")
}