Skip to content

Commit

Permalink
Add: external controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamacro committed Jun 18, 2018
1 parent bc4ca28 commit 79e5338
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 1 deletion.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ Unfortunately, there is no native elegant way to implement golang's daemon.

So we can use third-party daemon tools like pm2, supervisor, and so on.

In the case of pm2, we can start the daemon this way:
In the case of [pm2](https://github.com/Unitech/pm2), we can start the daemon this way:

```sh
pm2 start clash
```

If you have Docker installed, you can run clash directly using `docker-compose`.

[Run clash in docker](https://github.com/Dreamacro/clash/wiki/Run-clash-in-docker)

## Config

Configuration file at `$HOME/.config/clash/config.ini`
Expand All @@ -45,6 +49,9 @@ Below is a simple demo configuration file:
port = 7890
socks-port = 7891

# A RESTful API for clash
external-controller = 127.0.0.1:8080

[Proxy]
# name = ss, server, port, cipher, password
# The types of cipher are consistent with go-shadowsocks2
Expand Down
83 changes: 83 additions & 0 deletions hub/server.go
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()
}
}
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"syscall"

C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/hub"
"github.com/Dreamacro/clash/proxy/http"
"github.com/Dreamacro/clash/proxy/socks"
"github.com/Dreamacro/clash/tunnel"
Expand Down Expand Up @@ -37,6 +38,11 @@ func main() {
go http.NewHttpProxy(port)
go socks.NewSocksProxy(socksPort)

// Hub
if key, err := section.GetKey("external-controller"); err == nil {
go hub.NewHub(key.Value())
}

sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
<-sigCh
Expand Down
15 changes: 15 additions & 0 deletions tunnel/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ type Log struct {
Payload string
}

func (l *Log) Type() string {
switch l.LogType {
case INFO:
return "Info"
case WARNING:
return "Warning"
case ERROR:
return "Error"
case DEBUG:
return "Debug"
default:
return "Unknow"
}
}

func print(data Log) {
switch data.LogType {
case INFO:
Expand Down
8 changes: 8 additions & 0 deletions tunnel/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ func (t *Tunnel) Add(req C.ServerAdapter) {
t.queue.In() <- req
}

func (t *Tunnel) Traffic() *C.Traffic {
return t.traffic
}

func (t *Tunnel) Log() *observable.Observable {
return t.observable
}

func (t *Tunnel) UpdateConfig() (err error) {
cfg, err := C.GetConfig()
if err != nil {
Expand Down

0 comments on commit 79e5338

Please sign in to comment.