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
This commit is contained in:
ji ruo committed 2026-12-03 00:10:50 +08:00
1 parent b18cf89b77
commit 778e49317d
28 files changed
+1683 -124

No files matched your search

+9 -1
View File
@@ -34,7 +34,7 @@ type chatMentionCursor struct {
func ValidChatMentionKind(kind string) bool {
switch kind {
case "finding", "company", "asset", "endpoint", "ip", "app", "root_domain", "subdomain", "service":
case "task", "finding", "company", "asset", "endpoint", "ip", "app", "root_domain", "subdomain", "service":
return true
}
return false
@@ -68,6 +68,14 @@ func (d *DB) SearchChatMentionsPage(ctx context.Context, kind, query, cursor str
pattern := "%" + strings.NewReplacer(`\`, `\\`, "%", `\%`, "_", `\_`).Replace(query) + "%"
rows, err := d.QueryContext(ctx, `
SELECT kind, id, left(label, 160), left(description, 240) FROM (
(SELECT 'task' AS kind, id,
COALESCE(NULLIF(name,''), NULLIF(description,''), '任务 #'||id::text) AS label,
concat_ws(' · ', status, NULLIF(left(goal, 160), ''), NULLIF(left(description, 120), '')) AS description
FROM tasks WHERE deleted_at IS NULL AND ($1='' OR $1='task') AND
($2='' OR id::text=$2 OR concat_ws(' ', name, description, goal, remark) ILIKE $3)
AND ($4::bigint=0 OR (id::text=$2)<$6 OR ((id::text=$2)=$6 AND (id<$4 OR (id=$4 AND 'task'>$5))))
ORDER BY (id::text=$2) DESC, id DESC LIMIT 21)
UNION ALL
(SELECT 'finding' AS kind, id, COALESCE(NULLIF(name,''), vulnclass) AS label,
concat_ws(' · ', severity, status, left(summary, 160)) AS description
FROM findings WHERE ($1='' OR $1='finding') AND
+26 -6
View File
@@ -24,6 +24,9 @@ type TaskSchedule struct {
StartTime string `json:"start_time"`
EndTime string `json:"end_time,omitempty"`
StartImmediately bool `json:"start_immediately"`
ProxyMode string `json:"proxy_mode,omitempty"`
ProxyPoolID string `json:"proxy_pool_id,omitempty"`
ProxyURL string `json:"proxy_url,omitempty"`
Status string `json:"status"`
LastWindowKey string `json:"last_window_key,omitempty"`
LastTransitionAt *time.Time `json:"last_transition_at,omitempty"`
@@ -44,13 +47,16 @@ type TaskScheduleInput struct {
StartTime string
EndTime string
StartImmediately bool
ProxyMode string
ProxyPoolID string
ProxyURL string
TaskIDs []int64
}
const scheduleCols = `id,name,remark,enabled,timezone_mode,schedule_type,
COALESCE(to_char(run_date,'YYYY-MM-DD'),''),weekdays,to_char(start_time,'HH24:MI:SS'),
COALESCE(to_char(end_date,'YYYY-MM-DD'),''),
COALESCE(to_char(end_time,'HH24:MI:SS'),''),start_immediately,status,last_window_key,
COALESCE(to_char(end_time,'HH24:MI:SS'),''),start_immediately,proxy_mode,proxy_pool_id,proxy_url,status,last_window_key,
last_transition_at,last_error,created_at,updated_at`
func marshalWeekdays(days []int) string {
@@ -66,6 +72,7 @@ func scanSchedule(sc interface{ Scan(...any) error }) (*TaskSchedule, error) {
var rawDays string
if err := sc.Scan(&s.ID, &s.Name, &s.Remark, &s.Enabled, &s.TimezoneMode, &s.ScheduleType,
&s.RunDate, &rawDays, &s.StartTime, &s.EndDate, &s.EndTime, &s.StartImmediately,
&s.ProxyMode, &s.ProxyPoolID, &s.ProxyURL,
&s.Status, &s.LastWindowKey, &s.LastTransitionAt, &s.LastError,
&s.CreatedAt, &s.UpdatedAt); err != nil {
return nil, err
@@ -144,6 +151,19 @@ func normalizeScheduleInput(in TaskScheduleInput) (TaskScheduleInput, error) {
if len(in.TaskIDs) > 100 {
return in, fmt.Errorf("最多选择100个任务")
}
in.ProxyMode = strings.ToLower(strings.TrimSpace(in.ProxyMode))
if in.ProxyMode == "" {
in.ProxyMode = "inherit"
}
if in.ProxyMode != "inherit" && in.ProxyMode != "direct" && in.ProxyMode != "pool" && in.ProxyMode != "proxy" {
return in, fmt.Errorf("计划任务代理模式无效")
}
in.ProxyPoolID = strings.TrimSpace(in.ProxyPoolID)
in.ProxyURL = strings.TrimSpace(in.ProxyURL)
if in.ProxyMode != "proxy" {
in.ProxyPoolID = ""
in.ProxyURL = ""
}
return in, nil
}
@@ -172,9 +192,9 @@ func createTaskScheduleTxCommit(tx *sql.Tx, in TaskScheduleInput) (*TaskSchedule
}
func insertTaskSchedule(tx *sql.Tx, in TaskScheduleInput) (*TaskSchedule, error) {
row := tx.QueryRow(`INSERT INTO task_schedules(name,enabled,timezone_mode,schedule_type,run_date,end_date,weekdays,start_time,end_time,start_immediately)
VALUES ($1,$2,$3,$4,NULLIF($5,'')::date,NULLIF($6,'')::date,$7,$8::time,NULLIF($9,'')::time,$10) RETURNING `+scheduleCols,
in.Name, in.Enabled, in.TimezoneMode, in.ScheduleType, in.RunDate, in.EndDate, marshalWeekdays(in.Weekdays), in.StartTime, in.EndTime, in.StartImmediately)
row := tx.QueryRow(`INSERT INTO task_schedules(name,enabled,timezone_mode,schedule_type,run_date,end_date,weekdays,start_time,end_time,start_immediately,proxy_mode,proxy_pool_id,proxy_url)
VALUES ($1,$2,$3,$4,NULLIF($5,'')::date,NULLIF($6,'')::date,$7,$8::time,NULLIF($9,'')::time,$10,$11,$12,$13) RETURNING `+scheduleCols,
in.Name, in.Enabled, in.TimezoneMode, in.ScheduleType, in.RunDate, in.EndDate, marshalWeekdays(in.Weekdays), in.StartTime, in.EndTime, in.StartImmediately, in.ProxyMode, in.ProxyPoolID, in.ProxyURL)
s, err := scanSchedule(row)
if err != nil {
return nil, err
@@ -208,8 +228,8 @@ func (d *DB) UpdateTaskSchedule(id int64, in TaskScheduleInput) (*TaskSchedule,
return nil, err
}
defer tx.Rollback()
row := tx.QueryRow(`UPDATE task_schedules SET name=$2,enabled=$3,timezone_mode=$4,schedule_type=$5,run_date=NULLIF($6,'')::date,end_date=NULLIF($7,'')::date,weekdays=$8,start_time=$9::time,end_time=NULLIF($10,'')::time,start_immediately=$11,status=CASE WHEN $3 THEN CASE WHEN status='completed' THEN 'waiting' ELSE status END ELSE 'paused' END,last_error='',last_window_key='' WHERE id=$1 RETURNING `+scheduleCols,
id, in.Name, in.Enabled, in.TimezoneMode, in.ScheduleType, in.RunDate, in.EndDate, marshalWeekdays(in.Weekdays), in.StartTime, in.EndTime, in.StartImmediately)
row := tx.QueryRow(`UPDATE task_schedules SET name=$2,enabled=$3,timezone_mode=$4,schedule_type=$5,run_date=NULLIF($6,'')::date,end_date=NULLIF($7,'')::date,weekdays=$8,start_time=$9::time,end_time=NULLIF($10,'')::time,start_immediately=$11,proxy_mode=$12,proxy_pool_id=$13,proxy_url=$14,status=CASE WHEN $3 THEN CASE WHEN status='completed' THEN 'waiting' ELSE status END ELSE 'paused' END,last_error='',last_window_key='' WHERE id=$1 RETURNING `+scheduleCols,
id, in.Name, in.Enabled, in.TimezoneMode, in.ScheduleType, in.RunDate, in.EndDate, marshalWeekdays(in.Weekdays), in.StartTime, in.EndTime, in.StartImmediately, in.ProxyMode, in.ProxyPoolID, in.ProxyURL)
s, err := scanSchedule(row)
if err != nil {
return nil, err
+3
View File
@@ -1093,6 +1093,9 @@ CREATE TABLE IF NOT EXISTS task_schedules (
CREATE INDEX IF NOT EXISTS idx_task_schedules_enabled ON task_schedules(enabled, id);
ALTER TABLE task_schedules ADD COLUMN IF NOT EXISTS end_date DATE;
ALTER TABLE task_schedules ADD COLUMN IF NOT EXISTS remark TEXT NOT NULL DEFAULT '';
ALTER TABLE task_schedules ADD COLUMN IF NOT EXISTS proxy_mode TEXT NOT NULL DEFAULT 'inherit';
ALTER TABLE task_schedules ADD COLUMN IF NOT EXISTS proxy_pool_id TEXT NOT NULL DEFAULT '';
ALTER TABLE task_schedules ADD COLUMN IF NOT EXISTS proxy_url TEXT NOT NULL DEFAULT '';
DROP TRIGGER IF EXISTS trg_task_schedules_upd ON task_schedules;
CREATE TRIGGER trg_task_schedules_upd BEFORE UPDATE ON task_schedules
FOR EACH ROW EXECUTE FUNCTION set_updated_at();