index.js 23.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
'use strict';
/*global Promise*/
require('./lib/compat.js');

// I hate this code so much.
// Soooo many shims for backwards compatibility (some stuff dating back to v1)
// v3 will be a clean break and I'll delete half of the code...

var DAY = 24 * 60 * 60 * 1000;
//var MIN = 60 * 1000;
var ACME = require('acme-v2/compat').ACME;
var pkg = require('./package.json');
var util = require('util');

function promisifyAllSelf(obj) {
	if (obj.__promisified) {
		return obj;
	}
	Object.keys(obj).forEach(function(key) {
		if ('function' === typeof obj[key] && !/Async$/.test(key)) {
			obj[key + 'Async'] = util.promisify(obj[key]);
		}
	});
	obj.__promisified = true;
	return obj;
}
function promisifyAllStore(obj) {
	Object.keys(obj).forEach(function(key) {
		if ('function' !== typeof obj[key] || /Async$/.test(key)) {
			return;
		}

		var p;
		if (0 === obj[key].length || 1 === obj[key].length) {
			// wrap just in case it's synchronous (or improperly throws)
			p = function(opts) {
				return Promise.resolve().then(function() {
					return obj[key](opts);
				});
			};
		} else {
			p = util.promisify(obj[key]);
		}
		// internal backwards compat
		obj[key + 'Async'] = p;
	});
	obj.__promisified = true;
	return obj;
}

var Greenlock = module.exports;
Greenlock.Greenlock = Greenlock;
Greenlock.LE = Greenlock;
// in-process cache, shared between all instances
var ipc = {};

function _log(debug) {
	if (debug) {
		var args = Array.prototype.slice.call(arguments);
		args.shift();
		args.unshift('[gl/index.js]');
		console.log.apply(console, args);
	}
}

Greenlock.defaults = {
	productionServerUrl: 'https://acme-v01.api.letsencrypt.org/directory',
	stagingServerUrl: 'https://acme-staging.api.letsencrypt.org/directory',

	rsaKeySize: ACME.rsaKeySize || 2048,
	challengeType: ACME.challengeType || 'http-01',
	challengeTypes: ACME.challengeTypes || ['http-01', 'dns-01'],

	acmeChallengePrefix: ACME.acmeChallengePrefix
};

// backwards compat
Object.keys(Greenlock.defaults).forEach(function(key) {
	Greenlock[key] = Greenlock.defaults[key];
});

