Version 1.0 del totem en el sistema bascula autonoma
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* Template Socket.IO Connection Middleware for uibuilder.
|
||||
* UPDATED: 2022-04-01
|
||||
*
|
||||
* NOTES & WARNINGS:
|
||||
* 1) This function is only called ONCE - when a new client connects. So any authentication/security processing is limited
|
||||
* because you cannot use this to, for example, timeout/extend a session without further server processing of incoming messages.
|
||||
* However, see the sioUse.js and sioMsgOut.js middlewares for per-msg handling.
|
||||
* 2) Failing to either return or call `next()` will mean that your clients will never connect.
|
||||
* 3) An error in this function will probably cause Node-RED to fail to start at all.
|
||||
* 4) You have to restart Node-RED if you change this file.
|
||||
* 5) To use for authentication/authorisation with Express and sio connection middleware, create a common node.js module.
|
||||
*
|
||||
* Allows custom processing for authentication, session management, connection validation, logging, rate limiting, etc.
|
||||
*
|
||||
* see also: uibRoot/.config/sioUse.js & sioMsgOut.js
|
||||
* https://cheatsheetseries.owasp.org/cheatsheets/HTML5_Security_Cheat_Sheet.html#websocket-implementation-hints
|
||||
*
|
||||
* @param {*} socket Socket.IO socket object
|
||||
* @param {function} next The callback to hand off to the next middleware
|
||||
*/
|
||||
function sioMw(socket, next) {
|
||||
// Some SIO related info that might be useful in security checks
|
||||
console.log('== [sioMiddleware.js] ====================')
|
||||
console.log('New client connected to Namespace')
|
||||
console.log('--socket.request.connection.remoteAddress--', socket.request.connection.remoteAddress)
|
||||
console.log('--socket id--', socket.id)
|
||||
// Added by uibuilder when Namespace is created by uibuilder node instance - Note also that socket.nsp.log can be used to output to the Node-RED log
|
||||
console.log('--socket namespace metadata (server)--', {
|
||||
name: socket.nsp.name,
|
||||
url: socket.nsp.url,
|
||||
nodeId: socket.nsp.nodeId,
|
||||
useSecurity: socket.nsp.useSecurity,
|
||||
})
|
||||
//console.log('--socket handshake--', socket.handshake)
|
||||
//console.log('--socket properties--', Object.keys(socket))
|
||||
// Show the client id (set by uibuilder ExpressJS middleware)
|
||||
console.log('--client id handshake.auth--', socket.handshake.auth.clientId)
|
||||
console.log('--client id in custom header (polling only)--', socket.handshake.headers['x-clientid'])
|
||||
console.log('==========================================\n ')
|
||||
|
||||
// Simplistic auth example
|
||||
let auth = true
|
||||
if (auth !== true) {
|
||||
socket.nsp.log.error(`[uibuilder:sioMiddleware.js] - Authentication error, client disconnected - ID: ${socket.id}`)
|
||||
return next (new Error(`[uibuilder:sioMiddleware.js] - Authentication error, client disconnected - ID: ${socket.id}` ))
|
||||
}
|
||||
|
||||
return next()
|
||||
|
||||
} // Remember to end with a `next()` statement or nothing will work.
|
||||
|
||||
module.exports = sioMw
|
||||
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* Template Socket.IO outbound per-msg middleware for uibuilder. Fn will be called for EVERY outbound msg from Node-RED/uibuilder to a client.
|
||||
* UPDATED: 2022-04-01
|
||||
*
|
||||
* NOTES & WARNINGS:
|
||||
* 1) This function is called whenever any instance of uibuilder sends a msg to any client.
|
||||
* 2) You have to restart Node-RED if you change this file.
|
||||
* 3) You can use this to make changes to the msg before it is sent.
|
||||
*
|
||||
* Allows you to process outgoing data to clients. Use it to add security/user data or anything else.
|
||||
*
|
||||
* @param {object} msg The msg being seny by uibuilder to a client
|
||||
* @param {string} url The uibuilder instance url
|
||||
* @param {string} channel The socket.io channel being used
|
||||
*/
|
||||
function sioMsgOutMw( msg, url, channel ) {
|
||||
|
||||
console.log('[uibuilder:Socket.IO:sioMsgOut.js] msg from server: ', msg, url, channel)
|
||||
|
||||
}
|
||||
|
||||
module.exports = sioMsgOutMw
|
||||
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* Template Socket.IO `use` middleware for uibuilder. Fn will be called for EVERY inbound msg from a client to Node-RED/uibuilder.
|
||||
* UPDATED: 2022-04-01
|
||||
*
|
||||
* NOTES & WARNINGS:
|
||||
* 1) This function is called when a client sends a "packet" of data to the server.
|
||||
* 2) Failing to either return or call `next()` will mean that your clients will never be able to get responses.
|
||||
* 3) You can amend the incoming msg in this middleware.
|
||||
* 4) An error in this function will probably cause Node-RED to fail to start at all.
|
||||
* 5) You have to restart Node-RED if you change this file.
|
||||
* 6) If you call `next( new Error('blah') )` The error is sent back to the client and further proessing of the incoming msg stops.
|
||||
* 7) To use for authentication/authorisation with Express and sio connection middleware, create a common node.js module.
|
||||
*
|
||||
* Allows you to process incoming data from clients.
|
||||
*
|
||||
* see: https://socket.io/docs/v4/server-api/#socketusefn
|
||||
* see also: uibRoot/.config/sioMiddleware.js & sioMsgOut.js
|
||||
* and https://cheatsheetseries.owasp.org/cheatsheets/HTML5_Security_Cheat_Sheet.html#websocket-implementation-hints
|
||||
*
|
||||
* @param {[string,Array<Object>]} data The channel name (strictly the event name) and args send by a client (Socket.IO calls it a "packet"). data[0] is the channel/event name, data[args][0] is the actual msg
|
||||
* @param {function} next The callback to hand off to the next middleware
|
||||
*/
|
||||
function sioUseMw([ channel, ...args ], next) {
|
||||
|
||||
const msg = args[0]
|
||||
|
||||
console.log('[uibuilder:Socket.IO:sioUse.js] msg from client: ', 'Channel Name:', channel, ' Msg:', msg)
|
||||
|
||||
// Simplistic error example - looking for specific property on the inbound msg
|
||||
if ( msg.i_am_an_error ) {
|
||||
// The error is sent back to the client and further processing of the msg stops
|
||||
next(new Error('Oops! Some kind of error happened'))
|
||||
return
|
||||
}
|
||||
|
||||
// You can amend the incoming msg
|
||||
msg._test = 'added by sioUse.js middleware'
|
||||
|
||||
next()
|
||||
|
||||
} // Do not forget to end with a call to `next()` or clients will not be able to connect
|
||||
|
||||
module.exports = sioUseMw
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Template ExpressJS Middleware for uibuilder.
|
||||
* UPDATED: 2022-04-01
|
||||
*
|
||||
* NOTES & WARNINGS:
|
||||
* 1) This function is called EVERY TIME any web call is made to the URL defined by your uib instance.
|
||||
* So it should be kept short and efficient.
|
||||
* 2) Failing to either return or call `next()` will cause an ExpressJS error.
|
||||
* 3) An error in this function will probably cause Node-RED to fail to start at all.
|
||||
* 4) You have to restart Node-RED if you change this file.
|
||||
* 5) To use for authentication/authorisation with sio connection middleware, create a common node.js module.
|
||||
*
|
||||
* Allows custom processing for authentication, session management, custom logging, etc.
|
||||
*
|
||||
* @param {object} req The ExpressJS request object
|
||||
* @param {object} res The ExpressJS result object
|
||||
* @param {function} next The callback to hand off to the next middleware
|
||||
*/
|
||||
function uibMw(req,res,next) {
|
||||
|
||||
console.log('[uibuilder:uibMiddleware.js] Custom ExpressJS middleware called.')
|
||||
next()
|
||||
|
||||
} // Do not forget to end with a call to `next()`
|
||||
|
||||
module.exports = uibMw
|
||||
Reference in New Issue
Block a user