2018-06-18 03:31:49 +00:00
|
|
|
package hub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2018-10-06 05:15:02 +00:00
|
|
|
"strings"
|
2018-06-18 03:31:49 +00:00
|
|
|
"time"
|
|
|
|
|
2018-07-25 16:04:59 +00:00
|
|
|
"github.com/Dreamacro/clash/config"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2018-07-15 14:23:20 +00:00
|
|
|
T "github.com/Dreamacro/clash/tunnel"
|
2018-06-18 03:31:49 +00:00
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
2018-07-19 01:31:53 +00:00
|
|
|
"github.com/go-chi/cors"
|
2018-06-18 03:31:49 +00:00
|
|
|
"github.com/go-chi/render"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2018-10-06 05:15:02 +00:00
|
|
|
var secret = ""
|
|
|
|
|
2018-06-18 03:31:49 +00:00
|
|
|
type Traffic struct {
|
|
|
|
Up int64 `json:"up"`
|
|
|
|
Down int64 `json:"down"`
|
|
|
|
}
|
|
|
|
|
2018-07-25 16:04:59 +00:00
|
|
|
func newHub(signal chan struct{}) {
|
|
|
|
var addr string
|
|
|
|
ch := config.Instance().Subscribe()
|
|
|
|
signal <- struct{}{}
|
2018-10-06 05:15:02 +00:00
|
|
|
count := 0
|
2018-07-25 16:04:59 +00:00
|
|
|
for {
|
|
|
|
elm := <-ch
|
|
|
|
event := elm.(*config.Event)
|
2018-10-06 05:15:02 +00:00
|
|
|
switch event.Type {
|
|
|
|
case "external-controller":
|
2018-07-25 16:04:59 +00:00
|
|
|
addr = event.Payload.(string)
|
2018-10-06 05:15:02 +00:00
|
|
|
count++
|
|
|
|
case "secret":
|
|
|
|
secret = event.Payload.(string)
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
if count == 2 {
|
2018-07-25 16:04:59 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-18 03:31:49 +00:00
|
|
|
r := chi.NewRouter()
|
|
|
|
|
2018-07-19 01:31:53 +00:00
|
|
|
cors := cors.New(cors.Options{
|
|
|
|
AllowedOrigins: []string{"*"},
|
|
|
|
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
|
2018-10-06 05:15:02 +00:00
|
|
|
AllowedHeaders: []string{"Content-Type", "Authorization"},
|
2018-07-19 01:31:53 +00:00
|
|
|
MaxAge: 300,
|
|
|
|
})
|
|
|
|
|
2018-10-06 05:15:02 +00:00
|
|
|
r.Use(cors.Handler, authentication)
|
2018-07-19 01:31:53 +00:00
|
|
|
|
2018-08-07 06:45:16 +00:00
|
|
|
r.With(jsonContentType).Get("/traffic", traffic)
|
|
|
|
r.With(jsonContentType).Get("/logs", getLogs)
|
2018-06-20 14:41:02 +00:00
|
|
|
r.Mount("/configs", configRouter())
|
2018-07-18 13:50:16 +00:00
|
|
|
r.Mount("/proxies", proxyRouter())
|
2018-07-12 15:28:38 +00:00
|
|
|
r.Mount("/rules", ruleRouter())
|
2018-06-18 03:31:49 +00:00
|
|
|
|
2018-08-11 18:23:46 +00:00
|
|
|
log.Infof("RESTful API listening at: %s", addr)
|
2018-06-18 03:31:49 +00:00
|
|
|
err := http.ListenAndServe(addr, r)
|
|
|
|
if err != nil {
|
2018-07-25 16:04:59 +00:00
|
|
|
log.Errorf("External controller error: %s", err.Error())
|
2018-06-18 03:31:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-07 06:45:16 +00:00
|
|
|
func jsonContentType(next http.Handler) http.Handler {
|
|
|
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
return http.HandlerFunc(fn)
|
|
|
|
}
|
|
|
|
|
2018-10-06 05:15:02 +00:00
|
|
|
func authentication(next http.Handler) http.Handler {
|
|
|
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
header := r.Header.Get("Authorization")
|
|
|
|
text := strings.SplitN(header, " ", 2)
|
|
|
|
|
|
|
|
if secret == "" {
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
hasUnvalidHeader := text[0] != "Bearer"
|
|
|
|
hasUnvalidSecret := len(text) == 2 && text[1] != secret
|
|
|
|
if hasUnvalidHeader || hasUnvalidSecret {
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
render.JSON(w, r, Error{
|
|
|
|
Error: "Authentication failed",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
return http.HandlerFunc(fn)
|
|
|
|
}
|
|
|
|
|
2018-06-18 03:31:49 +00:00
|
|
|
func traffic(w http.ResponseWriter, r *http.Request) {
|
2018-06-20 14:41:02 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2018-06-18 03:31:49 +00:00
|
|
|
|
|
|
|
tick := time.NewTicker(time.Second)
|
2018-07-15 14:23:20 +00:00
|
|
|
t := tunnel.Traffic()
|
2018-06-18 03:31:49 +00: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 05:15:20 +00:00
|
|
|
type Log struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Payload string `json:"payload"`
|
|
|
|
}
|
|
|
|
|
2018-06-18 03:31:49 +00:00
|
|
|
func getLogs(w http.ResponseWriter, r *http.Request) {
|
2018-08-12 11:35:13 +00:00
|
|
|
levelText := r.URL.Query().Get("level")
|
|
|
|
if levelText == "" {
|
|
|
|
levelText = "info"
|
2018-06-22 05:15:20 +00:00
|
|
|
}
|
|
|
|
|
2018-08-12 11:35:13 +00:00
|
|
|
level, ok := C.LogLevelMapping[levelText]
|
2018-06-22 05:15:20 +00:00
|
|
|
if !ok {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
render.JSON(w, r, Error{
|
|
|
|
Error: "Level error",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-15 14:23:20 +00:00
|
|
|
src := tunnel.Log()
|
2018-06-18 03:31:49 +00:00
|
|
|
sub, err := src.Subscribe()
|
|
|
|
defer src.UnSubscribe(sub)
|
|
|
|
if err != nil {
|
2018-06-20 14:41:02 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2018-06-18 03:31:49 +00:00
|
|
|
render.JSON(w, r, Error{
|
|
|
|
Error: err.Error(),
|
|
|
|
})
|
2018-06-20 14:41:02 +00:00
|
|
|
return
|
2018-06-18 03:31:49 +00:00
|
|
|
}
|
|
|
|
render.Status(r, http.StatusOK)
|
|
|
|
for elm := range sub {
|
2018-07-15 14:23:20 +00:00
|
|
|
log := elm.(T.Log)
|
2018-06-22 05:15:20 +00:00
|
|
|
if log.LogLevel > level {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-06-18 03:31:49 +00:00
|
|
|
if err := json.NewEncoder(w).Encode(Log{
|
|
|
|
Type: log.Type(),
|
|
|
|
Payload: log.Payload,
|
|
|
|
}); err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
w.(http.Flusher).Flush()
|
|
|
|
}
|
|
|
|
}
|
2018-07-25 16:04:59 +00:00
|
|
|
|
|
|
|
// Run initial hub
|
|
|
|
func Run() {
|
|
|
|
signal := make(chan struct{})
|
|
|
|
go newHub(signal)
|
|
|
|
<-signal
|
|
|
|
}
|