예제 #1
0
        public async Task GetInfoForIpAsyncTest()
        {
            IpInfo info;

            info = await IpGeolocationService.GetInfoForIpAsync("208.80.152.201");

            Assert.IsNotNull(info);
            Assert.IsTrue(info.Success);
            Assert.AreEqual("US", info.CountryCode);
            Assert.That(info.City.StartsWith("San Francisco"));
            Assert.AreEqual("94105", info.ZipCode);

            info = await IpGeolocationService.GetInfoForIpAsync(IPAddress.Parse("208.80.152.201"));

            Assert.IsNotNull(info);
            Assert.IsTrue(info.Success);
            Assert.AreEqual("US", info.CountryCode);
            Assert.That(info.City.StartsWith("San Francisco"));
            Assert.AreEqual("94105", info.ZipCode);

            Assert.ThrowsAsync(typeof(ArgumentException), () => IpGeolocationService.GetInfoForIpAsync((string)null));
            Assert.ThrowsAsync(typeof(ArgumentNullException), () => IpGeolocationService.GetInfoForIpAsync((IPAddress)null));
            Assert.ThrowsAsync(typeof(ArgumentException), () => IpGeolocationService.GetInfoForIpAsync(""));
            Assert.ThrowsAsync(typeof(ArgumentException), () => IpGeolocationService.GetInfoForIpAsync(" "));
            Assert.ThrowsAsync(typeof(ArgumentException), () => IpGeolocationService.GetInfoForIpAsync("\n"));
            Assert.ThrowsAsync(typeof(ArgumentException), () => IpGeolocationService.GetInfoForIpAsync("asd"));
            Assert.ThrowsAsync(typeof(ArgumentException), () => IpGeolocationService.GetInfoForIpAsync("asd"));
            Assert.ThrowsAsync(typeof(ArgumentException), () => IpGeolocationService.GetInfoForIpAsync("555.123.123.123"));
        }
예제 #2
0
        public async void GetLocation()
        {
            Location     location;
            const string ip = "175.34.25.23";
            var          cacheRepository = new CacheRepository <Location>(_TomataboardContext);

            var ipGeolocationService = new IpGeolocationService(new StubLogger <IpGeolocationService>());

            location = await ipGeolocationService.Execute(ip);

            Assert.NotNull(location);

            var geoLiteService = new GeoLiteService(new StubLogger <GeoLiteService>(), _TomataboardContext);

            location = await geoLiteService.Execute(ip);

            Assert.NotNull(location);

            var freegeoipService = new FreegeoipService(new StubLogger <FreegeoipService>());

            location = await freegeoipService.Execute(ip);

            Assert.NotNull(location);

            //var provider = new LocationProvider(new StubLogger<LocationProvider>(), cacheRepository, ipGeolocationService, geoLiteService, freegeoipService);
            //location = await provider.Execute(ip);
            //Assert.NotNull(location);

            //// invalid ip
            //location = await provider.Execute("asdfasdF");
            //Assert.Null(location);

            //location = await provider.Execute(ip);
            //Assert.NotNull(location);
        }
예제 #3
0
        public async Task ExecuteGroupAsync(CommandContext ctx, [Description("IP.")] IPAddress ip)
        {
            var info = await IpGeolocationService.GetInfoForIpAsync(ip);

            if (!info.Success)
            {
                throw new CommandFailedException($"Retrieving IP geolocation info failed! Details: {info.ErrorMessage}");
            }

            await ctx.RespondAsync(embed : info.ToDiscordEmbed(this.ModuleColor));
        }
예제 #4
0
        public async Task IpAsync(CommandContext ctx,
                                  [Description("desc-ip")] IPAddress ip)
        {
            IpInfo?info = await IpGeolocationService.GetInfoForIpAsync(ip);

            if (info is null || !info.Success)
            {
                throw new CommandFailedException(ctx, "cmd-err-geoloc");
            }

            await ctx.RespondWithLocalizedEmbedAsync(emb => {
                emb.WithTitle(info.Ip);
                emb.WithColor(this.ModuleColor);
                emb.AddLocalizedTitleField("str-location", $"{info.City}, {info.RegionName} {info.RegionCode}, {info.CountryName} {info.CountryCode}");
                emb.AddLocalizedTitleField("str-location-exact", $"({info.Latitude} , {info.Longitude})", inline: true);
                emb.AddLocalizedTitleField("str-isp", info.Isp, inline: true);
                emb.AddLocalizedTitleField("str-org", info.Organization, inline: true);
                emb.AddLocalizedTitleField("str-as", info.As, inline: true);
                emb.WithLocalizedFooter("fmt-powered-by", null, "ip-api");
            });
        }