diff --git a/auth-handler.go b/auth-handler.go index 3b63824..f26d44e 100644 --- a/auth-handler.go +++ b/auth-handler.go @@ -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 @@ -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() diff --git a/tokens.go b/tokens.go index 9ed6952..1b60011 100644 --- a/tokens.go +++ b/tokens.go @@ -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 } @@ -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, @@ -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 @@ -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