rules.go 782 B

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