mirror of
https://github.com/jeessy2/ddns-go.git
synced 2025-10-21 07:53:45 +08:00
* feat: Add a login page * feat: Modify save rules, more secure * remove remoteAddr == "localhost" * "登录失败次数过多,请等待 %d 分钟后再试 * cookie remove secure * set cookie expires time by `NotAllowWanAccess` * prettier * fix: rename * feat: auto login if unfilled * feat: auto login if there is no username/password * auto login if no username/password
32 lines
658 B
Go
32 lines
658 B
Go
package util
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"fmt"
|
|
"math/rand"
|
|
"time"
|
|
)
|
|
|
|
// GenerateToken 生成Token
|
|
func GenerateToken(username string) string {
|
|
key := []byte(generateRandomKey())
|
|
h := hmac.New(sha256.New, key)
|
|
msg := fmt.Sprintf("%s%d", username, time.Now().Unix())
|
|
h.Write([]byte(msg))
|
|
return base64.StdEncoding.EncodeToString(h.Sum(nil))
|
|
}
|
|
|
|
// generateRandomKey 生成随机密钥
|
|
func generateRandomKey() string {
|
|
// 设置随机种子
|
|
source := rand.NewSource(time.Now().UnixNano())
|
|
random := rand.New(source)
|
|
|
|
// 生成随机的64位整数
|
|
randomNumber := random.Uint64()
|
|
|
|
return fmt.Sprint(randomNumber)
|
|
}
|