@@ -6,8 +6,15 @@ import { HttpProvider, HttpProviderOptions, HttpProxyProvider, HttpResponse, Rou
6
6
* Proxy HTTP provider.
7
7
*/
8
8
export class ProxyHttpProvider extends HttpProvider implements HttpProxyProvider {
9
+ /**
10
+ * Proxy domains are stored here without the leading `*.`
11
+ */
9
12
public readonly proxyDomains : string [ ]
10
13
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
+ */
11
18
public constructor ( options : HttpProviderOptions , proxyDomains : string [ ] = [ ] ) {
12
19
super ( options )
13
20
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
29
36
throw new HttpError ( "Not found" , HttpCode . NotFound )
30
37
}
31
38
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
38
47
}
39
48
40
49
public maybeProxy ( request : http . IncomingMessage ) : HttpResponse | undefined {
@@ -44,23 +53,21 @@ export class ProxyHttpProvider extends HttpProvider implements HttpProxyProvider
44
53
return undefined
45
54
}
46
55
56
+ // At minimum there needs to be sub.domain.tld.
47
57
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 ) {
50
60
return undefined
51
61
}
52
62
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 ) ) {
60
67
return undefined
61
68
}
62
69
63
- return this . proxy ( portStr )
70
+ return this . proxy ( port )
64
71
}
65
72
66
73
private proxy ( portStr : string ) : HttpResponse {
0 commit comments