forked from zhaojh329/rttys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bridge.go
179 lines (156 loc) · 5.7 KB
/
bridge.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
/*
* Copyright (C) 2017 Jianhui Zhao <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
package main
import (
"log"
"time"
"encoding/json"
"github.com/gorilla/websocket"
)
type Bridge struct {
// Registered devices.
devices map[string]*Client
// Registered users.
sessions map[string]*Session
// Register requests from the clients.
register chan *Client
// Unregister requests from clients.
unregister chan *Client
// Buffered channel of inbound messages.
inbound chan *wsMessage
}
type Session struct {
dev *Client
user *Client
}
func newBridge() *Bridge {
return &Bridge{
register: make(chan *Client),
unregister: make(chan *Client),
devices: make(map[string]*Client),
sessions: make(map[string]*Session),
inbound: make(chan *wsMessage, 100),
}
}
func (br *Bridge) newSession(user *Client) bool {
devid := user.devid
sid := generateSID(devid)
if dev, ok := br.devices[devid]; ok {
br.sessions[sid] = &Session{dev, user}
user.sid = sid
// Write to user
pkt := rttyPacketNew(RTTY_PACKET_LOGINACK)
pkt.PutString(RTTY_ATTR_SID, sid)
pkt.PutU8(RTTY_ATTR_CODE, 0)
user.wsWrite(websocket.BinaryMessage, pkt.Bytes())
// Write to device
pkt = rttyPacketNew(RTTY_PACKET_LOGIN)
pkt.PutString(RTTY_ATTR_SID, sid)
dev.wsWrite(websocket.BinaryMessage, pkt.Bytes())
log.Println("New session:", sid)
return true
} else {
// Write to user
pkt := rttyPacketNew(RTTY_PACKET_LOGINACK)
pkt.PutString(RTTY_ATTR_SID, sid)
pkt.PutU8(RTTY_ATTR_CODE, 1)
user.wsWrite(websocket.BinaryMessage, pkt.Bytes())
log.Println("Device", devid, "offline")
return false
}
}
func delSession(sessions map[string]*Session, sid string) {
if session, ok := sessions[sid]; ok {
delete(sessions, sid)
session.user.wsClose()
log.Println("Delete session: ", sid)
if session.dev != nil {
pkt := rttyPacketNew(RTTY_PACKET_LOGOUT)
pkt.PutString(RTTY_ATTR_SID, sid)
session.dev.wsWrite(websocket.BinaryMessage, pkt.Bytes())
}
}
}
func (br *Bridge) run() {
for {
select {
case client := <- br.register:
if client.isDev {
if dev, ok := br.devices[client.devid]; ok {
pkt := rttyPacketNew(RTTY_PACKET_ANNOUNCE)
pkt.PutU8(RTTY_ATTR_CODE, 1)
dev.wsWrite(websocket.BinaryMessage, pkt.Bytes())
log.Println("ID conflicting:", dev.devid)
} else {
br.devices[client.devid] = client
log.Printf("New device:id('%s'), description('%s')", client.devid, client.description)
}
} else {
// From user browse
if !br.newSession(client) {
time.AfterFunc(500 * time.Millisecond, client.wsClose)
}
}
case client := <- br.unregister:
if client.isDev {
client.wsClose()
if dev, ok := br.devices[client.devid]; ok {
log.Printf("Dead device:id('%s'), description('%s')", dev.devid, dev.description)
delete(br.devices, dev.devid)
}
for sid, session := range br.sessions {
if session.dev.devid == client.devid {
session.dev = nil
delSession(br.sessions, sid)
}
}
} else {
delSession(br.sessions, client.sid)
}
case msg := <- br.inbound:
if msg.msgType == websocket.TextMessage {
res := CommandResult{}
json.Unmarshal(msg.data, &res)
msg.c.mutex.Lock()
cmd := msg.c.cmd[res.ID]
msg.c.mutex.Unlock()
cmd <- msg
} else {
pkt := rttyPacketParse(msg.data)
if session, ok := br.sessions[pkt.sid]; ok {
if (msg.c.isDev) {
if pkt.typ == RTTY_PACKET_LOGOUT {
session.dev = nil
delSession(br.sessions, pkt.sid)
} else {
session.user.wsWrite(websocket.BinaryMessage, msg.data)
}
} else {
session.dev.wsWrite(websocket.BinaryMessage, msg.data)
if pkt.typ == RTTY_PACKET_UPFILE && len(session.dev.outbound) > 10 {
pkt := rttyPacketNew(RTTY_PACKET_UPFILE)
pkt.PutU8(RTTY_ATTR_CODE, 5)
session.user.wsWrite(websocket.BinaryMessage, pkt.Bytes())
}
}
}
}
}
}
}