forked from coolaj86/goldilocks.js
		
	
		
			
				
	
	
		
			160 lines
		
	
	
		
			4.9 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			160 lines
		
	
	
		
			4.9 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| 'use strict';
 | |
| 
 | |
| module.exports.create = function (deps, conf) {
 | |
|   function dnsType(addr) {
 | |
|     if (/^\d+\.\d+\.\d+\.\d+$/.test(addr)) {
 | |
|       return 'A';
 | |
|     }
 | |
|     if (-1 !== addr.indexOf(':') && /^[a-f:\.\d]+$/i.test(addr)) {
 | |
|       return 'AAAA';
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   var tldCache = {};
 | |
|   async function getTlds(provider) {
 | |
|     async function updateCache() {
 | |
|       var reqObj = {
 | |
|         url: deps.OAUTH3.url.normalize(provider)+'/api/com.daplie.domains/prices'
 | |
|       , method: 'GET'
 | |
|       , json: true
 | |
|       };
 | |
| 
 | |
|       var resp = await deps.OAUTH3.request(reqObj);
 | |
|       var tldObj = {};
 | |
|       resp.data.forEach(function (tldInfo) {
 | |
|         if (tldInfo.enabled) {
 | |
|           tldObj[tldInfo.com] = true;
 | |
|         }
 | |
|       });
 | |
| 
 | |
|       tldCache[provider] = {
 | |
|         time: Date.now()
 | |
|       , tlds: tldObj
 | |
|       };
 | |
|       return tldObj;
 | |
|     }
 | |
| 
 | |
|     // If we've never cached the results we need to return the promise that will fetch the recult,
 | |
|     // otherwise we can return the cached value. If the cached value has "expired", we can still
 | |
|     // return the cached value we just want to update the cache in parellel (making sure we only
 | |
|     // update once).
 | |
|     if (!tldCache[provider]) {
 | |
|       return updateCache();
 | |
|     }
 | |
|     if (!tldCache[provider].updating && Date.now() - tldCache[provider].time > 24*60*60*1000) {
 | |
|       tldCache[provider].updating = true;
 | |
|       updateCache();
 | |
|     }
 | |
| 
 | |
|     return tldCache[provider].tlds;
 | |
|   }
 | |
| 
 | |
|   async function splitDomains(provider, domains) {
 | |
|     var tlds = await getTlds(provider);
 | |
|     return domains.map(function (domain) {
 | |
|       var split = domain.split('.');
 | |
|       var tldSegCnt = tlds[split.slice(-2).join('.')] ? 2 : 1;
 | |
| 
 | |
|       // Currently assuming that the sld can't contain dots, and that the tld can have at
 | |
|       // most one dot. Not 100% sure this is a valid assumption, but exceptions should be
 | |
|       // rare even if the assumption isn't valid.
 | |
|       return {
 | |
|         tld: split.slice(-tldSegCnt).join('.')
 | |
|       , sld: split.slice(-tldSegCnt-1, 1)
 | |
|       , sub: split.slice(0, -tldSegCnt-1)
 | |
|       };
 | |
|     });
 | |
|   }
 | |
| 
 | |
|   async function setDeviceAddress(session, addr, domains) {
 | |
|     var directives = await deps.OAUTH3.discover(session.token.aud);
 | |
| 
 | |
|     // Set the address of the device to our public address.
 | |
|     await deps.request({
 | |
|       url: deps.OAUTH3.url.normalize(directives.api)+'/api/com.daplie.domains/acl/devices/' + conf.device.hostname
 | |
|     , method: 'POST'
 | |
|     , headers: {
 | |
|         'Authorization': 'Bearer ' + session.refresh_token
 | |
|       , 'Accept': 'application/json; charset=utf-8'
 | |
|       }
 | |
|     , json: {
 | |
|         addresses: [
 | |
|           { value: addr, type:  dnsType(addr) }
 | |
|         ]
 | |
|       }
 | |
|     });
 | |
| 
 | |
|     // Then update all of the records attached to our hostname, first removing the old records
 | |
|     // to remove the reference to the old address, then creating new records for the same domains
 | |
|     // using our new address.
 | |
|     var allDns = await deps.OAUTH3.api(directives.api, {session: session, api: 'dns.list'});
 | |
|     var ourDns = allDns.filter(function (record) {
 | |
|       if (record.device !== conf.device.hostname) {
 | |
|         return false;
 | |
|       }
 | |
|       if ([ 'A', 'AAAA' ].indexOf(record.type) < 0) {
 | |
|         return false;
 | |
|       }
 | |
|       return domains.indexOf(record.host) !== -1;
 | |
|     });
 | |
| 
 | |
|     var oldDomains = ourDns.filter(function (record) {
 | |
|       return record.value !== addr;
 | |
|     }).map(function (record) {
 | |
|       return record.host;
 | |
|     });
 | |
|     var oldDns = await splitDomains(directives.api, oldDomains);
 | |
|     var common = {
 | |
|       api: 'devices.detach'
 | |
|     , session: session
 | |
|     , device: conf.device.hostname
 | |
|     };
 | |
|     await deps.PromiseA.all(oldDns.map(function (record) {
 | |
|       return deps.OAUTH3.api(directives.api, Object.assign({}, common, record));
 | |
|     }));
 | |
| 
 | |
|     var newDns = await splitDomains(directives.api, domains);
 | |
|     common = {
 | |
|       api: 'devices.attach'
 | |
|     , session: session
 | |
|     , device: conf.device.hostname
 | |
|     , ip: addr
 | |
|     , ttl: 300
 | |
|     };
 | |
|     await deps.PromiseA.all(newDns.map(function (record) {
 | |
|       return deps.OAUTH3.api(directives.api, Object.assign({}, common, record));
 | |
|     }));
 | |
|   }
 | |
| 
 | |
|   async function getDeviceAddresses(session) {
 | |
|     var directives = await deps.OAUTH3.discover(session.token.aud);
 | |
| 
 | |
|     var result = await deps.request({
 | |
|       url: deps.OAUTH3.url.normalize(directives.api)+'/api/org.oauth3.dns/acl/devices'
 | |
|     , method: 'GET'
 | |
|     , headers: {
 | |
|         'Authorization': 'Bearer ' + session.refresh_token
 | |
|       , 'Accept': 'application/json; charset=utf-8'
 | |
|       }
 | |
|     , json: true
 | |
|     });
 | |
| 
 | |
|     if (!result.body) {
 | |
|       throw new Error('No response body in request for device addresses');
 | |
|     }
 | |
|     if (result.body.error) {
 | |
|       throw Object.assign(new Error('error getting device list'), result.body.error);
 | |
|     }
 | |
| 
 | |
|     var dev = result.body.devices.filter(function (dev) {
 | |
|       return dev.name === conf.device.hostname;
 | |
|     })[0];
 | |
|     return (dev || {}).addresses || [];
 | |
|   }
 | |
| 
 | |
|   return {
 | |
|     getDeviceAddresses: getDeviceAddresses
 | |
|   , setDeviceAddress:   setDeviceAddress
 | |
|   };
 | |
| };
 |