alist/server/check.go

81 lines
1.7 KiB
Go
Raw Normal View History

2021-10-27 14:45:36 +00:00
package server
import (
"fmt"
2021-11-17 14:19:11 +00:00
"github.com/Xhofe/alist/conf"
2021-10-27 14:45:36 +00:00
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
2021-11-13 07:53:26 +00:00
"github.com/gin-gonic/gin"
2021-11-17 14:19:11 +00:00
log "github.com/sirupsen/logrus"
2021-10-27 14:45:36 +00:00
"gorm.io/gorm"
"path/filepath"
2021-10-27 14:45:36 +00:00
)
2021-11-13 07:53:26 +00:00
func Auth(c *gin.Context) {
token := c.GetHeader("Authorization")
2021-10-27 14:45:36 +00:00
password, err := model.GetSettingByKey("password")
if err != nil {
if err == gorm.ErrRecordNotFound {
2021-11-13 07:53:26 +00:00
ErrorResp(c, fmt.Errorf("password not set"), 400)
return
2021-10-27 14:45:36 +00:00
}
2021-11-13 07:53:26 +00:00
ErrorResp(c, err, 500)
return
2021-10-27 14:45:36 +00:00
}
if token != utils.GetMD5Encode(password.Value) {
2021-11-13 07:53:26 +00:00
ErrorResp(c, fmt.Errorf("wrong password"), 401)
return
2021-10-27 14:45:36 +00:00
}
2021-11-13 07:53:26 +00:00
c.Next()
2021-10-27 14:45:36 +00:00
}
2021-10-28 04:37:31 +00:00
2021-11-13 07:53:26 +00:00
func Login(c *gin.Context) {
SuccessResp(c)
2021-10-29 16:35:29 +00:00
}
2021-11-13 07:53:26 +00:00
func CheckAccount(c *gin.Context) {
2021-10-28 04:37:31 +00:00
if model.AccountsCount() == 0 {
2021-11-13 07:53:26 +00:00
ErrorResp(c, fmt.Errorf("no accounts,please add one first"), 1001)
return
2021-10-28 04:37:31 +00:00
}
2021-11-13 07:53:26 +00:00
c.Next()
}
func CheckParent(path string, password string) bool {
meta, err := model.GetMetaByPath(path)
if err == nil {
if meta.Password != "" && meta.Password != password {
return false
}
return true
} else {
2021-11-23 08:22:23 +00:00
if path == "/" || path == "\\" {
return true
}
return CheckParent(filepath.Dir(path), password)
}
}
2021-11-17 14:19:11 +00:00
func CheckDownLink(path string, passwordMd5 string) bool {
if !conf.CheckDown {
return true
}
meta, err := model.GetMetaByPath(path)
log.Debugf("check down path: %s", path)
if err == nil {
log.Debugf("check down link: %s,%s", meta.Password, passwordMd5)
if meta.Password != "" && utils.Get16MD5Encode(meta.Password) != passwordMd5 {
return false
}
return true
} else {
if !conf.CheckParent {
return true
}
2021-11-23 08:22:23 +00:00
if path == "/" || path == "\\" {
2021-11-17 14:19:11 +00:00
return true
}
return CheckDownLink(filepath.Dir(path), passwordMd5)
}
}