-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalidns.js
163 lines (147 loc) · 4.5 KB
/
alidns.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
var request = require('request');
var copy = require('copy-to');
var crypto = require('crypto');
var defaultOptions = {
// API版本: https://help.aliyun.com/document_detail/29741.html
apiVerison: '2015-01-09',
region: 'cn-hangzhou',
timeout: 120000
}
var config = {
// DNS API的服务接入地址为:https://help.aliyun.com/document_detail/29744.html
endpoint: 'http://alidns.aliyuncs.com/'
}
var ALIDNS = function(options) {
if (!(this instanceof ALIDNS)) {
return new ALIDNS(options);
}
this.options = defaultOptions;
options && copy(options)
.toCover(this.options);
// 创建AccessKey
// https://help.aliyun.com/document_detail/53045.html
if (!this.options.accesskeyId ||
!this.options.accesskeySecret) {
throw new Error('accesskeyId, accesskeySecret is required');
}
}
module.exports = ALIDNS;
var proto = ALIDNS.prototype;
proto.queryData = function(conditions = {}, fn) {
// API概览 :
// https://help.aliyun.com/document_detail/29740.html
this._request('GET', conditions, fn);
}
// GET参数生成
proto._generateUrl = function(customParams) {
var params = this._getBasicParams();
for (var i in customParams) {
var value = customParams[i];
if (typeof value != 'string') {
value = JSON.stringify(value);
}
params[i] = value;
}
params['Signature'] = this._computeSignature(params, 'GET');
var url = '';
for (var i in params) {
url += '&' + this._percentEncode(i) + '=' + this._percentEncode(params[i]);
}
url = url.substring(1);
return config.endpoint + '?' + url;
}
// POST参数生成
proto._generateBody = function(customParams) {
var params = this._getBasicParams();
for (var i in customParams) {
var value = customParams[i];
if (typeof value != 'string') {
value = JSON.stringify(value);
}
params[i] = value;
}
params['Signature'] = this._computeSignature(params, 'POST');
var fields_string = '';
for (var i in params) {
fields_string += '&' + this._percentEncode(i) + '=' + this._percentEncode(params[i]);
}
fields_string = fields_string.substring(1);
return fields_string;
}
// 签名机制: https://help.aliyun.com/document_detail/29747.html
proto._computeSignature = function(params, method) {
var keys = Object.keys(params);
keys = keys.sort();
var canonicalizedQueryString = '';
for (var i = 0; i < keys.length; i++) {
canonicalizedQueryString += '&' + this._percentEncode(keys[i]) +
'=' + this._percentEncode(params[keys[i]]);
}
canonicalizedQueryString = this._percentEncode(canonicalizedQueryString.substring(1));
var stringToSign = method + '&%2F&' + canonicalizedQueryString;
var signature = crypto.createHmac('sha1', this.options.accesskeySecret + '&')
.update(stringToSign)
.digest()
.toString('base64');
return signature;
}
// 生成公共请求参数
// 公共请求参数说明: https://help.aliyun.com/document_detail/29745.html
proto._getBasicParams = function() {
var now = new Date();
var nonce = now.getTime() + '' + parseInt((Math.random() * 1000000000));
return {
RegionId: this.options.region,
AccessKeyId: this.options.accesskeyId,
Format: 'JSON',
SignatureMethod: 'HMAC-SHA1',
SignatureVersion: '1.0',
SignatureNonce: nonce,
Timestamp: now.toISOString(),
Version: this.options.apiVerison
}
}
// 规范化字符串,URL编码
proto._percentEncode = function(str) {
var str = encodeURIComponent(str);
str = str.replace(/\*/g, '%20');
str = str.replace(/\'/g, '%27');
str = str.replace(/\(/g, '%28');
str = str.replace(/\)/g, '%29');
return str;
}
// 调用请求
proto._request = function(method, body, fn) {
var reqOptions = {
timeout: this.options.timeout,
method: method
};
if (method.toUpperCase() == 'GET') {
reqOptions.url = this._generateUrl(body);
} else {
reqOptions.url = config.endpoint;
reqOptions.body = this._generateBody(body);
reqOptions.headers = { 'Content-Type': 'application/x-www-form-urlencoded' };
}
var that = this;
request(reqOptions, function(err, response, body) {
that._onRequestResponse(err, response, fn)
});
}
// request请求回调
proto._onRequestResponse = function(err, response, fn) {
if (err) {
return fn(err, null)
} else {
try {
var result = JSON.parse(response.body);
if (result.Message) {
return fn(new Error(result.Message), null);
}
return fn(null, result);
} catch (e) {
console.error(e);
return fn(new Error('Request MetricStore failed: ' + response.body), null);
}
}
}