-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathDnsServersCustom.cs
149 lines (126 loc) · 5.98 KB
/
DnsServersCustom.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
using System.Xml.Linq;
namespace SecureDNSClient;
public partial class FormMain
{
public class ReadDnsResult
{
public string DNS { get; set; } = string.Empty;
public CheckMode CheckMode { get; set; } = CheckMode.Unknown;
public string GroupName { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
}
private async Task<List<string>> ReadCustomServersXmlGroups(string xmlPath)
{
List<string> groupList = new();
Task rcs = Task.Run(() =>
{
try
{
if (!File.Exists(xmlPath)) return;
XDocument doc = XDocument.Load(xmlPath, LoadOptions.None);
if (doc.Root == null) return;
var groups = doc.Root.Elements().Elements();
for (int a = 0; a < groups.Count(); a++)
{
if (StopChecking) return;
XElement group = groups.ToList()[a];
XElement? groupNameElement = group.Element("Name");
if (groupNameElement == null) continue;
XElement? groupEnabledElement = group.Element("Enabled");
if (groupEnabledElement == null) continue;
if (groupEnabledElement.Value.ToLower().Trim().Equals("true")) // If Group Enabled
{
var dnss = group.Elements("DnsItem");
for (int b = 0; b < dnss.Count(); b++)
{
if (StopChecking) return;
XElement dnsItem = dnss.ToList()[b];
XElement? isDnsEnabled = dnsItem.Element("Enabled");
if (isDnsEnabled == null) continue;
if (isDnsEnabled.Value.ToLower().Trim().Equals("true")) // If DNS Enabled
{
// If group is not empty
groupList.Add(groupNameElement.Value.Trim());
break;
}
}
}
}
}
catch (Exception) { }
});
await rcs.WaitAsync(CancellationToken.None);
return groupList;
}
public static async Task<List<ReadDnsResult>> ReadCustomServersXml(string? xml, CheckRequest checkRequest, bool isXmlFile = true)
{
Task<List<ReadDnsResult>> rcs = Task.Run(() =>
{
List<ReadDnsResult> output = new();
if (string.IsNullOrEmpty(xml)) return output;
try
{
XDocument doc;
if (isXmlFile)
{
if (!File.Exists(xml)) return output;
doc = XDocument.Load(xml, LoadOptions.None);
}
else
{
doc = XDocument.Parse(xml, LoadOptions.None);
}
if (doc.Root == null) return output;
var groups = doc.Root.Elements().Elements();
for (int a = 0; a < groups.Count(); a++)
{
if (StopChecking) return output.ToList();
XElement group = groups.ToList()[a];
XElement? dnsGroupNameElement = group.Element("Name");
if (dnsGroupNameElement == null) continue;
string dnsGroupName = dnsGroupNameElement.Value.Trim();
// Skip if group name is not match
bool isGroupNameMatch = !string.IsNullOrEmpty(checkRequest.GroupName.Trim()) &&
checkRequest.GroupName.Trim().Equals(dnsGroupName);
if (checkRequest.HasUserGroupName && !isGroupNameMatch) continue;
XElement? groupEnabledElement = group.Element("Enabled");
if (groupEnabledElement == null) continue;
if (groupEnabledElement.Value.ToLower().Trim().Equals("true")) // If Group Enabled
{
var dnss = group.Elements("DnsItem");
for (int b = 0; b < dnss.Count(); b++)
{
if (StopChecking) return output;
XElement dnsItem = dnss.ToList()[b];
XElement? isDnsEnabled = dnsItem.Element("Enabled");
if (isDnsEnabled == null) continue;
if (isDnsEnabled.Value.ToLower().Trim().Equals("true")) // If DNS Enabled
{
XElement? dnsElement = dnsItem.Element("Dns");
if (dnsElement == null) continue;
string dnsAddress = dnsElement.Value.Trim();
ReadDnsResult rdr = new()
{
DNS = dnsAddress,
CheckMode = checkRequest.CheckMode
};
rdr.GroupName = rdr.CheckMode == CheckMode.BuiltIn || rdr.CheckMode == CheckMode.SavedServers
? checkRequest.GroupName : dnsGroupName;
XElement? dnsDescriptionElement = dnsItem.Element("Description");
if (dnsDescriptionElement != null)
{
string dnsDescription = dnsDescriptionElement.Value.Trim();
rdr.Description = dnsDescription;
}
output.Add(rdr);
}
}
}
}
}
catch (Exception) { }
return output;
});
return await rcs.WaitAsync(CancellationToken.None);
}
}