Browse Source

加密读写接口实现

master
刘子旺 7 months ago
parent
commit
89f84204e6
  1. 2
      godo/cmd/main.go
  2. 23
      godo/files/fs.go
  3. 16
      godo/files/os.go
  4. 56
      godo/files/pwdfile.go

2
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)

23
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

16
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)
}

56
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", "")
}

Loading…
Cancel
Save