65 lines
2.5 KiB
TypeScript
65 lines
2.5 KiB
TypeScript
import type { InstallContext } from "../types/context";
|
|
import { runMutating } from "../lib/process";
|
|
import { ensureDiagnosticsStorageRoot } from "../lib/diagnosticsStorage";
|
|
|
|
async function userExists(user: string): Promise<boolean> {
|
|
try {
|
|
await runMutating`id -u ${user} >/dev/null 2>&1`;
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
async function getUserField(user: string, field: number): Promise<string> {
|
|
return (await runMutating`getent passwd ${user} | cut -d: -f${field}`).trim();
|
|
}
|
|
|
|
async function getPrimaryGroup(user: string): Promise<string> {
|
|
return (await runMutating`id -gn ${user}`).trim();
|
|
}
|
|
|
|
async function ensureDir(path: string, mode: string, ownerGroup: string): Promise<void> {
|
|
await runMutating`install -d -m ${mode} -o ${ownerGroup.split(":")[0]} -g ${ownerGroup.split(":")[1]} ${path}`;
|
|
}
|
|
|
|
async function makeUser(user: string, expectedHome: string): Promise<void> {
|
|
await runMutating`useradd --system --home ${expectedHome} --shell /usr/sbin/nologin ${user}`;
|
|
}
|
|
|
|
async function ensureRuntimeIdentity(user: string, expectedHome: string): Promise<void> {
|
|
if (!(await userExists(user))) {
|
|
await makeUser(user, expectedHome);
|
|
return;
|
|
}
|
|
|
|
const shell = await getUserField(user, 7);
|
|
const home = await getUserField(user, 6);
|
|
const group = await getPrimaryGroup(user);
|
|
|
|
if (shell !== "/usr/sbin/nologin" && shell !== "/bin/false") {
|
|
throw new Error(`existing user '${user}' has unsupported shell: ${shell}`);
|
|
}
|
|
if (group !== user) {
|
|
throw new Error(`existing user '${user}' must have primary group '${user}', got: ${group}`);
|
|
}
|
|
if (home !== expectedHome) {
|
|
throw new Error(`existing user '${user}' has unexpected home: ${home} (expected ${expectedHome})`);
|
|
}
|
|
}
|
|
|
|
export async function prepareFilesystem(context: InstallContext): Promise<void> {
|
|
await ensureRuntimeIdentity("hysteria", "/var/lib/hysteria");
|
|
await ensureRuntimeIdentity("hy2xs-admin", context.config.dataDir);
|
|
await ensureDir("/etc/hy2xs", "0700", "root:root");
|
|
await ensureDir("/etc/hysteria", "0755", "root:root");
|
|
await ensureDir("/var/lib/hysteria", "0750", "hysteria:hysteria");
|
|
await ensureDir(context.config.dataDir, "0750", "hy2xs-admin:hy2xs-admin");
|
|
await ensureDir(context.config.logDir, "0750", "hy2xs-admin:hy2xs-admin");
|
|
await ensureDir(context.config.installDir, "0755", "root:root");
|
|
await ensureDir("/usr/local/lib/hy2xs", "0755", "root:root");
|
|
await ensureDir("/etc/nftables.d", "0755", "root:root");
|
|
await ensureDiagnosticsStorageRoot();
|
|
await runMutating`chmod -R go-w ${context.config.installDir}`;
|
|
}
|