Skip to content

Make assets unique #518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions build/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,21 @@ const buildServerBinaryCopy = register("build:server:binary:copy", async (runner
}
fse.copySync(defaultExtensionsPath, path.join(cliBuildPath, "extensions"));
fs.writeFileSync(path.join(cliBuildPath, "bootstrap-fork.js.gz"), zlib.gzipSync(fs.readFileSync(bootstrapForkPath)));
const cpDir = (dir: string, subdir: "auth" | "unauth", rootPath: string): void => {
const cpDir = (dir: string, rootPath: string, subdir?: "login"): void => {
const stat = fs.statSync(dir);
if (stat.isDirectory()) {
const paths = fs.readdirSync(dir);
paths.forEach((p) => cpDir(path.join(dir, p), subdir, rootPath));
paths.forEach((p) => cpDir(path.join(dir, p), rootPath, subdir));
} else if (stat.isFile()) {
const newPath = path.join(cliBuildPath, "web", subdir, path.relative(rootPath, dir));
const newPath = path.join(cliBuildPath, "web", subdir || "", path.relative(rootPath, dir));
fse.mkdirpSync(path.dirname(newPath));
fs.writeFileSync(newPath + ".gz", zlib.gzipSync(fs.readFileSync(dir)));
} else {
// Nothing
}
};
cpDir(webOutputPath, "auth", webOutputPath);
cpDir(browserAppOutputPath, "unauth", browserAppOutputPath);
cpDir(webOutputPath, webOutputPath);
cpDir(browserAppOutputPath, browserAppOutputPath, "login");
fse.mkdirpSync(path.join(cliBuildPath, "dependencies"));
fse.copySync(ripgrepPath, path.join(cliBuildPath, "dependencies", "rg"));
});
Expand Down
4 changes: 3 additions & 1 deletion packages/app/browser/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ if (!form) {

form.addEventListener("submit", (e) => {
e.preventDefault();
document.cookie = `password=${password.value}`;
document.cookie = `password=${password.value}; `
+ `path=${location.pathname.replace(/\/login\/?$/, "/")}; `
+ `domain=${location.hostname}`;
location.reload();
});

Expand Down
9 changes: 4 additions & 5 deletions packages/app/browser/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ const root = path.resolve(__dirname, "../../..");

module.exports = merge(
require(path.join(root, "scripts/webpack.client.config.js"))({
entry: path.join(root, "packages/app/browser/src/app.ts"),
template: path.join(root, "packages/app/browser/src/app.html"),
dirname: __dirname,
entry: path.join(__dirname, "src/app.ts"),
name: "login",
template: path.join(__dirname, "src/app.html"),
}), {
output: {
path: path.join(__dirname, "out"),
},
},
);
7 changes: 2 additions & 5 deletions packages/dns/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ const root = path.resolve(__dirname, "../..");

module.exports = merge(
require(path.join(root, "scripts/webpack.node.config.js"))({
// Options.
dirname: __dirname,
name: "dns",
}), {
externals: {
"node-named": "commonjs node-named",
},
output: {
path: path.join(__dirname, "out"),
filename: "main.js",
},
entry: [
"./packages/dns/src/dns.ts"
],
Expand Down
7 changes: 0 additions & 7 deletions packages/server/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,6 @@ const bold = (text: string | number): string | number => {
allowHttp: options.allowHttp,
bypassAuth: options.noAuth,
registerMiddleware: (app): void => {
app.use((req, res, next) => {
res.on("finish", () => {
logger.trace(`\u001B[1m${req.method} ${res.statusCode} \u001B[0m${req.url}`, field("host", req.hostname), field("ip", req.ip));
});

next();
});
// If we're not running from the binary and we aren't serving the static
// pre-built version, use webpack to serve the web files.
if (!isCli && !serveStatic) {
Expand Down
73 changes: 59 additions & 14 deletions packages/server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import * as os from "os";
import * as path from "path";
import * as pem from "pem";
import * as util from "util";
import * as url from "url";
import * as ws from "ws";
import { buildDir } from "./constants";
import { createPortScanner } from "./portScanner";
Expand Down Expand Up @@ -140,13 +141,13 @@ export const createApp = async (options: CreateAppOptions): Promise<{
};

const portScanner = createPortScanner();
wss.on("connection", (ws, req) => {
wss.on("connection", async (ws, req) => {
if (req.url && req.url.startsWith("/tunnel")) {
try {
const rawPort = req.url.split("/").pop();
const port = Number.parseInt(rawPort!, 10);

handleTunnel(ws, port);
await handleTunnel(ws, port);
} catch (ex) {
ws.close(TunnelCloseCode.Error, ex.toString());
}
Expand Down Expand Up @@ -189,31 +190,70 @@ export const createApp = async (options: CreateAppOptions): Promise<{
new Server(connection, options.serverOptions);
});

const redirect = (
req: express.Request, res: express.Response,
to: string = "", from: string = "",
code: number = 302, protocol: string = req.protocol,
): void => {
const currentUrl = `${protocol}://${req.headers.host}${req.originalUrl}`;
const newUrl = url.parse(currentUrl);
if (from && newUrl.pathname) {
newUrl.pathname = newUrl.pathname.replace(new RegExp(`\/${from}\/?$`), "/");
}
if (to) {
newUrl.pathname = (newUrl.pathname || "").replace(/\/$/, "") + `/${to}`;
}
newUrl.path = undefined; // Path is not necessary for format().
const newUrlString = url.format(newUrl);
logger.trace(`Redirecting from ${currentUrl} to ${newUrlString}`);

return res.redirect(code, newUrlString);
};

const baseDir = buildDir || path.join(__dirname, "..");
const authStaticFunc = expressStaticGzip(path.join(baseDir, "build/web/auth"));
const unauthStaticFunc = expressStaticGzip(path.join(baseDir, "build/web/unauth"));
const staticGzip = expressStaticGzip(path.join(baseDir, "build/web"));

app.use((req, res, next) => {
logger.trace(`\u001B[1m${req.method} ${res.statusCode} \u001B[0m${req.originalUrl}`, field("host", req.hostname), field("ip", req.ip));

// Force HTTPS unless allowing HTTP.
if (!isEncrypted(req.socket) && !options.allowHttp) {
return res.redirect(301, `https://${req.headers.host!}${req.path}`);
return redirect(req, res, "", "", 301, "https");
}

if (isAuthed(req)) {
// We can serve the actual VSCode bin
authStaticFunc(req, res, next);
} else {
// Serve only the unauthed version
unauthStaticFunc(req, res, next);
}
next();
});

// @ts-ignore
app.use((err, req, res, next) => {
app.use((err, _req, _res, next) => {
logger.error(err.message);
next();
});
app.get("/ping", (req, res) => {

// If not authenticated, redirect to the login page.
app.get("/", (req, res, next) => {
if (!isAuthed(req)) {
return redirect(req, res, "login");
}
next();
});

// If already authenticated, redirect back to the root.
app.get("/login", (req, res, next) => {
if (isAuthed(req)) {
return redirect(req, res, "", "login");
}
next();
});

// For getting general server data.
app.get("/ping", (_req, res) => {
res.json({
hostname: os.hostname(),
});
});

// For getting a resource on disk.
app.get("/resource/:url(*)", async (req, res) => {
if (!ensureAuthed(req, res)) {
return;
Expand Down Expand Up @@ -254,6 +294,8 @@ export const createApp = async (options: CreateAppOptions): Promise<{
res.end();
}
});

// For writing a resource to disk.
app.post("/resource/:url(*)", async (req, res) => {
if (!ensureAuthed(req, res)) {
return;
Expand Down Expand Up @@ -282,6 +324,9 @@ export const createApp = async (options: CreateAppOptions): Promise<{
}
});

// Everything else just pulls from the static build directory.
app.use(staticGzip);

return {
express: app,
server,
Expand Down
3 changes: 1 addition & 2 deletions packages/server/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ const root = path.resolve(__dirname, "../..");

module.exports = merge(
require(path.join(root, "scripts/webpack.node.config.js"))({
// Config options.
dirname: __dirname,
}), {
output: {
filename: "cli.js",
path: path.join(__dirname, "out"),
libraryTarget: "commonjs",
},
node: {
Expand Down
2 changes: 1 addition & 1 deletion packages/vscode/webpack.bootstrap.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const vsFills = path.join(root, "packages/vscode/src/fill");

module.exports = merge(
require(path.join(root, "scripts/webpack.node.config.js"))({
dirname: __dirname,
typescriptCompilerOptions: {
target: "es6",
},
Expand All @@ -15,7 +16,6 @@ module.exports = merge(
mode: "development",
output: {
chunkFilename: "[name].bundle.js",
path: path.resolve(__dirname, "out"),
publicPath: "/",
filename: "bootstrap-fork.js",
libraryTarget: "commonjs",
Expand Down
20 changes: 10 additions & 10 deletions packages/web/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
return;
}
document.body.style.background = bg;
})();
// Check that service workers are registered
if ("serviceWorker" in navigator) {
// Use the window load event to keep the page load performant
window.addEventListener("load", () => {
navigator.serviceWorker.register("/service-worker.js");
});
}
})();

// Check that service workers are registered
if ("serviceWorker" in navigator) {
// Use the window load event to keep the page load performant
window.addEventListener("load", () => {
navigator.serviceWorker.register("/service-worker.js");
});
}
</script>
</body>
</html>
</html>
7 changes: 2 additions & 5 deletions packages/web/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@ const vsFills = path.join(root, "packages/vscode/src/fill");

module.exports = merge(
require(path.join(root, "scripts/webpack.client.config.js"))({
dirname: __dirname,
entry: path.join(root, "packages/web/src/index.ts"),
name: "ide",
template: path.join(root, "packages/web/src/index.html"),
typescriptCompilerOptions: {
"target": "es5",
"lib": ["dom", "esnext"],
},
},
), {
output: {
chunkFilename: "[name]-[hash:6].bundle.js",
path: path.join(__dirname, "out"),
filename: "[hash:6].bundle.js",
},
node: {
module: "empty",
crypto: "empty",
Expand Down
Loading