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