Skip to content

Commit

Permalink
Extend std msg._uib to incl page name
Browse files Browse the repository at this point in the history
  • Loading branch information
TotallyInformation committed Nov 30, 2022
1 parent 63d7f6e commit be616ed
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 20 deletions.
23 changes: 18 additions & 5 deletions nodes/libs/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ function getClientRealIpAddress(socket) {
return clientRealIpAddress
} // --- End of getClientRealIpAddress --- //

/** Get client real ip address - NB: Optional chaining (?.) is node.js v14 not v12
* @param {socketio.Socket} socket Socket.IO socket object
* @returns {string | string[] | undefined} Best estimate of the client's real IP address
*/
function getClientPageName(socket, node) {
let pageName = socket.handshake.auth.pathName.replace(`/${node.url}/`, '')
if ( pageName.endsWith('/') ) pageName += 'index.html'
if ( pageName === '' ) pageName = 'index.html'

return pageName
} // --- End of getClientPageName --- //



class UibSockets {
// TODO: Replace _XXX with #XXX once node.js v14 is the minimum supported version
/** Flag to indicate whether setup() has been run
Expand Down Expand Up @@ -378,11 +392,12 @@ class UibSockets {
// If the sender hasn't added msg._socketId, add the Socket.id now
if ( !Object.prototype.hasOwnProperty.call(msg, '_socketId') ) msg._socketId = socket.id

// If required, add the clientId and real IP to the msg
if (node.showClientId) {
// If required, add the clientId, page name and real IP to the msg using msg._uib
if (node.showMsgUib) {
if (!msg._uib) msg._uib = {}
msg._uib.clientId = socket.handshake.auth.clientId
msg._uib.remoteAddress = getClientRealIpAddress(socket)
msg._uib.pageName = getClientPageName(socket, node)
}

// Send out the message for downstream flows
Expand Down Expand Up @@ -446,9 +461,7 @@ class UibSockets {
// Note, could use socket.handshake.auth.pageName instead
let pageName
if ( socket.handshake.auth.pathName ) {
pageName = socket.handshake.auth.pathName.replace(`/${node.url}/`, '')
if ( pageName.endsWith('/') ) pageName += 'index.html'
if ( pageName === '' ) pageName = 'index.html'
pageName = getClientPageName(socket, node)
}

//#region ----- Event Handlers ----- //
Expand Down
6 changes: 3 additions & 3 deletions nodes/uibuilder.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion nodes/uibuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ function nodeInstance(config) {
this.reload = config.reload === undefined ? false : config.reload
this.sourceFolder = config.sourceFolder // NB: Do not add a default here as undefined triggers a check for index.html in web.js:setupInstanceStatic
this.deployedVersion = config.deployedVersion
this.showClientId = config.showClientId
this.showMsgUib = config.showMsgUib // Show additional client id in standard msgs (see socket.js)
//#endregion ====== Local node config copy ====== //

log.trace(`[uibuilder:nodeInstance:${this.url}] ================ instance registered ================`)
Expand Down
4 changes: 2 additions & 2 deletions src/editor/uibuilder/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -1478,7 +1478,7 @@
$('#node-input-allowUnauth').prop('checked', node.allowUnauth)
$('#node-input-tokenAutoExtend').prop('checked', node.tokenAutoExtend)
$('#node-input-reload').prop('checked', node.reload)
$('#node-input-showClientId').prop('checked', node.showClientId)
$('#node-input-showMsgUib').prop('checked', node.showMsgUib)
}

/** Show what server is in use
Expand Down Expand Up @@ -2027,7 +2027,7 @@
reload: { value: false }, // If true, all connected clients will be reloaded if a file is changed on the edit screens
sourceFolder: { value: 'src', required: true, }, // Which folder to use for front-end code? (src or dist)
deployedVersion: { validate: validateVersion },
showClientId: { value: false }, // Show msg._uib.clientId & real IP in standard msgs
showMsgUib: { value: false }, // Show msg._uib in standard msgs (client id, ip, page name)
},
credentials: {
jwtSecret: { type: 'password' }, // text or password
Expand Down
4 changes: 2 additions & 2 deletions src/editor/uibuilder/editor.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/editor/uibuilder/help.html
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ <h4>Advanced Settings</h4>
This can now be changed simply by redeploying your flow, you don't need to restart Node-RED.
</dd>

<dt>Include <code>msg._uib.clientId</code> in standard msg output <span class="property-type">check box</span></dt>
<dt>Include <code>msg._uib</code> in standard msg output <span class="property-type">check box</span></dt>
<dd>
If selected, will show the browser clientId and real IP address in standard output (it is always shown in control msgs).
If selected, will show the browser clientId, page name and real IP address in standard output (it is always shown in control msgs).
</dd>

<dt>Show web view of source files <span class="property-type">check box</span></dt>
Expand Down
11 changes: 6 additions & 5 deletions src/editor/uibuilder/panel.html
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,13 @@ <h4>Template Settings</h4>
Need to re-deploy for the folder to change.
</div>

<div aria-label="Pass the client id and ip to standard message output?" class="form-row"
title="If turned on, msg._uib.clientId and real ip addr is included in standard msgs. (msg.clientId and IP are always included in control messages)">
<input type="checkbox" id="node-input-showClientId" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-showClientId" style="width: 90%;">Include <code>msg._uib.clientId|remoteAddress</code> in standard msg output.</label>
<div aria-label="Pass the msg._uib to standard message output?" class="form-row"
title="If turned on, msg._uib included in standard msgs">
<input type="checkbox" id="node-input-showMsgUib" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-showMsgUib" style="width: 90%;">Include <code>msg._uib</code> in standard msg output.</label>
</div>
<div aria-label="clientId information" class="form-row form-tips node-help">
<div aria-label="msg._uib information" class="form-row form-tips node-help">
This includes the <code>clientId</code>, <code>remoteAddress</code> and <code>pageName</code> properties from the client in the output<br>
The <code>clientId</code> is generated in the browser and remains stable while the browser is open.
It is reset when the browser restarts. Control msgs always include <code>msg.clientId</code>.
</div>
Expand Down

0 comments on commit be616ed

Please sign in to comment.