2021-02-04 22:29:44 +00:00
|
|
|
import { Request, Response } from "express"
|
|
|
|
import * as path from "path"
|
2020-10-20 23:05:58 +00:00
|
|
|
import qs from "qs"
|
2021-01-20 20:11:08 +00:00
|
|
|
import * as pluginapi from "../../../typings/pluginapi"
|
2020-03-23 18:47:01 +00:00
|
|
|
import { HttpCode, HttpError } from "../../common/http"
|
2020-11-11 00:14:18 +00:00
|
|
|
import { normalize } from "../../common/util"
|
2020-11-05 22:45:58 +00:00
|
|
|
import { authenticated, ensureAuthenticated, redirect } from "../http"
|
2021-02-04 22:29:44 +00:00
|
|
|
import { proxy as _proxy } from "../proxy"
|
2020-03-31 19:56:01 +00:00
|
|
|
|
2021-02-04 22:29:44 +00:00
|
|
|
const getProxyTarget = (req: Request, passthroughPath?: boolean): string => {
|
2021-01-11 18:17:45 +00:00
|
|
|
if (passthroughPath) {
|
|
|
|
return `http://0.0.0.0:${req.params.port}/${req.originalUrl}`
|
2020-03-23 18:47:01 +00:00
|
|
|
}
|
2021-01-11 18:17:45 +00:00
|
|
|
const query = qs.stringify(req.query)
|
|
|
|
return `http://0.0.0.0:${req.params.port}/${req.params[0] || ""}${query ? `?${query}` : ""}`
|
2020-10-20 23:05:58 +00:00
|
|
|
}
|
2020-03-23 18:47:01 +00:00
|
|
|
|
2021-02-04 22:29:44 +00:00
|
|
|
export function proxy(
|
|
|
|
req: Request,
|
|
|
|
res: Response,
|
|
|
|
opts?: {
|
|
|
|
passthroughPath?: boolean
|
|
|
|
},
|
|
|
|
): void {
|
2020-10-20 23:05:58 +00:00
|
|
|
if (!authenticated(req)) {
|
2020-11-11 00:14:18 +00:00
|
|
|
// If visiting the root (/:port only) redirect to the login page.
|
2020-10-20 23:05:58 +00:00
|
|
|
if (!req.params[0] || req.params[0] === "/") {
|
2020-11-11 00:14:18 +00:00
|
|
|
const to = normalize(`${req.baseUrl}${req.path}`)
|
2020-10-20 23:05:58 +00:00
|
|
|
return redirect(req, res, "login", {
|
2020-11-11 00:14:18 +00:00
|
|
|
to: to !== "/" ? to : undefined,
|
2020-10-20 23:05:58 +00:00
|
|
|
})
|
2020-03-23 23:02:31 +00:00
|
|
|
}
|
2020-10-20 23:05:58 +00:00
|
|
|
throw new HttpError("Unauthorized", HttpCode.Unauthorized)
|
2020-03-23 18:47:01 +00:00
|
|
|
}
|
2020-10-20 23:05:58 +00:00
|
|
|
|
2021-02-04 22:29:44 +00:00
|
|
|
if (!opts?.passthroughPath) {
|
2021-01-20 19:26:15 +00:00
|
|
|
// Absolute redirects need to be based on the subpath when rewriting.
|
2021-02-04 22:29:44 +00:00
|
|
|
// See proxy.ts.
|
|
|
|
;(req as any).base = req.path.split(path.sep).slice(0, 3).join(path.sep)
|
2021-01-20 19:26:15 +00:00
|
|
|
}
|
2020-10-20 23:05:58 +00:00
|
|
|
|
2021-02-04 22:29:44 +00:00
|
|
|
_proxy.web(req, res, {
|
2020-10-20 23:05:58 +00:00
|
|
|
ignorePath: true,
|
2021-02-04 22:29:44 +00:00
|
|
|
target: getProxyTarget(req, opts?.passthroughPath),
|
2020-10-20 23:05:58 +00:00
|
|
|
})
|
2021-02-04 22:29:44 +00:00
|
|
|
}
|
2020-11-05 18:58:37 +00:00
|
|
|
|
2021-06-02 20:27:55 +00:00
|
|
|
export async function wsProxy(
|
2021-01-20 20:11:08 +00:00
|
|
|
req: pluginapi.WebsocketRequest,
|
2021-02-04 22:29:44 +00:00
|
|
|
opts?: {
|
|
|
|
passthroughPath?: boolean
|
|
|
|
},
|
2021-06-02 20:27:55 +00:00
|
|
|
): Promise<void> {
|
|
|
|
await ensureAuthenticated(req)
|
2021-02-04 22:29:44 +00:00
|
|
|
_proxy.ws(req, req.ws, req.head, {
|
2020-10-20 23:05:58 +00:00
|
|
|
ignorePath: true,
|
2021-02-04 22:29:44 +00:00
|
|
|
target: getProxyTarget(req, opts?.passthroughPath),
|
2020-10-20 23:05:58 +00:00
|
|
|
})
|
2021-02-04 22:29:44 +00:00
|
|
|
}
|