2018-11-21 13:47:46 +08:00
|
|
|
package route
|
2018-07-12 23:28:38 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2018-11-21 13:47:46 +08:00
|
|
|
T "github.com/Dreamacro/clash/tunnel"
|
|
|
|
|
2018-07-12 23:28:38 +08:00
|
|
|
"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-25 00:09:55 +08:00
|
|
|
Type string `json:"type"`
|
|
|
|
Payload string `json:"payload"`
|
2018-10-06 14:19:06 +08:00
|
|
|
Proxy string `json:"proxy"`
|
2018-07-12 23:28:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func getRules(w http.ResponseWriter, r *http.Request) {
|
2018-11-21 13:47:46 +08:00
|
|
|
rawRules := T.Instance().Rules()
|
2018-07-12 23:28:38 +08:00
|
|
|
|
|
|
|
var rules []Rule
|
2018-07-26 00:04:59 +08:00
|
|
|
for _, rule := range rawRules {
|
2018-07-12 23:28:38 +08:00
|
|
|
rules = append(rules, Rule{
|
2018-10-25 00:09:55 +08:00
|
|
|
Type: rule.RuleType().String(),
|
2018-07-12 23:28:38 +08:00
|
|
|
Payload: rule.Payload(),
|
2018-10-06 14:19:06 +08:00
|
|
|
Proxy: rule.Adapter(),
|
2018-07-12 23:28:38 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2018-11-21 13:47:46 +08:00
|
|
|
render.Respond(w, r, map[string][]Rule{
|
|
|
|
"rules": rules,
|
2018-07-12 23:28:38 +08:00
|
|
|
})
|
|
|
|
}
|