github.com/chwjbn/xclash@v0.2.0/hub/route/rules.go (about)

     1  package route
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/chwjbn/xclash/tunnel"
     7  
     8  	"github.com/go-chi/chi/v5"
     9  	"github.com/go-chi/render"
    10  )
    11  
    12  func ruleRouter() http.Handler {
    13  	r := chi.NewRouter()
    14  	r.Get("/", getRules)
    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(w http.ResponseWriter, r *http.Request) {
    25  	rawRules := tunnel.Rules()
    26  
    27  	rules := []Rule{}
    28  	for _, rule := range rawRules {
    29  		rules = append(rules, Rule{
    30  			Type:    rule.RuleType().String(),
    31  			Payload: rule.Payload(),
    32  			Proxy:   rule.Adapter(),
    33  		})
    34  	}
    35  
    36  	render.JSON(w, r, render.M{
    37  		"rules": rules,
    38  	})
    39  }