diff options
| author | Ho3ein <ho3ein.sanaei@gmail.com> | 2023-12-10 17:42:52 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-12-10 17:42:52 +0300 |
| commit | e3f1d3c892a1af48f27fdc36f273a55f38d13b40 (patch) | |
| tree | b11d0c1ed3c15c8f6f891a5e6df8e021d5db8ab6 /web/controller | |
| parent | 36cf7c0a8fda915b51e75958ce729fd9a61a5c90 (diff) | |
| parent | 9fbe80f87f950673058f0001b3704251fa8b9243 (diff) | |
huge changes
Diffstat (limited to 'web/controller')
| -rw-r--r-- | web/controller/api.go | 6 | ||||
| -rw-r--r-- | web/controller/inbound.go | 32 | ||||
| -rw-r--r-- | web/controller/setting.go | 57 | ||||
| -rw-r--r-- | web/controller/xray_setting.go | 63 | ||||
| -rw-r--r-- | web/controller/xui.go | 11 |
5 files changed, 122 insertions, 47 deletions
diff --git a/web/controller/api.go b/web/controller/api.go index 32c639f8..73c36787 100644 --- a/web/controller/api.go +++ b/web/controller/api.go @@ -38,6 +38,8 @@ func (a *APIController) initRouter(g *gin.RouterGroup) { g.POST("/resetAllClientTraffics/:id", a.resetAllClientTraffics) g.POST("/delDepletedClients/:id", a.delDepletedClients) g.GET("/createbackup", a.createBackup) + g.POST("/onlines", a.onlines) + a.inboundController = NewInboundController(g) } @@ -104,3 +106,7 @@ func (a *APIController) delDepletedClients(c *gin.Context) { func (a *APIController) createBackup(c *gin.Context) { a.Tgbot.SendBackupToAdmins() } + +func (a *APIController) onlines(c *gin.Context) { + a.inboundController.onlines(c) +} diff --git a/web/controller/inbound.go b/web/controller/inbound.go index 8b64fe6e..123c486f 100644 --- a/web/controller/inbound.go +++ b/web/controller/inbound.go @@ -1,6 +1,7 @@ package controller import ( + "encoding/json" "fmt" "strconv" "x-ui/database/model" @@ -37,6 +38,8 @@ func (a *InboundController) initRouter(g *gin.RouterGroup) { g.POST("/resetAllTraffics", a.resetAllTraffics) g.POST("/resetAllClientTraffics/:id", a.resetAllClientTraffics) g.POST("/delDepletedClients/:id", a.delDepletedClients) + g.POST("/import", a.importInbound) + g.POST("/onlines", a.onlines) } func (a *InboundController) getInbounds(c *gin.Context) { @@ -265,6 +268,31 @@ func (a *InboundController) resetAllClientTraffics(c *gin.Context) { jsonMsg(c, "All traffics of client reseted", nil) } +func (a *InboundController) importInbound(c *gin.Context) { + inbound := &model.Inbound{} + err := json.Unmarshal([]byte(c.PostForm("data")), inbound) + if err != nil { + jsonMsg(c, "Something went wrong!", err) + return + } + user := session.GetLoginUser(c) + inbound.Id = 0 + inbound.UserId = user.Id + inbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port) + + for index := range inbound.ClientStats { + inbound.ClientStats[index].Id = 0 + inbound.ClientStats[index].Enable = true + } + + needRestart := false + inbound, needRestart, err = a.inboundService.AddInbound(inbound) + jsonMsgObj(c, I18nWeb(c, "pages.inbounds.create"), inbound, err) + if err == nil && needRestart { + a.xrayService.SetToNeedRestart() + } +} + func (a *InboundController) delDepletedClients(c *gin.Context) { id, err := strconv.Atoi(c.Param("id")) if err != nil { @@ -278,3 +306,7 @@ func (a *InboundController) delDepletedClients(c *gin.Context) { } jsonMsg(c, "All delpeted clients are deleted", nil) } + +func (a *InboundController) onlines(c *gin.Context) { + jsonObj(c, a.inboundService.GetOnlineClinets(), nil) +} diff --git a/web/controller/setting.go b/web/controller/setting.go index 32eccefe..64cae71b 100644 --- a/web/controller/setting.go +++ b/web/controller/setting.go @@ -41,7 +41,7 @@ func (a *SettingController) initRouter(g *gin.RouterGroup) { g.POST("/update", a.updateSetting) g.POST("/updateUser", a.updateUser) g.POST("/restartPanel", a.restartPanel) - g.GET("/getDefaultJsonConfig", a.getDefaultJsonConfig) + g.GET("/getDefaultJsonConfig", a.getDefaultXrayConfig) g.POST("/updateUserSecret", a.updateSecret) g.POST("/getUserSecret", a.getUserSecret) } @@ -55,54 +55,12 @@ func (a *SettingController) getAllSetting(c *gin.Context) { jsonObj(c, allSetting, nil) } -func (a *SettingController) getDefaultJsonConfig(c *gin.Context) { - defaultJsonConfig, err := a.settingService.GetDefaultJsonConfig() +func (a *SettingController) getDefaultSettings(c *gin.Context) { + result, err := a.settingService.GetDefaultSettings(c.Request.Host) if err != nil { jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err) return } - jsonObj(c, defaultJsonConfig, nil) -} - -func (a *SettingController) getDefaultSettings(c *gin.Context) { - type settingFunc func() (interface{}, error) - - settings := map[string]settingFunc{ - "expireDiff": func() (interface{}, error) { return a.settingService.GetExpireDiff() }, - "trafficDiff": func() (interface{}, error) { return a.settingService.GetTrafficDiff() }, - "defaultCert": func() (interface{}, error) { return a.settingService.GetCertFile() }, - "defaultKey": func() (interface{}, error) { return a.settingService.GetKeyFile() }, - "tgBotEnable": func() (interface{}, error) { return a.settingService.GetTgbotenabled() }, - "subEnable": func() (interface{}, error) { return a.settingService.GetSubEnable() }, - "subPort": func() (interface{}, error) { return a.settingService.GetSubPort() }, - "subPath": func() (interface{}, error) { return a.settingService.GetSubPath() }, - "subDomain": func() (interface{}, error) { return a.settingService.GetSubDomain() }, - "subKeyFile": func() (interface{}, error) { return a.settingService.GetSubKeyFile() }, - "subCertFile": func() (interface{}, error) { return a.settingService.GetSubCertFile() }, - "subEncrypt": func() (interface{}, error) { return a.settingService.GetSubEncrypt() }, - "subShowInfo": func() (interface{}, error) { return a.settingService.GetSubShowInfo() }, - } - - result := make(map[string]interface{}) - - for key, fn := range settings { - value, err := fn() - if err != nil { - jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err) - return - } - result[key] = value - } - - subTLS := false - if result["subKeyFile"] != "" || result["subCertFile"] != "" { - subTLS = true - } - result["subTLS"] = subTLS - - delete(result, "subKeyFile") - delete(result, "subCertFile") - jsonObj(c, result, nil) } @@ -169,3 +127,12 @@ func (a *SettingController) getUserSecret(c *gin.Context) { jsonObj(c, user, nil) } } + +func (a *SettingController) getDefaultXrayConfig(c *gin.Context) { + defaultJsonConfig, err := a.settingService.GetDefaultXrayConfig() + if err != nil { + jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err) + return + } + jsonObj(c, defaultJsonConfig, nil) +} diff --git a/web/controller/xray_setting.go b/web/controller/xray_setting.go new file mode 100644 index 00000000..d4deacd9 --- /dev/null +++ b/web/controller/xray_setting.go @@ -0,0 +1,63 @@ +package controller + +import ( + "x-ui/web/service" + + "github.com/gin-gonic/gin" +) + +type XraySettingController struct { + XraySettingService service.XraySettingService + SettingService service.SettingService + InboundService service.InboundService + XrayService service.XrayService +} + +func NewXraySettingController(g *gin.RouterGroup) *XraySettingController { + a := &XraySettingController{} + a.initRouter(g) + return a +} + +func (a *XraySettingController) initRouter(g *gin.RouterGroup) { + g = g.Group("/xray") + + g.POST("/", a.getXraySetting) + g.POST("/update", a.updateSetting) + g.GET("/getXrayResult", a.getXrayResult) + g.GET("/getDefaultJsonConfig", a.getDefaultXrayConfig) +} + +func (a *XraySettingController) getXraySetting(c *gin.Context) { + xraySetting, err := a.SettingService.GetXrayConfigTemplate() + if err != nil { + jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err) + return + } + inboundTags, err := a.InboundService.GetInboundTags() + if err != nil { + jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err) + return + } + xrayResponse := "{ \"xraySetting\": " + xraySetting + ", \"inboundTags\": " + inboundTags + " }" + jsonObj(c, xrayResponse, nil) +} + +func (a *XraySettingController) updateSetting(c *gin.Context) { + xraySetting := c.PostForm("xraySetting") + err := a.XraySettingService.SaveXraySetting(xraySetting) + jsonMsg(c, I18nWeb(c, "pages.settings.toasts.modifySettings"), err) +} + +func (a *XraySettingController) getDefaultXrayConfig(c *gin.Context) { + defaultJsonConfig, err := a.SettingService.GetDefaultXrayConfig() + if err != nil { + jsonMsg(c, I18nWeb(c, "pages.settings.toasts.getSettings"), err) + return + } + jsonObj(c, defaultJsonConfig, nil) +} + +func (a *XraySettingController) getXrayResult(c *gin.Context) { + jsonObj(c, a.XrayService.GetXrayResult(), nil) +} diff --git a/web/controller/xui.go b/web/controller/xui.go index 700bd52c..5b4c0a18 100644 --- a/web/controller/xui.go +++ b/web/controller/xui.go @@ -7,8 +7,9 @@ import ( type XUIController struct { BaseController - inboundController *InboundController - settingController *SettingController + inboundController *InboundController + settingController *SettingController + xraySettingController *XraySettingController } func NewXUIController(g *gin.RouterGroup) *XUIController { @@ -24,9 +25,11 @@ func (a *XUIController) initRouter(g *gin.RouterGroup) { g.GET("/", a.index) g.GET("/inbounds", a.inbounds) g.GET("/settings", a.settings) + g.GET("/xray", a.xraySettings) a.inboundController = NewInboundController(g) a.settingController = NewSettingController(g) + a.xraySettingController = NewXraySettingController(g) } func (a *XUIController) index(c *gin.Context) { @@ -40,3 +43,7 @@ func (a *XUIController) inbounds(c *gin.Context) { func (a *XUIController) settings(c *gin.Context) { html(c, "settings.html", "pages.settings.title", nil) } + +func (a *XUIController) xraySettings(c *gin.Context) { + html(c, "xray.html", "pages.xray.title", nil) +} |
