package main import ( "encoding/json" "fmt" "net/http" "os" "os/exec" "time" ) func downloadNetworkLog(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } filename := fmt.Sprintf("network.log.%s.tar.gz", time.Now().Format("20060102150405")) tmpFile := fmt.Sprintf("/tmp/%d.tar.gz", time.Now().UnixNano()) defer os.Remove(tmpFile) cmd := exec.Command( "tar", "--warning=no-file-changed", "-czf", tmpFile, "-C", "/opt/yfkj/networkd.service/log", ".", ) out, err := cmd.CombinedOutput() if err != nil { if exitErr, ok := err.(*exec.ExitError); !ok || exitErr.ExitCode() != 1 { http.Error(w, string(out), http.StatusInternalServerError) return } } w.Header().Set("Content-Type", "application/gzip") w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename)) http.ServeFile(w, r, tmpFile) } func restartNetworkService(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } cmd := exec.Command("systemctl", "restart", "yfkj-networkd.service") if out, err := cmd.CombinedOutput(); err != nil { http.Error(w, string(out)+err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/json") w.Write([]byte(`{"status":"ok"}`)) } func networkStatusHandler(w http.ResponseWriter, r *http.Request) { var modem struct { ICCID string `json:"iccid"` RSSI string `json:"rssi"` } var net struct { Status string `json:"net_ok"` Type string `json:"net_type"` } err1 := callRPCResult(r.Context(), 7000, "core.getNetStatus", nil, &net) err2 := callRPCResult(r.Context(), 7000, "core.getModemInfo", nil, &modem) netType := map[string]string{ "有线": "🌐有线", "蜂窝": "📶蜂窝", }[net.Type] if netType == "" { netType = noValue } rssi := modem.RSSI if rssi == "" { rssi = noValue } else { rssi = "📶" + rssi } iccid := modem.ICCID if iccid == "" { iccid = noValue } else { iccid = "🔖" + iccid } w.Header().Set("Content-Type", "application/json; charset=utf-8") json.NewEncoder(w).Encode(map[string]string{ "var1": map[bool]string{true: "🟢正常", false: noValue}[err1 == nil || err2 == nil], "var2": map[bool]string{true: "🟢正常", false: noValue}[net.Status == "true"], "var3": netType, "var4": rssi, "var5": iccid, }) }