-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a memory session to retain tested indexes across requests
- Loading branch information
1 parent
67b921a
commit 5a50686
Showing
4 changed files
with
82 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package store | ||
|
||
import ( | ||
"m3u-stream-merger/utils" | ||
"net/http" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type Session struct { | ||
ID string | ||
CreatedAt time.Time | ||
TestedIndexes []int | ||
} | ||
|
||
var sessionStore = struct { | ||
sync.RWMutex | ||
sessions map[string]Session | ||
}{sessions: make(map[string]Session)} | ||
|
||
func GetOrCreateSession(r *http.Request) Session { | ||
fingerprint := utils.GenerateFingerprint(r) | ||
|
||
sessionStore.RLock() | ||
session, exists := sessionStore.sessions[fingerprint] | ||
sessionStore.RUnlock() | ||
if exists { | ||
return session | ||
} | ||
|
||
session = Session{ | ||
ID: fingerprint, | ||
CreatedAt: time.Now(), | ||
TestedIndexes: []int{}, | ||
} | ||
|
||
sessionStore.Lock() | ||
sessionStore.sessions[session.ID] = session | ||
sessionStore.Unlock() | ||
|
||
return session | ||
} | ||
|
||
func (s *Session) SetTestedIndexes(indexes []int) { | ||
s.TestedIndexes = indexes | ||
|
||
sessionStore.Lock() | ||
sessionStore.sessions[s.ID] = *s | ||
sessionStore.Unlock() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package utils | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
func GenerateFingerprint(r *http.Request) string { | ||
// Collect relevant attributes | ||
ip := r.RemoteAddr | ||
if xff := r.Header.Get("X-Forwarded-For"); xff != "" { | ||
ip = xff | ||
} | ||
userAgent := r.Header.Get("User-Agent") | ||
accept := r.Header.Get("Accept") | ||
acceptLang := r.Header.Get("Accept-Language") | ||
|
||
// Combine into a single string | ||
data := fmt.Sprintf("%s|%s|%s|%s", ip, userAgent, accept, acceptLang) | ||
|
||
// Hash the string for a compact, fixed-length identifier | ||
hash := sha256.Sum256([]byte(data)) | ||
return hex.EncodeToString(hash[:]) | ||
} |