Files
collabvm-1.2.ts/cvmts/src/index.ts

97 lines
2.6 KiB
TypeScript
Raw Normal View History

2023-01-31 22:00:30 -05:00
import * as toml from 'toml';
import IConfig from './IConfig.js';
2024-04-24 03:50:17 -04:00
import * as fs from 'fs';
import CollabVMServer from './CollabVMServer.js';
import { QemuVM, QemuVmDefinition } from '@cvmts/qemu';
import * as Shared from '@cvmts/shared';
import AuthManager from './AuthManager.js';
import WSServer from './WebSocket/WSServer.js';
import { User } from './User.js';
import TCPServer from './TCP/TCPServer.js';
2024-06-11 13:46:24 -04:00
import VM from './VM.js';
import VNCVM from './VNCVM/VNCVM.js';
2023-02-24 22:54:28 -05:00
2024-04-24 03:50:17 -04:00
let logger = new Shared.Logger('CVMTS.Init');
2024-04-24 03:50:17 -04:00
logger.Info('CollabVM Server starting up');
2023-01-31 22:00:30 -05:00
// Parse the config file
2024-04-24 03:50:17 -04:00
let Config: IConfig;
2023-01-31 22:00:30 -05:00
2024-04-24 03:50:17 -04:00
if (!fs.existsSync('config.toml')) {
logger.Error('Fatal error: Config.toml not found. Please copy config.example.toml and fill out fields');
process.exit(1);
2023-01-31 22:00:30 -05:00
}
try {
2024-04-24 03:50:17 -04:00
var configRaw = fs.readFileSync('config.toml').toString();
Config = toml.parse(configRaw);
2023-01-31 22:00:30 -05:00
} catch (e) {
2024-04-24 03:50:17 -04:00
logger.Error('Fatal error: Failed to read or parse the config file: {0}', (e as Error).message);
process.exit(1);
2023-01-31 22:00:30 -05:00
}
2024-06-11 13:46:24 -04:00
let exiting = false;
let VM : VM;
async function stop() {
if (exiting) return;
exiting = true;
await VM.Stop();
process.exit(0);
}
2024-04-24 03:50:17 -04:00
2024-06-11 13:46:24 -04:00
async function start() {
2024-04-24 03:50:17 -04:00
// Init the auth manager if enabled
let auth = Config.auth.enabled ? new AuthManager(Config.auth.apiEndpoint, Config.auth.secretKey) : null;
2024-06-11 13:46:24 -04:00
switch (Config.vm.type) {
case "qemu": {
// Print a warning if qmpSockDir is set
// and the host OS is Windows, as this
// configuration will very likely not work.
if (process.platform === 'win32' && Config.qemu.qmpSockDir !== null) {
logger.Warning("You appear to have the option 'qmpSockDir' enabled in the config.");
logger.Warning('This is not supported on Windows, and you will likely run into issues.');
logger.Warning('To remove this warning, use the qmpHost and qmpPort options instead.');
}
2024-04-24 03:50:17 -04:00
2024-06-11 13:46:24 -04:00
// Fire up the VM
let def: QemuVmDefinition = {
id: Config.collabvm.node,
command: Config.qemu.qemuArgs
};
2024-04-24 03:50:17 -04:00
2024-06-11 13:46:24 -04:00
VM = new QemuVM(def);
break;
}
case "vncvm": {
VM = new VNCVM(Config.vncvm);
break;
}
default: {
logger.Error('Invalid VM type in config: {0}', Config.vm.type);
process.exit(1);
return;
}
}
process.on('SIGINT', async () => await stop());
process.on('SIGTERM', async () => await stop());
2024-04-24 03:50:17 -04:00
2024-06-11 13:46:24 -04:00
await VM.Start();
// Start up the server
var CVM = new CollabVMServer(Config, VM, auth);
var WS = new WSServer(Config);
WS.on('connect', (client: User) => CVM.addUser(client));
WS.start();
if (Config.tcp.enabled) {
var TCP = new TCPServer(Config);
TCP.on('connect', (client: User) => CVM.addUser(client));
TCP.start();
}
}
2024-04-24 03:50:17 -04:00
start();