github.com/TeaOSLab/EdgeNode@v1.3.8/internal/waf/allow_cookie_info.go (about)

     1  // Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
     2  
     3  package waf
     4  
     5  import (
     6  	"encoding/base64"
     7  	"encoding/binary"
     8  	"errors"
     9  	"github.com/TeaOSLab/EdgeNode/internal/utils"
    10  )
    11  
    12  type AllowCookieInfo struct {
    13  	SetId     int64
    14  	ExpiresAt int64
    15  }
    16  
    17  func (this *AllowCookieInfo) Encode() (string, error) {
    18  	if this.SetId < 0 {
    19  		this.SetId = 0
    20  	}
    21  	if this.ExpiresAt < 0 {
    22  		this.ExpiresAt = 0
    23  	}
    24  
    25  	var result = make([]byte, 16)
    26  	binary.BigEndian.PutUint64(result, uint64(this.SetId))
    27  	binary.BigEndian.PutUint64(result[8:], uint64(this.ExpiresAt))
    28  	return base64.StdEncoding.EncodeToString(utils.SimpleEncrypt(result)), nil
    29  }
    30  
    31  func (this *AllowCookieInfo) Decode(encodedString string) error {
    32  	data, err := base64.StdEncoding.DecodeString(encodedString)
    33  	if err != nil {
    34  		return err
    35  	}
    36  
    37  	var result = utils.SimpleDecrypt(data)
    38  	if len(result) != 16 {
    39  		return errors.New("unexpected data length")
    40  	}
    41  
    42  	this.SetId = int64(binary.BigEndian.Uint64(result[:8]))
    43  	this.ExpiresAt = int64(binary.BigEndian.Uint64(result[8:16]))
    44  
    45  	return nil
    46  }