Skip to content

Commit fabfa5e

Browse files
committed
Add PPP Connection config
1 parent fa7932b commit fabfa5e

File tree

9 files changed

+966
-5
lines changed

9 files changed

+966
-5
lines changed

package-lock.json

Lines changed: 80 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
"nyc": "^17.1.0",
9595
"pino-colada": "^2.2.2",
9696
"pino-http": "^10.3.0",
97+
"sinon": "^21.0.0",
9798
"vite-plugin-eslint": "^1.8.1",
9899
"vitest": "^3.1.1"
99100
},

server/index.js

Lines changed: 91 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const crypto = require('crypto');
3333

3434
// set up rate limiter: maximum of fifty requests per minute
3535
const RateLimit = require('express-rate-limit')
36+
const pppConnection = require('./pppConnection.js')
3637
const limiter = RateLimit({
3738
windowMs: 1 * 60 * 1000, // 1 minute
3839
max: 50
@@ -66,17 +67,46 @@ const cloud = new cloudManager(settings)
6667
const logConversion = new logConversionManager(settings)
6768
const adhocManager = new Adhoc(settings)
6869
const userMgmt = new userLogin()
70+
const pppConnectionManager = new pppConnection(settings)
6971

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+
})
7381

74-
function quitting () {
82+
process.on('SIGTERM', () => {
83+
console.log('Received SIGTERM. Shutting down gracefully...')
84+
pppConnectionManager.quitting()
7585
cloud.quitting()
7686
logConversion.quitting()
7787
console.log('---Shutdown Rpanion---')
7888
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+
})
80110

81111
// Got an RTCM message, send to flight controller
82112
ntripClient.eventEmitter.on('rtcmpacket', (msg, seq) => {
@@ -310,6 +340,62 @@ function authenticateToken(req, res, next) {
310340
})
311341
}
312342

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+
313399
// Serve the logfile
314400
app.get('/api/logfile', authenticateToken, (req, res) => {
315401
aboutPage.getsystemctllog((logStr) => {

0 commit comments

Comments
 (0)