| 
									
										
										
										
											2023-02-09 19:18:06 +00:00
										 |  |  | package controller | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"net" | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2024-03-10 21:31:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-09 19:18:06 +00:00
										 |  |  | 	"x-ui/config" | 
					
						
							|  |  |  | 	"x-ui/logger" | 
					
						
							|  |  |  | 	"x-ui/web/entity" | 
					
						
							| 
									
										
										
										
											2023-04-25 15:06:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/gin-gonic/gin" | 
					
						
							| 
									
										
										
										
											2023-02-09 19:18:06 +00:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func getRemoteIp(c *gin.Context) string { | 
					
						
							| 
									
										
										
										
											2024-05-23 13:16:13 +00:00
										 |  |  | 	value := c.GetHeader("X-Real-IP") | 
					
						
							|  |  |  | 	if value != "" { | 
					
						
							|  |  |  | 		return value | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	value = c.GetHeader("X-Forwarded-For") | 
					
						
							| 
									
										
										
										
											2023-02-09 19:18:06 +00:00
										 |  |  | 	if value != "" { | 
					
						
							|  |  |  | 		ips := strings.Split(value, ",") | 
					
						
							|  |  |  | 		return ips[0] | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-05-23 13:16:13 +00:00
										 |  |  | 	addr := c.Request.RemoteAddr | 
					
						
							|  |  |  | 	ip, _, _ := net.SplitHostPort(addr) | 
					
						
							|  |  |  | 	return ip | 
					
						
							| 
									
										
										
										
											2023-02-09 19:18:06 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func jsonMsg(c *gin.Context, msg string, err error) { | 
					
						
							|  |  |  | 	jsonMsgObj(c, msg, nil, err) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-12 19:13:51 +00:00
										 |  |  | func jsonObj(c *gin.Context, obj any, err error) { | 
					
						
							| 
									
										
										
										
											2023-02-09 19:18:06 +00:00
										 |  |  | 	jsonMsgObj(c, "", obj, err) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-12 19:13:51 +00:00
										 |  |  | func jsonMsgObj(c *gin.Context, msg string, obj any, err error) { | 
					
						
							| 
									
										
										
										
											2023-02-09 19:18:06 +00:00
										 |  |  | 	m := entity.Msg{ | 
					
						
							|  |  |  | 		Obj: obj, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if err == nil { | 
					
						
							|  |  |  | 		m.Success = true | 
					
						
							|  |  |  | 		if msg != "" { | 
					
						
							| 
									
										
										
										
											2025-05-09 03:46:29 +00:00
										 |  |  | 			m.Msg = msg | 
					
						
							| 
									
										
										
										
											2023-02-09 19:18:06 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		m.Success = false | 
					
						
							| 
									
										
										
										
											2025-05-09 03:46:29 +00:00
										 |  |  | 		m.Msg = msg + " (" + err.Error() + ")" | 
					
						
							| 
									
										
										
										
											2024-09-30 15:06:38 +00:00
										 |  |  | 		logger.Warning(msg+" "+I18nWeb(c, "fail")+": ", err) | 
					
						
							| 
									
										
										
										
											2023-02-09 19:18:06 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	c.JSON(http.StatusOK, m) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-10 21:31:24 +00:00
										 |  |  | func pureJsonMsg(c *gin.Context, statusCode int, success bool, msg string) { | 
					
						
							|  |  |  | 	c.JSON(statusCode, entity.Msg{ | 
					
						
							|  |  |  | 		Success: success, | 
					
						
							|  |  |  | 		Msg:     msg, | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2023-02-09 19:18:06 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func html(c *gin.Context, name string, title string, data gin.H) { | 
					
						
							|  |  |  | 	if data == nil { | 
					
						
							|  |  |  | 		data = gin.H{} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	data["title"] = title | 
					
						
							| 
									
										
										
										
											2024-05-23 21:51:19 +00:00
										 |  |  | 	host := c.GetHeader("X-Forwarded-Host") | 
					
						
							|  |  |  | 	if host == "" { | 
					
						
							|  |  |  | 		host = c.GetHeader("X-Real-IP") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if host == "" { | 
					
						
							|  |  |  | 		var err error | 
					
						
							|  |  |  | 		host, _, err = net.SplitHostPort(c.Request.Host) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			host = c.Request.Host | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	data["host"] = host | 
					
						
							| 
									
										
										
										
											2023-02-09 19:18:06 +00:00
										 |  |  | 	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-25 22:41:11 +00:00
										 |  |  | 	for key, value := range h { | 
					
						
							|  |  |  | 		a[key] = value | 
					
						
							| 
									
										
										
										
											2023-02-09 19:18:06 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return a | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func isAjax(c *gin.Context) bool { | 
					
						
							|  |  |  | 	return c.GetHeader("X-Requested-With") == "XMLHttpRequest" | 
					
						
							|  |  |  | } |