| 
									
										
										
										
											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
 | 
					
						
							|  |  |  |   var le = require('letsencrypt').create(opts); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 19:18:36 -06:00
										 |  |  |   opts.app = opts.app || function (req, res) { | 
					
						
							| 
									
										
										
										
											2016-08-15 21:15:16 -04:00
										 |  |  |     res.end("Hello, World!\nWith Love,\nLet's Encrypt Express"); | 
					
						
							| 
									
										
										
										
											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 = []; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							|  |  |  |       promises.push(new PromiseA(function (resolve, reject) { | 
					
						
							| 
									
										
										
										
											2016-08-16 13:05:41 -04:00
										 |  |  |         require('http').createServer(le.middleware(require('redirect-https')())).listen(p, function () { | 
					
						
							|  |  |  |           console.log("Handling ACME challenges and redirecting to https on plain port " + p); | 
					
						
							| 
									
										
										
										
											2016-08-15 21:15:16 -04:00
										 |  |  |           resolve(); | 
					
						
							|  |  |  |         }).on('error', reject); | 
					
						
							|  |  |  |       })); | 
					
						
							| 
									
										
										
										
											2016-08-10 13:10:00 -04:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 21:15:16 -04:00
										 |  |  |     ports.forEach(function (p) { | 
					
						
							|  |  |  |       promises.push(new PromiseA(function (resolve, reject) { | 
					
						
							|  |  |  |         var server = require('https').createServer(le.httpsOptions, le.middleware(le.app)).listen(p, function () { | 
					
						
							| 
									
										
										
										
											2016-08-16 13:05:41 -04:00
										 |  |  |           console.log("Handling ACME challenges and serving https " + p); | 
					
						
							| 
									
										
										
										
											2016-08-15 21:15:16 -04:00
										 |  |  |           resolve(); | 
					
						
							|  |  |  |         }).on('error', reject); | 
					
						
							|  |  |  |         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
										 |  |  | }; |