mirror of
https://github.com/xjasonlyu/tun2socks.git
synced 2025-10-05 16:56:54 +08:00
Chore(restapi): minor adjustment
This commit is contained in:
@@ -16,10 +16,6 @@ import (
|
|||||||
|
|
||||||
const defaultInterval = 1000
|
const defaultInterval = 1000
|
||||||
|
|
||||||
func init() {
|
|
||||||
registerMountPoint("/connections", connectionRouter())
|
|
||||||
}
|
|
||||||
|
|
||||||
func connectionRouter() http.Handler {
|
func connectionRouter() http.Handler {
|
||||||
r := chi.NewRouter()
|
r := chi.NewRouter()
|
||||||
r.Get("/", getConnections)
|
r.Get("/", getConnections)
|
||||||
@@ -30,8 +26,7 @@ func connectionRouter() http.Handler {
|
|||||||
|
|
||||||
func getConnections(w http.ResponseWriter, r *http.Request) {
|
func getConnections(w http.ResponseWriter, r *http.Request) {
|
||||||
if !websocket.IsWebSocketUpgrade(r) {
|
if !websocket.IsWebSocketUpgrade(r) {
|
||||||
snapshot := statistic.DefaultManager.Snapshot()
|
render.JSON(w, r, statistic.DefaultManager.Snapshot())
|
||||||
render.JSON(w, r, snapshot)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,8 +51,7 @@ func getConnections(w http.ResponseWriter, r *http.Request) {
|
|||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
sendSnapshot := func() error {
|
sendSnapshot := func() error {
|
||||||
buf.Reset()
|
buf.Reset()
|
||||||
snapshot := statistic.DefaultManager.Snapshot()
|
if err := json.NewEncoder(buf).Encode(statistic.DefaultManager.Snapshot()); err != nil {
|
||||||
if err := json.NewEncoder(buf).Encode(snapshot); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -20,18 +20,10 @@ import (
|
|||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var _upgrader = websocket.Upgrader{
|
||||||
_upgrader = websocket.Upgrader{
|
CheckOrigin: func(r *http.Request) bool {
|
||||||
CheckOrigin: func(r *http.Request) bool {
|
return true
|
||||||
return true
|
},
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
_mountPoints = make(map[string]http.Handler)
|
|
||||||
)
|
|
||||||
|
|
||||||
func registerMountPoint(pattern string, handler http.Handler) {
|
|
||||||
_mountPoints[pattern] = handler
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Start(addr, token string) error {
|
func Start(addr, token string) error {
|
||||||
@@ -51,10 +43,7 @@ func Start(addr, token string) error {
|
|||||||
r.Get("/logs", getLogs)
|
r.Get("/logs", getLogs)
|
||||||
r.Get("/traffic", traffic)
|
r.Get("/traffic", traffic)
|
||||||
r.Get("/version", version)
|
r.Get("/version", version)
|
||||||
|
r.Mount("/connections", connectionRouter())
|
||||||
for pattern, handler := range _mountPoints {
|
|
||||||
r.Mount(pattern, handler)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
listener, err := net.Listen("tcp", addr)
|
listener, err := net.Listen("tcp", addr)
|
||||||
@@ -143,7 +132,7 @@ func getLogs(w http.ResponseWriter, r *http.Request) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := json.NewEncoder(buf).Encode(e); err != nil {
|
if err = json.NewEncoder(buf).Encode(e); err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -160,15 +149,12 @@ func getLogs(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Traffic struct {
|
|
||||||
Up int64 `json:"up"`
|
|
||||||
Down int64 `json:"down"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func traffic(w http.ResponseWriter, r *http.Request) {
|
func traffic(w http.ResponseWriter, r *http.Request) {
|
||||||
var wsConn *websocket.Conn
|
var (
|
||||||
|
err error
|
||||||
|
wsConn *websocket.Conn
|
||||||
|
)
|
||||||
if websocket.IsWebSocketUpgrade(r) {
|
if websocket.IsWebSocketUpgrade(r) {
|
||||||
var err error
|
|
||||||
wsConn, err = _upgrader.Upgrade(w, r, nil)
|
wsConn, err = _upgrader.Upgrade(w, r, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@@ -182,13 +168,16 @@ func traffic(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
tick := time.NewTicker(time.Second)
|
tick := time.NewTicker(time.Second)
|
||||||
defer tick.Stop()
|
defer tick.Stop()
|
||||||
t := statistic.DefaultManager
|
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
var err error
|
|
||||||
for range tick.C {
|
for range tick.C {
|
||||||
buf.Reset()
|
buf.Reset()
|
||||||
up, down := t.Now()
|
|
||||||
if err := json.NewEncoder(buf).Encode(Traffic{
|
up, down := statistic.DefaultManager.Now()
|
||||||
|
if err = json.NewEncoder(buf).Encode(struct {
|
||||||
|
Up int64 `json:"up"`
|
||||||
|
Down int64 `json:"down"`
|
||||||
|
}{
|
||||||
Up: up,
|
Up: up,
|
||||||
Down: down,
|
Down: down,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
|
Reference in New Issue
Block a user