-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.lua
372 lines (321 loc) · 10.6 KB
/
manager.lua
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
local proto=require'proto'
local wire=require'wire'
local copas=require'copas'
local crypto=require'crypto'
local mutterbotreq = require'mutterbot_reqs'
local bit=require'bit'
local rshift,lshift,bor,band = bit.rshift,bit.lshift,bit.bor,bit.band
require'coxpcall'
local config = require'config'
local mbcs = "127.0.0.99"
local version = proto.Version {
version = 0x0000010200, release = "alpha", os = "OSv" }:save()
local manager = {
user_cnt = 0,
user = {}, -- key is socket (cs) -> {use_udp,ipaddr,port,state}
session = {}, -- key is session uid -> socket (cs)
ip = {}, -- key is ipaddr -> cs or nil
uid = 1, -- ID counter
co = nil, -- Manager coroutine handle
READY = 1,
REGISTER = 2,
REQUEST = 3,
REQUEST_UDP = 4,
TERMINATE = 5
}
local codecversion = proto.CodecVersion {
alpha = 2147483637,
beta = 0,
prefer_alpha = true}:save()
local cryptsetup = proto.CryptSetup {
key=crypto.rand.bytes(16),
client_nonce=crypto.rand.bytes(16),
server_nonce=crypto.rand.bytes(16) }:save()
local rootchannelstate = proto.ChannelState {
channel_id = 0, parent = 0, name = config.channel_name, description = config.channel_description }:save()
local serverconfig = proto.ServerConfig {
max_bandwidth = config.max_bandwidth,
allow_html=true,
message_length = 128 }:save()
local function handle_version(cs,msg)
cs:send(wire.make_packet(0,version))
end
local function broadcast(msg_p,ocs)
for cs,_ in pairs(manager.user) do
if cs ~= ocs and cs ~= mbcs then
cs:send(msg_p)
end
end
end
local function audio_broadcast(msg_p,ocs)
for cs,user in pairs(manager.user) do
if cs ~= ocs then
if (user.use_udp) then
udp_send(msg_p,user.ipaddr)
else
cs:send(msg_p)
end
end
end
end
local function send_user_states(ncs)
-- tell everybody about new user
local nu_p = wire.make_packet(proto.USERSTATE,manager.user[ncs].state:save())
print("EVERYBODY Here is user:", manager.user[ncs].state.name,
"Channel:", manager.user[ncs].state.channel_id,
"Session id:", manager.user[ncs].state.session)
broadcast(nu_p,ncs)
-- tell new user about everybody
for cs,user in pairs(manager.user) do
if (manager.user[ncs].state) then
print(manager.user[ncs].state.name, "Here is ",user.state.name,
user.state.session)
ncs:send(wire.make_packet(proto.USERSTATE, user.state:save()))
end
end
end
local function add_user(cs,aname)
if cs == nil then return end
print("Adding user...",manager.uid,cs)
local sid = manager.uid
local ipaddr = manager.user[cs].ipaddr
manager.user[cs].state = proto.UserState { name = aname,
channel_id = 0,
actor = 1,
session = sid,
user_id = sid }
manager.session[sid] = cs
manager.ip[ipaddr]=cs
manager.user[cs].use_udp=false
manager.uid = manager.uid + 1
manager.user_cnt = manager.user_cnt + 1
return sid
end
local function remove_user(cs)
if cs == nil or cs == mbcs then return nil end
local ipaddr = manager.user[cs].ipaddr
if manager.user[cs].state then
local sid = manager.user[cs].state.session
print("Removing user:", manager.user[cs].state.name)
manager.session[sid] = nil
else
print("Removing unregistered user.")
end
-- BUG BUG This is a problem with clients coming from same IP
manager.ip[ipaddr] = nil
manager.user[cs] = nil
manager.user_cnt = manager.user_cnt - 1
end
local function terminate_user(cs)
if cs == nil or cs == mbcs then return nil end
if manager.user[cs].state then
local ur =proto.UserRemove { session = manager.user[cs].state.session,
actor = 0 }:save()
local rm_p = wire.make_packet(proto.USERREMOVE,ur)
print("Terminate user:", manager.user[cs].state.name)
broadcast(rm_p,cs)
end
remove_user(cs)
end
local function handle_userstate(cs,typ,msg)
local newval = proto.UserState:load(msg)
-- manager.user[cs].state:merge(newval)
for k,v in pairs(newval) do
-- Why am I getting 0 for nil values?????
-- Apparently the pb4lua mumble.lua forces all nil number fields to be 0.
-- Ugh. Bad.
if v ~= nil and v ~= "" and v ~= 0 then
manager.user[cs].state[k] = v
end
end
for k,v in pairs(manager.user[cs].state) do
print(k,"=",v)
end
local us_p = wire.make_packet(proto.USERSTATE,manager.user[cs].state:save())
broadcast(us_p,0)
end
local function handle_auth(cs,typ,msg)
local auth = proto.Authenticate:load(msg)
local sid = add_user(cs,auth.username)
if manager.user_cnt > config.max_users then
local rej = proto.Reject { type = "ServerFull" }:save()
cs:send(wire.make_packet(proto.REJECT, rej))
cs:close()
return nil
end
if auth.username == config.coordinator and
auth.password ~= config.coordinatorpassword or
auth.username ~= config.coordinator and
auth.password ~= config.userpassword
then
local rej = proto.Reject { type = "WrongServerPW" }:save()
cs:send(wire.make_packet(proto.REJECT, rej))
cs:close()
return nil
end
print("Authenticating...")
cs:send(wire.make_packet(proto.CRYPTSETUP,cryptsetup))
cs:send(wire.make_packet(proto.CHANNELSTATE,rootchannelstate))
send_user_states(cs)
cs:send(wire.make_packet(proto.SERVERCONFIG,serverconfig))
local serversync = proto.ServerSync {
session = sid,
max_bandwidth = config.max_bandwidth,
welcome_text = config.welcome_text }:save()
cs:send(wire.make_packet(proto.SERVERSYNC,serversync))
end
local function mutter_request(fromcs,msg)
print("Got request:",msg.message)
local req,a1 = string.match(msg.message,"(%S+)%s*(.*)")
local doreq = mutterbotreq[req] or mutterbotreq["unknown-request"]
local respmsg = proto.TextMessage {
actor = manager.user[mbcs].state.session,
session = msg.session,
channel_id = msg.channel_id,
tree_id = msg.tree_id,
message = doreq.func(mutterbotreq,config,req,a1) }
local respmsg_p = wire.make_packet(proto.TEXTMESSAGE,respmsg:save())
local cs = manager.session[manager.user[fromcs].state.session]
cs:send(respmsg_p)
end
local function handle_textmessage(fromcs,typ,msg)
local txtmsg = proto.TextMessage:load(msg)
txtmsg.actor = manager.user[fromcs].state.session
local txtmsg_p = wire.make_packet(proto.TEXTMESSAGE,txtmsg:save())
local sessions = txtmsg.session
if next(sessions) == nil then
broadcast(txtmsg_p,session)
else
for i,session in pairs(sessions) do
local cs = manager.session[session]
if cs == mbcs then
mutter_request(fromcs,txtmsg)
else
cs:send(txtmsg_p)
end
end
end
end
local function handle_ping(cs,typ,msg)
cs:send(wire.make_packet(proto.PING,msg)) -- echo ping
end
local function handle_permissionquery(cs,typ,msg)
local pq = proto.PermissionQuery {
permissions = config.defpermissions }:save()
cs:send(wire.make_packet(proto.PERMISSIONQUERY,pq))
end
local function handle_udptunnel(cs,typ,msg)
manager.user[cs].use_udp = false
local typtarg = msg:sub(1,1)
local typtargn = string.byte(typtarg)
if typtargn == 0x20 then
print("PING")
cs:send(wire.make_packet(proto.UDPTUNNEL,msg)) -- ping
else
local blob = msg:sub(2,-1)
-- We are going to assume, for now... a maximum of 127 users (uid < 128)
local newblob = typtarg..string.char(manager.user[cs].state.session)..blob
local nmsg = wire.make_packet(proto.UDPTUNNEL,newblob)
if typtargn == 0x80 then
broadcast(nmsg,cs) -- broadcast to all
end
end
end
local function handle_undef(cs,typ,msg)
print("Unhandled:", typ)
end
local handle_msg = {
[proto.VERSION] = handle_version,
[proto.UDPTUNNEL] = handle_udptunnel,
[proto.AUTHENTICATE] = handle_auth,
[proto.PING] = handle_ping,
[proto.REJECT] = handle_undef,
[proto.SERVERSYNC] = handle_undef,
[proto.CHANNELREMOVE] = handle_undef,
[proto.CHANNELSTATE] = handle_undef,
[proto.USERREMOVE] = handle_undef,
[proto.USERSTATE] = handle_userstate,
[proto.BANLIST] = handle_undef,
[proto.TEXTMESSAGE] = handle_textmessage,
[proto.PERMISSIONDENIED] = handle_undef,
[proto.ACL] = handle_undef,
[proto.QUERYUSERS] = handle_undef,
[proto.CRYPTSETUP] = handle_undef,
[proto.CONTEXTACTIONMODIFY] = handle_undef,
[proto.CONTEXTACTION] = handle_undef,
[proto.USERLIST] = handle_undef,
[proto.VOICETARGET] = handle_undef,
[proto.PERMISSIONQUERY] = handle_permissionquery,
[proto.CODECVERSION] = handle_undef,
[proto.USERSTATS] = handle_undef,
[proto.REQUESTBLOB] = handle_undef,
[proto.SERVERCONFIG] = handle_undef,
[proto.SUGGESTCONFIG] = handle_undef
}
local function handle_ready(cs,_,_)
end
local function handle_incoming(cs,typ,msg)
handle_msg[typ](cs,typ,msg)
end
local function handle_incoming_udp(cs,ip,port,data)
local typtarg = wire.from_MSB32(data:sub(1,4))
local ident = data:sub(5,13)
if typtarg == 0x00 then
cs:sendto(string.char(0,1,2,0)..
ident..
wire.toMSB32(manager.user_cnt)..
wire.toMSB32(config.max_users)..
wire.toMSB32(config.max_bandwidth), ip,port)
print("PING")
else
-- handle decryption???? AES-OCB-128
-- http://web.cs.ucdavis.edu/~rogaway/ocb/license.htm says this
-- can't be used for military purposes and I can't find it in
-- libcrypto anyway.. ugh.
--
-- local typtarg = band(rshift(data:byte(1),5),0x7)
-- local target = band(data:byte(1),0x1f)
-- print("udp voice:",data:byte(1),typtarg,target)
end
end
local function handle_register(cs,ip,p)
print("Handling registration")
manager.user[cs] = {ipaddr = ip, port = p}
end
local function handle_terminate(cs,_,_)
print("handle_terminate",cs)
terminate_user(cs)
end
local handle_cmd = {
[manager.READY] = handle_ready,
[manager.REGISTER] = handle_register,
[manager.REQUEST] = handle_incoming,
[manager.REQUEST_UDP] = handle_incoming_udp,
[manager.TERMINATE] = handle_terminate
}
function manager.notify(cmd,...)
local stat,err = coroutine.resume(manager.co,cmd,...)
if not stat then print("notify:", err) end
end
function manager.notify_no_coroutine(cmd,csock,typ,msg,opt)
handle_cmd[cmd](csock,typ,msg,opt)
end
function manager.loop()
local resp = ""
while true do
local cmd,csock,typ,msg,opt = coroutine.yield(resp)
handle_cmd[cmd](csock,typ,msg,opt)
end
end
local function init_mutterbot()
handle_register(mbcs,0,0)
add_user(mbcs,"mutterbot")
end
function manager.start()
init_mutterbot()
print("Starting manager for "..config.channel_name)
manager.co = coroutine.create(manager.loop)
manager.notify(manager.READY)
return manager.co
end
return manager