util: Generate self signed certificate into data directory

Closes #1778
This commit is contained in:
Anmol Sethi 2020-10-30 04:13:22 -04:00
parent 860c99e3b8
commit 10b3028196
No known key found for this signature in database
GPG Key ID: 8CEF1878FF10ADEB
3 changed files with 14 additions and 9 deletions

View File

@ -144,6 +144,9 @@ For HTTPS, you can use a self signed certificate by passing in just `--cert` or
pass in an existing certificate by providing the path to `--cert` and the path to
the key with `--cert-key`.
The self signed certificate will be generated into
`~/.local/share/code-server/self-signed.cert`.
If `code-server` has been passed a certificate it will also respond to HTTPS
requests and will redirect all HTTP requests to HTTPS.

View File

@ -209,7 +209,7 @@ const main = async (args: Args, configArgs: Args): Promise<void> => {
logger.info(
args.cert && args.cert.value
? ` - Using provided certificate and key for HTTPS`
: ` - Using generated certificate and key for HTTPS`,
: ` - Using generated certificate and key for HTTPS: ${humanPath(options.cert)}`,
)
} else {
logger.info(" - Not serving HTTPS")

View File

@ -55,11 +55,10 @@ export function humanPath(p?: string): string {
}
export const generateCertificate = async (): Promise<{ cert: string; certKey: string }> => {
const paths = {
cert: path.join(tmpdir, "self-signed.cert"),
certKey: path.join(tmpdir, "self-signed.key"),
}
const checks = await Promise.all([fs.pathExists(paths.cert), fs.pathExists(paths.certKey)])
const certPath = path.join(paths.data, "self-signed.cert")
const certKeyPath = path.join(paths.data, "self-signed.key")
const checks = await Promise.all([fs.pathExists(certPath), fs.pathExists(certKeyPath)])
if (!checks[0] || !checks[1]) {
// Require on demand so openssl isn't required if you aren't going to
// generate certificates.
@ -69,10 +68,13 @@ export const generateCertificate = async (): Promise<{ cert: string; certKey: st
return error ? reject(error) : resolve(result)
})
})
await fs.mkdirp(tmpdir)
await Promise.all([fs.writeFile(paths.cert, certs.certificate), fs.writeFile(paths.certKey, certs.serviceKey)])
await fs.mkdirp(paths.data)
await Promise.all([fs.writeFile(certPath, certs.certificate), fs.writeFile(certKeyPath, certs.serviceKey)])
}
return {
cert: certPath,
certKey: certKeyPath,
}
return paths
}
export const generatePassword = async (length = 24): Promise<string> => {