mirror of
https://github.com/gazer-x/komari.git
synced 2026-06-22 08:15:56 +08:00
9f7758f1da
- Removed legacy session management code from `session.go` and `sqlite3.go`. - Introduced new `accounts` package for user and session management. - Implemented GORM for database interactions, replacing raw SQL queries. - Created models for `User`, `Session`, `Client`, `History`, and other entities. - Added functions for creating, retrieving, and deleting sessions. - Implemented automatic cleanup of old history records. - Updated `main.go` to initialize the database and create a default admin account. - Enhanced error handling and logging throughout the database operations.
25 lines
438 B
Go
25 lines
438 B
Go
package utils
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"math/rand"
|
|
"time"
|
|
)
|
|
|
|
func GenerateRandomString(length int) string {
|
|
b := make([]byte, length)
|
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
_, err := r.Read(b)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return base64.URLEncoding.EncodeToString(b)[:length]
|
|
}
|
|
|
|
func GeneratePassword() string {
|
|
return GenerateRandomString(12)
|
|
}
|
|
|
|
func GenerateToken() string {
|
|
return GenerateRandomString(16)
|
|
} |