2018-11-21 13:47:46 +08:00
|
|
|
package route
|
2018-06-18 11:31:49 +08:00
|
|
|
|
|
|
|
import (
|
2019-07-12 15:44:12 +08:00
|
|
|
"bytes"
|
2018-06-18 11:31:49 +08:00
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2018-10-06 13:15:02 +08:00
|
|
|
"strings"
|
2018-06-18 11:31:49 +08:00
|
|
|
"time"
|
|
|
|
|
2019-09-13 17:44:30 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2018-11-21 13:47:46 +08:00
|
|
|
"github.com/Dreamacro/clash/log"
|
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"
|
2018-07-19 09:31:53 +08:00
|
|
|
"github.com/go-chi/cors"
|
2018-06-18 11:31:49 +08:00
|
|
|
"github.com/go-chi/render"
|
2019-07-12 15:44:12 +08:00
|
|
|
"github.com/gorilla/websocket"
|
2018-06-18 11:31:49 +08:00
|
|
|
)
|
|
|
|
|
2018-11-21 13:47:46 +08:00
|
|
|
var (
|
|
|
|
serverSecret = ""
|
|
|
|
serverAddr = ""
|
2018-12-20 01:29:13 +08:00
|
|
|
|
|
|
|
uiPath = ""
|
2019-07-12 15:44:12 +08:00
|
|
|
|
2019-09-30 14:13:29 +08:00
|
|
|
upgrader = websocket.Upgrader{
|
|
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
}
|
2018-11-21 13:47:46 +08:00
|
|
|
)
|
2018-10-06 13:15:02 +08:00
|
|
|
|
2018-06-18 11:31:49 +08:00
|
|
|
type Traffic struct {
|
|
|
|
Up int64 `json:"up"`
|
|
|
|
Down int64 `json:"down"`
|
|
|
|
}
|
|
|
|
|
2018-12-20 01:29:13 +08:00
|
|
|
func SetUIPath(path string) {
|
2020-06-18 21:33:57 +08:00
|
|
|
uiPath = C.Path.Resolve(path)
|
2018-12-20 01:29:13 +08:00
|
|
|
}
|
|
|
|
|
2018-11-21 13:47:46 +08:00
|
|
|
func Start(addr string, secret string) {
|
|
|
|
if serverAddr != "" {
|
|
|
|
return
|
2018-07-26 00:04:59 +08:00
|
|
|
}
|
|
|
|
|
2018-11-21 13:47:46 +08:00
|
|
|
serverAddr = addr
|
|
|
|
serverSecret = secret
|
|
|
|
|
2018-06-18 11:31:49 +08:00
|
|
|
r := chi.NewRouter()
|
|
|
|
|
2018-07-19 09:31:53 +08:00
|
|
|
cors := cors.New(cors.Options{
|
|
|
|
AllowedOrigins: []string{"*"},
|
2018-12-29 14:11:54 +08:00
|
|
|
AllowedMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE"},
|
2018-10-06 13:15:02 +08:00
|
|
|
AllowedHeaders: []string{"Content-Type", "Authorization"},
|
2018-07-19 09:31:53 +08:00
|
|
|
MaxAge: 300,
|
|
|
|
})
|
|
|
|
|
2019-09-21 21:28:02 +08:00
|
|
|
r.Use(cors.Handler)
|
2018-12-21 17:48:29 +08:00
|
|
|
r.Group(func(r chi.Router) {
|
2019-09-21 21:28:02 +08:00
|
|
|
r.Use(authentication)
|
2018-12-21 17:48:29 +08:00
|
|
|
|
2020-02-21 18:00:19 +08:00
|
|
|
r.Get("/", hello)
|
2019-07-12 15:44:12 +08:00
|
|
|
r.Get("/logs", getLogs)
|
|
|
|
r.Get("/traffic", traffic)
|
2019-09-13 17:44:30 +08:00
|
|
|
r.Get("/version", version)
|
2018-12-21 17:48:29 +08:00
|
|
|
r.Mount("/configs", configRouter())
|
|
|
|
r.Mount("/proxies", proxyRouter())
|
|
|
|
r.Mount("/rules", ruleRouter())
|
2019-10-27 21:44:07 +08:00
|
|
|
r.Mount("/connections", connectionRouter())
|
2019-12-11 17:31:15 +08:00
|
|
|
r.Mount("/providers/proxies", proxyProviderRouter())
|
2018-12-21 17:48:29 +08:00
|
|
|
})
|
2018-06-18 11:31:49 +08:00
|
|
|
|
2018-12-20 01:29:13 +08:00
|
|
|
if uiPath != "" {
|
2018-12-21 17:48:29 +08:00
|
|
|
r.Group(func(r chi.Router) {
|
|
|
|
fs := http.StripPrefix("/ui", http.FileServer(http.Dir(uiPath)))
|
2019-09-08 22:33:52 +08:00
|
|
|
r.Get("/ui", http.RedirectHandler("/ui/", http.StatusTemporaryRedirect).ServeHTTP)
|
2018-12-21 17:48:29 +08:00
|
|
|
r.Get("/ui/*", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fs.ServeHTTP(w, r)
|
|
|
|
})
|
2018-12-20 01:29:13 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-11-21 13:47:46 +08:00
|
|
|
log.Infoln("RESTful API listening at: %s", addr)
|
2018-06-18 11:31:49 +08:00
|
|
|
err := http.ListenAndServe(addr, r)
|
|
|
|
if err != nil {
|
2018-11-21 13:47:46 +08:00
|
|
|
log.Errorln("External controller error: %s", err.Error())
|
2018-06-18 11:31:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-06 13:15:02 +08:00
|
|
|
func authentication(next http.Handler) http.Handler {
|
|
|
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
2018-11-21 13:47:46 +08:00
|
|
|
if serverSecret == "" {
|
2018-10-06 13:15:02 +08:00
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-30 14:13:29 +08:00
|
|
|
// Browser websocket not support custom header
|
|
|
|
if websocket.IsWebSocketUpgrade(r) && r.URL.Query().Get("token") != "" {
|
|
|
|
token := r.URL.Query().Get("token")
|
|
|
|
if token != serverSecret {
|
|
|
|
render.Status(r, http.StatusUnauthorized)
|
|
|
|
render.JSON(w, r, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
header := r.Header.Get("Authorization")
|
|
|
|
text := strings.SplitN(header, " ", 2)
|
|
|
|
|
2020-04-24 23:49:35 +08:00
|
|
|
hasInvalidHeader := text[0] != "Bearer"
|
|
|
|
hasInvalidSecret := len(text) != 2 || text[1] != serverSecret
|
|
|
|
if hasInvalidHeader || hasInvalidSecret {
|
2018-12-10 11:33:37 +08:00
|
|
|
render.Status(r, http.StatusUnauthorized)
|
|
|
|
render.JSON(w, r, ErrUnauthorized)
|
2018-10-06 13:15:02 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
return http.HandlerFunc(fn)
|
|
|
|
}
|
|
|
|
|
2018-12-29 14:11:54 +08:00
|
|
|
func hello(w http.ResponseWriter, r *http.Request) {
|
|
|
|
render.JSON(w, r, render.M{"hello": "clash"})
|
|
|
|
}
|
|
|
|
|
2018-06-18 11:31:49 +08:00
|
|
|
func traffic(w http.ResponseWriter, r *http.Request) {
|
2019-07-12 15:44:12 +08:00
|
|
|
var wsConn *websocket.Conn
|
|
|
|
if websocket.IsWebSocketUpgrade(r) {
|
|
|
|
var err error
|
|
|
|
wsConn, err = upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if wsConn == nil {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
render.Status(r, http.StatusOK)
|
|
|
|
}
|
2018-06-18 11:31:49 +08:00
|
|
|
|
|
|
|
tick := time.NewTicker(time.Second)
|
2019-10-27 21:44:07 +08:00
|
|
|
t := T.DefaultManager
|
2019-07-12 15:44:12 +08:00
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
var err error
|
2018-06-18 11:31:49 +08:00
|
|
|
for range tick.C {
|
2019-07-12 15:44:12 +08:00
|
|
|
buf.Reset()
|
2018-06-18 11:31:49 +08:00
|
|
|
up, down := t.Now()
|
2019-07-12 15:44:12 +08:00
|
|
|
if err := json.NewEncoder(buf).Encode(Traffic{
|
2018-06-18 11:31:49 +08:00
|
|
|
Up: up,
|
|
|
|
Down: down,
|
|
|
|
}); err != nil {
|
|
|
|
break
|
|
|
|
}
|
2019-07-12 15:44:12 +08:00
|
|
|
|
|
|
|
if wsConn == nil {
|
|
|
|
_, err = w.Write(buf.Bytes())
|
|
|
|
w.(http.Flusher).Flush()
|
|
|
|
} else {
|
|
|
|
err = wsConn.WriteMessage(websocket.TextMessage, buf.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2018-06-18 11:31:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 13:15:20 +08:00
|
|
|
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-08-12 19:35:13 +08:00
|
|
|
levelText := r.URL.Query().Get("level")
|
|
|
|
if levelText == "" {
|
|
|
|
levelText = "info"
|
2018-06-22 13:15:20 +08:00
|
|
|
}
|
|
|
|
|
2018-11-21 13:47:46 +08:00
|
|
|
level, ok := log.LogLevelMapping[levelText]
|
2018-06-22 13:15:20 +08:00
|
|
|
if !ok {
|
2018-12-10 11:33:37 +08:00
|
|
|
render.Status(r, http.StatusBadRequest)
|
|
|
|
render.JSON(w, r, ErrBadRequest)
|
2018-06-22 13:15:20 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-12 15:44:12 +08:00
|
|
|
var wsConn *websocket.Conn
|
|
|
|
if websocket.IsWebSocketUpgrade(r) {
|
|
|
|
var err error
|
|
|
|
wsConn, err = upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if wsConn == nil {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
render.Status(r, http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2018-11-21 13:47:46 +08:00
|
|
|
sub := log.Subscribe()
|
2019-07-12 15:44:12 +08:00
|
|
|
defer log.UnSubscribe(sub)
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
var err error
|
2018-06-18 11:31:49 +08:00
|
|
|
for elm := range sub {
|
2019-07-12 15:44:12 +08:00
|
|
|
buf.Reset()
|
2018-11-21 13:47:46 +08:00
|
|
|
log := elm.(*log.Event)
|
|
|
|
if log.LogLevel < level {
|
2018-06-22 13:15:20 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-07-12 15:44:12 +08:00
|
|
|
if err := json.NewEncoder(buf).Encode(Log{
|
2018-06-18 11:31:49 +08:00
|
|
|
Type: log.Type(),
|
|
|
|
Payload: log.Payload,
|
|
|
|
}); err != nil {
|
|
|
|
break
|
|
|
|
}
|
2019-07-12 15:44:12 +08:00
|
|
|
|
|
|
|
if wsConn == nil {
|
|
|
|
_, err = w.Write(buf.Bytes())
|
|
|
|
w.(http.Flusher).Flush()
|
|
|
|
} else {
|
|
|
|
err = wsConn.WriteMessage(websocket.TextMessage, buf.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2018-06-18 11:31:49 +08:00
|
|
|
}
|
|
|
|
}
|
2019-09-13 17:44:30 +08:00
|
|
|
|
|
|
|
func version(w http.ResponseWriter, r *http.Request) {
|
|
|
|
render.JSON(w, r, render.M{"version": C.Version})
|
|
|
|
}
|