headplane/app/utils/headscale.ts

154 lines
3.6 KiB
TypeScript
Raw Normal View History

2025-02-19 18:09:42 -05:00
import log, { noContext } from '~/utils/log';
export class HeadscaleError extends Error {
2024-12-31 10:30:14 +05:30
status: number;
constructor(message: string, status: number) {
2024-12-31 10:30:14 +05:30
super(message);
this.name = 'HeadscaleError';
this.status = status;
}
}
export class FatalError extends Error {
2024-03-29 01:53:32 -04:00
constructor() {
2024-12-31 10:30:14 +05:30
super(
'The Headscale server is not accessible or the supplied API key is invalid',
);
this.name = 'FatalError';
}
}
interface HeadscaleContext {
url: string;
2025-02-19 18:09:42 -05:00
}
declare const global: typeof globalThis & { __hs_context: HeadscaleContext };
export async function healthcheck() {
const prefix = __hs_context.url;
log.debug('APIC', 'GET /health');
const health = new URL('health', prefix);
const response = await fetch(health.toString(), {
headers: {
2025-02-13 12:35:12 -05:00
Accept: 'application/json',
},
});
// Intentionally not catching
return response.status === 200;
}
2024-03-25 17:51:11 -04:00
export async function pull<T>(url: string, key: string) {
if (!key || key === 'undefined' || key.length === 0) {
2024-12-31 10:30:14 +05:30
throw new Error('Missing API key, could this be a cookie setting issue?');
}
const prefix = __hs_context.url;
2024-12-31 10:30:14 +05:30
log.debug('APIC', 'GET %s', `${prefix}/api/${url}`);
2024-03-29 01:53:32 -04:00
const response = await fetch(`${prefix}/api/${url}`, {
headers: {
Authorization: `Bearer ${key}`,
},
2024-12-31 10:30:14 +05:30
});
2024-03-25 17:51:11 -04:00
2024-03-29 01:53:32 -04:00
if (!response.ok) {
2024-12-31 10:30:14 +05:30
log.debug(
'APIC',
'GET %s failed with status %d',
`${prefix}/api/${url}`,
response.status,
);
throw new HeadscaleError(await response.text(), response.status);
2024-03-25 17:51:11 -04:00
}
2024-03-29 01:53:32 -04:00
2024-12-31 10:30:14 +05:30
return response.json() as Promise<T>;
2024-03-25 17:51:11 -04:00
}
export async function post<T>(url: string, key: string, body?: unknown) {
if (!key || key === 'undefined' || key.length === 0) {
2024-12-31 10:30:14 +05:30
throw new Error('Missing API key, could this be a cookie setting issue?');
}
const prefix = __hs_context.url;
2024-12-31 10:30:14 +05:30
log.debug('APIC', 'POST %s', `${prefix}/api/${url}`);
2024-03-29 01:53:32 -04:00
const response = await fetch(`${prefix}/api/${url}`, {
method: 'POST',
body: body ? JSON.stringify(body) : undefined,
headers: {
Authorization: `Bearer ${key}`,
},
2024-12-31 10:30:14 +05:30
});
2024-03-25 17:51:11 -04:00
2024-03-29 01:53:32 -04:00
if (!response.ok) {
2024-12-31 10:30:14 +05:30
log.debug(
'APIC',
'POST %s failed with status %d',
`${prefix}/api/${url}`,
response.status,
);
throw new HeadscaleError(await response.text(), response.status);
2024-03-25 17:51:11 -04:00
}
2024-03-29 01:53:32 -04:00
2024-12-31 10:30:14 +05:30
return response.json() as Promise<T>;
2024-03-25 17:51:11 -04:00
}
export async function put<T>(url: string, key: string, body?: unknown) {
if (!key || key === 'undefined' || key.length === 0) {
2024-12-31 10:30:14 +05:30
throw new Error('Missing API key, could this be a cookie setting issue?');
}
const prefix = __hs_context.url;
2024-12-31 10:30:14 +05:30
log.debug('APIC', 'PUT %s', `${prefix}/api/${url}`);
const response = await fetch(`${prefix}/api/${url}`, {
method: 'PUT',
body: body ? JSON.stringify(body) : undefined,
headers: {
Authorization: `Bearer ${key}`,
},
2024-12-31 10:30:14 +05:30
});
if (!response.ok) {
2024-12-31 10:30:14 +05:30
log.debug(
'APIC',
'PUT %s failed with status %d',
`${prefix}/api/${url}`,
response.status,
);
throw new HeadscaleError(await response.text(), response.status);
}
2024-12-31 10:30:14 +05:30
return response.json() as Promise<T>;
}
2024-03-30 18:56:21 -04:00
export async function del<T>(url: string, key: string) {
if (!key || key === 'undefined' || key.length === 0) {
2024-12-31 10:30:14 +05:30
throw new Error('Missing API key, could this be a cookie setting issue?');
}
const prefix = __hs_context.url;
2024-12-31 10:30:14 +05:30
log.debug('APIC', 'DELETE %s', `${prefix}/api/${url}`);
2024-03-30 18:56:21 -04:00
const response = await fetch(`${prefix}/api/${url}`, {
method: 'DELETE',
headers: {
Authorization: `Bearer ${key}`,
},
2024-12-31 10:30:14 +05:30
});
2024-03-30 18:56:21 -04:00
if (!response.ok) {
2024-12-31 10:30:14 +05:30
log.debug(
'APIC',
'DELETE %s failed with status %d',
`${prefix}/api/${url}`,
response.status,
);
throw new HeadscaleError(await response.text(), response.status);
2024-03-30 18:56:21 -04:00
}
2024-12-31 10:30:14 +05:30
return response.json() as Promise<T>;
2024-03-30 18:56:21 -04:00
}