Client IP address

Hi,

Quick question: is it possible to get the client IP address from a Dashboard user (DB instance under FlowFuse)?

Thanks!

Not sure about D2 but it should be available when the client connects shouldn't it?

We don't explicitly expose it anywhere, i.e. as part of the msg._client - however we could fairly easily. It may already be available client-side inside the this.$socket object within a ui-template, but that would be constrained only to that particular node.

1 Like

If you want to adopt it, you should probably note that getting it isn't quite as straightforward as it first seems.

Here is the code from uibuilder's socket library for example:

/** 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 getClientRealIpAddress(socket) {
    let clientRealIpAddress
    if ( 'headers' in socket.request && 'x-real-ip' in socket.request.headers) {
        // get ip from behind a nginx proxy or proxy using nginx's 'x-real-ip header
        clientRealIpAddress = socket.request.headers['x-real-ip']
    } else if ( 'headers' in socket.request && 'x-forwarded-for' in socket.request.headers) {
        // else get ip from behind a general proxy
        if (socket.request.headers['x-forwarded-for'] === undefined) throw new Error('socket.request.headers["x-forwarded-for"] is undefined')
        if (!Array.isArray(socket.request.headers['x-forwarded-for'])) socket.request.headers['x-forwarded-for'] = [socket.request.headers['x-forwarded-for']]
        clientRealIpAddress = socket.request.headers['x-forwarded-for'][0].split(',').shift()
    } else if ( 'connection' in socket.request && 'remoteAddress' in socket.request.connection ) {
        // else get ip from socket.request that returns the reference to the request that originated the underlying engine.io Client
        clientRealIpAddress = socket.request.connection.remoteAddress
    } else {
        // else get ip from socket.handshake that is a object that contains handshake details
        clientRealIpAddress = socket.handshake.address
    }

    return clientRealIpAddress
} // --- End of getClientRealIpAddress --- //

And even so, it is as likely to return an IPv6 address which isn't always as useful as an IPv4.

1 Like

If it's not too much trouble, it is a request our SOC has made.
I'd be happy to create a github issue for this?

Thanks!

Please do

Done: Add client IP address in msg._client.user object · Issue #694 · FlowFuse/node-red-dashboard · GitHub

Thanks! :slight_smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.