Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
781c6047ca
|
||
|
|
51d76cd451
|
||
|
|
16e409727b
|
@@ -5,3 +5,5 @@ $state_file = "state.json"; // The location for the state file. If it does not e
|
||||
$error_reporting = false; // Whether PHP errors should be shown. Not recommended for production.
|
||||
|
||||
$lobby_timeout_seconds = 30; // How long it should take for unresponsive lobbies to get auto-delisted.
|
||||
|
||||
$check_reachability = true; // Whether to reach out to the lobby listing host to confirm the lobby is reachable.
|
||||
|
||||
@@ -28,16 +28,38 @@ if ($error_reporting) {
|
||||
ini_set('display_errors', 1);
|
||||
}
|
||||
|
||||
if (!file_exists($state_file)) {
|
||||
$reachable = false;
|
||||
if (isset($_GET["new"])) {
|
||||
if ($check_reachability) {
|
||||
require "enet_probe.php";
|
||||
$host = $_SERVER['REMOTE_ADDR'];
|
||||
if (!empty($_GET["host"])) {
|
||||
$host = $_GET["host"];
|
||||
}
|
||||
$port = 50403;
|
||||
if (!empty($_GET["port"]) && is_numeric($_GET["port"])) {
|
||||
$port = intval($_GET["port"]);
|
||||
}
|
||||
$reachable = enet_probe($host, $port);
|
||||
}
|
||||
else {
|
||||
$reachable = true;
|
||||
}
|
||||
}
|
||||
|
||||
$dirname = dirname($state_file);
|
||||
if (!is_dir($dirname)) {
|
||||
mkdir($dirname, 0777, true);
|
||||
}
|
||||
|
||||
file_put_contents($state_file, json_encode([ "lobbies" => []]));
|
||||
}
|
||||
$state_fp = fopen($state_file, "c+");
|
||||
flock($state_fp, LOCK_EX);
|
||||
|
||||
$state = json_decode(file_get_contents($state_file), true);
|
||||
$contents = stream_get_contents($state_fp);
|
||||
$state = json_decode($contents, true);
|
||||
if (empty($state)) {
|
||||
$state = ["lobbies" => []];
|
||||
}
|
||||
|
||||
$cutoff_timestamp = time() - $lobby_timeout_seconds;
|
||||
foreach (array_keys($state["lobbies"]) as $id) {
|
||||
@@ -63,16 +85,6 @@ if (isset($_GET["list"])) {
|
||||
echo json_encode(["lobbies" => $list]);
|
||||
}
|
||||
elseif (isset($_GET["new"])) {
|
||||
require "enet_probe.php";
|
||||
$host = $_SERVER['REMOTE_ADDR'];
|
||||
if (!empty($_GET["host"])) {
|
||||
$host = $_GET["host"];
|
||||
}
|
||||
$port = 50403;
|
||||
if (!empty($_GET["port"]) && is_numeric($_GET["port"])) {
|
||||
$port = intval($_GET["port"]);
|
||||
}
|
||||
$reachable = enet_probe($host, $port);
|
||||
header("Content-Type: application/json");
|
||||
if ($reachable) {
|
||||
$id = generate_uuid_v4();
|
||||
@@ -148,4 +160,9 @@ else {
|
||||
echo json_encode(["status" => "OK"]);
|
||||
}
|
||||
|
||||
file_put_contents($state_file, json_encode($state, JSON_PRETTY_PRINT));
|
||||
ftruncate($state_fp, 0);
|
||||
rewind($state_fp);
|
||||
fwrite($state_fp, json_encode($state, JSON_PRETTY_PRINT));
|
||||
fflush($state_fp);
|
||||
flock($state_fp, LOCK_UN);
|
||||
fclose($state_fp);
|
||||
|
||||
Reference in New Issue
Block a user