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:
Diffstat (limited to 'web/global')
-rw-r--r--web/global/global.go13
-rw-r--r--web/global/hashStorage.go18
2 files changed, 23 insertions, 8 deletions
diff --git a/web/global/global.go b/web/global/global.go
index e92c375b..025fa081 100644
--- a/web/global/global.go
+++ b/web/global/global.go
@@ -1,3 +1,4 @@
+// Package global provides global variables and interfaces for accessing web and subscription servers.
package global
import (
@@ -12,27 +13,33 @@ var (
subServer SubServer
)
+// WebServer interface defines methods for accessing the web server instance.
type WebServer interface {
- GetCron() *cron.Cron
- GetCtx() context.Context
+ GetCron() *cron.Cron // Get the cron scheduler
+ GetCtx() context.Context // Get the server context
}
+// SubServer interface defines methods for accessing the subscription server instance.
type SubServer interface {
- GetCtx() context.Context
+ GetCtx() context.Context // Get the server context
}
+// SetWebServer sets the global web server instance.
func SetWebServer(s WebServer) {
webServer = s
}
+// GetWebServer returns the global web server instance.
func GetWebServer() WebServer {
return webServer
}
+// SetSubServer sets the global subscription server instance.
func SetSubServer(s SubServer) {
subServer = s
}
+// GetSubServer returns the global subscription server instance.
func GetSubServer() SubServer {
return subServer
}
diff --git a/web/global/hashStorage.go b/web/global/hashStorage.go
index 5d8135ee..962493f7 100644
--- a/web/global/hashStorage.go
+++ b/web/global/hashStorage.go
@@ -8,18 +8,21 @@ import (
"time"
)
+// HashEntry represents a stored hash entry with its value and timestamp.
type HashEntry struct {
- Hash string
- Value string
- Timestamp time.Time
+ Hash string // MD5 hash string
+ Value string // Original value
+ Timestamp time.Time // Time when the hash was created
}
+// HashStorage provides thread-safe storage for hash-value pairs with expiration.
type HashStorage struct {
sync.RWMutex
- Data map[string]HashEntry
- Expiration time.Duration
+ Data map[string]HashEntry // Map of hash to entry
+ Expiration time.Duration // Expiration duration for entries
}
+// NewHashStorage creates a new HashStorage instance with the specified expiration duration.
func NewHashStorage(expiration time.Duration) *HashStorage {
return &HashStorage{
Data: make(map[string]HashEntry),
@@ -27,6 +30,7 @@ func NewHashStorage(expiration time.Duration) *HashStorage {
}
}
+// SaveHash generates an MD5 hash for the given query string and stores it with a timestamp.
func (h *HashStorage) SaveHash(query string) string {
h.Lock()
defer h.Unlock()
@@ -45,6 +49,7 @@ func (h *HashStorage) SaveHash(query string) string {
return md5HashString
}
+// GetValue retrieves the original value for the given hash, returning true if found.
func (h *HashStorage) GetValue(hash string) (string, bool) {
h.RLock()
defer h.RUnlock()
@@ -54,11 +59,13 @@ func (h *HashStorage) GetValue(hash string) (string, bool) {
return entry.Value, exists
}
+// IsMD5 checks if the given string is a valid 32-character MD5 hash.
func (h *HashStorage) IsMD5(hash string) bool {
match, _ := regexp.MatchString("^[a-f0-9]{32}$", hash)
return match
}
+// RemoveExpiredHashes removes all hash entries that have exceeded the expiration duration.
func (h *HashStorage) RemoveExpiredHashes() {
h.Lock()
defer h.Unlock()
@@ -72,6 +79,7 @@ func (h *HashStorage) RemoveExpiredHashes() {
}
}
+// Reset clears all stored hash entries.
func (h *HashStorage) Reset() {
h.Lock()
defer h.Unlock()