2024-12-31 10:31:50 +05:30
|
|
|
import type { ActionFunctionArgs } from 'react-router';
|
2025-01-06 08:11:45 +05:30
|
|
|
import { data, useLoaderData } from 'react-router';
|
2024-12-31 10:30:14 +05:30
|
|
|
|
|
|
|
|
import Code from '~/components/Code';
|
|
|
|
|
import Notice from '~/components/Notice';
|
|
|
|
|
import { loadContext } from '~/utils/config/headplane';
|
|
|
|
|
import { loadConfig, patchConfig } from '~/utils/config/headscale';
|
2025-01-06 08:11:45 +05:30
|
|
|
import { getSession } from '~/utils/sessions.server';
|
2024-12-31 10:30:14 +05:30
|
|
|
import { useLiveData } from '~/utils/useLiveData';
|
|
|
|
|
|
|
|
|
|
import DNS from './components/dns';
|
|
|
|
|
import Domains from './components/domains';
|
|
|
|
|
import MagicModal from './components/magic';
|
|
|
|
|
import Nameservers from './components/nameservers';
|
|
|
|
|
import RenameModal from './components/rename';
|
2024-03-26 17:42:45 -04:00
|
|
|
|
|
|
|
|
// We do not want to expose every config value
|
|
|
|
|
export async function loader() {
|
2024-12-31 10:30:14 +05:30
|
|
|
const context = await loadContext();
|
2024-05-20 14:05:09 -04:00
|
|
|
if (!context.config.read) {
|
2024-12-31 10:30:14 +05:30
|
|
|
throw new Error('No configuration is available');
|
2024-03-30 01:48:35 -04:00
|
|
|
}
|
|
|
|
|
|
2024-12-31 10:30:14 +05:30
|
|
|
const config = await loadConfig();
|
2024-03-26 17:42:45 -04:00
|
|
|
const dns = {
|
|
|
|
|
prefixes: config.prefixes,
|
2024-08-22 16:55:05 -04:00
|
|
|
magicDns: config.dns.magic_dns,
|
2025-01-18 13:22:43 +00:00
|
|
|
baseDomain: config.dns.base_domain,
|
2024-08-22 16:55:05 -04:00
|
|
|
nameservers: config.dns.nameservers.global,
|
|
|
|
|
splitDns: config.dns.nameservers.split,
|
|
|
|
|
searchDomains: config.dns.search_domains,
|
|
|
|
|
extraRecords: config.dns.extra_records,
|
2024-12-31 10:30:14 +05:30
|
|
|
};
|
2024-03-26 17:42:45 -04:00
|
|
|
|
2024-03-29 21:46:21 -04:00
|
|
|
return {
|
|
|
|
|
...dns,
|
2024-05-20 14:05:09 -04:00
|
|
|
...context,
|
2024-12-31 10:30:14 +05:30
|
|
|
};
|
2024-03-26 17:42:45 -04:00
|
|
|
}
|
|
|
|
|
|
2024-03-28 17:03:37 -04:00
|
|
|
export async function action({ request }: ActionFunctionArgs) {
|
2024-12-31 10:30:14 +05:30
|
|
|
const session = await getSession(request.headers.get('Cookie'));
|
2024-04-01 19:33:22 -04:00
|
|
|
if (!session.has('hsApiKey')) {
|
2025-01-06 08:18:46 +05:30
|
|
|
return data({ success: false }, { status: 401 });
|
2024-04-01 19:33:22 -04:00
|
|
|
}
|
|
|
|
|
|
2024-12-31 10:30:14 +05:30
|
|
|
const context = await loadContext();
|
2024-05-20 14:05:09 -04:00
|
|
|
if (!context.config.write) {
|
2025-01-06 08:18:46 +05:30
|
|
|
return data({ success: false }, { status: 403 });
|
2024-03-29 21:46:21 -04:00
|
|
|
}
|
|
|
|
|
|
2025-01-26 15:04:13 -05:00
|
|
|
const textData = await request.text();
|
|
|
|
|
if (!textData) {
|
|
|
|
|
return data({ success: true });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const patch = JSON.parse(textData) as Record<string, unknown>;
|
2025-01-06 08:18:46 +05:30
|
|
|
await patchConfig(patch);
|
2024-05-26 19:23:24 -04:00
|
|
|
|
2024-07-10 19:38:44 -04:00
|
|
|
if (context.integration?.onConfigChange) {
|
2024-12-31 10:30:14 +05:30
|
|
|
await context.integration.onConfigChange(context.integration.context);
|
2024-05-26 19:23:24 -04:00
|
|
|
}
|
|
|
|
|
|
2025-01-06 08:18:46 +05:30
|
|
|
return data({ success: true });
|
2024-03-28 17:03:37 -04:00
|
|
|
}
|
|
|
|
|
|
2024-03-26 17:42:45 -04:00
|
|
|
export default function Page() {
|
2024-12-31 10:30:14 +05:30
|
|
|
useLiveData({ interval: 5000 });
|
|
|
|
|
const data = useLoaderData<typeof loader>();
|
2024-05-21 17:21:14 -04:00
|
|
|
|
2024-12-31 10:30:14 +05:30
|
|
|
const allNs: Record<string, string[]> = {};
|
2024-05-21 17:21:14 -04:00
|
|
|
for (const key of Object.keys(data.splitDns)) {
|
2024-12-31 10:30:14 +05:30
|
|
|
allNs[key] = data.splitDns[key];
|
2024-05-21 17:21:14 -04:00
|
|
|
}
|
|
|
|
|
|
2024-12-31 10:30:14 +05:30
|
|
|
allNs.global = data.nameservers;
|
2024-03-26 17:42:45 -04:00
|
|
|
|
|
|
|
|
return (
|
2024-05-20 14:05:09 -04:00
|
|
|
<div className="flex flex-col gap-16 max-w-screen-lg">
|
2024-12-31 10:30:14 +05:30
|
|
|
{data.config.write ? undefined : (
|
|
|
|
|
<Notice>
|
|
|
|
|
The Headscale configuration is read-only. You cannot make changes to
|
|
|
|
|
the configuration
|
|
|
|
|
</Notice>
|
|
|
|
|
)}
|
2024-05-20 14:05:09 -04:00
|
|
|
<RenameModal name={data.baseDomain} disabled={!data.config.write} />
|
2024-12-31 10:30:14 +05:30
|
|
|
<Nameservers nameservers={allNs} isDisabled={!data.config.write} />
|
2024-03-28 14:20:19 -04:00
|
|
|
|
2024-12-31 10:30:14 +05:30
|
|
|
<DNS records={data.extraRecords} isDisabled={!data.config.write} />
|
2024-07-07 14:52:47 -04:00
|
|
|
|
2024-03-28 18:47:28 -04:00
|
|
|
<Domains
|
|
|
|
|
baseDomain={data.magicDns ? data.baseDomain : undefined}
|
|
|
|
|
searchDomains={data.searchDomains}
|
2024-05-20 14:05:09 -04:00
|
|
|
disabled={!data.config.write}
|
2024-03-28 18:47:28 -04:00
|
|
|
/>
|
|
|
|
|
|
2024-05-20 14:05:09 -04:00
|
|
|
<div className="flex flex-col w-2/3">
|
|
|
|
|
<h1 className="text-2xl font-medium mb-4">Magic DNS</h1>
|
|
|
|
|
<p className="text-gray-700 dark:text-gray-300 mb-4">
|
2024-12-31 10:30:14 +05:30
|
|
|
Automatically register domain names for each device on the tailnet.
|
|
|
|
|
Devices will be accessible at{' '}
|
2024-03-29 15:41:10 -04:00
|
|
|
<Code>
|
2024-09-25 16:22:00 -04:00
|
|
|
[device].
|
2024-05-20 14:05:09 -04:00
|
|
|
{data.baseDomain}
|
2024-12-31 10:30:14 +05:30
|
|
|
</Code>{' '}
|
2024-03-28 14:20:19 -04:00
|
|
|
when Magic DNS is enabled.
|
|
|
|
|
</p>
|
2024-05-20 14:05:09 -04:00
|
|
|
<MagicModal isEnabled={data.magicDns} disabled={!data.config.write} />
|
2024-03-28 14:20:19 -04:00
|
|
|
</div>
|
2024-03-26 17:42:45 -04:00
|
|
|
</div>
|
2024-12-31 10:30:14 +05:30
|
|
|
);
|
2024-03-26 17:42:45 -04:00
|
|
|
}
|