e444936c04
Co-authored-by: kaaax0815 <32197462+kaaax0815@users.noreply.github.com>
93 lines
2.7 KiB
TypeScript
93 lines
2.7 KiB
TypeScript
import debug from 'debug';
|
|
import packageJson from '@@/package.json';
|
|
|
|
export const RELEASE = 'v' + packageJson.version;
|
|
|
|
export const SERVER_DEBUG = debug('Server');
|
|
|
|
export const OLD_ENV = {
|
|
/** @deprecated Only for migration purposes */
|
|
PASSWORD: process.env.PASSWORD,
|
|
/** @deprecated Only for migration purposes */
|
|
PASSWORD_HASH: process.env.PASSWORD_HASH,
|
|
};
|
|
|
|
const detectAwg = async (): Promise<'awg' | 'wg'> => {
|
|
/** TODO: delete on next major version */
|
|
if (process.env.EXPERIMENTAL_AWG === 'true') {
|
|
const OVERRIDE_AUTO_AWG = process.env.OVERRIDE_AUTO_AWG?.toLowerCase();
|
|
|
|
if (
|
|
OVERRIDE_AUTO_AWG === ('wg' as const) ||
|
|
OVERRIDE_AUTO_AWG === ('awg' as const)
|
|
) {
|
|
return OVERRIDE_AUTO_AWG;
|
|
} else {
|
|
return await exec('modinfo amneziawg')
|
|
.then(() => 'awg' as const)
|
|
.catch(() => 'wg' as const);
|
|
}
|
|
} else return 'wg';
|
|
};
|
|
|
|
export const WG_ENV = {
|
|
/** UI is hosted on HTTP instead of HTTPS */
|
|
INSECURE: process.env.INSECURE === 'true',
|
|
/** Port the UI is listening on */
|
|
PORT: assertEnv('PORT'),
|
|
/** If IPv6 should be disabled */
|
|
DISABLE_IPV6: process.env.DISABLE_IPV6 === 'true',
|
|
WG_EXECUTABLE: await detectAwg(),
|
|
};
|
|
|
|
export const WG_INITIAL_ENV = {
|
|
ENABLED: process.env.INIT_ENABLED === 'true',
|
|
USERNAME: process.env.INIT_USERNAME,
|
|
PASSWORD: process.env.INIT_PASSWORD,
|
|
DNS: process.env.INIT_DNS?.split(',').map((x) => x.trim()),
|
|
IPV4_CIDR: process.env.INIT_IPV4_CIDR,
|
|
IPV6_CIDR: process.env.INIT_IPV6_CIDR,
|
|
ALLOWED_IPS: process.env.INIT_ALLOWED_IPS?.split(',').map((x) => x.trim()),
|
|
HOST: process.env.INIT_HOST,
|
|
PORT: process.env.INIT_PORT
|
|
? Number.parseInt(process.env.INIT_PORT, 10)
|
|
: undefined,
|
|
};
|
|
|
|
export const WG_OVERRIDE_ENV = {
|
|
/** Override the WireGuard interface port */
|
|
INTERFACE_PORT: process.env.OVERRIDE_INTERFACE_PORT
|
|
? Number.parseInt(process.env.OVERRIDE_INTERFACE_PORT, 10)
|
|
: undefined,
|
|
/** Override the network device/interface */
|
|
INTERFACE_DEVICE: process.env.OVERRIDE_INTERFACE_DEVICE,
|
|
/** Override the MTU setting */
|
|
INTERFACE_MTU: process.env.OVERRIDE_INTERFACE_MTU
|
|
? Number.parseInt(process.env.OVERRIDE_INTERFACE_MTU, 10)
|
|
: undefined,
|
|
};
|
|
|
|
function assertEnv<T extends string>(env: T) {
|
|
const val = process.env[env];
|
|
|
|
if (!val) {
|
|
throw new Error(`Missing environment variable: ${env}`);
|
|
}
|
|
|
|
return val;
|
|
}
|
|
|
|
/**
|
|
* Apply environment variable overrides to an interface object
|
|
*/
|
|
export function applyInterfaceOverrides<
|
|
T extends { port: number; device: string; mtu: number },
|
|
>(wgInterface: T): T {
|
|
return {
|
|
...wgInterface,
|
|
port: WG_OVERRIDE_ENV.INTERFACE_PORT ?? wgInterface.port,
|
|
device: WG_OVERRIDE_ENV.INTERFACE_DEVICE ?? wgInterface.device,
|
|
mtu: WG_OVERRIDE_ENV.INTERFACE_MTU ?? wgInterface.mtu,
|
|
};
|
|
}
|