Skip to content

Instantly share code, notes, and snippets.

@typable
Last active April 28, 2021 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save typable/36f686d78f6859c36303c0d4d595635e to your computer and use it in GitHub Desktop.
Save typable/36f686d78f6859c36303c0d4d595635e to your computer and use it in GitHub Desktop.
Update Domain IP
import request from './request.js';
const EXTERNAL_IP_SERVICE_URL = 'http://checkip.amazonaws.com';
const DOMAIN_IP_SERVICE_URL = 'https://dns.google.com/resolve';
const DOMAIN_PROVIDER_SERVICE_URL = 'https://dynamicdns.park-your-domain.com';
async function resolve(domain) {
if(typeof domain === 'undefined' || domain.length == 0) {
throw 'Invalid domain argument!';
}
let query = `?name=${domain}&type=A`;
let data = await request(DOMAIN_IP_SERVICE_URL + query, 'json');
if(typeof data === 'undefined' || data.length == 0) {
throw 'Request failed due to an unexpected error!';
}
if(typeof data['Answer'] === 'undefined' || data['Answer'].length == 0) {
throw 'Response data is empty!';
}
return data['Answer'].map((entry) => entry.data);
}
async function update(domain, host, password) {
let query = `/update?domain=${domain}&password=${password}&host=${host}`;
return 200 === (await request(DOMAIN_PROVIDER_SERVICE_URL + query, 'status'));
}
export { resolve, update};
import { resolve } from './dns.js';
const DOMAIN = {
base: 'DOMAIN_BASE',
host: 'DOMAIN_HOST',
password: 'DYNAMIC_DNS_PASSWORD'
};
let ip = (await resolve(`${DOMAIN.host}.${DOMAIN.base}`))[0];
console.log(ip);
export default function(url, type = 'text') {
if(typeof url === 'undefined' || url.length == 0) {
throw 'Invalid url argument!';
}
if(type !== 'text' && type !== 'json' && type !== 'status') {
throw 'Invalid type argument! [ text, json, status ]';
}
return fetch(url + '&time=' + Date.now(), { cache: 'no-store' })
.then(function(response) {
if(type !== 'status') {
if(response.status !== 200) {
throw 'Request failed due to status code ' + response.status;
}
return response[type]();
}
return response.status;
})
.catch(function() {
// ...
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment