2023-01-31 22:00:30 -05:00
|
|
|
import * as toml from 'toml';
|
2023-02-02 21:19:55 -05:00
|
|
|
import IConfig from './IConfig.js';
|
2024-04-24 03:50:17 -04:00
|
|
|
import * as fs from 'fs';
|
2024-05-26 23:19:55 -04:00
|
|
|
import CollabVMServer from './CollabVMServer.js';
|
2024-04-23 09:57:02 -04:00
|
|
|
|
2024-08-23 07:25:57 -04:00
|
|
|
import { QemuVmDefinition } from '@computernewb/superqemu';
|
2024-04-23 09:57:02 -04:00
|
|
|
|
2024-04-05 09:10:47 -04:00
|
|
|
import AuthManager from './AuthManager.js';
|
2024-05-26 23:19:55 -04:00
|
|
|
import WSServer from './WebSocket/WSServer.js';
|
|
|
|
|
import { User } from './User.js';
|
2024-05-27 00:06:05 -04:00
|
|
|
import TCPServer from './TCP/TCPServer.js';
|
2024-08-23 07:25:57 -04:00
|
|
|
import VM from './vm/interface.js';
|
|
|
|
|
import VNCVM from './vm/vnc/VNCVM.js';
|
2024-06-23 02:23:59 -04:00
|
|
|
import GeoIPDownloader from './GeoIPDownloader.js';
|
2024-07-16 08:29:52 -04:00
|
|
|
import pino from 'pino';
|
2024-07-31 16:34:42 -04:00
|
|
|
import { Database } from './Database.js';
|
|
|
|
|
import { BanManager } from './BanManager.js';
|
2024-08-23 07:25:57 -04:00
|
|
|
import { QemuVMShim } from './vm/qemu.js';
|
2024-08-21 07:10:58 -04:00
|
|
|
import { TheProtocolManager } from './Protocol.js';
|
|
|
|
|
import { GuacamoleProtocol } from './GuacamoleProtocol.js';
|
2023-02-24 22:54:28 -05:00
|
|
|
|
2024-07-16 08:29:52 -04:00
|
|
|
let logger = pino();
|
2024-04-23 09:57:02 -04:00
|
|
|
|
2024-07-16 08:29:52 -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')) {
|
2024-07-16 08:29:52 -04:00
|
|
|
logger.error('Fatal error: Config.toml not found. Please copy config.example.toml and fill out fields');
|
2024-04-24 03:50:17 -04:00
|
|
|
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-07-16 08:29:52 -04:00
|
|
|
logger.error('Fatal error: Failed to read or parse the config file: {0}', (e as Error).message);
|
2024-04-24 03:50:17 -04:00
|
|
|
process.exit(1);
|
2023-01-31 22:00:30 -05:00
|
|
|
}
|
|
|
|
|
|
2024-06-11 13:46:24 -04:00
|
|
|
let exiting = false;
|
2024-06-22 21:26:49 -04:00
|
|
|
let VM: VM;
|
2024-06-11 13:46:24 -04:00
|
|
|
|
|
|
|
|
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-06-23 02:23:59 -04:00
|
|
|
let geoipReader = null;
|
|
|
|
|
if (Config.geoip.enabled) {
|
|
|
|
|
let downloader = new GeoIPDownloader(Config.geoip.directory, Config.geoip.accountID, Config.geoip.licenseKey);
|
|
|
|
|
geoipReader = await downloader.getGeoIPReader();
|
|
|
|
|
}
|
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-07-31 16:34:42 -04:00
|
|
|
// Database and ban manager
|
|
|
|
|
if (Config.bans.cvmban && !Config.mysql.enabled) {
|
2024-08-04 15:50:00 -04:00
|
|
|
logger.error('MySQL must be configured to use cvmban.');
|
2024-07-31 16:34:42 -04:00
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
if (!Config.bans.cvmban && !Config.bans.bancmd) {
|
2024-08-04 15:50:00 -04:00
|
|
|
logger.warn('Neither cvmban nor ban command are configured. Bans will not function.');
|
2024-07-31 16:34:42 -04:00
|
|
|
}
|
|
|
|
|
let db = undefined;
|
|
|
|
|
if (Config.mysql.enabled) {
|
|
|
|
|
db = new Database(Config.mysql);
|
|
|
|
|
await db.init();
|
|
|
|
|
}
|
|
|
|
|
let banmgr = new BanManager(Config.bans, db);
|
2024-06-11 13:46:24 -04:00
|
|
|
switch (Config.vm.type) {
|
2024-06-22 21:26:49 -04:00
|
|
|
case 'qemu': {
|
2024-06-11 13:46:24 -04:00
|
|
|
// Fire up the VM
|
|
|
|
|
let def: QemuVmDefinition = {
|
|
|
|
|
id: Config.collabvm.node,
|
2024-06-19 18:03:10 -04:00
|
|
|
command: Config.qemu.qemuArgs,
|
2024-08-04 18:28:33 -04:00
|
|
|
snapshot: Config.qemu.snapshots,
|
|
|
|
|
forceTcp: false,
|
|
|
|
|
vncHost: '127.0.0.1',
|
|
|
|
|
vncPort: Config.qemu.vncPort,
|
2024-06-11 13:46:24 -04:00
|
|
|
};
|
2024-04-24 03:50:17 -04:00
|
|
|
|
2024-08-23 07:25:57 -04:00
|
|
|
VM = new QemuVMShim(def);
|
2024-06-11 13:46:24 -04:00
|
|
|
break;
|
|
|
|
|
}
|
2024-06-22 21:26:49 -04:00
|
|
|
case 'vncvm': {
|
2024-06-11 13:46:24 -04:00
|
|
|
VM = new VNCVM(Config.vncvm);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default: {
|
2024-07-16 08:29:52 -04:00
|
|
|
logger.error(`Invalid VM type in config: ${Config.vm.type}`);
|
2024-06-11 13:46:24 -04:00
|
|
|
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-08-21 07:10:58 -04:00
|
|
|
// Register protocol(s)
|
|
|
|
|
TheProtocolManager.registerProtocol("guacamole", () => new GuacamoleProtocol);
|
|
|
|
|
|
2024-06-11 13:46:24 -04:00
|
|
|
await VM.Start();
|
2024-05-26 23:19:55 -04:00
|
|
|
// Start up the server
|
2024-07-31 16:34:42 -04:00
|
|
|
var CVM = new CollabVMServer(Config, VM, banmgr, auth, geoipReader);
|
2024-05-26 23:19:55 -04:00
|
|
|
|
2024-07-31 16:34:42 -04:00
|
|
|
var WS = new WSServer(Config, banmgr);
|
2024-08-21 07:10:58 -04:00
|
|
|
WS.on('connect', (client: User) => CVM.connectionOpened(client));
|
2024-05-26 23:19:55 -04:00
|
|
|
WS.start();
|
2024-05-27 00:06:05 -04:00
|
|
|
|
|
|
|
|
if (Config.tcp.enabled) {
|
2024-07-31 16:34:42 -04:00
|
|
|
var TCP = new TCPServer(Config, banmgr);
|
2024-08-21 07:10:58 -04:00
|
|
|
TCP.on('connect', (client: User) => CVM.connectionOpened(client));
|
2024-05-27 00:06:05 -04:00
|
|
|
TCP.start();
|
|
|
|
|
}
|
2023-02-02 21:19:55 -05:00
|
|
|
}
|
2024-04-24 03:50:17 -04:00
|
|
|
start();
|