Skip to content

Commit 2edd560

Browse files
authored
Merge pull request #3 from Taraman17/https
Https
2 parents 0c5d8f0 + 0443d91 commit 2edd560

File tree

2 files changed

+79
-16
lines changed

2 files changed

+79
-16
lines changed

config.js

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1-
module.exports = class config {
1+
/**
2+
* Config class for SCGO Server API
3+
*/
4+
module.exports = class config {
25
constructor () {
36
this._userOptions = {
47
// Network interface over which the server is communicating. We set this and not the
58
// IP-address, in case the server is using DHCP in a LAN and not a static address.
69
'iface': 'eth0',
7-
// Steam Account to update the server with steamcmd.
8-
'steamAccount': '<username> <password>',
910
// steam serverToken. To get one see https://steamcommunity.com/dev/managegameservers
1011
'serverToken': '<token>',
1112
// Well, the rcon password...
1213
'rconPass': 'YourRconPass',
13-
// Path to steamcmd, can stay like this if installed via package manager.
14-
'steamExe': 'steamcmd',
14+
// https settings
15+
'useHttps': false,
16+
'httpsCertificate': '',
17+
'httpsPrivateKey': '',
18+
// Optional.In case your CA is not trusted by default (e.g. letsencrypt), you can add
19+
// the CA-Cert here.
20+
'httpsCa': '',
1521
// The folder, where your srcds_run is located
1622
'csgoDir': '/home/csgo/csgo_ds',
1723
// anything you want your server command line to have additional to:
@@ -24,17 +30,18 @@
2430
// The screen Logfile where the console output of screen and the server will be logged.
2531
// New logs are appended, so you may need to delete or rotate this log periodically.
2632
'screenLog': '/home/csgo/screen.log',
33+
// Path to steamcmd, can stay like this if installed via package manager.
34+
'steamExe': 'steamcmd',
35+
// Steam Account to update the server with steamcmd.
36+
'steamAccount': '<username> <password>',
2737
// Script to pass into steamcmd to update.
2838
// See https://steamcommunity.com/discussions/forum/1/492379159713970561/ for more info.
2939
'updateScript': '/home/csgo/update_csgo.txt'
3040
};
3141

32-
this.screenCommand = `${this._userOptions.screen} -L -Logfile ${this._userOptions.screenLog} -dmS ${this._userOptions.screenName}`;
42+
this.screenCommand = `${this._userOptions.screen} -L -Logfile ${this._userOptions.screenLog} -dmS ${this._userOptions.screenName}`;
3343
this.csgoCommand = `${this._userOptions.csgoDir}/srcds_run`;
3444
this.csgoArgs = `-game csgo -console -usercon +sv_setsteamaccount ${this._userOptions.serverToken} ${this._userOptions.csgoOptionalArgs}`;
35-
this.updateCommand = `${this._userOptions.steamExe}`;
36-
this.updateArguments = [`+login ${this._userOptions.steamAccount}`,
37-
`+runscript ${this._userOptions.updateScript}`];
3845
}
3946

4047
get rconPass () {
@@ -48,4 +55,24 @@
4855
get serverCommandline () {
4956
return `${this.screenCommand} ${this.csgoCommand} ${this.csgoArgs}`;
5057
}
58+
get updateCommand () {
59+
return this._userOptions.steamExe
60+
}
61+
get updateArguments () {
62+
return [`+login ${this._userOptions.steamAccount}`,
63+
`+runscript ${this._userOptions.updateScript}`];
64+
}
65+
66+
get useHttps () {
67+
return this._userOptions.useHttps;
68+
}
69+
get httpsCertificate () {
70+
return this._userOptions.httpsCertificate;
71+
}
72+
get httpsPrivateKey () {
73+
return this._userOptions.httpsPrivateKey;
74+
}
75+
get httpsCa () {
76+
return this._userOptions.httpsCa;
77+
}
5178
};

serverControl.js

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
const rcon = require('rcon-srcds');
1818
const logReceiver = require('srcds-log-receiver');
19-
const http = require('http');
2019
const webSocket = require('ws')
2120
const url = require('url');
21+
const fs = require('fs');
2222
const events = require('events');
2323
const pty = require('node-pty');
2424
const { exec, spawn } = require('child_process');
@@ -36,10 +36,26 @@ var state = {
3636
'serverRunning': false,
3737
'serverRcon': undefined,
3838
'authenticated': false,
39-
'authenticating': false
39+
'authenticating': false,
40+
'updating': false
4041
}
4142
var serverInfo = new si();
4243
var cfg = new config();
44+
var http = undefined;
45+
var httpOptions = {};
46+
// if configured for https, we fork here.
47+
if (cfg.useHttps) {
48+
http = require('https');
49+
httpOptions = {
50+
key: fs.readFileSync(cfg.httpsPrivateKey),
51+
cert: fs.readFileSync(cfg.httpsCertificate),
52+
};
53+
if (cfg.httpsCa != '') {
54+
httpOptions.ca = fs.readFileSync(cfg.httpsCa)
55+
}
56+
} else {
57+
http = require('http');
58+
}
4359

4460
// check for running Server on Startup
4561
exec('/bin/ps -a', (error, stdout, stderr) => {
@@ -154,7 +170,7 @@ function executeRcon (message) {
154170
/**
155171
* Creates a http server to communicate with a webInteraface.
156172
*/
157-
http.createServer((req, res) => {
173+
http.createServer(httpOptions, (req, res) => {
158174
var myUrl = url.parse(req.url, true);
159175

160176
// Process "control" messages.
@@ -205,20 +221,27 @@ http.createServer((req, res) => {
205221
});
206222

207223
//Update Server
208-
} else if (args.action == "update") {
224+
} else if (args.action == "update" && !state.updating) {
225+
let updateSuccess = false;
226+
state.updating = true;
209227
console.log('Updating Server.');
210228
let updateProcess = pty.spawn(cfg.updateCommand, cfg.updateArguments);
211229
updateProcess.on('data', (data) => {
212-
if (data.indexOf("Update state (0x") != -1) {
230+
console.log(data);
231+
if (data.indexOf('Update state (0x') != -1) {
213232
let rex = /Update state \(0x\d+\) (.+), progress: (\d{1,2})\.\d{2}/;
214233
let matches = rex.exec(data);
215234
updateEmitter.emit('progress', matches[1], matches[2]);
235+
} else if (data.indexOf('Success!') != -1) {
236+
console.log('update succeeded');
237+
updateSuccess = true;
216238
}
217239
});
218240
updateProcess.on('close', (code) => {
219241
res.writeHeader(200, {"Content-Type": "application/json"});
220-
res.write(`{ "success": true }`);
242+
res.write(`{ "success": ${updateSuccess} }`);
221243
res.end();
244+
state.updating = false;
222245
});
223246

224247
// Send Status
@@ -275,11 +298,16 @@ http.createServer((req, res) => {
275298
res.write('{ "error": true }');
276299
res.end();
277300
}
301+
} else {
302+
res.setHeader("Access-Control-Allow-Origin", "*");
303+
res.writeHeader(200, { 'Content-Type': 'text/plain' });
304+
res.write('command ignored');
278305
}
279306
}).listen(8090);
280307

281308
/*----------------- WebSockets Code -------------------*/
282-
const wss = new webSocket.Server({ port: 8091 })
309+
const wssServer = http.createServer(httpOptions);
310+
const wss = new webSocket.Server({ server: wssServer });
283311

284312
/**
285313
* Websocket to send data updates to a webClient.
@@ -329,6 +357,14 @@ wss.on('connection', (ws) => {
329357
});
330358
});
331359

360+
wssServer.listen(8091, () => {
361+
if(cfg.useHttps) {
362+
const ws = new webSocket(`wss://klosser.duckdns.org:${wssServer.address().port}`);
363+
} else {
364+
const ws = new webSocket(`ws://klosser.duckdns.org:${wssServer.address().port}`);
365+
}
366+
});
367+
332368
/*----------------- log receiving code --------------------*/
333369
// Since we only control locally installed servers and server logging is not working on
334370
// 'localhost', we use the ip-address of the interface configured.

0 commit comments

Comments
 (0)