mihomo/hub/route/rules.go

47 lines
895 B
Go
Raw Normal View History

2018-11-21 13:47:46 +08:00
package route
import (
2022-05-17 16:47:21 +08:00
"github.com/Dreamacro/clash/constant"
"net/http"
"github.com/Dreamacro/clash/tunnel"
2018-11-21 13:47:46 +08:00
2021-04-03 14:59:03 +08:00
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
)
func ruleRouter() http.Handler {
r := chi.NewRouter()
r.Get("/", getRules)
return r
}
type Rule struct {
2018-10-25 00:09:55 +08:00
Type string `json:"type"`
Payload string `json:"payload"`
2018-10-06 14:19:06 +08:00
Proxy string `json:"proxy"`
Size int `json:"size"`
}
func getRules(w http.ResponseWriter, r *http.Request) {
rawRules := tunnel.Rules()
2019-02-13 23:45:43 +08:00
rules := []Rule{}
for _, rule := range rawRules {
2022-05-17 16:47:21 +08:00
r := Rule{
2018-10-25 00:09:55 +08:00
Type: rule.RuleType().String(),
Payload: rule.Payload(),
2018-10-06 14:19:06 +08:00
Proxy: rule.Adapter(),
2022-05-17 16:47:21 +08:00
Size: -1,
}
if rule.RuleType() == constant.GEOIP || rule.RuleType() == constant.GEOSITE {
r.Size = rule.(constant.RuleGroup).GetRecodeSize()
}
rules = append(rules, r)
}
2019-02-13 23:45:43 +08:00
render.JSON(w, r, render.M{
2018-11-21 13:47:46 +08:00
"rules": rules,
})
}