|
1 | 1 | import { Request, Router } from "express"
|
2 | 2 | import { HttpCode, HttpError } from "../../common/http"
|
3 |
| -import { getHost } from "../http" |
4 |
| -import { authenticated, ensureAuthenticated, ensureOrigin, redirect, self } from "../http" |
| 3 | +import { getHost, authenticated, ensureAuthenticated, ensureOrigin, redirect, self } from "../http" |
5 | 4 | import { proxy } from "../proxy"
|
6 | 5 | import { Router as WsRouter } from "../wsRouter"
|
7 | 6 |
|
8 | 7 | export const router = Router()
|
9 | 8 |
|
10 | 9 | const proxyDomainToRegex = (matchString: string): RegExp => {
|
11 |
| - let escapedMatchString = matchString.replace(/[.*+?^$()|[\]\\]/g, "\\$&"); |
| 10 | + const escapedMatchString = matchString.replace(/[.*+?^$()|[\]\\]/g, "\\$&") |
12 | 11 |
|
13 | 12 | // Replace {{port}} with a regex group to capture the port
|
14 | 13 | // Replace {{host}} with .+ to allow any host match (so rely on DNS record here)
|
15 |
| - let regexString = escapedMatchString.replace("{{port}}", "(\\d+)"); |
16 |
| - regexString = regexString.replace("{{host}}", ".+"); |
| 14 | + let regexString = escapedMatchString.replace("{{port}}", "(\\d+)") |
| 15 | + regexString = regexString.replace("{{host}}", ".+") |
17 | 16 |
|
18 |
| - regexString = regexString.replace(/[{}]/g, "\\$&"); //replace any '{}' that might be left |
| 17 | + regexString = regexString.replace(/[{}]/g, "\\$&") //replace any '{}' that might be left |
19 | 18 |
|
20 |
| - return new RegExp("^" + regexString + "$"); |
| 19 | + return new RegExp("^" + regexString + "$") |
21 | 20 | }
|
22 | 21 |
|
23 |
| -let proxyRegexes : RegExp[] = []; |
24 |
| -const proxyDomainsToRegex = (proxyDomains : string[]): RegExp[] => { |
25 |
| - if(proxyDomains.length !== proxyRegexes.length) { |
26 |
| - proxyRegexes = proxyDomains.map(proxyDomainToRegex); |
| 22 | +let proxyRegexes: RegExp[] = [] |
| 23 | +const proxyDomainsToRegex = (proxyDomains: string[]): RegExp[] => { |
| 24 | + if (proxyDomains.length !== proxyRegexes.length) { |
| 25 | + proxyRegexes = proxyDomains.map(proxyDomainToRegex) |
27 | 26 | }
|
28 |
| - return proxyRegexes; |
| 27 | + return proxyRegexes |
29 | 28 | }
|
30 | 29 |
|
31 | 30 | /**
|
32 | 31 | * Return the port if the request should be proxied.
|
33 |
| - * |
| 32 | + * |
34 | 33 | * The proxy-domain should be of format anyprefix-{{port}}-anysuffix.{{host}}, where {{host}} is optional
|
35 | 34 | * e.g. code-8080.domain.tld would match for code-{{port}}.domain.tld and code-{{port}}.{{host}}.
|
36 |
| - * |
| 35 | + * |
37 | 36 | */
|
38 | 37 | const maybeProxy = (req: Request): string | undefined => {
|
39 |
| - let reqDomain = getHost(req); |
| 38 | + const reqDomain = getHost(req) |
40 | 39 | if (reqDomain === undefined) {
|
41 |
| - return undefined; |
| 40 | + return undefined |
42 | 41 | }
|
43 | 42 |
|
44 |
| - let regexs = proxyDomainsToRegex(req.args["proxy-domain"]); |
| 43 | + const regexs = proxyDomainsToRegex(req.args["proxy-domain"]) |
45 | 44 |
|
46 |
| - for(let regex of regexs){ |
47 |
| - let match = reqDomain.match(regex); |
| 45 | + for (const regex of regexs) { |
| 46 | + const match = reqDomain.match(regex) |
48 | 47 |
|
49 | 48 | if (match) {
|
50 |
| - return match[1]; // match[1] contains the port |
| 49 | + return match[1] // match[1] contains the port |
51 | 50 | }
|
52 | 51 | }
|
53 | 52 |
|
|
0 commit comments