| 
									
										
										
										
											2016-08-10 20:43:35 -04:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-11 03:07:20 -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); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-10 20:43:35 -04:00
										 |  |  | module.exports.create = function (opts) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-12 03:38:24 -04:00
										 |  |  |   // if another worker updates the certs,
 | 
					
						
							|  |  |  |   // receive a copy from master here as well
 | 
					
						
							|  |  |  |   // and update the sni cache manually
 | 
					
						
							|  |  |  |   process.on('message', function (msg) { | 
					
						
							|  |  |  |     if ('LE_RESPONSE' === msg.type && msg.certs) { | 
					
						
							|  |  |  |       opts.sni.cacheCerts(msg.certs); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2016-08-11 00:58:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-11 01:10:37 -04:00
										 |  |  |   opts.sni = require('le-sni-auto').create({ | 
					
						
							| 
									
										
										
										
											2016-08-12 03:48:21 -04:00
										 |  |  |     renewWithin: opts.renewWithin || (10 * 24 * 60 * 60 * 1000) | 
					
						
							|  |  |  |   , renewBy: opts.renewBy || (5 * 24 * 60 * 60 * 1000) | 
					
						
							| 
									
										
										
										
											2016-08-11 00:58:14 -04:00
										 |  |  |   , getCertificates: function (domain, certs, cb) { | 
					
						
							| 
									
										
										
										
											2016-08-11 18:44:35 -04:00
										 |  |  |       var workerOptions = { domains: [ domain ] }; | 
					
						
							|  |  |  |       opts.approveDomains(workerOptions, certs, function (_err, results) { | 
					
						
							|  |  |  |         if (_err) { | 
					
						
							|  |  |  |           cb(_err); | 
					
						
							|  |  |  |           return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         var err = new Error("___MESSAGE___"); | 
					
						
							|  |  |  |         process.send({ type: 'LE_REQUEST', domain: domain, options: results.options, certs: results.certs }); | 
					
						
							| 
									
										
										
										
											2016-08-10 20:43:35 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         process.on('message', function (msg) { | 
					
						
							| 
									
										
										
										
											2016-08-11 03:07:20 -04:00
										 |  |  |           log(opts.debug, 'Message from master'); | 
					
						
							|  |  |  |           log(opts.debug, msg); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-11 18:44:35 -04:00
										 |  |  |           if (msg.domain !== domain) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           if (msg.error) { | 
					
						
							|  |  |  |             err.message = msg.error.message || "unknown error sent from cluster master to worker"; | 
					
						
							|  |  |  |             err.stack.replace("___MESSAGE___", err.message); | 
					
						
							|  |  |  |             err = { | 
					
						
							|  |  |  |               message: err.message | 
					
						
							|  |  |  |             , stack: err.stack | 
					
						
							|  |  |  |             , data: { options: workerOptions, certs: certs } | 
					
						
							|  |  |  |             }; | 
					
						
							|  |  |  |           } else { | 
					
						
							|  |  |  |             err = null; | 
					
						
							| 
									
										
										
										
											2016-08-10 20:43:35 -04:00
										 |  |  |           } | 
					
						
							| 
									
										
										
										
											2016-08-11 18:44:35 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |           cb(err, msg.certs); | 
					
						
							| 
									
										
										
										
											2016-08-10 20:43:35 -04:00
										 |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-08-11 01:10:37 -04:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2016-08-10 20:43:35 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-11 00:58:14 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-11 01:10:37 -04:00
										 |  |  |   opts.httpsOptions = require('localhost.daplie.com-certificates').merge({ SNICallback: opts.sni.sniCallback }); | 
					
						
							| 
									
										
										
										
											2016-08-11 00:58:14 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   opts.challenge = { | 
					
						
							|  |  |  |     get: opts.getChallenge | 
					
						
							|  |  |  |       || (opts.challenge && opts.challenge.get) | 
					
						
							|  |  |  |       || require('le-challenge-fs').create({ webrootPath: opts.webrootPath }).get | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // opts.challenge.get, opts.acmeChallengePrefix
 | 
					
						
							| 
									
										
										
										
											2017-01-25 14:53:56 -07:00
										 |  |  |   opts.middleware = require('greenlock/lib/middleware').create(opts); | 
					
						
							| 
									
										
										
										
											2016-08-11 00:58:14 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-10 20:43:35 -04:00
										 |  |  |   return opts; | 
					
						
							|  |  |  | }; |