diff options
author | Wouter Tinus <win.acme.simple@gmail.com> | 2019-12-10 09:28:53 +0100 |
---|---|---|
committer | Wouter Tinus <win.acme.simple@gmail.com> | 2019-12-10 09:28:53 +0100 |
commit | f81bc94027f865727105e095ee2b9ef0dd478b3d (patch) | |
tree | 89c9f7271898f4ea157c1749f0248320f30e796b /src | |
parent | 8c2f653751338974e875ba75e47791271f62f1c5 (diff) | |
download | letsencrypt-win-simple-f81bc94027f865727105e095ee2b9ef0dd478b3d.zip letsencrypt-win-simple-f81bc94027f865727105e095ee2b9ef0dd478b3d.tar.gz letsencrypt-win-simple-f81bc94027f865727105e095ee2b9ef0dd478b3d.tar.bz2 |
Make DomainParseService respect proxy settings
Diffstat (limited to 'src')
-rw-r--r-- | src/main.lib/Services/DomainParseService.cs | 83 | ||||
-rw-r--r-- | src/main.lib/Services/ProxyService.cs | 4 |
2 files changed, 57 insertions, 30 deletions
diff --git a/src/main.lib/Services/DomainParseService.cs b/src/main.lib/Services/DomainParseService.cs index d9d28a0..e3fa80a 100644 --- a/src/main.lib/Services/DomainParseService.cs +++ b/src/main.lib/Services/DomainParseService.cs @@ -1,27 +1,28 @@ using Nager.PublicSuffix; using System; +using System.Collections.Generic; using System.IO; +using System.Net.Http; using System.Threading.Tasks; namespace PKISharp.WACS.Services { public class DomainParseService { + private const string Source = "https://publicsuffix.org/list/public_suffix_list.dat"; private readonly DomainParser _parser; - public DomainParseService(ILogService log, ISettingsService settings) + public DomainParseService(ILogService log, ProxyService proxy, ISettingsService settings) { + var path = Path.Combine(Path.GetDirectoryName(settings.ExePath), "public_suffix_list.dat"); + _parser = new DomainParser(new FileTldRuleProvider(path)); try { - _parser = new DomainParser(new WebTldRuleProvider(cacheProvider: new FileCacheProvider(settings))); + _parser = new DomainParser(new WebTldRuleProvider(proxy, settings)); } catch (Exception ex) { - log.Warning("Error retrieving public suffix list from https://publicsuffix.org/list/public_suffix_list.dat: {ex}", ex.Message); - } - if (_parser == null) - { - _parser = new DomainParser(new WebTldRuleProvider(cacheProvider: new StaticCacheProvider(settings))); + log.Warning("Error updating public suffix list from {source}: {ex}", Source, ex.Message); } } @@ -30,27 +31,6 @@ namespace PKISharp.WACS.Services public string GetSubDomain(string fulldomain) => _parser.Get(fulldomain).SubDomain; /// <summary> - /// Load re-distributed file from program directory, which is always valid so that no outside - /// request is needed. Also does not process any updates. - /// </summary> - private class StaticCacheProvider : ICacheProvider - { - private readonly string _value; - - public StaticCacheProvider(ISettingsService settings) - { - var path = Path.Combine(Path.GetDirectoryName(settings.ExePath), "public_suffix_list.dat"); - _value = File.Exists(path) ? File.ReadAllText(path) : string.Empty; - } - - public Task<string> GetAsync() => Task.FromResult(_value); - - public bool IsCacheValid() => true; - - public Task SetAsync(string val) => Task.CompletedTask; - } - - /// <summary> /// Regular 7 day file cache in the configuration folder /// </summary> private class FileCacheProvider : ICacheProvider @@ -69,5 +49,52 @@ namespace PKISharp.WACS.Services public Task SetAsync(string val) => File.WriteAllTextAsync(_file.FullName, val); } + + /// <summary> + /// Custom implementation so that we can use the proxy + /// that the user has configured and + /// </summary> + private class WebTldRuleProvider : ITldRuleProvider + { + private readonly string _fileUrl; + private readonly ProxyService _proxy; + private readonly ICacheProvider _cache; + + public WebTldRuleProvider(ProxyService proxy, ISettingsService settings) + { + _fileUrl = "https://publicsuffix.org/list/public_suffix_list.dat"; + _cache = new FileCacheProvider(settings); + _proxy = proxy; + } + + public async Task<IEnumerable<TldRule>> BuildAsync() + { + var ruleParser = new TldRuleParser(); + string ruleData; + if (!_cache.IsCacheValid()) + { + ruleData = await LoadFromUrl(_fileUrl).ConfigureAwait(false); + await _cache.SetAsync(ruleData).ConfigureAwait(false); + } + else + { + ruleData = await _cache.GetAsync().ConfigureAwait(false); + } + var rules = ruleParser.ParseRules(ruleData); + return rules; + } + + public async Task<string> LoadFromUrl(string url) + { + using var httpClient = _proxy.GetHttpClient(); + using var response = await httpClient.GetAsync(url).ConfigureAwait(false); + if (!response.IsSuccessStatusCode) + { + throw new RuleLoadException($"Cannot load from {url} {response.StatusCode}"); + } + return await response.Content.ReadAsStringAsync().ConfigureAwait(false); + } + } + } } diff --git a/src/main.lib/Services/ProxyService.cs b/src/main.lib/Services/ProxyService.cs index bf7e73d..49bf293 100644 --- a/src/main.lib/Services/ProxyService.cs +++ b/src/main.lib/Services/ProxyService.cs @@ -4,10 +4,10 @@ using System.Net.Http; namespace PKISharp.WACS.Services { - internal class ProxyService + public class ProxyService { private readonly ILogService _log; - private IWebProxy _proxy; + private IWebProxy? _proxy; private readonly ISettingsService _settings; public ProxyService(ILogService log, ISettingsService settings) |