🎨配置文件的初始化
This commit is contained in:
58
component/captcha_store.go
Normal file
58
component/captcha_store.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package component
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
"wireguard-dashboard/client"
|
||||
"wireguard-dashboard/constant"
|
||||
)
|
||||
|
||||
type CaptchaStore struct{}
|
||||
|
||||
// Set
|
||||
// @description: 验证码放入指定存储
|
||||
// @receiver CaptchaStore
|
||||
// @param id
|
||||
// @param value
|
||||
// @return error
|
||||
func (CaptchaStore) Set(id string, value string) error {
|
||||
return client.Redis.Set(context.Background(), fmt.Sprintf("%s:%s", constant.Captcha, id), value, 2*time.Minute).Err()
|
||||
}
|
||||
|
||||
// Get
|
||||
// @description: 获取验证码信息
|
||||
// @receiver CaptchaStore
|
||||
// @param id
|
||||
// @param clear
|
||||
// @return string
|
||||
func (CaptchaStore) Get(id string, clear bool) string {
|
||||
val, err := client.Redis.Get(context.Background(), fmt.Sprintf("%s:%s", constant.Captcha, id)).Result()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
if clear {
|
||||
client.Redis.Del(context.Background(), fmt.Sprintf("%s:%s", constant.Captcha, id))
|
||||
return val
|
||||
}
|
||||
|
||||
return val
|
||||
}
|
||||
|
||||
// Verify
|
||||
// @description: 校验
|
||||
// @receiver CaptchaStore
|
||||
// @param id
|
||||
// @param answer
|
||||
// @param clear
|
||||
// @return bool
|
||||
func (c CaptchaStore) Verify(id, answer string, clear bool) bool {
|
||||
if os.Getenv("GIN_MODE") != "release" {
|
||||
return true
|
||||
}
|
||||
storeAnswer := c.Get(id, clear)
|
||||
return strings.ToUpper(answer) == strings.ToUpper(storeAnswer)
|
||||
}
|
91
component/jwt.go
Normal file
91
component/jwt.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package component
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"gitee.ltd/lxh/logger/log"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"strings"
|
||||
"time"
|
||||
"wireguard-dashboard/client"
|
||||
"wireguard-dashboard/constant"
|
||||
)
|
||||
|
||||
const Secret = "IK8MSs76Pb2VJxleTDadf1Wzu3h9QROLv0XtmnCUErYgBG5wAyjk4cioqFZHNpZG"
|
||||
|
||||
type JwtClaims struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
jwt.RegisteredClaims `json:"-"`
|
||||
}
|
||||
|
||||
func JWT() JwtClaims {
|
||||
return JwtClaims{}
|
||||
}
|
||||
|
||||
// GenerateToken
|
||||
// @description: 生成token
|
||||
// @receiver Jwt
|
||||
// @return token
|
||||
// @return err
|
||||
func (j JwtClaims) GenerateToken(userId string) (token string, err error) {
|
||||
claims := JwtClaims{
|
||||
ID: userId,
|
||||
RegisteredClaims: jwt.RegisteredClaims{
|
||||
Subject: "wireguard-dashboard",
|
||||
ExpiresAt: jwt.NewNumericDate(time.Now().Local().Add(7 * time.Hour)),
|
||||
NotBefore: jwt.NewNumericDate(time.Now().Local()),
|
||||
IssuedAt: jwt.NewNumericDate(time.Now().Local()),
|
||||
},
|
||||
}
|
||||
|
||||
t := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
token, err = t.SignedString([]byte(Secret))
|
||||
if err != nil {
|
||||
log.Errorf("生成token失败: %v", err.Error())
|
||||
return "", errors.New("生成token失败")
|
||||
}
|
||||
|
||||
client.Redis.Set(context.Background(), fmt.Sprintf("%s:%s", constant.Token, userId), token, 7*time.Hour)
|
||||
return
|
||||
}
|
||||
|
||||
// ParseToken
|
||||
// @description: 解析token
|
||||
// @receiver Jwt
|
||||
// @return Jwt
|
||||
// @return error
|
||||
func (JwtClaims) ParseToken(token string) (*JwtClaims, error) {
|
||||
tokenStr := strings.Split(token, "Bearer ")[1]
|
||||
|
||||
t, err := jwt.ParseWithClaims(tokenStr, &JwtClaims{}, func(token *jwt.Token) (any, error) {
|
||||
return []byte(Secret), nil
|
||||
})
|
||||
|
||||
if claims, ok := t.Claims.(*JwtClaims); ok && t.Valid {
|
||||
userToken, err := client.Redis.Get(context.Background(), fmt.Sprintf("%s:%s", constant.Token, claims.ID)).Result()
|
||||
if err != nil {
|
||||
log.Errorf("缓存中用户[%s]的token查找失败: %v", claims.ID, err.Error())
|
||||
return nil, errors.New("token不存在")
|
||||
}
|
||||
|
||||
if userToken != tokenStr {
|
||||
log.Errorf("token不一致")
|
||||
return nil, errors.New("token错误")
|
||||
}
|
||||
|
||||
return claims, nil
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// Logout
|
||||
// @description: 退出登陆
|
||||
// @receiver JwtClaims
|
||||
// @param userId
|
||||
// @return err
|
||||
func (j JwtClaims) Logout(userId string) (err error) {
|
||||
return client.Redis.Del(context.Background(), fmt.Sprintf("%s:%s", constant.Token, userId)).Err()
|
||||
}
|
33
component/wireguard.go
Normal file
33
component/wireguard.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package component
|
||||
|
||||
import (
|
||||
"gitee.ltd/lxh/logger/log"
|
||||
"wireguard-dashboard/utils"
|
||||
)
|
||||
|
||||
type wireguard struct{}
|
||||
|
||||
func Wireguard() wireguard {
|
||||
return wireguard{}
|
||||
}
|
||||
|
||||
// Apply
|
||||
// @description: 应用配置
|
||||
// @receiver wireguard
|
||||
// @return err
|
||||
func (wireguard) Apply(templateFilePath, configFilePath string, data any) (err error) {
|
||||
|
||||
parseTemplate, err := utils.Template().Parse(templateFilePath)
|
||||
if err != nil {
|
||||
log.Errorf("解析模板信息失败")
|
||||
return err
|
||||
}
|
||||
|
||||
err = utils.Template().Execute(parseTemplate, data, configFilePath)
|
||||
if err != nil {
|
||||
log.Errorf("应用配置失败: %v", err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user