// show all possible options
var u; // undefined
Greenlock._undefined = {
	acme: u,
	store: u,
	//, challenge: u
	challenges: u,
	sni: u,
	tlsOptions: u,

	register: u,
	check: u,

	renewWithin: u, // le-auto-sni and core
	//, renewBy: u // le-auto-sni
	acmeChallengePrefix: u,
	rsaKeySize: u,
	challengeType: u,
	server: u,
	version: u,
	agreeToTerms: u,
	_ipc: u,
	duplicate: u,
	_acmeUrls: u
};
Greenlock._undefine = function(gl) {
	Object.keys(Greenlock._undefined).forEach(function(key) {
		if (!(key in gl)) {
			gl[key] = u;
		}
	});

	return gl;
};
Greenlock.create = function(gl) {
	if (!gl.store) {
		console.warn(
			"Deprecation Notice: You're haven't chosen a storage strategy." +
				" The old default is 'le-store-certbot', but the new default will be 'greenlock-store-fs'." +
				" Please `npm install greenlock-store-fs@3` and explicitly set `{ store: require('greenlock-store-fs') }`."
		);
		gl.store = require('le-store-certbot').create({
			debug: gl.debug,
			configDir: gl.configDir,
			logsDir: gl.logsDir,
			webrootPath: gl.webrootPath
		});
	}
	gl.core = require('./lib/core');
	var log = gl.log || _log;

	if (!gl.challenges) {
		gl.challenges = {};
	}
	if (!gl.challenges['http-01']) {
		gl.challenges['http-01'] = require('le-challenge-fs').create({
			debug: gl.debug,
			webrootPath: gl.webrootPath
		});
	}
	if (!gl.challenges['dns-01']) {
		try {
			gl.challenges['dns-01'] = require('le-challenge-ddns').create({
				debug: gl.debug
			});
		} catch (e) {
			try {
				gl.challenges['dns-01'] = require('le-challenge-dns').create({
					debug: gl.debug
				});
			} catch (e) {
				// not yet implemented
			}
		}
	}

	gl = Greenlock._undefine(gl);
	gl.acmeChallengePrefix = Greenlock.acmeChallengePrefix;
	gl.rsaKeySize = gl.rsaKeySize || Greenlock.rsaKeySize;
	gl.challengeType = gl.challengeType || Greenlock.challengeType;
	gl._ipc = ipc;
	gl._communityPackage = gl._communityPackage || 'greenlock.js';
	if ('greenlock.js' === gl._communityPackage) {
		gl._communityPackageVersion = pkg.version;
	} else {
		gl._communityPackageVersion =
			gl._communityPackageVersion || 'greenlock.js-' + pkg.version;
	}
	gl.agreeToTerms =
		gl.agreeToTerms ||
		function(args, agreeCb) {
			agreeCb(
				new Error(
					"'agreeToTerms' was not supplied to Greenlock and 'agreeTos' was not supplied to Greenlock.register"
				)
			);
		};

	if (!gl.renewWithin) {
		gl.renewWithin = 14 * DAY;
	}
	// renewBy has a default in le-sni-auto

	///////////////////////////
	// BEGIN VERSION MADNESS //
	///////////////////////////

	gl.version = gl.version || 'draft-11';
	gl.server = gl.server || 'https://acme-v02.api.letsencrypt.org/directory';
	if (!gl.version) {
		//console.warn("Please specify version: 'v01' (Let's Encrypt v1) or 'draft-12' (Let's Encrypt v2 / ACME draft 12)");
		console.warn('');
		console.warn('');
		console.warn('');
		console.warn(
			'=========================================================='
		);
		console.warn(
			'==                greenlock.js (v2.2.0+)                =='
		);
		console.warn(
			'=========================================================='
		);
		console.warn('');
		console.warn("Please specify 'version' option:");
		console.warn('');
		console.warn(
			"        'draft-12' for Let's Encrypt v2 and ACME draft 12"
		);
		console.warn("        ('v02' is an alias of 'draft-12'");
		console.warn('');
		console.warn('or');
		console.warn('');
		console.warn("        'v01' for Let's Encrypt v1 (deprecated)");
		console.warn(
			"         (also 'npm install --save le-acme-core' as this legacy dependency will soon be removed)"
		);
		console.warn('');
		console.warn('This will be required in versions v2.3+');
		console.warn('');
		console.warn('');
	} else if ('v02' === gl.version) {
		gl.version = 'draft-11';
	} else if ('draft-12' === gl.version) {
		gl.version = 'draft-11';
	} else if ('draft-11' === gl.version) {
		// no-op
	} else if ('v01' !== gl.version) {
		throw new Error("Unrecognized version '" + gl.version + "'");
	}

	if (!gl.server) {
		throw new Error(
			"opts.server must specify an ACME directory URL, such as 'https://acme-staging-v02.api.letsencrypt.org/directory'"
		);
	}
	if ('staging' === gl.server || 'production' === gl.server) {
		if ('staging' === gl.server) {
			gl.server = 'https://acme-staging.api.letsencrypt.org/directory';
			gl.version = 'v01';
			gl._deprecatedServerName = 'staging';
		} else if ('production' === gl.server) {
			gl.server = 'https://acme-v01.api.letsencrypt.org/directory';
			gl.version = 'v01';
			gl._deprecatedServerName = 'production';
		}
		console.warn('');
		console.warn('');
		console.warn('=== WARNING ===');
		console.warn('');
		console.warn(
			"Due to versioning issues the '" +
				gl._deprecatedServerName +
				"' option is deprecated."
		);
		console.warn('Please specify the full url and version.');
		console.warn('');
		console.warn('For APIs add:');
		console.warn('\t, "version": "' + gl.version + '"');
		console.warn('\t, "server": "' + gl.server + '"');
		console.warn('');
		console.warn('For the CLI add:');
		console.warn("\t--acme-url '" + gl.server + "' \\");
		console.warn("\t--acme-version '" + gl.version + "' \\");
		console.warn('');
		console.warn('');
	}

	function loadLeV01() {
		console.warn('');
		console.warn('=== WARNING ===');
		console.warn('');
		console.warn("Let's Encrypt v1 is deprecated.");
		console.warn("Please update to Let's Encrypt v2 (ACME draft 12)");
		console.warn('');
		try {
			return require('le-acme-core').ACME;
		} catch (e) {
			console.error('');
			console.error('=== Error (easy-to-fix) ===');
			console.error('');
			console.error(
				"Hey, this isn't a big deal, but you need to manually add v1 support:"
			);
			console.error('');
			console.error('        npm install --save le-acme-core');
			console.error('');
			console.error(
				'Just run that real quick, restart, and everything will work great.'
			);
			console.error('');
			console.error('');
			process.exit(e.code || 13);
		}
	}

	if (
		-1 !==
		[
			'https://acme-v02.api.letsencrypt.org/directory',
			'https://acme-staging-v02.api.letsencrypt.org/directory'
		].indexOf(gl.server)
	) {
		if ('draft-11' !== gl.version) {
			console.warn(
				"Detected Let's Encrypt v02 URL. Changing version to draft-12."
			);
			gl.version = 'draft-11';
		}
	} else if (
		-1 !==
			[
				'https://acme-v01.api.letsencrypt.org/directory',
				'https://acme-staging.api.letsencrypt.org/directory'
			].indexOf(gl.server) ||
		'v01' === gl.version
	) {
		if ('v01' !== gl.version) {
			console.warn(
				"Detected Let's Encrypt v01 URL (deprecated). Changing version to v01."
			);
			gl.version = 'v01';
		}
	}
	if ('v01' === gl.version) {
		ACME = loadLeV01();
	}
	/////////////////////////
	// END VERSION MADNESS //
	/////////////////////////

	gl.acme =
		gl.acme ||
		ACME.create({
			debug: gl.debug,
			skipChallengeTest: gl.skipChallengeTest,
			skipDryRun: gl.skipDryRun
		});
	if (gl.acme.create) {
		gl.acme = gl.acme.create(gl);
	}
	gl.acme = promisifyAllSelf(gl.acme);
	gl._acmeOpts =
		(gl.acme.getOptions && gl.acme.getOptions()) || gl.acme.options || {};
	Object.keys(gl._acmeOpts).forEach(function(key) {
		if (!(key in gl)) {
			gl[key] = gl._acmeOpts[key];
		}
	});

	try {
		if (gl.store.create) {
			gl.store = gl.store.create(gl);
		}
		gl.store = promisifyAllSelf(gl.store);
		gl.store.accounts = promisifyAllStore(gl.store.accounts);
		gl.store.certificates = promisifyAllStore(gl.store.certificates);
		gl._storeOpts =
			(gl.store.getOptions && gl.store.getOptions()) ||
			gl.store.options ||
			{};
	} catch (e) {
		console.error(e);
		console.error(
			'\nPROBABLE CAUSE:\n' +
				'\tYour greenlock-store module should have a create function and return { options, accounts, certificates }\n'
		);
		process.exit(18);
		return;
	}
	Object.keys(gl._storeOpts).forEach(function(key) {
		if (!(key in gl)) {
			gl[key] = gl._storeOpts[key];
		}
	});

	//
	// Backwards compat for <= v2.1.7
	//
	if (gl.challenge) {
		console.warn(
			"Deprecated use of gl.challenge. Use gl.challenges['" +
				Greenlock.challengeType +
				"'] instead."
		);
		gl.challenges[gl.challengeType] = gl.challenge;
		gl.challenge = undefined;
	}

	Object.keys(gl.challenges || {}).forEach(function(challengeType) {
		var challenger = gl.challenges[challengeType];

		if (challenger.create) {
			challenger = gl.challenges[challengeType] = challenger.create(gl);
		}
		challenger = gl.challenges[challengeType] = promisifyAllSelf(
			challenger
		);
		gl['_challengeOpts_' + challengeType] =
			(challenger.getOptions && challenger.getOptions()) ||
			challenger.options ||
			{};
		Object.keys(gl['_challengeOpts_' + challengeType]).forEach(function(
			key
		) {
			if (!(key in gl)) {
				gl[key] = gl['_challengeOpts_' + challengeType][key];
			}
		});

		// TODO wrap these here and now with tplCopy?
		if (!challenger.set || ![5, 2, 1].includes(challenger.set.length)) {
			throw new Error(
				'gl.challenges[' +
					challengeType +
					'].set receives the wrong number of arguments.' +
					' You must define setChallenge as function (opts) { return Promise.resolve(); }'
			);
		}
		if (challenger.get && ![4, 2, 1].includes(challenger.get.length)) {
			throw new Error(
				'gl.challenges[' +
					challengeType +
					'].get receives the wrong number of arguments.' +
					' You must define getChallenge as function (opts) { return Promise.resolve(); }'
			);
		}
		if (
			!challenger.remove ||
			![4, 2, 1].includes(challenger.remove.length)
		) {
			throw new Error(
				'gl.challenges[' +
					challengeType +
					'].remove receives the wrong number of arguments.' +
					' You must define removeChallenge as function (opts) { return Promise.resolve(); }'
			);
		}

		/*
    if (!gl._challengeWarn && (!challenger.loopback || 4 !== challenger.loopback.length)) {
      gl._challengeWarn = true;
      console.warn("gl.challenges[" + challengeType + "].loopback should be defined as function (opts, domain, token, cb) { ... } and should prove (by external means) that the ACME server challenge '" + challengeType + "' will succeed");
    }
    else if (!gl._challengeWarn && (!challenger.test || 5 !== challenger.test.length)) {
      gl._challengeWarn = true;
      console.warn("gl.challenges[" + challengeType + "].test should be defined as function (opts, domain, token, keyAuthorization, cb) { ... } and should prove (by external means) that the ACME server challenge '" + challengeType + "' will succeed");
    }
*/
	});

	gl.sni = gl.sni || null;
	gl.tlsOptions = gl.tlsOptions || gl.httpsOptions || {};

	// Workaround for https://github.com/nodejs/node/issues/22389
	gl._updateServernames = function(cert) {
		if (!gl._certnames) {
			gl._certnames = {};
		}

		// Note: Any given domain could exist on multiple certs
		// (especially during renewal where some may be added)
		// hence we use a separate object for each domain and list each domain on it
		// to get the minimal full set associated with each cert and domain
		var allDomains = [cert.subject].concat(cert.altnames.slice(0));
		allDomains.forEach(function(name) {
			name = name.toLowerCase();
			if (!gl._certnames[name]) {
				gl._certnames[name] = {};
			}
			allDomains.forEach(function(name2) {
				name2 = name2.toLowerCase();
				gl._certnames[name][name2] = true;
			});
		});
	};
	gl._checkServername = function(safeHost, servername) {
		// odd, but acceptable
		if (!safeHost || !servername) {
			return true;
		}
		if (safeHost === servername) {
			return true;
		}
		// connection established with servername and session is re-used for allowed name
		if (gl._certnames[servername] && gl._certnames[servername][safeHost]) {
			return true;
		}
		return false;
	};

	if (!gl.tlsOptions.SNICallback) {
		if (!gl.getCertificatesAsync && !gl.getCertificates) {
			if (Array.isArray(gl.approveDomains)) {
				gl.approvedDomains = gl.approveDomains;
				gl.approveDomains = null;
			}
			if (!gl.approveDomains) {
				gl.approveDomains = function(lexOpts, cb) {
					var err;
					var emsg;

					if (!gl.email) {
						throw new Error(
							'le-sni-auto is not properly configured. Missing email'
						);
					}
					if (!gl.agreeTos) {
						throw new Error(
							'le-sni-auto is not properly configured. Missing agreeTos'
						);
					}
					if (!/[a-z]/i.test(lexOpts.domain)) {
						cb(
							new Error(
								'le-sni-auto does not allow IP addresses in SNI'
							)
						);
						return;
					}

					if (!Array.isArray(gl.approvedDomains)) {
						// The acme-v2 package uses pre-flight test challenges to
						// verify that each requested domain is hosted by the server
						// these checks are sufficient for most use cases
						return cb(null, lexOpts);
					}

					if (
						lexOpts.domains.every(function(domain) {
							return -1 !== gl.approvedDomains.indexOf(domain);
						})
					) {
						// commented this out because people expect to be able to edit the list of domains
						// lexOpts.domains = gl.approvedDomains.slice(0);
						lexOpts.email = gl.email;
						lexOpts.agreeTos = gl.agreeTos;
						lexOpts.communityMember = gl.communityMember;
						lexOpts.telemetry = gl.telemetry;
						return cb(null, lexOpts);
					}

					emsg =
						"tls SNI for '" +
						lexOpts.domains.join(',') +
						"' rejected: not in list '" +
						gl.approvedDomains +
						"'";
					log(gl.debug, emsg, lexOpts.domains, gl.approvedDomains);
					err = new Error(emsg);
					err.code = 'E_REJECT_SNI';
					cb(err);
				};
			}

			gl.getCertificates = function(domain, certs, cb) {
				// certs come from current in-memory cache, not lookup
				log(
					gl.debug,
					'gl.getCertificates called for',
					domain,
					'with certs for',
					(certs && certs.altnames) || 'NONE'
				);
				var opts = {
					domain: domain,
					domains: (certs && certs.altnames) || [domain],
					certs: certs,
					certificate: {},
					account: {}
				};
				opts.wildname =
					'*.' +
					(domain || '')
						.split('.')
						.slice(1)
						.join('.');

				function cb2(results) {
					log(
						gl.debug,
						'gl.approveDomains called with certs for',
						(results.certs && results.certs.altnames) || 'NONE',
						'and options:'
					);
					log(gl.debug, results.options || results);
					var err;
					if (!results) {
						err = new Error('E_REJECT_SNI');
						err.code = 'E_REJECT_SNI';
						eb2(err);
						return;
					}

					var options = results.options || results;
					if (opts !== options) {
						Object.keys(options).forEach(function(key) {
							if (
								'undefined' !== typeof options[key] &&
								'domain' !== key
							) {
								opts[key] = options[key];
							}
						});
						options = opts;
					}
					if (
						Array.isArray(options.altnames) &&
						options.altnames.length
					) {
						options.domains = options.altnames;
					}
					options.altnames = options.domains;
					// just in case we get a completely different object from the one we originally created
					if (!options.account) {
						options.account = {};
					}
					if (!options.certificate) {
						options.certificate = {};
					}
					if (results.certs) {
						log(gl.debug, 'gl renewing');
						return gl.core.certificates
							.renewAsync(options, results.certs)
							.then(
								function(certs) {
									// Workaround for https://github.com/nodejs/node/issues/22389
									gl._updateServernames(certs);
									cb(null, certs);
								},
								function(e) {
									console.debug(
										"Error renewing certificate for '" +
											domain +
											"':"
									);
									console.debug(e);
									console.error('');
									cb(e);
								}
							);
					} else {
						log(
							gl.debug,
							'gl getting from disk or registering new'
						);
						return gl.core.certificates.getAsync(options).then(
							function(certs) {
								// Workaround for https://github.com/nodejs/node/issues/22389
								gl._updateServernames(certs);
								cb(null, certs);
							},
							function(e) {
								console.debug(
									"Error loading/registering certificate for '" +
										domain +
										"':"
								);
								console.debug(e);
								console.error('');
								cb(e);
							}
						);
					}
				}
				function eb2(_err) {
					if (false !== gl.logRejectedDomains) {
						console.error(
							"[Error] approveDomains rejected tls sni '" +
								domain +
								"'"
						);
						console.error(
							'[Error] (see https://git.coolaj86.com/coolaj86/greenlock.js/issues/11)'
						);
						if ('E_REJECT_SNI' !== _err.code) {
							console.error(
								'[Error] This is the rejection message:'
							);
							console.error(_err.message);
						}
						console.error('');
					}
					cb(_err);
					return;
				}
				function mb2(_err, results) {
					if (_err) {
						eb2(_err);
						return;
					}
					cb2(results);
				}

				try {
					if (1 === gl.approveDomains.length) {
						Promise.resolve(gl.approveDomains(opts))
							.then(cb2)
							.catch(eb2);
					} else if (2 === gl.approveDomains.length) {
						gl.approveDomains(opts, mb2);
					} else {
						gl.approveDomains(opts, certs, mb2);
					}
				} catch (e) {
					console.error(
						'[ERROR] Something went wrong in approveDomains:'
					);
					console.error(e);
					console.error(
						"BUT WAIT! Good news: It's probably your fault, so you can probably fix it."
					);
				}
			};
		}
		gl.sni = gl.sni || require('le-sni-auto');
		if (gl.sni.create) {
			gl.sni = gl.sni.create(gl);
		}
		gl.tlsOptions.SNICallback = function(_domain, cb) {
			// format and (lightly) sanitize sni so that users can be naive
			// and not have to worry about SQL injection or fs discovery
			var domain = (_domain || '').toLowerCase();
			// hostname labels allow a-z, 0-9, -, and are separated by dots
			// _ is sometimes allowed
			// REGEX // https://www.codeproject.com/Questions/1063023/alphanumeric-validation-javascript-without-regex
			if (
				!gl.__sni_allow_dangerous_names &&
				(!/^[a-z0-9_\.\-]+$/i.test(domain) ||
					-1 !== domain.indexOf('..'))
			) {
				log(gl.debug, "invalid sni '" + domain + "'");
				cb(new Error('invalid SNI'));
				return;
			}

			try {
				gl.sni.sniCallback(
					(gl.__sni_preserve_case && _domain) || domain,
					cb
				);
			} catch (e) {
				console.error(
					'[ERROR] Something went wrong in the SNICallback:'
				);
				console.error(e);
				cb(e);
			}
		};
	}

	// We want to move to using tlsOptions instead of httpsOptions, but we also need to make
	// sure anything that uses this object will still work if looking for httpsOptions.
	gl.httpsOptions = gl.tlsOptions;

	if (gl.core.create) {
		gl.core = gl.core.create(gl);
	}

	gl.renew = function(args, certs) {
		return gl.core.certificates.renewAsync(args, certs);
	};

	gl.register = function(args) {
		return gl.core.certificates.getAsync(args);
	};

	gl.check = function(args) {
		// TODO must return email, domains, tos, pems
		return gl.core.certificates.checkAsync(args);
	};

	gl.middleware = gl.middleware || require('./lib/middleware');
	if (gl.middleware.create) {
		gl.middleware = gl.middleware.create(gl);
	}

	//var SERVERNAME_RE = /^[a-z0-9\.\-_]+$/;
	var SERVERNAME_G = /[^a-z0-9\.\-_]/;
	gl.middleware.sanitizeHost = function(app) {
		return function(req, res, next) {
			function realNext() {
				if ('function' === typeof app) {
					app(req, res);
				} else if ('function' === typeof next) {
					next();
				} else {
					res.statusCode = 500;
					res.end('Error: no middleware assigned');
				}
			}
			// Get the host:port combo, if it exists
			var host = (req.headers.host || '').split(':');

			// if not, move along
			if (!host[0]) {
				realNext();
				return;
			}

			// if so, remove non-allowed characters
			var safehost = host[0].toLowerCase().replace(SERVERNAME_G, '');

			// if there were unallowed characters, complain
			if (
				!gl.__sni_allow_dangerous_names &&
				safehost.length !== host[0].length
			) {
				res.statusCode = 400;
				res.end("Malformed HTTP Header: 'Host: " + host[0] + "'");
				return;
			}

			// make lowercase
			if (!gl.__sni_preserve_case) {
				host[0] = safehost;
				req.headers.host = host.join(':');
			}

			// Note: This sanitize function is also called on plain sockets, which don't need Domain Fronting checks
			if (req.socket.encrypted && !gl.__sni_allow_domain_fronting) {
				if (req.socket && 'string' === typeof req.socket.servername) {
					// Workaround for https://github.com/nodejs/node/issues/22389
					if (
						!gl._checkServername(
							safehost,
							req.socket.servername.toLowerCase()
						)
					) {
						res.statusCode = 400;
						res.setHeader(
							'Content-Type',
							'text/html; charset=utf-8'
						);
						res.end(
							'<h1>Domain Fronting Error</h1>' +
								"<p>This connection was secured using TLS/SSL for '" +
								req.socket.servername.toLowerCase() +
								"'</p>" +
								"<p>The HTTP request specified 'Host: " +
								safehost +
								"', which is (obviously) different.</p>" +
								'<p>Because this looks like a domain fronting attack, the connection has been terminated.</p>'
						);
						return;
					}
				} else if (
					safehost &&
					!gl.middleware.sanitizeHost._skip_fronting_check
				) {
					// TODO how to handle wrapped sockets, as with telebit?
					console.warn(
						'\n\n\n[greenlock] WARN: no string for req.socket.servername,' +
							" skipping fronting check for '" +
							safehost +
							"'\n\n\n"
					);
					gl.middleware.sanitizeHost._skip_fronting_check = true;
				}
			}

			// carry on
			realNext();
		};
	};
	gl.middleware.sanitizeHost._skip_fronting_check = false;

	return gl;
};