feat: respect context in server

This commit is contained in:
Aarnav Tale 2025-02-19 18:09:42 -05:00
parent 06049169a2
commit f5436f5ee3
No known key found for this signature in database
32 changed files with 359 additions and 204 deletions

View File

@ -1,24 +1,39 @@
import { PassThrough } from 'node:stream';
import { createReadableStreamFromReadable } from '@react-router/node';
import { isbot } from 'isbot';
import type { RenderToPipeableStreamOptions } from 'react-dom/server';
import { renderToPipeableStream } from 'react-dom/server';
import type { AppLoadContext, EntryContext } 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;
// TODO: checkOidc
export default function handleRequest(
request: Request,
responseStatusCode: number,
responseHeaders: Headers,
routerContext: EntryContext,
loadContext: AppLoadContext,
loadContext: AppContext,
) {
const { context } = loadContext;
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;
const userAgent = request.headers.get('user-agent');

View File

@ -6,25 +6,34 @@ import {
} from 'react-router';
import Footer from '~/components/Footer';
import Header from '~/components/Header';
import { hs_getConfig } from '~/utils/config/loader';
import { noContext } from '~/utils/log';
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
// 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'));
if (!session.has('hsApiKey')) {
return redirect('/login');
}
const context = hp_getConfig();
if (!context) {
throw noContext();
}
const ctx = context.context;
const { mode, config } = hs_getConfig();
return {
config,
url: context.headscale.public_url ?? context.headscale.url,
url: ctx.headscale.public_url ?? ctx.headscale.url,
configAvailable: mode !== 'no',
debug: context.debug,
debug: ctx.debug,
user: session.get('user'),
};
}

View File

@ -7,17 +7,18 @@ import Link from '~/components/Link';
import Notice from '~/components/Notice';
import Spinner from '~/components/Spinner';
import Tabs from '~/components/Tabs';
import { hs_getConfig } from '~/utils/config/loader';
import { HeadscaleError, pull, put } from '~/utils/headscale';
import log from '~/utils/log';
import { send } from '~/utils/res';
import { getSession } from '~/utils/sessions.server';
import { hs_getConfig } from '~/utils/state';
import toast from '~/utils/toast';
import type { AppContext } from '~server/context/app';
import { Differ, Editor } from './components/cm.client';
import { ErrorView } from './components/error';
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'));
// The way policy is handled in 0.23 of Headscale and later is verbose.

View File

