-
Notifications
You must be signed in to change notification settings - Fork 0
/
CDynamicDomain.js
140 lines (129 loc) · 4.32 KB
/
CDynamicDomain.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
'use strict';
const Core = require('@alicloud/pop-core');
const https = require('https');
const requestOption = {
method: 'POST'
};
class CDynamicDomain
{
constructor( szAccessKeyID, szAccessKeySecret,
szDomain, szRR, szType, nInterval, funcGetValue )
{
this.m_funcGetValue = funcGetValue || function( funcCallback )
{
CDynamicDomain.GetWorldIP( funcCallback );
};
this.m_nInterval = nInterval;
this.m_szDomain = szDomain;
this.m_szType = szType;
this.m_szRR = szRR;
this.m_pClient = new Core({
accessKeyId: szAccessKeyID,
accessKeySecret: szAccessKeySecret,
endpoint: 'https://alidns.aliyuncs.com',
apiVersion: '2015-01-09'
});
this.m_aryDomainRecords = null;
this.m_nRecordID = 0;
this.m_szPrevValue = null;
this.m_szNextValue = null;
this.DelayCheck();
}
static MessageError( ex )
{
console.error( "error....................." );
console.error( ex );
console.error( "error....................." );
}
static GetWorldIP( funcCallback )
{
https.get( 'https://www.taobao.com/help/getip.php', ( res ) => {
res.on( 'data', ( d ) => {
var data = '';
for( var i = 0; i < d.length; ++i )
data += String.fromCharCode( d[i] );
function ipCallback( pObject )
{
return pObject.ip;
}
var szIP = eval( data );
funcCallback.call( null, szIP );
} );
} ).on( 'error', (e) => {
funcCallback.call( null, null );
} );
}
Check()
{
console.log( 'Check', this.m_nRecordID, this.m_szPrevValue, this.m_szNextValue );
if( this.m_szNextValue === null )
{
this.m_funcGetValue.call( this, ( szValue )=>{
this.m_szNextValue = szValue;
this.DelayCheck();
} );
}
else if( this.m_aryDomainRecords === null )
{
var params =
{
"DomainName": this.m_szDomain
};
this.m_pClient.request( 'DescribeDomainRecords', params, requestOption ).then( (result) => {
var aryRecord = result.DomainRecords.Record;
this.m_aryDomainRecords = aryRecord;
for( var i = 0; i < aryRecord.length; ++i )
{
var szRR = aryRecord[i].RR;
var szType = aryRecord[i].Type;
if( szRR != this.m_szRR || szType != this.m_szType )
continue;
this.m_nRecordID = aryRecord[i].RecordId;
this.m_szPrevValue = aryRecord[i].Value;
break;
}
this.DelayCheck();
}, CDynamicDomain.MessageError );
}
else if( this.m_nRecordID == 0 )
{
var params = {
"DomainName" : this.m_szDomain,
"RR": this.m_szRR,
"Type": this.m_szType,
"Value": this.m_szNextValue
};
this.m_pClient.request( 'AddDomainRecord', params, requestOption ).then( (result) => {
this.m_nRecordID = result.RecordId;
this.m_szPrevValue = this.m_szNextValue;
this.DelayCheck();
}, CDynamicDomain.MessageError );
}
else if( this.m_szPrevValue == this.m_szNextValue )
{
this.m_szNextValue = null;
this.DelayCheck();
}
else
{
var params = {
"RecordId" : this.m_nRecordID,
"RR": this.m_szRR,
"Type": this.m_szType,
"Value": this.m_szNextValue
};
this.m_pClient.request( 'UpdateDomainRecord', params, requestOption ).then( (result) => {
this.m_szPrevValue = this.m_szNextValue;
this.m_szNextValue = null;
this.DelayCheck();
}, CDynamicDomain.MessageError );
}
}
DelayCheck()
{
setTimeout( ()=>{
this.Check();
}, this.m_nInterval );
}
};
module.exports = CDynamicDomain;