2023-02-09 22:48:06 +03:30
|
|
|
package session
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/gob"
|
2025-09-12 13:04:36 +02:00
|
|
|
"net/http"
|
2026-05-09 21:47:37 +02:00
|
|
|
"time"
|
2024-03-11 01:01:24 +03:30
|
|
|
|
2026-05-10 02:13:42 +02:00
|
|
|
"github.com/mhsanaei/3x-ui/v3/database/model"
|
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/logger"
|
2023-04-25 15:00:21 +03:30
|
|
|
|
2024-04-21 00:56:55 +03:30
|
|
|
"github.com/gin-contrib/sessions"
|
2023-02-09 22:48:06 +03:30
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
)
|
|
|
|
|
|
2024-08-06 13:44:48 +02:00
|
|
|
const (
|
2026-05-09 21:47:37 +02:00
|
|
|
loginUserKey = "LOGIN_USER"
|
|
|
|
|
apiAuthUserKey = "api_auth_user"
|
|
|
|
|
sessionCookieName = "3x-ui"
|
2024-08-06 13:44:48 +02:00
|
|
|
)
|
2023-02-09 22:48:06 +03:30
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
gob.Register(model.User{})
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-05 18:27:49 +03:00
|
|
|
func SetLoginUser(c *gin.Context, user *model.User) error {
|
2024-12-16 14:24:59 +01:00
|
|
|
if user == nil {
|
2026-05-05 18:27:49 +03:00
|
|
|
return nil
|
2024-12-16 14:24:59 +01:00
|
|
|
}
|
2023-02-09 22:48:06 +03:30
|
|
|
s := sessions.Default(c)
|
2024-12-16 14:24:59 +01:00
|
|
|
s.Set(loginUserKey, *user)
|
2026-05-05 18:27:49 +03:00
|
|
|
return s.Save()
|
2023-02-09 22:48:06 +03:30
|
|
|
}
|
|
|
|
|
|
2026-05-09 17:38:48 +02:00
|
|
|
func SetAPIAuthUser(c *gin.Context, user *model.User) {
|
|
|
|
|
if user == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.Set(apiAuthUserKey, user)
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 22:48:06 +03:30
|
|
|
func GetLoginUser(c *gin.Context) *model.User {
|
2026-05-09 17:38:48 +02:00
|
|
|
if v, ok := c.Get(apiAuthUserKey); ok {
|
|
|
|
|
if u, ok2 := v.(*model.User); ok2 {
|
|
|
|
|
return u
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-09 22:48:06 +03:30
|
|
|
s := sessions.Default(c)
|
2024-12-16 14:24:59 +01:00
|
|
|
obj := s.Get(loginUserKey)
|
2024-08-06 13:44:48 +02:00
|
|
|
if obj == nil {
|
|
|
|
|
return nil
|
2023-02-09 22:48:06 +03:30
|
|
|
}
|
2024-08-06 13:44:48 +02:00
|
|
|
user, ok := obj.(model.User)
|
|
|
|
|
if !ok {
|
2024-12-16 14:24:59 +01:00
|
|
|
s.Delete(loginUserKey)
|
2026-05-05 18:27:49 +03:00
|
|
|
if err := s.Save(); err != nil {
|
|
|
|
|
logger.Warning("session: failed to drop stale user payload:", err)
|
|
|
|
|
}
|
2024-08-06 13:44:48 +02:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return &user
|
2023-02-09 22:48:06 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func IsLogin(c *gin.Context) bool {
|
|
|
|
|
return GetLoginUser(c) != nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-05 18:27:49 +03:00
|
|
|
func ClearSession(c *gin.Context) error {
|
2023-02-09 22:48:06 +03:30
|
|
|
s := sessions.Default(c)
|
|
|
|
|
s.Clear()
|
2026-04-20 20:03:40 +02:00
|
|
|
cookiePath := c.GetString("base_path")
|
|
|
|
|
if cookiePath == "" {
|
|
|
|
|
cookiePath = "/"
|
|
|
|
|
}
|
2026-05-09 21:47:37 +02:00
|
|
|
secure := c.Request.TLS != nil
|
2023-02-09 22:48:06 +03:30
|
|
|
s.Options(sessions.Options{
|
2026-04-20 20:03:40 +02:00
|
|
|
Path: cookiePath,
|
2024-08-06 13:44:48 +02:00
|
|
|
MaxAge: -1,
|
|
|
|
|
HttpOnly: true,
|
2026-05-09 21:47:37 +02:00
|
|
|
Secure: secure,
|
2025-09-12 13:04:36 +02:00
|
|
|
SameSite: http.SameSiteLaxMode,
|
2023-02-09 22:48:06 +03:30
|
|
|
})
|
2026-05-09 21:47:37 +02:00
|
|
|
if err := s.Save(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if cookiePath != "/" {
|
|
|
|
|
http.SetCookie(c.Writer, &http.Cookie{
|
|
|
|
|
Name: sessionCookieName,
|
|
|
|
|
Value: "",
|
|
|
|
|
Path: "/",
|
|
|
|
|
MaxAge: -1,
|
|
|
|
|
Expires: time.Unix(0, 0),
|
|
|
|
|
HttpOnly: true,
|
|
|
|
|
Secure: secure,
|
|
|
|
|
SameSite: http.SameSiteLaxMode,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2023-02-09 22:48:06 +03:30
|
|
|
}
|