2018-09-06 20:26:24 -07:00
'use strict' ;
exports = module . exports = {
upsert : upsert ,
get : get ,
del : del ,
2019-01-04 18:44:54 -08:00
wait : wait ,
2018-09-06 20:26:24 -07:00
verifyDnsConfig : verifyDnsConfig
} ;
var assert = require ( 'assert' ) ,
debug = require ( 'debug' ) ( 'box:dns/manual' ) ,
dns = require ( '../native-dns.js' ) ,
2019-01-04 18:44:54 -08:00
domains = require ( '../domains.js' ) ,
2018-09-06 20:26:24 -07:00
DomainsError = require ( '../domains.js' ) . DomainsError ,
sysinfo = require ( '../sysinfo.js' ) ,
2019-01-04 18:44:54 -08:00
util = require ( 'util' ) ,
waitForDns = require ( './waitfordns.js' ) ;
2018-09-06 20:26:24 -07:00
2019-01-04 18:44:54 -08:00
function upsert ( domainObject , location , type , values , callback ) {
assert . strictEqual ( typeof domainObject , 'object' ) ;
assert . strictEqual ( typeof location , 'string' ) ;
2018-09-06 20:26:24 -07:00
assert . strictEqual ( typeof type , 'string' ) ;
assert ( util . isArray ( values ) ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
2019-01-04 18:44:54 -08:00
debug ( 'upsert: %s for zone %s of type %s with values %j' , location , domainObject . zoneName , type , values ) ;
2018-09-06 20:26:24 -07:00
return callback ( null ) ;
}
2019-01-04 18:44:54 -08:00
function get ( domainObject , location , type , callback ) {
assert . strictEqual ( typeof domainObject , 'object' ) ;
assert . strictEqual ( typeof location , 'string' ) ;
2018-09-06 20:26:24 -07:00
assert . strictEqual ( typeof type , 'string' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
callback ( null , [ ] ) ; // returning ip confuses apptask into thinking the entry already exists
}
2019-01-04 18:44:54 -08:00
function del ( domainObject , location , type , values , callback ) {
assert . strictEqual ( typeof domainObject , 'object' ) ;
assert . strictEqual ( typeof location , 'string' ) ;
2018-09-06 20:26:24 -07:00
assert . strictEqual ( typeof type , 'string' ) ;
assert ( util . isArray ( values ) ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
return callback ( ) ;
}
2019-01-04 18:44:54 -08:00
function wait ( domainObject , location , type , value , options , callback ) {
assert . strictEqual ( typeof domainObject , 'object' ) ;
assert . strictEqual ( typeof location , 'string' ) ;
assert . strictEqual ( typeof type , 'string' ) ;
assert . strictEqual ( typeof value , 'string' ) ;
assert ( options && typeof options === 'object' ) ; // { interval: 5000, times: 50000 }
assert . strictEqual ( typeof callback , 'function' ) ;
const fqdn = domains . fqdn ( location , domainObject ) ;
waitForDns ( fqdn , domainObject . zoneName , type , value , options , callback ) ;
}
function verifyDnsConfig ( domainObject , callback ) {
assert . strictEqual ( typeof domainObject , 'object' ) ;
2018-09-06 20:26:24 -07:00
assert . strictEqual ( typeof callback , 'function' ) ;
2019-01-04 18:44:54 -08:00
const zoneName = domainObject . zoneName ;
2018-09-06 20:26:24 -07:00
// Very basic check if the nameservers can be fetched
dns . resolve ( zoneName , 'NS' , { timeout : 5000 } , function ( error , nameservers ) {
if ( error && error . code === 'ENOTFOUND' ) return callback ( new DomainsError ( DomainsError . BAD _FIELD , 'Unable to resolve nameservers for this domain' ) ) ;
if ( error || ! nameservers ) return callback ( new DomainsError ( DomainsError . BAD _FIELD , error ? error . message : 'Unable to get nameservers' ) ) ;
2019-01-04 18:44:54 -08:00
const location = 'cloudrontestdns' ;
const fqdn = domains . fqdn ( location , domainObject ) ;
2018-09-06 20:26:24 -07:00
dns . resolve ( fqdn , 'A' , { server : '127.0.0.1' , timeout : 5000 } , function ( error , result ) {
if ( error && error . code === 'ENOTFOUND' ) return callback ( new DomainsError ( DomainsError . BAD _FIELD , ` Unable to resolve ${ fqdn } ` ) ) ;
if ( error || ! result ) return callback ( new DomainsError ( DomainsError . BAD _FIELD , error ? error . message : ` Unable to resolve ${ fqdn } ` ) ) ;
sysinfo . getPublicIp ( function ( error , ip ) {
if ( error ) return callback ( new DomainsError ( DomainsError . EXTERNAL _ERROR , ` Failed to detect IP of this server: ${ error . message } ` ) ) ;
if ( result . length !== 1 || ip !== result [ 0 ] ) return callback ( new DomainsError ( DomainsError . EXTERNAL _ERROR , ` Domain resolves to ${ JSON . stringify ( result ) } instead of ${ ip } ` ) ) ;
2018-09-11 21:24:04 -07:00
callback ( null , { } ) ;
2018-09-06 20:26:24 -07:00
} ) ;
} ) ;
} ) ;
}