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

Commit b0ab747

Browse files
committed
Update preflight to allow access to more information
- Resolves PR #29
1 parent 2faf061 commit b0ab747

File tree

8 files changed

+93
-25
lines changed

8 files changed

+93
-25
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ data/*
1010
*.crt
1111
*.key
1212
*.csr
13+
Scales.iml

lib/api/preflight.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function Preflight(rootPath, publicPath, config, callback) {
2+
this.rootPath = rootPath;
3+
this.publicPath = publicPath;
4+
this.config = config;
5+
this.callback = callback;
6+
}
7+
8+
Preflight.prototype.getRootPath = function() {
9+
return this.rootPath;
10+
}
11+
12+
Preflight.prototype.getPublicPath = function() {
13+
return this.publicPath;
14+
}
15+
16+
Preflight.prototype.getConfig = function() {
17+
return this.config;
18+
}
19+
20+
Preflight.prototype.getCallBack = function() {
21+
return this.callback;
22+
}
23+
24+
module.exports = Preflight;

lib/plugins/bungeecord.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,13 @@ settings.query = function query(config) {
6161
* Check if we have a server settings file. If so; enable query, set server port, set query port, and set server ip.
6262
* If we do NOT have a server settings file, throw error and escape.
6363
*/
64-
settings.preflight = function(config, basePath, callback) {
64+
settings.preflight = function(preflightConfig) {
6565

66-
configPath = path.join(basePath, settings.cfg);
66+
var config = preflightConfig.getConfig();
67+
var basePath = preflightConfig.getPublicPath();
68+
var callback = preflightConfig.getCallBack();
69+
70+
var configPath = path.join(basePath, settings.cfg);
6771

6872
if(config.startup.variables.jar == undefined) {
6973

lib/plugins/mcserver.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,14 @@ settings.query = function query(config) {
3333

3434
}
3535

36-
settings.preflight = function(config, basePath, callback) {
36+
settings.preflight = function(preflightConfig) {
3737

38-
settingsPath = path.join(basePath, settings.cfg);
39-
webadminPath = path.join(basePath, settings.webadmin);
38+
var config = preflightConfig.getConfig();
39+
var basePath = preflightConfig.getPublicPath();
40+
var callback = preflightConfig.getCallBack();
41+
42+
var settingsPath = path.join(basePath, settings.cfg);
43+
var webadminPath = path.join(basePath, settings.webadmin);
4044

4145
if(!fs.existsSync(path.join(basePath, settings.exe))) {
4246

lib/plugins/minecraft-pre.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ settings.query = function query(config) {
5959
* Check if we have a server settings file. If so; enable query, set server port, set query port, and set server ip.
6060
* If we do NOT have a server settings file, throw error and escape.
6161
*/
62-
settings.preflight = function(config, basePath, callback) {
62+
settings.preflight = function(preflightConfig) {
63+
64+
var config = preflightConfig.getConfig();
65+
var basePath = preflightConfig.getPublicPath();
66+
var callback = preflightConfig.getCallBack();
6367

6468
propertiesPath = path.join(basePath, settings.cfg);
6569

lib/plugins/minecraft.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,14 @@ settings.query = function query(config) {
6161
* Check if we have a server settings file. If so; enable query, set server port, set query port, and set server ip.
6262
* If we do NOT have a server settings file, throw error and escape.
6363
*/
64-
settings.preflight = function(config, basePath, callback) {
64+
settings.preflight = function(preflightConfig) {
65+
66+
var config = preflightConfig.getConfig();
67+
var basePath = preflightConfig.getPublicPath();
68+
var callback = preflightConfig.getCallBack();
69+
70+
log.verbose("Preflight entered");
71+
log.verbose(preflightConfig);
6572

6673
propertiesPath = path.join(basePath, settings.cfg);
6774

lib/plugins/srcds.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,28 @@ settings.query = function query(config) {
6363
/**
6464
* Run Pre-Flight
6565
*/
66-
settings.preflight = function(config, serverPath, callback) {
66+
settings.preflight = function(preflightConfig) {
6767

6868
//callback();
69+
var publicPath = preflightConfig.getPublicPath();
70+
var rootPath = preflightConfig.getRootPath();
71+
var config = preflightConfig.getConfig();
72+
var callback = preflightConfig.getCallBack();
6973

7074
var storedFile, userFile;
75+
var rootFile = path.join(rootPath, 'srcds_run');
76+
var serverFile = path.join(publicPath, 'srcds_run');
7177
async.series([
7278
function(callback2) {
7379

7480
async.parallel([
7581
function(callbackp) {
7682

77-
fs.readFile(path.join(serverPath, 'srcds_run'), function (err, data) {
83+
fs.readFile(serverFile, function (err, data) {
7884

7985
if(err) {
8086

81-
log.error("Error detected while trying to open and read /home/" + config.user + "/srcds_run", err);
87+
log.error("Error detected while trying to open and read " + serverFile, err);
8288
log.error(err.message);
8389

8490
} else {
@@ -93,11 +99,11 @@ settings.preflight = function(config, serverPath, callback) {
9399
},
94100
function(callbackp) {
95101

96-
fs.readFile('/home/' + config.user + '/srcds_run', function (err, data) {
102+
fs.readFile(rootFile, function (err, data) {
97103

98104
if(err) {
99105

100-
log.error("Error detected while trying to open and read /home/" + config.user + "/srcds_run", err);
106+
log.error("Error detected while trying to open and read " + rootFile, err);
101107
log.error(err.message);
102108

103109
} else {
@@ -127,10 +133,10 @@ settings.preflight = function(config, serverPath, callback) {
127133

128134
// Tampered File
129135
log.warn("Detected srcds_run as being tampered with. Replacing this file now.");
130-
fs.copy('/home/' + config.user + '/srcds_run', path.join(serverPath, 'srcds_run'), function(error) {
136+
fs.copy(rootFile, serverFile, function(error) {
131137

132138
if(error) {
133-
log.error("An error occured while trying to overwrite changes to srcds_run due to a file hash mismatch.", error);
139+
log.error("An error occurred while trying to overwrite changes to srcds_run due to a file hash mismatch.", error);
134140
} else {
135141
callback2();
136142
}
@@ -143,10 +149,10 @@ settings.preflight = function(config, serverPath, callback) {
143149

144150
},
145151
function(callback2) {
146-
fs.remove(path.join(serverPath, settings.log), function (error) {
152+
fs.remove(path.join(publicPath, settings.log), function (error) {
147153

148154
if(error) {
149-
log.error("An error occured while trying to remove the old log file for " + config.name + " during the plugin preflight.", error);
155+
log.error("An error occurred while trying to remove the old log file for " + config.name + " during the plugin preflight.", error);
150156
} else {
151157
callback2();
152158
}

lib/process.js

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ var log = require("./logger.js"),
2121
querystring = require("querystring"),
2222
stripansi = require("strip-ansi"),
2323
logStream = false,
24-
server = {};
24+
server = {},
25+
Preflight = require("./api/preflight.js");
2526

2627
var OFF = 0,
2728
ON = 1,
@@ -103,7 +104,11 @@ Scales.prototype.preflight = function() {
103104
},
104105
plugin: function(callback) {
105106
log.verbose("Running plugin preflight for server " + s.config.name);
106-
s.plugin.preflight(s.config, s.buildPath(null), callback);
107+
log.verbose("Creating");
108+
log.verbose(s.config);
109+
var preflightConfig = new Preflight(s.getRootPath(), s.buildPublicPath(null), s.config, callback);
110+
log.verbose("Created");
111+
s.plugin.preflight(preflightConfig);
107112
},
108113
power: function(callback) {
109114
log.verbose("Running power on function for server " + s.config.name);
@@ -581,23 +586,36 @@ Scales.prototype.logContents = function(lines) {
581586

582587
}
583588

584-
Scales.prototype.buildPath = function(base) {
589+
Scales.prototype.getRootPath = function() {
590+
591+
var filepath = path.join(gconfig.basepath, this.config.user);
592+
return filepath;
593+
594+
}
595+
596+
Scales.prototype.buildPublicPath = function(base) {
597+
598+
var publicPath = path.join(this.getRootPath(), "/public");
585599

586600
if(base !== null) {
587-
var filepath = path.join(gconfig.basepath, this.config.user, "/public", path.normalize(querystring.unescape(base)));
588-
} else {
589-
var filepath = path.join(gconfig.basepath, this.config.user, "/public");
601+
publicPath = path.join(publicPath, path.normalize(querystring.unescape(base)));
590602
}
591603

592-
if(filepath.indexOf(gconfig.basepath + this.config.user) != 0) {
604+
if(publicPath.indexOf(gconfig.basepath + this.config.user) != 0) {
593605

594-
log.error("API attempted to delete a file outside of home directory " + gconfig.basepath + this.config.user + ". Request denied.");
606+
log.error("API attempted to access a file outside of base directory " + gconfig.basepath + this.config.user + ". Request denied.");
595607
return null;
596608

597609
}
598610

599-
return filepath;
611+
log.verbose(publicPath);
612+
613+
return publicPath;
614+
615+
}
600616

617+
Scales.prototype.buildPath = function (base) {
618+
return this.buildPublicPath(base);
601619
}
602620

603621
/**

0 commit comments

Comments
 (0)