Skip to content

Instantly share code, notes, and snippets.

@z0mt3c
Last active August 29, 2015 14:27
Show Gist options
  • Save z0mt3c/f81b00983474b1a7815b to your computer and use it in GitHub Desktop.
Save z0mt3c/f81b00983474b1a7815b to your computer and use it in GitHub Desktop.
HE.net DynDNS supporting ipv4 and ipv6 (single call)
module['exports'] = function simpleHttpRequest (hook) {
var request = require('request')
var async = require('async')
var Hoek = require('hoek')
var _ = require('lodash')
var host = hook.params.domain
var password = hook.params.password
var IPs = []
if (_.isString(hook.params.ipv4) && hook.params.ipv4.indexOf('.') !== -1) {
IPs.push(hook.params.ipv4)
}
if (_.isString(hook.params.ipv6) && hook.params.ipv6.indexOf(':') !== -1) {
IPs.push(hook.params.ipv6)
}
if (_.isEmpty(IPs) || !host || !password) {
hook.res.statusCode = 500
return hook.res.end(JSON.stringify({ status: 500, error: 'Parameter invalid host, password and at least ip is required (ipv4 and/or ipv6)', host: host, ips: IPs }, null, 2))
}
var defaults = {
url: 'https://dyn.dns.he.net/nic/update',
strictSSL: false,
form: {
hostname: host,
password: password
}
}
var statusMap = {
badauth: 401,
badip: 400
}
async.map(IPs, function (ip, next) {
request.post(Hoek.applyToDefaults(defaults, {form: {myip: ip}}), function (error, response, body) {
next(error, { ip: ip, status: statusMap[body] || response.statusCode, body: body })
})
}, function (error, results) {
if (error) {
hook.res.statusCode = 500
return hook.res.end(JSON.stringify({ status: 500, error: error, host: host, ips: IPs }, null, 2))
}
var failure = _.find(results, function (result) {
return result.status !== 200
})
var statusCode = failure ? failure.status : 200
var responseBody = JSON.stringify({ status: statusCode, host: host, ips: IPs, results: results }, null, 2)
hook.res.statusCode = statusCode
return hook.res.end(responseBody)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment