From 2a8ea3eea52534dc5bddb5b359a7af19ad5f3928 Mon Sep 17 00:00:00 2001 From: Shreyas Zare Date: Sat, 17 Dec 2022 13:53:29 +0530 Subject: [PATCH] AdvancedBlocking: removed newtonsoft --- Apps/AdvancedBlockingApp/App.cs | 111 ++++++++++++-------------------- 1 file changed, 40 insertions(+), 71 deletions(-) diff --git a/Apps/AdvancedBlockingApp/App.cs b/Apps/AdvancedBlockingApp/App.cs index 1cd2e73d..7d38b34e 100644 --- a/Apps/AdvancedBlockingApp/App.cs +++ b/Apps/AdvancedBlockingApp/App.cs @@ -18,7 +18,6 @@ along with this program. If not, see . */ using DnsServerCore.ApplicationCommon; -using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; @@ -27,9 +26,11 @@ using System.Net.Http; using System.Net.Sockets; using System.Security.Cryptography; using System.Text; +using System.Text.Json; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; +using TechnitiumLibrary; using TechnitiumLibrary.Net; using TechnitiumLibrary.Net.Dns; using TechnitiumLibrary.Net.Dns.ResourceRecords; @@ -252,29 +253,21 @@ namespace AdvancedBlocking _soaRecord = new DnsSOARecordData(_dnsServer.ServerDomain, "hostadmin@" + _dnsServer.ServerDomain, 1, 14400, 3600, 604800, 60); _nsRecord = new DnsNSRecordData(_dnsServer.ServerDomain); - dynamic jsonConfig = JsonConvert.DeserializeObject(config); + using JsonDocument jsonDocument = JsonDocument.Parse(config); + JsonElement jsonConfig = jsonDocument.RootElement; - _enableBlocking = jsonConfig.enableBlocking.Value; - _blockListUrlUpdateIntervalHours = Convert.ToInt32(jsonConfig.blockListUrlUpdateIntervalHours.Value); + _enableBlocking = jsonConfig.GetProperty("enableBlocking").GetBoolean(); + _blockListUrlUpdateIntervalHours = jsonConfig.GetProperty("blockListUrlUpdateIntervalHours").GetInt32(); + + _networkGroupMap = jsonConfig.ReadObjectAsMap("networkGroupMap", delegate (string network, JsonElement jsonGroup) + { + if (!NetworkAddress.TryParse(network, out NetworkAddress networkAddress)) + throw new InvalidOperationException("Network group map contains an invalid network address: " + network); + + return new Tuple(networkAddress, jsonGroup.GetString()); + }); { - Dictionary networkGroupMap = new Dictionary(); - - foreach (dynamic jsonProperty in jsonConfig.networkGroupMap) - { - string network = jsonProperty.Name; - string group = jsonProperty.Value; - - if (NetworkAddress.TryParse(network, out NetworkAddress networkAddress)) - networkGroupMap.Add(networkAddress, group); - } - - _networkGroupMap = networkGroupMap; - } - - { - Dictionary groups = new Dictionary(); - Dictionary allAllowListZones = new Dictionary(0); Dictionary allBlockListZones = new Dictionary(0); @@ -283,11 +276,9 @@ namespace AdvancedBlocking Dictionary allAdBlockListZones = new Dictionary(0); - foreach (dynamic jsonGroup in jsonConfig.groups) + _groups = jsonConfig.ReadArrayAsMap("groups", delegate (JsonElement jsonGroup) { Group group = new Group(this, jsonGroup); - if (!groups.TryAdd(group.Name, group)) - continue; foreach (Uri allowListUrl in group.AllowListUrls) { @@ -343,9 +334,9 @@ namespace AdvancedBlocking allAdBlockListZones.Add(adblockListUrl, new AdBlockList(_dnsServer, adblockListUrl)); } } - } - _groups = groups; + return new Tuple(group.Name, group); + }); _allAllowListZones = allAllowListZones; _allBlockListZones = allBlockListZones; @@ -600,22 +591,23 @@ namespace AdvancedBlocking #region constructor - public Group(App app, dynamic jsonGroup) + public Group(App app, JsonElement jsonGroup) { _app = app; - _name = jsonGroup.name.Value; - _enableBlocking = jsonGroup.enableBlocking.Value; - _allowTxtBlockingReport = jsonGroup.allowTxtBlockingReport.Value; - _blockAsNxDomain = jsonGroup.blockAsNxDomain.Value; + _name = jsonGroup.GetProperty("name").GetString(); + _enableBlocking = jsonGroup.GetProperty("enableBlocking").GetBoolean(); + _allowTxtBlockingReport = jsonGroup.GetProperty("allowTxtBlockingReport").GetBoolean(); + _blockAsNxDomain = jsonGroup.GetProperty("blockAsNxDomain").GetBoolean(); { + JsonElement jsonBlockingAddresses = jsonGroup.GetProperty("blockingAddresses"); List aRecords = new List(); List aaaaRecords = new List(); - foreach (dynamic jsonBlockingAddress in jsonGroup.blockingAddresses) + foreach (JsonElement jsonBlockingAddress in jsonBlockingAddresses.EnumerateArray()) { - string strAddress = jsonBlockingAddress.Value; + string strAddress = jsonBlockingAddress.GetString(); if (IPAddress.TryParse(strAddress, out IPAddress address)) { @@ -636,59 +628,36 @@ namespace AdvancedBlocking _aaaaRecords = aaaaRecords; } - _allowed = ReadJsonDomainArray(jsonGroup.allowed); - _blocked = ReadJsonDomainArray(jsonGroup.blocked); - _allowListUrls = ReadJsonUrlArray(jsonGroup.allowListUrls); - _blockListUrls = ReadJsonUrlArray(jsonGroup.blockListUrls); + _allowed = jsonGroup.ReadArrayAsMap("allowed", GetMapEntry); + _blocked = jsonGroup.ReadArrayAsMap("blocked", GetMapEntry); + _allowListUrls = jsonGroup.ReadArray("allowListUrls", GetUriEntry); + _blockListUrls = jsonGroup.ReadArray("blockListUrls", GetUriEntry); - _allowedRegex = ReadJsonRegexArray(jsonGroup.allowedRegex); - _blockedRegex = ReadJsonRegexArray(jsonGroup.blockedRegex); - _regexAllowListUrls = ReadJsonUrlArray(jsonGroup.regexAllowListUrls); - _regexBlockListUrls = ReadJsonUrlArray(jsonGroup.regexBlockListUrls); + _allowedRegex = jsonGroup.ReadArray("allowedRegex", GetRegexEntry); + _blockedRegex = jsonGroup.ReadArray("blockedRegex", GetRegexEntry); + _regexAllowListUrls = jsonGroup.ReadArray("regexAllowListUrls", GetUriEntry); + _regexBlockListUrls = jsonGroup.ReadArray("regexBlockListUrls", GetUriEntry); - _adblockListUrls = ReadJsonUrlArray(jsonGroup.adblockListUrls); + _adblockListUrls = jsonGroup.ReadArray("adblockListUrls", GetUriEntry); } #endregion #region private - private static IReadOnlyDictionary ReadJsonDomainArray(dynamic jsonDomainArray) + private static Tuple GetMapEntry(JsonElement jsonElement) { - Dictionary domains = new Dictionary(jsonDomainArray.Count); - - foreach (dynamic jsonDomain in jsonDomainArray) - domains.TryAdd(jsonDomain.Value, null); - - return domains; + return new Tuple(jsonElement.GetString(), null); } - private static IReadOnlyList ReadJsonRegexArray(dynamic jsonRegexArray) + private static Uri GetUriEntry(string uriString) { - List regices = new List(jsonRegexArray.Count); - - foreach (dynamic jsonRegex in jsonRegexArray) - { - string regexPattern = jsonRegex.Value; - - regices.Add(new Regex(regexPattern, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled)); - } - - return regices; + return new Uri(uriString); } - private static IReadOnlyList ReadJsonUrlArray(dynamic jsonUrlArray) + private static Regex GetRegexEntry(string pattern) { - List urls = new List(jsonUrlArray.Count); - - foreach (dynamic jsonUrl in jsonUrlArray) - { - string strUrl = jsonUrl.Value; - - urls.Add(new Uri(strUrl)); - } - - return urls; + return new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled); } #endregion