| 
									
										
										
										
											2016-10-17 15:44:49 -06:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-12 19:17:26 -06:00
										 |  |  | var PromiseA = require('bluebird'); | 
					
						
							|  |  |  | var resolveTxtAsync = PromiseA.promisify(require('dns').resolveTxt); | 
					
						
							|  |  |  | var Challenge = require('./'); | 
					
						
							|  |  |  | var leChallengeDns = Challenge.create({ }); | 
					
						
							| 
									
										
										
										
											2016-10-17 15:44:49 -06:00
										 |  |  | var opts = leChallengeDns.getOptions(); | 
					
						
							| 
									
										
										
										
											2018-05-12 19:17:26 -06:00
										 |  |  | var domain = 'test.example.com'; | 
					
						
							| 
									
										
										
										
											2016-10-17 15:44:49 -06:00
										 |  |  | var challenge = 'xxx-acme-challenge-xxx'; | 
					
						
							|  |  |  | var keyAuthorization = 'xxx-acme-challenge-xxx.xxx-acme-authorization-xxx'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-12 19:17:26 -06:00
										 |  |  | Challenge.loopback = function (defaults, domain, challenge, done) { | 
					
						
							|  |  |  |   var challengeDomain = (defaults.test || '') + defaults.acmeChallengeDns + domain; | 
					
						
							|  |  |  |   console.log("dig TXT +noall +answer @8.8.8.8 '" + challengeDomain + "' # " + challenge); | 
					
						
							|  |  |  |   resolveTxtAsync(challengeDomain).then(function (x) { done(null, x); }, done); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Challenge.test = function (args, domain, challenge, keyAuthorization, done) { | 
					
						
							|  |  |  |   var me = this; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   args.test = args.test || '_test.'; | 
					
						
							|  |  |  |   //defaults.test = args.test;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   me.set(args, domain, challenge, keyAuthorization || challenge, function (err, k) { | 
					
						
							|  |  |  |     if (err) { done(err); return; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     me.loopback(/*defaults*/args, domain, challenge, function (err, arr) { | 
					
						
							|  |  |  |       if (err) { done(err); return; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (!arr.some(function (a) { | 
					
						
							|  |  |  |         return a.some(function (keyAuthDigest) { | 
					
						
							|  |  |  |           return keyAuthDigest === k; | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       })) { | 
					
						
							|  |  |  |         err = new Error("txt record '" + challenge + "' doesn't match '" + k + "'"); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       me.remove(/*defaults*/args, domain, challenge, function (_err) { | 
					
						
							|  |  |  |         if (_err) { done(_err); return; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // TODO needs to use native-dns so that specific nameservers can be used
 | 
					
						
							|  |  |  |         // (otherwise the cache will still have the old answer)
 | 
					
						
							|  |  |  |         done(err || null); | 
					
						
							|  |  |  |         /* | 
					
						
							|  |  |  |         me.loopback(defaults, domain, challenge, function (err) { | 
					
						
							|  |  |  |           if (err) { done(err); return; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           done(); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         */ | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-17 15:44:49 -06:00
										 |  |  | setTimeout(function () { | 
					
						
							|  |  |  |   leChallengeDns.test(opts, domain, challenge, keyAuthorization, function (err) { | 
					
						
							|  |  |  |     // if there's an error, there's a problem
 | 
					
						
							|  |  |  |     if (err) { throw err; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     console.log('test passed'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }, 300); |