2020-03-24 01:47:01 +07:00
|
|
|
import * as http from "http"
|
|
|
|
import { HttpCode, HttpError } from "../../common/http"
|
2020-03-24 02:26:47 +07:00
|
|
|
import { HttpProvider, HttpProviderOptions, HttpProxyProvider, HttpResponse, Route } from "../http"
|
2020-03-24 01:47:01 +07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Proxy HTTP provider.
|
|
|
|
*/
|
2020-03-24 02:26:47 +07:00
|
|
|
export class ProxyHttpProvider extends HttpProvider implements HttpProxyProvider {
|
|
|
|
public readonly proxyDomains: string[]
|
|
|
|
|
|
|
|
public constructor(options: HttpProviderOptions, proxyDomains: string[] = []) {
|
2020-03-24 01:47:01 +07:00
|
|
|
super(options)
|
2020-03-24 02:26:47 +07:00
|
|
|
this.proxyDomains = proxyDomains.map((d) => d.replace(/^\*\./, "")).filter((d, i, arr) => arr.indexOf(d) === i)
|
2020-03-24 01:47:01 +07:00
|
|
|
}
|
|
|
|
|
2020-03-24 02:26:47 +07:00
|
|
|
public async handleRequest(route: Route, request: http.IncomingMessage): Promise<HttpResponse> {
|
|
|
|
if (!this.authenticated(request)) {
|
|
|
|
if (route.requestPath === "/index.html") {
|
|
|
|
return { redirect: "/login", query: { to: route.fullPath } }
|
|
|
|
}
|
|
|
|
throw new HttpError("Unauthorized", HttpCode.Unauthorized)
|
2020-03-24 01:47:01 +07:00
|
|
|
}
|
2020-03-24 02:26:47 +07:00
|
|
|
|
2020-03-24 01:47:01 +07:00
|
|
|
const payload = this.proxy(route.base.replace(/^\//, ""))
|
2020-03-24 02:26:47 +07:00
|
|
|
if (payload) {
|
|
|
|
return payload
|
2020-03-24 01:47:01 +07:00
|
|
|
}
|
2020-03-24 02:26:47 +07:00
|
|
|
|
|
|
|
throw new HttpError("Not found", HttpCode.NotFound)
|
2020-03-24 01:47:01 +07:00
|
|
|
}
|
|
|
|
|
2020-03-24 02:26:47 +07:00
|
|
|
public getProxyDomain(host?: string): string | undefined {
|
|
|
|
if (!host || !this.proxyDomains) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.proxyDomains.find((d) => host.endsWith(d))
|
2020-03-24 01:47:01 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
public maybeProxy(request: http.IncomingMessage): HttpResponse | undefined {
|
2020-03-24 02:26:47 +07:00
|
|
|
// No proxy until we're authenticated. This will cause the login page to
|
|
|
|
// show as well as let our assets keep loading normally.
|
|
|
|
if (!this.authenticated(request)) {
|
2020-03-24 01:47:01 +07:00
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
2020-03-24 02:26:47 +07:00
|
|
|
const host = request.headers.host
|
|
|
|
const proxyDomain = this.getProxyDomain(host)
|
|
|
|
if (!host || !proxyDomain) {
|
2020-03-24 01:47:01 +07:00
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
const proxyDomainLength = proxyDomain.split(".").length
|
|
|
|
const portStr = host
|
|
|
|
.split(".")
|
|
|
|
.slice(0, -proxyDomainLength)
|
|
|
|
.pop()
|
|
|
|
|
|
|
|
if (!portStr) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.proxy(portStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
private proxy(portStr: string): HttpResponse {
|
|
|
|
if (!portStr) {
|
|
|
|
return {
|
|
|
|
code: HttpCode.BadRequest,
|
|
|
|
content: "Port must be provided",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const port = parseInt(portStr, 10)
|
|
|
|
if (isNaN(port)) {
|
|
|
|
return {
|
|
|
|
code: HttpCode.BadRequest,
|
|
|
|
content: `"${portStr}" is not a valid number`,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
code: HttpCode.Ok,
|
|
|
|
content: `will proxy this to ${port}`,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|