From 17e05130e32c934101eb8bb21f8e9294db5d9852 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Tue, 6 Dec 2022 17:53:50 -0500 Subject: [PATCH] Need a nasty little handler to catch people (reasonably so) typing in localhost instead of 127.0.0.1. This is needed because socket.io defaults to IPVv6 on Node 18 for some reason, and probably others. --- commands/api.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/commands/api.js b/commands/api.js index d642d12..aecba0e 100644 --- a/commands/api.js +++ b/commands/api.js @@ -13,6 +13,7 @@ import devtools from '../lib/devtools.js'; import * as acorn from "acorn"; import fs from "fs"; import cors from "cors"; +import { base_host } from "../client/config.js"; import { createHttpTerminator } from 'http-terminator'; const app = express(); @@ -96,6 +97,21 @@ export const main = async (opts) => { if(opts.DANGER_ADMIN) { const { media_servers } = await import("../lib/config.js"); app.use(cors({origin: media_servers})); + + // this is necessary to deal with Node 18 defaulting to IPv6 for "localhost" + app.use((req, res, next) => { + const full_host = `${req.protocol}://${req.get('host')}`; + + if(full_host !== base_host) { + const redirect_to = new URL(req.url, base_host); + + log.error(`Your client/config.js:base_host is ${base_host} but you are accessing the site at ${full_host}. Redirecting to ${redirect_to} so cookies will function!`); + + res.redirect(redirect_to); + } else { + next(); + } + }) } if(opts.debugHeaders) {