a whole bunch of things

This commit is contained in:
STEINNI
2026-06-21 21:08:04 +00:00
parent 01bf35a238
commit 1db846daa3
7 changed files with 56 additions and 38 deletions
+12 -5
View File
@@ -8,11 +8,18 @@ export const methods = {
{ {
"action": "PONG", "action": "PONG",
} }
*/ */
action_PONG(action, payload){ action_PONG(action, payload, reqid){
clearTimeout(this.keepAliveBomb);
this.keepAliveNextTimeout = setTimeout(this.keepAlive.bind(this),this.config.server.keepAliveInterval*1000); if(reqid == this.latestPing.id) {
}, console.log('====>PONG', reqid, this.latestPing, Date.now() - this.latestPing.time)
this.roundTripTime = (this.roundTripTime + (Date.now() - this.latestPing.time)) / 2 //Exponential smoothing
}
// First defuse the bomb
clearTimeout(this.keepAliveBomb)
// Then setup the next one ;-)
this.keepAliveNextTimeout = setTimeout(this.keepAlive.bind(this),this.config.server.keepAliveInterval*1000);
},
/* Request: /* Request:
+3 -2
View File
@@ -117,8 +117,9 @@
"listenPath": { "type": "string" }, "listenPath": { "type": "string" },
"unsecure": { "type": "boolean" }, "unsecure": { "type": "boolean" },
"challengeExpiration": { "type": "integer" }, "challengeExpiration": { "type": "integer" },
"keepAliveInterval": { "type": "string" }, "keepAliveInterval": { "type": "number" },
"keepAliveTimeout": { "type": "string" } "keepAliveTimeout": { "type": "number" },
"refreshSessionInterval": { "type": "number" }
}, },
"required": [ "required": [
"challengeExpiration", "challengeExpiration",
+13 -8
View File
@@ -31,21 +31,26 @@ const mysqlCreds = {
// host: '127.0.0.1', // host: '127.0.0.1',
// port: 3306, // port: 3306,
socketPath: '/var/run/mysqld/mysqld.sock', socketPath: '/var/run/mysqld/mysqld.sock',
user: 'p42', user: process.env.mysql_user,
password: 'C3h=V9!r>Mvc>skxPf9?W2P3duJTk', password: process.env.mysql_pass,
database: 'p42', database: 'p42GUI',
waitForConnections: true, waitForConnections: true,
connectionLimit: 10, connectionLimit: 10,
queueLimit: 0 queueLimit: 0
} }
if(!mysqlCreds.user || !mysqlCreds.password) {
console.error('Missing MySQL credentials: set mysql_user and mysql_pass in environment')
process.exit(1)
}
const db = await mysql.createConnection(mysqlCreds) const db = await mysql.createConnection(mysqlCreds)
const sessionStore = new MySQLStore({ const sessionStore = new MySQLStore({
createDatabaseTable: false, createDatabaseTable: false,
clearExpired: true, clearExpired: true,
schema: { schema: {
tableName: 'p42_sessions', tableName: 'sessions',
columnNames: { columnNames: {
session_id: 'session_id', session_id: 'session_id',
expires: 'expires', expires: 'expires',
@@ -84,12 +89,12 @@ async function startAllRedis(wssGatewayConfig) {
) )
const loginResults = await Promise.allSettled( const loginResults = await Promise.allSettled(
redisConns.map(async cnx => { redisConns.map(async cnx => {
cnx.redisLogin() await cnx.redisLogin()
return cnx.redisId return cnx.redisId
}) })
) )
2. //make sure all connected before going any further //2. make sure all connected before going any further
const failedLogin = loginResults.filter(r => r.status !== 'fulfilled') const failedLogin = loginResults.filter(r => r.status !== 'fulfilled')
if (failedLogin.length > 0) { if (failedLogin.length > 0) {
console.error('Redis login failures:') console.error('Redis login failures:')
@@ -107,7 +112,7 @@ async function startAllRedis(wssGatewayConfig) {
// --- Phase 2: start channels for all (since all succeeded) // --- Phase 2: start channels for all (since all succeeded)
const chanResults = await Promise.allSettled( const chanResults = await Promise.allSettled(
redisConns.map(async cnx => { redisConns.map(async cnx => {
cnx.redisChansStart() await cnx.redisChansStart()
return cnx.redisId return cnx.redisId
}) })
) )
@@ -172,7 +177,7 @@ cfgh.fetchConfig().then( async wssGatewayConfig => {
return return
} }
WSSServer.handleUpgrade(req, socket, head, (ws) => { WSSServer.handleUpgrade(req, socket, head, (ws) => {
ws.session = req.session // direct access to Express session ws.session = req.session // Caution : that one is turned in stone, not alive !
WSSServer.emit('connection', ws, req) WSSServer.emit('connection', ws, req)
}) })
}) })
+5 -1
View File
@@ -1,11 +1,15 @@
#!/bin/sh #!/bin/sh
set -a
. /etc/p42/secrets.env
set +a
cd /opt/p42wssGateway/ cd /opt/p42wssGateway/
pid=`ps -ef | grep p42wssGateway |grep -v grep | awk '{print $2}'` pid=`ps -ef | grep p42wssGateway |grep -v grep | awk '{print $2}'`
if [ -z "$pid" ] if [ -z "$pid" ]
then then
node p42wssGateway.js > wssGateway.log 2>&1 & node p42wssGateway.js --debug > wssGateway.log 2>&1 &
else else
echo '' echo ''
echo 'Already running PID='"$pid"' (use stopWssGw.sh to stop it)' echo 'Already running PID='"$pid"' (use stopWssGw.sh to stop it)'
+8 -1
View File
@@ -17,6 +17,7 @@ export class WssConnexion {
this.userId = options.userId this.userId = options.userId
this.roles = options.roles this.roles = options.roles
this.sessionID = null // null until login this.sessionID = null // null until login
this.roundTripTime = 0
this.subscriptions = []; this.subscriptions = [];
this.usersWatched = []; this.usersWatched = [];
@@ -58,7 +59,7 @@ export class WssConnexion {
} }
if(typeof this['action_'+action] == "function") { if(typeof this['action_'+action] == "function") {
if((this.debug) && (action != 'PONG')) console.warn(`${action} for uuid ${this.uuid}`); if((this.debug) && (action != 'PONG rcv')) console.warn(`${action} for uuid ${this.uuid}`);
this['action_'+action](action, payload, reqid); this['action_'+action](action, payload, reqid);
} else { } else {
if(this.debug) console.warn(`Unknown action ${action} for UUID ${this.uuid}`); if(this.debug) console.warn(`Unknown action ${action} for UUID ${this.uuid}`);
@@ -72,9 +73,15 @@ export class WssConnexion {
keepAlive(){ keepAlive(){
this.latestPing = {
id: crypto.randomUUID(),
time: Date.now()
}
this.send(JSON.stringify({ this.send(JSON.stringify({
'action': 'PING', 'action': 'PING',
'reqid': this.latestPing.id,
})); }));
console.log('====>PING sent', this.latestPing)
this.keepAliveBomb = setTimeout(this.MissedKeepAlive.bind(this), (this.config.server.keepAliveTimeout+1)*1000); this.keepAliveBomb = setTimeout(this.MissedKeepAlive.bind(this), (this.config.server.keepAliveTimeout+1)*1000);
} }
+8 -14
View File
@@ -4,8 +4,9 @@
"listenHost": "127.0.0.1", "listenHost": "127.0.0.1",
"listenPort": 3999, "listenPort": 3999,
"listenPath": "/msgbus", "listenPath": "/msgbus",
"keepAliveInterval": "30", "keepAliveInterval": 30,
"keepAliveTimeout": "5", "keepAliveTimeout": 5,
"refreshSessionInterval": 600,
"XXcertFile": "/etc/letsencrypt/live/42.internike.com/fullchain.pem", "XXcertFile": "/etc/letsencrypt/live/42.internike.com/fullchain.pem",
"XXcertKeyFile": "/etc/letsencrypt/live/42.internike.com/privkey.pem", "XXcertKeyFile": "/etc/letsencrypt/live/42.internike.com/privkey.pem",
"challengeExpiration": 20, "challengeExpiration": 20,
@@ -14,20 +15,13 @@
"accessRights":[ "accessRights":[
{ {
"roles": "*", "roles": "*",
"mustSubscribe": [ "system:notifs:[UID]", "system:notifs" ], "mustSubscribe": [ "system:notifs:[UID]", "system:notifs", "system:replies:[UID]" ],
"canSubscribe": ["system:gps:*", "arena:gps:*","arena:agents:*"], "canSubscribe": ["system:gps:*", "arena:gps:*","arena:agents:*",
"canPublish": [ ], "system:observer:subscribed[UID]:agents",
"system:maestro:lifecycle:[UID]"],
"canPublish": [ "system:requests:*", "system:observer:requests"],
"canSet": [ ], "canSet": [ ],
"canGet": [ ] "canGet": [ ]
},
{
"roles": ["admin"],
"mustSubscribe": ["system:infraNotifs", "system:replies:[UID]"],
"canSubscribe": [ ],
"canPublish": [ "system:requests:*" ],
"canSet": ["system:*"],
"canGet": ["system:*"],
"canDo": ["getActiveUsers", "reloadAccessRights", "getAccessRights"]
} }
], ],
"redis":[ "redis":[
+7 -7
View File
@@ -92,13 +92,13 @@ export class wssServer {
return(OnlineUsers); return(OnlineUsers);
} }
sessionConnected(sessionID){ // sessionConnected(sessionID){
if(!sessionID) return(false) // If that cnx is not finished login-in // if(!sessionID) return(false) // If that cnx is not finished login-in
for(let uuid in this.AllWssConnections) { // for(let uuid in this.AllWssConnections) {
if(this.AllWssConnections[uuid].sessionID==sessionID) return(true) // if(this.AllWssConnections[uuid].sessionID==sessionID) return(true)
} // }
return(false) // return(false)
} // }
async reloadAccessRights() { async reloadAccessRights() {
await this.configHelper.refreshAccessRights() await this.configHelper.refreshAccessRights()