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

Commit a0024a2

Browse files
committed
remove old config
2 parents 455600c + c0b2ae0 commit a0024a2

26 files changed

+905
-798
lines changed

core/client/app.go renamed to client/app.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,3 @@ type WebSocksClientApp struct {
1212
//todo multiple client
1313
*WebSocksClient
1414
}
15-
16-
func (app *WebSocksClientApp) GetStatus() (stats *Stats) {
17-
if !app.running {
18-
return nil
19-
}
20-
21-
stats = app.WebSocksClient.Status()
22-
return
23-
}

core/client/client.go renamed to client/client.go

Lines changed: 3 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,13 @@ import (
66

77
"net/url"
88

9-
"crypto/tls"
10-
11-
"sync"
12-
139
"github.com/gorilla/websocket"
1410
"github.com/lzjluzijie/websocks/core"
1511
"github.com/sirupsen/logrus"
1612
)
1713

1814
var log = logrus.New()
1915

20-
type WebSocksClientConfig struct {
21-
ListenAddr string
22-
ServerURL string
23-
24-
SNI string
25-
InsecureCert bool
26-
27-
Mux bool
28-
}
29-
3016
type WebSocksClient struct {
3117
ServerURL *url.URL
3218
ListenAddr *net.TCPAddr
@@ -42,86 +28,18 @@ type WebSocksClient struct {
4228
//control
4329
stopC chan int
4430

45-
//statistics
4631
CreatedAt time.Time
47-
48-
Downloaded uint64
49-
DownloadSpeed uint64
50-
downloadMutex sync.Mutex
51-
downloadSpeedA uint64
52-
53-
Uploaded uint64
54-
UploadSpeed uint64
55-
uploadMutex sync.Mutex
56-
uploadSpeedA uint64
32+
Stats *core.Stats
5733
}
5834

59-
func NewWebSocksClient(config *WebSocksClientConfig) (client *WebSocksClient) {
60-
serverURL, err := url.Parse(config.ServerURL)
61-
if err != nil {
62-
return
63-
}
64-
65-
laddr, err := net.ResolveTCPAddr("tcp", config.ListenAddr)
66-
if err != nil {
67-
return
68-
}
69-
70-
tlsConfig := &tls.Config{
71-
InsecureSkipVerify: config.InsecureCert,
72-
ServerName: config.SNI,
73-
}
74-
75-
client = &WebSocksClient{
76-
ServerURL: serverURL,
77-
ListenAddr: laddr,
78-
dialer: &websocket.Dialer{
79-
ReadBufferSize: 4 * 1024,
80-
WriteBufferSize: 4 * 1024,
81-
HandshakeTimeout: 10 * time.Second,
82-
TLSClientConfig: tlsConfig,
83-
},
84-
85-
CreatedAt: time.Now(),
86-
Downloaded: 0,
87-
DownloadSpeed: 0,
88-
Uploaded: 0,
89-
UploadSpeed: 0,
90-
}
91-
return
92-
}
93-
94-
func (client *WebSocksClient) Listen() (err error) {
35+
func (client *WebSocksClient) Run() (err error) {
9536
listener, err := net.ListenTCP("tcp", client.ListenAddr)
9637
if err != nil {
9738
return err
9839
}
9940

10041
log.Infof("Start to listen at %s", client.ListenAddr.String())
10142

102-
//status
103-
go func() {
104-
t := time.NewTicker(time.Second)
105-
for range t.C {
106-
client.downloadMutex.Lock()
107-
client.DownloadSpeed = client.downloadSpeedA
108-
client.downloadSpeedA = 0
109-
client.downloadMutex.Unlock()
110-
log.Infof("Download speed: %d", client.DownloadSpeed)
111-
}
112-
}()
113-
114-
go func() {
115-
t := time.NewTicker(time.Second)
116-
for range t.C {
117-
client.uploadMutex.Lock()
118-
client.UploadSpeed = client.uploadSpeedA
119-
client.uploadSpeedA = 0
120-
client.uploadMutex.Unlock()
121-
log.Infof("Upload speed: %d", client.UploadSpeed)
122-
}
123-
}()
124-
12543
if client.Mux {
12644
err := client.OpenMux()
12745
if err != nil {
@@ -190,9 +108,7 @@ func (client *WebSocksClient) DialWebSocket(header map[string][]string) (ws *cor
190108
return
191109
}
192110

193-
ws = core.NewWebSocket(wsConn)
194-
ws.AddDownloaded = client.AddDownloaded
195-
ws.AddUploaded = client.AddUploaded
111+
ws = core.NewWebSocket(wsConn, client.Stats)
196112
//client.connMutex.Lock()
197113
//client.wsConns = append(client.wsConns, ws)
198114
//client.connMutex.Unlock()

core/client/config.go renamed to client/config.go

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,26 @@ package client
22

33
import (
44
"crypto/tls"
5-
"encoding/json"
6-
"io/ioutil"
75
"net"
86
"net/url"
97
"time"
108

119
"github.com/gorilla/websocket"
12-
"github.com/urfave/cli"
10+
"github.com/lzjluzijie/websocks/core"
1311
)
1412

13+
type Config struct {
14+
ListenAddr string
15+
ServerURL string
16+
17+
SNI string
18+
InsecureCert bool
19+
20+
Mux bool
21+
}
22+
1523
//GetClient return client from path
16-
func GetClient(config *WebSocksClientConfig) (client *WebSocksClient, err error) {
24+
func (config *Config) GetClient() (client *WebSocksClient, err error) {
1725
//tackle config
1826
serverURL, err := url.Parse(config.ServerURL)
1927
if err != nil {
@@ -43,30 +51,7 @@ func GetClient(config *WebSocksClientConfig) (client *WebSocksClient, err error)
4351
//todo mux
4452

4553
CreatedAt: time.Now(),
46-
}
47-
return
48-
}
49-
50-
//GenerateClientConfig create a client config from cli.Context
51-
func GenerateClientConfig(c *cli.Context) (err error) {
52-
path := c.String("path")
53-
54-
config := &WebSocksClientConfig{
55-
ListenAddr: c.String("l"),
56-
ServerURL: c.String("s"),
57-
SNI: c.String("sni"),
58-
InsecureCert: c.Bool("insecure"),
59-
Mux: c.Bool("mux"),
60-
}
61-
62-
data, err := json.MarshalIndent(config, "", " ")
63-
if err != nil {
64-
return
65-
}
66-
67-
err = ioutil.WriteFile(path, data, 600)
68-
if err != nil {
69-
return
54+
Stats: core.NewStats(),
7055
}
7156
return
7257
}
File renamed without changes.

core/client/mux.go renamed to client/mux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func (client *WebSocksClient) OpenMux() (err error) {
1515
return
1616
}
1717

18-
ws := core.NewWebSocket(wsConn)
18+
ws := core.NewWebSocket(wsConn, client.Stats)
1919

2020
muxWS := core.NewMuxWebSocket(ws)
2121
client.muxWS = muxWS
File renamed without changes.

core/client/web.go renamed to client/web.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package client
33
import (
44
"bytes"
55
"encoding/json"
6-
"fmt"
76
"io"
87
"io/ioutil"
98
"net/http"
@@ -42,12 +41,11 @@ func (app *WebSocksClientApp) RunWeb() {
4241
return
4342
})
4443
m.Get("/stats", func(ctx *macaron.Context) {
45-
stats := app.GetStatus()
46-
if stats == nil {
44+
if app.WebSocksClient == nil {
4745
ctx.Error(403, "websocks client is not running")
4846
return
4947
}
50-
ctx.JSON(200, stats)
48+
ctx.JSON(200, app.WebSocksClient.Stats)
5149
})
5250
m.Post("/start", app.StartClient)
5351
m.Post("/stop", app.StopClient)
@@ -71,30 +69,27 @@ func (app *WebSocksClientApp) RunWeb() {
7169
}
7270

7371
func (app *WebSocksClientApp) StartClient(ctx *macaron.Context) {
74-
webSocksClientConfig := &WebSocksClientConfig{}
72+
config := &Config{}
7573
data, err := ioutil.ReadAll(ctx.Req.Body().ReadCloser())
7674
if err != nil {
7775
ctx.Error(403, err.Error())
7876
}
7977

80-
err = json.Unmarshal(data, webSocksClientConfig)
78+
err = json.Unmarshal(data, config)
8179
if err != nil {
8280
ctx.Error(403, err.Error())
8381
}
8482

85-
websocksClient, err := GetClient(webSocksClientConfig)
83+
websocksClient, err := config.GetClient()
8684
if err != nil {
8785
ctx.Error(403, err.Error())
8886
}
8987

9088
app.WebSocksClient = websocksClient
9189
app.running = true
9290

93-
ctx.WriteHeader(200)
94-
ctx.Write([]byte(fmt.Sprintf("%v", webSocksClientConfig)))
95-
9691
go func() {
97-
err = websocksClient.Listen()
92+
err = websocksClient.Run()
9893
if err != nil {
9994
log.Error(err.Error())
10095
}

config/config.go

Lines changed: 0 additions & 92 deletions
This file was deleted.

0 commit comments

Comments
 (0)