From 8c5648eb09b0d6d1479f102156e8a548ed478caa Mon Sep 17 00:00:00 2001 From: Hamidreza Ghavami <70919649+hamid-gh98@users.noreply.github.com> Date: Sat, 20 May 2023 20:29:28 +0430 Subject: FIX callback query and BUTTON_DATA_INVALID error with hashStorage --- web/global/hashStorage.go | 91 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 web/global/hashStorage.go (limited to 'web/global/hashStorage.go') diff --git a/web/global/hashStorage.go b/web/global/hashStorage.go new file mode 100644 index 00000000..099a54ed --- /dev/null +++ b/web/global/hashStorage.go @@ -0,0 +1,91 @@ +package global + +import ( + "crypto/md5" + "encoding/hex" + "sync" + "time" +) + +type HashEntry struct { + Hash string + Value string + Timestamp time.Time +} + +type HashStorage struct { + sync.RWMutex + Data map[string]HashEntry + Expiration time.Duration + ForceSave bool +} + +func NewHashStorage(expiration time.Duration, forceSave bool) *HashStorage { + return &HashStorage{ + Data: make(map[string]HashEntry), + Expiration: expiration, + ForceSave: forceSave, + } +} + +func (h *HashStorage) AddHash(query string) string { + if h.ForceSave { + return h.saveValue(query) + } + + // we only need to hash for more than 64 chars by default + if len(query) <= 64 { + return query + } + + return h.saveValue(query) +} + +func (h *HashStorage) saveValue(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 { + h.RLock() + defer h.RUnlock() + + entry, exists := h.Data[hash] + if !exists { + return hash + } + return entry.Value +} + +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) +} -- cgit v1.2.3 From 786a3ac992043e1e1ca398d4b408e34d9ae46973 Mon Sep 17 00:00:00 2001 From: Hamidreza Ghavami <70919649+hamid-gh98@users.noreply.github.com> Date: Sat, 20 May 2023 21:46:42 +0430 Subject: FIX hashStorage --- web/global/hashStorage.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'web/global/hashStorage.go') diff --git a/web/global/hashStorage.go b/web/global/hashStorage.go index 099a54ed..3d5a6a0d 100644 --- a/web/global/hashStorage.go +++ b/web/global/hashStorage.go @@ -3,8 +3,10 @@ package global import ( "crypto/md5" "encoding/hex" + "regexp" "sync" "time" + "x-ui/util/common" ) type HashEntry struct { @@ -59,15 +61,23 @@ func (h *HashStorage) saveValue(query string) string { return md5HashString } -func (h *HashStorage) GetValue(hash string) string { +func (h *HashStorage) GetValue(hash string) (string, error) { h.RLock() defer h.RUnlock() entry, exists := h.Data[hash] if !exists { - return hash + if h.isMD5(hash) { + return "", common.NewError("hash not found in storage!") + } + return hash, nil } - return entry.Value + return entry.Value, nil +} + +func (h *HashStorage) isMD5(hash string) bool { + match, _ := regexp.MatchString("^[a-f0-9]{32}$", hash) + return match } func (h *HashStorage) RemoveExpiredHashes() { -- cgit v1.2.3 From f82d0051b2cf827228b5c939d22622bd0d066149 Mon Sep 17 00:00:00 2001 From: Hamidreza Ghavami <70919649+hamid-gh98@users.noreply.github.com> Date: Sun, 21 May 2023 08:33:08 +0430 Subject: Update hashstorage functionality --- web/global/hashStorage.go | 35 ++++++++--------------------------- 1 file changed, 8 insertions(+), 27 deletions(-) (limited to 'web/global/hashStorage.go') diff --git a/web/global/hashStorage.go b/web/global/hashStorage.go index 3d5a6a0d..9dfea169 100644 --- a/web/global/hashStorage.go +++ b/web/global/hashStorage.go @@ -6,7 +6,6 @@ import ( "regexp" "sync" "time" - "x-ui/util/common" ) type HashEntry struct { @@ -19,31 +18,17 @@ type HashStorage struct { sync.RWMutex Data map[string]HashEntry Expiration time.Duration - ForceSave bool + } -func NewHashStorage(expiration time.Duration, forceSave bool) *HashStorage { +func NewHashStorage(expiration time.Duration) *HashStorage { return &HashStorage{ Data: make(map[string]HashEntry), Expiration: expiration, - ForceSave: forceSave, } } -func (h *HashStorage) AddHash(query string) string { - if h.ForceSave { - return h.saveValue(query) - } - - // we only need to hash for more than 64 chars by default - if len(query) <= 64 { - return query - } - - return h.saveValue(query) -} - -func (h *HashStorage) saveValue(query string) string { +func (h *HashStorage) SaveHash(query string) string { h.Lock() defer h.Unlock() @@ -61,21 +46,17 @@ func (h *HashStorage) saveValue(query string) string { return md5HashString } -func (h *HashStorage) GetValue(hash string) (string, error) { + +func (h *HashStorage) GetValue(hash string) (string, bool) { h.RLock() defer h.RUnlock() entry, exists := h.Data[hash] - if !exists { - if h.isMD5(hash) { - return "", common.NewError("hash not found in storage!") - } - return hash, nil - } - return entry.Value, nil + + return entry.Value, exists } -func (h *HashStorage) isMD5(hash string) bool { +func (h *HashStorage) IsMD5(hash string) bool { match, _ := regexp.MatchString("^[a-f0-9]{32}$", hash) return match } -- cgit v1.2.3