Repository navigation
fix(webui): Proxy socket.io requests when localhost resolves to multiple addresses (fixes #1062). #1078
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(webui): Proxy socket.io requests when localhost resolves to multiple addresses (fixes #1062). #1078
Changes from 4 commits
12c2304
5e40873
4067548
f252baf
b28de19
96824c8
16d9769
4bb266b
2bd7696
b18ace4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,6 +3,10 @@ | |||||||||||||||||
| // TODO: Move listeners to a separate file to reduce lines | ||||||||||||||||||
| // Reference: https://github.com/socketio/socket.io/blob/main/examples/basic-crud-application/server/lib/todo-management/todo.handlers.ts | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| import {lookup as dnsLookup} from "node:dns"; | ||||||||||||||||||
|
|
||||||||||||||||||
| import fastifyHttpProxy from "@fastify/http-proxy"; | ||||||||||||||||||
| import { | ||||||||||||||||||
| FastifyInstance, | ||||||||||||||||||
| FastifyPluginAsync, | ||||||||||||||||||
|
|
@@ -77,6 +81,30 @@ class MongoSocketIoServer { | |||||||||||||||||
| InterServerEvents, | ||||||||||||||||||
| SocketData | ||||||||||||||||||
| >(fastify.server); | ||||||||||||||||||
|
|
||||||||||||||||||
| // Fastify listens on all resolved addresses for localhost (e.g. `127.0.0.1`, `::1`), but | ||||||||||||||||||
| // socket.io only uses the main one. When multiple addresses are resolved, we proxy | ||||||||||||||||||
|
junhaoliao marked this conversation as resolved.
Resolved
|
||||||||||||||||||
| // socket.io requests from secondary addresses to the main server. | ||||||||||||||||||
| dnsLookup(fastify.config.HOST, {all: true}, (err, addresses) => { | ||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could potentially do this with fastify.addresses property instead, but you would need to call in the main.ts file and after fastify.listen(after plugin registration). Overall I think the approach you took where it is done in the this plugin is better.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right, i believe the Fastify addresses info isn't available until |
||||||||||||||||||
| if (null !== err) { | ||||||||||||||||||
| fastify.log.error(`DNS lookup failed: ${err.message}`); | ||||||||||||||||||
|
|
||||||||||||||||||
| return; | ||||||||||||||||||
| } | ||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||||||||||||||||||
|
|
||||||||||||||||||
| if (1 < addresses.length) { | ||||||||||||||||||
|
junhaoliao marked this conversation as resolved.
Resolved
|
||||||||||||||||||
| fastify.log.warn( | ||||||||||||||||||
| `Multiple addresses found for ${fastify.config.HOST}: ${ | ||||||||||||||||||
| JSON.stringify(addresses)}. ` + | ||||||||||||||||||
| "Proxying all socket.io requests to the main server." | ||||||||||||||||||
|
Comment on lines
+106
to
+109
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick (assertive) Use a single template literal – current string concatenation violates style guide Static analysis is flagging the - fastify.log.warn(
- `Multiple addresses found for ${fastify.config.HOST}: ${
- JSON.stringify(addresses)}. ` +
- "Proxying all socket.io requests to the main server."
- );
+ fastify.log.warn(
+ `Multiple addresses found for ${fastify.config.HOST}: ${JSON.stringify(addresses)}. `
+ + "Proxying all socket.io requests to the main server."
+ );📝 Committable suggestion
Suggested change
🧰 Tools🪛 Biome (1.9.4)[error] 106-108: Template literals are preferred over string concatenation. Unsafe fix: Use a template literal. (lint/style/useTemplate) 🤖 Prompt for AI Agents
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it was broken down to avoid line length violation of max 100 characters
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @junhaoliao I understand - you're balancing the template literal preference with the 100-character line limit. Here's a solution that satisfies both constraints using a multi-line template literal: fastify.log.warn(
`Multiple addresses found for ${fastify.config.HOST}: ${JSON.stringify(addresses)}. ` +
`Proxying all socket.io requests to the main server.`
);This keeps each line under 100 characters while using template literals throughout, which should satisfy both the linter and the line length constraint. ✏️ Learnings added
🧠 Learnings used |
||||||||||||||||||
| ); | ||||||||||||||||||
| fastify.register(fastifyHttpProxy, { | ||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you may beable to get https/http from fastify.initialConfig.https, so not hardcoded at http
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we don't support HTTPS cert configuration in the Fastify server yet so this may not matter. still, i made the change to show the consideration. until we add HTTPS support in the fastify server and disable HTTP when deployment maintainers configure so, it's recommended to protect the HTTP server with an HTTPS-enabled Nginx reverse proxy |
||||||||||||||||||
| upstream: `http://${fastify.config.HOST}:${fastify.config.PORT}/socket.io/`, | ||||||||||||||||||
| prefix: "/socket.io/", | ||||||||||||||||||
| }); | ||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||||||||||||||||||
| } | ||||||||||||||||||
| }); | ||||||||||||||||||
|
|
||||||||||||||||||
| this.#registerEventListeners(); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.