2020-02-20 06:00:58 +07:00
|
|
|
import { field, logger } from "@coder/logger"
|
2020-02-05 02:27:46 +07:00
|
|
|
import * as fs from "fs-extra"
|
|
|
|
import * as http from "http"
|
2020-04-03 01:09:09 +07:00
|
|
|
import proxy from "http-proxy"
|
2020-02-05 02:27:46 +07:00
|
|
|
import * as httpolyglot from "httpolyglot"
|
|
|
|
import * as https from "https"
|
|
|
|
import * as net from "net"
|
|
|
|
import * as path from "path"
|
|
|
|
import * as querystring from "querystring"
|
|
|
|
import safeCompare from "safe-compare"
|
|
|
|
import { Readable } from "stream"
|
|
|
|
import * as tls from "tls"
|
|
|
|
import * as url from "url"
|
|
|
|
import { HttpCode, HttpError } from "../common/http"
|
2020-07-31 00:14:31 +07:00
|
|
|
import { arrayify, normalize, Options, plural, split, trimSlashes } from "../common/util"
|
2020-02-20 06:00:58 +07:00
|
|
|
import { SocketProxyProvider } from "./socket"
|
2020-05-10 12:35:42 +07:00
|
|
|
import { getMediaMime, paths } from "./util"
|
2020-02-05 02:27:46 +07:00
|
|
|
|
|
|
|
export type Cookies = { [key: string]: string[] | undefined }
|
|
|
|
export type PostData = { [key: string]: string | string[] | undefined }
|
|
|
|
|
2020-04-03 01:09:09 +07:00
|
|
|
interface ProxyRequest extends http.IncomingMessage {
|
|
|
|
base?: string
|
|
|
|
}
|
|
|
|
|
2020-02-05 02:27:46 +07:00
|
|
|
interface AuthPayload extends Cookies {
|
|
|
|
key?: string[]
|
|
|
|
}
|
|
|
|
|
|
|
|
export enum AuthType {
|
|
|
|
Password = "password",
|
|
|
|
None = "none",
|
|
|
|
}
|
|
|
|
|
|
|
|
export type Query = { [key: string]: string | string[] | undefined }
|
|
|
|
|
2020-04-03 01:09:09 +07:00
|
|
|
export interface ProxyOptions {
|
|
|
|
/**
|
2020-07-24 00:37:45 +07:00
|
|
|
* A path to strip from from the beginning of the request before proxying
|
2020-04-03 01:09:09 +07:00
|
|
|
*/
|
2020-07-24 00:37:45 +07:00
|
|
|
strip?: string
|
|
|
|
/**
|
|
|
|
* A path to add to the beginning of the request before proxying.
|
|
|
|
*/
|
|
|
|
prepend?: string
|
2020-04-03 01:09:09 +07:00
|
|
|
/**
|
|
|
|
* The port to proxy.
|
|
|
|
*/
|
|
|
|
port: string
|
|
|
|
}
|
|
|
|
|
2020-02-05 02:27:46 +07:00
|
|
|
export interface HttpResponse<T = string | Buffer | object> {
|
|
|
|
/*
|
|
|
|
* Whether to set cache-control headers for this response.
|
|
|
|
*/
|
|
|
|
cache?: boolean
|
|
|
|
/**
|
|
|
|
* If the code cannot be determined automatically set it here. The
|
|
|
|
* defaults are 302 for redirects and 200 for successful requests. For errors
|
|
|
|
* you should throw an HttpError and include the code there. If you
|
|
|
|
* use Error it will default to 404 for ENOENT and EISDIR and 500 otherwise.
|
|
|
|
*/
|
|
|
|
code?: number
|
|
|
|
/**
|
|
|
|
* Content to write in the response. Mutually exclusive with stream.
|
|
|
|
*/
|
|
|
|
content?: T
|
|
|
|
/**
|
|
|
|
* Cookie to write with the response.
|
2020-02-06 06:30:09 +07:00
|
|
|
* NOTE: Cookie paths must be absolute. The default is /.
|
2020-02-05 02:27:46 +07:00
|
|
|
*/
|
2020-02-06 06:30:09 +07:00
|
|
|
cookie?: { key: string; value: string; path?: string }
|
2020-02-05 02:27:46 +07:00
|
|
|
/**
|
|
|
|
* Used to automatically determine the appropriate mime type.
|
|
|
|
*/
|
|
|
|
filePath?: string
|
|
|
|
/**
|
|
|
|
* Additional headers to include.
|
|
|
|
*/
|
|
|
|
headers?: http.OutgoingHttpHeaders
|
|
|
|
/**
|
|
|
|
* If the mime type cannot be determined automatically set it here.
|
|
|
|
*/
|
|
|
|
mime?: string
|
|
|
|
/**
|
2020-07-23 02:53:15 +07:00
|
|
|
* Redirect to this path. This is constructed against the site base (not the
|
|
|
|
* provider's base).
|
2020-02-05 02:27:46 +07:00
|
|
|
*/
|
|
|
|
redirect?: string
|
|
|
|
/**
|
|
|
|
* Stream this to the response. Mutually exclusive with content.
|
|
|
|
*/
|
|
|
|
stream?: Readable
|
|
|
|
/**
|
|
|
|
* Query variables to add in addition to current ones when redirecting. Use
|
|
|
|
* `undefined` to remove a query variable.
|
|
|
|
*/
|
|
|
|
query?: Query
|
2020-03-24 06:02:31 +07:00
|
|
|
/**
|
2020-04-03 01:09:09 +07:00
|
|
|
* Indicates the request should be proxied.
|
|
|
|
*/
|
|
|
|
proxy?: ProxyOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface WsResponse {
|
|
|
|
/**
|
|
|
|
* Indicates the web socket should be proxied.
|
2020-03-24 06:02:31 +07:00
|
|
|
*/
|
2020-04-03 01:09:09 +07:00
|
|
|
proxy?: ProxyOptions
|
2020-02-05 02:27:46 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use when you need to run search and replace on a file's content before
|
|
|
|
* sending it.
|
|
|
|
*/
|
|
|
|
export interface HttpStringFileResponse extends HttpResponse {
|
|
|
|
content: string
|
|
|
|
filePath: string
|
|
|
|
}
|
|
|
|
|
2020-02-06 06:30:09 +07:00
|
|
|
export interface RedirectResponse extends HttpResponse {
|
|
|
|
redirect: string
|
|
|
|
}
|
|
|
|
|
2020-02-05 02:27:46 +07:00
|
|
|
export interface HttpServerOptions {
|
2020-02-05 05:55:27 +07:00
|
|
|
readonly auth?: AuthType
|
2020-02-05 02:27:46 +07:00
|
|
|
readonly cert?: string
|
|
|
|
readonly certKey?: string
|
2020-02-05 07:16:45 +07:00
|
|
|
readonly commit?: string
|
2020-02-05 02:27:46 +07:00
|
|
|
readonly host?: string
|
2020-02-05 05:55:27 +07:00
|
|
|
readonly password?: string
|
2020-02-25 05:49:10 +07:00
|
|
|
readonly port?: number
|
2020-04-03 01:09:09 +07:00
|
|
|
readonly proxyDomains?: string[]
|
2020-02-05 02:27:46 +07:00
|
|
|
readonly socket?: string
|
|
|
|
}
|
|
|
|
|
2020-02-06 06:30:09 +07:00
|
|
|
export interface Route {
|
2020-03-24 06:02:31 +07:00
|
|
|
/**
|
2020-07-23 02:53:15 +07:00
|
|
|
* Provider base path part (for /provider/base/path it would be /provider).
|
|
|
|
*/
|
|
|
|
providerBase: string
|
|
|
|
/**
|
|
|
|
* Base path part (for /provider/base/path it would be /base).
|
2020-03-24 06:02:31 +07:00
|
|
|
*/
|
2020-02-05 02:27:46 +07:00
|
|
|
base: string
|
2020-03-24 06:02:31 +07:00
|
|
|
/**
|
2020-07-23 02:53:15 +07:00
|
|
|
* Remaining part of the route after factoring out the base and provider base
|
|
|
|
* (for /provider/base/path it would be /path). It can be blank.
|
2020-03-24 06:02:31 +07:00
|
|
|
*/
|
2020-02-05 02:27:46 +07:00
|
|
|
requestPath: string
|
2020-03-24 06:02:31 +07:00
|
|
|
/**
|
|
|
|
* Query variables included in the request.
|
|
|
|
*/
|
2020-02-05 02:27:46 +07:00
|
|
|
query: querystring.ParsedUrlQuery
|
2020-03-24 06:02:31 +07:00
|
|
|
/**
|
|
|
|
* Normalized version of `originalPath`.
|
|
|
|
*/
|
2020-02-05 02:27:46 +07:00
|
|
|
fullPath: string
|
2020-03-24 06:02:31 +07:00
|
|
|
/**
|
|
|
|
* Original path of the request without any modifications.
|
|
|
|
*/
|
2020-02-05 02:27:46 +07:00
|
|
|
originalPath: string
|
|
|
|
}
|
|
|
|
|
2020-02-06 06:30:09 +07:00
|
|
|
interface ProviderRoute extends Route {
|
|
|
|
provider: HttpProvider
|
|
|
|
}
|
|
|
|
|
2020-02-05 02:27:46 +07:00
|
|
|
export interface HttpProviderOptions {
|
|
|
|
readonly auth: AuthType
|
2020-02-06 03:21:59 +07:00
|
|
|
readonly commit: string
|
2020-02-14 01:40:36 +07:00
|
|
|
readonly password?: string
|
2020-02-05 02:27:46 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides HTTP responses. This abstract class provides some helpers for
|
|
|
|
* interpreting, creating, and authenticating responses.
|
|
|
|
*/
|
|
|
|
export abstract class HttpProvider {
|
|
|
|
protected readonly rootPath = path.resolve(__dirname, "../..")
|
|
|
|
|
2020-02-05 07:16:45 +07:00
|
|
|
public constructor(protected readonly options: HttpProviderOptions) {}
|
2020-02-05 02:27:46 +07:00
|
|
|
|
2020-07-24 00:22:38 +07:00
|
|
|
public async dispose(): Promise<void> {
|
2020-02-05 02:27:46 +07:00
|
|
|
// No default behavior.
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-03 01:09:09 +07:00
|
|
|
* Handle web sockets on the registered endpoint. Normally the provider
|
|
|
|
* handles the request itself but it can return a response when necessary. The
|
|
|
|
* default is to throw a 404.
|
2020-02-05 02:27:46 +07:00
|
|
|
*/
|
2020-03-03 03:39:12 +07:00
|
|
|
public handleWebSocket(
|
|
|
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
|
|
_route: Route,
|
|
|
|
_request: http.IncomingMessage,
|
|
|
|
_socket: net.Socket,
|
|
|
|
_head: Buffer,
|
|
|
|
/* eslint-enable @typescript-eslint/no-unused-vars */
|
2020-04-03 01:09:09 +07:00
|
|
|
): Promise<WsResponse | void> {
|
2020-03-03 03:39:12 +07:00
|
|
|
throw new HttpError("Not found", HttpCode.NotFound)
|
|
|
|
}
|
2020-02-05 02:27:46 +07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle requests to the registered endpoint.
|
|
|
|
*/
|
2020-04-03 01:09:09 +07:00
|
|
|
public abstract handleRequest(route: Route, request: http.IncomingMessage): Promise<HttpResponse>
|
2020-02-06 03:21:59 +07:00
|
|
|
|
2020-02-05 02:27:46 +07:00
|
|
|
/**
|
2020-02-14 01:40:36 +07:00
|
|
|
* Get the base relative to the provided route. For each slash we need to go
|
|
|
|
* up a directory. For example:
|
2020-07-30 03:02:14 +07:00
|
|
|
* / => .
|
|
|
|
* /foo => .
|
|
|
|
* /foo/ => ./..
|
|
|
|
* /foo/bar => ./..
|
|
|
|
* /foo/bar/ => ./../..
|
2020-02-05 02:27:46 +07:00
|
|
|
*/
|
2020-02-06 06:30:09 +07:00
|
|
|
public base(route: Route): string {
|
2020-02-14 01:40:36 +07:00
|
|
|
const depth = (route.originalPath.match(/\//g) || []).length
|
2020-02-06 06:30:09 +07:00
|
|
|
return normalize("./" + (depth > 1 ? "../".repeat(depth - 1) : ""))
|
|
|
|
}
|
|
|
|
|
2020-02-28 03:56:14 +07:00
|
|
|
/**
|
|
|
|
* Get error response.
|
|
|
|
*/
|
2020-02-14 05:38:05 +07:00
|
|
|
public async getErrorRoot(route: Route, title: string, header: string, body: string): Promise<HttpResponse> {
|
|
|
|
const response = await this.getUtf8Resource(this.rootPath, "src/browser/pages/error.html")
|
|
|
|
response.content = response.content
|
|
|
|
.replace(/{{ERROR_TITLE}}/g, title)
|
|
|
|
.replace(/{{ERROR_HEADER}}/g, header)
|
|
|
|
.replace(/{{ERROR_BODY}}/g, body)
|
2020-02-28 03:56:14 +07:00
|
|
|
return this.replaceTemplates(route, response)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace common templates strings.
|
|
|
|
*/
|
2020-03-14 04:44:56 +07:00
|
|
|
protected replaceTemplates<T extends object>(
|
|
|
|
route: Route,
|
|
|
|
response: HttpStringFileResponse,
|
2020-07-29 03:06:15 +07:00
|
|
|
extraOptions?: Omit<T, "base" | "csStaticBase" | "logLevel">,
|
2020-02-28 03:56:14 +07:00
|
|
|
): HttpStringFileResponse {
|
2020-07-30 03:02:14 +07:00
|
|
|
const base = this.base(route)
|
2020-07-29 03:06:15 +07:00
|
|
|
const options: Options = {
|
2020-07-30 03:02:14 +07:00
|
|
|
base,
|
|
|
|
csStaticBase: base + "/static/" + this.options.commit + this.rootPath,
|
2020-07-29 03:06:15 +07:00
|
|
|
logLevel: logger.level,
|
|
|
|
...extraOptions,
|
2020-02-28 03:56:14 +07:00
|
|
|
}
|
|
|
|
response.content = response.content
|
2020-03-03 03:39:12 +07:00
|
|
|
.replace(/{{TO}}/g, Array.isArray(route.query.to) ? route.query.to[0] : route.query.to || "/dashboard")
|
2020-07-30 03:02:14 +07:00
|
|
|
.replace(/{{BASE}}/g, options.base)
|
|
|
|
.replace(/{{CS_STATIC_BASE}}/g, options.csStaticBase)
|
2020-07-29 03:06:15 +07:00
|
|
|
.replace(/"{{OPTIONS}}"/, `'${JSON.stringify(options)}'`)
|
2020-02-14 05:38:05 +07:00
|
|
|
return response
|
|
|
|
}
|
|
|
|
|
2020-02-06 06:30:09 +07:00
|
|
|
protected get isDev(): boolean {
|
|
|
|
return this.options.commit === "development"
|
2020-02-05 02:27:46 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a file resource.
|
|
|
|
* TODO: Would a stream be faster, at least for large files?
|
|
|
|
*/
|
|
|
|
protected async getResource(...parts: string[]): Promise<HttpResponse> {
|
|
|
|
const filePath = path.join(...parts)
|
|
|
|
return { content: await fs.readFile(filePath), filePath }
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a file resource as a string.
|
|
|
|
*/
|
|
|
|
protected async getUtf8Resource(...parts: string[]): Promise<HttpStringFileResponse> {
|
|
|
|
const filePath = path.join(...parts)
|
|
|
|
return { content: await fs.readFile(filePath, "utf8"), filePath }
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-02-14 05:38:05 +07:00
|
|
|
* Helper to error on invalid methods (default GET).
|
2020-02-05 02:27:46 +07:00
|
|
|
*/
|
2020-02-14 05:38:05 +07:00
|
|
|
protected ensureMethod(request: http.IncomingMessage, method?: string | string[]): void {
|
2020-07-31 00:14:31 +07:00
|
|
|
const check = arrayify(method || "GET")
|
2020-02-14 05:38:05 +07:00
|
|
|
if (!request.method || !check.includes(request.method)) {
|
2020-02-05 02:27:46 +07:00
|
|
|
throw new HttpError(`Unsupported method ${request.method}`, HttpCode.BadRequest)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper to error if not authorized.
|
|
|
|
*/
|
|
|
|
protected ensureAuthenticated(request: http.IncomingMessage): void {
|
|
|
|
if (!this.authenticated(request)) {
|
|
|
|
throw new HttpError("Unauthorized", HttpCode.Unauthorized)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use the first query value or the default if there isn't one.
|
|
|
|
*/
|
|
|
|
protected queryOrDefault(value: string | string[] | undefined, def: string): string {
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
value = value[0]
|
|
|
|
}
|
|
|
|
return typeof value !== "undefined" ? value : def
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the provided password value if the payload contains the right
|
|
|
|
* password otherwise return false. If no payload is specified use cookies.
|
|
|
|
*/
|
2020-04-03 01:09:09 +07:00
|
|
|
public authenticated(request: http.IncomingMessage, payload?: AuthPayload): string | boolean {
|
2020-02-05 02:27:46 +07:00
|
|
|
switch (this.options.auth) {
|
|
|
|
case AuthType.None:
|
|
|
|
return true
|
|
|
|
case AuthType.Password:
|
|
|
|
if (typeof payload === "undefined") {
|
|
|
|
payload = this.parseCookies<AuthPayload>(request)
|
|
|
|
}
|
|
|
|
if (this.options.password && payload.key) {
|
|
|
|
for (let i = 0; i < payload.key.length; ++i) {
|
|
|
|
if (safeCompare(payload.key[i], this.options.password)) {
|
|
|
|
return payload.key[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
throw new Error(`Unsupported auth type ${this.options.auth}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse POST data.
|
|
|
|
*/
|
|
|
|
protected getData(request: http.IncomingMessage): Promise<string | undefined> {
|
|
|
|
return request.method === "POST" || request.method === "DELETE"
|
|
|
|
? new Promise<string>((resolve, reject) => {
|
|
|
|
let body = ""
|
|
|
|
const onEnd = (): void => {
|
|
|
|
off() // eslint-disable-line @typescript-eslint/no-use-before-define
|
|
|
|
resolve(body || undefined)
|
|
|
|
}
|
|
|
|
const onError = (error: Error): void => {
|
|
|
|
off() // eslint-disable-line @typescript-eslint/no-use-before-define
|
|
|
|
reject(error)
|
|
|
|
}
|
|
|
|
const onData = (d: Buffer): void => {
|
|
|
|
body += d
|
|
|
|
if (body.length > 1e6) {
|
|
|
|
onError(new HttpError("Payload is too large", HttpCode.LargePayload))
|
|
|
|
request.connection.destroy()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const off = (): void => {
|
|
|
|
request.off("error", onError)
|
|
|
|
request.off("data", onError)
|
|
|
|
request.off("end", onEnd)
|
|
|
|
}
|
|
|
|
request.on("error", onError)
|
|
|
|
request.on("data", onData)
|
|
|
|
request.on("end", onEnd)
|
|
|
|
})
|
|
|
|
: Promise.resolve(undefined)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse cookies.
|
|
|
|
*/
|
|
|
|
protected parseCookies<T extends Cookies>(request: http.IncomingMessage): T {
|
|
|
|
const cookies: { [key: string]: string[] } = {}
|
|
|
|
if (request.headers.cookie) {
|
|
|
|
request.headers.cookie.split(";").forEach((keyValue) => {
|
|
|
|
const [key, value] = split(keyValue, "=")
|
|
|
|
if (!cookies[key]) {
|
|
|
|
cookies[key] = []
|
|
|
|
}
|
|
|
|
cookies[key].push(decodeURI(value))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return cookies as T
|
|
|
|
}
|
2020-04-01 23:28:09 +07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if the route is for the root page. For example /base, /base/,
|
|
|
|
* or /base/index.html but not /base/path or /base/file.js.
|
|
|
|
*/
|
|
|
|
protected isRoot(route: Route): boolean {
|
|
|
|
return !route.requestPath || route.requestPath === "/index.html"
|
|
|
|
}
|
2020-02-05 02:27:46 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides a heartbeat using a local file to indicate activity.
|
|
|
|
*/
|
|
|
|
export class Heart {
|
|
|
|
private heartbeatTimer?: NodeJS.Timeout
|
|
|
|
private heartbeatInterval = 60000
|
2020-08-31 22:29:12 +07:00
|
|
|
public lastHeartbeat = 0
|
2020-02-05 02:27:46 +07:00
|
|
|
|
|
|
|
public constructor(private readonly heartbeatPath: string, private readonly isActive: () => Promise<boolean>) {}
|
|
|
|
|
2020-08-31 22:29:12 +07:00
|
|
|
public alive(): boolean {
|
|
|
|
const now = Date.now()
|
|
|
|
return now - this.lastHeartbeat < this.heartbeatInterval
|
|
|
|
}
|
2020-02-05 02:27:46 +07:00
|
|
|
/**
|
|
|
|
* Write to the heartbeat file if we haven't already done so within the
|
|
|
|
* timeout and start or reset a timer that keeps running as long as there is
|
|
|
|
* activity. Failures are logged as warnings.
|
|
|
|
*/
|
|
|
|
public beat(): void {
|
2020-08-31 22:29:12 +07:00
|
|
|
if (!this.alive()) {
|
2020-02-05 02:27:46 +07:00
|
|
|
logger.trace("heartbeat")
|
|
|
|
fs.outputFile(this.heartbeatPath, "").catch((error) => {
|
|
|
|
logger.warn(error.message)
|
|
|
|
})
|
2020-08-31 22:29:12 +07:00
|
|
|
this.lastHeartbeat = Date.now()
|
2020-02-05 02:27:46 +07:00
|
|
|
if (typeof this.heartbeatTimer !== "undefined") {
|
|
|
|
clearTimeout(this.heartbeatTimer)
|
|
|
|
}
|
|
|
|
this.heartbeatTimer = setTimeout(() => {
|
2020-02-28 03:56:14 +07:00
|
|
|
this.isActive()
|
|
|
|
.then((active) => {
|
|
|
|
if (active) {
|
|
|
|
this.beat()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
logger.warn(error.message)
|
|
|
|
})
|
2020-02-05 02:27:46 +07:00
|
|
|
}, this.heartbeatInterval)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-05 05:55:27 +07:00
|
|
|
export interface HttpProvider0<T> {
|
|
|
|
new (options: HttpProviderOptions): T
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface HttpProvider1<A1, T> {
|
|
|
|
new (options: HttpProviderOptions, a1: A1): T
|
|
|
|
}
|
|
|
|
|
2020-02-15 04:57:51 +07:00
|
|
|
export interface HttpProvider2<A1, A2, T> {
|
|
|
|
new (options: HttpProviderOptions, a1: A1, a2: A2): T
|
|
|
|
}
|
|
|
|
|
2020-02-28 01:04:23 +07:00
|
|
|
export interface HttpProvider3<A1, A2, A3, T> {
|
|
|
|
new (options: HttpProviderOptions, a1: A1, a2: A2, a3: A3): T
|
|
|
|
}
|
|
|
|
|
2020-02-05 02:27:46 +07:00
|
|
|
/**
|
|
|
|
* An HTTP server. Its main role is to route incoming HTTP requests to the
|
|
|
|
* appropriate provider for that endpoint then write out the response. It also
|
|
|
|
* covers some common use cases like redirects and caching.
|
|
|
|
*/
|
|
|
|
export class HttpServer {
|
|
|
|
protected readonly server: http.Server | https.Server
|
|
|
|
private listenPromise: Promise<string | null> | undefined
|
|
|
|
public readonly protocol: "http" | "https"
|
|
|
|
private readonly providers = new Map<string, HttpProvider>()
|
2020-08-31 22:29:12 +07:00
|
|
|
public readonly heart: Heart
|
2020-02-20 06:00:58 +07:00
|
|
|
private readonly socketProvider = new SocketProxyProvider()
|
2020-04-03 01:09:09 +07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Proxy domains are stored here without the leading `*.`
|
|
|
|
*/
|
|
|
|
public readonly proxyDomains: Set<string>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides the actual proxying functionality.
|
|
|
|
*/
|
|
|
|
private readonly proxy = proxy.createProxyServer({})
|
2020-02-05 02:27:46 +07:00
|
|
|
|
2020-02-06 06:30:09 +07:00
|
|
|
public constructor(private readonly options: HttpServerOptions) {
|
2020-04-03 01:09:09 +07:00
|
|
|
this.proxyDomains = new Set((options.proxyDomains || []).map((d) => d.replace(/^\*\./, "")))
|
2020-05-10 12:35:42 +07:00
|
|
|
this.heart = new Heart(path.join(paths.data, "heartbeat"), async () => {
|
2020-02-05 02:27:46 +07:00
|
|
|
const connections = await this.getConnections()
|
2020-08-09 12:06:18 +07:00
|
|
|
logger.trace(plural(connections, `${connections} active connection`))
|
2020-02-05 02:27:46 +07:00
|
|
|
return connections !== 0
|
|
|
|
})
|
|
|
|
this.protocol = this.options.cert ? "https" : "http"
|
|
|
|
if (this.protocol === "https") {
|
|
|
|
this.server = httpolyglot.createServer(
|
|
|
|
{
|
|
|
|
cert: this.options.cert && fs.readFileSync(this.options.cert),
|
|
|
|
key: this.options.certKey && fs.readFileSync(this.options.certKey),
|
|
|
|
},
|
2020-02-15 07:46:00 +07:00
|
|
|
this.onRequest,
|
2020-02-05 02:27:46 +07:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
this.server = http.createServer(this.onRequest)
|
|
|
|
}
|
2020-04-08 23:54:18 +07:00
|
|
|
this.proxy.on("error", (error, _request, response) => {
|
|
|
|
response.writeHead(HttpCode.ServerError)
|
|
|
|
response.end(error.message)
|
|
|
|
})
|
2020-04-03 01:09:09 +07:00
|
|
|
// Intercept the response to rewrite absolute redirects against the base path.
|
|
|
|
this.proxy.on("proxyRes", (response, request: ProxyRequest) => {
|
|
|
|
if (response.headers.location && response.headers.location.startsWith("/") && request.base) {
|
|
|
|
response.headers.location = request.base + response.headers.location
|
|
|
|
}
|
|
|
|
})
|
2020-02-05 02:27:46 +07:00
|
|
|
}
|
|
|
|
|
2020-07-24 00:22:38 +07:00
|
|
|
/**
|
|
|
|
* Stop and dispose everything. Return an array of disposal errors.
|
|
|
|
*/
|
|
|
|
public async dispose(): Promise<Error[]> {
|
2020-02-20 06:00:58 +07:00
|
|
|
this.socketProvider.stop()
|
2020-07-24 00:22:38 +07:00
|
|
|
const providers = Array.from(this.providers.values())
|
|
|
|
// Catch so all the errors can be seen rather than just the first one.
|
|
|
|
const responses = await Promise.all<Error | undefined>(providers.map((p) => p.dispose().catch((e) => e)))
|
|
|
|
return responses.filter<Error>((r): r is Error => typeof r !== "undefined")
|
2020-02-05 02:27:46 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
public async getConnections(): Promise<number> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.server.getConnections((error, count) => {
|
|
|
|
return error ? reject(error) : resolve(count)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a provider for a top-level endpoint.
|
|
|
|
*/
|
2020-07-23 02:53:15 +07:00
|
|
|
public registerHttpProvider<T extends HttpProvider>(endpoint: string | string[], provider: HttpProvider0<T>): T
|
|
|
|
public registerHttpProvider<A1, T extends HttpProvider>(
|
|
|
|
endpoint: string | string[],
|
|
|
|
provider: HttpProvider1<A1, T>,
|
|
|
|
a1: A1,
|
|
|
|
): T
|
2020-02-15 04:57:51 +07:00
|
|
|
public registerHttpProvider<A1, A2, T extends HttpProvider>(
|
2020-07-23 02:53:15 +07:00
|
|
|
endpoint: string | string[],
|
2020-02-15 04:57:51 +07:00
|
|
|
provider: HttpProvider2<A1, A2, T>,
|
|
|
|
a1: A1,
|
2020-02-19 00:52:29 +07:00
|
|
|
a2: A2,
|
2020-02-15 04:57:51 +07:00
|
|
|
): T
|
2020-02-28 01:04:23 +07:00
|
|
|
public registerHttpProvider<A1, A2, A3, T extends HttpProvider>(
|
2020-07-23 02:53:15 +07:00
|
|
|
endpoint: string | string[],
|
2020-02-28 01:04:23 +07:00
|
|
|
provider: HttpProvider3<A1, A2, A3, T>,
|
|
|
|
a1: A1,
|
|
|
|
a2: A2,
|
|
|
|
a3: A3,
|
|
|
|
): T
|
2020-02-05 05:55:27 +07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2020-07-23 02:53:15 +07:00
|
|
|
public registerHttpProvider(endpoint: string | string[], provider: any, ...args: any[]): void {
|
2020-02-14 05:38:05 +07:00
|
|
|
const p = new provider(
|
|
|
|
{
|
|
|
|
auth: this.options.auth || AuthType.None,
|
|
|
|
commit: this.options.commit,
|
|
|
|
password: this.options.password,
|
|
|
|
},
|
2020-02-19 00:52:29 +07:00
|
|
|
...args,
|
2020-02-05 05:55:27 +07:00
|
|
|
)
|
2020-07-31 00:14:31 +07:00
|
|
|
const endpoints = arrayify(endpoint).map(trimSlashes)
|
2020-07-23 02:53:15 +07:00
|
|
|
endpoints.forEach((endpoint) => {
|
|
|
|
if (/\//.test(endpoint)) {
|
|
|
|
throw new Error(`Only top-level endpoints are supported (got ${endpoint})`)
|
|
|
|
}
|
|
|
|
const existingProvider = this.providers.get(`/${endpoint}`)
|
|
|
|
this.providers.set(`/${endpoint}`, p)
|
|
|
|
if (existingProvider) {
|
|
|
|
logger.debug(`Overridding existing /${endpoint} provider`)
|
|
|
|
// If the existing provider isn't registered elsewhere we can dispose.
|
|
|
|
if (!Array.from(this.providers.values()).find((p) => p === existingProvider)) {
|
|
|
|
logger.debug(`Disposing existing /${endpoint} provider`)
|
|
|
|
existingProvider.dispose()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2020-02-05 02:27:46 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start listening on the specified port.
|
|
|
|
*/
|
|
|
|
public listen(): Promise<string | null> {
|
|
|
|
if (!this.listenPromise) {
|
|
|
|
this.listenPromise = new Promise((resolve, reject) => {
|
|
|
|
this.server.on("error", reject)
|
|
|
|
this.server.on("upgrade", this.onUpgrade)
|
|
|
|
const onListen = (): void => resolve(this.address())
|
|
|
|
if (this.options.socket) {
|
|
|
|
this.server.listen(this.options.socket, onListen)
|
|
|
|
} else {
|
|
|
|
this.server.listen(this.options.port, this.options.host, onListen)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return this.listenPromise
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The *local* address of the server.
|
|
|
|
*/
|
|
|
|
public address(): string | null {
|
|
|
|
const address = this.server.address()
|
|
|
|
const endpoint =
|
|
|
|
typeof address !== "string" && address !== null
|
|
|
|
? (address.address === "::" ? "localhost" : address.address) + ":" + address.port
|
|
|
|
: address
|
|
|
|
return endpoint && `${this.protocol}://${endpoint}`
|
|
|
|
}
|
|
|
|
|
|
|
|
private onRequest = async (request: http.IncomingMessage, response: http.ServerResponse): Promise<void> => {
|
2020-02-14 05:38:05 +07:00
|
|
|
const route = this.parseUrl(request)
|
2020-08-31 22:29:12 +07:00
|
|
|
if (route.providerBase !== "/healthz") {
|
|
|
|
this.heart.beat()
|
|
|
|
}
|
2020-03-17 00:43:32 +07:00
|
|
|
const write = (payload: HttpResponse): void => {
|
2020-02-05 02:27:46 +07:00
|
|
|
response.writeHead(payload.redirect ? HttpCode.Redirect : payload.code || HttpCode.Ok, {
|
|
|
|
"Content-Type": payload.mime || getMediaMime(payload.filePath),
|
2020-02-14 05:38:05 +07:00
|
|
|
...(payload.redirect ? { Location: this.constructRedirect(request, route, payload as RedirectResponse) } : {}),
|
2020-02-06 06:30:09 +07:00
|
|
|
...(request.headers["service-worker"] ? { "Service-Worker-Allowed": route.provider.base(route) } : {}),
|
2020-02-05 02:27:46 +07:00
|
|
|
...(payload.cache ? { "Cache-Control": "public, max-age=31536000" } : {}),
|
|
|
|
...(payload.cookie
|
|
|
|
? {
|
2020-02-14 05:38:05 +07:00
|
|
|
"Set-Cookie": [
|
|
|
|
`${payload.cookie.key}=${payload.cookie.value}`,
|
|
|
|
`Path=${normalize(payload.cookie.path || "/", true)}`,
|
2020-04-14 04:14:40 +07:00
|
|
|
this.getCookieDomain(request.headers.host || ""),
|
2020-03-17 01:02:33 +07:00
|
|
|
// "HttpOnly",
|
2020-04-01 02:56:01 +07:00
|
|
|
"SameSite=lax",
|
2020-03-24 00:48:10 +07:00
|
|
|
]
|
|
|
|
.filter((l) => !!l)
|
|
|
|
.join(";"),
|
2020-02-05 02:27:46 +07:00
|
|
|
}
|
|
|
|
: {}),
|
|
|
|
...payload.headers,
|
|
|
|
})
|
|
|
|
if (payload.stream) {
|
|
|
|
payload.stream.on("error", (error: NodeJS.ErrnoException) => {
|
|
|
|
response.writeHead(error.code === "ENOENT" ? HttpCode.NotFound : HttpCode.ServerError)
|
|
|
|
response.end(error.message)
|
|
|
|
})
|
2020-02-21 04:50:01 +07:00
|
|
|
payload.stream.on("close", () => response.end())
|
2020-02-05 02:27:46 +07:00
|
|
|
payload.stream.pipe(response)
|
|
|
|
} else if (typeof payload.content === "string" || payload.content instanceof Buffer) {
|
|
|
|
response.end(payload.content)
|
|
|
|
} else if (payload.content && typeof payload.content === "object") {
|
|
|
|
response.end(JSON.stringify(payload.content))
|
|
|
|
} else {
|
|
|
|
response.end()
|
|
|
|
}
|
2020-03-17 00:43:32 +07:00
|
|
|
}
|
2020-03-24 01:47:01 +07:00
|
|
|
|
2020-03-17 00:43:32 +07:00
|
|
|
try {
|
2020-03-24 01:47:01 +07:00
|
|
|
const payload =
|
|
|
|
this.maybeRedirect(request, route) ||
|
2020-04-03 01:09:09 +07:00
|
|
|
(route.provider.authenticated(request) && this.maybeProxy(request)) ||
|
|
|
|
(await route.provider.handleRequest(route, request))
|
|
|
|
if (payload.proxy) {
|
|
|
|
this.doProxy(route, request, response, payload.proxy)
|
|
|
|
} else {
|
2020-03-24 06:02:31 +07:00
|
|
|
write(payload)
|
2020-03-17 00:43:32 +07:00
|
|
|
}
|
2020-02-05 02:27:46 +07:00
|
|
|
} catch (error) {
|
|
|
|
let e = error
|
|
|
|
if (error.code === "ENOENT" || error.code === "EISDIR") {
|
|
|
|
e = new HttpError("Not found", HttpCode.NotFound)
|
|
|
|
}
|
2020-02-14 05:38:05 +07:00
|
|
|
const code = typeof e.code === "number" ? e.code : HttpCode.ServerError
|
2020-07-29 03:06:15 +07:00
|
|
|
logger.debug("Request error", field("url", request.url), field("code", code), field("error", error))
|
2020-04-01 02:56:01 +07:00
|
|
|
if (code >= HttpCode.ServerError) {
|
|
|
|
logger.error(error.stack)
|
|
|
|
}
|
2020-04-18 02:58:14 +07:00
|
|
|
if (request.headers["content-type"] === "application/json") {
|
|
|
|
write({
|
|
|
|
code,
|
2020-07-24 00:23:33 +07:00
|
|
|
mime: "application/json",
|
2020-04-18 02:58:14 +07:00
|
|
|
content: {
|
|
|
|
error: e.message,
|
2020-07-24 00:23:33 +07:00
|
|
|
...(e.details || {}),
|
2020-04-18 02:58:14 +07:00
|
|
|
},
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
write({
|
|
|
|
code,
|
|
|
|
...(await route.provider.getErrorRoot(route, code, code, e.message)),
|
|
|
|
})
|
|
|
|
}
|
2020-02-05 02:27:46 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return any necessary redirection before delegating to a provider.
|
|
|
|
*/
|
2020-02-14 05:38:05 +07:00
|
|
|
private maybeRedirect(request: http.IncomingMessage, route: ProviderRoute): RedirectResponse | undefined {
|
|
|
|
// If we're handling TLS ensure all requests are redirected to HTTPS.
|
2020-02-05 02:27:46 +07:00
|
|
|
if (this.options.cert && !(request.connection as tls.TLSSocket).encrypted) {
|
2020-02-14 05:38:05 +07:00
|
|
|
return { redirect: route.fullPath }
|
2020-02-05 02:27:46 +07:00
|
|
|
}
|
2020-02-06 06:30:09 +07:00
|
|
|
|
2020-02-05 02:27:46 +07:00
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
2020-02-14 05:38:05 +07:00
|
|
|
/**
|
|
|
|
* Given a path that goes from the base, construct a relative redirect URL
|
|
|
|
* that will get you there considering that the app may be served from an
|
|
|
|
* unknown base path. If handling TLS, also ensure HTTPS.
|
|
|
|
*/
|
|
|
|
private constructRedirect(request: http.IncomingMessage, route: ProviderRoute, payload: RedirectResponse): string {
|
|
|
|
const query = {
|
|
|
|
...route.query,
|
|
|
|
...(payload.query || {}),
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.keys(query).forEach((key) => {
|
|
|
|
if (typeof query[key] === "undefined") {
|
|
|
|
delete query[key]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-02-20 06:00:58 +07:00
|
|
|
const secure = (request.connection as tls.TLSSocket).encrypted
|
|
|
|
const redirect =
|
|
|
|
(this.options.cert && !secure ? `${this.protocol}://${request.headers.host}/` : "") +
|
2020-02-14 05:38:05 +07:00
|
|
|
normalize(`${route.provider.base(route)}/${payload.redirect}`, true) +
|
|
|
|
(Object.keys(query).length > 0 ? `?${querystring.stringify(query)}` : "")
|
2020-03-17 00:43:32 +07:00
|
|
|
logger.debug("redirecting", field("secure", !!secure), field("from", request.url), field("to", redirect))
|
2020-02-20 06:00:58 +07:00
|
|
|
return redirect
|
2020-02-14 05:38:05 +07:00
|
|
|
}
|
|
|
|
|
2020-02-05 02:27:46 +07:00
|
|
|
private onUpgrade = async (request: http.IncomingMessage, socket: net.Socket, head: Buffer): Promise<void> => {
|
|
|
|
try {
|
|
|
|
this.heart.beat()
|
|
|
|
socket.on("error", () => socket.destroy())
|
|
|
|
|
|
|
|
if (this.options.cert && !(socket as tls.TLSSocket).encrypted) {
|
|
|
|
throw new HttpError("HTTP websocket", HttpCode.BadRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!request.headers.upgrade || request.headers.upgrade.toLowerCase() !== "websocket") {
|
|
|
|
throw new HttpError("HTTP/1.1 400 Bad Request", HttpCode.BadRequest)
|
|
|
|
}
|
|
|
|
|
2020-02-06 06:30:09 +07:00
|
|
|
const route = this.parseUrl(request)
|
|
|
|
if (!route.provider) {
|
2020-02-05 02:27:46 +07:00
|
|
|
throw new HttpError("Not found", HttpCode.NotFound)
|
|
|
|
}
|
|
|
|
|
2020-04-03 01:09:09 +07:00
|
|
|
// The socket proxy is so we can pass them to child processes (TLS sockets
|
|
|
|
// can't be transferred so we need an in-between).
|
|
|
|
const socketProxy = await this.socketProvider.createProxy(socket)
|
|
|
|
const payload =
|
|
|
|
this.maybeProxy(request) || (await route.provider.handleWebSocket(route, request, socketProxy, head))
|
|
|
|
if (payload && payload.proxy) {
|
|
|
|
this.doProxy(route, request, { socket: socketProxy, head }, payload.proxy)
|
2020-03-24 06:02:31 +07:00
|
|
|
}
|
2020-02-05 02:27:46 +07:00
|
|
|
} catch (error) {
|
|
|
|
socket.destroy(error)
|
|
|
|
logger.warn(`discarding socket connection: ${error.message}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a request URL so we can route it.
|
|
|
|
*/
|
|
|
|
private parseUrl(request: http.IncomingMessage): ProviderRoute {
|
|
|
|
const parse = (fullPath: string): { base: string; requestPath: string } => {
|
|
|
|
const match = fullPath.match(/^(\/?[^/]*)(.*)$/)
|
|
|
|
let [, /* ignore */ base, requestPath] = match ? match.map((p) => p.replace(/\/+$/, "")) : ["", "", ""]
|
|
|
|
if (base.indexOf(".") !== -1) {
|
|
|
|
// Assume it's a file at the root.
|
|
|
|
requestPath = base
|
|
|
|
base = "/"
|
|
|
|
} else if (base === "") {
|
|
|
|
// Happens if it's a plain `domain.com`.
|
|
|
|
base = "/"
|
|
|
|
}
|
|
|
|
return { base, requestPath }
|
|
|
|
}
|
|
|
|
|
|
|
|
const parsedUrl = request.url ? url.parse(request.url, true) : { query: {}, pathname: "" }
|
2020-02-14 01:40:36 +07:00
|
|
|
const originalPath = parsedUrl.pathname || "/"
|
2020-02-20 06:00:58 +07:00
|
|
|
const fullPath = normalize(originalPath, true)
|
2020-02-05 02:27:46 +07:00
|
|
|
const { base, requestPath } = parse(fullPath)
|
|
|
|
|
|
|
|
// Providers match on the path after their base so we need to account for
|
|
|
|
// that by shifting the next base out of the request path.
|
|
|
|
let provider = this.providers.get(base)
|
|
|
|
if (base !== "/" && provider) {
|
2020-07-23 02:53:15 +07:00
|
|
|
return { ...parse(requestPath), providerBase: base, fullPath, query: parsedUrl.query, provider, originalPath }
|
2020-02-05 02:27:46 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fall back to the top-level provider.
|
|
|
|
provider = this.providers.get("/")
|
|
|
|
if (!provider) {
|
|
|
|
throw new Error(`No provider for ${base}`)
|
|
|
|
}
|
2020-07-23 02:53:15 +07:00
|
|
|
return { base, providerBase: "/", fullPath, requestPath, query: parsedUrl.query, provider, originalPath }
|
2020-02-05 02:27:46 +07:00
|
|
|
}
|
2020-04-03 01:09:09 +07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Proxy a request to the target.
|
|
|
|
*/
|
|
|
|
private doProxy(
|
|
|
|
route: Route,
|
|
|
|
request: http.IncomingMessage,
|
|
|
|
response: http.ServerResponse,
|
|
|
|
options: ProxyOptions,
|
|
|
|
): void
|
|
|
|
/**
|
|
|
|
* Proxy a web socket to the target.
|
|
|
|
*/
|
|
|
|
private doProxy(
|
|
|
|
route: Route,
|
|
|
|
request: http.IncomingMessage,
|
|
|
|
response: { socket: net.Socket; head: Buffer },
|
|
|
|
options: ProxyOptions,
|
|
|
|
): void
|
|
|
|
/**
|
|
|
|
* Proxy a request or web socket to the target.
|
|
|
|
*/
|
|
|
|
private doProxy(
|
|
|
|
route: Route,
|
|
|
|
request: http.IncomingMessage,
|
|
|
|
response: http.ServerResponse | { socket: net.Socket; head: Buffer },
|
|
|
|
options: ProxyOptions,
|
|
|
|
): void {
|
|
|
|
const port = parseInt(options.port, 10)
|
|
|
|
if (isNaN(port)) {
|
|
|
|
throw new HttpError(`"${options.port}" is not a valid number`, HttpCode.BadRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
// REVIEW: Absolute redirects need to be based on the subpath but I'm not
|
|
|
|
// sure how best to get this information to the `proxyRes` event handler.
|
|
|
|
// For now I'm sticking it on the request object which is passed through to
|
|
|
|
// the event.
|
2020-07-24 00:37:45 +07:00
|
|
|
;(request as ProxyRequest).base = options.strip
|
2020-04-03 01:09:09 +07:00
|
|
|
|
|
|
|
const isHttp = response instanceof http.ServerResponse
|
2020-07-24 00:37:45 +07:00
|
|
|
const base = options.strip ? route.fullPath.replace(options.strip, "") : route.fullPath
|
|
|
|
const path = normalize("/" + (options.prepend || "") + "/" + base, true)
|
2020-04-03 01:09:09 +07:00
|
|
|
const proxyOptions: proxy.ServerOptions = {
|
|
|
|
changeOrigin: true,
|
|
|
|
ignorePath: true,
|
|
|
|
target: `${isHttp ? "http" : "ws"}://127.0.0.1:${port}${path}${
|
|
|
|
Object.keys(route.query).length > 0 ? `?${querystring.stringify(route.query)}` : ""
|
|
|
|
}`,
|
|
|
|
ws: !isHttp,
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response instanceof http.ServerResponse) {
|
|
|
|
this.proxy.web(request, response, proxyOptions)
|
|
|
|
} else {
|
|
|
|
this.proxy.ws(request, response.socket, response.head, proxyOptions)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-14 04:14:40 +07:00
|
|
|
* Get the value that should be used for setting a cookie domain. This will
|
|
|
|
* allow the user to authenticate only once. This will use the highest level
|
2020-04-03 01:09:09 +07:00
|
|
|
* domain (e.g. `coder.com` over `test.coder.com` if both are specified).
|
|
|
|
*/
|
2020-04-14 04:14:40 +07:00
|
|
|
private getCookieDomain(host: string): string | undefined {
|
|
|
|
const idx = host.lastIndexOf(":")
|
|
|
|
host = idx !== -1 ? host.substring(0, idx) : host
|
|
|
|
if (
|
|
|
|
// Might be blank/missing, so there's nothing more to do.
|
|
|
|
!host ||
|
|
|
|
// IP addresses can't have subdomains so there's no value in setting the
|
|
|
|
// domain for them. Assume anything with a : is ipv6 (valid domain name
|
|
|
|
// characters are alphanumeric or dashes).
|
|
|
|
host.includes(":") ||
|
|
|
|
// Assume anything entirely numbers and dots is ipv4 (currently tlds
|
|
|
|
// cannot be entirely numbers).
|
|
|
|
!/[^0-9.]/.test(host) ||
|
|
|
|
// localhost subdomains don't seem to work at all (browser bug?).
|
|
|
|
host.endsWith(".localhost") ||
|
|
|
|
// It might be localhost (or an IP, see above) if it's a proxy and it
|
|
|
|
// isn't setting the host header to match the access domain.
|
|
|
|
host === "localhost"
|
|
|
|
) {
|
2020-08-05 03:01:18 +07:00
|
|
|
logger.debug("no valid cookie doman", field("host", host))
|
2020-04-14 04:14:40 +07:00
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
2020-04-03 01:09:09 +07:00
|
|
|
this.proxyDomains.forEach((domain) => {
|
2020-04-14 04:14:40 +07:00
|
|
|
if (host.endsWith(domain) && domain.length < host.length) {
|
|
|
|
host = domain
|
2020-04-03 01:09:09 +07:00
|
|
|
}
|
|
|
|
})
|
2020-04-14 04:14:40 +07:00
|
|
|
|
2020-08-05 03:01:18 +07:00
|
|
|
logger.debug("got cookie doman", field("host", host))
|
2020-04-14 04:14:40 +07:00
|
|
|
return host ? `Domain=${host}` : undefined
|
2020-04-03 01:09:09 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a response if the request should be proxied. Anything that ends in a
|
|
|
|
* proxy domain and has a *single* subdomain should be proxied. Anything else
|
|
|
|
* should return `undefined` and will be handled as normal.
|
|
|
|
*
|
|
|
|
* For example if `coder.com` is specified `8080.coder.com` will be proxied
|
|
|
|
* but `8080.test.coder.com` and `test.8080.coder.com` will not.
|
|
|
|
*/
|
|
|
|
public maybeProxy(request: http.IncomingMessage): HttpResponse | undefined {
|
|
|
|
// Split into parts.
|
|
|
|
const host = request.headers.host || ""
|
|
|
|
const idx = host.indexOf(":")
|
|
|
|
const domain = idx !== -1 ? host.substring(0, idx) : host
|
|
|
|
const parts = domain.split(".")
|
|
|
|
|
|
|
|
// There must be an exact match.
|
|
|
|
const port = parts.shift()
|
|
|
|
const proxyDomain = parts.join(".")
|
|
|
|
if (!port || !this.proxyDomains.has(proxyDomain)) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
proxy: {
|
|
|
|
port,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2020-02-05 02:27:46 +07:00
|
|
|
}
|