2026-02-14 09:53:14 +01:00
import assert from 'node:assert' ;
import BoxError from '../boxerror.js' ;
import constants from '../constants.js' ;
import debugModule from 'debug' ;
2026-02-14 15:43:24 +01:00
import dig from '../dig.js' ;
import dns from '../dns.js' ;
2026-02-14 09:53:14 +01:00
import promiseRetry from '../promise-retry.js' ;
import safe from 'safetydance' ;
import superagent from '@cloudron/superagent' ;
import waitForDns from './waitfordns.js' ;
const debug = debugModule ( 'box:dns/hetznercloud' ) ;
2025-10-10 11:18:04 +02:00
// https://docs.hetzner.cloud/reference/cloud
const ENDPOINT = 'https://api.hetzner.cloud/v1' ;
function formatError ( response ) {
return ` Hetzner DNS error ${ response . status } ${ response . text } ` ;
}
function removePrivateFields ( domainObject ) {
delete domainObject . config . token ;
return domainObject ;
}
function injectPrivateFields ( newConfig , currentConfig ) {
if ( ! Object . hasOwn ( newConfig , 'token' ) ) newConfig . token = currentConfig . token ;
}
2026-02-14 16:34:34 +01:00
async function wait ( domainObject , subdomain , type , value , options ) {
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 }
const fqdn = dns . fqdn ( subdomain , domainObject . domain ) ;
await waitForDns ( fqdn , domainObject . zoneName , type , value , options ) ;
}
2025-10-10 11:18:04 +02:00
async function getZone ( domainConfig , zoneName ) {
assert . strictEqual ( typeof domainConfig , 'object' ) ;
assert . strictEqual ( typeof zoneName , 'string' ) ;
const [ error , response ] = await safe ( superagent . get ( ` ${ ENDPOINT } /zones ` )
. set ( 'Authorization' , ` Bearer ${ domainConfig . token } ` )
2026-01-29 15:35:29 +01:00
. query ( { name : zoneName } )
2025-10-10 11:18:04 +02:00
. timeout ( 30 * 1000 )
. retry ( 5 )
. ok ( ( ) => true ) ) ;
if ( error ) throw new BoxError ( BoxError . NETWORK _ERROR , error ) ;
if ( response . status === 401 || response . status === 403 ) throw new BoxError ( BoxError . ACCESS _DENIED , formatError ( response ) ) ;
if ( response . status !== 200 ) throw new BoxError ( BoxError . EXTERNAL _ERROR , formatError ( response ) ) ;
if ( ! Array . isArray ( response . body . zones ) ) throw new BoxError ( BoxError . EXTERNAL _ERROR , formatError ( response ) ) ;
const zone = response . body . zones . find ( z => z . name === zoneName ) ;
if ( ! zone ) throw new BoxError ( BoxError . NOT _FOUND , formatError ( response ) ) ;
return zone ;
}
async function getRecords ( domainConfig , zone , name , type ) {
assert . strictEqual ( typeof domainConfig , 'object' ) ;
assert . strictEqual ( typeof zone , 'object' ) ;
assert . strictEqual ( typeof name , 'string' ) ;
assert . strictEqual ( typeof type , 'string' ) ;
debug ( ` getRecords: getting dns records of ${ zone . name } with ${ name } and type ${ type } ` ) ;
const [ error , response ] = await safe ( superagent . get ( ` ${ ENDPOINT } /zones/ ${ zone . id } /rrsets/ ${ name } / ${ type . toUpperCase ( ) } ` )
. set ( 'Authorization' , ` Bearer ${ domainConfig . token } ` )
. timeout ( 30 * 1000 )
. retry ( 5 )
. ok ( ( ) => true ) ) ;
if ( error ) throw new BoxError ( BoxError . NETWORK _ERROR , error ) ;
if ( response . status === 404 ) return [ ] ;
if ( response . status === 401 || response . status === 403 ) throw new BoxError ( BoxError . ACCESS _DENIED , formatError ( response ) ) ;
if ( response . status !== 200 ) throw new BoxError ( BoxError . EXTERNAL _ERROR , formatError ( response ) ) ;
return response . body . rrset . records ;
}
async function waitForAction ( domainConfig , id ) {
assert . strictEqual ( typeof domainConfig , 'object' ) ;
assert . strictEqual ( typeof id , 'number' ) ;
await promiseRetry ( { times : 100 , interval : 1000 , debug } , async ( ) => {
const [ error , response ] = await safe ( superagent . get ( ` ${ ENDPOINT } /actions/ ${ id } ` )
. set ( 'Authorization' , ` Bearer ${ domainConfig . token } ` )
. timeout ( 30 * 1000 )
. retry ( 5 )
. ok ( ( ) => true ) ) ;
if ( error ) throw new BoxError ( BoxError . NETWORK _ERROR , error ) ;
if ( response . status === 404 ) return [ ] ;
if ( response . status === 401 || response . status === 403 ) throw new BoxError ( BoxError . ACCESS _DENIED , formatError ( response ) ) ;
if ( response . status !== 200 ) throw new BoxError ( BoxError . EXTERNAL _ERROR , formatError ( response ) ) ;
if ( response . body . action . status !== 'success' ) throw new BoxError ( BoxError . TRY _AGAIN , 'action not done yet' ) ;
} ) ;
}
2026-02-14 16:34:34 +01:00
async function del ( domainObject , location , type , values ) {
assert . strictEqual ( typeof domainObject , 'object' ) ;
assert . strictEqual ( typeof location , 'string' ) ;
assert . strictEqual ( typeof type , 'string' ) ;
assert ( Array . isArray ( values ) ) ;
const domainConfig = domainObject . config ,
zoneName = domainObject . zoneName ,
name = dns . getName ( domainObject , location , type ) || '@' ;
const zone = await getZone ( domainConfig , zoneName ) ;
const records = await getRecords ( domainConfig , zone , name , type ) ;
if ( records . length === 0 ) return ;
const [ error , response ] = await safe ( superagent . del ( ` ${ ENDPOINT } /zones/ ${ zone . id } /rrsets/ ${ name } / ${ type } ` )
. set ( 'Authorization' , ` Bearer ${ domainConfig . token } ` )
. timeout ( 30 * 1000 )
. retry ( 5 )
. ok ( ( ) => true ) ) ;
if ( error ) throw new BoxError ( BoxError . NETWORK _ERROR , error ) ;
if ( response . status === 404 ) return ;
if ( response . status === 403 || response . status === 401 ) throw new BoxError ( BoxError . ACCESS _DENIED , formatError ( response ) ) ;
if ( response . status !== 201 ) throw new BoxError ( BoxError . EXTERNAL _ERROR , formatError ( response ) ) ;
await waitForAction ( domainConfig , response . body . action . id ) ;
}
2025-10-10 11:18:04 +02:00
async function upsert ( domainObject , location , type , values ) {
assert . strictEqual ( typeof domainObject , 'object' ) ;
assert . strictEqual ( typeof location , 'string' ) ;
assert . strictEqual ( typeof type , 'string' ) ;
assert ( Array . isArray ( values ) ) ;
const domainConfig = domainObject . config ,
zoneName = domainObject . zoneName ,
name = dns . getName ( domainObject , location , type ) || '@' ;
debug ( ` upsert: ${ name } for zone ${ zoneName } of type ${ type } with values ${ JSON . stringify ( values ) } ` ) ;
const zone = await getZone ( domainConfig , zoneName ) ;
const records = await getRecords ( domainConfig , zone , name , type ) ;
// update means first delete then recreate
if ( records . length ) await del ( domainObject , location , type , values ) ;
const data = {
name ,
type ,
ttl : 60 ,
records : values . map ( v => { return { value : v , comment : 'managed by cloudron' } ; } ) ,
labels : {
managedBy : 'cloudron'
}
} ;
const [ error , response ] = await safe ( superagent . post ( ` ${ ENDPOINT } /zones/ ${ zone . id } /rrsets ` )
. set ( 'Authorization' , ` Bearer ${ domainConfig . token } ` )
. send ( data )
. timeout ( 30 * 1000 )
. retry ( 5 )
. ok ( ( ) => true ) ) ;
if ( error ) throw new BoxError ( BoxError . NETWORK _ERROR , error ) ;
if ( response . status === 403 || response . status === 401 ) throw new BoxError ( BoxError . ACCESS _DENIED , formatError ( response ) ) ;
if ( response . status === 422 ) throw new BoxError ( BoxError . BAD _FIELD , response . body . message ) ;
if ( response . status !== 201 ) throw new BoxError ( BoxError . EXTERNAL _ERROR , formatError ( response ) ) ;
await waitForAction ( domainConfig , response . body . action . id ) ;
}
async function get ( domainObject , location , type ) {
assert . strictEqual ( typeof domainObject , 'object' ) ;
assert . strictEqual ( typeof location , 'string' ) ;
assert . strictEqual ( typeof type , 'string' ) ;
const domainConfig = domainObject . config ,
zoneName = domainObject . zoneName ,
name = dns . getName ( domainObject , location , type ) || '@' ;
const zone = await getZone ( domainConfig , zoneName ) ;
const result = await getRecords ( domainConfig , zone , name , type ) ;
return result . map ( function ( record ) { return record . value ; } ) ;
}
async function verifyDomainConfig ( domainObject ) {
assert . strictEqual ( typeof domainObject , 'object' ) ;
const domainConfig = domainObject . config ,
zoneName = domainObject . zoneName ;
if ( ! domainConfig . token || typeof domainConfig . token !== 'string' ) throw new BoxError ( BoxError . BAD _FIELD , 'token must be a non-empty string' ) ;
if ( 'customNameservers' in domainConfig && typeof domainConfig . customNameservers !== 'boolean' ) throw new BoxError ( BoxError . BAD _FIELD , 'customNameservers must be a boolean' ) ;
const ip = '127.0.0.1' ;
const credentials = {
token : domainConfig . token ,
customNameservers : ! ! domainConfig . customNameservers
} ;
if ( constants . TEST ) return credentials ; // this shouldn't be here
const [ error , nameservers ] = await safe ( dig . resolve ( zoneName , 'NS' , { timeout : 5000 } ) ) ;
if ( error && error . code === 'ENOTFOUND' ) throw new BoxError ( BoxError . BAD _FIELD , 'Unable to resolve nameservers for this domain' ) ;
if ( error || ! nameservers ) throw new BoxError ( BoxError . BAD _FIELD , error ? error . message : 'Unable to get nameservers' ) ;
// https://docs.hetzner.com/dns-console/dns/general/dns-overview#the-hetzner-online-name-servers-are
if ( ! nameservers . every ( function ( n ) { return n . toLowerCase ( ) . search ( /hetzner|your-server|second-ns/ ) !== - 1 ; } ) ) {
debug ( 'verifyDomainConfig: %j does not contain Hetzner NS' , nameservers ) ;
if ( ! domainConfig . customNameservers ) throw new BoxError ( BoxError . BAD _FIELD , 'Domain nameservers are not set to Hetzner' ) ;
}
const location = 'cloudrontestdns' ;
await upsert ( domainObject , location , 'A' , [ ip ] ) ;
debug ( 'verifyDomainConfig: Test A record added' ) ;
await del ( domainObject , location , 'A' , [ ip ] ) ;
debug ( 'verifyDomainConfig: Test A record removed again' ) ;
return credentials ;
}
2026-02-14 15:43:24 +01:00
export default {
removePrivateFields ,
injectPrivateFields ,
upsert ,
get ,
del ,
wait ,
verifyDomainConfig
} ;