@@ -33,6 +33,7 @@ const crypto = require('crypto');
33
33
34
34
// set up rate limiter: maximum of fifty requests per minute
35
35
const RateLimit = require ( 'express-rate-limit' )
36
+ const pppConnection = require ( './pppConnection.js' )
36
37
const limiter = RateLimit ( {
37
38
windowMs : 1 * 60 * 1000 , // 1 minute
38
39
max : 50
@@ -66,17 +67,46 @@ const cloud = new cloudManager(settings)
66
67
const logConversion = new logConversionManager ( settings )
67
68
const adhocManager = new Adhoc ( settings )
68
69
const userMgmt = new userLogin ( )
70
+ const pppConnectionManager = new pppConnection ( settings )
69
71
70
- // cleanup, if needed
71
- process . on ( 'SIGINT' , quitting ) // run signal handler when main process exits
72
- // process.on('SIGTERM', quitting) // run signal handler when service exits. Need for Ubuntu??
72
+ // Add graceful shutdown handlers
73
+ process . on ( 'SIGINT' , ( ) => {
74
+ console . log ( 'Received SIGINT. Shutting down gracefully...' )
75
+ pppConnectionManager . quitting ( )
76
+ cloud . quitting ( )
77
+ logConversion . quitting ( )
78
+ console . log ( '---Shutdown Rpanion---' )
79
+ process . exit ( 0 )
80
+ } )
73
81
74
- function quitting ( ) {
82
+ process . on ( 'SIGTERM' , ( ) => {
83
+ console . log ( 'Received SIGTERM. Shutting down gracefully...' )
84
+ pppConnectionManager . quitting ( )
75
85
cloud . quitting ( )
76
86
logConversion . quitting ( )
77
87
console . log ( '---Shutdown Rpanion---' )
78
88
process . exit ( 0 )
79
- }
89
+ } )
90
+
91
+ // Also good to handle uncaught exceptions
92
+ process . on ( 'uncaughtException' , ( err ) => {
93
+ console . error ( 'Uncaught exception:' , err )
94
+ pppConnectionManager . quitting ( )
95
+ cloud . quitting ( )
96
+ logConversion . quitting ( )
97
+ console . log ( '---Shutdown Rpanion---' )
98
+ process . exit ( 1 )
99
+ } )
100
+
101
+ // Handle nodemon restarts
102
+ process . once ( 'SIGUSR2' , ( ) => {
103
+ console . log ( 'Received SIGUSR2. Shutting down gracefully...' )
104
+ pppConnectionManager . quitting ( )
105
+ cloud . quitting ( )
106
+ logConversion . quitting ( )
107
+ console . log ( '---Shutdown Rpanion---' )
108
+ process . kill ( process . pid , 'SIGUSR2' )
109
+ } )
80
110
81
111
// Got an RTCM message, send to flight controller
82
112
ntripClient . eventEmitter . on ( 'rtcmpacket' , ( msg , seq ) => {
@@ -310,6 +340,62 @@ function authenticateToken(req, res, next) {
310
340
} )
311
341
}
312
342
343
+ //pppConnectionManager url endpoints
344
+ app . get ( '/api/pppstatus' , authenticateToken , ( req , res ) => {
345
+ res . setHeader ( 'Content-Type' , 'application/json' )
346
+ pppConnectionManager . getPPPSettings ( ( err , settings ) => {
347
+ if ( err ) {
348
+ console . log ( 'Error in /api/pppstatus' , { message : err } )
349
+ res . send ( JSON . stringify ( { error : err } ) )
350
+ return
351
+ }
352
+ res . send ( JSON . stringify ( settings ) )
353
+ } )
354
+ } )
355
+
356
+ app . post ( '/api/pppmodify' , authenticateToken , [
357
+ check ( 'device' ) . isJSON ( ) ,
358
+ check ( 'baudrate' ) . isJSON ( ) ,
359
+ check ( 'localIP' ) . isIP ( ) ,
360
+ check ( 'remoteIP' ) . isIP ( ) ,
361
+ check ( 'enabled' ) . isBoolean ( )
362
+ ] , ( req , res ) => {
363
+ const errors = validationResult ( req )
364
+ if ( ! errors . isEmpty ( ) ) {
365
+ console . log ( 'Bad POST vars in /api/pppmodify' , { message : JSON . stringify ( errors . array ( ) ) } )
366
+ return res . status ( 422 ) . json ( { error : JSON . stringify ( errors . array ( ) ) } )
367
+ }
368
+
369
+ if ( req . body . enabled === true ) {
370
+ console . log ( 'Starting PPP connection' ) ;
371
+ res . setHeader ( 'Content-Type' , 'application/json' )
372
+ pppConnectionManager . startPPP ( JSON . parse ( req . body . device ) , JSON . parse ( req . body . baudrate ) , req . body . localIP , req . body . remoteIP , ( err , settings ) => {
373
+ if ( err !== null ) {
374
+ console . log ( 'Error in /api/pppmodify' , { message : err } )
375
+ console . log ( JSON . stringify ( { settings, error : err } ) )
376
+ res . send ( JSON . stringify ( { settings, error : err . toString ( ) } ) )
377
+ return
378
+ } else {
379
+ res . send ( JSON . stringify ( { settings} ) )
380
+ return
381
+ }
382
+ } )
383
+ }
384
+ else if ( req . body . enabled === false ) {
385
+ pppConnectionManager . stopPPP ( ( err , settings ) => {
386
+ if ( err ) {
387
+ //console.log('Error in /api/pppmodify', { message: err })
388
+ console . log ( JSON . stringify ( { settings, error : err } ) )
389
+ res . send ( JSON . stringify ( { settings, error : err } ) )
390
+ return
391
+ } else {
392
+ res . send ( JSON . stringify ( { settings} ) )
393
+ return
394
+ }
395
+ } )
396
+ }
397
+ } )
398
+
313
399
// Serve the logfile
314
400
app . get ( '/api/logfile' , authenticateToken , ( req , res ) => {
315
401
aboutPage . getsystemctllog ( ( logStr ) => {
0 commit comments