rules.go 897 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package route
  2. import (
  3. "github.com/metacubex/mihomo/constant"
  4. "net/http"
  5. "github.com/metacubex/mihomo/tunnel"
  6. "github.com/go-chi/chi/v5"
  7. "github.com/go-chi/render"
  8. )
  9. func ruleRouter() http.Handler {
  10. r := chi.NewRouter()
  11. r.Get("/", getRules)
  12. return r
  13. }
  14. type Rule struct {
  15. Type string `json:"type"`
  16. Payload string `json:"payload"`
  17. Proxy string `json:"proxy"`
  18. Size int `json:"size"`
  19. }
  20. func getRules(w http.ResponseWriter, r *http.Request) {
  21. rawRules := tunnel.Rules()
  22. rules := []Rule{}
  23. for _, rule := range rawRules {
  24. r := Rule{
  25. Type: rule.RuleType().String(),
  26. Payload: rule.Payload(),
  27. Proxy: rule.Adapter(),
  28. Size: -1,
  29. }
  30. if rule.RuleType() == constant.GEOIP || rule.RuleType() == constant.GEOSITE {
  31. r.Size = rule.(constant.RuleGroup).GetRecodeSize()
  32. }
  33. rules = append(rules, r)
  34. }
  35. render.JSON(w, r, render.M{
  36. "rules": rules,
  37. })
  38. }