Skip to content

Commit e08314f

Browse files
author
Lukasz Zajaczkowski
committed
Fix performance
1 parent 57cdf60 commit e08314f

File tree

5 files changed

+45
-53
lines changed

5 files changed

+45
-53
lines changed

client.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
mode = client
44

55
[client]
6-
server = 192.168.30.128
6+
server = 104.199.19.152
77
# server port
8-
port = 40100
8+
port = 80
99
# MTU
1010
mtu = 1400

main

-4 KB
Binary file not shown.

vpn/client.go

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,7 @@ func NewClient(cfg ClientConfig) error {
145145
logger.Debug("Read: ", string(r))
146146

147147
if messageType == websocket.TextMessage {
148-
var message Data
149-
if err := json.Unmarshal(r, &message); err != nil {
150-
client.ws.Close()
151-
close(client.data)
152-
logger.Panic(err)
153-
}
154-
client.dispatcher(&message)
148+
client.dispatcher(r)
155149
}
156150

157151
}
@@ -161,11 +155,17 @@ func NewClient(cfg ClientConfig) error {
161155
return errors.New("Not expected to exit")
162156
}
163157

164-
func (clt *Client) dispatcher(message *Data) {
158+
func (clt *Client) dispatcher(p []byte) {
165159
logger.Debug("Dispatcher: ", clt.state)
166160
switch clt.state {
167161
case STATE_INIT:
168162
logger.Debug("STATE_INIT")
163+
var message Data
164+
if err := json.Unmarshal(p, &message); err != nil {
165+
clt.ws.Close()
166+
close(clt.data)
167+
logger.Panic(err)
168+
}
169169
if message.ConnectionState == STATE_CONNECT {
170170

171171
ipStr := string(message.Payload)
@@ -180,12 +180,7 @@ func (clt *Client) dispatcher(message *Data) {
180180
clt.handleInterface()
181181
}
182182
case STATE_CONNECTED:
183-
if message.ConnectionState == STATE_CONNECTED {
184-
clt.toIface <- message.Payload
185-
}
186-
187-
case STATE_DISCONNECT:
188-
183+
clt.toIface <- p
189184

190185
}
191186
}
@@ -250,24 +245,23 @@ func (clt *Client) writePump() {
250245

251246
func (clt *Client) write(mt int, message *Data) error {
252247

253-
s, err := json.Marshal(message)
254-
if err != nil {
255-
logger.Panic(err)
248+
if message.ConnectionState == STATE_CONNECTED {
249+
return clt.ws.WriteMessage(mt, message.Payload)
250+
} else {
251+
s, err := json.Marshal(message)
252+
if err != nil {
253+
logger.Panic(err)
254+
}
255+
return clt.ws.WriteMessage(mt, s)
256256
}
257-
logger.Debug("Sending data: ", string(s))
258-
return clt.ws.WriteMessage(mt, s)
257+
259258
}
260259

261260
func (clt *Client) cleanUp() {
262261
c := make(chan os.Signal, 1)
263262
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
264263
<-c
265264
logger.Info("Cleaning Up")
266-
267-
if err := clt.ws.WriteControl(websocket.CloseMessage, []byte{}, time.Now().Add(writeWait)); err != nil {
268-
logger.Error("Send Close Message error", err)
269-
}
270-
271265
delRoute("0.0.0.0/1")
272266
delRoute("128.0.0.0/1")
273267
for _, dest := range clt.routes {

vpn/connection.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,7 @@ func (c *connection) readPump() {
9797
} else {
9898

9999
if messageType == websocket.TextMessage {
100-
var message Data
101-
if err := json.Unmarshal(r, &message); err != nil {
102-
logger.Panic(err)
103-
}
104-
c.dispatcher(&message)
100+
c.dispatcher(r)
105101
}
106102
}
107103
}
@@ -141,28 +137,38 @@ func (c *connection) writePump() {
141137

142138
func (c *connection) write(mt int, message *Data) error {
143139

144-
logger.Debug("write data len: ", len(message.Payload))
145-
s, err := json.Marshal(message)
146-
if err != nil {
147-
logger.Panic(err)
148-
return err
149-
}
150-
151140
c.ws.SetWriteDeadline(time.Now().Add(writeWait))
152-
err = c.ws.WriteMessage(mt, s)
153-
if err != nil {
154-
return err
155-
}
156141

157-
return nil
142+
if message.ConnectionState == STATE_CONNECTED {
143+
err := c.ws.WriteMessage(mt, message.Payload)
144+
if err != nil {
145+
return err
146+
}
147+
} else {
148+
s, err := json.Marshal(message)
149+
if err != nil {
150+
logger.Panic(err)
151+
return err
152+
}
153+
158154

155+
err = c.ws.WriteMessage(mt, s)
156+
if err != nil {
157+
return err
158+
}
159+
}
160+
return nil
159161
}
160162

161-
func (c *connection) dispatcher(message *Data) {
163+
func (c *connection) dispatcher(p []byte) {
162164
logger.Debug("Dispatcher: ", c.state)
163165
switch c.state {
164166
case STATE_INIT:
165167
logger.Debug("STATE_INIT")
168+
var message Data
169+
if err := json.Unmarshal(p, &message); err != nil {
170+
logger.Panic(err)
171+
}
166172
if message.ConnectionState == STATE_CONNECT {
167173
d := new(Data)
168174
d.ConnectionState = STATE_CONNECT
@@ -181,13 +187,7 @@ func (c *connection) dispatcher(message *Data) {
181187
}
182188
case STATE_CONNECTED:
183189
logger.Debug("STATE_CONNECTED")
184-
if message.ConnectionState == STATE_CONNECTED {
185-
logger.Debug("Data received: length ", len(message.Payload))
186-
c.server.toIface <- message.Payload
187-
}
188-
case STATE_DISCONNECT:
189-
190-
190+
c.server.toIface <- p
191191
}
192192
}
193193

vpn/server.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ func NewServer(cfg ServerConfig) error {
8787
vpnServer.ipnet = &net.IPNet{ip, subnet.Mask}
8888
vpnServer.ippool.subnet = subnet
8989

90-
//fixMSS(iface.Name(), true)
91-
9290
go vpnServer.cleanUp()
9391

9492
go vpnServer.run()

0 commit comments

Comments
 (0)