1
0
Fork 1

Merge pull request 'SOGS QR Caching & backend improvements' (#17) from gravel/sessioncommunities.online:backend-caching into main

Reviewed-on: #17
pull/22/head
SomeGuy 1 year ago
commit adb09de412

@ -7,6 +7,8 @@
$TEMPLATES_ROOT="$PROJECT_ROOT/sites";
$LANGUAGES_ROOT="$PROJECT_ROOT/languages";
include_once "$PROJECT_ROOT/php/utils/logging.php";
// set timeout for file_get_contents()
ini_set('default_socket_timeout', 6); // in seconds, default is 60
@ -16,6 +18,6 @@
// do not report warnings (timeouts, SSL/TLS errors)
error_reporting(E_ALL & ~E_WARNING);
date_default_timezone_set('UTC');
?>

@ -1,32 +1,71 @@
# First goal is always run with just `make`
all:
/bin/php php/update-listing.php
port = 8081
output = output
data:
# First goal is the default with `make`.
# List make scripts.
list:
grep "^[^[:space:]]*:" Makefile --before-context=1 --group-separator=""
## Using make dependencies is duplicating behaviour but reads better.
# /bin/php php/update-listing.php
# Refresh listing and generate HTML.
all: fetch html
# Fetch room listing.
fetch:
/bin/php php/fetch-servers.php
# Generate HTML from data.
html:
/bin/php php/generate-html.php
dev:
xdg-open http://localhost:8080
# Last item run in foreground to receive interrupts.
# Serve a local copy which responds to file changes.
dev: open
make server &
make watchdog
lan-dev:
# Serve a local copy on LAN which responds to file changes.
lan-dev: open
ip addr | fgrep -e ' 192.' -e ' 10.'
xdg-open http://localhost:8080
make lan-server &
make watchdog
# Serve a local copy.
server:
/bin/php -S localhost:8080 -t output
/bin/php -S localhost:$(port) -t $(output)
# Serve a local copy on all interfaces.
lan-server:
/bin/php -S 0.0.0.0:8080 -t output
/bin/php -S 0.0.0.0:$(port) -t $(output)
# Open locally served page in browser.
open:
xdg-open http://localhost:$(port) >/dev/null 2>/dev/null & disown
# Update html on file change
# Doesn't check for new files
# Update HTML on file change. Doesn't check for new files.
watchdog:
find . | entr -n -s "make html"
# Remove artefacts
clean:
rm -r cache
rm -r output/*.html
# Build everything from scratch and test functionality.
test: clean all open server
# Build everything from scratch and test functionality on LAN.
test-lan: clean all open lan-server
# -- Aliases --
serve: server
lan-serve: lan-server
data: fetch
watch: watchdog

@ -82,7 +82,8 @@
// write output to disk
global $output;
file_put_contents($output, json_encode($info_arrays)); // overwrites existing file
echo("Done. " . count($info_arrays) . " unique Session Communities on " . count_servers($info_arrays) . " servers have been found." . PHP_EOL);
log_info("Done. ");
log_info("Found " . count($info_arrays) . " unique Session Communities on " . count_servers($info_arrays) . " servers." . PHP_EOL);
}
/*
@ -98,13 +99,16 @@
$sd_pre = "https://session.directory/?all=groups" ; // this one has to be expanded first
// get awesome session group list html
log_info("Requesting Awesome Session Group list.");
$asgl_html = file_get_contents($asgl);
// get lokilocker.com html
log_info("Requesting Lokilocker Mods Open Group list.");
$ll_html = file_get_contents($ll);
// get session.directory html
$sd_html = "";
log_info("Requesting session.directory list.");
$sd_pre_html = file_get_contents($sd_pre);
$sd_pattern = "/view_session_group_user_lokinet\.php\?id=\d+/";
preg_match_all($sd_pattern, $sd_pre_html, $sd_links);
@ -116,8 +120,10 @@
$sd_html = $sd_html . file_get_contents($link) . PHP_EOL;
}
log_info("Done fetching sources.");
// merge all html into a single string
return(
return (
$asgl_html . PHP_EOL .
$ll_html . PHP_EOL .
$sd_html . PHP_EOL
@ -260,6 +266,7 @@
$endpoint = "/rooms?all=1";
$json_url = $server_url . $endpoint;
// $json = file_get_contents($json_url);
log_info("Polling $server_url for rooms.");
$json = curl_get_contents($json_url); // circumvents flaky routing
// echo("URL: " . $server_url . " - JSON URL: " . $json_url . PHP_EOL);
// echo("JSON: " . $json . PHP_EOL);
@ -269,6 +276,7 @@
$json_rooms = array();
// if response was not empty
if($json_obj) {
log_info("Received response from $server_url.");
foreach($json_obj as $json_room) {
$token = $json_room->token; // room "name"
$users_per_second = $json_room->active_users / $json_room->active_users_cutoff;
@ -309,6 +317,7 @@
if($legacy_rooms) {
$result = $legacy_rooms;
} else {
log_info("Failed to receive response from $server_url.");
$result = null;
}
}

@ -19,8 +19,10 @@
// Do not render auxilliary PHP files.
if (str_contains("$phppath", "/+") || $phppath[0] == "+")
continue;
$docpath = str_replace($TEMPLATES_ROOT, $DOCUMENT_ROOT, $phppath);
$relpath = str_replace($TEMPLATES_ROOT, "", $phppath);
$docpath = str_replace(".php", ".html", $docpath);
// This works? Yes, yes it does.
@ -28,8 +30,11 @@
// otherwise we could include the documents in an ob_* wrapper.
// Same as shell_exec, except we don't have to escape quotes.
log_info("Generating output for $relpath.");
$document = `cd "$TEMPLATES_ROOT"; php $phppath`;
file_put_contents($docpath, $document);
}
log_info("Done generating HTML.");
?>

@ -1,8 +1,3 @@
Running, please wait...
This script will usually take approximately 4 minutes to run.
It will take longer if the Chinese servers are spasming out
or if you are running this for the first time.
<?php
require_once 'fetch-servers.php';
require_once 'generate-html.php';

@ -0,0 +1,27 @@
<?php
$hrtime_start = hrtime();
$NANOSEC = 1E9;
/**
* Calculate process runtime as [s, ns].
*/
function hrtime_interval() {
global $hrtime_start, $NANOSEC;
list($s, $ns) = hrtime();
list($s0, $ns0) = $hrtime_start;
// Borrow
if ($ns < $ns0) { $s--; $ns += $NANOSEC; }
return [$s - $s0, $ns - $ns0];
}
function runtime_str() {
list($s, $ns) = hrtime_interval();
return
date('i:s.', $s) .
str_pad(intdiv($ns, 1E6), 3, "0", STR_PAD_LEFT);
}
function log_info($msg) {
fwrite(STDERR, "[" . runtime_str() . "] [i] $msg" . PHP_EOL);
}
?>

@ -1,56 +1,33 @@
<?php
/*
* @Deprecated
*/
function room_qr_code_cached($room_id) {
global $QR_CODES;
return "$QR_CODES/$room_id.png";
}
/*
* Takes join URL and derives the invite.png path from it
/**
* Derive URL of the invite code for a given room.
*/
function room_qr_code_native($join_url) {
// Ex.: https://open.getsession.org/session?public_key=[...]
// Goal: https://open.getsession.org/r/session/invite.png
// Note: No @legacy support (Ex.: https://reccacon.com/view/Ukraine/invite.png)
// TODO: How does this behave with unreliable connections to Chinese servers?
$exploded = explode("/", explode("?", $join_url)[0]); // everything before "?"
$png_url =
$exploded[0] . "//" . // https://
$exploded[2] . "/r/" . // open.getsession.org/r/
$exploded[3] . "/invite.png"; // session/invite.png
// fwrite(STDERR, "PNG URL: " . $png_url . PHP_EOL);
return $png_url;
function room_invite_png($room_id, $room) {
return $room->preview_link . "invite.png";
}
/*
* @Deprecated
* Use Google API to generate QR codes and encode them as base64
* Fetch QR codes from SOGS server and encode them as base64
*/
function base64_qr_code($room_id, $join_url, $size = "512x512") {
// Could use http_build_query() instead, but I won't break what works.
// https://developers.google.com/chart/infographics/docs/qr_codes
function base64_qr_code($room_id, $room, $size = "512x512") {
$png_cached = room_qr_code_cached($room_id);
if (file_exists($png_cached))
if (file_exists($png_cached)) {
// fwrite(STDERR, "QR code found for " . $room_id . PHP_EOL);
return base64_encode(file_get_contents($png_cached));
// fwrite(STDERR, "QR code NOT found for " . $room_id . PHP_EOL);
$data = urlencode($join_url);
$api_url =
"https://chart.googleapis.com/chart?cht=qr" .
"&chs=$size" .
"&chl=$data" .
"&chld=L|0";
// error correction level: L = 7%, M = 15%, Q = 25%, H = 30%
// | margin in number of rows
$png = file_get_contents($api_url);
}
// fwrite(STDERR, "QR code NOT found for " . $room_id . PHP_EOL);
log_info("Fetching QR code for $room_id.");
$png = file_get_contents(room_invite_png($room_id, $room));
file_put_contents($png_cached, $png);
return base64_encode($png);
}
file_exists($QR_CODES) or mkdir($QR_CODES, 0700); // @Deprecated
file_exists($QR_CODES) or mkdir($QR_CODES, 0700);
?>
<div id="modal-container">
@ -61,20 +38,11 @@
&times;
</span>
<img
src="data:image/png;base64,<?=base64_qr_code($id, $room->join_link)?>"
alt="Community join link encoded as QR code"
class="qr-code"
loading="lazy"
>
<!--
<img
src="<?=room_qr_code_native($room->join_link)?>"
src="data:image/png;base64,<?=base64_qr_code($id, $room)?>"
alt="Community join link encoded as QR code"
class="qr-code"
loading="lazy"
referrerpolicy="no-referrer"
>
-->
</div>
</div>
<?php endforeach; ?>