Skip to content

Instantly share code, notes, and snippets.

@mashihua
Created June 20, 2012 03:54
Show Gist options
  • Save mashihua/2958050 to your computer and use it in GitHub Desktop.
Save mashihua/2958050 to your computer and use it in GitHub Desktop.
Simply node.js client for DNSPod
#!/usr/bin/env coffee
common =
# login email
login_email : 'your mail'
# password
login_password : 'password'
# response format
format : 'json'
# Default vaule for Record.Modify, this vaule will pass to recordModify with no arguments given by terminal.
# You also can pass arguments at terminal. For example:
# dnspod recordModify 10 19 'phone' '127.0.0.1'
# Will set your phone.example.com to 127.0.0.1
#domain id;
domain_id = '10'
#recode id
record_id = '9'
#sub domain name
sub_domain = 'rc'
https = require 'https'
querystring = require 'querystring'
{spawn} = require 'child_process'
# modify a dns record
exports.recordModify = (args...) ->
record '/Record.Modify', args...
# Create a dns record
exports.recordCreate = (args...) ->
request '/Record.Create', args...
#list of domains
exports.domain = (type = 'mine') ->
request '/Domain.List', {type}
#list of records
exports.records = (domain_id) ->
request '/Record.List', {domain_id}
#all records method
record = (url, domain_id, record_id, sub_domain , value, record_type = 'A' , record_line = '默认') ->
request url, {domain_id, record_id, sub_domain, value, record_type, record_line}
#requesrt to server
request = (url, data) ->
post_data = params data
opts =
method : 'POST'
host : 'dnsapi.cn'
port : 443
path : url
headers:
Accept : 'text/json'
'Content-Type' : 'application/x-www-form-urlencoded'
'Content-Length' : post_data.length
req = https.request opts, (res) ->
res.setEncoding 'utf8'
data = ''
res.on 'data', (chunk) ->
data += chunk
res.on 'end', ->
console.log JSON.parse data
req.write post_data
req.end()
params = (data) ->
querystring.stringify merge common, data
merge = (from ,to) ->
if from and to
to[key] = val for own key, val of from
to
main = ->
if process.argv.length > 2
cmd = process.argv[2]
exports[cmd] && exports[cmd] process.argv[3..]...
else
ifconfig = spawn 'ifconfig', ['en1']
data = ''
ifconfig.stdout.on 'data', (chunk) ->
data += chunk
ifconfig.on 'exit', ->
ips = data.match /inet\s+([^\s]+)/i
exports.recordModify domain_id, record_id, sub_domain, ips[1] if ips
main() unless module.parent
#!/usr/bin/env node
/**
* Usage:
* dnspod domains Display domain list from dnspod
* dnspod records <domain_id> Display record list from given domain
* dnspod recordCreate Crteat a dns record
* dnspod Modify your dns record to your WI-FI interface,or set it by command line
**/
var common = {
// login email
login_email : "your mail"
// password
, login_password : "password"
//response format
, format : "json"
};
/*
Default vaule for Record.Modify, this vaule will pass to recordModify with no arguments given by terminal.
You also can pass arguments at terminal. For example:
dnspod recordModify 10 19 'phone' '127.0.0.1'
Will set your phone.example.com to 127.0.0.1
*/
//domain id;
var domain_id = "10";
//recode id
var record_id = "9";
//sub domain name
var sub_domain = "rc";
var https = require('https');
var querystring = require('querystring');
//modify a dns record
exports.recordModify = function (domain_id, record_id, name, value, record_type, record_line){
request('/Record.Modify', {
domain_id : domain_id
, record_id : record_id
, sub_domain : name
, record_type : (record_type || "A")
, record_line : (record_line || "默认")
, value : value});
}
//Create a dns record
exports.recordCreate = function (domain_id, record_id, name, value, record_type, record_line){
request('/Record.Create', {
domain_id : domain_id
, record_id : record_id
, sub_domain : name
, record_type : (record_type || "A")
, record_line : (record_line || "默认")
, value : value});
}
//list of domains
exports.domains = function (type){
request('/Domain.List', {type : (type || "mine")});
}
//list of records
exports.records = function (domain_id){
request('/Record.List', {domain_id : domain_id});
}
function request(url, data){
var post_data = params(data);
var req = https.request({
method : 'POST'
, host : 'dnsapi.cn'
, port : 443
, path : url
, headers: {
'Accept' : 'text/json'
, 'Content-Type': 'application/x-www-form-urlencoded'
, 'Content-Length': post_data.length
}
}, function (res){
res.setEncoding('utf8');
var data = "";
res.on('data', function (chunk) {
data += chunk.toString();
});
res.on('end', function (){
console.log(JSON.parse(data));
})
});
req.write(post_data);
req.end();
}
function params (data){
return querystring.stringify(merge(common, data));
}
function merge (from, to){
if(from && to)
for(key in from){
from.hasOwnProperty(key) && (to[key] = from[key]);
}
return to;
}
function main(){
if(process.argv.length > 2){
var cmd;
var args = [];
var i = 3;
cmd = process.argv[2];
for(; i < process.argv.length; i++){
args.push(process.argv[i]);
}
exports[cmd] && exports[cmd].apply(this, args);
}else{
var ifconfig = require('child_process').spawn('ifconfig', ['en1']);
var data = "";
ifconfig.stdout.on('data', function (chunk){
data += chunk;
});
ifconfig.on('exit',function (){
var ips = data.match(/inet\s+([^\s]+)/i);
ips && exports.recordModify(domain_id, record_id, sub_domain, ips[1]);
});
}
}
//main function
if(!module.parent){
main();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment