headplane/app/utils/headscale.ts

150 lines
3.9 KiB
TypeScript
Raw Normal View History

import { request } from 'undici';
import { hp_getConfig, hp_getSingleton } from '~server/context/global';
import log from '~server/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';
}
}
export async function healthcheck() {
log.debug('APIC', 'GET /health');
const health = new URL('health', hp_getConfig().headscale.url);
const response = await request(health.toString(), {
dispatcher: hp_getSingleton('api_agent'),
headers: {
2025-02-13 12:35:12 -05:00
Accept: 'application/json',
},
});
// Intentionally not catching
return response.statusCode === 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 = hp_getConfig().headscale.url;
2024-12-31 10:30:14 +05:30
log.debug('APIC', 'GET %s', `${prefix}/api/${url}`);
const response = await request(`${prefix}/api/${url}`, {
dispatcher: hp_getSingleton('api_agent'),
2024-03-29 01:53:32 -04:00
headers: {
Authorization: `Bearer ${key}`,
},
2024-12-31 10:30:14 +05:30
});
2024-03-25 17:51:11 -04:00
if (response.statusCode >= 400) {
2024-12-31 10:30:14 +05:30
log.debug(
'APIC',
'GET %s failed with status %d',
`${prefix}/api/${url}`,
response.statusCode,
2024-12-31 10:30:14 +05:30
);
throw new HeadscaleError(await response.body.text(), response.statusCode);
2024-03-25 17:51:11 -04:00
}
2024-03-29 01:53:32 -04:00
return response.body.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 = hp_getConfig().headscale.url;
2024-12-31 10:30:14 +05:30
log.debug('APIC', 'POST %s', `${prefix}/api/${url}`);
const response = await request(`${prefix}/api/${url}`, {
dispatcher: hp_getSingleton('api_agent'),
2024-03-29 01:53:32 -04:00
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
if (response.statusCode >= 400) {
2024-12-31 10:30:14 +05:30
log.debug(
'APIC',
'POST %s failed with status %d',
`${prefix}/api/${url}`,
response.statusCode,
2024-12-31 10:30:14 +05:30
);
throw new HeadscaleError(await response.body.text(), response.statusCode);
2024-03-25 17:51:11 -04:00
}
2024-03-29 01:53:32 -04:00
return response.body.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 = hp_getConfig().headscale.url;
2024-12-31 10:30:14 +05:30
log.debug('APIC', 'PUT %s', `${prefix}/api/${url}`);
const response = await request(`${prefix}/api/${url}`, {
dispatcher: hp_getSingleton('api_agent'),
method: 'PUT',
body: body ? JSON.stringify(body) : undefined,
headers: {
Authorization: `Bearer ${key}`,
},
2024-12-31 10:30:14 +05:30
});
if (response.statusCode >= 400) {
2024-12-31 10:30:14 +05:30
log.debug(
'APIC',
'PUT %s failed with status %d',
`${prefix}/api/${url}`,
response.statusCode,
2024-12-31 10:30:14 +05:30
);
throw new HeadscaleError(await response.body.text(), response.statusCode);
}
return response.body.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 = hp_getConfig().headscale.url;
2024-12-31 10:30:14 +05:30
log.debug('APIC', 'DELETE %s', `${prefix}/api/${url}`);
const response = await request(`${prefix}/api/${url}`, {
dispatcher: hp_getSingleton('api_agent'),
2024-03-30 18:56:21 -04:00
method: 'DELETE',
headers: {
Authorization: `Bearer ${key}`,
},
2024-12-31 10:30:14 +05:30
});
2024-03-30 18:56:21 -04:00
if (response.statusCode >= 400) {
2024-12-31 10:30:14 +05:30
log.debug(
'APIC',
'DELETE %s failed with status %d',
`${prefix}/api/${url}`,
response.statusCode,
2024-12-31 10:30:14 +05:30
);
throw new HeadscaleError(await response.body.text(), response.statusCode);
2024-03-30 18:56:21 -04:00
}
return response.body.json() as Promise<T>;
2024-03-30 18:56:21 -04:00
}