@@ -6,8 +6,15 @@ import { HttpProvider, HttpProviderOptions, HttpProxyProvider, HttpResponse, Rou
66 * Proxy HTTP provider.
77 */
88export class ProxyHttpProvider extends HttpProvider implements HttpProxyProvider {
9+ /**
10+ * Proxy domains are stored here without the leading `*.`
11+ */
912 public readonly proxyDomains : string [ ]
1013
14+ /**
15+ * Domains can be provided in the form `coder.com` or `*.coder.com`. Either
16+ * way, `<number>.coder.com` will be proxied to `number`.
17+ */
1118 public constructor ( options : HttpProviderOptions , proxyDomains : string [ ] = [ ] ) {
1219 super ( options )
1320 this . proxyDomains = proxyDomains . map ( ( d ) => d . replace ( / ^ \* \. / , "" ) ) . filter ( ( d , i , arr ) => arr . indexOf ( d ) === i )
@@ -29,12 +36,14 @@ export class ProxyHttpProvider extends HttpProvider implements HttpProxyProvider
2936 throw new HttpError ( "Not found" , HttpCode . NotFound )
3037 }
3138
32- public getProxyDomain ( host ?: string ) : string | undefined {
33- if ( ! host || ! this . proxyDomains ) {
34- return undefined
35- }
36-
37- return this . proxyDomains . find ( ( d ) => host . endsWith ( d ) )
39+ public getCookieDomain ( host : string ) : string {
40+ let current : string | undefined
41+ this . proxyDomains . forEach ( ( domain ) => {
42+ if ( host . endsWith ( domain ) && ( ! current || domain . length < current . length ) ) {
43+ current = domain
44+ }
45+ } )
46+ return current || host
3847 }
3948
4049 public maybeProxy ( request : http . IncomingMessage ) : HttpResponse | undefined {
@@ -44,23 +53,21 @@ export class ProxyHttpProvider extends HttpProvider implements HttpProxyProvider
4453 return undefined
4554 }
4655
56+ // At minimum there needs to be sub.domain.tld.
4757 const host = request . headers . host
48- const proxyDomain = this . getProxyDomain ( host )
49- if ( ! host || ! proxyDomain ) {
58+ const parts = host && host . split ( "." )
59+ if ( ! parts || parts . length < 3 ) {
5060 return undefined
5161 }
5262
53- const proxyDomainLength = proxyDomain . split ( "." ) . length
54- const portStr = host
55- . split ( "." )
56- . slice ( 0 , - proxyDomainLength )
57- . pop ( )
58-
59- if ( ! portStr ) {
63+ // There must be an exact match.
64+ const port = parts . shift ( )
65+ const proxyDomain = parts . join ( "." )
66+ if ( ! port || ! this . proxyDomains . includes ( proxyDomain ) ) {
6067 return undefined
6168 }
6269
63- return this . proxy ( portStr )
70+ return this . proxy ( port )
6471 }
6572
6673 private proxy ( portStr : string ) : HttpResponse {
0 commit comments