Skip to content
This repository has been archived by the owner on Sep 2, 2018. It is now read-only.

Commit

Permalink
Update preflight to allow access to more information
Browse files Browse the repository at this point in the history
- Resolves PR #29
  • Loading branch information
LordRalex committed Jul 29, 2015
1 parent 2faf061 commit b0ab747
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ data/*
*.crt
*.key
*.csr
Scales.iml
24 changes: 24 additions & 0 deletions lib/api/preflight.js
Original file line number Diff line number Diff line change
@@ -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;
8 changes: 6 additions & 2 deletions lib/plugins/bungeecord.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down
10 changes: 7 additions & 3 deletions lib/plugins/mcserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {

Expand Down
6 changes: 5 additions & 1 deletion lib/plugins/minecraft-pre.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
9 changes: 8 additions & 1 deletion lib/plugins/minecraft.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
24 changes: 15 additions & 9 deletions lib/plugins/srcds.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down
36 changes: 27 additions & 9 deletions lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit b0ab747

Please sign in to comment.