@@ -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"
@@ -26,38 +26,25 @@ const main = async (args: Args): Promise<void> => {
26
26
}
27
27
28
28
// Spawn the main HTTP server.
29
- const options = {
29
+ const options : HttpServerOptions = {
30
30
auth,
31
- cert : args . cert ? args . cert . value : undefined ,
32
- certKey : args [ "cert-key" ] ,
33
- sshHostKey : args [ "ssh-host-key" ] ,
34
31
commit : commit || "development" ,
35
32
host : args . host || ( args . auth === AuthType . Password && typeof args . cert !== "undefined" ? "0.0.0.0" : "localhost" ) ,
36
33
password : originalPassword ? hash ( originalPassword ) : undefined ,
37
34
port : typeof args . port !== "undefined" ? args . port : process . env . PORT ? parseInt ( process . env . PORT , 10 ) : 8080 ,
38
35
socket : args . socket ,
36
+ ...( args . cert && ! args . cert . value
37
+ ? await generateCertificate ( )
38
+ : {
39
+ cert : args . cert && args . cert . value ,
40
+ certKey : args [ "cert-key" ] ,
41
+ } ) ,
39
42
}
40
43
41
- if ( ! options . cert && args . cert ) {
42
- const { cert, certKey } = await generateCertificate ( )
43
- options . cert = cert
44
- options . certKey = certKey
45
- } else if ( args . cert && ! args [ "cert-key" ] ) {
44
+ if ( options . cert && ! options . certKey ) {
46
45
throw new Error ( "--cert-key is missing" )
47
46
}
48
47
49
- if ( ! args [ "disable-ssh" ] ) {
50
- if ( ! options . sshHostKey && typeof options . sshHostKey !== "undefined" ) {
51
- throw new Error ( "--ssh-host-key cannot be blank" )
52
- } else if ( ! options . sshHostKey ) {
53
- try {
54
- options . sshHostKey = await generateSshHostKey ( )
55
- } catch ( error ) {
56
- logger . error ( "Unable to start SSH server" , field ( "error" , error . message ) )
57
- }
58
- }
59
- }
60
-
61
48
const httpServer = new HttpServer ( options )
62
49
const vscode = httpServer . registerHttpProvider ( "/" , VscodeHttpProvider , args )
63
50
const api = httpServer . registerHttpProvider ( "/api" , ApiHttpProvider , httpServer , vscode , args [ "user-data-dir" ] )
@@ -70,18 +57,28 @@ const main = async (args: Args): Promise<void> => {
70
57
71
58
logger . info ( `code-server ${ require ( "../../package.json" ) . version } ` )
72
59
73
- let sshPort = ""
74
- if ( ! args [ "disable-ssh" ] && options . sshHostKey ) {
75
- const sshProvider = httpServer . registerHttpProvider ( "/ssh" , SshProvider , options . sshHostKey as string )
60
+ // Spawn the SSH server.
61
+ let sshHostKey = args [ "ssh-host-key" ]
62
+ if ( ! args [ "disable-ssh" ] && ! sshHostKey ) {
63
+ try {
64
+ sshHostKey = await generateSshHostKey ( )
65
+ } catch ( error ) {
66
+ logger . error ( "Unable to start SSH server" , field ( "error" , error . message ) )
67
+ }
68
+ }
69
+
70
+ let sshPort : string | undefined
71
+ if ( ! args [ "disable-ssh" ] && sshHostKey ) {
72
+ const sshProvider = httpServer . registerHttpProvider ( "/ssh" , SshProvider , sshHostKey )
76
73
sshPort = await sshProvider . listen ( )
77
74
}
78
75
79
76
const serverAddress = await httpServer . listen ( )
80
- logger . info ( `Server listening on ${ serverAddress } ` )
77
+ logger . info ( `HTTP server listening on ${ serverAddress } ` )
81
78
82
79
if ( auth === AuthType . Password && ! process . env . PASSWORD ) {
83
80
logger . info ( ` - Password is ${ originalPassword } ` )
84
- logger . info ( " - To use your own password, set the PASSWORD environment variable" )
81
+ logger . info ( " - To use your own password set the PASSWORD environment variable" )
85
82
if ( ! args . auth ) {
86
83
logger . info ( " - To disable use `--auth none`" )
87
84
}
@@ -93,27 +90,28 @@ const main = async (args: Args): Promise<void> => {
93
90
94
91
if ( httpServer . protocol === "https" ) {
95
92
logger . info (
96
- typeof args . cert === "string"
93
+ args . cert && args . cert . value
97
94
? ` - Using provided certificate and key for HTTPS`
98
95
: ` - Using generated certificate and key for HTTPS` ,
99
96
)
100
97
} else {
101
98
logger . info ( " - Not serving HTTPS" )
102
99
}
103
100
104
- logger . info ( ` - Automatic updates are ${ update . enabled ? "enabled" : "disabled" } ` )
101
+ logger . info ( `Automatic updates are ${ update . enabled ? "enabled" : "disabled" } ` )
105
102
106
103
if ( sshPort ) {
107
- logger . info ( ` - SSH Server - Listening :${ sshPort } ` )
104
+ logger . info ( `SSH server listening on :${ sshPort } ` )
105
+ logger . info ( " - To disable use `--disable-ssh`" )
108
106
} else {
109
- logger . info ( " - SSH Server - Disabled " )
107
+ logger . info ( "SSH server disabled " )
110
108
}
111
109
112
110
if ( serverAddress && ! options . socket && args . open ) {
113
111
// The web socket doesn't seem to work if browsing with 0.0.0.0.
114
112
const openAddress = serverAddress . replace ( / : \/ \/ 0 .0 .0 .0 / , "://localhost" )
115
113
await open ( openAddress ) . catch ( console . error )
116
- logger . info ( ` - Opened ${ openAddress } ` )
114
+ logger . info ( `Opened ${ openAddress } ` )
117
115
}
118
116
}
119
117
0 commit comments