Files
trihuy-russian/web/controller/util.go
T

86 lines
1.7 KiB
Go
Raw Normal View History

2023-02-09 22:48:06 +03:30
package controller
import (
"net"
"net/http"
"strings"
2024-03-11 01:01:24 +03:30
2023-02-09 22:48:06 +03:30
"x-ui/config"
"x-ui/logger"
"x-ui/web/entity"
2023-04-25 18:36:06 +03:30
"github.com/gin-gonic/gin"
2023-02-09 22:48:06 +03:30
)
func getRemoteIp(c *gin.Context) string {
value := c.GetHeader("X-Real-IP")
if value != "" {
return value
}
value = c.GetHeader("X-Forwarded-For")
2023-02-09 22:48:06 +03:30
if value != "" {
ips := strings.Split(value, ",")
return ips[0]
}
addr := c.Request.RemoteAddr
ip, _, _ := net.SplitHostPort(addr)
return ip
2023-02-09 22:48:06 +03:30
}
func jsonMsg(c *gin.Context, msg string, err error) {
jsonMsgObj(c, msg, nil, err)
}
func jsonObj(c *gin.Context, obj interface{}, err error) {
jsonMsgObj(c, "", obj, err)
}
func jsonMsgObj(c *gin.Context, msg string, obj interface{}, err error) {
m := entity.Msg{
Obj: obj,
}
if err == nil {
m.Success = true
if msg != "" {
2023-05-21 03:29:27 +04:30
m.Msg = msg + I18nWeb(c, "success")
2023-02-09 22:48:06 +03:30
}
} else {
m.Success = false
2023-05-21 03:29:27 +04:30
m.Msg = msg + I18nWeb(c, "fail") + ": " + err.Error()
logger.Warning(msg+I18nWeb(c, "fail")+": ", err)
2023-02-09 22:48:06 +03:30
}
c.JSON(http.StatusOK, m)
}
2024-03-11 01:01:24 +03:30
func pureJsonMsg(c *gin.Context, statusCode int, success bool, msg string) {
c.JSON(statusCode, entity.Msg{
Success: success,
Msg: msg,
})
2023-02-09 22:48:06 +03:30
}
func html(c *gin.Context, name string, title string, data gin.H) {
if data == nil {
data = gin.H{}
}
data["title"] = title
2024-05-22 17:51:55 +02:00
data["host"], _, _ = net.SplitHostPort(c.Request.Host)
2023-02-09 22:48:06 +03:30
data["request_uri"] = c.Request.RequestURI
data["base_path"] = c.GetString("base_path")
c.HTML(http.StatusOK, name, getContext(data))
}
func getContext(h gin.H) gin.H {
a := gin.H{
"cur_ver": config.GetVersion(),
}
2023-04-26 02:11:11 +03:30
for key, value := range h {
a[key] = value
2023-02-09 22:48:06 +03:30
}
return a
}
func isAjax(c *gin.Context) bool {
return c.GetHeader("X-Requested-With") == "XMLHttpRequest"
}