2023-02-09 22:18:06 +03:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
"x-ui/logger"
|
|
|
|
"x-ui/web/service"
|
|
|
|
"x-ui/web/session"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LoginForm struct {
|
2023-04-21 18:30:14 +03:00
|
|
|
Username string `json:"username" form:"username"`
|
|
|
|
Password string `json:"password" form:"password"`
|
|
|
|
LoginSecret string `json:"loginSecret" form:"loginSecret"`
|
2023-02-09 22:18:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type IndexController struct {
|
|
|
|
BaseController
|
|
|
|
|
2023-04-21 18:30:14 +03:00
|
|
|
settingService service.SettingService
|
|
|
|
userService service.UserService
|
|
|
|
tgbot service.Tgbot
|
2023-02-09 22:18:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIndexController(g *gin.RouterGroup) *IndexController {
|
|
|
|
a := &IndexController{}
|
|
|
|
a.initRouter(g)
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *IndexController) initRouter(g *gin.RouterGroup) {
|
|
|
|
g.GET("/", a.index)
|
|
|
|
g.POST("/login", a.login)
|
|
|
|
g.GET("/logout", a.logout)
|
2023-04-21 18:30:14 +03:00
|
|
|
g.POST("/getSecretStatus", a.getSecretStatus)
|
2023-02-09 22:18:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *IndexController) index(c *gin.Context) {
|
|
|
|
if session.IsLogin(c) {
|
|
|
|
c.Redirect(http.StatusTemporaryRedirect, "xui/")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
html(c, "login.html", "pages.login.title", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *IndexController) login(c *gin.Context) {
|
|
|
|
var form LoginForm
|
|
|
|
err := c.ShouldBind(&form)
|
|
|
|
if err != nil {
|
2023-02-18 15:37:32 +03:00
|
|
|
pureJsonMsg(c, false, I18n(c, "pages.login.toasts.invalidFormData"))
|
2023-02-09 22:18:06 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if form.Username == "" {
|
|
|
|
pureJsonMsg(c, false, I18n(c, "pages.login.toasts.emptyUsername"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if form.Password == "" {
|
2023-02-18 15:37:32 +03:00
|
|
|
pureJsonMsg(c, false, I18n(c, "pages.login.toasts.emptyPassword"))
|
2023-02-09 22:18:06 +03:00
|
|
|
return
|
|
|
|
}
|
2023-04-21 18:30:14 +03:00
|
|
|
user := a.userService.CheckUser(form.Username, form.Password, form.LoginSecret)
|
2023-02-09 22:18:06 +03:00
|
|
|
timeStr := time.Now().Format("2006-01-02 15:04:05")
|
|
|
|
if user == nil {
|
2023-03-17 19:07:49 +03:00
|
|
|
a.tgbot.UserLoginNotify(form.Username, getRemoteIp(c), timeStr, 0)
|
2023-02-09 22:18:06 +03:00
|
|
|
logger.Infof("wrong username or password: \"%s\" \"%s\"", form.Username, form.Password)
|
2023-02-18 15:37:32 +03:00
|
|
|
pureJsonMsg(c, false, I18n(c, "pages.login.toasts.wrongUsernameOrPassword"))
|
2023-02-09 22:18:06 +03:00
|
|
|
return
|
|
|
|
} else {
|
|
|
|
logger.Infof("%s login success,Ip Address:%s\n", form.Username, getRemoteIp(c))
|
2023-03-17 19:07:49 +03:00
|
|
|
a.tgbot.UserLoginNotify(form.Username, getRemoteIp(c), timeStr, 1)
|
2023-04-25 14:30:21 +03:00
|
|
|
sessionMaxAge, err := a.settingService.GetSessionMaxAge()
|
|
|
|
if err != nil {
|
|
|
|
logger.Infof("Unable to get session's max age from DB")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = session.SetMaxAge(c, sessionMaxAge*60)
|
|
|
|
if err != nil {
|
|
|
|
logger.Infof("Unable to set session's max age")
|
|
|
|
}
|
|
|
|
|
2023-02-09 22:18:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
err = session.SetLoginUser(c, user)
|
|
|
|
logger.Info("user", user.Id, "login success")
|
2023-02-18 15:37:32 +03:00
|
|
|
jsonMsg(c, I18n(c, "pages.login.toasts.successLogin"), err)
|
2023-02-09 22:18:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *IndexController) logout(c *gin.Context) {
|
|
|
|
user := session.GetLoginUser(c)
|
|
|
|
if user != nil {
|
|
|
|
logger.Info("user", user.Id, "logout")
|
|
|
|
}
|
|
|
|
session.ClearSession(c)
|
|
|
|
c.Redirect(http.StatusTemporaryRedirect, c.GetString("base_path"))
|
|
|
|
}
|
2023-04-21 18:30:14 +03:00
|
|
|
|
|
|
|
func (a *IndexController) getSecretStatus(c *gin.Context) {
|
|
|
|
status, err := a.settingService.GetSecretStatus()
|
|
|
|
if err == nil {
|
|
|
|
jsonObj(c, status, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|