diff options
| author | Lizzy Fleckenstein <lizzy@vlhl.dev> | 2026-06-03 17:36:51 +0200 |
|---|---|---|
| committer | Lizzy Fleckenstein <lizzy@vlhl.dev> | 2026-06-03 17:39:15 +0200 |
| commit | a42c94e103ecf7cb365a8888c3f5afc785def284 (patch) | |
| tree | 7522b24cf920dd2c62b733c1b65ab6a169977b36 | |
| parent | d9996907bc5fb78449a3e3b6192fe57d76056071 (diff) | |
| download | r6p-a42c94e103ecf7cb365a8888c3f5afc785def284.tar.xz | |
util: better rand_string, add mkdir
| -rw-r--r-- | client.lua | 11 | ||||
| -rw-r--r-- | common.lua | 31 | ||||
| -rwxr-xr-x | matchsrv.lua | 15 | ||||
| -rw-r--r-- | server.lua | 17 | ||||
| -rw-r--r-- | util.lua | 50 |
5 files changed, 73 insertions, 51 deletions
@@ -1,5 +1,6 @@ local enet = require("enet") local socket = require("socket") +local util = require("util") local common = require("common") local client = {} @@ -18,7 +19,7 @@ local function connect(clt, addr) end function client.join(invite, match_addr) - local invite_dec = common.base64_dec(invite) + local invite_dec = util.base64_dec(invite) if not invite_dec then return nil, "invalid_invite" end @@ -28,7 +29,7 @@ function client.join(invite, match_addr) local clt = create_client(secret) clt.match = clt.host:connect(match_addr or common.default_match_addr) - clt.match:send(common.json_enc({ type = "match_join", game_id = game_id })) + clt.match:send(util.json_enc({ type = "match_join", game_id = game_id })) clt.match_req = socket.gettime() clt.game_id = game_id clt.status = "wait_match" @@ -65,7 +66,7 @@ function client.update(clt) local event = clt.host:service(20) while event do if event.type == "receive" then - local pkt = common.json_dec(event.data) + local pkt = util.json_dec(event.data) if pkt then if event.peer == clt.match and clt.status == "wait_match" then handle_match(clt, pkt) @@ -77,9 +78,9 @@ function client.update(clt) end elseif event.type == "connect" then if event.peer == clt.match and clt.status == "wait_match" then - clt.match:send(common.json_enc({ type = "match_join", game_id = common.base64_enc(clt.game_id) })) + clt.match:send(util.json_enc({ type = "match_join", game_id = util.base64_enc(clt.game_id) })) elseif event.peer == clt.server and clt.status == "wait_server" then - clt.server:send(common.json_enc({ type = "server_hi", secret = common.base64_enc(clt.secret) })) + clt.server:send(util.json_enc({ type = "server_hi", secret = util.base64_enc(clt.secret) })) else event.peer:disconnect_now() end @@ -1,35 +1,4 @@ -local base64 = require("vendor.base64") -local json = require("vendor.JSON") - -local table_unpack = table.unpack or unpack -local function rand_string(n) - local b = {} - for i = 1, n do - table.insert(b, math.random(0, 255)) - end - return string.char(table_unpack(b)) -end - -local function base64_dec(x) - local succ, dec = pcall(base64.decode, x) - if succ then return dec end -end - -local function json_dec(x) - local succ, dec = pcall(json.decode, json, x) - if succ then return dec end -end - -local function json_enc(x) - return json:encode(x) -end - return { - rand_string = rand_string, - base64_dec = base64_dec, - base64_enc = base64.encode, - json_dec = json_dec, - json_enc = json_enc, default_match_addr = "ivy.vlhl.dev:18252", gameid_len = 8, secret_len = 4, diff --git a/matchsrv.lua b/matchsrv.lua index ac394b5..0ffa2b4 100755 --- a/matchsrv.lua +++ b/matchsrv.lua @@ -1,5 +1,6 @@ #!/usr/bin/env lua5.1 local enet = require("enet") +local util = require("util") local common = require("common") local host = enet.host_create("0.0.0.0:18252") @@ -18,21 +19,21 @@ end local function handle(peer, pkt) if pkt.type == "match_register" then remove_game(peer) - local game_id = common.rand_string(common.gameid_len) + local game_id = util.rand_string(common.gameid_len) peer_to_game[peer] = game_id game_to_peer[game_id] = peer - peer:send(common.json_enc({ type = "server_match", game_id = common.base64_enc(game_id) })) + peer:send(util.json_enc({ type = "server_match", game_id = util.base64_enc(game_id) })) print(peer, "registered game") elseif pkt.type == "match_join" then - local game_id = type(pkt.game_id) == "string" and common.base64_dec(pkt.game_id) + local game_id = type(pkt.game_id) == "string" and util.base64_dec(pkt.game_id) if game_id then local server = game_id and game_to_peer[game_id] if server then - server:send(common.json_enc({ type = "server_join", peer_addr = tostring(peer) })) - peer:send(common.json_enc({ type = "client_join", peer_addr = tostring(server) })) + server:send(util.json_enc({ type = "server_join", peer_addr = tostring(peer) })) + peer:send(util.json_enc({ type = "client_join", peer_addr = tostring(server) })) print(peer, "joined game", server) else - peer:send(common.json_enc({ type = "client_join_fail" })) + peer:send(util.json_enc({ type = "client_join_fail" })) print(peer, "failed to join game") end end @@ -45,7 +46,7 @@ while true do local event = host:service(100) while event do if event.type == "receive" then - local pkt = common.json_dec(event.data) + local pkt = util.json_dec(event.data) if pkt then handle(event.peer, pkt) end @@ -1,5 +1,6 @@ local enet = require("enet") local socket = require("socket") +local util = require("util") local common = require("common") local server = {} @@ -8,7 +9,7 @@ function server.create(match_addr) local srv = {} srv.host = enet.host_create() - srv.secret = common.rand_string(common.secret_len) + srv.secret = util.rand_string(common.secret_len) srv.clients = {} srv.match = srv.host:connect(match_addr or common.default_match_addr) @@ -19,7 +20,7 @@ end local function handle_match(srv, pkt) if pkt.type == "server_match" then - local game_id = type(pkt.game_id) == "string" and common.base64_dec(pkt.game_id) + local game_id = type(pkt.game_id) == "string" and util.base64_dec(pkt.game_id) if not game_id then print("[server] server_match: invalid game_id") return @@ -31,7 +32,7 @@ local function handle_match(srv, pkt) end srv.game_id = game_id - srv.invite = common.base64_enc(srv.game_id .. srv.secret) + srv.invite = util.base64_enc(srv.game_id .. srv.secret) elseif pkt.type == "server_join" then if type(pkt.peer_addr) ~= "string" then print("[server] server_join: invalid peer_addr") @@ -43,7 +44,7 @@ end local function handle_client(srv, peer, pkt) if pkt.type == "server_hi" then - local secret = type(pkt.secret) == "string" and common.base64_dec(pkt.secret) + local secret = type(pkt.secret) == "string" and util.base64_dec(pkt.secret) if not secret then print("[server] server_hi: invalid secret") return @@ -57,10 +58,10 @@ local function handle_client(srv, peer, pkt) if secret == srv.secret then print("[server] auth success " .. tostring(peer)) srv.clients[peer] = { peer = peer } - peer:send(common.json_enc({ type = "client_hi" })) + peer:send(util.json_enc({ type = "client_hi" })) else print("[server] auth failure " .. tostring(peer)) - peer:send(common.json_enc({ type = "client_reject" })) + peer:send(util.json_enc({ type = "client_reject" })) peer:disconnect_later() end end @@ -70,7 +71,7 @@ function server.update(srv) local event = srv.host:service(20) while event do if event.type == "receive" then - local pkt = common.json_dec(event.data) + local pkt = util.json_dec(event.data) if pkt then if event.peer == srv.match then handle_match(srv, pkt) @@ -80,7 +81,7 @@ function server.update(srv) end elseif event.type == "connect" then if event.peer == srv.match then - srv.match:send(common.json_enc({ type = "match_register" })) + srv.match:send(util.json_enc({ type = "match_register" })) end print("[server] connect " .. tostring(event.peer)) elseif event.type == "disconnect" then diff --git a/util.lua b/util.lua new file mode 100644 index 0000000..0628691 --- /dev/null +++ b/util.lua @@ -0,0 +1,50 @@ +local base64 = require("vendor.base64") +local json = require("vendor.JSON") +local table_unpack = table.unpack or unpack + +local function base64_dec(x) + local succ, dec = pcall(base64.decode, x) + if succ then return dec end +end + +local function json_dec(x) + local succ, dec = pcall(json.decode, json, x) + if succ then return dec end +end + +local function json_enc(x) + return json:encode(x) +end + +local mkdir, rand_string + +if love then + rand_string = function(n) + local b = {} + for i = 1, n do + table.insert(b, love.math.random(0, 255)) + end + return string.char(table_unpack(b)) + end + mkdir = love.filesystem.mkdir +else + local rand_file + rand_string = function(n) + local rand_file = rand_file or io.open("/dev/random") + return rand_file:read(n) + end + -- awful + mkdir = function(x) + local status = os.execute("mkdir -p " .. x) + return status == true + end +end + +return { + rand_string = rand_string, + mkdir = mkdir, + base64_dec = base64_dec, + base64_enc = base64.encode, + json_dec = json_dec, + json_enc = json_enc, +} |
