github.com/sagernet/sing-box@v1.9.0-rc.20/experimental/clashapi/rules.go (about) 1 package clashapi 2 3 import ( 4 "net/http" 5 6 "github.com/sagernet/sing-box/adapter" 7 8 "github.com/go-chi/chi/v5" 9 "github.com/go-chi/render" 10 ) 11 12 func ruleRouter(router adapter.Router) http.Handler { 13 r := chi.NewRouter() 14 r.Get("/", getRules(router)) 15 return r 16 } 17 18 type Rule struct { 19 Type string `json:"type"` 20 Payload string `json:"payload"` 21 Proxy string `json:"proxy"` 22 } 23 24 func getRules(router adapter.Router) func(w http.ResponseWriter, r *http.Request) { 25 return func(w http.ResponseWriter, r *http.Request) { 26 rawRules := router.Rules() 27 28 var rules []Rule 29 for _, rule := range rawRules { 30 rules = append(rules, Rule{ 31 Type: rule.Type(), 32 Payload: rule.String(), 33 Proxy: rule.Outbound(), 34 }) 35 } 36 37 render.JSON(w, r, render.M{ 38 "rules": rules, 39 }) 40 } 41 }