@@ -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