-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmasterserver.lua
More file actions
executable file
·307 lines (263 loc) · 12 KB
/
masterserver.lua
File metadata and controls
executable file
·307 lines (263 loc) · 12 KB
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
package.path = package.path .. ";script/package/?.lua" -- Required to load the underscore library
package.cpath = package.cpath .. ";lib/lib?.so" -- Requiered to load the liblua_mysql.so library
--[[ Confiruation Part ]]--
local port = 28787 -- Port to bind to
local ip = "0.0.0.0" -- IP address to bind to
local sauermaster_host = "sauerbraten.org" -- Host of the main masterserver to get the serverlist
local sauermaster_port = 28787 -- Port of the main masterserver
local debug_mode = true -- Print debug information
local db = {} -- Initialize the db table
db.type = "text" -- Type of the database: "text" or "mysql"
db.mysql = dofile("mysql.lua") -- Load the MySQL backend
db.text = dofile("text.lua")
-- MySQL Configuration
db.mysql.host = "localhost" -- Host of the MySQL server
db.mysql.port = "3306" -- Port of the MySQL server
db.mysql.name = "suckerserv_authserver" -- Name of the dedicated database
db.mysql.user = "suckerserv" -- User that have a read access to the database
db.mysql.pass = "suckerserv" -- Password for the above user
db.mysql.install = false -- If true, add the content of the "mysql_schema.sql" to the database
db.mysql.schema = "mysql_schema.sql" -- MySQL database schema file to install if db.mysql.install is true
-- Text database configuration
db.text.file = "auth_db" -- Text database file
--[[ Script Part ]]--
local challenges = {} -- Table used to store challenge when auth is requested
local users = {} -- Main users database
local conoutf = {} -- Table used to store console output functions
require("net") -- Load the network library
require("crypto") -- Load the cryptographic library to resolve challenges for authentification
local _ = require "underscore" -- Load the underscore library to split arguments
local master = {} -- Initialize the master table for the listening server
master.server, socket_error = net.tcp_acceptor(ip, port) -- Bind the IP and Port to the listener. If it fails, error is saved in "socket_error" and printed, then the script halt
local sauermaster = {} -- Initialize the sauermaster table for the masterserver client to get the server list
sauermaster.client = net.tcp_client() -- Initialize the masterserver client
-- Print error messages
function conoutf.error(msg, ...)
msg = string.format(msg, ...)
if debug_mode then
print(string.char(27) .. "[" .. "31" .. "m" .. string.char(27) .. "[" .. "1" .. "m" .. os.date("%x %X") .. " [Error] | " .. msg .. string.char(27) .. "[" .. "0" .. "m")
end
end
-- Print info messages
function conoutf.warning(msg, ...)
msg = string.format(msg, ...)
if debug_mode then
print(string.char(27) .. "[" .. "33" .. "m" .. os.date("%x %X") .. " [Warning] | " .. msg .. string.char(27) .. "[" .. "0" .. "m")
end
end
-- Print info messages
function conoutf.info(msg, ...)
msg = string.format(msg, ...)
if debug_mode then
print(string.char(27) .. "[" .. "34" .. "m" .. os.date("%x %X") .. " [Info] | " .. msg .. string.char(27) .. "[" .. "0" .. "m")
end
end
-- Print debug messages
function conoutf.debug(msg, ...)
msg = string.format(msg, ...)
if debug_mode then
print(string.char(27) .. "[" .. "32" .. "m" .. os.date("%x %X") .. " [Debug] | " .. msg .. string.char(27) .. "[" .. "0" .. "m")
end
end
-- A function used to calculate the size of a table as array of dictionary
local function table_size(t)
local max = 0
for k,v in pairs(t) do
max = max + 1
end
return max
end
-- Load the database and print its content
function db.load()
users, err = db[db.type].load(db[db.type])
if err
then
conoutf.warning(err)
conoutf.warning("Using text database")
db.type = "text"
db.load()
else
conoutf.info("Loaded "..db.type.." database")
for domain,user in pairs(users) do
conoutf.debug(domain..":")
for name,conf in pairs(user) do
conoutf.debug(" "..name..": ")
for k,v in pairs(conf) do
conoutf.debug(" "..v.."; ")
end
end
end
end
if table_size(users) < 1 then
conoutf.warning("Users table have less than one domain defined, you'll not able to use authserver functions")
end
end
-- Unload the database
function db.unload()
db[db.type].close()
users = {}
end
-- Reload database - Unused for now
function db.reload()
db.unload()
db.load()
end
--Handle any errors.
function master:handleError(errmsg, retry)
if not errmsg then return end
retry = retry or WAIT_TO_RECONNECT
conoutf.error(errmsg)
end
-- Get server list from Sauerbraten Master Server
local function send_serverlist()
-- Connect and send list request
function sauermaster:connectServer(client)
sauermaster.client:close()
sauermaster.client = net.tcp_client()
sauermaster.client:async_connect(sauermaster_host, sauermaster_port, function(errmsg)
if errmsg then
master:handleError(errmsg)
return
end
local localAddress = sauermaster.client:local_endpoint()
conoutf.debug(string.format("[Client] : Local socket address %s:%s", localAddress.ip, localAddress.port))
sauermaster.client:async_send("list\n", function(errmsg)
if errmsg then
master:handleError(errmsg)
return
end
sauermaster:readData(sauermaster.client)
end)
end)
end
--Main Data Loop
function sauermaster:readData(client)
sauermaster.client:async_read_until("\n", function(data)
if data then
sauermaster:processData(client, data)
sauermaster:readData(sauermaster.client)
else
sendmsg("]")
for i,line in ipairs(_.to_array(string.gmatch(script, "[^\n]+"))) do
sendmsg(line)
end
sauermaster.client:close()
masterserver:close()
end
end)
end
--Process data read from the server and send it to the client.
function sauermaster:processData(client, data)
if data == "" then return end
data = string.gsub(data, "addserver ", "")
data = string.gsub(data, "\n", "")
conoutf.debug("[Client] : "..data)
sendmsg('"'..data..'"')
end
-- Initiate Connection
sauermaster:connectServer(sauermaster.client)
end
-- Generate chalauth challenge from a public key
local function generate_challenge(key)
local key = crypto.sauerecc.key(key)
local gen_challenge = key:generate_challenge()
return gen_challenge
end
-- Send a response to the client
local function sendmsg(msg)
local remote_endpoint = masterserver:remote_endpoint(server)
conoutf.debug("[Output] | (%s:%s) : %s\n", remote_endpoint.ip, remote_endpoint.port, msg)
if not allow_stream then return end
masterserver:async_send(msg .. "\n", function(success) end)
end
-- Main loop: Accept client connection, read received data, repeat
local function accept_next(master_server)
master_server:async_accept(function(server)
masterserver = server
local remote_endpoint = masterserver:remote_endpoint(server)
conoutf.debug("[Input] | (%s:%s) : Connection accepted", remote_endpoint.ip, remote_endpoint.port)
allow_stream = true
master:readData()
accept_next(master.server)
end)
end
-- Read data from client
function master:readData()
masterserver:async_read_until("\n", function(data)
if data then
master:processData(master.server, data)
master:readData(master.server)
else
master:handleError("Read error")
end
end)
end
-- Process data read from the client.
function master:processData(server, data)
local remote_endpoint = masterserver:remote_endpoint(server)
conoutf.debug("[Input] | (%s:%s) : %s", remote_endpoint.ip, remote_endpoint.port, string.gsub(data, "\n$", ""))
-- List handler
if data.find(data,"list") then
sendmsg("serverlist = [")
send_serverlist(masterserver)
end
-- ReqAuth Handler
if string.match(data,"reqauth %d+ %w+ .*") then
local arguments = _.to_array(string.gmatch(data, "[^ \n]+"))
local request_id, name, domain = tonumber(arguments[2]), arguments[3]:lower(), (arguments[4] or "")
if not users[domain] then conoutf.debug(string.format("[Auth] | (%s:%s) : auth n°%s: Domain '%s' doesn't exist!", remote_endpoint.ip, remote_endpoint.port, request_id, domain)) return end
if not users[domain][name] or not users[domain][name][1] then conoutf.debug(string.format("[Auth] | (%s:%s) : auth n°%s: User '%s' doesn't exist in domain '%s' !", remote_endpoint.ip, remote_endpoint.port, request_id, name, domain)) return end
challenges[request_id] = generate_challenge(users[domain][name][1])
local challenge_str = challenges[request_id]:to_string()
conoutf.debug("[Auth] | (%s:%s) : Attempting auth n°%d for %s@%s", remote_endpoint.ip, remote_endpoint.port, request_id, name, domain or '')
sendmsg(string.format("chalauth %i %s", request_id, challenge_str))
end
-- ConfAuth Handler
if string.match(data, "confauth %d+ .+") then
local arguments = _.to_array(string.gmatch(data, "[^ \n]+"))
local request_id, answer = tonumber(arguments[2]), arguments[3]
if not challenges[request_id] then return end
local challenge_expected_answer = challenges[request_id]:expected_answer(answer)
if challenge_expected_answer then
conoutf.debug(string.format("[Auth] | (%s:%s) : Succeded auth n°%d with answer %s", remote_endpoint.ip, remote_endpoint.port, request_id, answer))
sendmsg(string.format("succauth %d", request_id))
else
conoutf.debug(string.format("[Auth] | (%s:%s) : Failed auth n°%d with answer %s", remote_endpoint.ip, remote_endpoint.port, request_id, answer))
sendmsg(string.format("failauth %d", request_id))
end
table.remove(challenges, request_id)
end
-- QueryId Handler
if string.match(data, "QueryId %d+ %w+ .*") then
local arguments = _.to_array(string.gmatch(data, "[^ \n]+"))
local request_id, name, domain = tonumber(arguments[2]), arguments[3]:lower(), (arguments[4] or "")
if not users[domain] then
conoutf.debug(string.format("[Auth] | (%s:%s) : auth n°%s: Domain '%s' doesn't exist!", remote_endpoint.ip, remote_endpoint.port, request_id, domain))
sendmsg(string.format("DomainNotFound %d", request_id))
return
end
if not users[domain][name] or not users[domain][name][1] then
conoutf.debug(string.format("[Auth] | (%s:%s) : auth n°%s: User '%s' doesn't exist in domain '%s' !", remote_endpoint.ip, remote_endpoint.port, request_id, name, domain))
sendmsg(string.format("NameNotFound %d", request_id))
return
end
conoutf.debug(string.format("[Auth] | (%s:%s) : auth n°%s: User '%s' found in domain '%s' with '%s' rights", remote_endpoint.ip, remote_endpoint.port, request_id, name, domain, users[domain][name][2]))
sendmsg(string.format("FoundId %d %s", request_id, users[domain][name][2]))
end
end
-- Open the script to send to the client requesting server list
file = assert(io.open("sauer_masterscript", "r"))
script = file:read("*all")
-- Check if the port was correctly binded
if not master.server then
conoutf.error("Failed to open a listen socket on " .. ip .. ":" .. port .. ": " .. socket_error)
return false
else
master.server:listen()
end
conoutf.info("Sauerbraten MasterServer listening on " .. ip .. ":" .. port)
-- Load the database
db.load()
conoutf.info("Ready")
-- Enter the main loop
accept_next(master.server)