aboutsummaryrefslogtreecommitdiff
path: root/src/server/server_Initialization.ts
diff options
context:
space:
mode:
authorBob Zeleznik <zzzman@gmail.com>2020-03-17 12:10:47 -0400
committerBob Zeleznik <zzzman@gmail.com>2020-03-17 12:10:47 -0400
commit43580a656b88b287f3c62528fb51513e0ee65f6e (patch)
tree87d310b113e48433147094a9ddf2ea327f15d0f2 /src/server/server_Initialization.ts
parent1b21c5234a179df337ad51f55c9f82b9d7022c25 (diff)
final fixes hopefully to web pages and cors stuff
Diffstat (limited to 'src/server/server_Initialization.ts')
-rw-r--r--src/server/server_Initialization.ts83
1 files changed, 37 insertions, 46 deletions
diff --git a/src/server/server_Initialization.ts b/src/server/server_Initialization.ts
index 3ba9cc474..76cb22521 100644
--- a/src/server/server_Initialization.ts
+++ b/src/server/server_Initialization.ts
@@ -36,24 +36,7 @@ export default async function InitializeServer(routeSetter: RouteSetter) {
setHeaders: res => res.setHeader("Access-Control-Allow-Origin", "*")
}));
app.use("/images", express.static(publicDirectory));
- const corsOptions = {
- origin: function (_origin: any, callback: any) {
- callback(null, true);
- }
- };
- app.use(cors(corsOptions));
- // app.use("*", ({ user, originalUrl }, res, next) => {
- // if (user && !originalUrl.includes("Heartbeat")) {
- // const userEmail = (user as any).email;
- // if (userEmail) {
- // timeMap[userEmail] = Date.now();
- // }
- // }
- // if (!user && originalUrl === "/") {
- // return res.redirect("/login");
- // }
- // next();
- // });
+ app.use(cors({ origin: (_origin: any, callback: any) => callback(null, true) }));
app.use(wdm(compiler, { publicPath: config.output.publicPath }));
app.use(whm(compiler));
@@ -61,7 +44,6 @@ export default async function InitializeServer(routeSetter: RouteSetter) {
registerAuthenticationRoutes(app);
registerCorsProxy(app);
-
const isRelease = determineEnvironment();
routeSetter(new RouteManager(app, isRelease));
@@ -69,7 +51,7 @@ export default async function InitializeServer(routeSetter: RouteSetter) {
const serverPort = isRelease ? Number(process.env.serverPort) : 1050;
const server = app.listen(serverPort, () => {
- logPort("server", Number(serverPort));
+ logPort("server", serverPort);
console.log();
});
disconnect = async () => new Promise<Error>(resolve => server.close(resolve));
@@ -140,38 +122,47 @@ function registerAuthenticationRoutes(server: express.Express) {
function registerCorsProxy(server: express.Express) {
const headerCharRegex = /[^\t\x20-\x7e\x80-\xff]/;
server.use("/corsProxy", (req, res) => {
- req.pipe(request(decodeURIComponent(req.url.substring(1)))).on("response", res => {
- const headers = Object.keys(res.headers);
- headers.forEach(headerName => {
- const header = res.headers[headerName];
- if (Array.isArray(header)) {
- res.headers[headerName] = header.filter(h => !headerCharRegex.test(h));
- } else if (header) {
- if (headerCharRegex.test(header as any)) {
- delete res.headers[headerName];
+
+ let requrl = decodeURIComponent(req.url.substring(1));
+ const referer = req.headers.referer ? decodeURIComponent(req.headers.referer) : "";
+ // cors weirdness here...
+ // if the referer is a cors page and the cors() route (I think) redirected to /corsProxy/<path> and the requested url path was relative,
+ // then we redirect again to the cors referer and just add the relative path.
+ if (!requrl.startsWith("http") && req.originalUrl.startsWith("/corsProxy") && referer?.includes("corsProxy")) {
+ res.redirect(referer + requrl);
+ }
+ else {
+ req.pipe(request(requrl)).on("response", res => {
+ const headers = Object.keys(res.headers);
+ headers.forEach(headerName => {
+ const header = res.headers[headerName];
+ if (Array.isArray(header)) {
+ res.headers[headerName] = header.filter(h => !headerCharRegex.test(h));
+ } else if (header) {
+ if (headerCharRegex.test(header as any)) {
+ delete res.headers[headerName];
+ }
}
- }
- });
- }).pipe(res);
+ });
+ }).pipe(res);
+ }
});
}
function registerRelativePath(server: express.Express) {
server.use("*", (req, res) => {
- const target = req.originalUrl;
- if (!res.headersSent && req.headers.referer?.includes("corsProxy")) {
- const url = decodeURIComponent(req.headers.referer);
- const start = url.match(/.*corsProxy\//)![0];
- const original = url.replace(start, "");
- const theurl = original.match(/http[s]?:\/\/[^\/]*/)![0];
- const newdirect = start + encodeURIComponent(theurl + target);
- res.redirect(newdirect);
- return;
- } else if (target.startsWith("/search")) {
- const newdirect = req.headers.referer + "corsProxy/" + encodeURIComponent("http://www.google.com" + target);
- res.redirect(newdirect);
- return;
+ const relativeUrl = req.originalUrl;
+ if (!res.headersSent && req.headers.referer?.includes("corsProxy")) { // a request for something by a proxied referrer means it must be a relative reference. So construct a proxied absolute reference here.
+ const proxiedRefererUrl = decodeURIComponent(req.headers.referer); // (e.g., http://localhost:1050/corsProxy/https://en.wikipedia.org/wiki/Engelbart)
+ const dashServerUrl = proxiedRefererUrl.match(/.*corsProxy\//)![0]; // the dash server url (e.g.: http://localhost:1050/corsProxy/ )
+ const actualReferUrl = proxiedRefererUrl.replace(dashServerUrl, ""); // the url of the referer without the proxy (e.g., : http:s//en.wikipedia.org/wiki/Engelbart)
+ const absoluteTargetBaseUrl = actualReferUrl.match(/http[s]?:\/\/[^\/]*/)![0]; // the base of the original url (e.g., https://en.wikipedia.org)
+ const redirectedProxiedUrl = dashServerUrl + encodeURIComponent(absoluteTargetBaseUrl + relativeUrl); // the new proxied full url (e..g, http://localhost:1050/corsProxy/https://en.wikipedia.org/<somethingelse>)
+ res.redirect(redirectedProxiedUrl);
+ } else if (relativeUrl.startsWith("/search")) { // detect search query and use default search engine
+ res.redirect(req.headers.referer + "corsProxy/" + encodeURIComponent("http://www.google.com" + relativeUrl));
+ } else {
+ res.end();
}
- res.end();
});
}