-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.cs
159 lines (139 loc) · 5.07 KB
/
Utils.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
using System.Net;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text;
using Aliyun.Acs.Alidns.Model.V20150109;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Auth;
using Aliyun.Acs.Core.Profile;
using Newtonsoft.Json;
namespace cf2dns_script_DotnetCSharp
{
internal class Utils
{
public class ip
{
[JsonProperty("ip")]
public string ipnumber { get; set; }
[JsonProperty("latency")]
public int ping { get; set; }
[JsonProperty("speed")]
public int speedkbs { get; set; }
}
public class iplist
{
[JsonProperty("CM")]
public List<ip> CMCCip { get; set; }
[JsonProperty("CT")]
public List<ip> CTCCip { get; set; }
[JsonProperty("CU")]
public List<ip> CUCCip { get; set; }
}
public class CloudFlare_Optimization_IP
{
[JsonProperty("info")]
public iplist info { get; set; }
}
public class AliDNSRecordsResult
{
[JsonProperty("DomainRecords")]
public DomainRecords info { get; set; }
}
public class DomainRecords
{
public List<DNSRecord> Record { get; set; }
}
public class DNSRecord
{
public string RR { get; set; }
public string Line { get; set; }
public string Type { get; set; }
public string RecordId { get; set; }
}
public enum LogEnum
{
Info=1,
Warning=2,
Error=3
}
public enum LineEnum
{
telecom=1,
unicom=2,
mobile=3,
oversea=4
}
public class IPKey
{
public string key { get; set; }
}
private static readonly HttpClient HttpClient = new HttpClient();
public static async Task<string> POST(string host, string body)
{
using var content = new StringContent(body);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
using var responseMessage = await HttpClient.PostAsync(host, content);
responseMessage.EnsureSuccessStatusCode();
return await responseMessage.Content.ReadAsStringAsync();
/*
string result = null;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
request.Method = "POST";
request.Headers.Add("Content-Type", "application/json");
byte[] data = Encoding.UTF8.GetBytes(body);
request.ContentLength = data.Length;
using (Stream reqStream = request.GetRequestStream())
{
reqStream.Write(data, 0, data.Length);
reqStream.Close();
}
request.ContentLength = data.Length;
using (Stream reqStream = request.GetRequestStream())
{
reqStream.Write(data, 0, data.Length);
reqStream.Close();
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
result = reader.ReadToEnd();
}
return result;
*/
}
public static void WriteLog(LogEnum e, string body)
{
Console.WriteLine("["+e.ToString()+"] "+body);
}
public static void AddDomainRecord(DefaultAcsClient client, string Type, string RR,
string DomainName, string Value, LineEnum line)
{
WriteLog(LogEnum.Info,RR+"."+DomainName+" "+line.ToString()+" "+Value);
var request = new AddDomainRecordRequest();
request.Type=Type;
request.RR=RR;
request.DomainName=DomainName;
request._Value=Value;
request.Line = line.ToString();
var response = client.GetAcsResponse(request);
WriteLog(LogEnum.Info,System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
}
public static string DescribeDomainRecords(DefaultAcsClient client, string DomainName)
{
var request=new DescribeDomainRecordsRequest();
request.DomainName = DomainName;
request.PageSize = 500;
var response = client.GetAcsResponse(request);
WriteLog(LogEnum.Info,System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
return Encoding.Default.GetString(response.HttpResponse.Content);
}
public static void DeleteDomainRecord(DefaultAcsClient client, string RecordId)
{
var request = new DeleteDomainRecordRequest();
request.RecordId = RecordId;
var response = client.GetAcsResponse(request);
WriteLog(LogEnum.Info,Encoding.Default.GetString(response.HttpResponse.Content));
}
}
}