diff --git a/godo/cmd/main.go b/godo/cmd/main.go index c10e000..7bef3f6 100644 --- a/godo/cmd/main.go +++ b/godo/cmd/main.go @@ -101,6 +101,8 @@ func OsStart() { fileRouter.HandleFunc("/unzip", files.HandleUnZip).Methods(http.MethodGet) fileRouter.HandleFunc("/watch", files.WatchHandler).Methods(http.MethodGet) fileRouter.HandleFunc("/setfilepwd", files.HandleSetFilePwd).Methods(http.MethodGet) + fileRouter.HandleFunc("/changefilepwd", files.HandleChangeFilePwd).Methods(http.MethodGet) + fileRouter.HandleFunc("/changeisPwd", files.HandleSetIsPwd).Methods(http.MethodGet) localchatRouter := router.PathPrefix("/localchat").Subrouter() localchatRouter.HandleFunc("/message", localchat.HandleMessage).Methods(http.MethodPost) diff --git a/godo/files/fs.go b/godo/files/fs.go index d5408a5..cce2530 100644 --- a/godo/files/fs.go +++ b/godo/files/fs.go @@ -315,9 +315,8 @@ func HandleCopyFile(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(res) } -// HandleWriteFile writes content to a file +// 带加密写 func HandleWriteFile(w http.ResponseWriter, r *http.Request) { - // basepath = "/Users/sujia/.godoos/os" filePath := r.URL.Query().Get("filePath") basePath, err := libs.GetOsDir() if err != nil { @@ -346,12 +345,27 @@ func HandleWriteFile(w http.ResponseWriter, r *http.Request) { } defer file.Close() - // 内容为空直接返回,不为空则加密 + // 内容为空直接返回 if len(filedata) == 0 { CheckAddDesktop(filePath) libs.SuccessMsg(w, "", "success") return } + + // 判读是否加密 + ispwd := GetPwdFlag() + + // 没有加密写入明文 + if ispwd == 0 { + _, err := io.Copy(file, fileContent) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + CheckAddDesktop(filePath) + libs.SuccessMsg(w, "", "success") + return + } // 加密 data, err := libs.EncryptData(filedata, libs.EncryptionKey) if err != nil { @@ -365,8 +379,7 @@ func HandleWriteFile(w http.ResponseWriter, r *http.Request) { } // 判断下是否添加到桌面上 CheckAddDesktop(filePath) - res := libs.APIResponse{Message: fmt.Sprintf("File '%s' successfully written.", filePath)} - json.NewEncoder(w).Encode(res) + libs.SuccessMsg(w, "", "success") } // HandleAppendFile appends content to a file diff --git a/godo/files/os.go b/godo/files/os.go index 320a240..6e263f0 100644 --- a/godo/files/os.go +++ b/godo/files/os.go @@ -339,7 +339,7 @@ func CheckDeleteDesktop(filePath string) error { // 校验文件密码 func CheckFilePwd(fpwd, salt string) bool { pwd := libs.HashPassword(fpwd, salt) - oldpwd, err := libs.GetConfig("filepwd") + oldpwd, err := libs.GetConfig("filePwd") if !err { return false } @@ -365,3 +365,17 @@ func GetSalt(r *http.Request) string { return salt } } + +// 获取密码标识位,没有添加上 +func GetPwdFlag() int { + isPwd, has := libs.GetConfig("isPwd") + if !has { + req := libs.ReqBody{ + Name: "isPwd", + Value: 0, + } + libs.SetConfig(req) + libs.SaveConfig() + } + return isPwd.(int) +} diff --git a/godo/files/pwdfile.go b/godo/files/pwdfile.go index a52fbda..45422df 100644 --- a/godo/files/pwdfile.go +++ b/godo/files/pwdfile.go @@ -5,33 +5,24 @@ import ( "encoding/json" "godo/libs" "net/http" + "strconv" "strings" ) -// 带加密读 +// 加密读 func HandleReadFile(w http.ResponseWriter, r *http.Request) { + // 初始值 path := r.URL.Query().Get("path") fPwd := r.Header.Get("fPwd") - hasPwd := IsHavePwd(fPwd) - - // 获取salt值 salt := GetSalt(r) - + hasPwd := GetPwdFlag() // 校验文件路径 if err := validateFilePath(path); err != nil { libs.HTTPError(w, http.StatusBadRequest, err.Error()) return } - // 有密码校验密码 - if hasPwd { - if !CheckFilePwd(fPwd, salt) { - libs.HTTPError(w, http.StatusBadRequest, "密码错误") - return - } - } - // 获取文件路径 basePath, err := libs.GetOsDir() if err != nil { @@ -45,14 +36,31 @@ func HandleReadFile(w http.ResponseWriter, r *http.Request) { return } - // 解密 - data, err := libs.DecryptData(fileContent, libs.EncryptionKey) + // 没有加密 base64明文传输 + if hasPwd == 0 { + data := string(fileContent) + if !strings.HasPrefix(data, "link::") { + data = base64.StdEncoding.EncodeToString(fileContent) + resp := libs.APIResponse{Message: "success", Data: data} + json.NewEncoder(w).Encode(resp) + return + } + } + + // 有加密,先校验密码,再解密 + if !CheckFilePwd(fPwd, salt) { + libs.HTTPError(w, http.StatusBadRequest, "密码错误") + return + } + + var data []byte + fileContent, err = libs.DecryptData(fileContent, libs.EncryptionKey) if err != nil { libs.HTTPError(w, http.StatusInternalServerError, err.Error()) return } - content := string(data) + content := string(fileContent) // 检查文件内容是否以"link::"开头 if !strings.HasPrefix(content, "link::") { content = base64.StdEncoding.EncodeToString(data) @@ -60,7 +68,6 @@ func HandleReadFile(w http.ResponseWriter, r *http.Request) { // 初始响应 res := libs.APIResponse{Code: 0, Message: "success", Data: content} - json.NewEncoder(w).Encode(res) } @@ -68,6 +75,7 @@ func HandleReadFile(w http.ResponseWriter, r *http.Request) { func HandleSetFilePwd(w http.ResponseWriter, r *http.Request) { fPwd := r.Header.Get("filepPwd") salt := r.Header.Get("salt") + // 服务端再hash加密 hashPwd := libs.HashPassword(fPwd, salt) @@ -104,3 +112,17 @@ func HandleChangeFilePwd(w http.ResponseWriter, r *http.Request) { libs.SetConfig(pwdReq) libs.SuccessMsg(w, "success", "The file password change success!") } + +// 更改加密状态 +func HandleSetIsPwd(w http.ResponseWriter, r *http.Request) { + isPwd := r.URL.Query().Get("ispwd") + // 0非加密机器 1加密机器 + isPwdValue, _ := strconv.Atoi(isPwd) + pwdReq := libs.ReqBody{ + Name: "isPwd", + Value: isPwdValue, + } + libs.SetConfig(pwdReq) + libs.SaveConfig() + libs.SuccessMsg(w, "success", "") +}