prettier reformat for merge (and remove jpeg-turbo Again)
This commit is contained in:
@@ -26,13 +26,11 @@ export default class AuthManager {
|
||||
});
|
||||
|
||||
// Make sure the fetch returned okay
|
||||
if(!response.ok)
|
||||
throw new Error(`Failed to query quth server: ${response.statusText}`)
|
||||
if (!response.ok) throw new Error(`Failed to query quth server: ${response.statusText}`);
|
||||
|
||||
let json = (await response.json()) as JoinResponse;
|
||||
|
||||
if (!json.success)
|
||||
throw new Error(json.error);
|
||||
if (!json.success) throw new Error(json.error);
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
@@ -23,8 +23,6 @@ const kCVMTSAssetsRoot = path.resolve(__dirname, '../../assets');
|
||||
|
||||
const kRestartTimeout = 5000;
|
||||
|
||||
|
||||
|
||||
type ChatHistory = {
|
||||
user: string;
|
||||
msg: string;
|
||||
@@ -115,14 +113,14 @@ export default class CollabVMServer {
|
||||
this.VM = vm;
|
||||
|
||||
// hack but whatever (TODO: less rickity)
|
||||
if(config.vm.type == "qemu") {
|
||||
if (config.vm.type == 'qemu') {
|
||||
(vm as QemuVM).on('statechange', (newState: VMState) => {
|
||||
if(newState == VMState.Stopped) {
|
||||
this.logger.Info("stopped ?");
|
||||
if (newState == VMState.Stopped) {
|
||||
this.logger.Info('stopped ?');
|
||||
setTimeout(async () => {
|
||||
this.logger.Info("restarting VM");
|
||||
this.logger.Info('restarting VM');
|
||||
await this.VM.Start();
|
||||
}, kRestartTimeout)
|
||||
}, kRestartTimeout);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -642,7 +640,7 @@ export default class CollabVMServer {
|
||||
}
|
||||
} catch (err) {
|
||||
// No
|
||||
this.logger.Error(`User ${user?.IP.address} ${user?.username ? `with username ${user?.username}` : ''} sent broken Guacamole: ${(err as Error)}`);
|
||||
this.logger.Error(`User ${user?.IP.address} ${user?.username ? `with username ${user?.username}` : ''} sent broken Guacamole: ${err as Error}`);
|
||||
user?.kick();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import VNCVMDef from "./VNCVM/VNCVMDef";
|
||||
import VNCVMDef from './VNCVM/VNCVMDef';
|
||||
|
||||
export default interface IConfig {
|
||||
http: {
|
||||
@@ -13,7 +13,7 @@ export default interface IConfig {
|
||||
enabled: boolean;
|
||||
host: string;
|
||||
port: number;
|
||||
}
|
||||
};
|
||||
auth: {
|
||||
enabled: boolean;
|
||||
apiEndpoint: string;
|
||||
@@ -26,7 +26,7 @@ export default interface IConfig {
|
||||
};
|
||||
};
|
||||
vm: {
|
||||
type: "qemu" | "vncvm";
|
||||
type: 'qemu' | 'vncvm';
|
||||
};
|
||||
qemu: {
|
||||
qemuArgs: string;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
export default interface NetworkClient {
|
||||
getIP() : string;
|
||||
send(msg: string) : Promise<void>;
|
||||
close() : void;
|
||||
on(event: string, listener: (...args: any[]) => void) : void;
|
||||
off(event: string, listener: (...args: any[]) => void) : void;
|
||||
isOpen() : boolean;
|
||||
}
|
||||
getIP(): string;
|
||||
send(msg: string): Promise<void>;
|
||||
close(): void;
|
||||
on(event: string, listener: (...args: any[]) => void): void;
|
||||
off(event: string, listener: (...args: any[]) => void): void;
|
||||
isOpen(): boolean;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
export default interface NetworkServer {
|
||||
start() : void;
|
||||
stop() : void;
|
||||
on(event: string, listener: (...args: any[]) => void) : void;
|
||||
off(event: string, listener: (...args: any[]) => void) : void;
|
||||
}
|
||||
start(): void;
|
||||
stop(): void;
|
||||
on(event: string, listener: (...args: any[]) => void): void;
|
||||
off(event: string, listener: (...args: any[]) => void): void;
|
||||
}
|
||||
|
||||
@@ -1,55 +1,55 @@
|
||||
import EventEmitter from "events";
|
||||
import NetworkClient from "../NetworkClient.js";
|
||||
import { Socket } from "net";
|
||||
import EventEmitter from 'events';
|
||||
import NetworkClient from '../NetworkClient.js';
|
||||
import { Socket } from 'net';
|
||||
|
||||
export default class TCPClient extends EventEmitter implements NetworkClient {
|
||||
private socket: Socket;
|
||||
private cache: string;
|
||||
|
||||
constructor(socket: Socket) {
|
||||
super();
|
||||
this.socket = socket;
|
||||
this.cache = '';
|
||||
this.socket.on('end', () => {
|
||||
this.emit('disconnect');
|
||||
})
|
||||
this.socket.on('data', (data) => {
|
||||
var msg = data.toString('utf-8');
|
||||
if (msg[msg.length - 1] === '\n') msg = msg.slice(0, -1);
|
||||
this.cache += msg;
|
||||
this.readCache();
|
||||
});
|
||||
}
|
||||
private socket: Socket;
|
||||
private cache: string;
|
||||
|
||||
private readCache() {
|
||||
for (var index = this.cache.indexOf(';'); index !== -1; index = this.cache.indexOf(';')) {
|
||||
this.emit('msg', this.cache.slice(0, index + 1));
|
||||
this.cache = this.cache.slice(index + 1);
|
||||
}
|
||||
}
|
||||
constructor(socket: Socket) {
|
||||
super();
|
||||
this.socket = socket;
|
||||
this.cache = '';
|
||||
this.socket.on('end', () => {
|
||||
this.emit('disconnect');
|
||||
});
|
||||
this.socket.on('data', (data) => {
|
||||
var msg = data.toString('utf-8');
|
||||
if (msg[msg.length - 1] === '\n') msg = msg.slice(0, -1);
|
||||
this.cache += msg;
|
||||
this.readCache();
|
||||
});
|
||||
}
|
||||
|
||||
getIP(): string {
|
||||
return this.socket.remoteAddress!;
|
||||
}
|
||||
private readCache() {
|
||||
for (var index = this.cache.indexOf(';'); index !== -1; index = this.cache.indexOf(';')) {
|
||||
this.emit('msg', this.cache.slice(0, index + 1));
|
||||
this.cache = this.cache.slice(index + 1);
|
||||
}
|
||||
}
|
||||
|
||||
send(msg: string): Promise<void> {
|
||||
return new Promise((res, rej) => {
|
||||
this.socket.write(msg, (err) => {
|
||||
if (err) {
|
||||
rej(err);
|
||||
return;
|
||||
}
|
||||
res();
|
||||
});
|
||||
});
|
||||
}
|
||||
getIP(): string {
|
||||
return this.socket.remoteAddress!;
|
||||
}
|
||||
|
||||
close(): void {
|
||||
this.emit('disconnect');
|
||||
this.socket.end();
|
||||
}
|
||||
send(msg: string): Promise<void> {
|
||||
return new Promise((res, rej) => {
|
||||
this.socket.write(msg, (err) => {
|
||||
if (err) {
|
||||
rej(err);
|
||||
return;
|
||||
}
|
||||
res();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
isOpen(): boolean {
|
||||
return this.socket.writable;
|
||||
}
|
||||
}
|
||||
close(): void {
|
||||
this.emit('disconnect');
|
||||
this.socket.end();
|
||||
}
|
||||
|
||||
isOpen(): boolean {
|
||||
return this.socket.writable;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,40 +1,40 @@
|
||||
import EventEmitter from "events";
|
||||
import NetworkServer from "../NetworkServer.js";
|
||||
import { Server, Socket } from "net";
|
||||
import IConfig from "../IConfig.js";
|
||||
import { Logger } from "@cvmts/shared";
|
||||
import TCPClient from "./TCPClient.js";
|
||||
import { IPDataManager } from "../IPData.js";
|
||||
import { User } from "../User.js";
|
||||
import EventEmitter from 'events';
|
||||
import NetworkServer from '../NetworkServer.js';
|
||||
import { Server, Socket } from 'net';
|
||||
import IConfig from '../IConfig.js';
|
||||
import { Logger } from '@cvmts/shared';
|
||||
import TCPClient from './TCPClient.js';
|
||||
import { IPDataManager } from '../IPData.js';
|
||||
import { User } from '../User.js';
|
||||
|
||||
export default class TCPServer extends EventEmitter implements NetworkServer {
|
||||
listener: Server;
|
||||
Config: IConfig;
|
||||
logger: Logger;
|
||||
clients: TCPClient[];
|
||||
listener: Server;
|
||||
Config: IConfig;
|
||||
logger: Logger;
|
||||
clients: TCPClient[];
|
||||
|
||||
constructor(config: IConfig) {
|
||||
super();
|
||||
this.logger = new Logger("CVMTS.TCPServer");
|
||||
this.Config = config;
|
||||
this.listener = new Server();
|
||||
this.clients = [];
|
||||
this.listener.on('connection', socket => this.onConnection(socket));
|
||||
}
|
||||
constructor(config: IConfig) {
|
||||
super();
|
||||
this.logger = new Logger('CVMTS.TCPServer');
|
||||
this.Config = config;
|
||||
this.listener = new Server();
|
||||
this.clients = [];
|
||||
this.listener.on('connection', (socket) => this.onConnection(socket));
|
||||
}
|
||||
|
||||
private onConnection(socket: Socket) {
|
||||
this.logger.Info(`New TCP connection from ${socket.remoteAddress}`);
|
||||
var client = new TCPClient(socket);
|
||||
this.clients.push(client);
|
||||
this.emit('connect', new User(client, IPDataManager.GetIPData(client.getIP()), this.Config));
|
||||
}
|
||||
private onConnection(socket: Socket) {
|
||||
this.logger.Info(`New TCP connection from ${socket.remoteAddress}`);
|
||||
var client = new TCPClient(socket);
|
||||
this.clients.push(client);
|
||||
this.emit('connect', new User(client, IPDataManager.GetIPData(client.getIP()), this.Config));
|
||||
}
|
||||
|
||||
start(): void {
|
||||
this.listener.listen(this.Config.tcp.port, this.Config.tcp.host, () => {
|
||||
this.logger.Info(`TCP server listening on ${this.Config.tcp.host}:${this.Config.tcp.port}`);
|
||||
})
|
||||
}
|
||||
stop(): void {
|
||||
this.listener.close();
|
||||
}
|
||||
}
|
||||
start(): void {
|
||||
this.listener.listen(this.Config.tcp.port, this.Config.tcp.host, () => {
|
||||
this.logger.Info(`TCP server listening on ${this.Config.tcp.host}:${this.Config.tcp.port}`);
|
||||
});
|
||||
}
|
||||
stop(): void {
|
||||
this.listener.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ export class User {
|
||||
clearInterval(this.msgRecieveInterval);
|
||||
this.msgRecieveInterval = setInterval(() => this.onNoMsg(), 10000);
|
||||
});
|
||||
|
||||
|
||||
this.nopSendInterval = setInterval(() => this.sendNop(), 5000);
|
||||
this.msgRecieveInterval = setInterval(() => this.onNoMsg(), 10000);
|
||||
this.sendNop();
|
||||
@@ -66,7 +66,7 @@ export class User {
|
||||
this.VoteRateLimit = new RateLimiter(3, 3);
|
||||
this.VoteRateLimit.on('limit', () => this.closeConnection());
|
||||
}
|
||||
|
||||
|
||||
assignGuestName(existingUsers: string[]): string {
|
||||
var username;
|
||||
do {
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import VMDisplay from "./VMDisplay.js";
|
||||
import VMDisplay from './VMDisplay.js';
|
||||
|
||||
export default interface VM {
|
||||
Start(): Promise<void>;
|
||||
Stop(): Promise<void>;
|
||||
Reboot(): Promise<void>;
|
||||
Reset(): Promise<void>;
|
||||
MonitorCommand(command: string): Promise<any>;
|
||||
GetDisplay(): VMDisplay;
|
||||
SnapshotsSupported(): boolean;
|
||||
}
|
||||
Start(): Promise<void>;
|
||||
Stop(): Promise<void>;
|
||||
Reboot(): Promise<void>;
|
||||
Reset(): Promise<void>;
|
||||
MonitorCommand(command: string): Promise<any>;
|
||||
GetDisplay(): VMDisplay;
|
||||
SnapshotsSupported(): boolean;
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import { Size } from "@cvmts/shared";
|
||||
import EventEmitter from "node:events";
|
||||
import { Size } from '@cvmts/shared';
|
||||
import EventEmitter from 'node:events';
|
||||
|
||||
export default interface VMDisplay extends EventEmitter {
|
||||
Connect(): void;
|
||||
Disconnect(): void;
|
||||
Connected(): boolean;
|
||||
Buffer(): Buffer;
|
||||
Size(): Size;
|
||||
MouseEvent(x: number, y: number, buttons: number): void;
|
||||
KeyboardEvent(keysym: number, pressed: boolean): void;
|
||||
}
|
||||
Connect(): void;
|
||||
Disconnect(): void;
|
||||
Connected(): boolean;
|
||||
Buffer(): Buffer;
|
||||
Size(): Size;
|
||||
MouseEvent(x: number, y: number, buttons: number): void;
|
||||
KeyboardEvent(keysym: number, pressed: boolean): void;
|
||||
}
|
||||
|
||||
@@ -1,31 +1,28 @@
|
||||
import EventEmitter from "events";
|
||||
import VNCVMDef from "./VNCVMDef";
|
||||
import VM from "../VM";
|
||||
import VMDisplay from "../VMDisplay";
|
||||
import { Clamp, Logger, Rect, Size, Sleep } from "@cvmts/shared";
|
||||
import EventEmitter from 'events';
|
||||
import VNCVMDef from './VNCVMDef';
|
||||
import VM from '../VM';
|
||||
import VMDisplay from '../VMDisplay';
|
||||
import { Clamp, Logger, Rect, Size, Sleep } from '@cvmts/shared';
|
||||
import { VncClient } from '@computernewb/nodejs-rfb';
|
||||
import { BatchRects } from "@cvmts/qemu";
|
||||
import { execaCommand } from "execa";
|
||||
import { BatchRects } from '@cvmts/qemu';
|
||||
import { execaCommand } from 'execa';
|
||||
|
||||
export default class VNCVM extends EventEmitter implements VM, VMDisplay {
|
||||
def : VNCVMDef;
|
||||
logger: Logger;
|
||||
private displayVnc = new VncClient({
|
||||
debug: false,
|
||||
fps: 60,
|
||||
encodings: [
|
||||
VncClient.consts.encodings.raw,
|
||||
VncClient.consts.encodings.pseudoDesktopSize
|
||||
]
|
||||
});
|
||||
private vncShouldReconnect: boolean = false;
|
||||
def: VNCVMDef;
|
||||
logger: Logger;
|
||||
private displayVnc = new VncClient({
|
||||
debug: false,
|
||||
fps: 60,
|
||||
encodings: [VncClient.consts.encodings.raw, VncClient.consts.encodings.pseudoDesktopSize]
|
||||
});
|
||||
private vncShouldReconnect: boolean = false;
|
||||
|
||||
constructor(def : VNCVMDef) {
|
||||
super();
|
||||
this.def = def;
|
||||
this.logger = new Logger(`CVMTS.VNCVM/${this.def.vncHost}:${this.def.vncPort}`);
|
||||
constructor(def: VNCVMDef) {
|
||||
super();
|
||||
this.def = def;
|
||||
this.logger = new Logger(`CVMTS.VNCVM/${this.def.vncHost}:${this.def.vncPort}`);
|
||||
|
||||
this.displayVnc.on('connectTimeout', () => {
|
||||
this.displayVnc.on('connectTimeout', () => {
|
||||
this.Reconnect();
|
||||
});
|
||||
|
||||
@@ -34,7 +31,7 @@ export default class VNCVM extends EventEmitter implements VM, VMDisplay {
|
||||
});
|
||||
|
||||
this.displayVnc.on('disconnect', () => {
|
||||
this.logger.Info('Disconnected');
|
||||
this.logger.Info('Disconnected');
|
||||
this.Reconnect();
|
||||
});
|
||||
|
||||
@@ -43,7 +40,7 @@ export default class VNCVM extends EventEmitter implements VM, VMDisplay {
|
||||
});
|
||||
|
||||
this.displayVnc.on('firstFrameUpdate', () => {
|
||||
this.logger.Info('Connected');
|
||||
this.logger.Info('Connected');
|
||||
// apparently this library is this good.
|
||||
// at least it's better than the two others which exist.
|
||||
this.displayVnc.changeFps(60);
|
||||
@@ -77,17 +74,16 @@ export default class VNCVM extends EventEmitter implements VM, VMDisplay {
|
||||
|
||||
this.emit('frame');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async Reset(): Promise<void> {
|
||||
if (this.def.restoreCmd) await execaCommand(this.def.restoreCmd, {shell: true});
|
||||
else {
|
||||
await this.Stop();
|
||||
await Sleep(1000);
|
||||
await this.Start();
|
||||
}
|
||||
}
|
||||
async Reset(): Promise<void> {
|
||||
if (this.def.restoreCmd) await execaCommand(this.def.restoreCmd, { shell: true });
|
||||
else {
|
||||
await this.Stop();
|
||||
await Sleep(1000);
|
||||
await this.Start();
|
||||
}
|
||||
}
|
||||
|
||||
private Reconnect() {
|
||||
if (this.displayVnc.connected) return;
|
||||
@@ -98,60 +94,60 @@ export default class VNCVM extends EventEmitter implements VM, VMDisplay {
|
||||
// if we fail after max tries, emit a event
|
||||
|
||||
this.displayVnc.connect({
|
||||
host: this.def.vncHost,
|
||||
port: this.def.vncPort,
|
||||
path: null,
|
||||
host: this.def.vncHost,
|
||||
port: this.def.vncPort,
|
||||
path: null
|
||||
});
|
||||
}
|
||||
|
||||
async Start(): Promise<void> {
|
||||
this.logger.Info('Connecting');
|
||||
if (this.def.startCmd) await execaCommand(this.def.startCmd, {shell: true});
|
||||
this.Connect();
|
||||
}
|
||||
async Start(): Promise<void> {
|
||||
this.logger.Info('Connecting');
|
||||
if (this.def.startCmd) await execaCommand(this.def.startCmd, { shell: true });
|
||||
this.Connect();
|
||||
}
|
||||
|
||||
async Stop(): Promise<void> {
|
||||
this.logger.Info('Disconnecting');
|
||||
this.Disconnect();
|
||||
if (this.def.stopCmd) await execaCommand(this.def.stopCmd, {shell: true});
|
||||
}
|
||||
async Stop(): Promise<void> {
|
||||
this.logger.Info('Disconnecting');
|
||||
this.Disconnect();
|
||||
if (this.def.stopCmd) await execaCommand(this.def.stopCmd, { shell: true });
|
||||
}
|
||||
|
||||
async Reboot(): Promise<void> {
|
||||
if (this.def.rebootCmd) await execaCommand(this.def.rebootCmd, {shell: true});
|
||||
}
|
||||
async Reboot(): Promise<void> {
|
||||
if (this.def.rebootCmd) await execaCommand(this.def.rebootCmd, { shell: true });
|
||||
}
|
||||
|
||||
async MonitorCommand(command: string): Promise<any> {
|
||||
// TODO: This can maybe run a specified command?
|
||||
return "This VM does not support monitor commands.";
|
||||
}
|
||||
async MonitorCommand(command: string): Promise<any> {
|
||||
// TODO: This can maybe run a specified command?
|
||||
return 'This VM does not support monitor commands.';
|
||||
}
|
||||
|
||||
GetDisplay(): VMDisplay {
|
||||
return this;
|
||||
}
|
||||
GetDisplay(): VMDisplay {
|
||||
return this;
|
||||
}
|
||||
|
||||
SnapshotsSupported(): boolean {
|
||||
return true;
|
||||
}
|
||||
SnapshotsSupported(): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
Connect(): void {
|
||||
this.vncShouldReconnect = true;
|
||||
this.Reconnect();
|
||||
}
|
||||
Connect(): void {
|
||||
this.vncShouldReconnect = true;
|
||||
this.Reconnect();
|
||||
}
|
||||
|
||||
Disconnect(): void {
|
||||
this.vncShouldReconnect = false;
|
||||
this.displayVnc.disconnect();
|
||||
}
|
||||
Disconnect(): void {
|
||||
this.vncShouldReconnect = false;
|
||||
this.displayVnc.disconnect();
|
||||
}
|
||||
|
||||
Connected(): boolean {
|
||||
return this.displayVnc.connected;
|
||||
}
|
||||
Connected(): boolean {
|
||||
return this.displayVnc.connected;
|
||||
}
|
||||
|
||||
Buffer(): Buffer {
|
||||
return this.displayVnc.fb;
|
||||
}
|
||||
Buffer(): Buffer {
|
||||
return this.displayVnc.fb;
|
||||
}
|
||||
|
||||
Size(): Size {
|
||||
Size(): Size {
|
||||
if (!this.displayVnc.connected)
|
||||
return {
|
||||
width: 0,
|
||||
@@ -162,13 +158,13 @@ export default class VNCVM extends EventEmitter implements VM, VMDisplay {
|
||||
width: this.displayVnc.clientWidth,
|
||||
height: this.displayVnc.clientHeight
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
MouseEvent(x: number, y: number, buttons: number): void {
|
||||
MouseEvent(x: number, y: number, buttons: number): void {
|
||||
if (this.displayVnc.connected) this.displayVnc.sendPointerEvent(Clamp(x, 0, this.displayVnc.clientWidth), Clamp(y, 0, this.displayVnc.clientHeight), buttons);
|
||||
}
|
||||
}
|
||||
|
||||
KeyboardEvent(keysym: number, pressed: boolean): void {
|
||||
KeyboardEvent(keysym: number, pressed: boolean): void {
|
||||
if (this.displayVnc.connected) this.displayVnc.sendKeyEvent(keysym, pressed);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
export default interface VNCVMDef {
|
||||
vncHost : string;
|
||||
vncPort : number;
|
||||
startCmd : string | null;
|
||||
stopCmd : string | null;
|
||||
rebootCmd : string | null;
|
||||
restoreCmd : string | null;
|
||||
}
|
||||
vncHost: string;
|
||||
vncPort: number;
|
||||
startCmd: string | null;
|
||||
stopCmd: string | null;
|
||||
rebootCmd: string | null;
|
||||
restoreCmd: string | null;
|
||||
}
|
||||
|
||||
@@ -1,62 +1,60 @@
|
||||
import { WebSocket } from "ws";
|
||||
import NetworkClient from "../NetworkClient.js";
|
||||
import EventEmitter from "events";
|
||||
import { Logger } from "@cvmts/shared";
|
||||
import { WebSocket } from 'ws';
|
||||
import NetworkClient from '../NetworkClient.js';
|
||||
import EventEmitter from 'events';
|
||||
import { Logger } from '@cvmts/shared';
|
||||
|
||||
export default class WSClient extends EventEmitter implements NetworkClient {
|
||||
socket: WebSocket;
|
||||
ip: string;
|
||||
socket: WebSocket;
|
||||
ip: string;
|
||||
|
||||
constructor(ws: WebSocket, ip: string) {
|
||||
super();
|
||||
this.socket = ws;
|
||||
this.ip = ip;
|
||||
this.socket.on('message', (buf: Buffer, isBinary: boolean) => {
|
||||
constructor(ws: WebSocket, ip: string) {
|
||||
super();
|
||||
this.socket = ws;
|
||||
this.ip = ip;
|
||||
this.socket.on('message', (buf: Buffer, isBinary: boolean) => {
|
||||
// Close the user's connection if they send a non-string message
|
||||
if (isBinary) {
|
||||
this.close();
|
||||
return;
|
||||
}
|
||||
|
||||
this.emit('msg', buf.toString("utf-8"));
|
||||
this.emit('msg', buf.toString('utf-8'));
|
||||
});
|
||||
|
||||
this.socket.on('close', () => {
|
||||
this.emit('disconnect');
|
||||
});
|
||||
}
|
||||
this.socket.on('close', () => {
|
||||
this.emit('disconnect');
|
||||
});
|
||||
}
|
||||
|
||||
isOpen(): boolean {
|
||||
return this.socket.readyState === WebSocket.OPEN;
|
||||
}
|
||||
isOpen(): boolean {
|
||||
return this.socket.readyState === WebSocket.OPEN;
|
||||
}
|
||||
|
||||
getIP(): string {
|
||||
return this.ip;
|
||||
}
|
||||
send(msg: string): Promise<void> {
|
||||
return new Promise((res,rej) => {
|
||||
if(!this.isOpen())
|
||||
getIP(): string {
|
||||
return this.ip;
|
||||
}
|
||||
send(msg: string): Promise<void> {
|
||||
return new Promise((res, rej) => {
|
||||
if (!this.isOpen()) res();
|
||||
|
||||
this.socket.send(msg, (err) => {
|
||||
if (err) {
|
||||
rej(err);
|
||||
return;
|
||||
}
|
||||
res();
|
||||
|
||||
this.socket.send(msg, (err) => {
|
||||
if (err) {
|
||||
rej(err);
|
||||
return;
|
||||
}
|
||||
res();
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
close(): void {
|
||||
if(this.isOpen()) {
|
||||
close(): void {
|
||||
if (this.isOpen()) {
|
||||
// While this seems counterintutive, do note that the WebSocket protocol
|
||||
// *sends* a data frame whilist closing a connection. Therefore, if the other end
|
||||
// has forcibly hung up (closed) their connection, the best way to handle that
|
||||
// is to just let the inner TCP socket propegate that, which `ws` will do for us.
|
||||
// Otherwise, we'll try to send data to a closed client then SIGPIPE.
|
||||
this.socket.close();
|
||||
this.socket.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,18 +11,18 @@ import { User } from '../User.js';
|
||||
import { Logger } from '@cvmts/shared';
|
||||
|
||||
export default class WSServer extends EventEmitter implements NetworkServer {
|
||||
private httpServer: http.Server;
|
||||
private wsServer: WebSocketServer;
|
||||
private clients: WSClient[];
|
||||
private Config: IConfig;
|
||||
private logger: Logger;
|
||||
private httpServer: http.Server;
|
||||
private wsServer: WebSocketServer;
|
||||
private clients: WSClient[];
|
||||
private Config: IConfig;
|
||||
private logger: Logger;
|
||||
|
||||
constructor(config : IConfig) {
|
||||
super();
|
||||
this.Config = config;
|
||||
this.clients = [];
|
||||
this.logger = new Logger("CVMTS.WSServer");
|
||||
this.httpServer = http.createServer();
|
||||
constructor(config: IConfig) {
|
||||
super();
|
||||
this.Config = config;
|
||||
this.clients = [];
|
||||
this.logger = new Logger('CVMTS.WSServer');
|
||||
this.httpServer = http.createServer();
|
||||
this.wsServer = new WebSocketServer({ noServer: true });
|
||||
this.httpServer.on('upgrade', (req: http.IncomingMessage, socket: internal.Duplex, head: Buffer) => this.httpOnUpgrade(req, socket, head));
|
||||
this.httpServer.on('request', (req, res) => {
|
||||
@@ -30,19 +30,19 @@ export default class WSServer extends EventEmitter implements NetworkServer {
|
||||
res.write('This server only accepts WebSocket connections.');
|
||||
res.end();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
start(): void {
|
||||
this.httpServer.listen(this.Config.http.port, this.Config.http.host, () => {
|
||||
this.logger.Info(`WebSocket server listening on ${this.Config.http.host}:${this.Config.http.port}`);
|
||||
});
|
||||
}
|
||||
start(): void {
|
||||
this.httpServer.listen(this.Config.http.port, this.Config.http.host, () => {
|
||||
this.logger.Info(`WebSocket server listening on ${this.Config.http.host}:${this.Config.http.port}`);
|
||||
});
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
this.httpServer.close();
|
||||
}
|
||||
stop(): void {
|
||||
this.httpServer.close();
|
||||
}
|
||||
|
||||
private httpOnUpgrade(req: http.IncomingMessage, socket: internal.Duplex, head: Buffer) {
|
||||
private httpOnUpgrade(req: http.IncomingMessage, socket: internal.Duplex, head: Buffer) {
|
||||
var killConnection = () => {
|
||||
socket.write('HTTP/1.1 400 Bad Request\n\n400 Bad Request');
|
||||
socket.destroy();
|
||||
@@ -121,7 +121,7 @@ export default class WSServer extends EventEmitter implements NetworkServer {
|
||||
|
||||
let ipdata = IPDataManager.GetIPDataMaybe(ip);
|
||||
|
||||
if(ipdata != null) {
|
||||
if (ipdata != null) {
|
||||
let connections = ipdata.refCount;
|
||||
if (connections + 1 > this.Config.collabvm.maxConnections) {
|
||||
socket.write('HTTP/1.1 429 Too Many Requests\n\n429 Too Many Requests');
|
||||
@@ -136,11 +136,11 @@ export default class WSServer extends EventEmitter implements NetworkServer {
|
||||
}
|
||||
|
||||
private onConnection(ws: WebSocket, req: http.IncomingMessage, ip: string) {
|
||||
let client = new WSClient(ws, ip);
|
||||
this.clients.push(client);
|
||||
let client = new WSClient(ws, ip);
|
||||
this.clients.push(client);
|
||||
let user = new User(client, IPDataManager.GetIPData(ip), this.Config);
|
||||
|
||||
this.emit('connect', user);
|
||||
this.emit('connect', user);
|
||||
|
||||
ws.on('error', (e) => {
|
||||
this.logger.Error(`${e} (caused by connection ${ip})`);
|
||||
|
||||
@@ -34,7 +34,7 @@ try {
|
||||
}
|
||||
|
||||
let exiting = false;
|
||||
let VM : VM;
|
||||
let VM: VM;
|
||||
|
||||
async function stop() {
|
||||
if (exiting) return;
|
||||
@@ -47,7 +47,7 @@ async function start() {
|
||||
// Init the auth manager if enabled
|
||||
let auth = Config.auth.enabled ? new AuthManager(Config.auth.apiEndpoint, Config.auth.secretKey) : null;
|
||||
switch (Config.vm.type) {
|
||||
case "qemu": {
|
||||
case 'qemu': {
|
||||
// Print a warning if qmpSockDir is set
|
||||
// and the host OS is Windows, as this
|
||||
// configuration will very likely not work.
|
||||
@@ -67,7 +67,7 @@ async function start() {
|
||||
VM = new QemuVM(def);
|
||||
break;
|
||||
}
|
||||
case "vncvm": {
|
||||
case 'vncvm': {
|
||||
VM = new VNCVM(Config.vncvm);
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user