forked from dosgo/ngrok-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddns.cpp
243 lines (232 loc) · 7.72 KB
/
ddns.cpp
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "ddns.h"
static char * headstring="Accept:*/*\r\nUser-Agent:dnspodc/0.1([email protected])";
string getDomainId(string user,string password,string domain)
{
string DomainId="";
string poststring="login_email="+user+"&login_password="+password+"&format=json&domain="+domain;
struct http_response *hresp = http_post("https://dnsapi.cn/Domain.Info",headstring,(char*)poststring.c_str());
if(hresp!=NULL){
int start=strlpos(hresp->body,"{",0);
int end=strrpos(hresp->body,"}",0);
char*jsonstr=hresp->body+start;
jsonstr[end+1]='\0';
cJSON *json = cJSON_Parse(jsonstr);
if(json)
{
cJSON *status = cJSON_GetObjectItem(json, "status");
if(status)
{
cJSON *code= cJSON_GetObjectItem(status, "code");
if(code)
{
char *codestr=code->valuestring;
if ( strcmp(codestr,"1")==0)
{
cJSON *domain = cJSON_GetObjectItem(json, "domain");
//
char * domain_id=cJSON_GetObjectItem(domain, "id")->valuestring;
DomainId=string(domain_id);
}
}
}
}
cJSON_Delete(json);
}
free(hresp);
return DomainId;
}
string getRecordList(string user,string password,string domain_id)
{
string jsonstr="";
string poststring="login_email="+user+"&login_password="+password+"&format=json&domain_id="+domain_id;
struct http_response *hresp = http_post("https://dnsapi.cn/Record.List",headstring,(char *)poststring.c_str());
if(hresp!=NULL){
int start=strlpos(hresp->body,"{",0);
int end=strrpos(hresp->body,"}",0);
char*json=hresp->body+start;
json[end+1]='\0';
jsonstr=string(json);
}
free(hresp);
return jsonstr;
}
int _ddns(string user,string password,string domain_id,string record_id,string sub_domain,string record_line,string value)
{
string poststring="login_email="+user+"&login_password="+password+"&format=json&lang=en&domain_id="+domain_id+"&record_id="+record_id+"&sub_domain="+sub_domain+"&record_line="+record_line+"&value="+value;
printf("poststring:%s\r\n",poststring.c_str());
struct http_response *hresp = http_post("https://dnsapi.cn/Record.Ddns",headstring,(char *)poststring.c_str());
if(hresp!=NULL){
int start=strlpos(hresp->body,"{",0);
int end=strrpos(hresp->body,"}",0);
char*jsonstr=hresp->body+start;
jsonstr[end+1]='\0';
// printf("jsonstr:%s\r\n",jsonstr);
cJSON *json = cJSON_Parse(jsonstr);
if(!json)
{
cJSON_Delete(json);
return -1;
}
cJSON *status = cJSON_GetObjectItem(json, "status");
if(!status)
{
cJSON_Delete(json);
return -1;
}
char *code= cJSON_GetObjectItem(status, "code")->valuestring;
if ( strcmp(code,"1")==0)
{
cJSON_Delete(json);
return 0;
}
cJSON_Delete(json);
}
free(hresp);
return -1;
}
int ddns(string user,string password,string domain,string sub_domain,string value)
{
string DomainId=getDomainId(user,password,domain);
if(!DomainId.empty())
{
string jsonstr=getRecordList(user,password,DomainId);
if(!jsonstr.empty())
{
cJSON *json = cJSON_Parse(jsonstr.c_str());
if(!json)
{
cJSON_Delete(json);
return -1;
}
cJSON *status = cJSON_GetObjectItem(json, "status");
if(!status)
{
cJSON_Delete(json);
return -1;
}
cJSON *code= cJSON_GetObjectItem(status, "code");
if(!status)
{
cJSON_Delete(json);
return -1;
}
char *codestr=code->valuestring;
if (strcmp(codestr,"1")==0)
{
cJSON*records = cJSON_GetObjectItem(json, "records");
if(!records)
{
cJSON_Delete(json);
return -1;
}
for(int i=0;i<cJSON_GetArraySize(records);i++)
{
cJSON *recordsitem=cJSON_GetArrayItem(records,i);
if(!recordsitem)
{
cJSON_Delete(json);
return -1;
}
char *item_sub_domain =cJSON_GetObjectItem(recordsitem, "name")->valuestring;
char *item_value =cJSON_GetObjectItem(recordsitem, "value")->valuestring;
if(strcmp(sub_domain.c_str(),item_sub_domain)==0&&strcmp(value.c_str(),item_value)!=0)
{
string item_record_line=string(cJSON_GetObjectItem(recordsitem, "line")->valuestring);
string item_record_id =string(cJSON_GetObjectItem(recordsitem, "id")->valuestring);
int err=_ddns(user,password,DomainId,item_record_id,sub_domain,item_record_line,value);
if(err==0)
{
printf("DDNS update OK \r\n");
return 0;
}
printf("DDNS update err \r\n");
return -1;
}
}
}
cJSON_Delete(json);
}
}
printf("DDNS not edit \r\n");
return 0;
}
int strrpos(const char *haystack,const char *needle, int ignorecase = 0)
{
int back=0;
register unsigned char c, needc;
unsigned char const *from, *end;
int len = strlen(haystack);
int needlen = strlen(needle);
from = (unsigned char *)haystack;
end = (unsigned char *)haystack + len;
const char *findreset = needle;
for (int i = 0; from < end; ++i) {
c = *from++;
needc = *needle;
if (ignorecase) {
if (c >= 65 && c < 97)
c += 32;
if (needc >= 65 && needc < 97)
needc += 32;
}
if(c == needc) {
++needle;
if(*needle == '\0') {
if (len == needlen)
{
back=0;
//return 0;
}
else
{
back=i - needlen+1;
//return i - needlen+1;
}
}
}
else {
if(*needle == '\0' && needlen > 0)
{
back=i - needlen+1;
//return i - needlen +1;
}
needle = findreset;
}
}
return back;
}
int strlpos(const char *haystack,const char *needle, int ignorecase = 0)
{
register unsigned char c, needc;
unsigned char const *from, *end;
int len = strlen(haystack);
int needlen = strlen(needle);
from = (unsigned char *)haystack;
end = (unsigned char *)haystack + len;
const char *findreset = needle;
for (int i = 0; from < end; ++i) {
c = *from++;
needc = *needle;
if (ignorecase) {
if (c >= 65 && c < 97)
c += 32;
if (needc >= 65 && needc < 97)
needc += 32;
}
if(c == needc) {
++needle;
if(*needle == '\0') {
if (len == needlen)
return 0;
else
return i - needlen+1;
}
}
else {
if(*needle == '\0' && needlen > 0)
return i - needlen +1;
needle = findreset;
}
}
return 0;
}