2020-03-24 01:47:01 +07:00
|
|
|
import * as http from "http"
|
|
|
|
import { HttpCode, HttpError } from "../../common/http"
|
2020-04-03 01:09:09 +07:00
|
|
|
import { HttpProvider, HttpResponse, Route, WsResponse } from "../http"
|
2020-04-01 02:56:01 +07:00
|
|
|
|
2020-03-24 01:47:01 +07:00
|
|
|
/**
|
|
|
|
* Proxy HTTP provider.
|
|
|
|
*/
|
2020-04-03 01:09:09 +07:00
|
|
|
export class ProxyHttpProvider extends HttpProvider {
|
|
|
|
public async handleRequest(route: Route, request: http.IncomingMessage): Promise<HttpResponse> {
|
2020-03-24 02:26:47 +07:00
|
|
|
if (!this.authenticated(request)) {
|
2020-04-01 23:28:09 +07:00
|
|
|
if (this.isRoot(route)) {
|
2020-04-01 00:59:07 +07:00
|
|
|
return { redirect: "/login", query: { to: route.fullPath } }
|
2020-03-24 02:26:47 +07:00
|
|
|
}
|
2020-04-01 00:59:07 +07:00
|
|
|
throw new HttpError("Unauthorized", HttpCode.Unauthorized)
|
2020-03-24 01:47:01 +07:00
|
|
|
}
|
2020-03-24 02:26:47 +07:00
|
|
|
|
2020-04-01 00:59:07 +07:00
|
|
|
// Ensure there is a trailing slash so relative paths work correctly.
|
2020-04-01 23:28:09 +07:00
|
|
|
if (this.isRoot(route) && !route.fullPath.endsWith("/")) {
|
2020-04-01 00:59:07 +07:00
|
|
|
return {
|
2020-04-02 23:32:19 +07:00
|
|
|
redirect: `${route.fullPath}/`,
|
2020-04-01 00:59:07 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 23:32:19 +07:00
|
|
|
const port = route.base.replace(/^\//, "")
|
2020-04-03 01:09:09 +07:00
|
|
|
return {
|
|
|
|
proxy: {
|
|
|
|
base: `${this.options.base}/${port}`,
|
|
|
|
port,
|
|
|
|
},
|
2020-03-24 01:47:01 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 01:09:09 +07:00
|
|
|
public async handleWebSocket(route: Route, request: http.IncomingMessage): Promise<WsResponse> {
|
2020-03-24 06:02:31 +07:00
|
|
|
this.ensureAuthenticated(request)
|
2020-04-01 02:56:01 +07:00
|
|
|
const port = route.base.replace(/^\//, "")
|
2020-04-03 01:09:09 +07:00
|
|
|
return {
|
|
|
|
proxy: {
|
|
|
|
base: `${this.options.base}/${port}`,
|
|
|
|
port,
|
|
|
|
},
|
2020-03-24 06:02:31 +07:00
|
|
|
}
|
2020-03-24 01:47:01 +07:00
|
|
|
}
|
|
|
|
}
|