github.com/metacubex/mihomo@v1.18.5/hub/route/rules.go (about)

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