winzik/docker/relay/room-id.patch

60 lines
2.8 KiB
Diff

--- a/php/src/Server/RelayServer.php
+++ b/php/src/Server/RelayServer.php
@@ -250,7 +250,7 @@
}
break;
case 'join':
- $code = strtoupper(trim((string)($message['room'] ?? '')));
+ $code = self::roomCode($message['room'] ?? '');
if (!isset($this->rooms[$code])) {
$this->error($id, 'no-room', "no room $code");
break;
@@ -264,7 +264,7 @@
break;
case 'watch':
// a watcher (a web page): the room, its chat, never a player
- $code = strtoupper(trim((string)($message['room'] ?? '')));
+ $code = self::roomCode($message['room'] ?? '');
if (!isset($this->rooms[$code])) {
$this->error($id, 'no-room', "no room $code");
break;
@@ -306,12 +306,37 @@
private function create(int $id, array $message, bool $public, bool $watch = false): void
{
$this->leave($id);
- $code = $this->newCode();
+ $custom = self::longId($message['room'] ?? null);
+ if ($custom !== null) {
+ // a client-chosen long id (IDE Remote: unguessable, never listed); an existing one is entered again (the host reconnecting)
+ if (isset($this->rooms[$custom])) {
+ if (count($this->rooms[$custom]['peers']) >= $this->rooms[$custom]['max']) {
+ $this->error($id, 'room-full', 'room is full');
+ return;
+ }
+ $this->enter($id, $custom);
+ return;
+ }
+ $public = false;
+ }
+ $code = $custom ?? $this->newCode();
$max = max(2, min(Protocol::MAX_PEERS, (int)($message['max'] ?? 8)));
$this->rooms[$code] = ['max' => $max, 'meta' => self::meta($message), 'public' => $public, 'locked' => false,
'host' => -1, 'peers' => [], 'watchers' => [], 'info' => [], 'empty' => null];
$this->enter($id, $code, $watch);
- $this->log("#$id created $code" . ($public ? ' (public)' : '') . ($watch ? ' (watching)' : ''));
+ $this->log("#$id created " . ($custom !== null ? 'a private room' : $code) . ($public ? ' (public)' : '') . ($watch ? ' (watching)' : ''));
+ }
+
+ /** A client-chosen room id: 32 to 64 characters of base64url, case kept (null for anything else). */
+ private static function longId(mixed $room): ?string
+ {
+ return is_string($room) && preg_match('/^[A-Za-z0-9_-]{32,64}\z/', $room) ? $room : null;
+ }
+
+ private static function roomCode(mixed $room): string
+ {
+ $room = trim((string)$room);
+ return self::longId($room) ?? strtoupper($room);
}
private static function meta(array $message): array