| 
									
										
										
										
											2016-08-10 13:10:00 -04:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-12 03:02:33 -04:00
										 |  |  | // opts.addWorker(worker)
 | 
					
						
							|  |  |  | // opts.approveDomains(options, certs, cb)
 | 
					
						
							| 
									
										
										
										
											2016-08-10 13:10:00 -04:00
										 |  |  | module.exports.create = function (opts) { | 
					
						
							| 
									
										
										
										
											2016-08-12 03:02:33 -04:00
										 |  |  |   opts = opts || { }; | 
					
						
							| 
									
										
										
										
											2016-08-12 03:38:24 -04:00
										 |  |  |   opts._workers = []; | 
					
						
							| 
									
										
										
										
											2016-08-12 03:02:33 -04:00
										 |  |  |   opts.webrootPath = opts.webrootPath || require('os').tmpdir() + require('path').sep + 'acme-challenge'; | 
					
						
							| 
									
										
										
										
											2017-01-25 14:53:56 -07:00
										 |  |  |   if (!opts.greenlock) { opts.greenlock = require('greenlock').create(opts); } | 
					
						
							| 
									
										
										
										
											2016-08-10 16:31:25 -04:00
										 |  |  |   if ('function' !== typeof opts.approveDomains) { | 
					
						
							| 
									
										
										
										
											2016-08-10 22:33:12 -04:00
										 |  |  |     throw new Error("You must provide opts.approveDomains(domain, certs, callback) to approve certificates"); | 
					
						
							| 
									
										
										
										
											2016-08-10 13:10:00 -04:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function log(debug) { | 
					
						
							|  |  |  |     if (!debug) { | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     var args = Array.prototype.slice.call(arguments); | 
					
						
							|  |  |  |     args.shift(); | 
					
						
							|  |  |  |     args.unshift("[le/lib/core.js]"); | 
					
						
							|  |  |  |     console.log.apply(console, args); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   opts.addWorker = function (worker) { | 
					
						
							| 
									
										
										
										
											2016-08-12 03:38:24 -04:00
										 |  |  |     opts._workers.push(worker); | 
					
						
							| 
									
										
										
										
											2016-08-10 13:10:00 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     worker.on('online', function () { | 
					
						
							|  |  |  |       log(opts.debug, 'worker is up'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     worker.on('message', function (msg) { | 
					
						
							| 
									
										
										
										
											2016-08-11 03:07:20 -04:00
										 |  |  |       log(opts.debug, 'Message from worker ' + worker.id); | 
					
						
							| 
									
										
										
										
											2016-08-10 13:10:00 -04:00
										 |  |  |       if ('LE_REQUEST' !== (msg && msg.type)) { | 
					
						
							| 
									
										
										
										
											2016-08-11 03:07:20 -04:00
										 |  |  |         log(opts.debug, 'Ignoring irrelevant message'); | 
					
						
							|  |  |  |         log(opts.debug, msg); | 
					
						
							| 
									
										
										
										
											2016-08-10 13:10:00 -04:00
										 |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-11 03:07:20 -04:00
										 |  |  |       log(opts.debug, 'about to approveDomains'); | 
					
						
							| 
									
										
										
										
											2016-08-11 18:44:35 -04:00
										 |  |  |       opts.approveDomains(msg.options, msg.certs, function (err, results) { | 
					
						
							| 
									
										
										
										
											2016-08-10 13:10:00 -04:00
										 |  |  |         if (err) { | 
					
						
							|  |  |  |           log(opts.debug, 'Approval got ERROR', err.stack || err); | 
					
						
							| 
									
										
										
										
											2016-08-11 18:44:35 -04:00
										 |  |  |           worker.send({ | 
					
						
							|  |  |  |             type: 'LE_RESPONSE' | 
					
						
							|  |  |  |           , domain: msg.domain | 
					
						
							|  |  |  |           , error: { message: err.message, code: err.code, stack: err.stack } | 
					
						
							|  |  |  |           }); | 
					
						
							| 
									
										
										
										
											2016-08-10 13:10:00 -04:00
										 |  |  |           return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         var promise; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-11 18:44:35 -04:00
										 |  |  |         //
 | 
					
						
							|  |  |  |         /* | 
					
						
							| 
									
										
										
										
											2016-08-11 03:07:20 -04:00
										 |  |  |         var certs = require('localhost.daplie.com-certificates').merge({ | 
					
						
							|  |  |  |           subject: msg.domain | 
					
						
							|  |  |  |         , altnames: [ msg.domain ] | 
					
						
							|  |  |  |         , issuedAt: Date.now() | 
					
						
							|  |  |  |         , expiresAt: Date.now() + (90 * 24 * 60 * 60 * 1000) | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         certs.privkey = certs.key.toString('ascii'); | 
					
						
							|  |  |  |         certs.cert = certs.cert.toString('ascii'); | 
					
						
							|  |  |  |         certs.chain = ''; | 
					
						
							|  |  |  |         worker.send({ type: 'LE_RESPONSE', domain: msg.domain, certs: certs }); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |         // */
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-10 13:10:00 -04:00
										 |  |  |         if (results.certs) { | 
					
						
							| 
									
										
										
										
											2017-01-25 14:53:56 -07:00
										 |  |  |           promise = opts.greenlock.renew(results.options, results.certs); | 
					
						
							| 
									
										
										
										
											2016-08-10 13:10:00 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							| 
									
										
										
										
											2017-01-25 14:53:56 -07:00
										 |  |  |           promise = opts.greenlock.register(results.options); | 
					
						
							| 
									
										
										
										
											2016-08-10 13:10:00 -04:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         promise.then(function (certs) { | 
					
						
							|  |  |  |           log(opts.debug, 'Approval got certs', certs); | 
					
						
							|  |  |  |           // certs = { subject, domains, issuedAt, expiresAt, privkey, cert, chain };
 | 
					
						
							| 
									
										
										
										
											2016-08-12 03:38:24 -04:00
										 |  |  |           opts._workers.forEach(function (w) { | 
					
						
							|  |  |  |             w.send({ type: 'LE_RESPONSE', domain: msg.domain, certs: certs }); | 
					
						
							|  |  |  |           }); | 
					
						
							| 
									
										
										
										
											2016-08-10 13:10:00 -04:00
										 |  |  |         }, function (err) { | 
					
						
							|  |  |  |           log(opts.debug, 'Approval got ERROR', err.stack || err); | 
					
						
							| 
									
										
										
										
											2016-08-11 03:07:20 -04:00
										 |  |  |           worker.send({ type: 'LE_RESPONSE', domain: msg.domain, error: err }); | 
					
						
							| 
									
										
										
										
											2016-08-10 13:10:00 -04:00
										 |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return opts; | 
					
						
							|  |  |  | }; |