| 
									
										
										
										
											2015-12-15 15:21:27 +00:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-19 11:59:50 -08:00
										 |  |  | var path = require('path'); | 
					
						
							|  |  |  | var homeRe = new RegExp("^~(\\/|\\\|\\" + path.sep + ")"); | 
					
						
							| 
									
										
										
										
											2015-12-19 02:18:32 -08:00
										 |  |  | var re = /^[a-zA-Z0-9\.\-]+$/; | 
					
						
							|  |  |  | var punycode = require('punycode'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-06 01:32:59 -04:00
										 |  |  | module.exports.attachCertInfo = function (results) { | 
					
						
							|  |  |  |   var getCertInfo = require('./cert-info').getCertInfo; | 
					
						
							|  |  |  |   // XXX Note: Parsing the certificate info comes at a great cost (~500kb)
 | 
					
						
							|  |  |  |   var certInfo = getCertInfo(results.cert); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   //results.issuedAt = arr[3].mtime.valueOf()
 | 
					
						
							|  |  |  |   results.issuedAt = Date(certInfo.notBefore.value).valueOf(); // Date.now()
 | 
					
						
							|  |  |  |   results.expiresAt = Date(certInfo.notAfter.value).valueOf(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return results; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-19 11:59:50 -08:00
										 |  |  | module.exports.isValidDomain = function (domain) { | 
					
						
							| 
									
										
										
										
											2015-12-19 02:18:32 -08:00
										 |  |  |   if (re.test(domain)) { | 
					
						
							|  |  |  |     return domain; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   domain = punycode.toASCII(domain); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (re.test(domain)) { | 
					
						
							|  |  |  |     return domain; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ''; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-04 18:49:35 -04:00
										 |  |  | module.exports.merge = function (/*defaults, args*/) { | 
					
						
							|  |  |  |   var allDefaults = Array.prototype.slice.apply(arguments); | 
					
						
							|  |  |  |   var args = args.shift(); | 
					
						
							| 
									
										
										
										
											2015-12-17 08:46:40 +00:00
										 |  |  |   var copy = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-04 18:49:35 -04:00
										 |  |  |   allDefaults.forEach(function (defaults) { | 
					
						
							|  |  |  |     Object.keys(defaults).forEach(function (key) { | 
					
						
							|  |  |  |       copy[key] = defaults[key]; | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-12-17 08:46:40 +00:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2016-08-04 18:49:35 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-17 08:46:40 +00:00
										 |  |  |   Object.keys(args).forEach(function (key) { | 
					
						
							|  |  |  |     copy[key] = args[key]; | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return copy; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-04 18:49:35 -04:00
										 |  |  | module.exports.tplCopy = function (copy) { | 
					
						
							| 
									
										
										
										
											2015-12-19 11:59:50 -08:00
										 |  |  |   var homedir = require('homedir')(); | 
					
						
							| 
									
										
										
										
											2015-12-20 00:27:48 +00:00
										 |  |  |   var tpls = { | 
					
						
							|  |  |  |     hostname: (copy.domains || [])[0] | 
					
						
							|  |  |  |   , server: (copy.server || '').replace('https://', '').replace(/(\/)$/, '') | 
					
						
							|  |  |  |   , conf: copy.configDir | 
					
						
							|  |  |  |   , config: copy.configDir | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-17 08:46:40 +00:00
										 |  |  |   Object.keys(copy).forEach(function (key) { | 
					
						
							| 
									
										
										
										
											2016-08-05 04:14:40 -04:00
										 |  |  |     if ('string' !== typeof copy[key]) { | 
					
						
							|  |  |  |       return; | 
					
						
							| 
									
										
										
										
											2015-12-17 08:46:40 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-05 04:14:40 -04:00
										 |  |  |     copy[key] = copy[key].replace(homeRe, homedir + path.sep); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Object.keys(tpls).sort(function (a, b) { | 
					
						
							|  |  |  |       return b.length - a.length; | 
					
						
							|  |  |  |     }).forEach(function (tplname) { | 
					
						
							|  |  |  |       if (!tpls[tplname]) { | 
					
						
							|  |  |  |         // what can't be templated now may be templatable later
 | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       copy[key] = copy[key].replace(':' + tplname, tpls[tplname]); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2015-12-17 08:46:40 +00:00
										 |  |  | }; |