Skip to content

Commit

Permalink
Remove dups from token usage history
Browse files Browse the repository at this point in the history
  • Loading branch information
Jipok committed Mar 29, 2024
1 parent 02b363b commit ee93cdf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
11 changes: 7 additions & 4 deletions auth-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func handleLogout(w http.ResponseWriter, req *http.Request) {
tokenInfo := tmp.(Token_Info)
log.Printf(blue("User `%s` logged out. Token: %s"), tokenInfo.username, cookie.Value)
tokens.Delete(cookie.Value)
// Important information, save now
go saveTokens()
}
}
// MaxAge: -1 mean deleting cookie
Expand Down Expand Up @@ -248,13 +250,14 @@ func buildAuthHandler(handler http.Handler) http.Handler {
}
// Check useragent and IP address change
ip := strings.Split(req.RemoteAddr, ":")[0]
lastEntry := tokenInfo.history[len(tokenInfo.history)-1]
if (lastEntry.ip != ip) || (lastEntry.useragent != req.UserAgent()) {
tokenInfo.history = append(tokenInfo.history, Token_Usage_History{
history_key := ip + " " + req.UserAgent()
_, history_found := tokenInfo.history[history_key]
if !history_found {
tokenInfo.history[history_key] = Token_Usage_History{
time: time.Now().Unix(),
ip: ip,
useragent: req.UserAgent(),
})
}
tokens.Store(token, tokenInfo)
// Important information, save now
go saveTokens()
Expand Down
12 changes: 7 additions & 5 deletions tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ type Token_Info struct {
// Updated to the maximum(cfg.MaxNonActiveTime) value on any use of the token
countdown int
username string
history []Token_Usage_History
history map[string]Token_Usage_History
}

type Token_Usage_History struct {
time int64
time int64 // First usage
ip string
useragent string
}
Expand Down Expand Up @@ -70,7 +70,8 @@ func newToken(username string, ip string, useragent string) string {
ip: ip,
useragent: useragent,
}
history := []Token_Usage_History{hEntry}
history_key := ip + " " + useragent
history := map[string]Token_Usage_History{history_key: hEntry}
tokenInfo := Token_Info{
username: username,
countdown: cfg.MaxNonActiveTime,
Expand Down Expand Up @@ -158,7 +159,7 @@ func loadTokens() error {
log.Fatal(err)
}
// Parse token history. Each entry starts with tab
history := []Token_Usage_History{}
history := map[string]Token_Usage_History{}
var hEntry Token_Usage_History
for (i+1 < len(lines)) && (len(lines[i+1]) > 0) && (lines[i+1][0] == '\t') {
i += 1
Expand All @@ -174,7 +175,8 @@ func loadTokens() error {
}
hEntry.ip = parts[2]
hEntry.useragent = parts[3]
history = append(history, hEntry)
history_key := hEntry.ip + " " + hEntry.useragent
history[history_key] = hEntry
}
// Drop token for deleted user
in_tg := false
Expand Down

0 comments on commit ee93cdf

Please sign in to comment.