-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
144 lines (105 loc) · 4.52 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
var request = require('request')
var https = require('https')
module.exports = function(options) {
return new Dynadot(options)
}
function Dynadot(options) {
//if (options.proxy)
// this.agent = new HttpsProxyAgent(options.proxy)
this.options = options
this.options.proxy = options.proxy || false
}
Dynadot.prototype.search = function(domains, cb) {
var rqurl = 'https://api.dynadot.com/api2.html?key=' + this.options.key + '&command=search&' + domains.map(function(d, idx) { return "domain"+(idx+1)+"="+d }).join('&') + '&show_price=1'
request({
'url':rqurl,
'proxy': this.options.proxy
}, function (err, res, body) {
if (err)
return cb(err)
if (res.statusCode !== 200)
return cb({errorCode: "Invalid HTTP response code"})
var dynadot_status = (body.split("\n\n")[0] || '').split(',')
var dynadot_data = body.split("\n\n").slice(1).join("\n\n")
if (dynadot_status[0] === 'error')
return cb({errorCode: "error", errorMessage: dynadot_status[1] })
if (dynadot_status[0] !== 'ok')
return cb({errorCode: "Invalid dynadot response, expected ok, received " + dynadot_status[0], errorMessage: dynadot_status[1] })
//console.log(body)
var dynadot_data = dynadot_data.split("\n")
.filter(function(d) { if (d.trim()=='') return false; return true})
.map(function(d) {
return d.split(',')
})
if (dynadot_data[0][0] == 'error')
return cb({errorCode: dynadot_data[0][1] })
cb( null,dynadot_data.map(function(d) {
return {
//id: d[0],
name: d[1],
available: d[3] === 'error' ? false : (d[3] == 'yes' ? true : false),
error: ['error','offline','system_busy'].indexOf(d[3]) !== -1 ? d[4] : null,
price: d[3] == 'yes' ? {
amount: (d[4].split(' in '))[0],
currency: (d[4].split(' in '))[1],
} : null,
}
}))
})
}
Dynadot.prototype.register = function(domain, duration, cb) {
var rqurl = 'https://api.dynadot.com/api2.html?key=' + this.options.key + '&command=register&domain=' + domain + '&duration='+duration
request({
'url':rqurl,
'proxy': this.options.proxy
}, function (err, res, body) {
if (err)
return cb(err)
if (res.statusCode !== 200)
return cb({errorCode: "Invalid HTTP response code"})
var dynadot_status = (body.split("\n\n")[0] || '').split(',')
var dynadot_data = body.split("\n\n").slice(1).join("\n\n")
if (dynadot_status[0] === 'error')
return cb({errorCode: "error", errorMessage: dynadot_status[1] })
if (dynadot_status[0] !== 'ok')
return cb({errorCode: "Invalid dynadot response, expected ok, received " + dynadot_status[0], errorMessage: dynadot_status[1] })
var dynadot_data = dynadot_data.split("\n")
.filter(function(d) { if (d.trim()=='') return false; return true})
.map(function(d) {
return d.split(',')
})
// 0: success, not_available, insufficient_funds, offline, system_busy, error
if (dynadot_data[0][0] == 'not_available')
return cb({errorCode: "not_available", errorMessage: 'The domain is not available' })
if (dynadot_data[0][0] == 'insufficient_funds')
return cb({errorCode: "insufficient_funds", errorMessage: 'Not enough account balance to process this registration' })
if (dynadot_data[0][0] == 'offline')
return cb({errorCode: "offline", errorMessage: 'The central registry for this domain is currently offline' })
if (dynadot_data[0][0] == 'system_busy')
return cb({errorCode: "system_busy", errorMessage: 'All connections are busy' })
if (dynadot_data[0][0] == 'error')
return cb({errorCode: "error", errorMessage: dynadot_data[0][1] })
if (dynadot_data[0][0] == 'success')
return cb(null, { expire: dynadot_data[0][2]})
return cb({errorCode: "error", errorMessage: 'Unhandled error', rar_response: body })
})
}
Dynadot.prototype.set_ns = function(domain, nsz, cb) {
var rqurl = 'https://api.dynadot.com/api2.html?key=' + this.options.key + '&command=set_ns&domain=' + domain + '&' + nsz.map(function(n, idx) { return "ns"+(idx+1)+"="+n }).join('&')
request({
'url':rqurl,
'proxy': this.options.proxy
}, function (err, res, body) {
if (err)
return cb(err)
if (res.statusCode !== 200)
return cb({errorCode: "Invalid HTTP response code"})
var dynadot_status = (body.split("\n\n")[0] || '').split(',')
var dynadot_data = body.split("\n\n").slice(1).join("\n\n")
if (dynadot_status[0] === 'error')
return cb({errorCode: "error", errorMessage: dynadot_status[1] })
if (dynadot_status[0] !== 'ok')
return cb({errorCode: "Invalid dynadot response, expected ok, received " + dynadot_status[0], errorMessage: dynadot_status[1] })
cb(null)
})
}