diff --git a/web/controller/common.go b/web/controller/common.go index 01276a1a..9dedaa9d 100644 --- a/web/controller/common.go +++ b/web/controller/common.go @@ -8,6 +8,7 @@ import ( "github.com/shirou/gopsutil/mem" "github.com/shirou/gopsutil/net" "time" + "trojan/asset" "trojan/core" "trojan/trojan" ) @@ -66,6 +67,26 @@ func SetDomain(domain string) *ResponseBody { return &responseBody } +// SetClashRules 设置clash规则 +func SetClashRules(rules string) *ResponseBody { + responseBody := ResponseBody{Msg: "success"} + defer TimeCost(time.Now(), &responseBody) + core.SetValue("clash-rules", rules) + return &responseBody +} + +// GetClashRules 获取clash规则 +func GetClashRules() *ResponseBody { + responseBody := ResponseBody{Msg: "success"} + defer TimeCost(time.Now(), &responseBody) + rules, _ := core.GetValue("clash-rules") + if rules == "" { + rules = string(asset.GetAsset("clash-rules.yaml")) + } + responseBody.Data = rules + return &responseBody +} + // SetTrojanType 设置trojan类型 func SetTrojanType(tType string) *ResponseBody { responseBody := ResponseBody{Msg: "success"} diff --git a/web/controller/user.go b/web/controller/user.go index 60ea8e1e..bc1ef7d8 100644 --- a/web/controller/user.go +++ b/web/controller/user.go @@ -205,6 +205,10 @@ func ClashSubInfo(c *gin.Context) { c.Header("subscription-userinfo", userInfo) domain, port := trojan.GetDomainAndPort() name := fmt.Sprintf("%s:%d", domain, port) + rules, _ := core.GetValue("clash-rules") + if rules == "" { + rules = string(asset.GetAsset("clash-rules.yaml")) + } result := fmt.Sprintf(`proxies: - {name: %s, server: %s, port: %d, type: trojan, password: %s, sni: %s} @@ -215,7 +219,7 @@ proxy-groups: - %s %s -`, name, domain, port, data.Pass, domain, name, string(asset.GetAsset("clash-rules.yaml"))) +`, name, domain, port, data.Pass, domain, name, rules) c.String(200, result) return } diff --git a/web/web.go b/web/web.go index de659c0f..92d761f4 100644 --- a/web/web.go +++ b/web/web.go @@ -139,6 +139,13 @@ func commonRouter(router *gin.Engine) { common.GET("/serverInfo", func(c *gin.Context) { c.JSON(200, controller.ServerInfo()) }) + common.GET("/clashRules", func(c *gin.Context) { + c.JSON(200, controller.GetClashRules()) + }) + common.POST("/clashRules", func(c *gin.Context) { + rules := c.PostForm("rules") + c.JSON(200, controller.SetClashRules(rules)) + }) common.POST("/loginInfo", func(c *gin.Context) { c.JSON(200, controller.SetLoginInfo(c.PostForm("title"))) })