public void Setup() { _asInfoProvider = A.Fake <IAsInfoProvider>(); _blocklistProvider = A.Fake <IBlocklistProvider>(); _reverseDnsProvider = A.Fake <IReverseDnsProvider>(); _log = A.Fake <ILogger <IpAddressLookup> >(); _clock = A.Fake <IClock>(); _ipAddressLookup = new IpAddressLookup(_asInfoProvider, _blocklistProvider, _reverseDnsProvider, _clock, _log); A.CallTo(() => _blocklistProvider.GetBlocklistAppearances(A <List <string> > ._)).ReturnsLazily((List <string> arguments) => { return(Task.FromResult(arguments.Select(x => new BlocklistResult(x, new List <BlocklistAppearance> { new BlocklistAppearance("flag", "source", Guid.NewGuid().ToString()) }) ).ToList())); }); A.CallTo(() => _reverseDnsProvider.GetReverseDnsResult(A <List <string> > ._)).ReturnsLazily((List <string> arguments) => { return(Task.FromResult(arguments.Select(x => new ReverseDnsResult(x, new List <ReverseDnsResponse> { new ReverseDnsResponse(Guid.NewGuid().ToString(), new List <string>()) })).ToList())); }); A.CallTo(() => _asInfoProvider.GetAsInfo(A <List <string> > ._)).ReturnsLazily((List <string> arguments) => { return(Task.FromResult(arguments.Select(x => new AsInfo { IpAddress = x, Description = Guid.NewGuid().ToString() }).ToList())); }); A.CallTo(() => _clock.GetDateTimeUtc()).Returns(new DateTime(1999, 01, 01)); }
public async Task <List <IpAddressDetails> > Lookup(List <IpAddressDetailsRequest> ipAddressDetailsRequests) { DateTime nowUtc = _clock.GetDateTimeUtc(); List <string> distinctIpAddresses = ipAddressDetailsRequests .Select(x => x.IpAddress).Distinct().ToList(); List <string> blockListIpAddressesToLookUp = ipAddressDetailsRequests .Where(x => x.Date >= nowUtc.Date.AddDays(BlocklistLookupWindowDays)) .Select(x => x.IpAddress).Distinct().ToList(); _log.LogDebug($"IpAddressLookup received request for {ipAddressDetailsRequests.Count} IpAddressDetails containing {distinctIpAddresses.Count} distinct IP addresses and {blockListIpAddressesToLookUp.Count} distinct IP addresses within blocklist window"); Task <List <BlocklistResult> > blocklistResultsTask = _blocklistProvider.GetBlocklistAppearances(blockListIpAddressesToLookUp); Task <List <AsInfo> > asInfoTask = _asInfoProvider.GetAsInfo(distinctIpAddresses); Task <List <ReverseDnsResult> > reverseDnsResponsesTask = _reverseDnsProvider.GetReverseDnsResult(distinctIpAddresses); await Task.WhenAll(blocklistResultsTask, asInfoTask, reverseDnsResponsesTask); List <BlocklistResult> blocklistResults = await blocklistResultsTask; List <AsInfo> asInfos = await asInfoTask; List <ReverseDnsResult> reverseDnsResponses = await reverseDnsResponsesTask; Dictionary <string, BlocklistResult> blocklistResultsDictionary = blocklistResults.Where(x => x.IpAddress != null).ToDictionary(x => x.IpAddress); Dictionary <string, AsInfo> asInfoDictionary = asInfos.Where(x => x.IpAddress != null).ToDictionary(x => x.IpAddress); Dictionary <string, ReverseDnsResult> reverseDnsResponseDictionary = reverseDnsResponses.Where(x => x.OriginalIpAddress != null).ToDictionary(x => x.OriginalIpAddress); List <IpAddressDetails> responses = ipAddressDetailsRequests.Select(request => { bool blockListResultHasValue = blocklistResultsDictionary.TryGetValue(request.IpAddress, out BlocklistResult blockListResult); bool asInfoHasValue = asInfoDictionary.TryGetValue(request.IpAddress, out AsInfo asInfo); bool reverseDnsResponseHasValue = reverseDnsResponseDictionary.TryGetValue(request.IpAddress, out ReverseDnsResult reverseDnsResult); return(new IpAddressDetails( request.IpAddress, request.Date, asInfoHasValue ? asInfo.AsNumber : 0, asInfoHasValue ? asInfo.Description : "", asInfoHasValue ? asInfo.CountryCode : "", blockListResultHasValue ? blockListResult.BlocklistAppearances : null, reverseDnsResponseHasValue ? reverseDnsResult.ForwardResponses : null, nowUtc.AddDays(AsnDataAgeDays).Date, nowUtc, nowUtc)); }).ToList(); _log.LogInformation($"Returning {responses.Count} IpAddressDetails from request for {ipAddressDetailsRequests.Count}"); return(responses); }
private async Task <ReverseDnsResult> Lookup(string ip) { return((await _reverseDnsProvider.GetReverseDnsResult(new List <string> { ip })).FirstOrDefault()); }
public async Task Test() { List <ReverseDnsResult> reverseDnsResult = await _reverseDnsProvider.GetReverseDnsResult(new List <string> { Ip }); }