Dispatch 提供给 nodeagent.APIDispatcher 复用的非 HTTP 入口。 method 取值:IPSentinelDetect / IPSentinelDetectGoogle / IPSentinelRun / IPSentinelStatus / IPSentinelGetConfig / IPSentinelSetConfig。 未识别 method 返回 (nil, error)。
(ctx context.Context, method string, body []byte)
| 104 | // IPSentinelStatus / IPSentinelGetConfig / IPSentinelSetConfig。 |
| 105 | // 未识别 method 返回 (nil, error)。 |
| 106 | func (h *NodeHandler) Dispatch(ctx context.Context, method string, body []byte) (any, error) { |
| 107 | switch method { |
| 108 | case "IPSentinelDetect": |
| 109 | return Detect(ctx) |
| 110 | case "IPSentinelDetectGoogle": |
| 111 | cfg, err := h.loadConfig() |
| 112 | if err != nil { |
| 113 | cfg = Config{ValidURLSuffix: "com"} |
| 114 | } |
| 115 | return DetectGoogle(ctx, cfg) |
| 116 | case "IPSentinelRun": |
| 117 | var req struct { |
| 118 | Type string `json:"type"` |
| 119 | TaskType string `json:"task_type"` |
| 120 | } |
| 121 | if len(body) > 0 { |
| 122 | _ = json.Unmarshal(body, &req) |
| 123 | } |
| 124 | taskType := req.Type |
| 125 | if taskType == "" { |
| 126 | taskType = req.TaskType |
| 127 | } |
| 128 | if taskType == "" { |
| 129 | taskType = "auto" |
| 130 | } |
| 131 | cfg, err := h.loadConfig() |
| 132 | if err != nil { |
| 133 | return nil, err |
| 134 | } |
| 135 | if h.runner.IsRunning() { |
| 136 | return map[string]any{"ok": false, "running": true, "message": "已有任务正在运行"}, nil |
| 137 | } |
| 138 | go h.runner.Run(h.ctx, cfg, taskType) |
| 139 | return map[string]any{"ok": true, "running": true}, nil |
| 140 | case "IPSentinelStatus": |
| 141 | return NodeStatus{Running: h.runner.IsRunning(), Last: h.runner.GetLastResult()}, nil |
| 142 | case "IPSentinelGetConfig": |
| 143 | return h.loadConfig() |
| 144 | case "IPSentinelSetConfig": |
| 145 | var cfg Config |
| 146 | if err := json.Unmarshal(body, &cfg); err != nil { |
| 147 | return nil, err |
| 148 | } |
| 149 | if err := h.saveConfig(cfg); err != nil { |
| 150 | return nil, err |
| 151 | } |
| 152 | return map[string]any{"ok": true}, nil |
| 153 | } |
| 154 | return nil, fmt.Errorf("ipsentinel: unknown method %q", method) |
| 155 | } |
| 156 | |
| 157 | func (h *NodeHandler) handleDetect(w http.ResponseWriter, r *http.Request) { |
| 158 | result, err := Detect(r.Context()) |
nothing calls this directly
no test coverage detected