forked from lzjluzijie/websocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
muxclient.go
81 lines (66 loc) · 1.36 KB
/
muxclient.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package core
import (
"net"
)
func (muxWS *MuxWebSocket) ClientListen() {
for {
m, err := muxWS.ReceiveMessage()
if err != nil {
logger.Debugf(err.Error())
return
}
//get conn and send message
conn := muxWS.GetMuxConn(m.ConnID)
err = conn.HandleMessage(m)
if err != nil {
logger.Debugf(err.Error())
continue
}
}
}
func (client *Client) OpenMux() (err error) {
wsConn, _, err := client.Dialer.Dial(client.URL.String(), map[string][]string{
"WebSocks-Mux": {"mux"},
})
if err != nil {
return
}
ws := &WebSocket{
conn: wsConn,
}
muxWS := NewMuxWebSocket(ws)
client.MuxWS = muxWS
return
}
func (client *Client) DialMuxConn(host string, conn *net.TCPConn) {
muxConn := NewMuxConn(client.MuxWS)
err := muxConn.DialMessage(host)
if err != nil {
logger.Errorf(err.Error())
err = client.OpenMux()
if err != nil {
logger.Errorf(err.Error())
}
return
}
muxConn.muxWS.PutMuxConn(muxConn)
logger.Debugf("dialed mux for %s", host)
muxConn.Run(conn)
return
}
//client dial remote
func (conn *MuxConn) DialMessage(host string) (err error) {
m := &Message{
Method: MessageMethodDial,
MessageID: 18446744073709551615,
ConnID: conn.ID,
Data: []byte(host),
}
logger.Debugf("dial for %s", host)
err = conn.muxWS.SendMessage(m)
if err != nil {
return
}
logger.Debugf("%d %s", conn.ID, host)
return
}