MCPcopy Create free account
hub / github.com/0xUnixIO/pulse / Dispatch

Method Dispatch

internal/ipsentinel/nodehandler.go:106–155  ·  view source on GitHub ↗

Dispatch 提供给 nodeagent.APIDispatcher 复用的非 HTTP 入口。 method 取值:IPSentinelDetect / IPSentinelDetectGoogle / IPSentinelRun / IPSentinelStatus / IPSentinelGetConfig / IPSentinelSetConfig。 未识别 method 返回 (nil, error)。

(ctx context.Context, method string, body []byte)

Source from the content-addressed store, hash-verified

104// IPSentinelStatus / IPSentinelGetConfig / IPSentinelSetConfig。
105// 未识别 method 返回 (nil, error)。
106func (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
157func (h *NodeHandler) handleDetect(w http.ResponseWriter, r *http.Request) {
158 result, err := Detect(r.Context())

Callers

nothing calls this directly

Calls 7

loadConfigMethod · 0.95
saveConfigMethod · 0.95
DetectFunction · 0.85
DetectGoogleFunction · 0.85
IsRunningMethod · 0.80
GetLastResultMethod · 0.80
RunMethod · 0.45

Tested by

no test coverage detected