diff --git a/.github/release-notes.md b/.github/release-notes.md index 05f73fa..3da485e 100644 --- a/.github/release-notes.md +++ b/.github/release-notes.md @@ -8,6 +8,11 @@ Download `connect-buttons-${VERSION}.pteroext` below, then upload it under **Adm php artisan p:extension:install /path/to/connect-buttons-${VERSION}.pteroext --enable ``` +### What's new in ${VERSION} + +- Join and Copy show the server's domain from the Domains extension when it has one, without the port when an SRV record covers the game. +- The buttons now look like the panel's own buttons and line up with the other extensions above the console. + Requires Pterodactyl Panel 2.0 (2.0-develop or later) and PHP 8.3+. See the README for features and settings. This is a community extension and is not affiliated with or endorsed by Pterodactyl. diff --git a/CHANGELOG.md b/CHANGELOG.md index 9993786..59c2bc3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## 1.1.0 + +- Works with the Domains extension. When the server's main allocation has a working domain, Join and Copy use it instead of the IP. If the domain has an SRV record for the game, the port is left off; otherwise it is shown as domain:port. +- Other ports on the same IP, such as RCON, show the domain too. Ports on another IP keep their IP. +- Without the Domains extension, or without a domain, nothing changes: the IP alias or IP is used as before. +- Join and Copy now look exactly like the panel's own buttons, so their height, corners and font match the rest of the console. Join is the main button and Copy the outlined one. Join is still a normal link, so it can be copied or opened in a new tab. +- The button row has the same 12px gap below it as the other extensions above the console. + ## 1.0.0 - First release. diff --git a/README.md b/README.md index 3985308..a361764 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,8 @@ One-click Join and Copy address buttons on every server's console page, set up a - **Copy address button** for every game. Minecraft addresses leave off the default `:25565`. - **Other ports listed with their notes**, such as "RCON: 1.2.3.4:28016". The notes are the ones you already set on the server's **Network** page, so there is nothing new to fill in. - **Works out the game by itself** from the egg's name. To change it, open **Admin → Eggs → (egg)** and pick a join method under **Connect Buttons**: Steam, Minecraft, FiveM, TeamSpeak, Mumble, copy only, hidden, or your own link with `{ip}` and `{port}` (http, https, steam, fivem, ts3server and mumble links only). -- Uses the allocation's IP alias when one is set. +- **Works with the [Domains](https://github.com/Rustix-Development/pterodactyl-domains) extension.** When the server's main allocation has a working domain, Join and Copy use it instead of the IP: just the name when the domain has an SRV record for the game, otherwise `domain:port`. Other ports on the same IP, such as RCON, show the domain too. Without Domains, or without a domain, the allocation's IP alias (or IP) is used. +- **Matches the console.** Join and Copy look exactly like the panel's own buttons (Join stays a normal link), and the row lines up with Announcements, Custom Links and Next Wipe above the console. ## API diff --git a/extension.json b/extension.json index 5b089e4..b215908 100644 --- a/extension.json +++ b/extension.json @@ -2,7 +2,7 @@ "$schema": "./node_modules/@pterodactyl/sdk/manifest.schema.json", "id": "connect-buttons", "name": "Connect Buttons", - "version": "1.0.0", + "version": "1.1.0", "description": "One-click Join and Copy address buttons on every server's console page, set up automatically for Minecraft, Rust, Source games, FiveM, TeamSpeak and Mumble.", "author": "RustixDev", "icon": "gamepad-2", diff --git a/package-lock.json b/package-lock.json index 99c994a..ab2a2bf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "connect-buttons", - "version": "1.0.0", + "version": "1.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "connect-buttons", - "version": "1.0.0", + "version": "1.1.0", "devDependencies": { "@base-ui/react": "^1.6.0", "@pterodactyl/sdk": "file:./sdk/pterodactyl-sdk-2.0.0-beta.4.tgz", diff --git a/package.json b/package.json index ffd20b2..ae0d7df 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "connect-buttons", "private": true, - "version": "1.0.0", + "version": "1.1.0", "type": "module", "scripts": { "build": "vite build", diff --git a/src/Http/Controllers/ConnectController.php b/src/Http/Controllers/ConnectController.php index 261505b..f3a8a64 100644 --- a/src/Http/Controllers/ConnectController.php +++ b/src/Http/Controllers/ConnectController.php @@ -7,6 +7,7 @@ use Pterodactyl\Models\Server; use Pterodactyl\Services\Extensions\ExtensionSettings; use RustixDev\ConnectButtons\Support\ConnectPresets; +use RustixDev\ConnectButtons\Support\ServerDomains; class ConnectController { @@ -23,18 +24,23 @@ public function show(Server $server): JsonResponse } $default = $server->allocation; - $host = $default ? ($default->ip_alias ?: $default->ip) : null; + $domains = ServerDomains::forServer($server); + $domain = $default ? ($domains[$default->id] ?? null) : null; + + $host = $default ? ($domain['name'] ?? ServerDomains::ip($default)) : null; + // An SRV record for the game's port means players need only the name. + $withPort = ! ($domain !== null && $domain['srv'] && $domain['port'] === $default?->port); + $hidden = $type === 'none' || $default === null; return new JsonResponse([ 'type' => $type, - 'url' => $type === 'none' || $default === null - ? null - : ConnectPresets::url($type, $host, $default->port, $fields['template'] ?? null), - 'address' => $type === 'none' || $default === null ? null : ConnectPresets::address($type, $host, $default->port), + 'url' => $hidden ? null : ConnectPresets::url($type, $host, $default->port, $fields['template'] ?? null, $withPort), + 'address' => $hidden ? null : ConnectPresets::address($type, $host, $default->port, $withPort), + 'domain' => $domain['name'] ?? null, 'ports' => $server->allocations ->sortBy(fn (Allocation $a) => [$a->id !== $server->allocation_id, $a->port]) ->map(fn (Allocation $a) => [ - 'address' => ($a->ip_alias ?: $a->ip).':'.$a->port, + 'address' => ServerDomains::hostFor($a, $default, $domains).':'.$a->port, 'notes' => $a->notes, 'is_default' => $a->id === $server->allocation_id, ]) diff --git a/src/Support/ConnectPresets.php b/src/Support/ConnectPresets.php index 118d8aa..d308013 100644 --- a/src/Support/ConnectPresets.php +++ b/src/Support/ConnectPresets.php @@ -35,14 +35,19 @@ public static function detect(string $eggName): string return 'copy'; } - /** The join URL for a type, or null when players can only copy the address. */ - public static function url(string $type, string $host, int $port, ?string $template): ?string + /** + * The join URL for a type, or null when players can only copy the address. + * Without the port (a domain with an SRV record) the game looks the port up itself; + * a custom template still gets {port}. + */ + public static function url(string $type, string $host, int $port, ?string $template, bool $withPort = true): ?string { + $suffix = $withPort ? ":{$port}" : ''; $url = match ($type) { - 'steam' => "steam://connect/{$host}:{$port}", - 'fivem' => "fivem://connect/{$host}:{$port}", - 'teamspeak' => "ts3server://{$host}?port={$port}", - 'mumble' => "mumble://{$host}:{$port}", + 'steam' => "steam://connect/{$host}{$suffix}", + 'fivem' => "fivem://connect/{$host}{$suffix}", + 'teamspeak' => $withPort ? "ts3server://{$host}?port={$port}" : "ts3server://{$host}", + 'mumble' => "mumble://{$host}{$suffix}", 'custom' => $template === null ? null : strtr($template, ['{ip}' => rawurlencode($host), '{port}' => (string) $port]), default => null, }; @@ -50,10 +55,10 @@ public static function url(string $type, string $host, int $port, ?string $templ return $url !== null && self::hasAllowedScheme($url) ? $url : null; } - /** What players paste into the game. Minecraft leaves off its default port. */ - public static function address(string $type, string $host, int $port): string + /** What players paste into the game. Minecraft leaves off its default port, and an SRV record any port. */ + public static function address(string $type, string $host, int $port, bool $withPort = true): string { - return $type === 'minecraft' && $port === 25565 ? $host : "{$host}:{$port}"; + return ! $withPort || ($type === 'minecraft' && $port === 25565) ? $host : "{$host}:{$port}"; } public static function hasAllowedScheme(string $url): bool diff --git a/src/Support/ServerDomains.php b/src/Support/ServerDomains.php new file mode 100644 index 0000000..11e23a4 --- /dev/null +++ b/src/Support/ServerDomains.php @@ -0,0 +1,132 @@ + + */ + public static function forServer(Server $server): array + { + try { + if (! Schema::hasTable(self::TABLE)) { + return []; + } + + $rows = DB::table(self::TABLE) + ->where('server_id', $server->id) + ->whereIn('dns_status', self::LIVE) + ->orderBy('id') + ->get(['kind', 'name', 'allocation_id', 'target', 'port', 'srv_service', 'dns_records', 'dns_status', 'dns_error']) + ->map(fn (object $row) => (array) $row) + ->all(); + } catch (\Throwable) { + return []; + } + + return self::byAllocation($rows, $server->allocation_id); + } + + /** + * Picks one domain per allocation. A domain without an allocation belongs to the + * primary one. When an allocation has several, one with a working SRV record wins, + * then the oldest. + * + * @param list> $rows + * + * @return array + */ + public static function byAllocation(array $rows, ?int $primaryAllocationId): array + { + $domains = []; + foreach ($rows as $row) { + if (! in_array($row['dns_status'] ?? null, self::LIVE, true) || ! is_string($row['name'] ?? null) || $row['name'] === '') { + continue; + } + + $allocation = isset($row['allocation_id']) ? (int) $row['allocation_id'] : $primaryAllocationId; + if ($allocation === null) { + continue; + } + + $domain = [ + 'name' => $row['name'], + 'target' => is_string($row['target'] ?? null) && $row['target'] !== '' ? $row['target'] : null, + 'port' => isset($row['port']) ? (int) $row['port'] : null, + 'srv' => self::hasSrv($row), + ]; + if (! isset($domains[$allocation]) || ($domain['srv'] && ! $domains[$allocation]['srv'])) { + $domains[$allocation] = $domain; + } + } + + return $domains; + } + + /** + * Whether players can join with the name alone: the domain has an SRV service + * and the record is in place. Subdomains keep the provider's record id; own + * domains report a missing SRV record in their DNS check. + * + * @param array $row + */ + public static function hasSrv(array $row): bool + { + if (empty($row['srv_service']) || ! isset($row['port'])) { + return false; + } + + if (($row['kind'] ?? 'subdomain') === 'own') { + return $row['dns_status'] === 'verified' && ! str_contains((string) ($row['dns_error'] ?? ''), 'SRV record'); + } + + $records = is_string($row['dns_records'] ?? null) ? json_decode($row['dns_records'], true) : ($row['dns_records'] ?? null); + + return is_array($records) && isset($records['srv']); + } + + /** The address players use for an allocation: its IP alias, else its IP. */ + public static function ip(Allocation $allocation): string + { + return $allocation->ip_alias ?: $allocation->ip; + } + + /** + * The host to show for an allocation: its own domain, else the primary + * allocation's domain when the domain points at this allocation's address too + * (such as RCON next to the game port), else its IP. + * + * @param array $domains + */ + public static function hostFor(Allocation $allocation, ?Allocation $primary, array $domains): string + { + if (isset($domains[$allocation->id])) { + return $domains[$allocation->id]['name']; + } + + $domain = $primary !== null ? ($domains[$primary->id] ?? null) : null; + if ($domain !== null && self::ip($allocation) === ($domain['target'] ?? self::ip($primary))) { + return $domain['name']; + } + + return self::ip($allocation); + } +} diff --git a/src/client/api.ts b/src/client/api.ts index b649f11..89f8dcd 100644 --- a/src/client/api.ts +++ b/src/client/api.ts @@ -11,6 +11,8 @@ export interface Connect { type: string; url: string | null; address: string | null; + /** The Domains extension's name for the primary allocation, when there is one. */ + domain?: string | null; ports: Port[]; } diff --git a/src/client/components/ConnectBar.tsx b/src/client/components/ConnectBar.tsx index af6e216..6656ed0 100644 --- a/src/client/components/ConnectBar.tsx +++ b/src/client/components/ConnectBar.tsx @@ -1,11 +1,14 @@ import { useState } from 'react'; import { useQuery } from '@tanstack/react-query'; import { Check, Copy, Gamepad2 } from 'lucide-react'; -import { toast, type SdkServer as Server } from '@pterodactyl/sdk'; +import { Button, toast, type SdkServer as Server } from '@pterodactyl/sdk'; import { connectQuery } from '../api'; -const BUTTON = - 'rxcb:inline-flex rxcb:min-h-9 rxcb:items-center rxcb:gap-2 rxcb:rounded-md rxcb:px-3 rxcb:py-1.5 rxcb:text-sm rxcb:font-medium rxcb:no-underline rxcb:transition-colors rxcb:focus-visible:outline-2 rxcb:focus-visible:outline-ring'; +const LABEL = 'rxcb:inline-flex rxcb:items-center rxcb:gap-2'; + +/** The core primary Button's look, for the Join link (a real link, so it can be copied or opened elsewhere). */ +const PRIMARY_LINK = + 'rxcb:relative rxcb:inline-block rxcb:rounded-sm rxcb:border rxcb:px-4 rxcb:py-2 rxcb:text-sm rxcb:uppercase rxcb:tracking-wide rxcb:no-underline rxcb:transition-colors rxcb:duration-150 rxcb:border-primary rxcb:bg-primary rxcb:text-primary-foreground rxcb:[&:hover]:border-primary rxcb:[&:hover]:bg-primary/80 rxcb:[&:active]:border-primary/70 rxcb:[&:active]:bg-primary/70'; /** Join and Copy address buttons above the console, with the server's other ports and their notes. */ export default function ConnectBar({ data: server }: { data: Server }) { @@ -29,23 +32,27 @@ export default function ConnectBar({ data: server }: { data: Server }) { const others = data.ports.filter((port) => !port.is_default); return ( -
+
{data.url && ( - - + + )} {data.address && ( - + + {copied ? + )} {others.length > 0 && (
    diff --git a/src/client/connect.spec.tsx b/src/client/connect.spec.tsx index fdac7ea..6730bca 100644 --- a/src/client/connect.spec.tsx +++ b/src/client/connect.spec.tsx @@ -50,3 +50,49 @@ test('nothing shows when the egg is set to no buttons', async () => { await waitFor(() => expect(container.textContent).toBe('')); }); + +test('Join is a link styled as the primary button and Copy a secondary core button', async () => { + host.queryClient.setQueryData(key, { + type: 'steam', + url: 'steam://connect/203.0.113.10:28015', + address: '203.0.113.10:28015', + ports: [], + }); + const { container } = render(, { wrapper: host.Wrapper }); + + const join = (await screen.findByText('Join server')).closest('a')!; + const copy = screen.getByRole('button', { name: /Copy address/ }); + expect(join.getAttribute('href')).toBe('steam://connect/203.0.113.10:28015'); + expect(join.className.split(' ')).toEqual(expect.arrayContaining(['rxcb:bg-primary', 'rxcb:px-4', 'rxcb:py-2', 'rxcb:uppercase'])); + expect(copy.className).toContain('bg-transparent'); + expect(copy.className.split(' ')).not.toContain('bg-primary'); + expect((container.firstElementChild as HTMLElement).className).toContain('rxcb:mb-3'); +}); + +test('a domain with an SRV record is shown without the port', async () => { + host.queryClient.setQueryData(key, { + type: 'minecraft', + url: null, + address: 'mc.example.com', + domain: 'mc.example.com', + ports: [{ address: 'mc.example.com:25575', notes: 'RCON', is_default: false }], + }); + render(, { wrapper: host.Wrapper }); + + expect(await screen.findByText('mc.example.com')).toBeTruthy(); + expect(screen.getByText('mc.example.com:25575')).toBeTruthy(); +}); + +test('a domain without an SRV record keeps the port in Join and Copy', async () => { + host.queryClient.setQueryData(key, { + type: 'steam', + url: 'steam://connect/rust.example.com:28015', + address: 'rust.example.com:28015', + domain: 'rust.example.com', + ports: [], + }); + render(, { wrapper: host.Wrapper }); + + expect(await screen.findByText('rust.example.com:28015')).toBeTruthy(); + expect(screen.getByText('Join server').closest('a')!.getAttribute('href')).toBe('steam://connect/rust.example.com:28015'); +});