@@ -6,8 +6,9 @@ import { WebApp } from 'meteor/webapp'
66import { Meteor } from 'meteor/meteor'
77import { getRandomString } from '@sofie-automation/corelib/dist/lib'
88import { stringifyError } from '@sofie-automation/shared-lib/dist/lib/stringifyError'
9- import _ from 'underscore'
109import { getRootSubpath , public_dir } from '../../lib'
10+ import { getClientAddress } from '../../lib/clientAddress'
11+ import { STANDALONE_DDP_SERVER_ENABLED , STANDALONE_DDP_SERVER_PATH } from '../../ddp-server/config'
1112import staticServe from 'koa-static'
1213import { logger } from '../../logging'
1314import { PackageInfo } from '../../coreSystem'
@@ -124,6 +125,12 @@ function getExtendedMeteorRuntimeConfig() {
124125 // @ts -expect-error missing types for internal meteor detail
125126 ...__meteor_runtime_config__ ,
126127 sofieVersionExtended : versionExtended ,
128+ // The webui's DDP connection endpoint is defined here — point it at the standalone DDP server when
129+ // enabled, otherwise Meteor's own DDP endpoint. This is a site-root-relative path (no prefix): the
130+ // vendored client resolves it against ROOT_URL_PATH_PREFIX (it no longer appends `/websocket` — see
131+ // packages/webui/src/meteor/socket-stream-client/urls.js), and the server matches the prefixed path
132+ // the same way (`getRootSubpath() + path` in ddp-server/index.ts).
133+ DDP_DEFAULT_CONNECTION_URL : STANDALONE_DDP_SERVER_ENABLED ? STANDALONE_DDP_SERVER_PATH : '/websocket' ,
127134 } ) } )`
128135}
129136
@@ -164,37 +171,6 @@ export function bindKoaRouter(koaRouter: KoaRouter, bindPath: string): void {
164171 rootRouter . use ( bindPathWithPrefix , koaRouter . routes ( ) ) . use ( bindPathWithPrefix , koaRouter . allowedMethods ( ) )
165172}
166173
167- const REVERSE_PROXY_COUNT = process . env . HTTP_FORWARDED_COUNT ? parseInt ( process . env . HTTP_FORWARDED_COUNT ) : 0
168-
169- // X-Forwarded-For (a de-facto standard) has the following syntax by convention
170- // X-Forwarded-For: 203.0.113.195, 2001:db8:85a3:8d3:1319:8a2e:370:7348
171- // X-Forwarded-For: 203.0.113.195,2001:db8:85a3:8d3:1319:8a2e:370:7348,198.51.100.178
172- function getClientAddrFromXForwarded ( headerVal : undefined | string | string [ ] ) : string | undefined {
173- if ( headerVal === undefined ) return undefined
174- if ( typeof headerVal !== 'string' ) {
175- headerVal = _ . last ( headerVal ) as string
176- }
177- const remoteAddresses = headerVal . split ( ',' )
178- return remoteAddresses [ remoteAddresses . length - REVERSE_PROXY_COUNT ] ?. trim ( ) ?? remoteAddresses [ 0 ] ?. trim ( )
179- }
180-
181- // Forwarded uses the following syntax:
182- // Forwarded: for=192.0.2.60;proto=http;by=203.0.113.43
183- // Forwarded: for=192.0.2.43, for="[2001:db8:cafe::17]"
184- function getClientAddrFromForwarded ( forwardedVal : string | undefined ) : string | undefined {
185- if ( forwardedVal === undefined ) return undefined
186- const allProxies = forwardedVal . split ( ',' )
187- const proxyInfo = allProxies [ allProxies . length - REVERSE_PROXY_COUNT ] ?? allProxies [ 0 ]
188- const directives = proxyInfo ?. trim ( ) . split ( ';' )
189- for ( const directive of directives ) {
190- let match : RegExpMatchArray | null
191- if ( ( match = directive . trim ( ) . match ( / ^ f o r = ( " \[ ) ? ( [ \w . : ] ) + ( \] " ) ? / ) ) ) {
192- return match [ 2 ]
193- }
194- }
195- return undefined
196- }
197-
198174export const makeMeteorConnectionFromKoa = (
199175 ctx : Koa . ParameterizedContext < Koa . DefaultState , Koa . DefaultContext , unknown >
200176) : Meteor . Connection => {
@@ -206,13 +182,7 @@ export const makeMeteorConnectionFromKoa = (
206182 onClose : ( ) => {
207183 /* no-op */
208184 } ,
209- clientAddress :
210- // This replicates Meteor behavior which uses the HTTP_FORWARDED_COUNT to extract the "world-facing"
211- // IP address of the Client User Agent
212- getClientAddrFromForwarded ( ctx . req . headers . forwarded ) ||
213- getClientAddrFromXForwarded ( ctx . req . headers [ 'x-forwarded-for' ] ) ||
214- ctx . req . socket . remoteAddress ||
215- 'unknown' ,
185+ clientAddress : getClientAddress ( ctx . req . headers , ctx . req . socket . remoteAddress ) ,
216186 httpHeaders : ctx . req . headers as Record < string , string > ,
217187 }
218188}
0 commit comments