diff --git a/.gitignore b/.gitignore index ee5c9d8..8919847 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,10 @@ # See http://help.github.com/ignore-files/ for more about ignoring files. +# SS +app.log +config.json +dist.zip + # compiled output /dist /tmp @@ -10,6 +15,7 @@ # IDEs and editors /.idea +.idea .project .classpath .c9/ diff --git a/src/api/certManager.ts b/src/api/certManager.ts index 010f816..e7deb37 100755 --- a/src/api/certManager.ts +++ b/src/api/certManager.ts @@ -4,8 +4,8 @@ import {SSManager} from "../ssmanager"; import * as proc from "child_process"; import * as util from "util"; -class CertManager { - public ensureCerts = async () => { +export class CertManager { + public ensureCerts = async (): Promise => { try { await fs.stat(path.join(SSManager.getRoot(), "../certs/server.cert")); await fs.stat(path.join(SSManager.getRoot(), "../certs/server.key")); @@ -16,14 +16,14 @@ class CertManager { } }; - public getOptions = async () => { + public getOptions = async (): Promise => { return { key: await fs.readFile(path.join(SSManager.getRoot(), "../certs/server.key")), cert: await fs.readFile(path.join(SSManager.getRoot(), "../certs/server.cert")) } }; - private generateCerts = async () => { + private generateCerts = async (): Promise => { await new Promise((resolve, reject) => { proc.exec(util.format(path.join(SSManager.getRoot(), "/bashScripts/generateSsl.sh") + " %s", SSManager.config.api.addr), {cwd: path.join(SSManager.getRoot(), "../certs/")}, (err) => { if (err) return reject(err); @@ -33,4 +33,3 @@ class CertManager { }; } -export {CertManager} diff --git a/src/api/controllers/games.ts b/src/api/controllers/games.ts index 85ffaa2..ee2f88a 100644 --- a/src/api/controllers/games.ts +++ b/src/api/controllers/games.ts @@ -1,9 +1,7 @@ import {SSManager} from "../../ssmanager"; -class GamesController { +export class GamesController { public getGames = async (req, res, next) => { res.json({games: SSManager.configsController.games}); }; -} - -export {GamesController} \ No newline at end of file +} \ No newline at end of file diff --git a/src/api/controllers/gameserver.ts b/src/api/controllers/gameserver.ts index db02dad..ab1b0f0 100644 --- a/src/api/controllers/gameserver.ts +++ b/src/api/controllers/gameserver.ts @@ -4,7 +4,7 @@ import {SSManager} from "../../ssmanager"; import * as async from "async"; -class ServersController { +export class ServersController { public getGameservers = async (req, res, next) => { const serverList = []; @@ -444,6 +444,4 @@ class ServersController { server: newServer.getInfo() }); } -} - -export {ServersController} \ No newline at end of file +} \ No newline at end of file diff --git a/src/api/controllers/node.ts b/src/api/controllers/node.ts index f1ba536..63108d9 100644 --- a/src/api/controllers/node.ts +++ b/src/api/controllers/node.ts @@ -1,7 +1,7 @@ import * as diskspace from "diskspace"; import * as osUtils from "os-utils"; -class NodeController { +export class NodeController { public getStatus = async (req, res, next) => { osUtils.cpuUsage(function (cpu) { diskspace.check('/', function (err, disk) { @@ -27,6 +27,4 @@ class NodeController { }); }); }; -} - -export {NodeController} \ No newline at end of file +} \ No newline at end of file diff --git a/src/api/controllers/plugins.ts b/src/api/controllers/plugins.ts index 40e40c0..b5f2226 100644 --- a/src/api/controllers/plugins.ts +++ b/src/api/controllers/plugins.ts @@ -1,9 +1,7 @@ import {SSManager} from "../../ssmanager"; -class PluginsController { +export class PluginsController { public getPlugins = async (req, res, next) => { res.json({games: SSManager.configsController.plugins}); }; -} - -export {PluginsController} \ No newline at end of file +} \ No newline at end of file diff --git a/src/api/server.ts b/src/api/server.ts index 79d315c..abc3fe8 100644 --- a/src/api/server.ts +++ b/src/api/server.ts @@ -12,8 +12,9 @@ import {PluginsController} from "./controllers/plugins"; import {ServersController} from "./controllers/gameserver"; import {LoadedMiddleware} from "./middleware/loaded"; import {CertManager} from "./certManager"; +import {Gameserver} from "../manager/controllers/gameserver/gameserver"; -class APIServer { +export class APIServer { public express; public http; public io; @@ -32,7 +33,7 @@ class APIServer { this.certManager = new CertManager(); } - public bootstrapExpress = async () => { + public bootstrapExpress = async (): Promise => { //Make sure certs are installed await this.certManager.ensureCerts(); @@ -111,7 +112,7 @@ class APIServer { await this.createHttp(); }; - private createHttp = async () => { + private createHttp = async (): Promise => { SSManager.logger.verbose("HTTP server hosted on :" + SSManager.config.api.port); this.http = https.createServer(await this.certManager.getOptions(), this.express); @@ -161,6 +162,4 @@ class APIServer { this.express.use('', apiRouter); }; -} - -export {APIServer} \ No newline at end of file +} \ No newline at end of file diff --git a/src/manager/controllers/configs/configManager.ts b/src/manager/controllers/configs/configManager.ts index 6a5b561..41f1a78 100644 --- a/src/manager/controllers/configs/configManager.ts +++ b/src/manager/controllers/configs/configManager.ts @@ -4,7 +4,7 @@ import {SSManager} from "../../../ssmanager"; import {IGame} from "./gameConfig"; import {IPlugin} from "./pluginConfig"; -class ConfigsController { +export class ConfigsController { public games: Array; public plugins: Array; @@ -14,13 +14,11 @@ class ConfigsController { this.dataFolder = dataFolder; } - public loadGames = async () => { + public loadGames = async (): Promise => { this.games = await SSUtil.dirToJson(path.join(SSManager.getRoot(), this.dataFolder, "/games/")); }; - public loadPlugins = async () => { + public loadPlugins = async (): Promise => { this.plugins = await SSUtil.dirToJson(path.join(SSManager.getRoot(), this.dataFolder, "/plugins/")); }; } - -export { ConfigsController }; \ No newline at end of file diff --git a/src/manager/controllers/configs/gameConfig.ts b/src/manager/controllers/configs/gameConfig.ts index 2cb7507..2e6413f 100644 --- a/src/manager/controllers/configs/gameConfig.ts +++ b/src/manager/controllers/configs/gameConfig.ts @@ -1,4 +1,4 @@ -interface IGame { +export interface IGame { name: string, gamedig: { active: boolean, @@ -17,6 +17,4 @@ interface IGame { useStdout: boolean, }, verify: any -} - -export {IGame} \ No newline at end of file +} \ No newline at end of file diff --git a/src/manager/controllers/configs/pluginConfig.ts b/src/manager/controllers/configs/pluginConfig.ts index bc62382..6580798 100644 --- a/src/manager/controllers/configs/pluginConfig.ts +++ b/src/manager/controllers/configs/pluginConfig.ts @@ -1,8 +1,6 @@ -interface IPlugin { +export interface IPlugin { game: string, name: string, install: any, remove: any -} - -export {IPlugin} \ No newline at end of file +} \ No newline at end of file diff --git a/src/manager/controllers/configs/serverConfig.ts b/src/manager/controllers/configs/serverConfig.ts index 583c14d..c8ec234 100644 --- a/src/manager/controllers/configs/serverConfig.ts +++ b/src/manager/controllers/configs/serverConfig.ts @@ -1,6 +1,6 @@ import {IGame} from "./gameConfig"; -interface IServer { +export interface IServer { game: IGame, id: string, port: number, @@ -12,6 +12,4 @@ interface IServer { plugins: any, installed: boolean, players: number -} - -export { IServer } \ No newline at end of file +} \ No newline at end of file diff --git a/src/manager/controllers/gameserver/gameserver.ts b/src/manager/controllers/gameserver/gameserver.ts index 09a0793..ee2ff4f 100644 --- a/src/manager/controllers/gameserver/gameserver.ts +++ b/src/manager/controllers/gameserver/gameserver.ts @@ -18,7 +18,7 @@ import * as Pty from "pty.js"; import * as stripAnsi from "strip-ansi"; import {GamedigHelper} from "./helpers/gamedig"; -class Gameserver extends EventEmitter { +export class Gameserver extends EventEmitter { currentGame: IGame; status: Status; @@ -86,7 +86,7 @@ class Gameserver extends EventEmitter { }; }; - public reloadConfig = async (conf: IServer) => { + public reloadConfig = async (conf: IServer): Promise => { if (this.isBlocked) throw new ServerActionError("Server is locked. It may be installing or updating."); @@ -107,11 +107,11 @@ class Gameserver extends EventEmitter { this.setBlocked(false); }; - public updateConfig = async () => { + public updateConfig = async (): Promise => { await fs.outputJson(path.join(SSManager.getRoot(), "../storage/servers/", this.id + ".json"), this.exportConfig()); }; - public create = async (password: string) => { + public create = async (password: string): Promise => { if (this.isBlocked) throw new ServerActionError("Server is locked. It may be installing or updating."); this.setBlocked(true); @@ -127,23 +127,23 @@ class Gameserver extends EventEmitter { this.setBlocked(false); }; - public createIdentity = async () => { + public createIdentity = async (): Promise => { await fs.outputJson(path.join(this.fsHelper.getRoot(), "/identity.json"), { id: this.id }); }; - private runUpdateScripts = async () => { + private runUpdateScripts = async (): Promise => { const updateCommands = this.currentGame.update; await this.executeShellStack(updateCommands); }; - private runInstallScripts = async () => { + private runInstallScripts = async (): Promise => { const installCommands = this.currentGame.install; await this.executeShellStack(installCommands); }; - public start = async () => { + public start = async (): Promise => { if (this.isBlocked) throw new ServerActionError("Server is locked. It may be installing or updating."); @@ -179,7 +179,7 @@ class Gameserver extends EventEmitter { await this.dockerHelper.startContainer(); }; - public executeCommand = (command: string) => { + public executeCommand = (command: string): void => { if (this.status !== Status.Running) throw new ServerActionError("Server is not running."); @@ -189,7 +189,7 @@ class Gameserver extends EventEmitter { this.dockerHelper.writeToProcess(command); }; - public removePlugin = async (plugin: string) => { + public removePlugin = async (plugin: string): Promise => { if (this.isBlocked) throw new ServerActionError("Server is locked. It may be installing or updating."); if (this.status !== Status.Off) @@ -204,7 +204,7 @@ class Gameserver extends EventEmitter { await this.executeShellStack(pluginData.remove); }; - public installPlugin = async (plugin: string) => { + public installPlugin = async (plugin: string): Promise => { if (this.isBlocked) throw new ServerActionError("Server is locked. It may be installing or updating."); if (this.status !== Status.Off) @@ -231,7 +231,7 @@ class Gameserver extends EventEmitter { this.setBlocked(false); }; - public changePassword = async (password: string) => { + public changePassword = async (password: string): Promise => { //TODO: double check if support is added for SFTP //Strip all possible things that might mess this up const newPassword = '"' + password.replace(/(["\s'$`\\])/g, '\\$1') + '"'; @@ -248,7 +248,7 @@ class Gameserver extends EventEmitter { /* WARNING: Do not directly call this, as it will not remove the server from the listing. */ - public remove = async () => { + public remove = async (): Promise => { if (this.isBlocked) throw new ServerActionError("Server is locked. It may be installing or updating."); if (this.status !== Status.Off) @@ -274,7 +274,7 @@ class Gameserver extends EventEmitter { await fs.unlink(path.join(SSManager.getRoot(), "../storage/servers/", this.id + ".json")); }; - public reinstall = async () => { + public reinstall = async (): Promise => { if (this.isBlocked) throw new ServerActionError("Server is locked. It may be installing or updating."); if (!this.isInstalled) @@ -315,14 +315,14 @@ class Gameserver extends EventEmitter { this.setBlocked(false); }; - public stop = () => { + public stop = (): void => { if (this.status !== Status.Running) throw new ServerActionError("Server is not running."); this.dockerHelper.writeToProcess(this.currentGame.stopConsoleCommand); this.updateStatus(Status.Stopping); }; - public install = async () => { + public install = async (): Promise => { if (this.isBlocked) throw new ServerActionError("Server is locked. It may be installing or updating."); @@ -345,32 +345,32 @@ class Gameserver extends EventEmitter { }; - public logInfo = (data: string) => { + public logInfo = (data: string): void => { if (this.status === Status.Starting) this.updateStatus(Status.Running); SSManager.logger.verbose("[Server " + this.id + "] " + data); this.emit('console', stripAnsi(data)); }; - public logAnnounce = (data: string) => { + public logAnnounce = (data: string): void => { SSManager.logger.verbose("[Server " + this.id + "] " + data); this.emit('announcement', data); }; - public updateStatus = (status: Status) => { + public updateStatus = (status: Status): void => { SSManager.logger.verbose("Server " + this.id + " status updated to " + status); this.status = status; this.emit('statusChange', status); }; - private setBlocked = (isBlocked: boolean) => { + private setBlocked = (isBlocked: boolean): void => { SSManager.logger.verbose("[Server " + this.id + " ] Blocked set to " + isBlocked); this.isBlocked = isBlocked; this.emit('block', isBlocked); }; - private setInstalled = async (isInstalled: boolean) => { + private setInstalled = async (isInstalled: boolean): Promise => { this.isInstalled = isInstalled; this.emit('installed', isInstalled); await this.updateConfig(); @@ -379,7 +379,7 @@ class Gameserver extends EventEmitter { /* This will throw an exception if the server is not running. */ - public forceKill = async () => { + public forceKill = async (): Promise => { if (this.status === Status.Off) throw new ServerActionError("Server is not running."); @@ -389,7 +389,7 @@ class Gameserver extends EventEmitter { /* This won't, so use it internally. */ - public killContainer = async (updateStatus: boolean = true) => { + public killContainer = async (updateStatus: boolean = true): Promise => { if(updateStatus) this.updateStatus(Status.Stopping); @@ -399,7 +399,7 @@ class Gameserver extends EventEmitter { } }; - private executeShellStack = async (stack: any) => { + private executeShellStack = async (stack: any): Promise => { await new Promise((resolve) => { async.forEachSeries(stack, (cmd: any, next) => { let shell = 'su'; @@ -421,6 +421,4 @@ class Gameserver extends EventEmitter { }); }); } -} - -export {Gameserver} \ No newline at end of file +} \ No newline at end of file diff --git a/src/manager/controllers/gameserver/gameserverManager.ts b/src/manager/controllers/gameserver/gameserverManager.ts index df176a8..d69eebd 100644 --- a/src/manager/controllers/gameserver/gameserverManager.ts +++ b/src/manager/controllers/gameserver/gameserverManager.ts @@ -4,7 +4,7 @@ import {SSManager} from "../../../ssmanager"; import {Gameserver} from "./gameserver"; import {IServer} from "../configs/serverConfig"; -class GameserverController { +export class GameserverController { public servers: Array; private readonly dataFolder: string; @@ -15,14 +15,14 @@ class GameserverController { this.servers = []; } - public loadServers = async () => { + public loadServers = async (): Promise => { const serversJSON = await SSUtil.dirToJson(path.join(SSManager.getRoot(), this.dataFolder, "/servers/")); serversJSON.map(server => { this.servers.push(new Gameserver(server)); }) }; - public getNiceConfigs = () => { + public getNiceConfigs = (): object => { let niceConfigs = []; this.servers.map(server => { niceConfigs.push(server.getInfo()); @@ -30,14 +30,14 @@ class GameserverController { return niceConfigs; }; - public addNewServer = async (jsonData: IServer) => { + public addNewServer = async (jsonData: IServer): Promise => { const newServer = new Gameserver(jsonData); this.servers.push(newServer); await newServer.updateConfig(); return newServer; }; - public removeServer = async (targetServer: Gameserver) => { + public removeServer = async (targetServer: Gameserver): Promise => { let removed = false; await Promise.all(this.servers.map(async (server, index) => { if (server === targetServer) { @@ -48,6 +48,4 @@ class GameserverController { })); return removed; } -} - -export {GameserverController} \ No newline at end of file +} \ No newline at end of file diff --git a/src/manager/dockerInstaller.ts b/src/manager/dockerInstaller.ts index f337e35..a1cb405 100644 --- a/src/manager/dockerInstaller.ts +++ b/src/manager/dockerInstaller.ts @@ -16,13 +16,13 @@ class DockerInstaller { }); } - public bootstrap = async () => { + public bootstrap = async (): Promise => { if (!(await DockerodeUtils.imageExists(this.dockerContoller, "ssjava"))) { await this.addImage(path.join(SSManager.getRoot(), "../dockerfiles/java/"), "ssjava"); } }; - private addImage = async (path: string, name: string) => { + private addImage = async (path: string, name: string): Promise => { SSManager.logger.verbose("Adding Docker image for " + name); await new Promise((resolve, reject) => { diff --git a/src/ssmanager.ts b/src/ssmanager.ts index 4d380df..3e08b9d 100644 --- a/src/ssmanager.ts +++ b/src/ssmanager.ts @@ -7,7 +7,7 @@ import {GameserverController} from "./manager/controllers/gameserver/gameserverM import {APIServer} from "./api/server"; import {DockerInstaller} from "./manager/dockerInstaller"; -class SSManager { +export class SSManager { static config: IConfig; static logger: Logger; static loaded: boolean; @@ -32,7 +32,7 @@ class SSManager { }) } - private bootstrap = async () => { + private bootstrap = async (): Promise => { //Bootstrap docker const dockerInstaller = new DockerInstaller(); SSManager.logger.info("Checking Docker images..."); @@ -60,6 +60,4 @@ class SSManager { static getRoot(): string { return __dirname; } -} - -export {SSManager} \ No newline at end of file +} \ No newline at end of file diff --git a/src/util/config.ts b/src/util/config.ts index 1405f6d..c76c41d 100644 --- a/src/util/config.ts +++ b/src/util/config.ts @@ -1,4 +1,4 @@ -interface IConfig { +export interface IConfig { "servers": { "pingTime": number, "maxPort": number, @@ -12,6 +12,4 @@ interface IConfig { "socket": { "maxFileSize": number } -} - -export {IConfig} \ No newline at end of file +} \ No newline at end of file diff --git a/src/util/logger.ts b/src/util/logger.ts index 93b75f7..43a4b0d 100644 --- a/src/util/logger.ts +++ b/src/util/logger.ts @@ -1,17 +1,13 @@ import {SSManager} from "../ssmanager"; import * as path from 'path'; import * as winston from 'winston'; -import {transports} from "winston"; -class Logger { +export class Logger { //TODO: implement fs logging - private readonly logToFile; private logger; constructor(logToFile: boolean) { - this.logToFile = logToFile; - const options = { file: { level: 'info', @@ -48,18 +44,16 @@ class Logger { }) } - public info = (message: string) => { + public info = (message: string): void => { this.logger.info("[Info] " + message) }; - public verbose = (message: string) => { + public verbose = (message: string): void => { this.logger.debug("[Verbose] " + message) }; - public error = (message: string) => { + public error = (message: string): void => { this.logger.error("[Verbose] " + message) }; -} - -export {Logger} \ No newline at end of file +} \ No newline at end of file diff --git a/src/util/util.ts b/src/util/util.ts index cd359c4..6793d5c 100644 --- a/src/util/util.ts +++ b/src/util/util.ts @@ -2,7 +2,7 @@ import * as fs from 'fs-extra'; import * as path from 'path'; import {SSManager} from "../ssmanager"; -export const dirToJson = async (dataFolder: string) => { +export const dirToJson = async (dataFolder: string): Promise => { //Get all files in a directory const filesList = await fs.readdir(dataFolder);