@@ -81,23 +81,33 @@ public static void Main(string[] args)
81
81
CreateHostBuilder ( args ) . Build ( ) . Run ( ) ;
82
82
}
83
83
84
- public static string GetCertPath ( string filename )
84
+ public static string ? GetCertPath ( string filename )
85
85
{
86
86
// Check if the 'certs/' folder is in the current directory
87
- if ( Directory . Exists ( Path . Combine ( Directory . GetCurrentDirectory ( ) , "certs" ) ) )
87
+ var currentDirectory = Directory . GetCurrentDirectory ( ) ;
88
+ if ( Directory . Exists ( Path . Combine ( currentDirectory , "certs" ) ) )
88
89
{
89
- return Path . Combine ( Directory . GetCurrentDirectory ( ) , "certs" , filename ) ;
90
+ return Path . Combine ( currentDirectory , "certs" , filename ) ;
90
91
}
91
92
92
93
// Check if the 'certs/' folder is two directories up
93
- var twoDirectoriesUp = Directory . GetParent ( Directory . GetParent ( Directory . GetCurrentDirectory ( ) ) . FullName ) . FullName ;
94
+ var parent1 = Directory . GetParent ( currentDirectory ) ;
95
+ if ( parent1 == null )
96
+ {
97
+ return null ;
98
+ }
99
+ var parent2 = Directory . GetParent ( parent1 . FullName ) ;
100
+ if ( parent2 == null )
101
+ {
102
+ return null ;
103
+ }
104
+ var twoDirectoriesUp = parent2 . FullName ;
94
105
if ( Directory . Exists ( Path . Combine ( twoDirectoriesUp , "certs" ) ) )
95
106
{
96
107
return Path . Combine ( twoDirectoriesUp , "certs" , filename ) ;
97
108
}
98
109
99
- // If the 'certs/' folder is not found, throw
100
- throw new Exception ( "Could not find the 'certs/' folder" ) ;
110
+ return null ;
101
111
}
102
112
103
113
public static IHostBuilder CreateHostBuilder ( string [ ] args ) =>
@@ -176,27 +186,44 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
176
186
// Remove the `Server: Kestrel` response header
177
187
serverOptions . AddServerHeader = false ;
178
188
189
+ // See if we have a cert
190
+ var certPath = GetCertPath ( "lambdadispatch.local.pfx" ) ;
191
+
179
192
// We have to reparse the config once, bummer
180
193
var config = Config . CreateAndValidate ( context . Configuration ) ;
181
194
//
182
195
// Incoming Requests
183
196
//
184
197
serverOptions . ListenAnyIP ( config . IncomingRequestHTTPPort ) ;
185
- serverOptions . ListenAnyIP ( config . IncomingRequestHTTPSPort , listenOptions =>
198
+ if ( certPath == null )
186
199
{
187
- listenOptions . UseHttps ( GetCertPath ( "lambdadispatch.local.pfx" ) ) ;
188
- } ) ;
200
+ Console . WriteLine ( "No cert found, not starting HTTPS incoming request channel" ) ;
201
+ }
202
+ else
203
+ {
204
+ serverOptions . ListenAnyIP ( config . IncomingRequestHTTPSPort , listenOptions =>
205
+ {
206
+ listenOptions . UseHttps ( certPath ) ;
207
+ } ) ;
208
+ }
189
209
//
190
210
// Control Channels
191
211
//
192
212
if ( config . AllowInsecureControlChannel )
193
213
{
194
214
serverOptions . ListenAnyIP ( config . ControlChannelInsecureHTTP2Port , o => o . Protocols = HttpProtocols . Http2 ) ;
195
215
}
196
- serverOptions . ListenAnyIP ( config . ControlChannelHTTP2Port , listenOptions =>
216
+ if ( certPath == null )
197
217
{
198
- listenOptions . UseHttps ( GetCertPath ( "lambdadispatch.local.pfx" ) ) ;
199
- } ) ;
218
+ Console . WriteLine ( "No cert found, not starting HTTPS control channel" ) ;
219
+ }
220
+ else
221
+ {
222
+ serverOptions . ListenAnyIP ( config . ControlChannelHTTP2Port , listenOptions =>
223
+ {
224
+ listenOptions . UseHttps ( certPath ) ;
225
+ } ) ;
226
+ }
200
227
} ) ;
201
228
webBuilder . UseStartup < Startup > ( ) ;
202
229
} ) ;
0 commit comments