| 
									
										
										
										
											2016-08-10 13:10:00 -04:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2016-08-15 21:15:16 -04:00
										 |  |  |   // accept all defaults for le.challenges, le.store, le.middleware
 | 
					
						
							| 
									
										
										
										
											2018-05-10 12:09:20 -06:00
										 |  |  |   opts._communityPackage = opts._communityPackage || 'greenlock-express.js'; | 
					
						
							| 
									
										
										
										
											2017-01-25 15:02:16 -07:00
										 |  |  |   var le = require('greenlock').create(opts); | 
					
						
							| 
									
										
										
										
											2016-08-15 21:15:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 19:18:36 -06:00
										 |  |  |   opts.app = opts.app || function (req, res) { | 
					
						
							| 
									
										
										
										
											2018-05-10 12:09:43 -06:00
										 |  |  |     res.end("Hello, World!\nWith Love,\nGreenlock for Express.js"); | 
					
						
							| 
									
										
										
										
											2016-09-14 15:07:15 -06:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2016-08-15 21:15:16 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   opts.listen = function (plainPort, port) { | 
					
						
							| 
									
										
										
										
											2016-10-13 19:31:52 -06:00
										 |  |  |     var PromiseA; | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       PromiseA = require('bluebird'); | 
					
						
							|  |  |  |     } catch(e) { | 
					
						
							|  |  |  |       console.warn("Package 'bluebird' not installed. Using global.Promise instead"); | 
					
						
							|  |  |  |       console.warn("(want bluebird instead? npm install --save bluebird)"); | 
					
						
							|  |  |  |       PromiseA = global.Promise; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-08-15 21:15:16 -04:00
										 |  |  |     var promises = []; | 
					
						
							|  |  |  |     var plainPorts = plainPort; | 
					
						
							|  |  |  |     var ports = port; | 
					
						
							|  |  |  |     var servers = []; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-11 13:15:28 -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; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       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; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       console.error(e.code + ": '" + e.address + ":" + e.port + "'"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-21 09:13:36 +00:00
										 |  |  |     if (!plainPorts) { | 
					
						
							| 
									
										
										
										
											2016-08-15 21:15:16 -04:00
										 |  |  |       plainPorts = 80; | 
					
						
							| 
									
										
										
										
											2016-12-21 09:13:36 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     if (!ports) { | 
					
						
							| 
									
										
										
										
											2016-08-15 21:15:16 -04:00
										 |  |  |       ports = 443; | 
					
						
							| 
									
										
										
										
											2016-08-10 13:10:00 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 21:15:16 -04:00
										 |  |  |     if (!Array.isArray(plainPorts)) { | 
					
						
							|  |  |  |       plainPorts = [ plainPorts ]; | 
					
						
							|  |  |  |       ports = [ ports ]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-08-10 13:10:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 21:15:16 -04:00
										 |  |  |     plainPorts.forEach(function (p) { | 
					
						
							| 
									
										
										
										
											2018-05-11 13:15:28 -06:00
										 |  |  |       if (!(parseInt(p, 10) >= 0)) { console.warn("'" + p + "' doesn't seem to be a valid port number for http"); } | 
					
						
							|  |  |  |       promises.push(new PromiseA(function (resolve) { | 
					
						
							| 
									
										
										
										
											2016-08-16 13:05:41 -04:00
										 |  |  |         require('http').createServer(le.middleware(require('redirect-https')())).listen(p, function () { | 
					
						
							| 
									
										
										
										
											2018-05-11 13:15:28 -06:00
										 |  |  |           console.log("Success! Bound to port '" + p + "' to handle ACME challenges and redirect to https"); | 
					
						
							| 
									
										
										
										
											2016-08-15 21:15:16 -04:00
										 |  |  |           resolve(); | 
					
						
							| 
									
										
										
										
											2018-05-11 13:15:28 -06:00
										 |  |  |         }).on('error', function (e) { | 
					
						
							|  |  |  |           console.log("Did not successfully create http server and bind to port '" + p + "':"); | 
					
						
							|  |  |  |           explainError(e); | 
					
						
							|  |  |  |           process.exit(0); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2016-08-15 21:15:16 -04:00
										 |  |  |       })); | 
					
						
							| 
									
										
										
										
											2016-08-10 13:10:00 -04:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 21:15:16 -04:00
										 |  |  |     ports.forEach(function (p) { | 
					
						
							| 
									
										
										
										
											2018-05-11 13:15:28 -06:00
										 |  |  |       if (!(parseInt(p, 10) >= 0)) { console.warn("'" + p + "' doesn't seem to be a valid port number for https"); } | 
					
						
							|  |  |  |       promises.push(new PromiseA(function (resolve) { | 
					
						
							| 
									
										
										
										
											2018-05-11 20:29:21 -06:00
										 |  |  |         var server = require('https').createServer(le.tlsOptions, le.middleware(le.app)).listen(p, function () { | 
					
						
							| 
									
										
										
										
											2018-05-11 13:15:28 -06:00
										 |  |  |           console.log("Success! Serving https on port '" + p + "'"); | 
					
						
							| 
									
										
										
										
											2016-08-15 21:15:16 -04:00
										 |  |  |           resolve(); | 
					
						
							| 
									
										
										
										
											2018-05-11 13:15:28 -06:00
										 |  |  |         }).on('error', function (e) { | 
					
						
							|  |  |  |           console.log("Did not successfully create https server and bind to port '" + p + "':"); | 
					
						
							|  |  |  |           explainError(e); | 
					
						
							|  |  |  |           process.exit(0); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2016-08-15 21:15:16 -04:00
										 |  |  |         servers.push(server); | 
					
						
							|  |  |  |       })); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2016-08-11 03:07:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 21:15:16 -04:00
										 |  |  |     if (!Array.isArray(port)) { | 
					
						
							|  |  |  |       servers = servers[0]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-08-10 13:10:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 21:15:16 -04:00
										 |  |  |     return servers; | 
					
						
							| 
									
										
										
										
											2016-08-10 13:10:00 -04:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 21:15:16 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return le; | 
					
						
							| 
									
										
										
										
											2016-08-10 13:10:00 -04:00
										 |  |  | }; |