| 
									
										
										
										
											2016-08-10 13:10:00 -04:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-29 03:24:58 -06:00
										 |  |  | var PromiseA; | 
					
						
							|  |  |  | try { | 
					
						
							|  |  |  |   PromiseA = require('bluebird'); | 
					
						
							|  |  |  | } catch(e) { | 
					
						
							|  |  |  |   PromiseA = global.Promise; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-12 03:02:33 -04:00
										 |  |  | // opts.approveDomains(options, certs, cb)
 | 
					
						
							| 
									
										
										
										
											2016-08-10 13:10:00 -04:00
										 |  |  | module.exports.create = function (opts) { | 
					
						
							| 
									
										
										
										
											2018-05-23 03:22:20 -06:00
										 |  |  |   // accept all defaults for greenlock.challenges, greenlock.store, greenlock.middleware
 | 
					
						
							| 
									
										
										
										
											2018-08-16 20:45:31 -06:00
										 |  |  |   if (!opts._communityPackage) { | 
					
						
							|  |  |  |     opts._communityPackage = 'greenlock-express.js'; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-08-15 21:15:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-16 20:45:31 -06:00
										 |  |  |   function explainError(e) { | 
					
						
							|  |  |  |     console.error('Error:' + e.message); | 
					
						
							|  |  |  |     if ('EACCES' === e.errno) { | 
					
						
							|  |  |  |       console.error("You don't have prmission to access '" + e.address + ":" + e.port + "'."); | 
					
						
							|  |  |  |       console.error("You probably need to use \"sudo\" or \"sudo setcap 'cap_net_bind_service=+ep' $(which node)\""); | 
					
						
							|  |  |  |       return; | 
					
						
							| 
									
										
										
										
											2016-08-10 13:10:00 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-08-16 20:45:31 -06:00
										 |  |  |     if ('EADDRINUSE' === e.errno) { | 
					
						
							|  |  |  |       console.error("'" + e.address + ":" + e.port + "' is already being used by some other program."); | 
					
						
							|  |  |  |       console.error("You probably need to stop that program or restart your computer."); | 
					
						
							|  |  |  |       return; | 
					
						
							| 
									
										
										
										
											2016-08-15 21:15:16 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-08-16 20:45:31 -06:00
										 |  |  |     console.error(e.code + ": '" + e.address + ":" + e.port + "'"); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-08-10 13:10:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-18 01:52:32 -06:00
										 |  |  |   function _listen(plainPort, plain) { | 
					
						
							| 
									
										
										
										
											2018-08-16 20:45:31 -06:00
										 |  |  |     if (!plainPort) { plainPort = 80; } | 
					
						
							| 
									
										
										
										
											2018-08-18 01:52:32 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     var parts = String(plainPort).split(':'); | 
					
						
							|  |  |  |     var p = parts.pop(); | 
					
						
							|  |  |  |     var addr = parts.join(':').replace(/^\[/, '').replace(/\]$/, ''); | 
					
						
							|  |  |  |     var args = []; | 
					
						
							|  |  |  |     var httpType; | 
					
						
							|  |  |  |     var server; | 
					
						
							| 
									
										
										
										
											2018-08-16 20:45:31 -06:00
										 |  |  |     var validHttpPort = (parseInt(p, 10) >= 0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-18 01:52:32 -06:00
										 |  |  |     function tryPlain() { | 
					
						
							|  |  |  |       server = require('http').createServer( | 
					
						
							|  |  |  |         greenlock.middleware.sanitizeHost(greenlock.middleware(require('redirect-https')())) | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |       httpType = 'http'; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-08-10 13:10:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-18 01:52:32 -06:00
										 |  |  |     function trySecure() { | 
					
						
							|  |  |  |       var https; | 
					
						
							|  |  |  |       try { | 
					
						
							|  |  |  |         https = require('spdy'); | 
					
						
							|  |  |  |         greenlock.tlsOptions.spdy = { protocols: [ 'h2', 'http/1.1' ], plain: false }; | 
					
						
							|  |  |  |         httpType = 'http2 (spdy/h2)'; | 
					
						
							|  |  |  |       } catch(e) { | 
					
						
							|  |  |  |         https = require('https'); | 
					
						
							|  |  |  |         httpType = 'https'; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       server = https.createServer( | 
					
						
							|  |  |  |         greenlock.tlsOptions | 
					
						
							|  |  |  |       , greenlock.middleware.sanitizeHost(function (req, res) { | 
					
						
							|  |  |  |           try { | 
					
						
							|  |  |  |             greenlock.app(req, res); | 
					
						
							|  |  |  |           } catch(e) { | 
					
						
							|  |  |  |             console.error("[error] [greenlock.app] Your HTTP handler had an uncaught error:"); | 
					
						
							|  |  |  |             console.error(e); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |       server.type = httpType; | 
					
						
							| 
									
										
										
										
											2018-08-16 20:45:31 -06:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-08-18 01:52:32 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (addr) { args[1] = addr; } | 
					
						
							|  |  |  |     if (!validHttpPort && !/(\/)|(\\\\)/.test(p)) { | 
					
						
							|  |  |  |       console.warn("'" + p + "' doesn't seem to be a valid port number, socket path, or pipe"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (plain) { tryPlain(); } else { trySecure(); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-16 20:45:31 -06:00
										 |  |  |     var promise = new PromiseA(function (resolve) { | 
					
						
							| 
									
										
										
										
											2018-08-18 01:52:32 -06:00
										 |  |  |       args[0] = p; | 
					
						
							|  |  |  |       args.push(function () { resolve(server); }); | 
					
						
							|  |  |  |       server.listen.apply(server, args).on('error', function (e) { | 
					
						
							| 
									
										
										
										
											2018-08-17 19:58:50 -06:00
										 |  |  |         if (server.listenerCount('error') < 2) { | 
					
						
							| 
									
										
										
										
											2018-08-18 01:52:32 -06:00
										 |  |  |           console.warn("Did not successfully create http server and bind to port '" + p + "':"); | 
					
						
							| 
									
										
										
										
											2018-08-17 19:58:50 -06:00
										 |  |  |           explainError(e); | 
					
						
							| 
									
										
										
										
											2018-08-18 01:52:32 -06:00
										 |  |  |           process.exit(41); | 
					
						
							| 
									
										
										
										
											2018-08-17 19:58:50 -06:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-08-16 20:45:31 -06:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2016-08-15 21:15:16 -04:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2018-08-18 01:52:32 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-16 20:45:31 -06:00
										 |  |  |     promise.server = server; | 
					
						
							|  |  |  |     return promise; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-08-11 03:07:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-16 20:45:31 -06:00
										 |  |  |   var greenlock = require('greenlock').create(opts); | 
					
						
							| 
									
										
										
										
											2016-08-10 13:10:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-16 20:45:31 -06:00
										 |  |  |   opts.app = opts.app || function (req, res) { | 
					
						
							|  |  |  |     res.end("Hello, World!\nWith Love,\nGreenlock for Express.js"); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-18 01:52:32 -06:00
										 |  |  |   opts.listen = function (plainPort, port, fnPlain, fn) { | 
					
						
							| 
									
										
										
										
											2018-08-16 20:45:31 -06:00
										 |  |  |     var promises = []; | 
					
						
							| 
									
										
										
										
											2018-08-17 19:58:50 -06:00
										 |  |  |     var server; | 
					
						
							| 
									
										
										
										
											2018-08-17 20:43:32 -06:00
										 |  |  |     var plainServer; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-18 01:52:32 -06:00
										 |  |  |     if (!fn) { | 
					
						
							|  |  |  |       fn = fnPlain; | 
					
						
							|  |  |  |       fnPlain = null; | 
					
						
							| 
									
										
										
										
											2018-08-17 20:43:32 -06:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-08-17 19:58:50 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-18 01:52:32 -06:00
										 |  |  |     promises.push(_listen(plainPort, true)); | 
					
						
							|  |  |  |     promises.push(_listen(port, false)); | 
					
						
							| 
									
										
										
										
											2018-08-17 19:58:50 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     server = promises[1].server; | 
					
						
							| 
									
										
										
										
											2018-08-17 20:43:32 -06:00
										 |  |  |     plainServer = promises[0].server; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-17 19:58:50 -06:00
										 |  |  |     PromiseA.all(promises).then(function () { | 
					
						
							| 
									
										
										
										
											2018-08-17 20:43:32 -06:00
										 |  |  |       // Report plain http status
 | 
					
						
							|  |  |  |       if ('function' === typeof fnPlain) { | 
					
						
							|  |  |  |         fnPlain.apply(plainServer); | 
					
						
							| 
									
										
										
										
											2018-08-18 02:03:04 -06:00
										 |  |  |       } else if (!fn && !plainServer.listenerCount('listening') && !server.listenerCount('listening')) { | 
					
						
							| 
									
										
										
										
											2018-08-18 01:52:32 -06:00
										 |  |  |         console.info('[:' + (plainServer.address().port || plainServer.address()) | 
					
						
							|  |  |  |           + "] Handling ACME challenges and redirecting to " + server.type); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // Report h2/https status
 | 
					
						
							|  |  |  |       if ('function' === typeof fn) { | 
					
						
							|  |  |  |         fn.apply(server); | 
					
						
							| 
									
										
										
										
											2018-08-18 02:03:04 -06:00
										 |  |  |       } else if (!server.listenerCount('listening')) { | 
					
						
							| 
									
										
										
										
											2018-08-18 01:52:32 -06:00
										 |  |  |         console.info('[:' + (server.address().port || server.address()) + "] Serving " + server.type); | 
					
						
							| 
									
										
										
										
											2018-08-17 19:58:50 -06:00
										 |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-17 20:43:32 -06:00
										 |  |  |     server.unencrypted = plainServer; | 
					
						
							| 
									
										
										
										
											2018-08-17 19:58:50 -06:00
										 |  |  |     return server; | 
					
						
							| 
									
										
										
										
											2016-08-10 13:10:00 -04:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 21:15:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-23 03:22:20 -06:00
										 |  |  |   return greenlock; | 
					
						
							| 
									
										
										
										
											2016-08-10 13:10:00 -04:00
										 |  |  | }; |