DropRequests: removed newtonsoft

This commit is contained in:
Shreyas Zare
2022-12-17 16:33:57 +05:30
parent 4412f03132
commit b0f2290bc0

View File

@@ -18,12 +18,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using DnsServerCore.ApplicationCommon;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text.Json;
using System.Threading.Tasks;
using TechnitiumLibrary;
using TechnitiumLibrary.Net;
using TechnitiumLibrary.Net.Dns;
using TechnitiumLibrary.Net.Dns.ResourceRecords;
@@ -55,64 +56,32 @@ namespace DropRequests
public async Task InitializeAsync(IDnsServer dnsServer, string config)
{
dynamic jsonConfig = JsonConvert.DeserializeObject(config);
using JsonDocument jsonDocument = JsonDocument.Parse(config);
JsonElement jsonConfig = jsonDocument.RootElement;
_enableBlocking = jsonConfig.enableBlocking.Value;
_enableBlocking = jsonConfig.GetProperty("enableBlocking").GetBoolean();
if (jsonConfig.dropMalformedRequests is null)
if (jsonConfig.TryGetProperty("dropMalformedRequests", out JsonElement jsonDropMalformedRequests))
_dropMalformedRequests = jsonDropMalformedRequests.GetBoolean();
else
_dropMalformedRequests = false;
else
_dropMalformedRequests = jsonConfig.dropMalformedRequests.Value;
if (jsonConfig.allowedNetworks is null)
{
_allowedNetworks = Array.Empty<NetworkAddress>();
}
else
{
List<NetworkAddress> allowedNetworks = new List<NetworkAddress>();
foreach (dynamic allowedNetwork in jsonConfig.allowedNetworks)
{
allowedNetworks.Add(NetworkAddress.Parse(allowedNetwork.Value));
}
if (jsonConfig.TryReadArray("allowedNetworks", NetworkAddress.Parse, out NetworkAddress[] allowedNetworks))
_allowedNetworks = allowedNetworks;
}
if (jsonConfig.blockedNetworks is null)
{
_blockedNetworks = Array.Empty<NetworkAddress>();
}
else
{
List<NetworkAddress> blockedNetworks = new List<NetworkAddress>();
foreach (dynamic blockedNetwork in jsonConfig.blockedNetworks)
{
blockedNetworks.Add(NetworkAddress.Parse(blockedNetwork.Value));
}
_allowedNetworks = Array.Empty<NetworkAddress>();
if (jsonConfig.TryReadArray("blockedNetworks", NetworkAddress.Parse, out NetworkAddress[] blockedNetworks))
_blockedNetworks = blockedNetworks;
}
if (jsonConfig.blockedQuestions is null)
{
_blockedQuestions = Array.Empty<BlockedQuestion>();
}
else
{
List<BlockedQuestion> blockedQuestions = new List<BlockedQuestion>();
foreach (dynamic blockedQuestion in jsonConfig.blockedQuestions)
{
blockedQuestions.Add(new BlockedQuestion(blockedQuestion));
}
_blockedNetworks = Array.Empty<NetworkAddress>();
if (jsonConfig.TryReadArray("blockedQuestions", delegate (JsonElement blockedQuestion) { return new BlockedQuestion(blockedQuestion); }, out BlockedQuestion[] blockedQuestions))
_blockedQuestions = blockedQuestions;
}
else
_blockedQuestions = Array.Empty<BlockedQuestion>();
if (jsonConfig.dropMalformedRequests is null)
if (!jsonConfig.TryGetProperty("dropMalformedRequests", out _))
{
config = config.Replace("\"allowedNetworks\"", "\"dropMalformedRequests\": false,\r\n \"allowedNetworks\"");
@@ -177,17 +146,15 @@ namespace DropRequests
#region constructor
public BlockedQuestion(dynamic jsonQuestion)
public BlockedQuestion(JsonElement jsonQuestion)
{
_name = jsonQuestion.name?.Value;
if (_name is not null)
_name = _name.TrimEnd('.');
if (jsonQuestion.TryGetProperty("name", out JsonElement jsonName))
_name = jsonName.GetString().TrimEnd('.');
if (jsonQuestion.blockZone is not null)
_blockZone = jsonQuestion.blockZone.Value;
if (jsonQuestion.TryGetProperty("blockZone", out JsonElement jsonBlockZone))
_blockZone = jsonBlockZone.GetBoolean();
string strType = jsonQuestion.type?.Value;
if (!string.IsNullOrEmpty(strType) && Enum.TryParse(strType, true, out DnsResourceRecordType type))
if (jsonQuestion.TryGetProperty("type", out JsonElement jsonType) && Enum.TryParse(jsonType.GetString(), true, out DnsResourceRecordType type))
_type = type;
else
_type = DnsResourceRecordType.Unknown;