Skip to content

Commit 4904078

Browse files
committed
添加获取与修改clash规则接口
1 parent d1dac21 commit 4904078

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

web/controller/common.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/shirou/gopsutil/mem"
99
"github.com/shirou/gopsutil/net"
1010
"time"
11+
"trojan/asset"
1112
"trojan/core"
1213
"trojan/trojan"
1314
)
@@ -66,6 +67,26 @@ func SetDomain(domain string) *ResponseBody {
6667
return &responseBody
6768
}
6869

70+
// SetClashRules 设置clash规则
71+
func SetClashRules(rules string) *ResponseBody {
72+
responseBody := ResponseBody{Msg: "success"}
73+
defer TimeCost(time.Now(), &responseBody)
74+
core.SetValue("clash-rules", rules)
75+
return &responseBody
76+
}
77+
78+
// GetClashRules 获取clash规则
79+
func GetClashRules() *ResponseBody {
80+
responseBody := ResponseBody{Msg: "success"}
81+
defer TimeCost(time.Now(), &responseBody)
82+
rules, _ := core.GetValue("clash-rules")
83+
if rules == "" {
84+
rules = string(asset.GetAsset("clash-rules.yaml"))
85+
}
86+
responseBody.Data = rules
87+
return &responseBody
88+
}
89+
6990
// SetTrojanType 设置trojan类型
7091
func SetTrojanType(tType string) *ResponseBody {
7192
responseBody := ResponseBody{Msg: "success"}

web/controller/user.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ func ClashSubInfo(c *gin.Context) {
205205
c.Header("subscription-userinfo", userInfo)
206206
domain, port := trojan.GetDomainAndPort()
207207
name := fmt.Sprintf("%s:%d", domain, port)
208+
rules, _ := core.GetValue("clash-rules")
209+
if rules == "" {
210+
rules = string(asset.GetAsset("clash-rules.yaml"))
211+
}
208212
result := fmt.Sprintf(`proxies:
209213
- {name: %s, server: %s, port: %d, type: trojan, password: %s, sni: %s}
210214
@@ -215,7 +219,7 @@ proxy-groups:
215219
- %s
216220
217221
%s
218-
`, name, domain, port, data.Pass, domain, name, string(asset.GetAsset("clash-rules.yaml")))
222+
`, name, domain, port, data.Pass, domain, name, rules)
219223
c.String(200, result)
220224
return
221225
}

web/web.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@ func commonRouter(router *gin.Engine) {
139139
common.GET("/serverInfo", func(c *gin.Context) {
140140
c.JSON(200, controller.ServerInfo())
141141
})
142+
common.GET("/clashRules", func(c *gin.Context) {
143+
c.JSON(200, controller.GetClashRules())
144+
})
145+
common.POST("/clashRules", func(c *gin.Context) {
146+
rules := c.PostForm("rules")
147+
c.JSON(200, controller.SetClashRules(rules))
148+
})
142149
common.POST("/loginInfo", func(c *gin.Context) {
143150
c.JSON(200, controller.SetLoginInfo(c.PostForm("title")))
144151
})

0 commit comments

Comments
 (0)