Beispiel #1
0
        public void GetGeoLocationData_InvalidInputAddress_ExceptionIsThrown(string address)
        {
            //Arrange
            var ipStackService = new IpStackService(_configuration);

            //Assert
            Assert.Throws <UriFormatException>(() => ipStackService.GetIpAddressDetails(address));
        }
 public VisitorTrackingService(IHttpContextAccessor httpContextAccessor,
                               FairplaytubeDatabaseContext fairplaytubeDatabaseContext,
                               IpStackService ipStackService, IpDataService ipDataService)
 {
     this.HttpContextAccessor         = httpContextAccessor;
     this.FairplaytubeDatabaseContext = fairplaytubeDatabaseContext;
     this.IpStackService = ipStackService;
     this.IpDataService  = ipDataService;
 }
Beispiel #3
0
        public void GetIpStackTest()
        {
            var ipStack = IpStack.FromJson(IpStackService.GetIpStack("178.235.146.51").Result);

            Assert.AreEqual("Gdynia", ipStack.City);
            Assert.AreEqual("Poland", ipStack.CountryName);
            Assert.IsTrue(54.483871459960938 == ipStack.Latitude);
            Assert.IsTrue(18.464729309082031 == ipStack.Longitude);
            Assert.AreEqual("178.235.146.51", ipStack.Ip);
        }
Beispiel #4
0
        public static ResultCode AddGeolocation(string ipOrUrl)
        {
            if (GeolocationDao.GetGeolocationByIpOrHost(ipOrUrl) != null)
            {
                return(ResultCode.RecordAlreadyExists);
            }
            string response = null;

            try
            {
                response = IpStackService.GetIpStack(ipOrUrl).Result;
            }
            catch (Exception e)
            {
                log.Error("Failed to get geolocation from IPStack", e);
                return(ResultCode.ServiceUnavailable);
            }
            if (response == null)
            {
                return(ResultCode.ServiceUnavailable);
            }
            IpStack ipStack = null;

            try
            {
                ipStack = IpStack.FromJson(response);
            }
            catch (Exception e)
            {
                log.Error("Failed to parse json from IPStack", e);
                return(ResultCode.UnexpectedError);
            }
            if (ipStack.Latitude == null || ipStack.Longitude == null)
            {
                return(ResultCode.UrlNotFound);
            }
            var geolocation = IpStackService.GetGeolocationFromIpStack(ipStack, ipOrUrl);

            try
            {
                if (GeolocationDao.Insert(geolocation))
                {
                    return(ResultCode.OK);
                }
            }
            catch (Exception)
            {
                return(ResultCode.DatabaseError);
            }
            return(ResultCode.UnexpectedError);
        }
Beispiel #5
0
        public void GetGeoLocationData_ValidIpAddress_SpecialFieldsSelected_GeoLocationDataForSelectedFieldsReturned(string address)
        {
            //Arrange
            var ipStackService = new IpStackService(_configuration);

            //Action
            var result = ipStackService.GetIpAddressDetails(address, includeHostname: true, includeSecurity: true, language: "en");

            //Assert
            Assert.Multiple(() =>
            {
                Assert.IsNotEmpty(result.Hostname);
                Assert.IsNull(result.Security);     // THIS should be null because of free subscription (security is not available)
            });
        }
Beispiel #6
0
        public void GetGeoLocationData_ValidIpAddress_OnlySelectedFields_GeoLocationDataForSelectedFieldsReturned(string address)
        {
            //Arrange
            var ipStackService = new IpStackService(_configuration);

            //Action
            var result = ipStackService.GetIpAddressDetails(address, "ip,country_code,location.capital");

            //Assert
            Assert.Multiple(() =>
            {
                Assert.IsNotEmpty(result.Ip);
                Assert.IsNotEmpty(result.CountryCode);
                Assert.IsNotEmpty(result.Location.Capital);
            });
        }
Beispiel #7
0
        public void GetGeoLocationData_ValidAddress_AllFields_GeoLocationDataForALLNonPaidSubscriptionReturned(string address)
        {
            //Arrange
            var ipStackService = new IpStackService(_configuration);

            //Action
            var result = ipStackService.GetIpAddressDetails(address);

            //Assert
            Assert.Multiple(() =>
            {
                Assert.IsNotEmpty(result.City);
                Assert.IsNotEmpty(result.Ip);
                Assert.IsNotNull(result.Latitude);
                Assert.IsNotNull(result.Longitude);
            });
        }
        public async Task <ActionResult> Get(IpViewModel inputModel)
        {
            if (ModelState.IsValid)
            {
                IpCache cache = new IpCache();

                IpViewModel ipViewModel;

                if (cache.TryGetFromCache(inputModel.IP, out IpViewModel model))
                {
                    ipViewModel = model;

                    return(View("Index", ipViewModel));
                }


                //try ip2c Service
                int tries = 0;

                while (tries < 3)
                {
                    try
                    {
                        IService ip2cService = new Ip2cService();

                        ipViewModel = await ip2cService.TryGetIpViewModel(inputModel.IP);

                        ipViewModel.ProviderOrCacheUsed = "Ip2c";

                        return(View("Index", ipViewModel));
                    }
                    catch (TaskCanceledException)
                    {
                        tries += 1;
                    }
                    catch (HttpStatusCodeNotOKException)
                    {
                        tries += 1;
                    }
                    catch (IpIsValidButDoesNotCorrespondToAnyCountryException)
                    {
                        ViewData["ErrorMessage"] = "This Ip is valid but does not correspond to any country";
                        return(View("Index"));
                    }
                    catch (Exception)
                    {
                        tries += 1;
                    }
                }

                // try ipStack Service
                tries = 0;

                while (tries < 3)
                {
                    try
                    {
                        IService ipStackService = new IpStackService();

                        ipViewModel = await ipStackService.TryGetIpViewModel(inputModel.IP);

                        ipViewModel.ProviderOrCacheUsed = "IpStack";

                        return(View("Index", ipViewModel));
                    }
                    catch (TaskCanceledException)
                    {
                        tries += 1;
                    }
                    catch (HttpStatusCodeNotOKException)
                    {
                        tries += 1;
                    }
                    catch (IpIsValidButDoesNotCorrespondToAnyCountryException)
                    {
                        ViewData["ErrorMessage"] = "This Ip is valid but does not correspond to any country";
                        return(View("Index"));
                    }
                    catch (Exception)
                    {
                        tries += 1;
                    }
                }

                // this far all have failed so return null model
                ViewData["ErrorMessage"] = "Services Failed";
                return(View("Index"));
            }
            else
            {
                return(View("Index"));
            }
        }