2019-01-16 18:05:42 +02:00
'use strict' ;
exports = module . exports = {
2019-02-08 11:11:49 +01:00
removePrivateFields : removePrivateFields ,
2019-02-09 19:08:15 +01:00
injectPrivateFields : injectPrivateFields ,
2019-01-16 18:05:42 +02:00
upsert : upsert ,
get : get ,
del : del ,
2019-01-22 12:12:46 +01:00
verifyDnsConfig : verifyDnsConfig ,
wait : wait
2019-01-16 18:05:42 +02:00
} ;
var assert = require ( 'assert' ) ,
2019-10-23 10:02:04 -07:00
BoxError = require ( '../boxerror.js' ) ,
2020-05-14 23:01:44 +02:00
constants = require ( '../constants.js' ) ,
2019-01-16 18:05:42 +02:00
debug = require ( 'debug' ) ( 'box:dns/namecheap' ) ,
dns = require ( '../native-dns.js' ) ,
2019-01-21 14:36:21 +01:00
domains = require ( '../domains.js' ) ,
2021-04-13 21:36:05 -07:00
querystring = require ( 'querystring' ) ,
2019-05-16 17:00:17 +02:00
safe = require ( 'safetydance' ) ,
2019-05-16 16:53:36 +02:00
superagent = require ( 'superagent' ) ,
2019-05-16 17:00:17 +02:00
sysinfo = require ( '../sysinfo.js' ) ,
2019-05-16 16:53:36 +02:00
waitForDns = require ( './waitfordns.js' ) ,
xml2js = require ( 'xml2js' ) ;
2019-01-16 18:05:42 +02:00
2019-05-16 16:53:36 +02:00
const ENDPOINT = 'https://api.namecheap.com/xml.response' ;
2019-01-16 18:05:42 +02:00
2019-02-08 11:11:49 +01:00
function removePrivateFields ( domainObject ) {
2020-05-14 23:01:44 +02:00
domainObject . config . token = constants . SECRET _PLACEHOLDER ;
2019-02-08 11:11:49 +01:00
return domainObject ;
}
2019-02-09 19:08:15 +01:00
function injectPrivateFields ( newConfig , currentConfig ) {
2020-05-14 23:01:44 +02:00
if ( newConfig . token === constants . SECRET _PLACEHOLDER ) newConfig . token = currentConfig . token ;
2019-02-09 19:08:15 +01:00
}
2019-05-16 16:53:36 +02:00
function getQuery ( dnsConfig , callback ) {
2019-01-21 14:36:21 +01:00
assert . strictEqual ( typeof dnsConfig , 'object' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
2019-10-29 15:46:33 -07:00
sysinfo . getServerIp ( function ( error , ip ) {
2019-10-23 10:02:04 -07:00
if ( error ) return callback ( error ) ;
2019-01-21 14:36:21 +01:00
2019-05-16 16:53:36 +02:00
callback ( null , {
ApiUser : dnsConfig . username ,
ApiKey : dnsConfig . token ,
UserName : dnsConfig . username ,
ClientIp : ip
} ) ;
2019-01-21 14:36:21 +01:00
} ) ;
}
2021-04-13 15:10:24 -07:00
function getZone ( dnsConfig , zoneName , callback ) {
2019-01-16 18:05:42 +02:00
assert . strictEqual ( typeof dnsConfig , 'object' ) ;
assert . strictEqual ( typeof zoneName , 'string' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
2019-05-16 16:53:36 +02:00
getQuery ( dnsConfig , function ( error , query ) {
2019-01-21 14:36:21 +01:00
if ( error ) return callback ( error ) ;
2019-01-16 18:05:42 +02:00
2019-05-16 16:53:36 +02:00
query . Command = 'namecheap.domains.dns.getHosts' ;
query . SLD = zoneName . split ( '.' ) [ 0 ] ;
query . TLD = zoneName . split ( '.' ) [ 1 ] ;
2019-01-16 18:05:42 +02:00
2019-05-16 16:53:36 +02:00
superagent . get ( ENDPOINT ) . query ( query ) . end ( function ( error , result ) {
2019-10-23 10:02:04 -07:00
if ( error ) return callback ( new BoxError ( BoxError . EXTERNAL _ERROR , error ) ) ;
2019-01-16 18:05:42 +02:00
2019-05-16 16:53:36 +02:00
var parser = new xml2js . Parser ( ) ;
parser . parseString ( result . text , function ( error , result ) {
2019-10-23 10:02:04 -07:00
if ( error ) return callback ( new BoxError ( BoxError . EXTERNAL _ERROR , error ) ) ;
2019-05-16 16:53:36 +02:00
var tmp = result . ApiResponse ;
2019-09-23 20:00:49 +02:00
if ( tmp [ '$' ] . Status !== 'OK' ) {
var errorMessage = safe . query ( tmp , 'Errors[0].Error[0]._' , 'Invalid response' ) ;
2019-10-23 10:02:04 -07:00
if ( errorMessage === 'API Key is invalid or API access has not been enabled' ) return callback ( new BoxError ( BoxError . ACCESS _DENIED , errorMessage ) ) ;
2019-09-23 20:00:49 +02:00
2019-10-23 10:02:04 -07:00
return callback ( new BoxError ( BoxError . EXTERNAL _ERROR , errorMessage ) ) ;
2019-09-23 20:00:49 +02:00
}
2020-09-16 16:44:24 -07:00
const host = safe . query ( tmp , 'CommandResponse[0].DomainDNSGetHostsResult[0].host' ) ;
if ( ! host ) return callback ( new BoxError ( BoxError . EXTERNAL _ERROR , ` Invalid response: ${ JSON . stringify ( tmp ) } ` ) ) ;
if ( ! Array . isArray ( host ) ) return callback ( new BoxError ( BoxError . EXTERNAL _ERROR , ` host is not an array: ${ JSON . stringify ( tmp ) } ` ) ) ;
2019-05-16 16:53:36 +02:00
2020-09-16 16:44:24 -07:00
const hosts = host . map ( h => h [ '$' ] ) ;
2019-05-16 16:53:36 +02:00
callback ( null , hosts ) ;
} ) ;
2019-01-21 14:36:21 +01:00
} ) ;
2019-01-16 18:05:42 +02:00
} ) ;
}
2021-04-13 15:10:24 -07:00
function setZone ( dnsConfig , zoneName , hosts , callback ) {
2019-02-08 20:21:16 -08:00
assert . strictEqual ( typeof dnsConfig , 'object' ) ;
assert . strictEqual ( typeof zoneName , 'string' ) ;
assert ( Array . isArray ( hosts ) ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
2019-05-16 16:53:36 +02:00
getQuery ( dnsConfig , function ( error , query ) {
2019-02-08 20:21:16 -08:00
if ( error ) return callback ( error ) ;
2019-05-16 16:53:36 +02:00
query . Command = 'namecheap.domains.dns.setHosts' ;
query . SLD = zoneName . split ( '.' ) [ 0 ] ;
query . TLD = zoneName . split ( '.' ) [ 1 ] ;
// Map to query params https://www.namecheap.com/support/api/methods/domains-dns/set-hosts.aspx
hosts . forEach ( function ( host , i ) {
var n = i + 1 ; // api starts with 1 not 0
query [ 'TTL' + n ] = '300' ; // keep it low
2019-05-16 17:47:27 +02:00
query [ 'HostName' + n ] = host . HostName || host . Name ;
query [ 'RecordType' + n ] = host . RecordType || host . Type ;
2019-05-16 16:53:36 +02:00
query [ 'Address' + n ] = host . Address ;
if ( host . Type === 'MX' ) {
query [ 'EmailType' + n ] = 'MX' ;
if ( host . MXPref ) query [ 'MXPref' + n ] = host . MXPref ;
}
} ) ;
2021-04-13 21:36:05 -07:00
// namecheap recommends sending as POSTDATA with > 10 records
const qs = querystring . stringify ( query ) ;
superagent . post ( ENDPOINT ) . set ( 'Content-Type' , 'application/x-www-form-urlencoded' ) . send ( qs ) . end ( function ( error , result ) {
2019-10-23 10:02:04 -07:00
if ( error ) return callback ( new BoxError ( BoxError . EXTERNAL _ERROR , error ) ) ;
2019-05-16 16:53:36 +02:00
var parser = new xml2js . Parser ( ) ;
parser . parseString ( result . text , function ( error , result ) {
2019-10-23 10:02:04 -07:00
if ( error ) return callback ( new BoxError ( BoxError . EXTERNAL _ERROR , error ) ) ;
2019-02-08 20:21:16 -08:00
2019-05-16 16:53:36 +02:00
var tmp = result . ApiResponse ;
2019-09-23 20:00:49 +02:00
if ( tmp [ '$' ] . Status !== 'OK' ) {
var errorMessage = safe . query ( tmp , 'Errors[0].Error[0]._' , 'Invalid response' ) ;
2019-10-23 10:02:04 -07:00
if ( errorMessage === 'API Key is invalid or API access has not been enabled' ) return callback ( new BoxError ( BoxError . ACCESS _DENIED , errorMessage ) ) ;
2019-09-23 20:00:49 +02:00
2019-10-23 10:02:04 -07:00
return callback ( new BoxError ( BoxError . EXTERNAL _ERROR , errorMessage ) ) ;
2019-09-23 20:00:49 +02:00
}
2019-10-23 10:02:04 -07:00
if ( ! tmp . CommandResponse [ 0 ] ) return callback ( new BoxError ( BoxError . EXTERNAL _ERROR , 'Invalid response' ) ) ;
if ( ! tmp . CommandResponse [ 0 ] . DomainDNSSetHostsResult [ 0 ] ) return callback ( new BoxError ( BoxError . EXTERNAL _ERROR , 'Invalid response' ) ) ;
if ( tmp . CommandResponse [ 0 ] . DomainDNSSetHostsResult [ 0 ] [ '$' ] . IsSuccess !== 'true' ) return callback ( new BoxError ( BoxError . EXTERNAL _ERROR , 'Invalid response' ) ) ;
2019-05-16 16:53:36 +02:00
callback ( null ) ;
} ) ;
2019-02-08 20:21:16 -08:00
} ) ;
2019-01-16 18:05:42 +02:00
} ) ;
}
2019-01-21 14:36:21 +01:00
function upsert ( domainObject , subdomain , type , values , callback ) {
assert . strictEqual ( typeof domainObject , 'object' ) ;
2019-01-16 18:05:42 +02:00
assert . strictEqual ( typeof subdomain , 'string' ) ;
assert . strictEqual ( typeof type , 'string' ) ;
2021-04-13 15:10:24 -07:00
assert ( Array . isArray ( values ) ) ;
2019-01-16 18:05:42 +02:00
assert . strictEqual ( typeof callback , 'function' ) ;
2019-01-21 14:36:21 +01:00
const dnsConfig = domainObject . config ;
const zoneName = domainObject . zoneName ;
subdomain = domains . getName ( domainObject , subdomain , type ) || '@' ;
2019-01-16 18:05:42 +02:00
debug ( 'upsert: %s for zone %s of type %s with values %j' , subdomain , zoneName , type , values ) ;
2021-04-13 15:10:24 -07:00
getZone ( dnsConfig , zoneName , function ( error , result ) {
2019-01-21 14:36:21 +01:00
if ( error ) return callback ( error ) ;
2019-01-16 18:05:42 +02:00
// Array to keep track of records that need to be inserted
let toInsert = [ ] ;
2021-04-13 15:10:24 -07:00
for ( let i = 0 ; i < values . length ; i ++ ) {
2019-01-16 18:05:42 +02:00
let curValue = values [ i ] ;
let wasUpdate = false ;
2021-04-13 15:10:24 -07:00
for ( let j = 0 ; j < result . length ; j ++ ) {
2019-01-16 18:05:42 +02:00
let curHost = result [ j ] ;
if ( curHost . Type === type && curHost . Name === subdomain ) {
// Updating an already existing host
wasUpdate = true ;
2019-01-21 14:36:21 +01:00
if ( type === 'MX' ) {
2019-01-16 18:05:42 +02:00
curHost . MXPref = curValue . split ( ' ' ) [ 0 ] ;
curHost . Address = curValue . split ( ' ' ) [ 1 ] ;
} else {
curHost . Address = curValue ;
}
}
}
// We don't have this host at all yet, let's push to toInsert array
if ( ! wasUpdate ) {
let newRecord = {
RecordType : type ,
HostName : subdomain ,
Address : curValue
} ;
// Special case for MX records
2019-01-21 14:36:21 +01:00
if ( type === 'MX' ) {
2019-01-16 18:05:42 +02:00
newRecord . MXPref = curValue . split ( ' ' ) [ 0 ] ;
newRecord . Address = curValue . split ( ' ' ) [ 1 ] ;
}
toInsert . push ( newRecord ) ;
}
}
2021-04-13 15:10:24 -07:00
const hosts = result . concat ( toInsert ) ;
2019-01-16 18:05:42 +02:00
2021-04-13 15:10:24 -07:00
setZone ( dnsConfig , zoneName , hosts , callback ) ;
2019-01-16 18:05:42 +02:00
} ) ;
}
2019-01-21 14:36:21 +01:00
function get ( domainObject , subdomain , type , callback ) {
assert . strictEqual ( typeof domainObject , 'object' ) ;
2019-01-16 18:05:42 +02:00
assert . strictEqual ( typeof subdomain , 'string' ) ;
assert . strictEqual ( typeof type , 'string' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
2019-01-21 14:36:21 +01:00
const dnsConfig = domainObject . config ;
const zoneName = domainObject . zoneName ;
subdomain = domains . getName ( domainObject , subdomain , type ) || '@' ;
2019-01-16 18:05:42 +02:00
2021-04-13 15:10:24 -07:00
getZone ( dnsConfig , zoneName , function ( error , result ) {
2019-01-16 18:05:42 +02:00
if ( error ) return callback ( error ) ;
// We need to filter hosts to ones with this subdomain and type
2021-04-13 15:10:24 -07:00
const actualHosts = result . filter ( ( host ) => host . Type === type && host . Name === subdomain ) ;
2019-01-21 14:36:21 +01:00
2019-01-16 18:05:42 +02:00
// We only return the value string
2021-04-13 15:10:24 -07:00
const tmp = actualHosts . map ( function ( record ) { return record . Address ; } ) ;
2019-01-16 18:05:42 +02:00
2021-04-13 15:10:24 -07:00
debug ( ` get: subdomain: ${ subdomain } type: ${ type } value: ${ JSON . stringify ( tmp ) } ` ) ;
2019-01-16 18:05:42 +02:00
return callback ( null , tmp ) ;
} ) ;
}
2019-01-21 14:36:21 +01:00
function del ( domainObject , subdomain , type , values , callback ) {
assert . strictEqual ( typeof domainObject , 'object' ) ;
2019-01-16 18:05:42 +02:00
assert . strictEqual ( typeof subdomain , 'string' ) ;
assert . strictEqual ( typeof type , 'string' ) ;
2021-04-13 15:10:24 -07:00
assert ( Array . isArray ( values ) ) ;
2019-01-16 18:05:42 +02:00
assert . strictEqual ( typeof callback , 'function' ) ;
2019-01-21 14:36:21 +01:00
const dnsConfig = domainObject . config ;
const zoneName = domainObject . zoneName ;
subdomain = domains . getName ( domainObject , subdomain , type ) || '@' ;
2019-01-16 18:05:42 +02:00
debug ( 'del: %s for zone %s of type %s with values %j' , subdomain , zoneName , type , values ) ;
2021-04-13 15:10:24 -07:00
getZone ( dnsConfig , zoneName , function ( error , result ) {
2019-01-16 18:05:42 +02:00
if ( error ) return callback ( error ) ;
2019-01-21 14:36:21 +01:00
if ( result . length === 0 ) return callback ( ) ;
2021-04-13 22:27:38 -07:00
const originalLength = result . length ;
2019-01-16 18:05:42 +02:00
2021-04-13 22:27:38 -07:00
for ( let i = 0 ; i < values . length ; i ++ ) {
2019-01-16 18:05:42 +02:00
let curValue = values [ i ] ;
2021-04-13 22:27:38 -07:00
result = result . filter ( curHost => curHost . Type !== type || curHost . Name !== subdomain || curHost . Address !== curValue ) ;
2019-01-16 18:05:42 +02:00
}
2021-04-13 22:27:38 -07:00
if ( result . length !== originalLength ) return setZone ( dnsConfig , zoneName , result , callback ) ;
2019-01-21 14:36:21 +01:00
callback ( ) ;
2019-01-16 18:05:42 +02:00
} ) ;
}
2019-01-21 14:36:21 +01:00
function verifyDnsConfig ( domainObject , callback ) {
assert . strictEqual ( typeof domainObject , 'object' ) ;
2019-01-16 18:05:42 +02:00
assert . strictEqual ( typeof callback , 'function' ) ;
2019-01-21 14:36:21 +01:00
const dnsConfig = domainObject . config ;
const zoneName = domainObject . zoneName ;
const ip = '127.0.0.1' ;
2019-10-23 10:02:04 -07:00
if ( ! dnsConfig . username || typeof dnsConfig . username !== 'string' ) return callback ( new BoxError ( BoxError . BAD _FIELD , 'username must be a non-empty string' , { field : 'username' } ) ) ;
if ( ! dnsConfig . token || typeof dnsConfig . token !== 'string' ) return callback ( new BoxError ( BoxError . BAD _FIELD , 'token must be a non-empty string' , { field : 'token' } ) ) ;
2019-01-16 18:05:42 +02:00
2019-02-08 19:09:28 -08:00
let credentials = {
2019-01-16 18:05:42 +02:00
username : dnsConfig . username ,
2019-02-08 20:22:42 -08:00
token : dnsConfig . token
2019-01-16 18:05:42 +02:00
} ;
2019-05-20 22:21:07 +02:00
if ( process . env . BOX _ENV === 'test' ) return callback ( null , credentials ) ; // this shouldn't be here
2019-01-16 18:05:42 +02:00
dns . resolve ( zoneName , 'NS' , { timeout : 5000 } , function ( error , nameservers ) {
2019-10-23 10:02:04 -07:00
if ( error && error . code === 'ENOTFOUND' ) return callback ( new BoxError ( BoxError . BAD _FIELD , 'Unable to resolve nameservers for this domain' , { field : 'nameservers' } ) ) ;
if ( error || ! nameservers ) return callback ( new BoxError ( BoxError . BAD _FIELD , error ? error . message : 'Unable to get nameservers' , { field : 'nameservers' } ) ) ;
2019-01-16 18:05:42 +02:00
2019-01-22 11:56:56 +01:00
if ( nameservers . some ( function ( n ) { return n . toLowerCase ( ) . indexOf ( '.registrar-servers.com' ) === - 1 ; } ) ) {
2019-01-16 18:05:42 +02:00
debug ( 'verifyDnsConfig: %j does not contains NC NS' , nameservers ) ;
2019-10-23 10:02:04 -07:00
return callback ( new BoxError ( BoxError . BAD _FIELD , 'Domain nameservers are not set to NameCheap' , { field : 'nameservers' } ) ) ;
2019-01-16 18:05:42 +02:00
}
const testSubdomain = 'cloudrontestdns' ;
2019-01-21 14:36:21 +01:00
upsert ( domainObject , testSubdomain , 'A' , [ ip ] , function ( error , changeId ) {
2019-01-16 18:05:42 +02:00
if ( error ) return callback ( error ) ;
debug ( 'verifyDnsConfig: Test A record added with change id %s' , changeId ) ;
2019-01-22 12:04:17 +01:00
del ( domainObject , testSubdomain , 'A' , [ ip ] , function ( error ) {
2019-01-16 18:05:42 +02:00
if ( error ) return callback ( error ) ;
debug ( 'verifyDnsConfig: Test A record removed again' ) ;
2019-02-08 19:09:28 -08:00
callback ( null , credentials ) ;
2019-01-16 18:05:42 +02:00
} ) ;
} ) ;
} ) ;
}
2019-01-22 12:12:46 +01:00
function wait ( domainObject , subdomain , type , value , options , callback ) {
assert . strictEqual ( typeof domainObject , 'object' ) ;
assert . strictEqual ( typeof subdomain , '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 ( subdomain , domainObject ) ;
waitForDns ( fqdn , domainObject . zoneName , type , value , options , callback ) ;
}