package app import ( "encoding/json" "io" "net/http" "strings" ) // --------------------------------------------------------------------- // EN: `handleDNSSmartdnsService` is an HTTP handler for dns smartdns service. // RU: `handleDNSSmartdnsService` - HTTP-обработчик для dns smartdns service. // --------------------------------------------------------------------- func handleDNSSmartdnsService(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } var body struct { Action string `json:"action"` } if r.Body != nil { defer r.Body.Close() _ = json.NewDecoder(io.LimitReader(r.Body, 1<<20)).Decode(&body) } action := strings.ToLower(strings.TrimSpace(body.Action)) if action == "" { action = "restart" } switch action { case "start", "stop", "restart": default: http.Error(w, "unknown action", http.StatusBadRequest) return } res := runSmartdnsUnitAction(action) mode := loadDNSMode() rt := smartDNSRuntimeSnapshot() writeJSON(w, http.StatusOK, map[string]any{ "ok": res.OK, "message": res.Message, "exitCode": res.ExitCode, "stdout": res.Stdout, "stderr": res.Stderr, "unit_state": smartdnsUnitState(), "via_smartdns": mode.ViaSmartDNS, "smartdns_addr": mode.SmartDNSAddr, "mode": mode.Mode, "runtime_nftset": rt.Enabled, "wildcard_source": rt.WildcardSource, }) } // --------------------------------------------------------------------- // EN: `handleSmartdnsService` is an HTTP handler for smartdns service. // RU: `handleSmartdnsService` - HTTP-обработчик для smartdns service. // --------------------------------------------------------------------- func handleSmartdnsService(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: writeJSON(w, http.StatusOK, map[string]string{"state": smartdnsUnitState()}) case http.MethodPost: var body struct { Action string `json:"action"` } if r.Body != nil { defer r.Body.Close() _ = json.NewDecoder(io.LimitReader(r.Body, 1<<20)).Decode(&body) } action := strings.ToLower(strings.TrimSpace(body.Action)) if action == "" { action = "restart" } switch action { case "start", "stop", "restart": default: http.Error(w, "unknown action", http.StatusBadRequest) return } writeJSON(w, http.StatusOK, runSmartdnsUnitAction(action)) default: http.Error(w, "method not allowed", http.StatusMethodNotAllowed) } }