Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/MHSanaei/3x-ui.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHo3ein <ho3ein.sanaei@gmail.com>2023-05-22 10:56:14 +0300
committerGitHub <noreply@github.com>2023-05-22 10:56:14 +0300
commit5f489c3d08b36c9569fa67f0c5bf46be8ada7b21 (patch)
tree0241a998df1f52f5170aec9bc263414e8a2a3e96 /web/global/hashStorage.go
parent3d712890753a4f9981cca95685dec7ad4fff0acf (diff)
parentf82d0051b2cf827228b5c939d22622bd0d066149 (diff)
Merge pull request #491 from hamid-gh98/main
[tgbot] Multi language + More...
Diffstat (limited to 'web/global/hashStorage.go')
-rw-r--r--web/global/hashStorage.go82
1 files changed, 82 insertions, 0 deletions
diff --git a/web/global/hashStorage.go b/web/global/hashStorage.go
new file mode 100644
index 00000000..9dfea169
--- /dev/null
+++ b/web/global/hashStorage.go
@@ -0,0 +1,82 @@
+package global
+
+import (
+ "crypto/md5"
+ "encoding/hex"
+ "regexp"
+ "sync"
+ "time"
+)
+
+type HashEntry struct {
+ Hash string
+ Value string
+ Timestamp time.Time
+}
+
+type HashStorage struct {
+ sync.RWMutex
+ Data map[string]HashEntry
+ Expiration time.Duration
+
+}
+
+func NewHashStorage(expiration time.Duration) *HashStorage {
+ return &HashStorage{
+ Data: make(map[string]HashEntry),
+ Expiration: expiration,
+ }
+}
+
+func (h *HashStorage) SaveHash(query string) string {
+ h.Lock()
+ defer h.Unlock()
+
+ md5Hash := md5.Sum([]byte(query))
+ md5HashString := hex.EncodeToString(md5Hash[:])
+
+ entry := HashEntry{
+ Hash: md5HashString,
+ Value: query,
+ Timestamp: time.Now(),
+ }
+
+ h.Data[md5HashString] = entry
+
+ return md5HashString
+}
+
+
+func (h *HashStorage) GetValue(hash string) (string, bool) {
+ h.RLock()
+ defer h.RUnlock()
+
+ entry, exists := h.Data[hash]
+
+ return entry.Value, exists
+}
+
+func (h *HashStorage) IsMD5(hash string) bool {
+ match, _ := regexp.MatchString("^[a-f0-9]{32}$", hash)
+ return match
+}
+
+func (h *HashStorage) RemoveExpiredHashes() {
+ h.Lock()
+ defer h.Unlock()
+
+ now := time.Now()
+
+ for hash, entry := range h.Data {
+ if now.Sub(entry.Timestamp) > h.Expiration {
+ delete(h.Data, hash)
+ }
+ }
+}
+
+func (h *HashStorage) Reset() {
+ h.Lock()
+ defer h.Unlock()
+
+ h.Data = make(map[string]HashEntry)
+}