clash/hub/route/rules.go

41 lines
709 B
Go
Raw Normal View History

2018-11-21 05:47:46 +00:00
package route
import (
"net/http"
2018-11-21 05:47:46 +00:00
T "github.com/Dreamacro/clash/tunnel"
"github.com/go-chi/chi"
"github.com/go-chi/render"
)
func ruleRouter() http.Handler {
r := chi.NewRouter()
r.Get("/", getRules)
return r
}
type Rule struct {
2018-10-24 16:09:55 +00:00
Type string `json:"type"`
Payload string `json:"payload"`
2018-10-06 06:19:06 +00:00
Proxy string `json:"proxy"`
}
func getRules(w http.ResponseWriter, r *http.Request) {
2018-11-21 05:47:46 +00:00
rawRules := T.Instance().Rules()
var rules []Rule
for _, rule := range rawRules {
rules = append(rules, Rule{
2018-10-24 16:09:55 +00:00
Type: rule.RuleType().String(),
Payload: rule.Payload(),
2018-10-06 06:19:06 +00:00
Proxy: rule.Adapter(),
})
}
w.WriteHeader(http.StatusOK)
2018-11-21 05:47:46 +00:00
render.Respond(w, r, map[string][]Rule{
"rules": rules,
})
}