@ -10,10 +10,14 @@ import Code from '~/components/Code';
import Input from '~/components/Input';
import type { Key } from '~/types';
import { pull } from '~/utils/headscale';
import { noContext } from '~/utils/log';
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'));
if (session.has('hsApiKey')) {
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
if (context.oidc?.disable_api_key_login) {
const ctx = context.context;
if (ctx.oidc?.disable_api_key_login) {
return redirect('/oidc/start');
}
return {
oidc: context.oidc?.issuer,
apiKey: !context.oidc?.disable_api_key_login,
oidc: ctx.oidc?.issuer,
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 oidcStart = formData.get('oidc-start');
const session = await getSession(request.headers.get('Cookie'));
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');
}

View File

@ -1,10 +1,14 @@
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 { 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
const url = new URL(request.url);
if (url.searchParams.toString().length === 0) {
@ -16,7 +20,11 @@ export async function loader({ request }: LoaderFunctionArgs) {
return redirect('/machines');
}
const { oidc } = hp_getConfig();
if (!context) {
throw noContext();
}
const { oidc } = context.context;
if (!oidc) {
throw new Error('An invalid OIDC configuration was provided');
}

View File

@ -1,17 +1,23 @@
import { type LoaderFunctionArgs, redirect } from 'react-router';
import { noContext } from '~/utils/log';
import { beginAuthFlow, getRedirectUri } from '~/utils/oidc';
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'));
if (session.has('hsApiKey')) {
return redirect('/machines');
}
// This is a hold-over from the old code
// TODO: Rewrite checkOIDC in the context loader
const { oidc } = hp_getConfig();
if (!context) {
throw noContext();
}
const { oidc } = context.context;
if (!oidc) {
throw new Error('An invalid OIDC configuration was provided');
}

View File

@ -1,7 +1,6 @@
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 { hs_getConfig } from '~/utils/state';
export async function dnsAction({ request }: ActionFunctionArgs) {
const session = await auth(request);

View File

@ -2,7 +2,7 @@ import type { ActionFunctionArgs } from 'react-router';
import { useLoaderData } from 'react-router';
import Code from '~/components/Code';
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 ManageNS from './components/manage-ns';
import ManageRecords from './components/manage-records';

View File

@ -109,9 +109,13 @@ export async function menuAction(request: ActionFunctionArgs['request']) {
const to = String(data.get('to'));
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}` };
} catch {
} catch (error) {
console.error(error);
return send(
{ message: `Failed to move node ${id} to ${to}` },
{

View File

@ -11,9 +11,9 @@ import StatusCircle from '~/components/StatusCircle';
import Tooltip from '~/components/Tooltip';
import type { Machine, Route, User } from '~/types';
import cn from '~/utils/cn';
import { hs_getConfig } from '~/utils/config/loader';
import { pull } from '~/utils/headscale';
import { getSession } from '~/utils/sessions.server';
import { hs_getConfig } from '~/utils/state';
import { menuAction } from './action';
import MenuOptions from './components/menu';
import Routes from './dialogs/routes';

View File

@ -12,12 +12,17 @@ import { getSession } from '~/utils/sessions.server';
import { initAgentSocket, queryAgent } from '~/utils/ws-agent';
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 MachineRow from './components/machine';
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 [machines, routes, users] = await Promise.all([
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')!),
]);
initAgentSocket(lC);
if (!context) {
throw noContext();
}
initAgentSocket(context);
const stats = await queryAgent(machines.nodes.map((node) => node.nodeKey));
const context = hp_getConfig();
const ctx = context.context;
const { mode, config } = hs_getConfig();
let magic: string | undefined;
@ -45,8 +54,8 @@ export async function loader({ request, context: lC }: LoaderFunctionArgs) {
users: users.users,
magic,
stats,
server: context.headscale.url,
publicServer: context.headscale.public_url,
server: ctx.headscale.url,
publicServer: ctx.headscale.public_url,
};
}

View File

@ -7,9 +7,10 @@ import Select from '~/components/Select';
import TableList from '~/components/TableList';
import type { PreAuthKey, User } from '~/types';
import { post, pull } from '~/utils/headscale';
import { noContext } from '~/utils/log';
import { send } from '~/utils/res';
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 AddPreAuthKey from './dialogs/new';
@ -90,14 +91,21 @@ export async function action({ request }: ActionFunctionArgs) {
}
}
export async function loader({ request }: LoaderFunctionArgs) {
const context = hp_getConfig();
export async function loader({
request,
context,
}: LoaderFunctionArgs<AppContext>) {
const session = await getSession(request.headers.get('Cookie'));
const users = await pull<{ users: User[] }>(
'v1/user',
session.get('hsApiKey')!,
);
if (!context) {
throw noContext();
}
const ctx = context.context;
const preAuthKeys = await Promise.all(
users.users.map((user) => {
const qp = new URLSearchParams();
@ -113,7 +121,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
return {
keys: preAuthKeys.flatMap((keys) => keys.preAuthKeys),
users: users.users,
server: context.headscale.public_url ?? context.headscale.url,
server: ctx.headscale.public_url ?? ctx.headscale.url,
};
}

View File

@ -1,22 +1,23 @@
import { useMemo } from 'react';
import { useLoaderData, type LoaderFunctionArgs } from 'react-router';
import { getSession, commitSession } from '~/utils/sessions.server'
import { queryAgent } from '~/utils/ws-agent'
import AgentManagement from './components/agent/manage'
import { type LoaderFunctionArgs, useLoaderData } from 'react-router';
import { commitSession, getSession } from '~/utils/sessions.server';
import { queryAgent } from '~/utils/ws-agent';
import AgentManagement from './components/agent/manage';
export async function loader({ request, context }: LoaderFunctionArgs) {
const { ws, wsAuthKey } = context;
const session = await getSession(request.headers.get('Cookie'));
const onboarding = session.get('agent_onboarding') ?? false;
const nodeKey = 'nodekey:542dad28354eb8d51e240aada7adf0222ba3ecc74af0bbd56123f03eefdb391b'
const nodeKey =
'nodekey:542dad28354eb8d51e240aada7adf0222ba3ecc74af0bbd56123f03eefdb391b';
const stats = await queryAgent([nodeKey]);
return {
configured: wsAuthKey !== undefined,
onboarding,
stats: stats[nodeKey]
}
stats: stats?.[nodeKey],
};
}
export default function Page() {
@ -24,21 +25,18 @@ export default function Page() {
// Whether we show the onboarding or management UI
const management = useMemo(() => {
return data.configured && (data.onboarding === false)
return data.configured && data.onboarding === false;
}, [data.configured, data.onboarding]);
return (
<div className="flex flex-col gap-8 max-w-screen-lg">
{management ? (
<AgentManagement
reachable={true}
hostInfo={data.stats}
/>
<AgentManagement reachable={true} hostInfo={data.stats} />
) : (
<div>
<h1>Local Agent Coming Soon</h1>
</div>
<div>
<h1>Local Agent Coming Soon</h1>
</div>
)}
</div>
)
);
}

View File

@ -1,11 +1,11 @@
import { Building2, House, Key } from 'lucide-react';
import Card from '~/components/Card';
import Link from '~/components/Link';
import { HeadplaneConfig } from '~/utils/state';
import type { AppContext } from '~server/context/app';
import CreateUser from '../dialogs/create-user';
interface Props {
oidc?: NonNullable<HeadplaneConfig['oidc']>;
oidc?: NonNullable<AppContext['context']['oidc']>;
}
export default function ManageBanner({ oidc }: Props) {

View File

@ -14,14 +14,22 @@ import cn from '~/utils/cn';
import { pull } from '~/utils/headscale';
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 DeleteUser from './dialogs/delete-user';
import RenameUser from './dialogs/rename-user';
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'));
if (!context) {
throw noContext();
}
const [machines, apiUsers] = await Promise.all([
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),
}));
const context = hp_getConfig();
const ctx = context.context;
const { mode, config } = hs_getConfig();
let magic: string | undefined;
@ -44,7 +52,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
}
return {
oidc: context.oidc,
oidc: ctx.oidc,
magic,
users,
};

View File

@ -1,8 +1,8 @@
import { constants, access, readFile, writeFile } from 'node:fs/promises';
import { Document, parseDocument } from 'yaml';
import { HeadplaneConfig } from '~/utils/context/parser';
import log from '~/utils/log';
import mutex from '~/utils/mutex';
import type { HeadplaneConfig } from '~server/context/parser';
import { HeadscaleConfig, validateConfig } from './parser';
let runtimeYaml: Document | undefined = undefined;
@ -13,7 +13,7 @@ let runtimeStrict = true;
const runtimeLock = mutex();
type ConfigModes =
export type ConfigModes =
| {
mode: 'rw' | 'ro';
config: HeadscaleConfig;

View File

@ -1,6 +1,7 @@
import log from '~/utils/log';
import { hp_getConfig } from '~/utils/state';
import log, { noContext } from '~/utils/log';
import { AppContext } from '~server/context/app';
type Context = AppContext['context'];
export class HeadscaleError extends Error {
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() {
const context = hp_getConfig();
if (!context) {
throw noContext();
}
const prefix = context.headscale.url;
log.debug('APIC', 'GET /health');
@ -37,11 +50,14 @@ export async function healthcheck() {
}
export async function pull<T>(url: string, key: string) {
if (!context) {
throw noContext();
}
if (!key || key === 'undefined' || key.length === 0) {
throw new Error('Missing API key, could this be a cookie setting issue?');
}
const context = hp_getConfig();
const prefix = context.headscale.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) {
if (!context) {
throw noContext();
}
if (!key || key === 'undefined' || key.length === 0) {
throw new Error('Missing API key, could this be a cookie setting issue?');
}
const context = hp_getConfig();
const prefix = context.headscale.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) {
if (!context) {
throw noContext();
}
if (!key || key === 'undefined' || key.length === 0) {
throw new Error('Missing API key, could this be a cookie setting issue?');
}
const context = hp_getConfig();
const prefix = context.headscale.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) {
if (!context) {
throw noContext();
}
if (!key || key === 'undefined' || key.length === 0) {
throw new Error('Missing API key, could this be a cookie setting issue?');
}
const context = hp_getConfig();
const prefix = context.headscale.url;
log.debug('APIC', 'DELETE %s', `${prefix}/api/${url}`);

View File

@ -3,16 +3,6 @@ export function hp_loadLogger(debug: boolean) {
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 WILL fill up quickly',
);
}
}
@ -43,4 +33,10 @@ function defaultLog(
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;

View File

@ -1,13 +1,12 @@
import * as client from 'openid-client';
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 {
const __PREFIX__: string;
}
type OidcConfig = NonNullable<HeadplaneConfig['oidc']>;
// We try our best to infer the callback URI of our Headplane instance
// By default it is always /<base_path>/oidc/callback
// (This can ALWAYS be overridden through the OidcConfig)

View File

@ -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';

View File

@ -2,8 +2,8 @@
import { readFile, writeFile } from 'node:fs/promises';
import { setTimeout as pSetTimeout } from 'node:timers/promises';
import type { LoaderFunctionArgs } from 'react-router';
import type { HostInfo } from '~/types';
import { WebSocket } from 'ws';
import type { HostInfo } from '~/types';
import log from './log';
// Essentially a HashMap which invalidates entries after a certain time.
@ -68,7 +68,7 @@ class TimedCache<K, V> {
this.writeLock = true;
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');
@ -85,10 +85,11 @@ export async function initAgentCache(defaultTTL: number, filepath: string) {
}
let agentSocket: WebSocket | undefined;
// TODO: Actually type this?
export function initAgentSocket(context: LoaderFunctionArgs['context']) {
if (!context.ws) {
return;
};
}
const client = context.ws.clients.values().next().value;
agentSocket = client;
@ -97,21 +98,24 @@ export function initAgentSocket(context: LoaderFunctionArgs['context']) {
// Check the cache and then attempt the websocket query
// If we aren't connected to an agent, then debug log and return the cache
export async function queryAgent(nodes: string[]) {
return;
return;
// biome-ignore lint: bruh
if (!cache) {
log.error('CACH', 'Cache not initialized');
return;
}
const cached: Record<string, HostInfo> = {};
await Promise.all(nodes.map(async node => {
const cachedData = await cache?.get(node);
if (cachedData) {
cached[node] = cachedData;
}
}))
await Promise.all(
nodes.map(async (node) => {
const cachedData = await cache?.get(node);
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
if (uncached.length === 0) {
@ -124,29 +128,32 @@ export async function queryAgent(nodes: string[]) {
return cached;
}
agentSocket.send(JSON.stringify({ NodeIDs: uncached }));
const returnData = await new Promise<Record<string, HostInfo> | void>((resolve, reject) => {
const timeout = setTimeout(() => {
agentSocket?.removeAllListeners('message');
resolve();
}, 3000);
agentSocket?.on('message', async (message: string) => {
const data = JSON.parse(message.toString());
if (Object.keys(data).length === 0) {
agentSocket?.send(JSON.stringify({ NodeIDs: uncached }));
// biome-ignore lint: bruh
const returnData = await new Promise<Record<string, HostInfo> | void>(
(resolve, reject) => {
const timeout = setTimeout(() => {
agentSocket?.removeAllListeners('message');
resolve();
}
}, 3000);
agentSocket?.removeAllListeners('message');
resolve(data);
});
});
if (returnData) {
for await (const [node, info] of Object.entries(returnData)) {
await cache.set(node, info);
}
}
agentSocket?.on('message', async (message: string) => {
const data = JSON.parse(message.toString());
if (Object.keys(data).length === 0) {
resolve();
}
agentSocket?.removeAllListeners('message');
resolve(data);
});
},
);
// if (returnData) {
// for await (const [node, info] of Object.entries(returnData)) {
// await cache?.set(node, info);
// }
// }
return returnData ? { ...cached, ...returnData } : cached;
}

12
server/context/app.ts Normal file
View File

@ -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(),
};
}

View File

@ -1,11 +1,9 @@
import { constants, access, readFile } from 'node:fs/promises';
import { type } from 'arktype';
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 { 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';
const envBool = type('string | undefined').pipe((v) => {
@ -67,7 +65,7 @@ export async function hp_loadConfig() {
}
// Load our debug based logger before ANYTHING
hp_loadLogger(envs.HEADPLANE_DEBUG_LOG);
hpServer_loadLogger(envs.HEADPLANE_DEBUG_LOG);
if (envs.HEADPLANE_CONFIG_PATH) {
path = envs.HEADPLANE_CONFIG_PATH;
@ -104,17 +102,11 @@ export async function hp_loadConfig() {
process.exit(1);
}
if (config.headscale.config_path) {
await hs_loadConfig(config);
}
if (config.oidc?.strict_validation) {
testOidc(config.oidc);
}
runtimeConfig = config;
initSessionManager(config.server.cookie_secret, config.server.cookie_secure);
runtimeLock.release();
}

View File

@ -1,9 +1,7 @@
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;
const stringToBool = type('string | boolean').pipe((v) => Boolean(v));
const serverConfig = type({
host: 'string.ip',

View File

@ -1,7 +1,7 @@
import { createRequestHandler } from 'react-router'
import { createRequestHandler } from 'react-router';
export default createRequestHandler(
// @ts-expect-error: React Router Vite plugin
() => import('virtual:react-router/server-build'),
'development'
'development',
);

View File

@ -1,23 +1,23 @@
import log from '~server/log';
import { createServer, type ViteDevServer } from 'vite';
import { type createRequestHandler } from 'react-router';
import { type ViteDevServer, createServer } from 'vite';
import log from '~server/utils/log';
// TODO: Remove env.NODE_ENV
let server: ViteDevServer | undefined;
export async function loadDevtools() {
log.info('DEVX', 'Starting Vite Development server')
log.info('DEVX', 'Starting Vite Development server');
process.env.NODE_ENV = 'development';
// This is loading the ROOT vite.config.ts
server = await createServer({
server: {
middlewareMode: true,
}
},
});
// 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
// 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 {
server,
handler: handler.default,
@ -26,11 +26,11 @@ export async function loadDevtools() {
export async function stacksafeTry(
devtools: {
server: ViteDevServer,
handler: any, // import() is dynamic
server: ViteDevServer;
handler: (req: Request, context: unknown) => Promise<Response>;
},
req: Request,
context: unknown
context: unknown,
) {
try {
const result = await devtools.handler(req, context);
@ -38,7 +38,6 @@ export async function stacksafeTry(
} catch (error) {
log.error('DEVX', 'Error in request handler', error);
if (typeof error === 'object' && error instanceof Error) {
console.log('got error');
devtools.server.ssrFixStacktrace(error);
}

View File

@ -1,8 +1,9 @@
// import { initWebsocket } from '~server/ws';
import { constants, access } from 'node:fs/promises';
import { createServer } from 'node:http';
import { hp_getConfig, hp_loadConfig } from '~server/context/loader';
import { listener } from '~server/listener';
import { initWebsocket } from '~server/ws';
import { access, constants } from 'node:fs/promises';
import log from '~server/log';
import log from '~server/utils/log';
log.info('SRVX', 'Running Node.js %s', process.versions.node);
@ -15,21 +16,26 @@ try {
process.exit(1);
}
await hp_loadConfig();
const server = createServer(listener);
const port = process.env.PORT || 3000;
const host = process.env.HOST || '0.0.0.0';
const ws = initWebsocket();
if (ws) {
server.on('upgrade', (req, socket, head) => {
ws.handleUpgrade(req, socket, head, (ws) => {
ws.emit('connection', ws, req);
});
});
}
// const ws = initWebsocket();
// if (ws) {
// server.on('upgrade', (req, socket, head) => {
// ws.handleUpgrade(req, socket, head, (ws) => {
// ws.emit('connection', ws, req);
// });
// });
// }
server.listen(Number(port), host, () => {
log.info('SRVX', 'Running on %s:%s', host, port);
const context = hp_getConfig();
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) {
@ -41,5 +47,3 @@ if (import.meta.hot) {
server.close();
});
}
// export const app = listener;

View File

@ -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 { type RequestListener } from 'node:http';
import { join, resolve } from 'node:path';
import {
createReadableStreamFromReadable,
writeReadableStreamToWritable,
} from '@react-router/node';
import mime from 'mime/lite'
import { loadDevtools, stacksafeTry } from '~server/dev';
import { appContext } from '~server/ws';
import mime from 'mime/lite';
import appContext from '~server/context/app';
import { loadDevtools, stacksafeTry } from '~server/dev/hot-server';
import prodBuild from '~server/prod-handler';
declare global {
@ -19,13 +15,9 @@ declare global {
const __hp_prefix: string;
}
const devtools = import.meta.env.DEV
? await loadDevtools()
: undefined;
const devtools = import.meta.env.DEV ? await loadDevtools() : undefined;
const prodHandler = import.meta.env.PROD
? await prodBuild()
: undefined;
const prodHandler = import.meta.env.PROD ? await prodBuild() : undefined;
const buildPath = process.env.BUILD_PATH ?? './build';
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
...(req.method !== 'GET' && req.method !== 'HEAD'
? {
body: createReadableStreamFromReadable(req),
duplex: 'half',
} : {}),
body: createReadableStreamFromReadable(req),
duplex: 'half',
}
: {}),
});
const response = devtools
? await stacksafeTry(devtools, frameworkReq, appContext())
: await prodHandler?.(frameworkReq, appContext());
if (!response) {
res.writeHead(404);
res.end();
return;
}
res.statusCode = response.status;
res.statusMessage = response.statusText;
@ -134,4 +133,4 @@ export const listener: RequestListener = async (req, res) => {
}
res.end();
}
};

View File

@ -1,9 +1,9 @@
import { createRequestHandler } from 'react-router'
import { access, constants } from 'node:fs/promises';
import { constants, access } from 'node:fs/promises';
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 server = resolve(join(buildPath, 'server'));
@ -12,7 +12,10 @@ export default async function() {
log.info('SRVX', 'Using build directory %s', resolve(buildPath));
} catch (error) {
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);
process.exit(1);
}

View File

@ -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[]) => {
defaultLog('INFO', category, message, ...args);
},
@ -11,11 +29,7 @@ export default {
defaultLog('ERRO', category, message, ...args);
},
debug: (category: string, message: string, ...args: unknown[]) => {
if (process.env.DEBUG === 'true') {
defaultLog('DEBG', category, message, ...args);
}
},
debug: (category: string, message: string, ...args: unknown[]) => {},
};
function defaultLog(
@ -27,3 +41,5 @@ function defaultLog(
const date = new Date().toISOString();
console.log(`${date} (${level}) [${category}] ${message}`, ...args);
}
export default log;

32
server/utils/mutex.ts Normal file
View File

@ -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);
}

View File

@ -1,5 +1,5 @@
import WebSocket, { WebSocketServer } from 'ws'
import log from '~server/log'
import WebSocket, { WebSocketServer } from 'ws';
import log from '~server/utils/log';
const server = new WebSocketServer({ noServer: true });
export function initWebsocket() {
@ -13,6 +13,7 @@ export function initWebsocket() {
log.info('CACH', 'Initializing agent WebSocket');
server.on('connection', (ws, req) => {
// biome-ignore lint: this file is not USED
const auth = req.headers['authorization'];
if (auth !== `Bearer ${key}`) {
log.warn('CACH', 'Invalid agent WebSocket connection');
@ -20,7 +21,6 @@ export function initWebsocket() {
return;
}
const nodeID = req.headers['x-headplane-ts-node-id'];
if (!nodeID) {
log.warn('CACH', 'Invalid agent WebSocket connection');
@ -46,7 +46,7 @@ export function initWebsocket() {
log.error('CACH', 'Closing agent WebSocket connection');
log.error('CACH', 'Agent WebSocket error: %s', error);
ws.close(1011, 'ERR_INTERNAL_ERROR');
})
});
});
return server;
@ -56,5 +56,5 @@ export function appContext() {
return {
ws: server,
wsAuthKey: process.env.LOCAL_AGENT_AUTHKEY,
}
};
}