|
1 | 1 | import { Request, Router } from "express"
|
2 | 2 | import { HttpCode, HttpError } from "../../common/http"
|
3 |
| -import { authenticated, ensureAuthenticated, ensureOrigin, redirect, self } from "../http" |
| 3 | +import { getHost, authenticated, ensureAuthenticated, ensureOrigin, redirect, self } from "../http" |
4 | 4 | import { proxy } from "../proxy"
|
5 | 5 | import { Router as WsRouter } from "../wsRouter"
|
6 | 6 |
|
7 | 7 | export const router = Router()
|
8 | 8 |
|
| 9 | +const proxyDomainToRegex = (matchString: string): RegExp => { |
| 10 | + const escapedMatchString = matchString.replace(/[.*+?^$()|[\]\\]/g, "\\$&") |
| 11 | + |
| 12 | + // Replace {{port}} with a regex group to capture the port |
| 13 | + // Replace {{host}} with .+ to allow any host match (so rely on DNS record here) |
| 14 | + let regexString = escapedMatchString.replace("{{port}}", "(\\d+)") |
| 15 | + regexString = regexString.replace("{{host}}", ".+") |
| 16 | + |
| 17 | + regexString = regexString.replace(/[{}]/g, "\\$&") //replace any '{}' that might be left |
| 18 | + |
| 19 | + return new RegExp("^" + regexString + "$") |
| 20 | +} |
| 21 | + |
| 22 | +let proxyRegexes: RegExp[] = [] |
| 23 | +const proxyDomainsToRegex = (proxyDomains: string[]): RegExp[] => { |
| 24 | + if (proxyDomains.length !== proxyRegexes.length) { |
| 25 | + proxyRegexes = proxyDomains.map(proxyDomainToRegex) |
| 26 | + } |
| 27 | + return proxyRegexes |
| 28 | +} |
| 29 | + |
9 | 30 | /**
|
10 |
| - * Return the port if the request should be proxied. Anything that ends in a |
11 |
| - * proxy domain and has a *single* subdomain should be proxied. Anything else |
12 |
| - * should return `undefined` and will be handled as normal. |
| 31 | + * Return the port if the request should be proxied. |
| 32 | + * |
| 33 | + * The proxy-domain should be of format anyprefix-{{port}}-anysuffix.{{host}}, where {{host}} is optional |
| 34 | + * e.g. code-8080.domain.tld would match for code-{{port}}.domain.tld and code-{{port}}.{{host}}. |
13 | 35 | *
|
14 |
| - * For example if `coder.com` is specified `8080.coder.com` will be proxied |
15 |
| - * but `8080.test.coder.com` and `test.8080.coder.com` will not. |
16 | 36 | */
|
17 | 37 | const maybeProxy = (req: Request): string | undefined => {
|
18 |
| - // Split into parts. |
19 |
| - const host = req.headers.host || "" |
20 |
| - const idx = host.indexOf(":") |
21 |
| - const domain = idx !== -1 ? host.substring(0, idx) : host |
22 |
| - const parts = domain.split(".") |
23 |
| - |
24 |
| - // There must be an exact match. |
25 |
| - const port = parts.shift() |
26 |
| - const proxyDomain = parts.join(".") |
27 |
| - if (!port || !req.args["proxy-domain"].includes(proxyDomain)) { |
| 38 | + const reqDomain = getHost(req) |
| 39 | + if (reqDomain === undefined) { |
28 | 40 | return undefined
|
29 | 41 | }
|
30 | 42 |
|
31 |
| - return port |
| 43 | + const regexs = proxyDomainsToRegex(req.args["proxy-domain"]) |
| 44 | + |
| 45 | + for (const regex of regexs) { |
| 46 | + const match = reqDomain.match(regex) |
| 47 | + |
| 48 | + if (match) { |
| 49 | + return match[1] // match[1] contains the port |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return undefined |
32 | 54 | }
|
33 | 55 |
|
34 | 56 | router.all("*", async (req, res, next) => {
|
|
0 commit comments