Skip to content

Commit

Permalink
添加获取与修改clash规则接口
Browse files Browse the repository at this point in the history
  • Loading branch information
Jrohy committed Dec 3, 2021
1 parent d1dac21 commit 4904078
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
21 changes: 21 additions & 0 deletions web/controller/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/net"
"time"
"trojan/asset"
"trojan/core"
"trojan/trojan"
)
Expand Down Expand Up @@ -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"}
Expand Down
6 changes: 5 additions & 1 deletion web/controller/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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
}
Expand Down
7 changes: 7 additions & 0 deletions web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
})
Expand Down

0 comments on commit 4904078

Please sign in to comment.