package main import ( "bytes" "encoding/json" "fmt" "io" "net/http" "os" "os/exec" "path/filepath" "strings" "time" "hnyfkj.com.cn/rtu/linux/baseapp" ) const cbdLocalRPCPort = 7200 func downloadCBDAppLog(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } filename := fmt.Sprintf("cbd.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", "/home/root/yfkj-user-app/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 cbdUnlockHandler(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } if !checkOperationPassword(w, r) { return } w.WriteHeader(http.StatusOK) } func cbdWriteMCUDataHandler(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } var data json.RawMessage if err := json.NewDecoder(r.Body).Decode(&data); err != nil { http.Error(w, "参数错误: "+err.Error(), http.StatusBadRequest) return } data = json.RawMessage(bytes.TrimSpace(data)) if len(data) == 0 || len(data) > 512 || !json.Valid(data) { http.Error(w, "参数错误: 指令不能为空或超过512字节", http.StatusBadRequest) return } if err := callRPCResult(r.Context(), cbdLocalRPCPort, "core.writeMCUData", data, nil); err != nil { http.Error(w, "数据下发失败: "+err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/json; charset=utf-8") _ = json.NewEncoder(w).Encode(map[string]string{"result": "success"}) } func cbdUploadFirmwareHandler(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } hwType := strings.ToLower(r.URL.Query().Get("type")) if hwType != "hmi" && hwType != "mcu" { http.Error(w, "无效的升级类型, 只支持 hmi 或 mcu", http.StatusBadRequest) return } r.Body = http.MaxBytesReader(w, r.Body, 100<<20) // 100MB mr, err := r.MultipartReader() if err != nil { http.Error(w, "解析上传请求失败: "+err.Error(), http.StatusBadRequest) return } foundFile := false for { part, err := mr.NextPart() if err == io.EOF { break } if err != nil { http.Error(w, "读取上传数据失败: "+err.Error(), http.StatusBadRequest) return } if part.FormName() != "file" { part.Close() continue } foundFile = true filename := filepath.Base(part.FileName()) if filename == "" || filename == "." { part.Close() http.Error(w, "文件名为空", http.StatusBadRequest) return } suffix := ".rbl" rpcMethod := "core.doMCUUpgrade" if hwType == "hmi" { suffix = ".zip" rpcMethod = "core.doMCUUpgrade" } if !strings.HasSuffix(strings.ToLower(filename), suffix) { part.Close() http.Error(w, fmt.Sprintf("固件类型错误: %s 升级只允许上传 %s 类型的文件", strings.ToUpper(hwType), suffix), http.StatusBadRequest) return } path := filepath.Join("/tmp", filename) err = saveUploadFile(part, path) part.Close() if err != nil { http.Error(w, "保存上传文件失败: "+err.Error(), http.StatusInternalServerError) return } size := int64(0) if stat, err := os.Stat(path); err == nil { size = stat.Size() } baseapp.Logger.Infof( "[上传文件成功] 上传文件名: %s, 上传文件大小: %d 字节, 本地存储路径: %s", filename, size, path) if err := callRPCResult(r.Context(), cbdLocalRPCPort, rpcMethod, map[string]string{"filePath": path}, nil); err != nil { http.Error(w, "执行固件升级失败: "+err.Error(), http.StatusInternalServerError) return } break } if !foundFile { http.Error(w, "未找到上传文件", http.StatusBadRequest) return } w.Header().Set("Content-Type", "application/json; charset=utf-8") json.NewEncoder(w).Encode(map[string]string{"result": "success"}) } func cbdServerCfgHandler(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: if !checkOperationPassword(w, r) { return } callRPCResponse(w, r, cbdLocalRPCPort, "core.getServerCfg", nil) case http.MethodPost: if !checkOperationPassword(w, r) { return } var params json.RawMessage if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil { http.Error(w, "服务器配置无效: "+err.Error(), http.StatusBadRequest) return } callRPCResponse(w, r, cbdLocalRPCPort, "core.setServerCfg", params) default: http.Error(w, "method not allowed", http.StatusMethodNotAllowed) } } func cbdRunStatusHandler(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } callRPCResponse(w, r, cbdLocalRPCPort, "core.getRunStatus", nil) }