Esempio n. 1
0
        /// <summary>
        /// 初始化方法,必须先调用这个方法进行初始化
        /// </summary>
        /// <param name="dataPath">纯真IP数据库文件地址</param>
        public static void Initialize(string dataPath)
        {
            if (dataPath == _instanceFilePath)
            {
                return;
            }

            //throw new Exception("请不要反复初始化同一文件路径");
            _instanceFilePath = dataPath;

            var loc = new IPLocator();

            using (FileStream fs = File.OpenRead(dataPath))
            {
                loc.data = new byte[fs.Length];
                fs.Read(loc.data, 0, loc.data.Length);
            }
            byte[] buffer = new byte[8];
            Array.Copy(loc.data, 0, buffer, 0, 8);
            loc._firstStartIpOffset = ((buffer[0] + (buffer[1] * 0x100)) + ((buffer[2] * 0x100) * 0x100)) + (((buffer[3] * 0x100) * 0x100) * 0x100);
            loc._lastStartIpOffset  = ((buffer[4] + (buffer[5] * 0x100)) + ((buffer[6] * 0x100) * 0x100)) + (((buffer[7] * 0x100) * 0x100) * 0x100);
            loc.Count = Convert.ToInt64((((loc._lastStartIpOffset - loc._firstStartIpOffset)) / 7d));

            if (loc.Count <= 1L)
            {
                throw new ArgumentException("IP File DataError.");
            }

            _instance = loc;
        }
Esempio n. 2
0
        private void btnImport_Click(object sender, EventArgs e)
        {
            string ipfile = txtIPFile.Text.Trim();

            if (!File.Exists(ipfile))
            {
                MessageBox.Show("未指定IP纯真库文件,或指定的文件不存在");
                return;
            }
            try
            {
                IPLocator.Query("127.0.0.1", ipfile);
            }
            catch (Exception)
            {
                MessageBox.Show("指定的IP纯真库文件格式有误");
                return;
            }
            string constr = txtConstr.Text.Trim();

            if (string.IsNullOrEmpty(constr))
            {
                MessageBox.Show("请输入数据库连接串");
                return;
            }
            btnImport.Enabled = false;
            ThreadPool.UnsafeQueueUserWorkItem(state =>
            {
                try
                {
                    DateTime now  = DateTime.Now;
                    string tbname = "ip_" + now.ToString("yyyyMMddHHmmss");
                    IPLocator.ExportToSqlServer(constr, tbname);
                    Utility.InvokeControl(btnImport, () =>
                    {
                        btnImport.Enabled = true;
                        string msg        = "导入成功,新建的表名:" + tbname + "耗时(ms):" +
                                            (DateTime.Now - now).TotalMilliseconds.ToString("N0");
                        MessageBox.Show(msg);
                        txtRet.Text = msg + "\r\n\r\n" + txtRet.Text;
                    });
                }
                catch (Exception exp)
                {
                    MessageBox.Show("导入出错:" + exp);
                }
            }, null);
        }
Esempio n. 3
0
        private void btnSearch_Click(object sender, EventArgs e)
        {
            string ipfile = txtIPFile.Text.Trim();

            if (!File.Exists(ipfile))
            {
                MessageBox.Show("未指定IP纯真库文件,或指定的文件不存在");
                return;
            }
            string ip = txtIP.Text.Trim(new char[] { ';', ',', ':', '.' });

            ip = ip.Trim();
            if (string.IsNullOrEmpty(ip))
            {
                MessageBox.Show("请输入IP");
                return;
            }
            long lIP;

            if (long.TryParse(ip, out lIP))
            {
                ip = IPLocator.ConvertLongToIP(lIP);
            }
            if (!Regex.IsMatch(ip, @"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"))
            {
                MessageBox.Show("请输入正确格式的IP");
                return;
            }
            try
            {
                IPLocation location = IPLocator.Query(ip, ipfile);
                txtRet.Text = "  IP: " + location.IP + "\r\n" +
                              "整型IP: " + IPLocator.ConvertIpToLong(location.IP) + "\r\n" +
                              "  地址: " + location.Country + "\r\n" +
                              "  其它: " + location.Area + "\r\n\r\n" + txtRet.Text;
            }
            catch (Exception exp)
            {
                txtRet.Text = "出错了:\r\n" + exp + "\r\n\r\n" + txtRet.Text;
            }
        }
Esempio n. 4
0
        /// <summary>
        /// 查询并返回指定IPv4的实体,null表示不是正确IP,或属于环回地址,或属于局域网地址
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static IPLocation Query(string ip)
        {
            IPLocator objLocator = Instance;

            IPLocation ipLocation = new IPLocation()
            {
                IP = ip
            };

            IPAddress ipa;

            if (!IPAddress.TryParse(ip, out ipa))
            {
                ipLocation.Country = "非法IP地址";
                return(ipLocation);
            }

            if (IPAddress.IsLoopback(ipa))
            {
                ipLocation.Country = "本地环回地址";
                return(ipLocation);
            }
            if (IsLocalNetwork(ipa))
            {
                ipLocation.Country = "局域网地址";
                return(ipLocation);
            }

            long intIP = ConvertIpToLong(ip);

            long right = objLocator.Count;
            long left  = 0L;
            long middle;
            long startIp;
            long endIpOff;
            long endIp;
            int  countryFlag;

            while (left < (right - 1L))
            {
                middle  = (right + left) / 2L;
                startIp = objLocator.GetStartIp(middle, out endIpOff);
                if (intIP == startIp)
                {
                    left = middle;
                    break;
                }
                if (intIP > startIp)
                {
                    left = middle;
                }
                else
                {
                    right = middle;
                }
            }
            startIp = objLocator.GetStartIp(left, out endIpOff);
            endIp   = objLocator.GetEndIp(endIpOff, out countryFlag);
            if ((startIp <= intIP) && (endIp >= intIP))
            {
                string local;
                ipLocation.Country = objLocator.GetCountry(endIpOff, countryFlag, out local);
                ipLocation.Area    = local;
            }
            else
            {
                ipLocation.Country = "未知国家";
                ipLocation.Area    = "未知地区";
            }
            return(ipLocation);
        }