public async Task <MxPollResult> Process(string domain) { Stopwatch stopwatch = Stopwatch.StartNew(); DnsResult <List <HostMxRecord> > mxHosts = await _dnsClient.GetMxRecords(domain); stopwatch.Stop(); if (!_config.AllowNullResults && (mxHosts.IsErrored || mxHosts.Value.TrueForAll(x => string.IsNullOrWhiteSpace(x.Id)))) { throw new MxPollerException($"Unable to retrieve mx hosts for {domain}."); } if (mxHosts.IsErrored) { return(new MxPollResult(domain, new Error(Id, ErrorType.Error, $"Failed MX hosts query for {domain} with error {mxHosts.Error}", string.Empty))); } if (mxHosts.IsErrored) { _log.LogError($"Processed request for MX records in {stopwatch.ElapsedMilliseconds} for domain: {domain}"); } return(new MxPollResult(domain, mxHosts.Value, stopwatch.Elapsed)); }
public async Task <SpfRecords> Process(string domain, Term term) { Mx mx = term as Mx; string mxDomain = string.IsNullOrEmpty(mx.DomainSpec.Domain) ? domain : mx.DomainSpec.Domain; DnsResult <List <string> > mxRecords = await _dnsClient.GetMxRecords(mxDomain); if (mxRecords.IsErrored) { string message = string.Format(SpfExpansionResource.FailedMxRecordQueryErrorMessage, mxDomain, mxRecords.Error); string markdown = string.Format(SpfExpansionMarkdownResource.FailedMxRecordQueryErrorMessage, mxDomain, mxRecords.Error); Guid id = Guid.Parse("464F1D16-3945-41D3-9C02-D7B781BCB363"); mx.AddError(new Error(id, ErrorType.Error, message, markdown)); } else { List <MxHost> mxHosts = new List <MxHost>(); foreach (var mxRecord in mxRecords.Value) { DnsResult <List <string> > ips = await _dnsClient.GetARecords(mxRecord); if (ips.IsErrored) { mxHosts.Add(new MxHost(mxRecord, new List <string>())); string message = string.Format(SpfExpansionResource.FailedARecordQueryErrorMessage, mxRecord, ips.Error); string markdown = string.Format(SpfExpansionMarkdownResource.FailedARecordQueryErrorMessage, mxRecord, ips.Error); Guid id = Guid.Parse("DA9C6FF2-5DD0-4AA0-BE66-2E443C93C9A2"); mx.AddError(new Error(id, ErrorType.Error, message, markdown)); } else if (ips.Value.Count > 10) { mxHosts.Add(new MxHost(mxRecord, new List <string>())); string message = string.Format(SpfExpansionResource.TooManyARecordsErrorMessage, ips.Value.Count, mxRecord); string markdown = string.Format(SpfExpansionMarkdownResource.TooManyARecordsErrorMessage, ips.Value.Count, mxRecord); Guid id = Guid.Parse("6ABDDBDE-8147-49C4-A6A2-23DCA683DFDA"); mx.AddError(new Error(id, ErrorType.Error, message, markdown)); } else { mxHosts.Add(new MxHost(mxRecord, ips.Value)); } } mx.MxHosts = mxHosts; } return(null); }
public async Task ErroredWhenRetrievingMxRecordTest() { string domain = "abc.com"; A.CallTo(() => _config.AllowNullResults).Returns(true); A.CallTo(() => _dnsClient.GetMxRecords(A <string> ._)) .Returns(new DnsResult <List <HostMxRecord> >("error")); MxPollResult result = await _mxProcessor.Process(domain); Assert.That(domain, Is.EqualTo(result.Id)); }
public async Task MxTermExpanded() { string host1 = "host1"; string host2 = "host2"; string ip1 = "192.168.1.1"; string ip2 = "192.168.1.2"; A.CallTo(() => _dnsClient.GetMxRecords(A<string>._)) .Returns(Task.FromResult(new DnsResult<List<string>> (new List<string> { host1, host2 }, 200))); A.CallTo(() => _dnsClient.GetARecords(A<string>._)) .ReturnsNextFromSequence( Task.FromResult(new DnsResult<List<string>>(new List<string> { ip1, ip2 }, 200)), Task.FromResult(new DnsResult<List<string>>(new List<string> { ip1 }, 200))); Mx mx = new Mx("", Qualifier.Pass, new DomainSpec(""), new DualCidrBlock(new Ip4CidrBlock(32), new Ip6CidrBlock(128))); SpfRecords spfRecords = await _spfMxTermExpander.Process("", mx); Assert.That(spfRecords, Is.Null); Assert.That(mx.MxHosts.Count, Is.EqualTo(2)); Assert.That(mx.MxHosts[0].Host, Is.EqualTo(host1)); Assert.That(mx.MxHosts[1].Host, Is.EqualTo(host2)); Assert.That(mx.MxHosts[0].Ip4S.Count, Is.EqualTo(2)); Assert.That(mx.MxHosts[1].Ip4S.Count, Is.EqualTo(1)); Assert.That(mx.MxHosts[0].Ip4S[0], Is.EqualTo(ip1)); Assert.That(mx.MxHosts[0].Ip4S[1], Is.EqualTo(ip2)); Assert.That(mx.MxHosts[1].Ip4S[0], Is.EqualTo(ip1)); Assert.That(mx.AllErrors, Is.Empty); }