import { existsSync as fsExistsSync, promises as fs, readFileSync as fsReadFileSync, } from "node:fs"; import path from "node:path"; import type { Instance } from "../index.ts"; import System from "../system.ts"; export enum WorkspacesFeatureFlags { SlashCommands = "slash_commands", ShootYourselfInTheFoot = "shoot_yourself_in_the_foot", AllowUserSignups = "allow_user_signups", ExperimentalTerminalGui = "experimental_terminal_gui", DisplayProfilesAtLogon = "display_profiles_at_logon" } export const FEATURE_FLAG_DESCRIPTIONS = { [WorkspacesFeatureFlags.SlashCommands]: "Enable the ability to use slash commands in the instance's console", [WorkspacesFeatureFlags.ShootYourselfInTheFoot]: "Allow administrators to alter settings / configuration options which may cause the instance to malfunction. (Only enable this if you are sure you know what you are doing!)", [WorkspacesFeatureFlags.AllowUserSignups]: "Allow new users to create accounts from the instance user login page", }; export default class ConfigurationSystem extends System { isDevMode: boolean = true; databases: { postgres: { user: string; password: string; host: string; port: number; database: string; }; } = { postgres: { user: "postgres", password: "postgres", host: "localhost", port: 5432, database: "onlineworkspace_workspace", }, }; proxy: { secure: boolean; hostname: string } = { secure: true, hostname: "localhost", }; enabledFeatures: (WorkspacesFeatureFlags | string)[] = [ WorkspacesFeatureFlags.SlashCommands, ]; signupRequirements: { email: boolean; twoFactorAuthentication: boolean; passwordMinimumLength?: number; passwordContains?: { minimumUppercase?: number; minimumLowercase?: number; minimumNumbers?: number; minimumSymbols?: number; }; } = { email: false, twoFactorAuthentication: false, passwordMinimumLength: 5, passwordContains: { minimumLowercase: 1, minimumNumbers: 1, minimumSymbols: 1, minimumUppercase: 1, }, }; displayName: string; mailServer: { host: string; port: number; secure: boolean; auth: { user: string; pass: string; }; }; termsOfUse: { message: string; lastUpdated: number }; defaultQuickShortcuts: string[] = [ "uk.ewsgit.dashboard", "uk.ewsgit.store", "uk.ewsgit.settings", "uk.ewsgit.photos", "uk.ewsgit.files", ]; defaultApplications: { id: string; uri: string }[] = [ { id: "uk.ewsgit.dashboard", uri: "local:uk.ewsgit.dashboard" }, { id: "uk.ewsgit.store", uri: "local:uk.ewsgit.store" }, { id: "uk.ewsgit.settings", uri: "local:uk.ewsgit.settings" }, { id: "uk.ewsgit.photos", uri: "local:uk.ewsgit.photos" }, { id: "uk.ewsgit.files", uri: "local:uk.ewsgit.files" }, ]; userDefault: { homeDirectories: string[]; quotaSize: number; displayNameFormat: string; } = { homeDirectories: ["Documents", "Photos", "Videos", "Projects"], quotaSize: 1024 * 1024 * 10, displayNameFormat: "New User %num%", }; caddyfile: string | undefined = "../Caddyfile"; apiPort: number = 3563; constructor(instance: Instance) { super("configuration", instance); if (process.env.POSTGRES_DATABASE_HOST) { this.databases.postgres.host = process.env.POSTGRES_DATABASE_HOST; } this.displayName = "Workspace"; this.mailServer = { host: "smtp.example.com", port: 587, secure: true, auth: { user: "user", pass: "password", }, }; this.termsOfUse = { message: `1. Acceptance of Terms - By logging in, you agree to these rules. If you do not agree, please do not use the service. 2. Account Security - You are the gatekeeper of your account. Keep your password private, as you are responsible for all activity that happens under your login. 3. Content Ownership - What is yours remains yours. We claim no ownership over the files, photos, or data you upload to this instance. 4. Acceptable Use - Do not use this space for anything illegal, malicious, or harmful. This includes uploading malware or attempting to disrupt the service for others. 5. Privacy and Access - We value your privacy. We will not access your stored data unless it is strictly necessary for technical support or required by legal authorities. 6. Storage and Maintenance - While we strive for 100% uptime, this service is provided "as is." We may occasionally perform maintenance that results in temporary downtime. 7. Personal Responsibility - Hardware and software can fail. You agree to maintain your own external backups of any mission-critical data. We are not liable for data loss. 8. Termination - We reserve the right to suspend or close accounts that violate these terms or compromise the security of the server. 9. Policy Updates - These terms may change. If we make significant updates, we will post a notification within the app or send an email.`, lastUpdated: Date.now(), }; if ( fsExistsSync( path.join( this.instance.sys.filesystem.AUTO_INSTALL_PATH, "configuration.json", ), ) ) { this.log.info( "Auto-install configuration detected. Loading configuration from auto-install.", ); const autoInstallConfig = JSON.parse( fsReadFileSync( path.join( this.instance.sys.filesystem.AUTO_INSTALL_PATH, "configuration.json", ), ).toString(), ); for (const key of Object.keys(autoInstallConfig)) { if (key in this) { // @ts-ignore this[key] = autoInstallConfig[key]; } } } } hasFeature(feature: WorkspacesFeatureFlags | string): boolean { return !!this.enabledFeatures.find((f) => f === feature); } async enableFeature(feature: WorkspacesFeatureFlags | string) { if (!this.enabledFeatures.includes(feature)) { this.enabledFeatures.push(feature); } await this.saveConfiguration(); return true; } async disableFeature(feature: WorkspacesFeatureFlags | string) { this.enabledFeatures = this.enabledFeatures.filter((feat) => feat !== feature ); await this.saveConfiguration(); return true; } async saveConfiguration(): Promise { const CONFIGURATION_FILE_PATH = path.join( this.instance.sys.filesystem.FS_ROOT, "configuration.json", ); const allowedProperties = (Object.keys(this) as (keyof ConfigurationSystem)[]).filter( (property) => { return typeof this[property] !== "function" && property !== "instance" && property !== "id" && property !== "log"; }, ); const configurationFileContents: Record = {}; for (const propertyKey of allowedProperties) { console.log(propertyKey); configurationFileContents[propertyKey] = this[propertyKey]; } await fs.writeFile( CONFIGURATION_FILE_PATH, JSON.stringify(configurationFileContents, null, 2), ); return true; } override async startup(): Promise { const CONFIGURATION_FILE_PATH = path.join( this.instance.sys.filesystem.FS_ROOT, "configuration.json", ); if (!fsExistsSync(CONFIGURATION_FILE_PATH)) { await this.saveConfiguration(); } const configurationFile = JSON.parse( (await fs.readFile(CONFIGURATION_FILE_PATH)).toString(), ); const allowedProperties = (Object.keys(this) as (keyof ConfigurationSystem)[]).filter( (property) => { return typeof this[property] !== "function" && property !== "instance" && property !== "id" && property !== "log"; }, ); for (const propertyKey of allowedProperties) { if (propertyKey in configurationFile) { // @ts-expect-error We can ignore this as the properties which are read-only are already removed from the allowedProperties by this stage. this[propertyKey] = configurationFile[propertyKey]; } } for ( const feature of Object.keys( WorkspacesFeatureFlags, ) as (keyof typeof WorkspacesFeatureFlags)[] ) { this.log.info( `Feature ${feature} -> ${ this.enabledFeatures.includes(WorkspacesFeatureFlags[feature]) }`, ); } return true; } }