forked from fossabot/clash
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package hub | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/Dreamacro/clash/tunnel" | ||
|
||
"github.com/go-chi/chi" | ||
"github.com/go-chi/render" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
var ( | ||
tun = tunnel.GetInstance() | ||
) | ||
|
||
type Traffic struct { | ||
Up int64 `json:"up"` | ||
Down int64 `json:"down"` | ||
} | ||
|
||
type Log struct { | ||
Type string `json:"type"` | ||
Payload string `json:"payload"` | ||
} | ||
|
||
type Error struct { | ||
Error string `json:"error"` | ||
} | ||
|
||
func NewHub(addr string) { | ||
r := chi.NewRouter() | ||
|
||
r.Get("/traffic", traffic) | ||
r.Get("/logs", getLogs) | ||
|
||
err := http.ListenAndServe(addr, r) | ||
if err != nil { | ||
log.Fatalf("External controller error: %s", err.Error()) | ||
} | ||
} | ||
|
||
func traffic(w http.ResponseWriter, r *http.Request) { | ||
render.Status(r, http.StatusOK) | ||
|
||
tick := time.NewTicker(time.Second) | ||
t := tun.Traffic() | ||
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() | ||
} | ||
} | ||
|
||
func getLogs(w http.ResponseWriter, r *http.Request) { | ||
src := tun.Log() | ||
sub, err := src.Subscribe() | ||
defer src.UnSubscribe(sub) | ||
if err != nil { | ||
render.Status(r, http.StatusInternalServerError) | ||
render.JSON(w, r, Error{ | ||
Error: err.Error(), | ||
}) | ||
} | ||
render.Status(r, http.StatusOK) | ||
for elm := range sub { | ||
log := elm.(tunnel.Log) | ||
if err := json.NewEncoder(w).Encode(Log{ | ||
Type: log.Type(), | ||
Payload: log.Payload, | ||
}); err != nil { | ||
break | ||
} | ||
w.(http.Flusher).Flush() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters