@@ -9,7 +9,7 @@ import { StaticHttpProvider } from "./app/static"
99import { UpdateHttpProvider } from "./app/update"
1010import { VscodeHttpProvider } from "./app/vscode"
1111import { Args , optionDescriptions , parse } from "./cli"
12- import { AuthType , HttpServer } from "./http"
12+ import { AuthType , HttpServer , HttpServerOptions } from "./http"
1313import { SshProvider } from "./ssh/server"
1414import { generateCertificate , generatePassword , generateSshHostKey , hash , open } from "./util"
1515import { ipcMain , wrap } from "./wrapper"
@@ -36,38 +36,25 @@ const main = async (args: Args): Promise<void> => {
3636 const originalPassword = auth === AuthType . Password && ( process . env . PASSWORD || ( await generatePassword ( ) ) )
3737
3838 // Spawn the main HTTP server.
39- const options = {
39+ const options : HttpServerOptions = {
4040 auth,
41- cert : args . cert ? args . cert . value : undefined ,
42- certKey : args [ "cert-key" ] ,
43- sshHostKey : args [ "ssh-host-key" ] ,
4441 commit,
4542 host : args . host || ( args . auth === AuthType . Password && typeof args . cert !== "undefined" ? "0.0.0.0" : "localhost" ) ,
4643 password : originalPassword ? hash ( originalPassword ) : undefined ,
4744 port : typeof args . port !== "undefined" ? args . port : process . env . PORT ? parseInt ( process . env . PORT , 10 ) : 8080 ,
4845 socket : args . socket ,
46+ ...( args . cert && ! args . cert . value
47+ ? await generateCertificate ( )
48+ : {
49+ cert : args . cert && args . cert . value ,
50+ certKey : args [ "cert-key" ] ,
51+ } ) ,
4952 }
5053
51- if ( ! options . cert && args . cert ) {
52- const { cert, certKey } = await generateCertificate ( )
53- options . cert = cert
54- options . certKey = certKey
55- } else if ( args . cert && ! args [ "cert-key" ] ) {
54+ if ( options . cert && ! options . certKey ) {
5655 throw new Error ( "--cert-key is missing" )
5756 }
5857
59- if ( ! args [ "disable-ssh" ] ) {
60- if ( ! options . sshHostKey && typeof options . sshHostKey !== "undefined" ) {
61- throw new Error ( "--ssh-host-key cannot be blank" )
62- } else if ( ! options . sshHostKey ) {
63- try {
64- options . sshHostKey = await generateSshHostKey ( )
65- } catch ( error ) {
66- logger . error ( "Unable to start SSH server" , field ( "error" , error . message ) )
67- }
68- }
69- }
70-
7158 const httpServer = new HttpServer ( options )
7259 const vscode = httpServer . registerHttpProvider ( "/" , VscodeHttpProvider , args )
7360 const api = httpServer . registerHttpProvider ( "/api" , ApiHttpProvider , httpServer , vscode , args [ "user-data-dir" ] )
@@ -84,7 +71,7 @@ const main = async (args: Args): Promise<void> => {
8471
8572 if ( auth === AuthType . Password && ! process . env . PASSWORD ) {
8673 logger . info ( ` - Password is ${ originalPassword } ` )
87- logger . info ( " - To use your own password, set the PASSWORD environment variable" )
74+ logger . info ( " - To use your own password set the PASSWORD environment variable" )
8875 if ( ! args . auth ) {
8976 logger . info ( " - To disable use `--auth none`" )
9077 }
@@ -96,7 +83,7 @@ const main = async (args: Args): Promise<void> => {
9683
9784 if ( httpServer . protocol === "https" ) {
9885 logger . info (
99- typeof args . cert === "string"
86+ args . cert && args . cert . value
10087 ? ` - Using provided certificate and key for HTTPS`
10188 : ` - Using generated certificate and key for HTTPS` ,
10289 )
@@ -106,9 +93,18 @@ const main = async (args: Args): Promise<void> => {
10693
10794 logger . info ( `Automatic updates are ${ update . enabled ? "enabled" : "disabled" } ` )
10895
96+ let sshHostKey = args [ "ssh-host-key" ]
97+ if ( ! args [ "disable-ssh" ] && ! sshHostKey ) {
98+ try {
99+ sshHostKey = await generateSshHostKey ( )
100+ } catch ( error ) {
101+ logger . error ( "Unable to start SSH server" , field ( "error" , error . message ) )
102+ }
103+ }
104+
109105 let sshPort : number | undefined
110- if ( ! args [ "disable-ssh" ] && options . sshHostKey ) {
111- const sshProvider = httpServer . registerHttpProvider ( "/ssh" , SshProvider , options . sshHostKey as string )
106+ if ( ! args [ "disable-ssh" ] && sshHostKey ) {
107+ const sshProvider = httpServer . registerHttpProvider ( "/ssh" , SshProvider , sshHostKey )
112108 try {
113109 sshPort = await sshProvider . listen ( )
114110 } catch ( error ) {
@@ -118,6 +114,7 @@ const main = async (args: Args): Promise<void> => {
118114
119115 if ( typeof sshPort !== "undefined" ) {
120116 logger . info ( `SSH server listening on localhost:${ sshPort } ` )
117+ logger . info ( " - To disable use `--disable-ssh`" )
121118 } else {
122119 logger . info ( "SSH server disabled" )
123120 }
0 commit comments