From 2db9e9ee83961204aae2167c1e35f6b4c807c65a Mon Sep 17 00:00:00 2001 From: Shreyas Zare Date: Sat, 17 Feb 2024 17:22:29 +0530 Subject: [PATCH] AdvancedForwarding: updated adguard upstream implementation to support multiple forwarders. --- Apps/AdvancedForwardingApp/App.cs | 49 ++++++++++++++++++- .../adguard-upstreams.txt | 2 +- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/Apps/AdvancedForwardingApp/App.cs b/Apps/AdvancedForwardingApp/App.cs index 58b06bc0..c3044587 100644 --- a/Apps/AdvancedForwardingApp/App.cs +++ b/Apps/AdvancedForwardingApp/App.cs @@ -547,6 +547,8 @@ namespace AdvancedForwarding { #region variables + static readonly char[] _popWordSeperator = new char[] { ' ' }; + readonly IDnsServer _dnsServer; readonly string _name; @@ -666,7 +668,28 @@ namespace AdvancedForwarding } else { - forwardings.Add(new Forwarding(domains, NameServerAddress.Parse(forwarder), _dnssecValidation, _configProxyServer)); + List forwarderRecords = new List(); + string word = PopWord(ref forwarder); + + while (word.Length > 0) + { + string nextWord = PopWord(ref forwarder); + + if (nextWord.StartsWith('(')) + { + word += " " + nextWord; + nextWord = PopWord(ref forwarder); + } + + forwarderRecords.Add(GetForwarderRecord(NameServerAddress.Parse(word), _dnssecValidation, _configProxyServer)); + + word = nextWord; + } + + if (forwarderRecords.Count == 0) + throw new FormatException("Invalid AdGuard Upstreams config file format: missing upstream servers."); + + forwardings.Add(new Forwarding(forwarderRecords, domains)); } } else @@ -689,6 +712,30 @@ namespace AdvancedForwarding } } + private static string PopWord(ref string line) + { + if (line.Length == 0) + return line; + + line = line.TrimStart(_popWordSeperator); + + int i = line.IndexOfAny(_popWordSeperator); + string word; + + if (i < 0) + { + word = line; + line = ""; + } + else + { + word = line.Substring(0, i); + line = line.Substring(i + 1); + } + + return word; + } + #endregion #region public diff --git a/Apps/AdvancedForwardingApp/adguard-upstreams.txt b/Apps/AdvancedForwardingApp/adguard-upstreams.txt index 77674a51..a99f408f 100644 --- a/Apps/AdvancedForwardingApp/adguard-upstreams.txt +++ b/Apps/AdvancedForwardingApp/adguard-upstreams.txt @@ -4,7 +4,7 @@ # Example: # 8.8.8.8 # udp://9.9.9.9 -# [/host.com/example.com/]tls://1.1.1.1 +# [/host.com/example.com/]https://cloudflare-dns.com/dns-query (1.1.1.1) tls://1.1.1.1 # [/maps.host.com/]# # [/home/]192.168.10.2 # [/test.com/]https://dns.quad9.net/dns-query (9.9.9.9)