4200f90159
Go 控制面(SQLite + REST API)嵌入 Mihomo 数据面,Vue 3 多语言 Web 管理台。 含订阅/节点/规则/出站/监控/日志、OpenAPI、API Key、Gitea Actions CI(runs-on: docker) 与开源文档;镜像发布至 git.rc707blog.top Container Registry。 Co-authored-by: Cursor <cursoragent@cursor.com>
36 lines
787 B
Go
36 lines
787 B
Go
package api
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func issueToken(secret string, ttl time.Duration) string {
|
|
exp := time.Now().Add(ttl).Unix()
|
|
return fmt.Sprintf("%d.%s", exp, signToken(secret, exp))
|
|
}
|
|
|
|
func verifyToken(secret, token string) bool {
|
|
parts := strings.SplitN(token, ".", 2)
|
|
if len(parts) != 2 {
|
|
return false
|
|
}
|
|
exp, err := strconv.ParseInt(parts[0], 10, 64)
|
|
if err != nil || time.Now().Unix() > exp {
|
|
return false
|
|
}
|
|
expected := signToken(secret, exp)
|
|
return hmac.Equal([]byte(parts[1]), []byte(expected))
|
|
}
|
|
|
|
func signToken(secret string, exp int64) string {
|
|
mac := hmac.New(sha256.New, []byte(secret))
|
|
_, _ = mac.Write([]byte(strconv.FormatInt(exp, 10)))
|
|
return hex.EncodeToString(mac.Sum(nil))
|
|
}
|