2018-06-18 11:31:49 +08:00
|
|
|
package hub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2018-07-15 22:23:20 +08:00
|
|
|
T "github.com/Dreamacro/clash/tunnel"
|
2018-06-18 11:31:49 +08:00
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/go-chi/render"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Traffic struct {
|
|
|
|
Up int64 `json:"up"`
|
|
|
|
Down int64 `json:"down"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHub(addr string) {
|
|
|
|
r := chi.NewRouter()
|
|
|
|
|
|
|
|
r.Get("/traffic", traffic)
|
|
|
|
r.Get("/logs", getLogs)
|
2018-06-20 22:41:02 +08:00
|
|
|
r.Mount("/configs", configRouter())
|
2018-07-18 21:50:16 +08:00
|
|
|
r.Mount("/proxies", proxyRouter())
|
2018-07-12 23:28:38 +08:00
|
|
|
r.Mount("/rules", ruleRouter())
|
2018-06-18 11:31:49 +08:00
|
|
|
|
|
|
|
err := http.ListenAndServe(addr, r)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("External controller error: %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func traffic(w http.ResponseWriter, r *http.Request) {
|
2018-06-20 22:41:02 +08:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2018-06-18 11:31:49 +08:00
|
|
|
|
|
|
|
tick := time.NewTicker(time.Second)
|
2018-07-15 22:23:20 +08:00
|
|
|
t := tunnel.Traffic()
|
2018-06-18 11:31:49 +08:00
|
|
|
for range tick.C {
|
|
|
|
up, down := t.Now()
|
|
|
|
if err := json.NewEncoder(w).Encode(Traffic{
|
|
|
|
Up: up,
|
|
|
|
Down: down,
|
|
|
|
}); err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
w.(http.Flusher).Flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 13:15:20 +08:00
|
|
|
type GetLogs struct {
|
|
|
|
Level string `json:"level"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Log struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Payload string `json:"payload"`
|
|
|
|
}
|
|
|
|
|
2018-06-18 11:31:49 +08:00
|
|
|
func getLogs(w http.ResponseWriter, r *http.Request) {
|
2018-06-22 13:15:20 +08:00
|
|
|
req := &GetLogs{}
|
|
|
|
render.DecodeJSON(r.Body, req)
|
|
|
|
if req.Level == "" {
|
|
|
|
req.Level = "info"
|
|
|
|
}
|
|
|
|
|
2018-07-15 22:23:20 +08:00
|
|
|
mapping := map[string]T.LogLevel{
|
|
|
|
"info": T.INFO,
|
|
|
|
"debug": T.DEBUG,
|
|
|
|
"error": T.ERROR,
|
|
|
|
"warning": T.WARNING,
|
2018-06-22 13:15:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
level, ok := mapping[req.Level]
|
|
|
|
if !ok {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
render.JSON(w, r, Error{
|
|
|
|
Error: "Level error",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-15 22:23:20 +08:00
|
|
|
src := tunnel.Log()
|
2018-06-18 11:31:49 +08:00
|
|
|
sub, err := src.Subscribe()
|
|
|
|
defer src.UnSubscribe(sub)
|
|
|
|
if err != nil {
|
2018-06-20 22:41:02 +08:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2018-06-18 11:31:49 +08:00
|
|
|
render.JSON(w, r, Error{
|
|
|
|
Error: err.Error(),
|
|
|
|
})
|
2018-06-20 22:41:02 +08:00
|
|
|
return
|
2018-06-18 11:31:49 +08:00
|
|
|
}
|
|
|
|
render.Status(r, http.StatusOK)
|
|
|
|
for elm := range sub {
|
2018-07-15 22:23:20 +08:00
|
|
|
log := elm.(T.Log)
|
2018-06-22 13:15:20 +08:00
|
|
|
if log.LogLevel > level {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-06-18 11:31:49 +08:00
|
|
|
if err := json.NewEncoder(w).Encode(Log{
|
|
|
|
Type: log.Type(),
|
|
|
|
Payload: log.Payload,
|
|
|
|
}); err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
w.(http.Flusher).Flush()
|
|
|
|
}
|
|
|
|
}
|