142 lines
3.7 KiB
PHP
142 lines
3.7 KiB
PHP
<?php
|
|
|
|
function generate_uuid_v4(): string {
|
|
$bytes = openssl_random_pseudo_bytes(16);
|
|
$bytes[6] = chr(ord($bytes[6]) & 0x0f | 0x40); // set version to 0100
|
|
$bytes[8] = chr(ord($bytes[8]) & 0x3f | 0x80); // set bits 6-7 to 10
|
|
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($bytes), 4));
|
|
}
|
|
|
|
if (file_exists("config.php")) {
|
|
include "config.php";
|
|
}
|
|
|
|
if (empty($state_file)) {
|
|
$state_file = "state.json";
|
|
}
|
|
|
|
if (empty($error_reporting)) {
|
|
$error_reporting = false;
|
|
}
|
|
|
|
if (empty($lobby_timeout_seconds)) {
|
|
$lobby_timeout_seconds = 30;
|
|
}
|
|
|
|
if ($error_reporting) {
|
|
error_reporting(E_ALL);
|
|
ini_set('display_errors', 1);
|
|
}
|
|
|
|
if (!file_exists($state_file)) {
|
|
mkdir($state_file, 0777, true);
|
|
file_put_contents($state_file, json_encode([ "lobbies" => []]));
|
|
}
|
|
|
|
$state = json_decode(file_get_contents($state_file), true);
|
|
|
|
$cutoff_timestamp = time() - $lobby_timeout_seconds;
|
|
foreach (array_keys($state["lobbies"]) as $id) {
|
|
if ($state["lobbies"][$id]["updated_at"] < $cutoff_timestamp) {
|
|
unset($state["lobbies"][$id]);
|
|
}
|
|
}
|
|
|
|
if (isset($_GET["list"])) {
|
|
$list = [];
|
|
foreach ($state["lobbies"] as $id => $lobby) {
|
|
$list[$id] = [
|
|
"id" => $lobby["id"],
|
|
"host" => $lobby["host"],
|
|
"name" => $lobby["name"],
|
|
"creator" => $lobby["creator"],
|
|
"version" => $lobby["version"],
|
|
"created_at" => $lobby["created_at"],
|
|
];
|
|
}
|
|
header("Content-Type: application/json");
|
|
echo json_encode(["lobbies" => $list]);
|
|
}
|
|
elseif (isset($_GET["new"])) {
|
|
require "enet_probe.php";
|
|
$host = $_SERVER['REMOTE_ADDR'];
|
|
if (!empty($_GET["host"])) {
|
|
$host = $_GET["host"];
|
|
}
|
|
$reachable = enet_probe($host, 50403);
|
|
header("Content-Type: application/json");
|
|
if ($reachable) {
|
|
$id = generate_uuid_v4();
|
|
$secret = generate_uuid_v4();
|
|
$version = $_GET["version"] ?? "";
|
|
$creator = $_GET["creator"] ?? "";
|
|
$name = "{$creator}'s lobby";
|
|
if (!empty($_GET["name"])) {
|
|
$name = $_GET["name"];
|
|
}
|
|
$lobby = [
|
|
"id" => $id,
|
|
"secret" => $secret,
|
|
"host" => $host,
|
|
"name" => $name,
|
|
"version" => $version,
|
|
"creator" => $creator,
|
|
"updated_at" => time(),
|
|
"created_at" => time(),
|
|
];
|
|
$state["lobbies"][$id] = $lobby;
|
|
echo json_encode($lobby);
|
|
}
|
|
else {
|
|
http_response_code(424);
|
|
echo json_encode(["error" => "NOT_REACHABLE", "host" => $host]);
|
|
}
|
|
}
|
|
elseif (isset($_GET["update"])) {
|
|
$id = $_GET["id"];
|
|
$secret = $_GET["secret"];
|
|
$lobby = $state["lobbies"][$id];
|
|
header("Content-Type: application/json");
|
|
if (!in_array($id, array_keys($state["lobbies"])) || empty($lobby)) {
|
|
http_response_code(404);
|
|
echo json_encode(["error" => "NOT_FOUND", "id" => $id]);
|
|
}
|
|
elseif ($lobby["secret"] != $secret) {
|
|
http_response_code(403);
|
|
echo json_encode(["error" => "NOT_AUTHORIZED", "id" => $id]);
|
|
}
|
|
else {
|
|
$name = $_GET["name"] ?? "";
|
|
if (!empty($name)) {
|
|
$lobby["name"] = $name;
|
|
}
|
|
$lobby["updated_at"] = time();
|
|
$state["lobbies"][$id] = $lobby;
|
|
echo json_encode(["result" => "updated"]);
|
|
}
|
|
}
|
|
elseif (isset($_GET["close"])) {
|
|
$id = $_GET["id"];
|
|
$secret = $_GET["secret"];
|
|
$lobby = $state["lobbies"][$id];
|
|
header("Content-Type: application/json");
|
|
if (!in_array($id, array_keys($state["lobbies"])) || empty($lobby)) {
|
|
http_response_code(404);
|
|
echo json_encode(["error" => "NOT_FOUND", "id" => $id]);
|
|
}
|
|
elseif ($lobby["secret"] != $secret) {
|
|
http_response_code(403);
|
|
echo json_encode(["error" => "NOT_AUTHORIZED", "id" => $id]);
|
|
}
|
|
else {
|
|
unset($state["lobbies"][$id]);
|
|
echo json_encode(["result" => "closed"]);
|
|
}
|
|
}
|
|
else {
|
|
header("Content-Type: application/json");
|
|
echo json_encode(["status" => "OK"]);
|
|
}
|
|
|
|
file_put_contents($state_file, json_encode($state, JSON_PRETTY_PRINT));
|