forked from zhaojh329/rttys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
broker.go
212 lines (179 loc) · 5.22 KB
/
broker.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"encoding/binary"
"crypto/x509"
"github.com/gorilla/websocket"
jsoniter "github.com/json-iterator/go"
"github.com/rs/zerolog/log"
"github.com/zhaojh329/rttys/client"
"github.com/zhaojh329/rttys/config"
)
type session struct {
devid string
devsid byte
u client.Client
}
type broker struct {
cfg *config.Config
devices map[string]client.Client
loginAck chan *loginAckMsg
logout chan string
register chan client.Client
unregister chan client.Client
waitLoginUsers map[string]client.Client
sessions map[string]*session
cmdReq chan *commandReq
webCon chan *webNewCon
webReq chan *webReq
termMessage chan *termMessage
userMessage chan *usrMessage
cmdMessage chan []byte
webMessage chan *webResp
devCertPool *x509.CertPool
}
func newBroker(cfg *config.Config) *broker {
return &broker{
cfg: cfg,
loginAck: make(chan *loginAckMsg, 1000),
logout: make(chan string, 1000),
register: make(chan client.Client, 1000),
unregister: make(chan client.Client, 1000),
devices: make(map[string]client.Client),
waitLoginUsers: make(map[string]client.Client),
sessions: make(map[string]*session),
cmdReq: make(chan *commandReq, 1000),
webCon: make(chan *webNewCon, 1000),
webReq: make(chan *webReq, 1000),
termMessage: make(chan *termMessage, 1000),
userMessage: make(chan *usrMessage, 1000),
cmdMessage: make(chan []byte, 1000),
webMessage: make(chan *webResp, 1000),
}
}
func (br *broker) run() {
for {
select {
case c := <-br.register:
devid := c.DeviceID()
if c.IsDevice() {
err := byte(0)
msg := "OK"
if _, ok := br.devices[devid]; ok {
log.Error().Msg("Device ID conflicting: " + devid)
msg = "ID conflicting"
err = 1
} else if br.cfg.Token != "" && c.(*device).token != br.cfg.Token {
log.Error().Msg("Invalid token from terminal device")
msg = "Invalid token"
err = 1
} else {
br.devices[devid] = c
log.Info().Msg("New device: " + devid)
}
c.WriteMsg(msgTypeRegister, append([]byte{err}, msg...))
} else {
if dev, ok := br.devices[devid]; ok {
if _, ok := br.waitLoginUsers[devid]; ok {
userLoginAck(loginErrorBusy, c)
log.Error().Msg("login fail, device busy")
} else {
br.waitLoginUsers[devid] = c
dev.WriteMsg(msgTypeLogin, []byte{})
}
} else {
userLoginAck(loginErrorOffline, c)
log.Error().Msgf("Not found the device '%s'", devid)
}
}
case c := <-br.unregister:
id := c.DeviceID()
if c.IsDevice() {
if _, ok := br.devices[id]; ok {
delete(br.devices, id)
}
for sid, s := range br.sessions {
if s.devid == id {
s.u.Close()
delete(br.sessions, sid)
log.Info().Msg("Delete session: " + sid)
}
}
} else {
sid := c.(*user).sid
if s, ok := br.sessions[sid]; ok {
delete(br.sessions, sid)
c.Close()
if dev, ok := br.devices[s.devid]; ok {
dev.WriteMsg(msgTypeLogout, []byte{sid[len(sid)-1] - '0'})
}
log.Info().Msg("Delete session: " + sid)
}
}
case msg := <-br.loginAck:
if c, ok := br.waitLoginUsers[msg.devid]; ok {
if msg.isBusy {
userLoginAck(loginErrorBusy, c)
log.Error().Msg("login fail, device busy")
} else {
sid := msg.devid + string(msg.sid+'0')
br.sessions[sid] = &session{msg.devid, msg.sid, c}
u := c.(*user)
u.sid = sid
userLoginAck(loginErrorNone, c)
log.Info().Msg("New session: " + sid)
}
delete(br.waitLoginUsers, msg.devid)
}
// device active logout
// typically, executing the exit command at the terminal will case this
case sid := <-br.logout:
if s, ok := br.sessions[sid]; ok {
delete(br.sessions, sid)
s.u.Close()
log.Info().Msg("Delete session: " + sid)
}
// from device, includes terminal data and file data
case msg := <-br.termMessage:
if s, ok := br.sessions[msg.sid]; ok {
s.u.WriteMsg(websocket.BinaryMessage, msg.data)
}
case msg := <-br.userMessage:
if s, ok := br.sessions[msg.sid]; ok {
if dev, ok := br.devices[s.devid]; ok {
devsid := msg.sid[len(msg.sid)-1] - '0'
data := msg.data
if msg.typ == websocket.BinaryMessage {
if data[0] == 1 {
dev.WriteMsg(msgTypeFile, data[1:])
} else {
dev.WriteMsg(msgTypeTermData, append([]byte{devsid}, data[1:]...))
}
} else {
typ := jsoniter.Get(data, "type").ToString()
switch typ {
case "winsize":
b := [5]byte{devsid}
cols := jsoniter.Get(data, "cols").ToUint()
rows := jsoniter.Get(data, "rows").ToUint()
binary.BigEndian.PutUint16(b[1:], uint16(cols))
binary.BigEndian.PutUint16(b[3:], uint16(rows))
dev.WriteMsg(msgTypeWinsize, b[:])
}
}
}
} else {
log.Error().Msg("Not found sid: " + msg.sid)
}
case req := <-br.cmdReq:
req.dev.WriteMsg(msgTypeCmd, req.data)
case c := <-br.webCon:
handleWebCon(br, c)
case req := <-br.webReq:
handleWebReq(req)
case data := <-br.cmdMessage:
handleCmdResp(br, data)
case resp := <-br.webMessage:
handleWebResp(resp)
}
}
}