-
Notifications
You must be signed in to change notification settings - Fork 0
/
AliyunDDNS.cs
323 lines (280 loc) · 10.6 KB
/
AliyunDDNS.cs
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
using Aliyun.Acs.Alidns.Model.V20150109;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using System.Net;
using System.Net.Sockets;
using System.Text;
using static Aliyun.Acs.Alidns.Model.V20150109.DescribeDomainRecordsResponse;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
public class AliyunDDNSUpdater
{
private IAcsClient Client { get; set; }
private readonly ServiceConfiguration config;
public AliyunDDNSUpdater(ServiceConfiguration sconfig)
{
config = sconfig;
Client = new DefaultAcsClient(DefaultProfile.GetProfile(),
new Aliyun.Acs.Core.Auth.BasicCredentials(config.AccessKey.ID, config.AccessKey.Secret));
}
public bool UpdateDNS()
{
int page = 1;
List<DescribeDomainRecords_Record>? records = GetRecords(page);
if (records == null)
{
return UpdateRecord();
}
while (records != null && records.Count != 0)
{
foreach (DescribeDomainRecords_Record record in records)
{
if (record.DomainName == config.Domain && record.RR == config.SubDomain)
{
// update the record if found
return UpdateRecord(record);
}
}
records = GetRecords(++page);
}
// add record if not found
return AddRecord();
}
public static string GetIPv6Locally()
{
List<Tuple<string, long>> ipv6s = new List<Tuple<string, long>>();
// get all network interfaces
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
// get all IP addresses
foreach (NetworkInterface adapter in adapters)
{
if (adapter.NetworkInterfaceType != NetworkInterfaceType.Ethernet && adapter.NetworkInterfaceType != NetworkInterfaceType.Wireless80211)
{
continue;
}
if (adapter.OperationalStatus != OperationalStatus.Up)
{
continue;
}
IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
UnicastIPAddressInformationCollection unicastIPAddresses = adapterProperties.UnicastAddresses;
// get IPv6 address
foreach (UnicastIPAddressInformation unicastIPAddress in unicastIPAddresses)
{
// discard addresses that are not IPv6
if (unicastIPAddress.Address.AddressFamily != AddressFamily.InterNetworkV6)
continue;
// discard addresses
if (unicastIPAddress.Address.IsIPv4MappedToIPv6 || unicastIPAddress.Address.IsIPv6LinkLocal || unicastIPAddress.Address.IsIPv6Multicast || unicastIPAddress.Address.IsIPv6SiteLocal || unicastIPAddress.Address.IsIPv6Teredo || unicastIPAddress.Address.IsIPv6UniqueLocal)
continue;
// discard the loopback address, unspecified address
if (unicastIPAddress.Address.ToString().StartsWith("::1") || unicastIPAddress.Address.ToString().StartsWith("::"))
continue;
// if platform is win, select the longest AddressPreferredLifetime, AddressValidLifetime
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// discard random address
// if (unicastIPAddress.SuffixOrigin == SuffixOrigin.Random)
// continue;
ipv6s.Add(new Tuple<string, long>(unicastIPAddress.Address.ToString(), unicastIPAddress.AddressPreferredLifetime));
}
else
{
ipv6s.Add(new Tuple<string, long>(unicastIPAddress.Address.ToString(), unicastIPAddress.PrefixLength));
}
}
}
string ipv6 = "";
long max = long.MinValue;
foreach (Tuple<string, long> ipv6Info in ipv6s)
{
if (ipv6Info.Item2 > max)
{
ipv6 = ipv6Info.Item1;
max = ipv6Info.Item2;
}
}
return ipv6;
}
public static string GetIPv6()
{
string ipv6 = String.Empty;
HttpClient client = new(
new SocketsHttpHandler()
{
ConnectCallback = async (context, cancellationToken) =>
{
// Use DNS to look up the IP address(es) of the target host
IPHostEntry ipHostEntry = await Dns.GetHostEntryAsync(context.DnsEndPoint.Host);
// Filter for IPv6 addresses only
Func<IPAddress, bool> predicate = i =>
{
return i.AddressFamily == AddressFamily.InterNetworkV6;
};
IPAddress? ipAddress = ipHostEntry?.AddressList.FirstOrDefault(predicate: predicate);
// Fail the connection if there aren't any IPV6 addresses
if (ipAddress == null)
{
Task.Delay(10000).Wait();
//return Stream.Null;
throw new Exception($"No IP4 address for {context.DnsEndPoint.Host}");
}
// Open the connection to the target host/port
TcpClient tcp = new();
await tcp.ConnectAsync(ipAddress, context.DnsEndPoint.Port, cancellationToken);
// Return the NetworkStream to the caller
return tcp.GetStream();
}
});
Task<string>[] tasks =
{
client.GetStringAsync("http://ip.jqknono.com:30000/ip"),
client.GetStringAsync("https://6.ipw.cn"),
client.GetStringAsync("https://api64.ipify.org"),
};
try
{
int index = Task.WaitAny(tasks, TimeSpan.FromSeconds(15));
if (index == -1)
{
return ipv6;
}
if (tasks[index].Status == TaskStatus.RanToCompletion)
{
ipv6 = tasks[index].Result;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return string.Empty;
}
return ipv6;
}
private List<DescribeDomainRecords_Record>? GetRecords(int page)
{
DescribeDomainRecordsRequest request = new()
{
DomainName = config.Domain,
#if DEBUG
PageSize = 10,
#else
PageSize = 50,
#endif
PageNumber = page,
Type = "AAAA",
Status = "ENABLE"
};
try
{
DescribeDomainRecordsResponse response = Client.GetAcsResponse(request);
return response.DomainRecords;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return null;
}
private List<DescribeDomainRecords_Record> GetAllRecords()
{
List<DescribeDomainRecords_Record> records = new();
int page = 1;
List<DescribeDomainRecords_Record>? pageRecords = GetRecords(page);
while (pageRecords != null && pageRecords.Count != 0)
{
records.AddRange(pageRecords);
pageRecords = GetRecords(++page);
}
return records;
}
public string ListRecords()
{
List<DescribeDomainRecords_Record> records = GetAllRecords();
if (records.Count == 0)
{
return "Failed to get records.";
}
StringBuilder sb = new();
string format = "{0,-20} {1,-20} {2,-6} {3,-20}";
sb.AppendLine(string.Format(format, "RecordId", "RR", "Type", "Value"));
foreach (DescribeDomainRecords_Record record in records)
{
// add with alignment
sb.AppendLine(string.Format(format, record.RecordId, record.RR, record.Type, record._Value));
}
return sb.ToString();
}
private bool UpdateRecord(DescribeDomainRecords_Record? record = null)
{
string ip = GetIPv6();
if (string.IsNullOrEmpty(ip))
{
ip = GetIPv6Locally();
}
if (string.IsNullOrEmpty(ip))
{
Console.WriteLine("[Error]Can not get ipv6 address, check your network connection.");
return false;
}
if (record != null && record._Value == ip)
{
Console.WriteLine("IPv6 address is not changed.");
return true;
}
Console.WriteLine("Update SubDomain:" + config.SubDomain + "." + config.Domain + " to=>[" + ip + "]");
if (string.IsNullOrEmpty(record?.RecordId) && string.IsNullOrEmpty(config.RecordId))
{
Console.WriteLine("[Error]Can not get recordId, please check your config.");
return false;
}
UpdateDomainRecordRequest request = new()
{
RecordId = config.RecordId ?? record?.RecordId,
RR = config.SubDomain,
Type = config.Type,
_Value = ip,
};
try
{
UpdateDomainRecordResponse response = Client.GetAcsResponse(request);
return response.HttpResponse.isSuccess();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return false;
}
private bool AddRecord()
{
string ip = GetIPv6();
if (string.IsNullOrEmpty(ip))
{
ip = GetIPv6Locally();
}
if (string.IsNullOrEmpty(ip))
{
Console.WriteLine("Can not get ipv6 address.");
return false;
}
Console.WriteLine("Add SubDomain:" + config.SubDomain + "." + config.Domain + " to=>[" + ip + "]");
AddDomainRecordRequest request = new()
{
DomainName = config.Domain,
RR = config.SubDomain,
Type = config.Type,
_Value = ip,
};
try
{
AddDomainRecordResponse response = Client.GetAcsResponse(request);
return response.HttpResponse.isSuccess();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return false;
}
}