Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ Download `custom-links-${VERSION}.pteroext` below, then upload it under **Admin
php artisan p:extension:install /path/to/custom-links-${VERSION}.pteroext --enable
```

### What's new in ${VERSION}

- New `{server.domain}` placeholder, filled with the server's domain from the Domains extension or its IP when it has none.
- Links 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.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 1.1.0

- New `{server.domain}` placeholder: the server's domain from the Domains extension, or its IP when it has no working domain (or Domains is not installed).
- Links now look exactly like the panel's own outlined buttons, with their icon, so they match the Join and Copy buttons from Connect Buttons and the rest of the console. They are still normal links, so middle-click and open in new tab work.
- The console row has the same 12px gap below it as the other extensions above the console.

## 1.0.0

- First release.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ Add your own buttons and links to server pages and the dashboard: wikis, Discord

- **Three places to show links**: a button row above the server console, extra tabs in the server's menu, and a button row above the server list on the dashboard.
- **Per-egg targeting**: show a link on every server, or only on servers using chosen eggs (a map viewer for Rust, a mod list for Minecraft).
- **Placeholders** filled in for each server: `{server.name}`, `{server.ip}`, `{server.port}`, `{server.uuid}`, `{server.identifier}` and `{user.username}`. Values are URL-encoded, so a server name can't change where a link goes.
- **Placeholders** filled in for each server: `{server.name}`, `{server.domain}`, `{server.ip}`, `{server.port}`, `{server.uuid}`, `{server.identifier}` and `{user.username}`. Values are URL-encoded, so a server name can't change where a link goes.
- **`{server.domain}`** is the server's domain from the [Domains](https://github.com/Rustix-Development/pterodactyl-domains) extension, such as `play.example.com`. When the server has no working domain, or Domains is not installed, it is the same as `{server.ip}`.
- **Safe links only**: http, https and `steam://` (for one-click joins like `steam://connect/{server.ip}:{server.port}`). Anything else is rejected when saving and again after placeholders are filled in.
- **Any icon** from the panel's icon set, an order number, and a choice of opening in a new tab.
- **Admin page** at **Admin → Custom Links**.
Expand Down
2 changes: 1 addition & 1 deletion extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "./node_modules/@pterodactyl/sdk/manifest.schema.json",
"id": "custom-links",
"name": "Custom Links",
"version": "1.0.0",
"version": "1.1.0",
"description": "Add your own buttons and links to server pages and the dashboard: wikis, Discord, map viewers, store pages. Show them on every server or only on chosen eggs.",
"author": "RustixDev",
"icon": "link",
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "custom-links",
"private": true,
"version": "1.0.0",
"version": "1.1.0",
"type": "module",
"scripts": {
"build": "vite build",
Expand Down
5 changes: 4 additions & 1 deletion src/Models/CustomLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Model;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\User;
use RustixDev\CustomLinks\Support\ServerDomain;

/**
* @property int $id
Expand Down Expand Up @@ -59,11 +60,13 @@ public function resolveUrl(User $user, ?Server $server = null): ?string

if ($server !== null) {
$allocation = $server->allocation;
$ip = $allocation ? ($allocation->ip_alias ?: $allocation->ip) : '';
$values += [
'{server.name}' => $server->name,
'{server.uuid}' => $server->uuid,
'{server.identifier}' => config('pterodactyl.features.new_server_identifiers') ? $server->identifier : $server->uuidShort,
'{server.ip}' => $allocation ? ($allocation->ip_alias ?: $allocation->ip) : '',
'{server.domain}' => $allocation ? (ServerDomain::forAllocation($server, $allocation) ?? $ip) : '',
'{server.ip}' => $ip,
'{server.port}' => $allocation ? (string) $allocation->port : '',
];
}
Expand Down
64 changes: 64 additions & 0 deletions src/Support/ServerDomain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace RustixDev\CustomLinks\Support;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Pterodactyl\Models\Allocation;
use Pterodactyl\Models\Server;

/**
* The server's domain from the Domains extension, when that extension is installed.
* Only its table is read, so Custom Links works the same without it.
*/
final class ServerDomain
{
public const string TABLE = 'ext_domains_domains';

/** DNS states in which the domain points at the server. */
public const array LIVE = ['active', 'verified'];

/** Answers per server object, so a page of links asks the database once. */
private static ?\WeakMap $cache = null;

/**
* The working domain for the allocation (a domain without an allocation counts
* for the primary one), oldest first, or null when there is none.
*/
public static function forAllocation(Server $server, Allocation $allocation): ?string
{
self::$cache ??= new \WeakMap();
$known = self::$cache[$server] ?? [];
if (! array_key_exists($allocation->id, $known)) {
$known[$allocation->id] = self::lookup($server, $allocation);
self::$cache[$server] = $known;
}

return $known[$allocation->id];
}

private static function lookup(Server $server, Allocation $allocation): ?string
{
try {
if (! Schema::hasTable(self::TABLE)) {
return null;
}

$name = DB::table(self::TABLE)
->where('server_id', $server->id)
->whereIn('dns_status', self::LIVE)
->where(function ($query) use ($server, $allocation) {
$query->where('allocation_id', $allocation->id);
if ($allocation->id === $server->allocation_id) {
$query->orWhereNull('allocation_id');
}
})
->orderBy('id')
->value('name');
} catch (\Throwable) {
return null;
}

return is_string($name) && $name !== '' ? $name : null;
}
}
16 changes: 11 additions & 5 deletions src/client/components/LinkRow.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import { NamedIcon } from '@pterodactyl/sdk';
import type { ClientLink } from '../api';

/** A wrapping row of button-styled links. Renders nothing when there are none. */
/** The core secondary Button's look, on a real link so it can be middle-clicked, copied or opened in a new tab. */
const SECONDARY_LINK =
'rxcl:relative rxcl:inline-block rxcl:rounded-sm rxcl:border rxcl:px-4 rxcl:py-2 rxcl:text-sm rxcl:uppercase rxcl:tracking-wide rxcl:no-underline rxcl:transition-colors rxcl:duration-150 rxcl:border-border rxcl:bg-transparent rxcl:text-foreground rxcl:[&:hover]:border-primary rxcl:[&:hover]:bg-primary rxcl:[&:hover]:text-primary-foreground';

/** A wrapping row of links styled as secondary buttons. Renders nothing when there are none. */
export default function LinkRow({ links, className = '' }: { links: ClientLink[]; className?: string }) {
if (links.length === 0) return null;

return (
<nav aria-label='Links' className={`rxcl:flex rxcl:flex-wrap rxcl:gap-2 ${className}`}>
<nav aria-label='Links' className={`rxcl:flex rxcl:flex-wrap rxcl:items-center rxcl:gap-2 ${className}`}>
{links.map((link) => (
<a
key={link.id}
href={link.url}
{...(link.new_tab ? { target: '_blank', rel: 'noopener noreferrer' } : {})}
className='rxcl:inline-flex rxcl:min-h-9 rxcl:items-center rxcl:gap-2 rxcl:rounded-md rxcl:border rxcl:border-border rxcl:bg-card rxcl:px-3 rxcl:py-1.5 rxcl:text-sm rxcl:text-card-foreground rxcl:no-underline rxcl:transition-colors rxcl:hover:bg-accent rxcl:hover:text-accent-foreground rxcl:focus-visible:outline-2 rxcl:focus-visible:outline-ring'
className={`${SECONDARY_LINK} rxcl:max-w-full`}
>
<NamedIcon name={link.icon} size={16} aria-hidden='true' />
<span className='rxcl:truncate'>{link.label}</span>
<span className='rxcl:inline-flex rxcl:max-w-full rxcl:items-center rxcl:gap-2'>
<NamedIcon name={link.icon} size={16} aria-hidden='true' />
<span className='rxcl:truncate'>{link.label}</span>
</span>
</a>
))}
</nav>
Expand Down
2 changes: 1 addition & 1 deletion src/client/components/ServerLinks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const useServerLinks = (server: Server) => {
export function ConsoleLinks({ data: server }: { data: Server }) {
const links = useServerLinks(server).filter((link) => link.placement === 'console');

return <LinkRow links={links} className='rxcl:mb-4' />;
return <LinkRow links={links} className='rxcl:mt-0 rxcl:mb-3' />;
}

/** Extra entries in the server's tab bar, after the core tabs. */
Expand Down
26 changes: 23 additions & 3 deletions src/client/links.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,33 @@ const link = (overrides: Partial<ClientLink> = {}): ClientLink => ({
...overrides,
});

test('links open in a new tab safely when asked to', async () => {
render(<LinkRow links={[link(), link({ id: 2, label: 'Wiki', new_tab: false })]} />, { wrapper: host.Wrapper });
test('links are real links styled as secondary buttons and open in a new tab safely when asked to', async () => {
render(<LinkRow links={[link(), link({ id: 2, label: 'Wiki', url: 'https://wiki.example.com', new_tab: false })]} />, {
wrapper: host.Wrapper,
});

const map = (await screen.findByText('Live map')).closest('a')!;
expect(map.getAttribute('href')).toBe('https://map.example.com/?ip=1.2.3.4');
expect(map.getAttribute('target')).toBe('_blank');
expect(map.getAttribute('rel')).toBe('noopener noreferrer');
expect(screen.getByText('Wiki').closest('a')!.getAttribute('target')).toBeNull();
expect(map.className.split(' ')).toEqual(expect.arrayContaining(['rxcl:bg-transparent', 'rxcl:border', 'rxcl:px-4', 'rxcl:py-2', 'rxcl:uppercase']));
expect(map.className).not.toContain('rxcl:bg-primary ');
const wiki = screen.getByText('Wiki').closest('a')!;
expect(wiki.getAttribute('href')).toBe('https://wiki.example.com');
expect(wiki.getAttribute('target')).toBeNull();
expect(document.querySelector('nav button')).toBeNull();
});

test('the console row has 12px below it and no top margin', async () => {
host.queryClient.setQueryData(serverLinksQuery(server.attributes.uuid).queryKey, [
link({ url: 'https://map.example.com/?host=play.example.com' }),
]);
render(<ConsoleLinks data={server} />, { wrapper: host.Wrapper });

const row = (await screen.findByText('Live map')).closest('nav')!;
expect(row.className).toContain('rxcl:mb-3');
expect(row.className).toContain('rxcl:mt-0');
expect(row.querySelector('a')!.getAttribute('href')).toBe('https://map.example.com/?host=play.example.com');
});

test('an empty list renders nothing', async () => {
Expand Down
5 changes: 3 additions & 2 deletions src/client/screens/LinkForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const ICONS = [
'users',
];

const PLACEHOLDERS = ['{server.name}', '{server.ip}', '{server.port}', '{server.uuid}', '{server.identifier}', '{user.username}'];
const PLACEHOLDERS = ['{server.name}', '{server.domain}', '{server.ip}', '{server.port}', '{server.uuid}', '{server.identifier}', '{user.username}'];

interface Props {
initial: AdminLink | null;
Expand Down Expand Up @@ -96,7 +96,8 @@ export default function LinkForm({ initial, pending, onCancel, onSubmit }: Props
onChange={(e) => setUrl(e.target.value)}
/>
<p className='rxcl:mt-1 rxcl:text-xs rxcl:text-muted-foreground'>
http, https or steam links. Placeholders: {PLACEHOLDERS.join(', ')}.
http, https or steam links. Placeholders: {PLACEHOLDERS.join(', ')}. {'{server.domain}'} is the
server's domain from the Domains extension, or its IP when it has none.
</p>
</div>
<div className='rxcl:grid rxcl:gap-4 rxcl:sm:grid-cols-[1fr_8rem]'>
Expand Down
Loading