From b8599ba3d2ed0186ed9d928675892e7ff21b56ba Mon Sep 17 00:00:00 2001 From: Ben Hardill Date: Tue, 5 Dec 2023 14:05:50 +0000 Subject: [PATCH 1/3] Enable affinity part of FlowFuse/flowfuse#2514 Reuses the affinity cookie if present, shares the affinity cookie with the forge platform if available. --- lib/editor/tunnel.js | 20 ++++++++++++++++++-- lib/mqtt.js | 2 +- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/editor/tunnel.js b/lib/editor/tunnel.js index 3e48d48..7c89be8 100644 --- a/lib/editor/tunnel.js +++ b/lib/editor/tunnel.js @@ -22,6 +22,7 @@ class EditorTunnel { this.port = config.port this.config = config this.options = options || {} + this.affinity = undefined // How long to wait before attempting to reconnect. Start at 500ms - back // off if connect fails @@ -57,9 +58,24 @@ class EditorTunnel { info(`Connecting editor tunnel to ${forgeWSEndpoint}`) // * Enable Device Editor (Step 8) - (device->forge:WS) Initiate WS connection (with token) + const headers = { + 'x-access-token': this.options.token + } + if (this.affinity) { + headers.cookie = `INGRESSCOOKIE=${this.affinity}` + } const socket = newWsConnection(forgeWSEndpoint, { - headers: { - 'x-access-token': this.options.token + headers + }) + socket.on('upgrade', (evt) => { + if (evt.headers && evt.headers['set-cookie']) { + const cookies = evt.headers['set-cookie'] + cookies.forEach(cookie => { + const parts = cookie.split(';')[0].split(['=']) + if (parts === 'INGRESSCOOKIE') { + this.affinity = parts[1] + } + }) } }) socket.onopen = (evt) => { diff --git a/lib/mqtt.js b/lib/mqtt.js index 5915fd6..ec41e80 100644 --- a/lib/mqtt.js +++ b/lib/mqtt.js @@ -115,7 +115,7 @@ class MQTTClient { // * Enable Device Editor (Step 7) - (device) Begin the device tunnel connect process const result = await this.tunnel.connect() // * Enable Device Editor (Step 10) - (device->forge:MQTT) Send a response to the platform - this.sendCommandResponse(msg, { connected: result, token: msg?.payload?.token }) + this.sendCommandResponse(msg, { connected: result, token: msg?.payload?.token, affinity: this.tunnel.affinity }) this.sendStatus() return } else if (msg.command === 'stopEditor') { From c6c5927442c3239840d1b294f8c2cdf68c537c27 Mon Sep 17 00:00:00 2001 From: Ben Hardill Date: Mon, 8 Jan 2024 17:26:51 +0000 Subject: [PATCH 2/3] change COOKIE name --- lib/editor/tunnel.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/editor/tunnel.js b/lib/editor/tunnel.js index 7c89be8..06b1d76 100644 --- a/lib/editor/tunnel.js +++ b/lib/editor/tunnel.js @@ -62,7 +62,7 @@ class EditorTunnel { 'x-access-token': this.options.token } if (this.affinity) { - headers.cookie = `INGRESSCOOKIE=${this.affinity}` + headers.cookie = `FFSESSION=${this.affinity}` } const socket = newWsConnection(forgeWSEndpoint, { headers @@ -72,7 +72,7 @@ class EditorTunnel { const cookies = evt.headers['set-cookie'] cookies.forEach(cookie => { const parts = cookie.split(';')[0].split(['=']) - if (parts === 'INGRESSCOOKIE') { + if (parts === 'FFSESSION') { this.affinity = parts[1] } }) From 630656f04d96c0eaa4df41a8e61c0fd9e932a9dd Mon Sep 17 00:00:00 2001 From: Ben Hardill Date: Tue, 16 Jan 2024 09:46:15 +0000 Subject: [PATCH 3/3] Test if set-cookie is an array --- lib/editor/tunnel.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/editor/tunnel.js b/lib/editor/tunnel.js index 06b1d76..464eeaa 100644 --- a/lib/editor/tunnel.js +++ b/lib/editor/tunnel.js @@ -70,12 +70,19 @@ class EditorTunnel { socket.on('upgrade', (evt) => { if (evt.headers && evt.headers['set-cookie']) { const cookies = evt.headers['set-cookie'] - cookies.forEach(cookie => { - const parts = cookie.split(';')[0].split(['=']) + if (Array.isArray(cookies)) { + cookies.forEach(cookie => { + const parts = cookie.split(';')[0].split(['=']) + if (parts === 'FFSESSION') { + this.affinity = parts[1] + } + }) + } else { + const parts = cookies.split(';')[0].split(['=']) if (parts === 'FFSESSION') { this.affinity = parts[1] } - }) + } } }) socket.onopen = (evt) => {