diff --git a/.gitignore b/.gitignore index c530e94..448ca13 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ data/* *.crt *.key *.csr +Scales.iml diff --git a/lib/api/preflight.js b/lib/api/preflight.js new file mode 100644 index 0000000..6bd7e70 --- /dev/null +++ b/lib/api/preflight.js @@ -0,0 +1,24 @@ +function Preflight(rootPath, publicPath, config, callback) { + this.rootPath = rootPath; + this.publicPath = publicPath; + this.config = config; + this.callback = callback; +} + +Preflight.prototype.getRootPath = function() { + return this.rootPath; +} + +Preflight.prototype.getPublicPath = function() { + return this.publicPath; +} + +Preflight.prototype.getConfig = function() { + return this.config; +} + +Preflight.prototype.getCallBack = function() { + return this.callback; +} + +module.exports = Preflight; \ No newline at end of file diff --git a/lib/plugins/bungeecord.js b/lib/plugins/bungeecord.js index e282fbc..554b9d0 100644 --- a/lib/plugins/bungeecord.js +++ b/lib/plugins/bungeecord.js @@ -61,9 +61,13 @@ settings.query = function query(config) { * Check if we have a server settings file. If so; enable query, set server port, set query port, and set server ip. * If we do NOT have a server settings file, throw error and escape. */ -settings.preflight = function(config, basePath, callback) { +settings.preflight = function(preflightConfig) { - configPath = path.join(basePath, settings.cfg); + var config = preflightConfig.getConfig(); + var basePath = preflightConfig.getPublicPath(); + var callback = preflightConfig.getCallBack(); + + var configPath = path.join(basePath, settings.cfg); if(config.startup.variables.jar == undefined) { diff --git a/lib/plugins/mcserver.js b/lib/plugins/mcserver.js index b1ce47f..b1b8230 100644 --- a/lib/plugins/mcserver.js +++ b/lib/plugins/mcserver.js @@ -33,10 +33,14 @@ settings.query = function query(config) { } -settings.preflight = function(config, basePath, callback) { +settings.preflight = function(preflightConfig) { - settingsPath = path.join(basePath, settings.cfg); - webadminPath = path.join(basePath, settings.webadmin); + var config = preflightConfig.getConfig(); + var basePath = preflightConfig.getPublicPath(); + var callback = preflightConfig.getCallBack(); + + var settingsPath = path.join(basePath, settings.cfg); + var webadminPath = path.join(basePath, settings.webadmin); if(!fs.existsSync(path.join(basePath, settings.exe))) { diff --git a/lib/plugins/minecraft-pre.js b/lib/plugins/minecraft-pre.js index bd86e49..92f400d 100644 --- a/lib/plugins/minecraft-pre.js +++ b/lib/plugins/minecraft-pre.js @@ -59,7 +59,11 @@ settings.query = function query(config) { * Check if we have a server settings file. If so; enable query, set server port, set query port, and set server ip. * If we do NOT have a server settings file, throw error and escape. */ -settings.preflight = function(config, basePath, callback) { +settings.preflight = function(preflightConfig) { + + var config = preflightConfig.getConfig(); + var basePath = preflightConfig.getPublicPath(); + var callback = preflightConfig.getCallBack(); propertiesPath = path.join(basePath, settings.cfg); diff --git a/lib/plugins/minecraft.js b/lib/plugins/minecraft.js index 543bf71..2835f25 100644 --- a/lib/plugins/minecraft.js +++ b/lib/plugins/minecraft.js @@ -61,7 +61,14 @@ settings.query = function query(config) { * Check if we have a server settings file. If so; enable query, set server port, set query port, and set server ip. * If we do NOT have a server settings file, throw error and escape. */ -settings.preflight = function(config, basePath, callback) { +settings.preflight = function(preflightConfig) { + + var config = preflightConfig.getConfig(); + var basePath = preflightConfig.getPublicPath(); + var callback = preflightConfig.getCallBack(); + + log.verbose("Preflight entered"); + log.verbose(preflightConfig); propertiesPath = path.join(basePath, settings.cfg); diff --git a/lib/plugins/srcds.js b/lib/plugins/srcds.js index 1ca78ca..448b8e0 100644 --- a/lib/plugins/srcds.js +++ b/lib/plugins/srcds.js @@ -63,22 +63,28 @@ settings.query = function query(config) { /** * Run Pre-Flight */ -settings.preflight = function(config, serverPath, callback) { +settings.preflight = function(preflightConfig) { //callback(); + var publicPath = preflightConfig.getPublicPath(); + var rootPath = preflightConfig.getRootPath(); + var config = preflightConfig.getConfig(); + var callback = preflightConfig.getCallBack(); var storedFile, userFile; + var rootFile = path.join(rootPath, 'srcds_run'); + var serverFile = path.join(publicPath, 'srcds_run'); async.series([ function(callback2) { async.parallel([ function(callbackp) { - fs.readFile(path.join(serverPath, 'srcds_run'), function (err, data) { + fs.readFile(serverFile, function (err, data) { if(err) { - log.error("Error detected while trying to open and read /home/" + config.user + "/srcds_run", err); + log.error("Error detected while trying to open and read " + serverFile, err); log.error(err.message); } else { @@ -93,11 +99,11 @@ settings.preflight = function(config, serverPath, callback) { }, function(callbackp) { - fs.readFile('/home/' + config.user + '/srcds_run', function (err, data) { + fs.readFile(rootFile, function (err, data) { if(err) { - log.error("Error detected while trying to open and read /home/" + config.user + "/srcds_run", err); + log.error("Error detected while trying to open and read " + rootFile, err); log.error(err.message); } else { @@ -127,10 +133,10 @@ settings.preflight = function(config, serverPath, callback) { // Tampered File log.warn("Detected srcds_run as being tampered with. Replacing this file now."); - fs.copy('/home/' + config.user + '/srcds_run', path.join(serverPath, 'srcds_run'), function(error) { + fs.copy(rootFile, serverFile, function(error) { if(error) { - log.error("An error occured while trying to overwrite changes to srcds_run due to a file hash mismatch.", error); + log.error("An error occurred while trying to overwrite changes to srcds_run due to a file hash mismatch.", error); } else { callback2(); } @@ -143,10 +149,10 @@ settings.preflight = function(config, serverPath, callback) { }, function(callback2) { - fs.remove(path.join(serverPath, settings.log), function (error) { + fs.remove(path.join(publicPath, settings.log), function (error) { if(error) { - log.error("An error occured while trying to remove the old log file for " + config.name + " during the plugin preflight.", error); + log.error("An error occurred while trying to remove the old log file for " + config.name + " during the plugin preflight.", error); } else { callback2(); } diff --git a/lib/process.js b/lib/process.js index a9340ad..14ff2e0 100644 --- a/lib/process.js +++ b/lib/process.js @@ -21,7 +21,8 @@ var log = require("./logger.js"), querystring = require("querystring"), stripansi = require("strip-ansi"), logStream = false, - server = {}; + server = {}, + Preflight = require("./api/preflight.js"); var OFF = 0, ON = 1, @@ -103,7 +104,11 @@ Scales.prototype.preflight = function() { }, plugin: function(callback) { log.verbose("Running plugin preflight for server " + s.config.name); - s.plugin.preflight(s.config, s.buildPath(null), callback); + log.verbose("Creating"); + log.verbose(s.config); + var preflightConfig = new Preflight(s.getRootPath(), s.buildPublicPath(null), s.config, callback); + log.verbose("Created"); + s.plugin.preflight(preflightConfig); }, power: function(callback) { log.verbose("Running power on function for server " + s.config.name); @@ -581,23 +586,36 @@ Scales.prototype.logContents = function(lines) { } -Scales.prototype.buildPath = function(base) { +Scales.prototype.getRootPath = function() { + + var filepath = path.join(gconfig.basepath, this.config.user); + return filepath; + +} + +Scales.prototype.buildPublicPath = function(base) { + + var publicPath = path.join(this.getRootPath(), "/public"); if(base !== null) { - var filepath = path.join(gconfig.basepath, this.config.user, "/public", path.normalize(querystring.unescape(base))); - } else { - var filepath = path.join(gconfig.basepath, this.config.user, "/public"); + publicPath = path.join(publicPath, path.normalize(querystring.unescape(base))); } - if(filepath.indexOf(gconfig.basepath + this.config.user) != 0) { + if(publicPath.indexOf(gconfig.basepath + this.config.user) != 0) { - log.error("API attempted to delete a file outside of home directory " + gconfig.basepath + this.config.user + ". Request denied."); + log.error("API attempted to access a file outside of base directory " + gconfig.basepath + this.config.user + ". Request denied."); return null; } - return filepath; + log.verbose(publicPath); + + return publicPath; + +} +Scales.prototype.buildPath = function (base) { + return this.buildPublicPath(base); } /**