sing-box/experimental/clashapi/rules.go

41 lines
788 B
Go
Raw Normal View History

2022-07-19 22:16:49 +08:00
package clashapi
import (
"net/http"
"github.com/sagernet/sing-box/adapter"
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
)
func ruleRouter(router adapter.Router) http.Handler {
r := chi.NewRouter()
r.Get("/", getRules(router))
return r
}
type Rule struct {
Type string `json:"type"`
Payload string `json:"payload"`
Proxy string `json:"proxy"`
}
func getRules(router adapter.Router) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
rawRules := router.Rules()
var rules []Rule
for _, rule := range rawRules {
rules = append(rules, Rule{
Type: rule.Type(),
Payload: rule.String(),
2024-10-21 23:38:34 +08:00
Proxy: rule.Action().String(),
2022-07-19 22:16:49 +08:00
})
}
render.JSON(w, r, render.M{
"rules": rules,
})
}
}