2023-02-09 22:48:06 +03:30
|
|
|
package controller
|
|
|
|
|
|
|
|
|
|
import (
|
2026-05-04 16:36:33 +02:00
|
|
|
"fmt"
|
2023-02-09 22:48:06 +03:30
|
|
|
"net"
|
|
|
|
|
"net/http"
|
2026-05-04 16:36:33 +02:00
|
|
|
"net/netip"
|
2023-02-09 22:48:06 +03:30
|
|
|
"strings"
|
2024-03-11 01:01:24 +03:30
|
|
|
|
2026-05-10 02:13:42 +02:00
|
|
|
"github.com/mhsanaei/3x-ui/v3/logger"
|
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/web/entity"
|
2026-05-13 12:52:52 +02:00
|
|
|
"github.com/mhsanaei/3x-ui/v3/web/service"
|
2023-04-25 18:36:06 +03:30
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-02-09 22:48:06 +03:30
|
|
|
)
|
|
|
|
|
|
2025-09-20 09:35:50 +02:00
|
|
|
// getRemoteIp extracts the real IP address from the request headers or remote address.
|
2023-02-09 22:48:06 +03:30
|
|
|
func getRemoteIp(c *gin.Context) string {
|
2026-05-13 12:52:52 +02:00
|
|
|
remoteIP, ok := extractTrustedIP(c.Request.RemoteAddr)
|
|
|
|
|
if !ok {
|
|
|
|
|
return "unknown"
|
2026-05-04 16:36:33 +02:00
|
|
|
}
|
|
|
|
|
|
2026-05-13 12:52:52 +02:00
|
|
|
if isTrustedProxy(remoteIP) {
|
|
|
|
|
if ip, ok := extractTrustedIP(c.GetHeader("X-Real-IP")); ok {
|
|
|
|
|
return ip
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if xff := c.GetHeader("X-Forwarded-For"); xff != "" {
|
|
|
|
|
for _, part := range strings.Split(xff, ",") {
|
|
|
|
|
if ip, ok := extractTrustedIP(part); ok {
|
|
|
|
|
return ip
|
|
|
|
|
}
|
2026-05-04 16:36:33 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-13 12:52:52 +02:00
|
|
|
return remoteIP
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func isTrustedForwardedRequest(c *gin.Context) bool {
|
|
|
|
|
remoteIP, ok := extractTrustedIP(c.Request.RemoteAddr)
|
|
|
|
|
return ok && isTrustedProxy(remoteIP)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func isTrustedProxy(ip string) bool {
|
|
|
|
|
addr, err := netip.ParseAddr(ip)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
trusted := trustedProxyCIDRs()
|
|
|
|
|
for _, value := range strings.Split(trusted, ",") {
|
|
|
|
|
value = strings.TrimSpace(value)
|
|
|
|
|
if value == "" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if prefix, err := netip.ParsePrefix(value); err == nil {
|
|
|
|
|
if prefix.Contains(addr) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if proxyIP, err := netip.ParseAddr(value); err == nil && proxyIP.Unmap() == addr.Unmap() {
|
|
|
|
|
return true
|
|
|
|
|
}
|
2026-05-04 16:36:33 +02:00
|
|
|
}
|
2026-05-13 12:52:52 +02:00
|
|
|
return false
|
|
|
|
|
}
|
2026-05-04 16:36:33 +02:00
|
|
|
|
2026-05-13 12:52:52 +02:00
|
|
|
func trustedProxyCIDRs() (trusted string) {
|
|
|
|
|
trusted = "127.0.0.1/32,::1/128"
|
|
|
|
|
defer func() {
|
|
|
|
|
_ = recover()
|
|
|
|
|
}()
|
|
|
|
|
settingService := service.SettingService{}
|
|
|
|
|
if value, err := settingService.GetTrustedProxyCIDRs(); err == nil && strings.TrimSpace(value) != "" {
|
|
|
|
|
trusted = value
|
|
|
|
|
}
|
|
|
|
|
return trusted
|
2026-05-04 16:36:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func extractTrustedIP(value string) (string, bool) {
|
|
|
|
|
candidate := strings.TrimSpace(value)
|
|
|
|
|
if candidate == "" {
|
|
|
|
|
return "", false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ip, ok := parseIPCandidate(candidate); ok {
|
|
|
|
|
return ip.String(), true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if host, _, err := net.SplitHostPort(candidate); err == nil {
|
|
|
|
|
if ip, ok := parseIPCandidate(host); ok {
|
|
|
|
|
return ip.String(), true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if strings.Count(candidate, ":") == 1 {
|
|
|
|
|
if host, _, err := net.SplitHostPort(fmt.Sprintf("[%s]", candidate)); err == nil {
|
|
|
|
|
if ip, ok := parseIPCandidate(host); ok {
|
|
|
|
|
return ip.String(), true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "", false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func parseIPCandidate(value string) (netip.Addr, bool) {
|
|
|
|
|
ip, err := netip.ParseAddr(strings.TrimSpace(value))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return netip.Addr{}, false
|
|
|
|
|
}
|
|
|
|
|
return ip.Unmap(), true
|
2023-02-09 22:48:06 +03:30
|
|
|
}
|
|
|
|
|
|
2025-09-20 09:35:50 +02:00
|
|
|
// jsonMsg sends a JSON response with a message and error status.
|
2023-02-09 22:48:06 +03:30
|
|
|
func jsonMsg(c *gin.Context, msg string, err error) {
|
|
|
|
|
jsonMsgObj(c, msg, nil, err)
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-20 09:35:50 +02:00
|
|
|
// jsonObj sends a JSON response with an object and error status.
|
2025-03-12 20:13:51 +01:00
|
|
|
func jsonObj(c *gin.Context, obj any, err error) {
|
2023-02-09 22:48:06 +03:30
|
|
|
jsonMsgObj(c, "", obj, err)
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-20 09:35:50 +02:00
|
|
|
// jsonMsgObj sends a JSON response with a message, object, and error status.
|
2025-03-12 20:13:51 +01:00
|
|
|
func jsonMsgObj(c *gin.Context, msg string, obj any, err error) {
|
2023-02-09 22:48:06 +03:30
|
|
|
m := entity.Msg{
|
|
|
|
|
Obj: obj,
|
|
|
|
|
}
|
|
|
|
|
if err == nil {
|
|
|
|
|
m.Success = true
|
|
|
|
|
if msg != "" {
|
2025-05-09 10:46:29 +07:00
|
|
|
m.Msg = msg
|
2023-02-09 22:48:06 +03:30
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
m.Success = false
|
2026-04-19 22:24:24 +03:00
|
|
|
errStr := err.Error()
|
|
|
|
|
if errStr != "" {
|
|
|
|
|
m.Msg = msg + " (" + errStr + ")"
|
|
|
|
|
logger.Warning(msg+" "+I18nWeb(c, "fail")+": ", err)
|
|
|
|
|
} else if msg != "" {
|
|
|
|
|
m.Msg = msg
|
|
|
|
|
logger.Warning(msg + " " + I18nWeb(c, "fail"))
|
|
|
|
|
} else {
|
|
|
|
|
m.Msg = I18nWeb(c, "somethingWentWrong")
|
|
|
|
|
logger.Warning(I18nWeb(c, "somethingWentWrong") + " " + I18nWeb(c, "fail"))
|
|
|
|
|
}
|
2023-02-09 22:48:06 +03:30
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, m)
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-20 09:35:50 +02:00
|
|
|
// pureJsonMsg sends a pure JSON message response with custom status code.
|
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
|
|
|
}
|
|
|
|
|
|
2025-09-20 09:35:50 +02:00
|
|
|
// isAjax checks if the request is an AJAX request.
|
2023-02-09 22:48:06 +03:30
|
|
|
func isAjax(c *gin.Context) bool {
|
|
|
|
|
return c.GetHeader("X-Requested-With") == "XMLHttpRequest"
|
|
|
|
|
}
|