feat: add reverse proxy support with configurable base path and trust proxy settings (#47)

This commit is contained in:
M Firdaus Riawan 2025-09-02 19:32:55 +07:00 • committed by GitHub
commit 62860a658c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 204 additions and 9 deletions

View file

@ -1,14 +1,23 @@
require('./routes')
const express = require('express')
const { routes } = require('./routes')
const { maxAttachmentSize } = require('./config')
const { maxAttachmentSize, basePath, trustProxy } = require('./config')
const app = express()
// Initialize Express app
app.disable('x-powered-by')
// Configure trust proxy for reverse proxy compatibility
if (trustProxy) {
app.set('trust proxy', true)
}
app.use(express.json({ limit: maxAttachmentSize + 1000000 }))
app.use(express.urlencoded({ limit: maxAttachmentSize + 1000000, extended: true }))
app.use('/', routes)
// Mount routes with configurable base path
const mountPath = basePath || '/'
app.use(mountPath, routes)
module.exports = app

View file

@ -22,6 +22,8 @@ const logLevel = process.env.LOG_LEVEL || 'info'
const enableWebHook = process.env.ENABLE_WEBHOOK ? (process.env.ENABLE_WEBHOOK).toLowerCase() === 'true' : true
const enableWebSocket = process.env.ENABLE_WEBSOCKET ? (process.env.ENABLE_WEBSOCKET).toLowerCase() === 'true' : false
const autoStartSessions = process.env.AUTO_START_SESSIONS ? (process.env.AUTO_START_SESSIONS).toLowerCase() === 'true' : true
const basePath = process.env.BASE_PATH || ''
const trustProxy = process.env.TRUST_PROXY ? (process.env.TRUST_PROXY).toLowerCase() === 'true' : false
module.exports = {
sessionFolderPath,
@ -43,5 +45,7 @@ module.exports = {
logLevel,
enableWebHook,
enableWebSocket,
autoStartSessions
autoStartSessions,
basePath,
trustProxy
}

View file

@ -72,7 +72,11 @@ const sessionValidation = async (req, res, next) => {
const rateLimiter = rateLimiting({
limit: rateLimitMax,
windowMs: rateLimitWindowMs,
message: "You can't make any more requests at the moment. Try again later"
message: "You can't make any more requests at the moment. Try again later",
// Use real client IP when behind reverse proxy
keyGenerator: (req) => {
return req.ip || req.connection.remoteAddress
}
})
const sessionSwagger = async (req, res, next) => {

View file

@ -1,5 +1,5 @@
const { WebSocketServer } = require('ws')
const { enableWebSocket } = require('./config')
const { enableWebSocket, basePath } = require('./config')
const { logger } = require('./logger')
const wssMap = new Map()
@ -52,10 +52,15 @@ const triggerWebSocket = (sessionId, dataType, data) => {
}
const handleUpgrade = (request, socket, head) => {
const baseUrl = 'ws://' + request.headers.host + '/'
const host = request.headers['x-forwarded-host'] || request.headers.host
const baseUrl = 'ws://' + host + '/'
const { pathname } = new URL(request.url, baseUrl)
if (pathname.startsWith('/ws/')) {
const sessionId = pathname.split('/')[2]
// Handle base path for WebSocket connections
const wsPath = basePath ? `${basePath}/ws/` : '/ws/'
if (pathname.startsWith(wsPath)) {
const pathParts = pathname.split('/')
const sessionId = basePath ? pathParts[3] : pathParts[2]
const server = wssMap.get(sessionId)
if (server) {
server.handleUpgrade(request, socket, head, (ws) => {