Skip to content
This repository has been archived by the owner on Aug 27, 2020. It is now read-only.

Commit

Permalink
client api v0 with stats
Browse files Browse the repository at this point in the history
  • Loading branch information
lzjluzijie committed Jul 12, 2018
1 parent abeb813 commit e78b7b1
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 99 deletions.
23 changes: 23 additions & 0 deletions core/client/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package client

import "gopkg.in/macaron.v1"

type WebSocksClientApp struct {
//todo
WebListenAddr string

m macaron.Macaron

running bool
//todo multiple client
*WebSocksClient
}

func (app *WebSocksClientApp) GetStatus() (stats *Stats) {
if !app.running {
return nil
}

stats = app.WebSocksClient.Status()
return
}
49 changes: 14 additions & 35 deletions core/client/client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package client

import (
"io"
"net"
"time"

Expand Down Expand Up @@ -40,19 +39,21 @@ type WebSocksClient struct {
//todo enable mux
Mux bool

//control
stopC chan int

//statistics
CreatedAt time.Time
Downloaded uint64
Uploaded uint64
CreatedAt time.Time

downloadMutex sync.Mutex
Downloaded uint64
DownloadSpeed uint64
downloadMutex sync.Mutex
downloadSpeedA uint64
uploadMutex sync.Mutex
UploadSpeed uint64
uploadSpeedA uint64

Uploaded uint64
UploadSpeed uint64
uploadMutex sync.Mutex
uploadSpeedA uint64
}

func NewWebSocksClient(config *WebSocksClientConfig) (client *WebSocksClient) {
Expand Down Expand Up @@ -81,7 +82,11 @@ func NewWebSocksClient(config *WebSocksClientConfig) (client *WebSocksClient) {
TLSClientConfig: tlsConfig,
},

CreatedAt: time.Now(),
CreatedAt: time.Now(),
Downloaded: 0,
DownloadSpeed: 0,
Uploaded: 0,
UploadSpeed: 0,
}
return
}
Expand Down Expand Up @@ -176,32 +181,6 @@ func (client *WebSocksClient) HandleConn(conn *net.TCPConn) {
}

lc.Run(ws)
//client.DialWSConn(lc.Host, lc)
return
}

//todo rewrite
func (client *WebSocksClient) DialWSConn(host string, conn io.ReadWriter) {
ws, err := client.DialWebSocket(core.NewHostHeader(host))
if err != nil {
log.Errorf(err.Error())
return
}

go func() {
_, err = io.Copy(ws, conn)
if err != nil {
log.Debugf(err.Error())
return
}
return
}()

_, err = io.Copy(conn, ws)
if err != nil {
log.Debugf(err.Error())
return
}
return
}

Expand Down
20 changes: 0 additions & 20 deletions core/client/speed.go

This file was deleted.

35 changes: 35 additions & 0 deletions core/client/stats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package client

func (client *WebSocksClient) AddDownloaded(downloaded uint64) {
client.downloadMutex.Lock()
client.Downloaded += downloaded
client.downloadSpeedA += downloaded
client.downloadMutex.Unlock()
return
}

func (client *WebSocksClient) AddUploaded(uploaded uint64) {
client.uploadMutex.Lock()
client.Uploaded += uploaded
client.uploadSpeedA += uploaded
client.uploadMutex.Unlock()
return
}

type Stats struct {
Downloaded uint64
DownloadSpeed uint64
Uploaded uint64
UploadSpeed uint64

//todo conns
}

func (client *WebSocksClient) Status() (stats *Stats) {
return &Stats{
Downloaded: client.Downloaded,
DownloadSpeed: client.DownloadSpeed,
Uploaded: client.Uploaded,
UploadSpeed: client.UploadSpeed,
}
}
42 changes: 27 additions & 15 deletions core/client/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ import (
"gopkg.in/macaron.v1"
)

var websocksClient *WebSocksClient

func RunWeb() {
func (app *WebSocksClientApp) RunWeb() {
//log setup
buf := make([]byte, 0)
buffer := bytes.NewBuffer(buf)
Expand All @@ -31,19 +29,30 @@ func RunWeb() {
return
})

m.Post("/api/client/start", StartClient)
m.Post("/api/client/stop", StopClient)

m.Get("/api/log", func(ctx *macaron.Context) {
ctx.WriteHeader(200)
ctx.Write(buffer.Bytes())
return
})
//todo pac
m.Get("/pac", func(ctx *macaron.Context) {
return
})

//api v0
m.Group("/api/v0/client", func() {
m.Get("/log", func(ctx *macaron.Context) {
ctx.WriteHeader(200)
ctx.Write(buffer.Bytes())
return
})
m.Get("/stats", func(ctx *macaron.Context) {
stats := app.GetStatus()
if stats == nil {
ctx.Error(403, "websocks client is not running")
return
}
ctx.JSON(200, stats)
})
m.Post("/start", app.StartClient)
m.Post("/stop", app.StopClient)
})

go func() {
err := exec.Command("explorer", "http://127.0.0.1:10801").Run()
if err != nil {
Expand All @@ -61,7 +70,7 @@ func RunWeb() {
return
}

func StartClient(ctx *macaron.Context) {
func (app *WebSocksClientApp) StartClient(ctx *macaron.Context) {
webSocksClientConfig := &WebSocksClientConfig{}
data, err := ioutil.ReadAll(ctx.Req.Body().ReadCloser())
if err != nil {
Expand All @@ -73,11 +82,14 @@ func StartClient(ctx *macaron.Context) {
ctx.Error(403, err.Error())
}

websocksClient, err = GetClient(webSocksClientConfig)
websocksClient, err := GetClient(webSocksClientConfig)
if err != nil {
ctx.Error(403, err.Error())
}

app.WebSocksClient = websocksClient
app.running = true

ctx.WriteHeader(200)
ctx.Write([]byte(fmt.Sprintf("%v", webSocksClientConfig)))

Expand All @@ -90,8 +102,8 @@ func StartClient(ctx *macaron.Context) {
return
}

func StopClient(ctx *macaron.Context) {
websocksClient.Stop()
func (app *WebSocksClientApp) StopClient(ctx *macaron.Context) {
app.WebSocksClient.Stop()
ctx.WriteHeader(200)
ctx.Write([]byte("stopped"))
return
Expand Down
2 changes: 1 addition & 1 deletion templates/base/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<body>
{% block body %} {% endblock %}

<footer>
<footer class="footer">
<div class="content has-text-centered">
<p>
<a href="https://websocks.org/" target="_blank"><strong>WebSocks</strong></a> by <a href="https://halu.lu/" target="_blank">Halulu</a>
Expand Down
Loading

0 comments on commit e78b7b1

Please sign in to comment.