mirror of
https://github.com/tale/headplane.git
synced 2026-09-04 11:15:42 +00:00
feat: respect context in server
This commit is contained in:
+19
-4
@@ -1,24 +1,39 @@
|
|||||||
import { PassThrough } from 'node:stream';
|
import { PassThrough } from 'node:stream';
|
||||||
|
|
||||||
import { createReadableStreamFromReadable } from '@react-router/node';
|
import { createReadableStreamFromReadable } from '@react-router/node';
|
||||||
import { isbot } from 'isbot';
|
import { isbot } from 'isbot';
|
||||||
import type { RenderToPipeableStreamOptions } from 'react-dom/server';
|
import type { RenderToPipeableStreamOptions } from 'react-dom/server';
|
||||||
import { renderToPipeableStream } from 'react-dom/server';
|
import { renderToPipeableStream } from 'react-dom/server';
|
||||||
import type { AppLoadContext, EntryContext } from 'react-router';
|
import type { AppLoadContext, EntryContext } from 'react-router';
|
||||||
import { ServerRouter } from 'react-router';
|
import { ServerRouter } from 'react-router';
|
||||||
import { hp_loadConfig } from '~/utils/context/loader';
|
import { hs_loadConfig } from '~/utils/config/loader';
|
||||||
|
import { hp_storeContext } from '~/utils/headscale';
|
||||||
|
import { hp_loadLogger } from '~/utils/log';
|
||||||
|
import { initSessionManager } from '~/utils/sessions.server';
|
||||||
|
import type { AppContext } from '~server/context/app';
|
||||||
|
|
||||||
hp_loadConfig();
|
|
||||||
export const streamTimeout = 5_000;
|
export const streamTimeout = 5_000;
|
||||||
|
|
||||||
|
// TODO: checkOidc
|
||||||
export default function handleRequest(
|
export default function handleRequest(
|
||||||
request: Request,
|
request: Request,
|
||||||
responseStatusCode: number,
|
responseStatusCode: number,
|
||||||
responseHeaders: Headers,
|
responseHeaders: Headers,
|
||||||
routerContext: EntryContext,
|
routerContext: EntryContext,
|
||||||
loadContext: AppLoadContext,
|
loadContext: AppContext,
|
||||||
) {
|
) {
|
||||||
|
const { context } = loadContext;
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
initSessionManager(
|
||||||
|
context.server.cookie_secret,
|
||||||
|
context.server.cookie_secure,
|
||||||
|
);
|
||||||
|
|
||||||
|
// This is a promise but we don't need to wait for it to finish
|
||||||
|
// before we start rendering the shell since it only loads once.
|
||||||
|
hs_loadConfig(context);
|
||||||
|
hp_storeContext(context);
|
||||||
|
hp_loadLogger(context.debug);
|
||||||
|
|
||||||
let shellRendered = false;
|
let shellRendered = false;
|
||||||
const userAgent = request.headers.get('user-agent');
|
const userAgent = request.headers.get('user-agent');
|
||||||
|
|
||||||
|
|||||||
+14
-5
@@ -6,25 +6,34 @@ import {
|
|||||||
} from 'react-router';
|
} from 'react-router';
|
||||||
import Footer from '~/components/Footer';
|
import Footer from '~/components/Footer';
|
||||||
import Header from '~/components/Header';
|
import Header from '~/components/Header';
|
||||||
|
import { hs_getConfig } from '~/utils/config/loader';
|
||||||
|
import { noContext } from '~/utils/log';
|
||||||
import { getSession } from '~/utils/sessions.server';
|
import { getSession } from '~/utils/sessions.server';
|
||||||
import { hp_getConfig, hs_getConfig } from '~/utils/state';
|
import type { AppContext } from '~server/context/app';
|
||||||
|
|
||||||
// This loads the bare minimum for the application to function
|
// This loads the bare minimum for the application to function
|
||||||
// So we know that if context fails to load then well, oops?
|
// So we know that if context fails to load then well, oops?
|
||||||
export async function loader({ request }: LoaderFunctionArgs) {
|
export async function loader({
|
||||||
|
request,
|
||||||
|
context,
|
||||||
|
}: LoaderFunctionArgs<AppContext>) {
|
||||||
const session = await getSession(request.headers.get('Cookie'));
|
const session = await getSession(request.headers.get('Cookie'));
|
||||||
if (!session.has('hsApiKey')) {
|
if (!session.has('hsApiKey')) {
|
||||||
return redirect('/login');
|
return redirect('/login');
|
||||||
}
|
}
|
||||||
|
|
||||||
const context = hp_getConfig();
|
if (!context) {
|
||||||
|
throw noContext();
|
||||||
|
}
|
||||||
|
|
||||||
|
const ctx = context.context;
|
||||||
const { mode, config } = hs_getConfig();
|
const { mode, config } = hs_getConfig();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
config,
|
config,
|
||||||
url: context.headscale.public_url ?? context.headscale.url,
|
url: ctx.headscale.public_url ?? ctx.headscale.url,
|
||||||
configAvailable: mode !== 'no',
|
configAvailable: mode !== 'no',
|
||||||
debug: context.debug,
|
debug: ctx.debug,
|
||||||
user: session.get('user'),
|
user: session.get('user'),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,17 +7,18 @@ import Link from '~/components/Link';
|
|||||||
import Notice from '~/components/Notice';
|
import Notice from '~/components/Notice';
|
||||||
import Spinner from '~/components/Spinner';
|
import Spinner from '~/components/Spinner';
|
||||||
import Tabs from '~/components/Tabs';
|
import Tabs from '~/components/Tabs';
|
||||||
|
import { hs_getConfig } from '~/utils/config/loader';
|
||||||
import { HeadscaleError, pull, put } from '~/utils/headscale';
|
import { HeadscaleError, pull, put } from '~/utils/headscale';
|
||||||
import log from '~/utils/log';
|
import log from '~/utils/log';
|
||||||
import { send } from '~/utils/res';
|
import { send } from '~/utils/res';
|
||||||
import { getSession } from '~/utils/sessions.server';
|
import { getSession } from '~/utils/sessions.server';
|
||||||
import { hs_getConfig } from '~/utils/state';
|
|
||||||
import toast from '~/utils/toast';
|
import toast from '~/utils/toast';
|
||||||
|
import type { AppContext } from '~server/context/app';
|
||||||
import { Differ, Editor } from './components/cm.client';
|
import { Differ, Editor } from './components/cm.client';
|
||||||
import { ErrorView } from './components/error';
|
import { ErrorView } from './components/error';
|
||||||
import { Unavailable } from './components/unavailable';
|
import { Unavailable } from './components/unavailable';
|
||||||
|
|
||||||
export async function loader({ request }: LoaderFunctionArgs) {
|
export async function loader({ request }: LoaderFunctionArgs<AppContext>) {
|
||||||
const session = await getSession(request.headers.get('Cookie'));
|
const session = await getSession(request.headers.get('Cookie'));
|
||||||
|
|
||||||
// The way policy is handled in 0.23 of Headscale and later is verbose.
|
// The way policy is handled in 0.23 of Headscale and later is verbose.
|
||||||
|
|||||||
@@ -10,10 +10,14 @@ import Code from '~/components/Code';
|
|||||||
import Input from '~/components/Input';
|
import Input from '~/components/Input';
|
||||||
import type { Key } from '~/types';
|
import type { Key } from '~/types';
|
||||||
import { pull } from '~/utils/headscale';
|
import { pull } from '~/utils/headscale';
|
||||||
|
import { noContext } from '~/utils/log';
|
||||||
import { commitSession, getSession } from '~/utils/sessions.server';
|
import { commitSession, getSession } from '~/utils/sessions.server';
|
||||||
import { hp_getConfig } from '~/utils/state';
|
import type { AppContext } from '~server/context/app';
|
||||||
|
|
||||||
export async function loader({ request }: LoaderFunctionArgs) {
|
export async function loader({
|
||||||
|
request,
|
||||||
|
context,
|
||||||
|
}: LoaderFunctionArgs<AppContext>) {
|
||||||
const session = await getSession(request.headers.get('Cookie'));
|
const session = await getSession(request.headers.get('Cookie'));
|
||||||
if (session.has('hsApiKey')) {
|
if (session.has('hsApiKey')) {
|
||||||
return redirect('/machines', {
|
return redirect('/machines', {
|
||||||
@@ -23,28 +27,37 @@ export async function loader({ request }: LoaderFunctionArgs) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const context = hp_getConfig();
|
if (!context) {
|
||||||
|
throw noContext();
|
||||||
|
}
|
||||||
|
|
||||||
// Only set if OIDC is properly enabled anyways
|
// Only set if OIDC is properly enabled anyways
|
||||||
if (context.oidc?.disable_api_key_login) {
|
const ctx = context.context;
|
||||||
|
if (ctx.oidc?.disable_api_key_login) {
|
||||||
return redirect('/oidc/start');
|
return redirect('/oidc/start');
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
oidc: context.oidc?.issuer,
|
oidc: ctx.oidc?.issuer,
|
||||||
apiKey: !context.oidc?.disable_api_key_login,
|
apiKey: !ctx.oidc?.disable_api_key_login,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function action({ request }: ActionFunctionArgs) {
|
export async function action({
|
||||||
|
request,
|
||||||
|
context,
|
||||||
|
}: ActionFunctionArgs<AppContext>) {
|
||||||
const formData = await request.formData();
|
const formData = await request.formData();
|
||||||
const oidcStart = formData.get('oidc-start');
|
const oidcStart = formData.get('oidc-start');
|
||||||
const session = await getSession(request.headers.get('Cookie'));
|
const session = await getSession(request.headers.get('Cookie'));
|
||||||
|
|
||||||
if (oidcStart) {
|
if (oidcStart) {
|
||||||
const context = hp_getConfig();
|
if (!context) {
|
||||||
|
throw noContext();
|
||||||
|
}
|
||||||
|
|
||||||
if (!context.oidc) {
|
const ctx = context.context;
|
||||||
|
if (!ctx.oidc) {
|
||||||
throw new Error('An invalid OIDC configuration was provided');
|
throw new Error('An invalid OIDC configuration was provided');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,14 @@
|
|||||||
import { type LoaderFunctionArgs, redirect } from 'react-router';
|
import { type LoaderFunctionArgs, redirect } from 'react-router';
|
||||||
import { finishAuthFlow, formatError, getRedirectUri } from '~/utils/oidc';
|
import { noContext } from '~/utils/log';
|
||||||
|
import { finishAuthFlow, formatError } from '~/utils/oidc';
|
||||||
import { send } from '~/utils/res';
|
import { send } from '~/utils/res';
|
||||||
import { commitSession, getSession } from '~/utils/sessions.server';
|
import { commitSession, getSession } from '~/utils/sessions.server';
|
||||||
import { hp_getConfig } from '~/utils/state';
|
import type { AppContext } from '~server/context/app';
|
||||||
|
|
||||||
export async function loader({ request }: LoaderFunctionArgs) {
|
export async function loader({
|
||||||
|
request,
|
||||||
|
context,
|
||||||
|
}: LoaderFunctionArgs<AppContext>) {
|
||||||
// Check if we have 0 query parameters
|
// Check if we have 0 query parameters
|
||||||
const url = new URL(request.url);
|
const url = new URL(request.url);
|
||||||
if (url.searchParams.toString().length === 0) {
|
if (url.searchParams.toString().length === 0) {
|
||||||
@@ -16,7 +20,11 @@ export async function loader({ request }: LoaderFunctionArgs) {
|
|||||||
return redirect('/machines');
|
return redirect('/machines');
|
||||||
}
|
}
|
||||||
|
|
||||||
const { oidc } = hp_getConfig();
|
if (!context) {
|
||||||
|
throw noContext();
|
||||||
|
}
|
||||||
|
|
||||||
|
const { oidc } = context.context;
|
||||||
if (!oidc) {
|
if (!oidc) {
|
||||||
throw new Error('An invalid OIDC configuration was provided');
|
throw new Error('An invalid OIDC configuration was provided');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,23 @@
|
|||||||
import { type LoaderFunctionArgs, redirect } from 'react-router';
|
import { type LoaderFunctionArgs, redirect } from 'react-router';
|
||||||
|
import { noContext } from '~/utils/log';
|
||||||
import { beginAuthFlow, getRedirectUri } from '~/utils/oidc';
|
import { beginAuthFlow, getRedirectUri } from '~/utils/oidc';
|
||||||
import { commitSession, getSession } from '~/utils/sessions.server';
|
import { commitSession, getSession } from '~/utils/sessions.server';
|
||||||
import { hp_getConfig } from '~/utils/state';
|
import type { AppContext } from '~server/context/app';
|
||||||
|
|
||||||
export async function loader({ request }: LoaderFunctionArgs) {
|
export async function loader({
|
||||||
|
request,
|
||||||
|
context,
|
||||||
|
}: LoaderFunctionArgs<AppContext>) {
|
||||||
const session = await getSession(request.headers.get('Cookie'));
|
const session = await getSession(request.headers.get('Cookie'));
|
||||||
if (session.has('hsApiKey')) {
|
if (session.has('hsApiKey')) {
|
||||||
return redirect('/machines');
|
return redirect('/machines');
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is a hold-over from the old code
|
if (!context) {
|
||||||
// TODO: Rewrite checkOIDC in the context loader
|
throw noContext();
|
||||||
const { oidc } = hp_getConfig();
|
}
|
||||||
|
|
||||||
|
const { oidc } = context.context;
|
||||||
if (!oidc) {
|
if (!oidc) {
|
||||||
throw new Error('An invalid OIDC configuration was provided');
|
throw new Error('An invalid OIDC configuration was provided');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import { ActionFunctionArgs, data } from 'react-router';
|
import { ActionFunctionArgs, data } from 'react-router';
|
||||||
import { hs_patchConfig } from '~/utils/config/loader';
|
import { hs_getConfig, hs_patchConfig } from '~/utils/config/loader';
|
||||||
import { auth } from '~/utils/sessions.server';
|
import { auth } from '~/utils/sessions.server';
|
||||||
import { hs_getConfig } from '~/utils/state';
|
|
||||||
|
|
||||||
export async function dnsAction({ request }: ActionFunctionArgs) {
|
export async function dnsAction({ request }: ActionFunctionArgs) {
|
||||||
const session = await auth(request);
|
const session = await auth(request);
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import type { ActionFunctionArgs } from 'react-router';
|
|||||||
import { useLoaderData } from 'react-router';
|
import { useLoaderData } from 'react-router';
|
||||||
import Code from '~/components/Code';
|
import Code from '~/components/Code';
|
||||||
import Notice from '~/components/Notice';
|
import Notice from '~/components/Notice';
|
||||||
import { hs_getConfig } from '~/utils/state';
|
import { hs_getConfig } from '~/utils/config/loader';
|
||||||
import ManageDomains from './components/manage-domains';
|
import ManageDomains from './components/manage-domains';
|
||||||
import ManageNS from './components/manage-ns';
|
import ManageNS from './components/manage-ns';
|
||||||
import ManageRecords from './components/manage-records';
|
import ManageRecords from './components/manage-records';
|
||||||
|
|||||||
@@ -109,9 +109,13 @@ export async function menuAction(request: ActionFunctionArgs['request']) {
|
|||||||
const to = String(data.get('to'));
|
const to = String(data.get('to'));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await post(`v1/node/${id}/user?user=${to}`, session.get('hsApiKey')!);
|
await post(`v1/node/${id}/user`, session.get('hsApiKey')!, {
|
||||||
|
user: to,
|
||||||
|
});
|
||||||
|
|
||||||
return { message: `Moved node ${id} to ${to}` };
|
return { message: `Moved node ${id} to ${to}` };
|
||||||
} catch {
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
return send(
|
return send(
|
||||||
{ message: `Failed to move node ${id} to ${to}` },
|
{ message: `Failed to move node ${id} to ${to}` },
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -11,9 +11,9 @@ import StatusCircle from '~/components/StatusCircle';
|
|||||||
import Tooltip from '~/components/Tooltip';
|
import Tooltip from '~/components/Tooltip';
|
||||||
import type { Machine, Route, User } from '~/types';
|
import type { Machine, Route, User } from '~/types';
|
||||||
import cn from '~/utils/cn';
|
import cn from '~/utils/cn';
|
||||||
|
import { hs_getConfig } from '~/utils/config/loader';
|
||||||
import { pull } from '~/utils/headscale';
|
import { pull } from '~/utils/headscale';
|
||||||
import { getSession } from '~/utils/sessions.server';
|
import { getSession } from '~/utils/sessions.server';
|
||||||
import { hs_getConfig } from '~/utils/state';
|
|
||||||
import { menuAction } from './action';
|
import { menuAction } from './action';
|
||||||
import MenuOptions from './components/menu';
|
import MenuOptions from './components/menu';
|
||||||
import Routes from './dialogs/routes';
|
import Routes from './dialogs/routes';
|
||||||
|
|||||||
@@ -12,12 +12,17 @@ import { getSession } from '~/utils/sessions.server';
|
|||||||
import { initAgentSocket, queryAgent } from '~/utils/ws-agent';
|
import { initAgentSocket, queryAgent } from '~/utils/ws-agent';
|
||||||
|
|
||||||
import Tooltip from '~/components/Tooltip';
|
import Tooltip from '~/components/Tooltip';
|
||||||
import { hp_getConfig, hs_getConfig } from '~/utils/state';
|
import { hs_getConfig } from '~/utils/config/loader';
|
||||||
|
import { noContext } from '~/utils/log';
|
||||||
|
import { AppContext } from '~server/context/app';
|
||||||
import { menuAction } from './action';
|
import { menuAction } from './action';
|
||||||
import MachineRow from './components/machine';
|
import MachineRow from './components/machine';
|
||||||
import NewMachine from './dialogs/new';
|
import NewMachine from './dialogs/new';
|
||||||
|
|
||||||
export async function loader({ request, context: lC }: LoaderFunctionArgs) {
|
export async function loader({
|
||||||
|
request,
|
||||||
|
context,
|
||||||
|
}: LoaderFunctionArgs<AppContext>) {
|
||||||
const session = await getSession(request.headers.get('Cookie'));
|
const session = await getSession(request.headers.get('Cookie'));
|
||||||
const [machines, routes, users] = await Promise.all([
|
const [machines, routes, users] = await Promise.all([
|
||||||
pull<{ nodes: Machine[] }>('v1/node', session.get('hsApiKey')!),
|
pull<{ nodes: Machine[] }>('v1/node', session.get('hsApiKey')!),
|
||||||
@@ -25,10 +30,14 @@ export async function loader({ request, context: lC }: LoaderFunctionArgs) {
|
|||||||
pull<{ users: User[] }>('v1/user', session.get('hsApiKey')!),
|
pull<{ users: User[] }>('v1/user', session.get('hsApiKey')!),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
initAgentSocket(lC);
|
if (!context) {
|
||||||
|
throw noContext();
|
||||||
|
}
|
||||||
|
|
||||||
|
initAgentSocket(context);
|
||||||
|
|
||||||
const stats = await queryAgent(machines.nodes.map((node) => node.nodeKey));
|
const stats = await queryAgent(machines.nodes.map((node) => node.nodeKey));
|
||||||
const context = hp_getConfig();
|
const ctx = context.context;
|
||||||
const { mode, config } = hs_getConfig();
|
const { mode, config } = hs_getConfig();
|
||||||
|
|
||||||
let magic: string | undefined;
|
let magic: string | undefined;
|
||||||
@@ -45,8 +54,8 @@ export async function loader({ request, context: lC }: LoaderFunctionArgs) {
|
|||||||
users: users.users,
|
users: users.users,
|
||||||
magic,
|
magic,
|
||||||
stats,
|
stats,
|
||||||
server: context.headscale.url,
|
server: ctx.headscale.url,
|
||||||
publicServer: context.headscale.public_url,
|
publicServer: ctx.headscale.public_url,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,9 +7,10 @@ import Select from '~/components/Select';
|
|||||||
import TableList from '~/components/TableList';
|
import TableList from '~/components/TableList';
|
||||||
import type { PreAuthKey, User } from '~/types';
|
import type { PreAuthKey, User } from '~/types';
|
||||||
import { post, pull } from '~/utils/headscale';
|
import { post, pull } from '~/utils/headscale';
|
||||||
|
import { noContext } from '~/utils/log';
|
||||||
import { send } from '~/utils/res';
|
import { send } from '~/utils/res';
|
||||||
import { getSession } from '~/utils/sessions.server';
|
import { getSession } from '~/utils/sessions.server';
|
||||||
import { hp_getConfig } from '~/utils/state';
|
import type { AppContext } from '~server/context/app';
|
||||||
import AuthKeyRow from './components/key';
|
import AuthKeyRow from './components/key';
|
||||||
import AddPreAuthKey from './dialogs/new';
|
import AddPreAuthKey from './dialogs/new';
|
||||||
|
|
||||||
@@ -90,14 +91,21 @@ export async function action({ request }: ActionFunctionArgs) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function loader({ request }: LoaderFunctionArgs) {
|
export async function loader({
|
||||||
const context = hp_getConfig();
|
request,
|
||||||
|
context,
|
||||||
|
}: LoaderFunctionArgs<AppContext>) {
|
||||||
const session = await getSession(request.headers.get('Cookie'));
|
const session = await getSession(request.headers.get('Cookie'));
|
||||||
const users = await pull<{ users: User[] }>(
|
const users = await pull<{ users: User[] }>(
|
||||||
'v1/user',
|
'v1/user',
|
||||||
session.get('hsApiKey')!,
|
session.get('hsApiKey')!,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (!context) {
|
||||||
|
throw noContext();
|
||||||
|
}
|
||||||
|
|
||||||
|
const ctx = context.context;
|
||||||
const preAuthKeys = await Promise.all(
|
const preAuthKeys = await Promise.all(
|
||||||
users.users.map((user) => {
|
users.users.map((user) => {
|
||||||
const qp = new URLSearchParams();
|
const qp = new URLSearchParams();
|
||||||
@@ -113,7 +121,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
|
|||||||
return {
|
return {
|
||||||
keys: preAuthKeys.flatMap((keys) => keys.preAuthKeys),
|
keys: preAuthKeys.flatMap((keys) => keys.preAuthKeys),
|
||||||
users: users.users,
|
users: users.users,
|
||||||
server: context.headscale.public_url ?? context.headscale.url,
|
server: ctx.headscale.public_url ?? ctx.headscale.url,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,22 +1,23 @@
|
|||||||
import { useMemo } from 'react';
|
import { useMemo } from 'react';
|
||||||
import { useLoaderData, type LoaderFunctionArgs } from 'react-router';
|
import { type LoaderFunctionArgs, useLoaderData } from 'react-router';
|
||||||
import { getSession, commitSession } from '~/utils/sessions.server'
|
import { commitSession, getSession } from '~/utils/sessions.server';
|
||||||
import { queryAgent } from '~/utils/ws-agent'
|
import { queryAgent } from '~/utils/ws-agent';
|
||||||
import AgentManagement from './components/agent/manage'
|
import AgentManagement from './components/agent/manage';
|
||||||
|
|
||||||
export async function loader({ request, context }: LoaderFunctionArgs) {
|
export async function loader({ request, context }: LoaderFunctionArgs) {
|
||||||
const { ws, wsAuthKey } = context;
|
const { ws, wsAuthKey } = context;
|
||||||
const session = await getSession(request.headers.get('Cookie'));
|
const session = await getSession(request.headers.get('Cookie'));
|
||||||
const onboarding = session.get('agent_onboarding') ?? false;
|
const onboarding = session.get('agent_onboarding') ?? false;
|
||||||
|
|
||||||
const nodeKey = 'nodekey:542dad28354eb8d51e240aada7adf0222ba3ecc74af0bbd56123f03eefdb391b'
|
const nodeKey =
|
||||||
|
'nodekey:542dad28354eb8d51e240aada7adf0222ba3ecc74af0bbd56123f03eefdb391b';
|
||||||
const stats = await queryAgent([nodeKey]);
|
const stats = await queryAgent([nodeKey]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
configured: wsAuthKey !== undefined,
|
configured: wsAuthKey !== undefined,
|
||||||
onboarding,
|
onboarding,
|
||||||
stats: stats[nodeKey]
|
stats: stats?.[nodeKey],
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function Page() {
|
export default function Page() {
|
||||||
@@ -24,21 +25,18 @@ export default function Page() {
|
|||||||
|
|
||||||
// Whether we show the onboarding or management UI
|
// Whether we show the onboarding or management UI
|
||||||
const management = useMemo(() => {
|
const management = useMemo(() => {
|
||||||
return data.configured && (data.onboarding === false)
|
return data.configured && data.onboarding === false;
|
||||||
}, [data.configured, data.onboarding]);
|
}, [data.configured, data.onboarding]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-col gap-8 max-w-screen-lg">
|
<div className="flex flex-col gap-8 max-w-screen-lg">
|
||||||
{management ? (
|
{management ? (
|
||||||
<AgentManagement
|
<AgentManagement reachable={true} hostInfo={data.stats} />
|
||||||
reachable={true}
|
|
||||||
hostInfo={data.stats}
|
|
||||||
/>
|
|
||||||
) : (
|
) : (
|
||||||
<div>
|
<div>
|
||||||
<h1>Local Agent Coming Soon</h1>
|
<h1>Local Agent Coming Soon</h1>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
import { Building2, House, Key } from 'lucide-react';
|
import { Building2, House, Key } from 'lucide-react';
|
||||||
import Card from '~/components/Card';
|
import Card from '~/components/Card';
|
||||||
import Link from '~/components/Link';
|
import Link from '~/components/Link';
|
||||||
import { HeadplaneConfig } from '~/utils/state';
|
import type { AppContext } from '~server/context/app';
|
||||||
import CreateUser from '../dialogs/create-user';
|
import CreateUser from '../dialogs/create-user';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
oidc?: NonNullable<HeadplaneConfig['oidc']>;
|
oidc?: NonNullable<AppContext['context']['oidc']>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function ManageBanner({ oidc }: Props) {
|
export default function ManageBanner({ oidc }: Props) {
|
||||||
|
|||||||
@@ -14,14 +14,22 @@ import cn from '~/utils/cn';
|
|||||||
import { pull } from '~/utils/headscale';
|
import { pull } from '~/utils/headscale';
|
||||||
import { getSession } from '~/utils/sessions.server';
|
import { getSession } from '~/utils/sessions.server';
|
||||||
|
|
||||||
import { hp_getConfig, hs_getConfig } from '~/utils/state';
|
import { hs_getConfig } from '~/utils/config/loader';
|
||||||
|
import { noContext } from '~/utils/log';
|
||||||
|
import type { AppContext } from '~server/context/app';
|
||||||
import ManageBanner from './components/manage-banner';
|
import ManageBanner from './components/manage-banner';
|
||||||
import DeleteUser from './dialogs/delete-user';
|
import DeleteUser from './dialogs/delete-user';
|
||||||
import RenameUser from './dialogs/rename-user';
|
import RenameUser from './dialogs/rename-user';
|
||||||
import { userAction } from './user-actions';
|
import { userAction } from './user-actions';
|
||||||
|
|
||||||
export async function loader({ request }: LoaderFunctionArgs) {
|
export async function loader({
|
||||||
|
request,
|
||||||
|
context,
|
||||||
|
}: LoaderFunctionArgs<AppContext>) {
|
||||||
const session = await getSession(request.headers.get('Cookie'));
|
const session = await getSession(request.headers.get('Cookie'));
|
||||||
|
if (!context) {
|
||||||
|
throw noContext();
|
||||||
|
}
|
||||||
|
|
||||||
const [machines, apiUsers] = await Promise.all([
|
const [machines, apiUsers] = await Promise.all([
|
||||||
pull<{ nodes: Machine[] }>('v1/node', session.get('hsApiKey')!),
|
pull<{ nodes: Machine[] }>('v1/node', session.get('hsApiKey')!),
|
||||||
@@ -33,7 +41,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
|
|||||||
machines: machines.nodes.filter((machine) => machine.user.id === user.id),
|
machines: machines.nodes.filter((machine) => machine.user.id === user.id),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const context = hp_getConfig();
|
const ctx = context.context;
|
||||||
const { mode, config } = hs_getConfig();
|
const { mode, config } = hs_getConfig();
|
||||||
let magic: string | undefined;
|
let magic: string | undefined;
|
||||||
|
|
||||||
@@ -44,7 +52,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
oidc: context.oidc,
|
oidc: ctx.oidc,
|
||||||
magic,
|
magic,
|
||||||
users,
|
users,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { constants, access, readFile, writeFile } from 'node:fs/promises';
|
import { constants, access, readFile, writeFile } from 'node:fs/promises';
|
||||||
import { Document, parseDocument } from 'yaml';
|
import { Document, parseDocument } from 'yaml';
|
||||||
import { HeadplaneConfig } from '~/utils/context/parser';
|
|
||||||
import log from '~/utils/log';
|
import log from '~/utils/log';
|
||||||
import mutex from '~/utils/mutex';
|
import mutex from '~/utils/mutex';
|
||||||
|
import type { HeadplaneConfig } from '~server/context/parser';
|
||||||
import { HeadscaleConfig, validateConfig } from './parser';
|
import { HeadscaleConfig, validateConfig } from './parser';
|
||||||
|
|
||||||
let runtimeYaml: Document | undefined = undefined;
|
let runtimeYaml: Document | undefined = undefined;
|
||||||
@@ -13,7 +13,7 @@ let runtimeStrict = true;
|
|||||||
|
|
||||||
const runtimeLock = mutex();
|
const runtimeLock = mutex();
|
||||||
|
|
||||||
type ConfigModes =
|
export type ConfigModes =
|
||||||
| {
|
| {
|
||||||
mode: 'rw' | 'ro';
|
mode: 'rw' | 'ro';
|
||||||
config: HeadscaleConfig;
|
config: HeadscaleConfig;
|
||||||
|
|||||||
+32
-7
@@ -1,6 +1,7 @@
|
|||||||
import log from '~/utils/log';
|
import log, { noContext } from '~/utils/log';
|
||||||
import { hp_getConfig } from '~/utils/state';
|
import { AppContext } from '~server/context/app';
|
||||||
|
|
||||||
|
type Context = AppContext['context'];
|
||||||
export class HeadscaleError extends Error {
|
export class HeadscaleError extends Error {
|
||||||
status: number;
|
status: number;
|
||||||
|
|
||||||
@@ -20,8 +21,20 @@ export class FatalError extends Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let context: Context | undefined = undefined;
|
||||||
|
export function hp_storeContext(ctx: Context) {
|
||||||
|
if (context) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
context = ctx;
|
||||||
|
}
|
||||||
|
|
||||||
export async function healthcheck() {
|
export async function healthcheck() {
|
||||||
const context = hp_getConfig();
|
if (!context) {
|
||||||
|
throw noContext();
|
||||||
|
}
|
||||||
|
|
||||||
const prefix = context.headscale.url;
|
const prefix = context.headscale.url;
|
||||||
log.debug('APIC', 'GET /health');
|
log.debug('APIC', 'GET /health');
|
||||||
|
|
||||||
@@ -37,11 +50,14 @@ export async function healthcheck() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function pull<T>(url: string, key: string) {
|
export async function pull<T>(url: string, key: string) {
|
||||||
|
if (!context) {
|
||||||
|
throw noContext();
|
||||||
|
}
|
||||||
|
|
||||||
if (!key || key === 'undefined' || key.length === 0) {
|
if (!key || key === 'undefined' || key.length === 0) {
|
||||||
throw new Error('Missing API key, could this be a cookie setting issue?');
|
throw new Error('Missing API key, could this be a cookie setting issue?');
|
||||||
}
|
}
|
||||||
|
|
||||||
const context = hp_getConfig();
|
|
||||||
const prefix = context.headscale.url;
|
const prefix = context.headscale.url;
|
||||||
|
|
||||||
log.debug('APIC', 'GET %s', `${prefix}/api/${url}`);
|
log.debug('APIC', 'GET %s', `${prefix}/api/${url}`);
|
||||||
@@ -65,11 +81,14 @@ export async function pull<T>(url: string, key: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function post<T>(url: string, key: string, body?: unknown) {
|
export async function post<T>(url: string, key: string, body?: unknown) {
|
||||||
|
if (!context) {
|
||||||
|
throw noContext();
|
||||||
|
}
|
||||||
|
|
||||||
if (!key || key === 'undefined' || key.length === 0) {
|
if (!key || key === 'undefined' || key.length === 0) {
|
||||||
throw new Error('Missing API key, could this be a cookie setting issue?');
|
throw new Error('Missing API key, could this be a cookie setting issue?');
|
||||||
}
|
}
|
||||||
|
|
||||||
const context = hp_getConfig();
|
|
||||||
const prefix = context.headscale.url;
|
const prefix = context.headscale.url;
|
||||||
|
|
||||||
log.debug('APIC', 'POST %s', `${prefix}/api/${url}`);
|
log.debug('APIC', 'POST %s', `${prefix}/api/${url}`);
|
||||||
@@ -95,11 +114,14 @@ export async function post<T>(url: string, key: string, body?: unknown) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function put<T>(url: string, key: string, body?: unknown) {
|
export async function put<T>(url: string, key: string, body?: unknown) {
|
||||||
|
if (!context) {
|
||||||
|
throw noContext();
|
||||||
|
}
|
||||||
|
|
||||||
if (!key || key === 'undefined' || key.length === 0) {
|
if (!key || key === 'undefined' || key.length === 0) {
|
||||||
throw new Error('Missing API key, could this be a cookie setting issue?');
|
throw new Error('Missing API key, could this be a cookie setting issue?');
|
||||||
}
|
}
|
||||||
|
|
||||||
const context = hp_getConfig();
|
|
||||||
const prefix = context.headscale.url;
|
const prefix = context.headscale.url;
|
||||||
|
|
||||||
log.debug('APIC', 'PUT %s', `${prefix}/api/${url}`);
|
log.debug('APIC', 'PUT %s', `${prefix}/api/${url}`);
|
||||||
@@ -125,11 +147,14 @@ export async function put<T>(url: string, key: string, body?: unknown) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function del<T>(url: string, key: string) {
|
export async function del<T>(url: string, key: string) {
|
||||||
|
if (!context) {
|
||||||
|
throw noContext();
|
||||||
|
}
|
||||||
|
|
||||||
if (!key || key === 'undefined' || key.length === 0) {
|
if (!key || key === 'undefined' || key.length === 0) {
|
||||||
throw new Error('Missing API key, could this be a cookie setting issue?');
|
throw new Error('Missing API key, could this be a cookie setting issue?');
|
||||||
}
|
}
|
||||||
|
|
||||||
const context = hp_getConfig();
|
|
||||||
const prefix = context.headscale.url;
|
const prefix = context.headscale.url;
|
||||||
|
|
||||||
log.debug('APIC', 'DELETE %s', `${prefix}/api/${url}`);
|
log.debug('APIC', 'DELETE %s', `${prefix}/api/${url}`);
|
||||||
|
|||||||
+6
-10
@@ -3,16 +3,6 @@ export function hp_loadLogger(debug: boolean) {
|
|||||||
log.debug = (category: string, message: string, ...args: unknown[]) => {
|
log.debug = (category: string, message: string, ...args: unknown[]) => {
|
||||||
defaultLog('DEBG', category, message, ...args);
|
defaultLog('DEBG', category, message, ...args);
|
||||||
};
|
};
|
||||||
|
|
||||||
log.info('CFGX', 'Debug logging enabled');
|
|
||||||
log.info(
|
|
||||||
'CFGX',
|
|
||||||
'This is very verbose and should only be used for debugging purposes',
|
|
||||||
);
|
|
||||||
log.info(
|
|
||||||
'CFGX',
|
|
||||||
'If you run this in production, your storage WILL fill up quickly',
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,4 +33,10 @@ function defaultLog(
|
|||||||
console.log(`${date} (${level}) [${category}] ${message}`, ...args);
|
console.log(`${date} (${level}) [${category}] ${message}`, ...args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function noContext() {
|
||||||
|
return new Error(
|
||||||
|
'Context is not loaded. This is most likely a configuration error with your reverse proxy.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export default log;
|
export default log;
|
||||||
|
|||||||
+2
-3
@@ -1,13 +1,12 @@
|
|||||||
import * as client from 'openid-client';
|
import * as client from 'openid-client';
|
||||||
import log from '~/utils/log';
|
import log from '~/utils/log';
|
||||||
import { HeadplaneConfig } from '~/utils/state';
|
import type { AppContext } from '~server/context/app';
|
||||||
|
|
||||||
|
type OidcConfig = NonNullable<AppContext['context']['oidc']>;
|
||||||
declare global {
|
declare global {
|
||||||
const __PREFIX__: string;
|
const __PREFIX__: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
type OidcConfig = NonNullable<HeadplaneConfig['oidc']>;
|
|
||||||
|
|
||||||
// We try our best to infer the callback URI of our Headplane instance
|
// We try our best to infer the callback URI of our Headplane instance
|
||||||
// By default it is always /<base_path>/oidc/callback
|
// By default it is always /<base_path>/oidc/callback
|
||||||
// (This can ALWAYS be overridden through the OidcConfig)
|
// (This can ALWAYS be overridden through the OidcConfig)
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
export { hp_getConfig } from '~/utils/context/loader';
|
|
||||||
export { hs_getConfig } from '~/utils/config/loader';
|
|
||||||
|
|
||||||
export type { HeadplaneConfig } from '~/utils/context/parser';
|
|
||||||
export type { HeadscaleConfig } from '~/utils/config/parser';
|
|
||||||
+39
-32
@@ -2,8 +2,8 @@
|
|||||||
import { readFile, writeFile } from 'node:fs/promises';
|
import { readFile, writeFile } from 'node:fs/promises';
|
||||||
import { setTimeout as pSetTimeout } from 'node:timers/promises';
|
import { setTimeout as pSetTimeout } from 'node:timers/promises';
|
||||||
import type { LoaderFunctionArgs } from 'react-router';
|
import type { LoaderFunctionArgs } from 'react-router';
|
||||||
import type { HostInfo } from '~/types';
|
|
||||||
import { WebSocket } from 'ws';
|
import { WebSocket } from 'ws';
|
||||||
|
import type { HostInfo } from '~/types';
|
||||||
import log from './log';
|
import log from './log';
|
||||||
|
|
||||||
// Essentially a HashMap which invalidates entries after a certain time.
|
// Essentially a HashMap which invalidates entries after a certain time.
|
||||||
@@ -68,7 +68,7 @@ class TimedCache<K, V> {
|
|||||||
|
|
||||||
this.writeLock = true;
|
this.writeLock = true;
|
||||||
const data = Array.from(this._cache.entries()).map(([key, value]) => {
|
const data = Array.from(this._cache.entries()).map(([key, value]) => {
|
||||||
return { key, value, expires: this._timeCache.get(key) }
|
return { key, value, expires: this._timeCache.get(key) };
|
||||||
});
|
});
|
||||||
|
|
||||||
await writeFile(this.filepath, JSON.stringify(data), 'utf-8');
|
await writeFile(this.filepath, JSON.stringify(data), 'utf-8');
|
||||||
@@ -85,10 +85,11 @@ export async function initAgentCache(defaultTTL: number, filepath: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let agentSocket: WebSocket | undefined;
|
let agentSocket: WebSocket | undefined;
|
||||||
|
// TODO: Actually type this?
|
||||||
export function initAgentSocket(context: LoaderFunctionArgs['context']) {
|
export function initAgentSocket(context: LoaderFunctionArgs['context']) {
|
||||||
if (!context.ws) {
|
if (!context.ws) {
|
||||||
return;
|
return;
|
||||||
};
|
}
|
||||||
|
|
||||||
const client = context.ws.clients.values().next().value;
|
const client = context.ws.clients.values().next().value;
|
||||||
agentSocket = client;
|
agentSocket = client;
|
||||||
@@ -97,21 +98,24 @@ export function initAgentSocket(context: LoaderFunctionArgs['context']) {
|
|||||||
// Check the cache and then attempt the websocket query
|
// Check the cache and then attempt the websocket query
|
||||||
// If we aren't connected to an agent, then debug log and return the cache
|
// If we aren't connected to an agent, then debug log and return the cache
|
||||||
export async function queryAgent(nodes: string[]) {
|
export async function queryAgent(nodes: string[]) {
|
||||||
return;
|
return;
|
||||||
|
// biome-ignore lint: bruh
|
||||||
if (!cache) {
|
if (!cache) {
|
||||||
log.error('CACH', 'Cache not initialized');
|
log.error('CACH', 'Cache not initialized');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const cached: Record<string, HostInfo> = {};
|
const cached: Record<string, HostInfo> = {};
|
||||||
await Promise.all(nodes.map(async node => {
|
await Promise.all(
|
||||||
const cachedData = await cache?.get(node);
|
nodes.map(async (node) => {
|
||||||
if (cachedData) {
|
const cachedData = await cache?.get(node);
|
||||||
cached[node] = cachedData;
|
if (cachedData) {
|
||||||
}
|
cached[node] = cachedData;
|
||||||
}))
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
const uncached = nodes.filter(node => !cached[node]);
|
const uncached = nodes.filter((node) => !cached[node]);
|
||||||
|
|
||||||
// No need to query the agent if we have all the data cached
|
// No need to query the agent if we have all the data cached
|
||||||
if (uncached.length === 0) {
|
if (uncached.length === 0) {
|
||||||
@@ -124,29 +128,32 @@ export async function queryAgent(nodes: string[]) {
|
|||||||
return cached;
|
return cached;
|
||||||
}
|
}
|
||||||
|
|
||||||
agentSocket.send(JSON.stringify({ NodeIDs: uncached }));
|
agentSocket?.send(JSON.stringify({ NodeIDs: uncached }));
|
||||||
const returnData = await new Promise<Record<string, HostInfo> | void>((resolve, reject) => {
|
// biome-ignore lint: bruh
|
||||||
const timeout = setTimeout(() => {
|
const returnData = await new Promise<Record<string, HostInfo> | void>(
|
||||||
agentSocket?.removeAllListeners('message');
|
(resolve, reject) => {
|
||||||
resolve();
|
const timeout = setTimeout(() => {
|
||||||
}, 3000);
|
agentSocket?.removeAllListeners('message');
|
||||||
|
|
||||||
agentSocket?.on('message', async (message: string) => {
|
|
||||||
const data = JSON.parse(message.toString());
|
|
||||||
if (Object.keys(data).length === 0) {
|
|
||||||
resolve();
|
resolve();
|
||||||
}
|
}, 3000);
|
||||||
|
|
||||||
agentSocket?.removeAllListeners('message');
|
agentSocket?.on('message', async (message: string) => {
|
||||||
resolve(data);
|
const data = JSON.parse(message.toString());
|
||||||
});
|
if (Object.keys(data).length === 0) {
|
||||||
});
|
resolve();
|
||||||
|
}
|
||||||
if (returnData) {
|
|
||||||
for await (const [node, info] of Object.entries(returnData)) {
|
agentSocket?.removeAllListeners('message');
|
||||||
await cache.set(node, info);
|
resolve(data);
|
||||||
}
|
});
|
||||||
}
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
// if (returnData) {
|
||||||
|
// for await (const [node, info] of Object.entries(returnData)) {
|
||||||
|
// await cache?.set(node, info);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
return returnData ? { ...cached, ...returnData } : cached;
|
return returnData ? { ...cached, ...returnData } : cached;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
import { hp_getConfig } from './loader';
|
||||||
|
import { HeadplaneConfig } from './parser';
|
||||||
|
|
||||||
|
export interface AppContext {
|
||||||
|
context: HeadplaneConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function appContext() {
|
||||||
|
return {
|
||||||
|
context: hp_getConfig(),
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,11 +1,9 @@
|
|||||||
import { constants, access, readFile } from 'node:fs/promises';
|
import { constants, access, readFile } from 'node:fs/promises';
|
||||||
import { type } from 'arktype';
|
import { type } from 'arktype';
|
||||||
import { parseDocument } from 'yaml';
|
import { parseDocument } from 'yaml';
|
||||||
import { hs_loadConfig } from '~/utils/config/loader';
|
|
||||||
import log, { hp_loadLogger } from '~/utils/log';
|
|
||||||
import mutex from '~/utils/mutex';
|
|
||||||
import { testOidc } from '~/utils/oidc';
|
import { testOidc } from '~/utils/oidc';
|
||||||
import { initSessionManager } from '~/utils/sessions.server';
|
import log, { hpServer_loadLogger } from '~server/utils/log';
|
||||||
|
import mutex from '~server/utils/mutex';
|
||||||
import { HeadplaneConfig, coalesceConfig, validateConfig } from './parser';
|
import { HeadplaneConfig, coalesceConfig, validateConfig } from './parser';
|
||||||
|
|
||||||
const envBool = type('string | undefined').pipe((v) => {
|
const envBool = type('string | undefined').pipe((v) => {
|
||||||
@@ -67,7 +65,7 @@ export async function hp_loadConfig() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Load our debug based logger before ANYTHING
|
// Load our debug based logger before ANYTHING
|
||||||
hp_loadLogger(envs.HEADPLANE_DEBUG_LOG);
|
hpServer_loadLogger(envs.HEADPLANE_DEBUG_LOG);
|
||||||
|
|
||||||
if (envs.HEADPLANE_CONFIG_PATH) {
|
if (envs.HEADPLANE_CONFIG_PATH) {
|
||||||
path = envs.HEADPLANE_CONFIG_PATH;
|
path = envs.HEADPLANE_CONFIG_PATH;
|
||||||
@@ -104,17 +102,11 @@ export async function hp_loadConfig() {
|
|||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config.headscale.config_path) {
|
|
||||||
await hs_loadConfig(config);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (config.oidc?.strict_validation) {
|
if (config.oidc?.strict_validation) {
|
||||||
testOidc(config.oidc);
|
testOidc(config.oidc);
|
||||||
}
|
}
|
||||||
|
|
||||||
runtimeConfig = config;
|
runtimeConfig = config;
|
||||||
|
|
||||||
initSessionManager(config.server.cookie_secret, config.server.cookie_secure);
|
|
||||||
runtimeLock.release();
|
runtimeLock.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,9 +1,7 @@
|
|||||||
import { type } from 'arktype';
|
import { type } from 'arktype';
|
||||||
import log from '~/utils/log';
|
import log from '~server/utils/log';
|
||||||
|
|
||||||
// TODO: ALLOW HEADSCALE CONFIG TO OVERRIDE HEADPLANE CONFIG MAYBE FOR OIDC?
|
|
||||||
export type HeadplaneConfig = typeof headplaneConfig.infer;
|
export type HeadplaneConfig = typeof headplaneConfig.infer;
|
||||||
|
|
||||||
const stringToBool = type('string | boolean').pipe((v) => Boolean(v));
|
const stringToBool = type('string | boolean').pipe((v) => Boolean(v));
|
||||||
const serverConfig = type({
|
const serverConfig = type({
|
||||||
host: 'string.ip',
|
host: 'string.ip',
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import { createRequestHandler } from 'react-router'
|
import { createRequestHandler } from 'react-router';
|
||||||
|
|
||||||
export default createRequestHandler(
|
export default createRequestHandler(
|
||||||
// @ts-expect-error: React Router Vite plugin
|
// @ts-expect-error: React Router Vite plugin
|
||||||
() => import('virtual:react-router/server-build'),
|
() => import('virtual:react-router/server-build'),
|
||||||
'development'
|
'development',
|
||||||
);
|
);
|
||||||
@@ -1,23 +1,23 @@
|
|||||||
import log from '~server/log';
|
import { type ViteDevServer, createServer } from 'vite';
|
||||||
import { createServer, type ViteDevServer } from 'vite';
|
import log from '~server/utils/log';
|
||||||
import { type createRequestHandler } from 'react-router';
|
|
||||||
|
|
||||||
|
// TODO: Remove env.NODE_ENV
|
||||||
let server: ViteDevServer | undefined;
|
let server: ViteDevServer | undefined;
|
||||||
export async function loadDevtools() {
|
export async function loadDevtools() {
|
||||||
log.info('DEVX', 'Starting Vite Development server')
|
log.info('DEVX', 'Starting Vite Development server');
|
||||||
process.env.NODE_ENV = 'development';
|
process.env.NODE_ENV = 'development';
|
||||||
|
|
||||||
// This is loading the ROOT vite.config.ts
|
// This is loading the ROOT vite.config.ts
|
||||||
server = await createServer({
|
server = await createServer({
|
||||||
server: {
|
server: {
|
||||||
middlewareMode: true,
|
middlewareMode: true,
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// We can't just do ssrLoadModule for virtual:react-router/server-build
|
// We can't just do ssrLoadModule for virtual:react-router/server-build
|
||||||
// because for hot reload to work server side it needs to be imported
|
// because for hot reload to work server side it needs to be imported
|
||||||
// using builtin import in its own file.
|
// using builtin import in its own file.
|
||||||
const handler = await server.ssrLoadModule('./server/dev-handler.ts');
|
const handler = await server.ssrLoadModule('./server/dev/dev-handler.ts');
|
||||||
return {
|
return {
|
||||||
server,
|
server,
|
||||||
handler: handler.default,
|
handler: handler.default,
|
||||||
@@ -26,11 +26,11 @@ export async function loadDevtools() {
|
|||||||
|
|
||||||
export async function stacksafeTry(
|
export async function stacksafeTry(
|
||||||
devtools: {
|
devtools: {
|
||||||
server: ViteDevServer,
|
server: ViteDevServer;
|
||||||
handler: any, // import() is dynamic
|
handler: (req: Request, context: unknown) => Promise<Response>;
|
||||||
},
|
},
|
||||||
req: Request,
|
req: Request,
|
||||||
context: unknown
|
context: unknown,
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
const result = await devtools.handler(req, context);
|
const result = await devtools.handler(req, context);
|
||||||
@@ -38,7 +38,6 @@ export async function stacksafeTry(
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
log.error('DEVX', 'Error in request handler', error);
|
log.error('DEVX', 'Error in request handler', error);
|
||||||
if (typeof error === 'object' && error instanceof Error) {
|
if (typeof error === 'object' && error instanceof Error) {
|
||||||
console.log('got error');
|
|
||||||
devtools.server.ssrFixStacktrace(error);
|
devtools.server.ssrFixStacktrace(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
+21
-17
@@ -1,8 +1,9 @@
|
|||||||
|
// import { initWebsocket } from '~server/ws';
|
||||||
|
import { constants, access } from 'node:fs/promises';
|
||||||
import { createServer } from 'node:http';
|
import { createServer } from 'node:http';
|
||||||
|
import { hp_getConfig, hp_loadConfig } from '~server/context/loader';
|
||||||
import { listener } from '~server/listener';
|
import { listener } from '~server/listener';
|
||||||
import { initWebsocket } from '~server/ws';
|
import log from '~server/utils/log';
|
||||||
import { access, constants } from 'node:fs/promises';
|
|
||||||
import log from '~server/log';
|
|
||||||
|
|
||||||
log.info('SRVX', 'Running Node.js %s', process.versions.node);
|
log.info('SRVX', 'Running Node.js %s', process.versions.node);
|
||||||
|
|
||||||
@@ -15,21 +16,26 @@ try {
|
|||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await hp_loadConfig();
|
||||||
const server = createServer(listener);
|
const server = createServer(listener);
|
||||||
const port = process.env.PORT || 3000;
|
|
||||||
const host = process.env.HOST || '0.0.0.0';
|
|
||||||
|
|
||||||
const ws = initWebsocket();
|
// const ws = initWebsocket();
|
||||||
if (ws) {
|
// if (ws) {
|
||||||
server.on('upgrade', (req, socket, head) => {
|
// server.on('upgrade', (req, socket, head) => {
|
||||||
ws.handleUpgrade(req, socket, head, (ws) => {
|
// ws.handleUpgrade(req, socket, head, (ws) => {
|
||||||
ws.emit('connection', ws, req);
|
// ws.emit('connection', ws, req);
|
||||||
});
|
// });
|
||||||
});
|
// });
|
||||||
}
|
// }
|
||||||
|
|
||||||
server.listen(Number(port), host, () => {
|
const context = hp_getConfig();
|
||||||
log.info('SRVX', 'Running on %s:%s', host, port);
|
server.listen(context.server.port, context.server.host, () => {
|
||||||
|
log.info(
|
||||||
|
'SRVX',
|
||||||
|
'Running on %s:%s',
|
||||||
|
context.server.host,
|
||||||
|
context.server.port,
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (import.meta.hot) {
|
if (import.meta.hot) {
|
||||||
@@ -41,5 +47,3 @@ if (import.meta.hot) {
|
|||||||
server.close();
|
server.close();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// export const app = listener;
|
|
||||||
|
|||||||
+18
-19
@@ -1,17 +1,13 @@
|
|||||||
import { type RequestListener } from 'node:http';
|
|
||||||
import { resolve, join } from 'node:path'
|
|
||||||
import { createServer } from 'vite'
|
|
||||||
import { createRequestHandler } from 'react-router'
|
|
||||||
import { access, constants } from 'node:fs/promises';
|
|
||||||
import { createReadStream, existsSync, statSync } from 'node:fs';
|
import { createReadStream, existsSync, statSync } from 'node:fs';
|
||||||
|
import { type RequestListener } from 'node:http';
|
||||||
|
import { join, resolve } from 'node:path';
|
||||||
import {
|
import {
|
||||||
createReadableStreamFromReadable,
|
createReadableStreamFromReadable,
|
||||||
writeReadableStreamToWritable,
|
writeReadableStreamToWritable,
|
||||||
} from '@react-router/node';
|
} from '@react-router/node';
|
||||||
import mime from 'mime/lite'
|
import mime from 'mime/lite';
|
||||||
|
import appContext from '~server/context/app';
|
||||||
import { loadDevtools, stacksafeTry } from '~server/dev';
|
import { loadDevtools, stacksafeTry } from '~server/dev/hot-server';
|
||||||
import { appContext } from '~server/ws';
|
|
||||||
import prodBuild from '~server/prod-handler';
|
import prodBuild from '~server/prod-handler';
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
@@ -19,13 +15,9 @@ declare global {
|
|||||||
const __hp_prefix: string;
|
const __hp_prefix: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const devtools = import.meta.env.DEV
|
const devtools = import.meta.env.DEV ? await loadDevtools() : undefined;
|
||||||
? await loadDevtools()
|
|
||||||
: undefined;
|
|
||||||
|
|
||||||
const prodHandler = import.meta.env.PROD
|
const prodHandler = import.meta.env.PROD ? await prodBuild() : undefined;
|
||||||
? await prodBuild()
|
|
||||||
: undefined;
|
|
||||||
|
|
||||||
const buildPath = process.env.BUILD_PATH ?? './build';
|
const buildPath = process.env.BUILD_PATH ?? './build';
|
||||||
const baseDir = resolve(join(buildPath, 'client'));
|
const baseDir = resolve(join(buildPath, 'client'));
|
||||||
@@ -113,15 +105,22 @@ export const listener: RequestListener = async (req, res) => {
|
|||||||
// If we have a body, we set the duplex and load it
|
// If we have a body, we set the duplex and load it
|
||||||
...(req.method !== 'GET' && req.method !== 'HEAD'
|
...(req.method !== 'GET' && req.method !== 'HEAD'
|
||||||
? {
|
? {
|
||||||
body: createReadableStreamFromReadable(req),
|
body: createReadableStreamFromReadable(req),
|
||||||
duplex: 'half',
|
duplex: 'half',
|
||||||
} : {}),
|
}
|
||||||
|
: {}),
|
||||||
});
|
});
|
||||||
|
|
||||||
const response = devtools
|
const response = devtools
|
||||||
? await stacksafeTry(devtools, frameworkReq, appContext())
|
? await stacksafeTry(devtools, frameworkReq, appContext())
|
||||||
: await prodHandler?.(frameworkReq, appContext());
|
: await prodHandler?.(frameworkReq, appContext());
|
||||||
|
|
||||||
|
if (!response) {
|
||||||
|
res.writeHead(404);
|
||||||
|
res.end();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
res.statusCode = response.status;
|
res.statusCode = response.status;
|
||||||
res.statusMessage = response.statusText;
|
res.statusMessage = response.statusText;
|
||||||
|
|
||||||
@@ -134,4 +133,4 @@ export const listener: RequestListener = async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
res.end();
|
res.end();
|
||||||
}
|
};
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import { createRequestHandler } from 'react-router'
|
import { constants, access } from 'node:fs/promises';
|
||||||
import { access, constants } from 'node:fs/promises';
|
|
||||||
import { join, resolve } from 'node:path';
|
import { join, resolve } from 'node:path';
|
||||||
import log from '~server/log';
|
import { createRequestHandler } from 'react-router';
|
||||||
|
import log from '~server/utils/log';
|
||||||
|
|
||||||
export default async function() {
|
export default async function () {
|
||||||
const buildPath = process.env.BUILD_PATH ?? './build';
|
const buildPath = process.env.BUILD_PATH ?? './build';
|
||||||
const server = resolve(join(buildPath, 'server'));
|
const server = resolve(join(buildPath, 'server'));
|
||||||
|
|
||||||
@@ -12,7 +12,10 @@ export default async function() {
|
|||||||
log.info('SRVX', 'Using build directory %s', resolve(buildPath));
|
log.info('SRVX', 'Using build directory %s', resolve(buildPath));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log.error('SRVX', 'No build found. Please refer to the documentation');
|
log.error('SRVX', 'No build found. Please refer to the documentation');
|
||||||
log.error('SRVX', 'https://github.com/tale/headplane/blob/main/docs/integration/Native.md');
|
log.error(
|
||||||
|
'SRVX',
|
||||||
|
'https://github.com/tale/headplane/blob/main/docs/integration/Native.md',
|
||||||
|
);
|
||||||
console.error(error);
|
console.error(error);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,22 @@
|
|||||||
export default {
|
export function hpServer_loadLogger(debug: boolean) {
|
||||||
|
if (debug) {
|
||||||
|
log.debug = (category: string, message: string, ...args: unknown[]) => {
|
||||||
|
defaultLog('DEBG', category, message, ...args);
|
||||||
|
};
|
||||||
|
|
||||||
|
log.info('CFGX', 'Debug logging enabled');
|
||||||
|
log.info(
|
||||||
|
'CFGX',
|
||||||
|
'This is very verbose and should only be used for debugging purposes',
|
||||||
|
);
|
||||||
|
log.info(
|
||||||
|
'CFGX',
|
||||||
|
'If you run this in production, your storage COULD fill up quickly',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const log = {
|
||||||
info: (category: string, message: string, ...args: unknown[]) => {
|
info: (category: string, message: string, ...args: unknown[]) => {
|
||||||
defaultLog('INFO', category, message, ...args);
|
defaultLog('INFO', category, message, ...args);
|
||||||
},
|
},
|
||||||
@@ -11,11 +29,7 @@ export default {
|
|||||||
defaultLog('ERRO', category, message, ...args);
|
defaultLog('ERRO', category, message, ...args);
|
||||||
},
|
},
|
||||||
|
|
||||||
debug: (category: string, message: string, ...args: unknown[]) => {
|
debug: (category: string, message: string, ...args: unknown[]) => {},
|
||||||
if (process.env.DEBUG === 'true') {
|
|
||||||
defaultLog('DEBG', category, message, ...args);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function defaultLog(
|
function defaultLog(
|
||||||
@@ -27,3 +41,5 @@ function defaultLog(
|
|||||||
const date = new Date().toISOString();
|
const date = new Date().toISOString();
|
||||||
console.log(`${date} (${level}) [${category}] ${message}`, ...args);
|
console.log(`${date} (${level}) [${category}] ${message}`, ...args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default log;
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
class Mutex {
|
||||||
|
private locked = false;
|
||||||
|
private queue: (() => void)[] = [];
|
||||||
|
|
||||||
|
constructor(locked: boolean) {
|
||||||
|
this.locked = locked;
|
||||||
|
}
|
||||||
|
|
||||||
|
acquire() {
|
||||||
|
return new Promise<void>((resolve) => {
|
||||||
|
if (!this.locked) {
|
||||||
|
this.locked = true;
|
||||||
|
resolve();
|
||||||
|
} else {
|
||||||
|
this.queue.push(resolve);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
release() {
|
||||||
|
if (this.queue.length > 0) {
|
||||||
|
const next = this.queue.shift();
|
||||||
|
next?.();
|
||||||
|
} else {
|
||||||
|
this.locked = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function mutex(locked = false) {
|
||||||
|
return new Mutex(locked);
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import WebSocket, { WebSocketServer } from 'ws'
|
import WebSocket, { WebSocketServer } from 'ws';
|
||||||
import log from '~server/log'
|
import log from '~server/utils/log';
|
||||||
|
|
||||||
const server = new WebSocketServer({ noServer: true });
|
const server = new WebSocketServer({ noServer: true });
|
||||||
export function initWebsocket() {
|
export function initWebsocket() {
|
||||||
@@ -13,6 +13,7 @@ export function initWebsocket() {
|
|||||||
|
|
||||||
log.info('CACH', 'Initializing agent WebSocket');
|
log.info('CACH', 'Initializing agent WebSocket');
|
||||||
server.on('connection', (ws, req) => {
|
server.on('connection', (ws, req) => {
|
||||||
|
// biome-ignore lint: this file is not USED
|
||||||
const auth = req.headers['authorization'];
|
const auth = req.headers['authorization'];
|
||||||
if (auth !== `Bearer ${key}`) {
|
if (auth !== `Bearer ${key}`) {
|
||||||
log.warn('CACH', 'Invalid agent WebSocket connection');
|
log.warn('CACH', 'Invalid agent WebSocket connection');
|
||||||
@@ -20,7 +21,6 @@ export function initWebsocket() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const nodeID = req.headers['x-headplane-ts-node-id'];
|
const nodeID = req.headers['x-headplane-ts-node-id'];
|
||||||
if (!nodeID) {
|
if (!nodeID) {
|
||||||
log.warn('CACH', 'Invalid agent WebSocket connection');
|
log.warn('CACH', 'Invalid agent WebSocket connection');
|
||||||
@@ -46,7 +46,7 @@ export function initWebsocket() {
|
|||||||
log.error('CACH', 'Closing agent WebSocket connection');
|
log.error('CACH', 'Closing agent WebSocket connection');
|
||||||
log.error('CACH', 'Agent WebSocket error: %s', error);
|
log.error('CACH', 'Agent WebSocket error: %s', error);
|
||||||
ws.close(1011, 'ERR_INTERNAL_ERROR');
|
ws.close(1011, 'ERR_INTERNAL_ERROR');
|
||||||
})
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
return server;
|
return server;
|
||||||
@@ -56,5 +56,5 @@ export function appContext() {
|
|||||||
return {
|
return {
|
||||||
ws: server,
|
ws: server,
|
||||||
wsAuthKey: process.env.LOCAL_AGENT_AUTHKEY,
|
wsAuthKey: process.env.LOCAL_AGENT_AUTHKEY,
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user