@@ -9,7 +9,7 @@ import { StaticHttpProvider } from "./app/static"
9
9
import { UpdateHttpProvider } from "./app/update"
10
10
import { VscodeHttpProvider } from "./app/vscode"
11
11
import { Args , optionDescriptions , parse } from "./cli"
12
- import { AuthType , HttpServer } from "./http"
12
+ import { AuthType , HttpServer , HttpServerOptions } from "./http"
13
13
import { SshProvider } from "./ssh/server"
14
14
import { generateCertificate , generatePassword , generateSshHostKey , hash , open } from "./util"
15
15
import { ipcMain , wrap } from "./wrapper"
@@ -36,38 +36,25 @@ const main = async (args: Args): Promise<void> => {
36
36
const originalPassword = auth === AuthType . Password && ( process . env . PASSWORD || ( await generatePassword ( ) ) )
37
37
38
38
// Spawn the main HTTP server.
39
- const options = {
39
+ const options : HttpServerOptions = {
40
40
auth,
41
- cert : args . cert ? args . cert . value : undefined ,
42
- certKey : args [ "cert-key" ] ,
43
- sshHostKey : args [ "ssh-host-key" ] ,
44
41
commit,
45
42
host : args . host || ( args . auth === AuthType . Password && typeof args . cert !== "undefined" ? "0.0.0.0" : "localhost" ) ,
46
43
password : originalPassword ? hash ( originalPassword ) : undefined ,
47
44
port : typeof args . port !== "undefined" ? args . port : process . env . PORT ? parseInt ( process . env . PORT , 10 ) : 8080 ,
48
45
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
+ } ) ,
49
52
}
50
53
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 ) {
56
55
throw new Error ( "--cert-key is missing" )
57
56
}
58
57
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
-
71
58
const httpServer = new HttpServer ( options )
72
59
const vscode = httpServer . registerHttpProvider ( "/" , VscodeHttpProvider , args )
73
60
const api = httpServer . registerHttpProvider ( "/api" , ApiHttpProvider , httpServer , vscode , args [ "user-data-dir" ] )
@@ -84,7 +71,7 @@ const main = async (args: Args): Promise<void> => {
84
71
85
72
if ( auth === AuthType . Password && ! process . env . PASSWORD ) {
86
73
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" )
88
75
if ( ! args . auth ) {
89
76
logger . info ( " - To disable use `--auth none`" )
90
77
}
@@ -96,7 +83,7 @@ const main = async (args: Args): Promise<void> => {
96
83
97
84
if ( httpServer . protocol === "https" ) {
98
85
logger . info (
99
- typeof args . cert === "string"
86
+ args . cert && args . cert . value
100
87
? ` - Using provided certificate and key for HTTPS`
101
88
: ` - Using generated certificate and key for HTTPS` ,
102
89
)
@@ -106,18 +93,24 @@ const main = async (args: Args): Promise<void> => {
106
93
107
94
logger . info ( `Automatic updates are ${ update . enabled ? "enabled" : "disabled" } ` )
108
95
109
- let sshPort : number | undefined
110
- if ( ! args [ "disable-ssh" ] && options . sshHostKey ) {
111
- const sshProvider = httpServer . registerHttpProvider ( "/ssh" , SshProvider , options . sshHostKey as string )
96
+ let sshHostKey = args [ "ssh-host-key" ]
97
+ if ( ! args [ "disable-ssh" ] && ! sshHostKey ) {
112
98
try {
113
- sshPort = await sshProvider . listen ( )
99
+ sshHostKey = await generateSshHostKey ( )
114
100
} catch ( error ) {
115
- logger . warn ( ` SSH server: ${ error . message } ` )
101
+ logger . error ( "Unable to start SSH server" , field ( " error" , error . message ) )
116
102
}
117
103
}
118
104
105
+ let sshPort : number | undefined
106
+ if ( ! args [ "disable-ssh" ] && sshHostKey ) {
107
+ const sshProvider = httpServer . registerHttpProvider ( "/ssh" , SshProvider , sshHostKey )
108
+ sshPort = await sshProvider . listen ( )
109
+ }
110
+
119
111
if ( typeof sshPort !== "undefined" ) {
120
112
logger . info ( `SSH server listening on localhost:${ sshPort } ` )
113
+ logger . info ( " - To disable use `--disable-ssh`" )
121
114
} else {
122
115
logger . info ( "SSH server disabled" )
123
116
}
0 commit comments