Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
Vanessa 2022-07-21 22:23:25 +08:00
commit 369062f990
5 changed files with 64 additions and 6 deletions

View file

@ -252,6 +252,28 @@ func importRepoKey(c *gin.Context) {
}
}
func initRepoKeyFromPassphrase(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)
arg, ok := util.JsonArg(c, ret)
if !ok {
return
}
pass := arg["pass"].(string)
if err := model.InitRepoKeyFromPassphrase(pass); nil != err {
ret.Code = -1
ret.Msg = fmt.Sprintf(model.Conf.Language(137), err)
ret.Data = map[string]interface{}{"closeTimeout": 5000}
return
}
ret.Data = map[string]interface{}{
"key": model.Conf.Repo.Key,
}
}
func initRepoKey(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)

View file

@ -244,6 +244,7 @@ func ServeAPI(ginServer *gin.Engine) {
ginServer.Handle("POST", "/api/bazaar/getBazaarPackageREAME", model.CheckAuth, getBazaarPackageREAME)
ginServer.Handle("POST", "/api/repo/initRepoKey", model.CheckAuth, initRepoKey)
ginServer.Handle("POST", "/api/repo/InitRepoKeyFromPassphrase", model.CheckAuth, initRepoKeyFromPassphrase)
ginServer.Handle("POST", "/api/repo/resetRepo", model.CheckAuth, resetRepo)
ginServer.Handle("POST", "/api/repo/importRepoKey", model.CheckAuth, importRepoKey)
ginServer.Handle("POST", "/api/repo/createSnapshot", model.CheckAuth, createSnapshot)

View file

@ -39,7 +39,7 @@ require (
github.com/panjf2000/ants/v2 v2.5.0
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/radovskyb/watcher v1.0.7
github.com/siyuan-note/dejavu v0.0.0-20220721023258-cdb41813cb07
github.com/siyuan-note/dejavu v0.0.0-20220721132530-5b44d0d47c35
github.com/siyuan-note/encryption v0.0.0-20220713091850-5ecd92177b75
github.com/siyuan-note/eventbus v0.0.0-20220624162334-ca7c06dc771f
github.com/siyuan-note/filelock v0.0.0-20220720144616-011221f7e128
@ -87,7 +87,7 @@ require (
github.com/imdario/mergo v0.3.13 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/juju/errors v1.0.0 // indirect
github.com/klauspost/compress v1.15.8 // indirect
github.com/klauspost/compress v1.15.9 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/lucas-clemente/quic-go v0.28.0 // indirect
github.com/marten-seemann/qpack v0.2.1 // indirect

View file

@ -356,8 +356,8 @@ github.com/juju/testing v0.0.0-20191001232224-ce9dec17d28b/go.mod h1:63prj8cnj0t
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
github.com/klauspost/compress v1.15.8 h1:JahtItbkWjf2jzm/T+qgMxkP9EMHsqEUA6vCMGmXvhA=
github.com/klauspost/compress v1.15.8/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
github.com/klauspost/compress v1.15.9 h1:wKRjX6JRtDdrE9qwa4b/Cip7ACOshUI4smpCQanqjSY=
github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
@ -515,8 +515,8 @@ github.com/shurcooL/users v0.0.0-20180125191416-49c67e49c537/go.mod h1:QJTqeLYED
github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546/go.mod h1:TrYk7fJVaAttu97ZZKrO9UbRa8izdowaMIZcxYMbVaw=
github.com/shurcooL/webdavfs v0.0.0-20170829043945-18c3829fa133/go.mod h1:hKmq5kWdCj2z2KEozexVbfEZIWiTjhE0+UjmZgPqehw=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/siyuan-note/dejavu v0.0.0-20220721023258-cdb41813cb07 h1:ZFF3idb409zorwia8Fyng7WhUuSY5NQXYojzzyUh26I=
github.com/siyuan-note/dejavu v0.0.0-20220721023258-cdb41813cb07/go.mod h1:o+UdjzTM4t7gPFpbNUTPgCEVhEcFAHvonVZm6SeyJJs=
github.com/siyuan-note/dejavu v0.0.0-20220721132530-5b44d0d47c35 h1:LmW0bVwy6stkUtm7sHJqm8WcGYS+vyuhc7d8VlFlnYo=
github.com/siyuan-note/dejavu v0.0.0-20220721132530-5b44d0d47c35/go.mod h1:o+UdjzTM4t7gPFpbNUTPgCEVhEcFAHvonVZm6SeyJJs=
github.com/siyuan-note/encryption v0.0.0-20220713091850-5ecd92177b75 h1:Bi7/7f29LW+Fm0cHc0J1NO1cZqyJwljSWVmfOqVZgaE=
github.com/siyuan-note/encryption v0.0.0-20220713091850-5ecd92177b75/go.mod h1:H8fyqqAbp9XreANjeSbc72zEdFfKTXYN34tc1TjZwtw=
github.com/siyuan-note/eventbus v0.0.0-20220624162334-ca7c06dc771f h1:JMobMNZ7AqaKKyEK+WeWFhix/2TDQXgPZDajU00IybU=

View file

@ -18,6 +18,7 @@ package model
import (
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"errors"
"fmt"
@ -125,6 +126,40 @@ func ResetRepo() (err error) {
return
}
func InitRepoKeyFromPassphrase(passphrase string) (err error) {
util.PushMsg(Conf.Language(136), 3000)
if err = os.RemoveAll(Conf.Repo.GetSaveDir()); nil != err {
return
}
if err = os.MkdirAll(Conf.Repo.GetSaveDir(), 0755); nil != err {
return
}
passphrase = gulu.Str.RemoveInvisible(passphrase)
passphrase = strings.TrimSpace(passphrase)
if "" == passphrase {
return errors.New(Conf.Language(142))
}
salt := fmt.Sprintf("%x", sha256.Sum256([]byte(passphrase)))[:16]
key, err := encryption.KDF(passphrase, salt)
if nil != err {
logging.LogErrorf("init data repo key failed: %s", err)
return
}
Conf.Repo.Key = key
Conf.Save()
time.Sleep(1 * time.Second)
util.PushMsg(Conf.Language(138), 3000)
time.Sleep(1 * time.Second)
if initErr := IndexRepo("[Init] Init data repo"); nil != initErr {
util.PushErrMsg(fmt.Sprintf(Conf.Language(140), initErr), 0)
}
return
}
func InitRepoKey() (err error) {
util.PushMsg(Conf.Language(136), 3000)