// GET api/values/5
        public object Get(uint id)
        {
            try
            {
                IPCountryFinder ipcf = this.LoadIPDB();

                string ipv4        = this.ConvertIntToIpAddress(id);
                string countryCode = ipcf.GetCountryCode(ipv4);

                return(new
                {
                    CountryName = ipcf.ConvertCountryCodeToName(countryCode),
                    CountryCode = countryCode,
                    ServerInfo = new
                    {
                        ClientAddress = System.Web.HttpContext.Current.Request.UserHostAddress,
                        ServerAddress = System.Web.HttpContext.Current.Request.ServerVariables["LOCAL_ADDR"],
                        Version = this.GetType().Assembly.GetName().Version.ToString(),
                        QueryTime = DateTime.Now.ToString("s")
                    }
                });
            }
            catch (Exception ex)
            {
                return(ex);
            }
        }
Example #2
0
        public void ResultCheck()
        {
            //Verirfy the result by result from ip2c.org
            var finder = new IPCountryFinder("default-ipdb.csv");
            var sw     = new Stopwatch();
            var lines  = File.ReadAllLines("TestIPs.txt");

            sw.Start();
            foreach (var line in lines)
            {
                var p    = line.Split(',');
                var code = finder.GetCountryCode(p[0]);
                if (string.IsNullOrEmpty(p[1]))
                {
                    p[1] = "--";
                }

                Debug.WriteLine(p);
                Assert.AreEqual(code, p[1]);
            }
            sw.Stop();
            Debug.WriteLine($@"Time={sw.ElapsedMilliseconds:n0}ms 
Count={lines.Count()}
Average ={(double)sw.ElapsedMilliseconds / lines.Count():n2}ms");
        }
        private IPCountryFinder LoadIPDB()
        {
            string cachekey = "storage:ip2c";

            IPCountryFinder result = MemoryCache.Default.Get(cachekey) as IPCountryFinder;

            if (result == null)
            {
                string filepath = null;

                if (string.IsNullOrEmpty(filepath))
                {
                    filepath = HostingEnvironment.MapPath("~/App_Data/ipdb.csv");
                }

                if (string.IsNullOrEmpty(filepath))
                {
                    filepath = Path.Combine(
                        Path.GetDirectoryName(this.GetType().Assembly.Location),
                        "ipdb.csv");
                }

                if (string.IsNullOrEmpty(filepath) || File.Exists(filepath) == false)
                {
                    throw new FileNotFoundException("IPDB.csv file not found.", filepath);
                }

                try
                {
                    var cip = new CacheItemPolicy();
                    cip.ChangeMonitors.Add(new HostFileChangeMonitor(new List <string> {
                        filepath
                    }));

                    result = new IPCountryFinder(filepath);
                    MemoryCache.Default.Add(
                        cachekey,
                        result,
                        cip);
                }
                catch
                {
                    // can not monitor file

                    result = new IPCountryFinder(filepath);
                    MemoryCache.Default.Add(
                        cachekey,
                        result,
                        DateTimeOffset.Now.AddMinutes(60));
                }
            }

            return(result);
        }
Example #4
0
        // GET api/values/5
        public object Get(uint id)
        {
            IPCountryFinder ipcf = this.LoadIPDB();

            string ipv4        = this.ConvertIntToIpAddress(id);
            string countryCode = ipcf.GetCountryCode(ipv4);

            return(new
            {
                CountryName = ipcf.ConvertCountryCodeToName(countryCode),
                CountryCode = countryCode
            });
        }
Example #5
0
        static bool TestFile(string file)
        {
            IPCountryFinder finder = new IPCountryFinder(file);

            // add test case here
            if (finder.GetCountryCode("168.95.1.1") != "TW")
            {
                return(false);
            }


            return(true);
        }
Example #6
0
        public void FindTest()
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            var finder = new IPCountryFinder("default-ipdb.csv");

            sw.Stop();
            Assert.IsTrue(sw.ElapsedMilliseconds < 1000);

            var ci = this._client.FindIPCountry("168.95.1.1");

            Assert.AreEqual(ci.CountryCode, "TW");
            Assert.AreEqual(ci.CountryName, "Taiwan; Republic of China (ROC)");
        }
Example #7
0
        public void FindTest()
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            var finder = new IPCountryFinder("IpToCountry.csv");

            sw.Stop();
            Assert.IsTrue(sw.ElapsedMilliseconds < 1000);
            var code = finder.GetCountryCode("168.95.1.1");

            Assert.AreEqual(code, "TW");
            var name = finder.GetCountryName("168.95.1.1");

            Assert.AreEqual(name, "Taiwan; Republic of China (ROC)");
        }
        private IPCountryFinder LoadIPDB()
        {
            string cachekey = "storage:ip2c";

            IPCountryFinder result = MemoryCache.Default.Get(cachekey) as IPCountryFinder;

            if (result == null)
            {
                string filepath = HostingEnvironment.MapPath("~/App_Data/ipdb.csv");

                try
                {
                    var cip = new CacheItemPolicy();
                    cip.ChangeMonitors.Add(new HostFileChangeMonitor(new List <string> {
                        filepath
                    }));

                    result = new IPCountryFinder(filepath);
                    MemoryCache.Default.Add(
                        cachekey,
                        result,
                        cip);
                }
                catch
                {
                    // can not monitor file

                    result = new IPCountryFinder(filepath);
                    MemoryCache.Default.Add(
                        cachekey,
                        result,
                        DateTimeOffset.Now.AddMinutes(60));
                }
            }

            return(result);
        }
Example #9
0
 public void ParseTest()
 {
     var finder = new IPCountryFinder("IpToCountryBad.csv");
 }
Example #10
0
 public void LoadPathTest()
 {
     var finder = new IPCountryFinder("C:\\NOT_THERE\\IpToCountry.csv");
 }
Example #11
0
 public void LoadPathTest()
 {
     var finder = new IPCountryFinder("C:\\NOT_THERE\\default-ipdb.csv");
 }