Skip to content

Commit 67773d8

Browse files
committed
Force certificates
1 parent 22329f4 commit 67773d8

File tree

3 files changed

+43
-16
lines changed

3 files changed

+43
-16
lines changed

packages/app/browser/src/app.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33

44
<head>
55
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
6-
<title>Coder</title>
6+
<title>Authenticate: code-server</title>
77
</head>
88

99
<body>
1010
<div class="login">
1111
<div class="back">
1212
<- Back </div>
13-
<!-- <h4 class="title">AWS Cloud</h4> -->
13+
<h4 class="title">code-server</h4>
1414
<h2 class="subtitle">
1515
Enter server password
1616
</h2>

packages/server/src/modules.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as fs from "fs";
22
import * as path from "path";
3+
import * as os from "os";
34
import { isCli, buildDir } from "./constants";
45

56
declare var __non_webpack_require__: typeof require;
@@ -19,7 +20,7 @@ export const setup = (dataDirectory: string): void => {
1920
}
2021

2122
return currentDir;
22-
}); // Might need path.sep here for linux. Having it for windows causes an error because \C:\Users ...
23+
}, os.platform() === "win32" ? undefined! : path.sep); // Might need path.sep here for linux. Having it for windows causes an error because \C:\Users ...
2324

2425
const unpackModule = (moduleName: string): void => {
2526
const memFile = path.join(isCli ? buildDir! : path.join(__dirname, ".."), "build/dependencies", moduleName);

packages/server/src/server.ts

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,24 +86,46 @@ export const createApp = async (options: CreateAppOptions): Promise<{
8686
options.registerMiddleware(app);
8787
}
8888

89-
const certs = await new Promise<pem.CertificateCreationResult>((res, rej): void => {
90-
pem.createCertificate({
91-
selfSigned: true,
92-
}, (err, result) => {
93-
if (err) {
94-
rej(err);
95-
96-
return;
89+
interface CertificateInfo {
90+
readonly key: string;
91+
// tslint:disable-next-line:no-any
92+
readonly cert: any;
93+
}
94+
95+
const certs = await new Promise<CertificateInfo>(async (resolve, reject): Promise<void> => {
96+
const selfSignedKeyPath = path.join(options.serverOptions!.dataDirectory, "self-signed.key");
97+
const selfSignedCertPath = path.join(options.serverOptions!.dataDirectory, "self-signed.cert");
98+
99+
if (!fs.existsSync(selfSignedKeyPath) || !fs.existsSync(selfSignedCertPath)) {
100+
try {
101+
const certs = await new Promise<pem.CertificateCreationResult>((res, rej): void => {
102+
pem.createCertificate({
103+
selfSigned: true,
104+
}, (err, result) => {
105+
if (err) {
106+
rej(err);
107+
108+
return;
109+
}
110+
111+
res(result);
112+
});
113+
});
114+
115+
fs.writeFileSync(selfSignedKeyPath, certs.serviceKey);
116+
fs.writeFileSync(selfSignedCertPath, certs.certificate);
117+
} catch (ex) {
118+
return reject(ex);
97119
}
120+
}
98121

99-
res(result);
122+
resolve({
123+
cert: fs.readFileSync(selfSignedCertPath).toString(),
124+
key: fs.readFileSync(selfSignedKeyPath).toString(),
100125
});
101126
});
102127

103-
const server = httpolyglot.createServer({
104-
key: certs.serviceKey,
105-
cert: certs.certificate,
106-
}, app) as http.Server;
128+
const server = httpolyglot.createServer(options.httpsOptions || certs, app) as http.Server;
107129
const wss = new ws.Server({ server });
108130

109131
wss.shouldHandle = (req): boolean => {
@@ -161,6 +183,10 @@ export const createApp = async (options: CreateAppOptions): Promise<{
161183
const authStaticFunc = expressStaticGzip(path.join(baseDir, "build/web/auth"));
162184
const unauthStaticFunc = expressStaticGzip(path.join(baseDir, "build/web/unauth"));
163185
app.use((req, res, next) => {
186+
if (!isEncrypted(req.socket)) {
187+
return res.redirect(301, `https://${req.headers.host!}${req.path}`);
188+
}
189+
164190
if (isAuthed(req)) {
165191
// We can serve the actual VSCode bin
166192
authStaticFunc(req, res, next);

0 commit comments

Comments
 (0)