人机识别验证成功后记录到Cookie,以便于在重启、切换节点时仍能恢复验证状态

This commit is contained in:
刘祥超
2024-04-11 16:14:14 +08:00
parent b1151479e0
commit dc9cff541e
4 changed files with 136 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
// Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
package waf
import (
"encoding/base64"
"encoding/binary"
"errors"
"github.com/TeaOSLab/EdgeNode/internal/utils"
)
type AllowCookieInfo struct {
SetId int64
ExpiresAt int64
}
func (this *AllowCookieInfo) Encode() (string, error) {
if this.SetId < 0 {
this.SetId = 0
}
if this.ExpiresAt < 0 {
this.ExpiresAt = 0
}
var result = make([]byte, 16)
binary.BigEndian.PutUint64(result, uint64(this.SetId))
binary.BigEndian.PutUint64(result[8:], uint64(this.ExpiresAt))
return base64.StdEncoding.EncodeToString(utils.SimpleEncrypt(result)), nil
}
func (this *AllowCookieInfo) Decode(encodedString string) error {
data, err := base64.StdEncoding.DecodeString(encodedString)
if err != nil {
return err
}
var result = utils.SimpleDecrypt(data)
if len(result) != 16 {
return errors.New("unexpected data length")
}
this.SetId = int64(binary.BigEndian.Uint64(result[:8]))
this.ExpiresAt = int64(binary.BigEndian.Uint64(result[8:16]))
return nil
}