16
16
17
17
const rcon = require ( 'rcon-srcds' ) ;
18
18
const logReceiver = require ( 'srcds-log-receiver' ) ;
19
- const http = require ( 'http' ) ;
20
19
const webSocket = require ( 'ws' )
21
20
const url = require ( 'url' ) ;
21
+ const fs = require ( 'fs' ) ;
22
22
const events = require ( 'events' ) ;
23
23
const pty = require ( 'node-pty' ) ;
24
24
const { exec, spawn } = require ( 'child_process' ) ;
@@ -36,10 +36,26 @@ var state = {
36
36
'serverRunning' : false ,
37
37
'serverRcon' : undefined ,
38
38
'authenticated' : false ,
39
- 'authenticating' : false
39
+ 'authenticating' : false ,
40
+ 'updating' : false
40
41
}
41
42
var serverInfo = new si ( ) ;
42
43
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
+ }
43
59
44
60
// check for running Server on Startup
45
61
exec ( '/bin/ps -a' , ( error , stdout , stderr ) => {
@@ -154,7 +170,7 @@ function executeRcon (message) {
154
170
/**
155
171
* Creates a http server to communicate with a webInteraface.
156
172
*/
157
- http . createServer ( ( req , res ) => {
173
+ http . createServer ( httpOptions , ( req , res ) => {
158
174
var myUrl = url . parse ( req . url , true ) ;
159
175
160
176
// Process "control" messages.
@@ -205,20 +221,27 @@ http.createServer((req, res) => {
205
221
} ) ;
206
222
207
223
//Update Server
208
- } else if ( args . action == "update" ) {
224
+ } else if ( args . action == "update" && ! state . updating ) {
225
+ let updateSuccess = false ;
226
+ state . updating = true ;
209
227
console . log ( 'Updating Server.' ) ;
210
228
let updateProcess = pty . spawn ( cfg . updateCommand , cfg . updateArguments ) ;
211
229
updateProcess . on ( 'data' , ( data ) => {
212
- if ( data . indexOf ( "Update state (0x" ) != - 1 ) {
230
+ console . log ( data ) ;
231
+ if ( data . indexOf ( 'Update state (0x' ) != - 1 ) {
213
232
let rex = / U p d a t e s t a t e \( 0 x \d + \) ( .+ ) , p r o g r e s s : ( \d { 1 , 2 } ) \. \d { 2 } / ;
214
233
let matches = rex . exec ( data ) ;
215
234
updateEmitter . emit ( 'progress' , matches [ 1 ] , matches [ 2 ] ) ;
235
+ } else if ( data . indexOf ( 'Success!' ) != - 1 ) {
236
+ console . log ( 'update succeeded' ) ;
237
+ updateSuccess = true ;
216
238
}
217
239
} ) ;
218
240
updateProcess . on ( 'close' , ( code ) => {
219
241
res . writeHeader ( 200 , { "Content-Type" : "application/json" } ) ;
220
- res . write ( `{ "success": true }` ) ;
242
+ res . write ( `{ "success": ${ updateSuccess } }` ) ;
221
243
res . end ( ) ;
244
+ state . updating = false ;
222
245
} ) ;
223
246
224
247
// Send Status
@@ -275,11 +298,16 @@ http.createServer((req, res) => {
275
298
res . write ( '{ "error": true }' ) ;
276
299
res . end ( ) ;
277
300
}
301
+ } else {
302
+ res . setHeader ( "Access-Control-Allow-Origin" , "*" ) ;
303
+ res . writeHeader ( 200 , { 'Content-Type' : 'text/plain' } ) ;
304
+ res . write ( 'command ignored' ) ;
278
305
}
279
306
} ) . listen ( 8090 ) ;
280
307
281
308
/*----------------- WebSockets Code -------------------*/
282
- const wss = new webSocket . Server ( { port : 8091 } )
309
+ const wssServer = http . createServer ( httpOptions ) ;
310
+ const wss = new webSocket . Server ( { server : wssServer } ) ;
283
311
284
312
/**
285
313
* Websocket to send data updates to a webClient.
@@ -329,6 +357,14 @@ wss.on('connection', (ws) => {
329
357
} ) ;
330
358
} ) ;
331
359
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
+
332
368
/*----------------- log receiving code --------------------*/
333
369
// Since we only control locally installed servers and server logging is not working on
334
370
// 'localhost', we use the ip-address of the interface configured.
0 commit comments