博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
17monipdb根据IP获得区域
阅读量:5910 次
发布时间:2019-06-19

本文共 4648 字,大约阅读时间需要 15 分钟。

https://www.ipip.net/download.html

https://github.com/17mon/csharp

 

IpAndPositionHelper
public class IpAndPositionHelper    {        #region Resource        public static bool EnableFileWatch = false;        private static int offset;        private static uint[] index = new uint[256];        private static byte[] dataBuffer;        private static byte[] indexBuffer;        private static long lastModifyTime = 0L;        private static string ipFile;        private static readonly object @lock = new object();        #endregion        #region LoadDat        public static void Load(string filename)        {            ipFile = new FileInfo(filename).FullName;            Load();            if (EnableFileWatch)            {                Watch();            }        }        private static void Load()        {            lock (@lock)            {                var file = new FileInfo(ipFile);                lastModifyTime = file.LastWriteTime.Ticks;                try                {                    dataBuffer = new byte[file.Length];                    using (var fin = new FileStream(file.FullName, FileMode.Open, FileAccess.Read))                    {                        fin.Read(dataBuffer, 0, dataBuffer.Length);                    }                    var indexLength = BytesToLong(dataBuffer[0], dataBuffer[1], dataBuffer[2], dataBuffer[3]);                    indexBuffer = new byte[indexLength];                    Array.Copy(dataBuffer, 4, indexBuffer, 0, indexLength);                    offset = (int)indexLength;                    for (var loop = 0; loop < 256; loop++)                    {                        index[loop] = BytesToLong(indexBuffer[loop * 4 + 3], indexBuffer[loop * 4 + 2],                            indexBuffer[loop * 4 + 1],                            indexBuffer[loop * 4]);                    }                }                catch (Exception ex)                {                    throw ex;                }            }        }        private static uint BytesToLong(byte a, byte b, byte c, byte d)        {            return ((uint)a << 24) | ((uint)b << 16) | ((uint)c << 8) | d;        }        private static void Watch()        {            var file = new FileInfo(ipFile);            if (file.DirectoryName == null) return;            var watcher = new FileSystemWatcher(file.DirectoryName, file.Name) { NotifyFilter = NotifyFilters.LastWrite };            watcher.Changed += (s, e) =>            {                var time = File.GetLastWriteTime(ipFile).Ticks;                if (time > lastModifyTime)                {                    Load();                }            };            watcher.EnableRaisingEvents = true;        }        #endregion        #region FindIP        public static string[] Find(string ip)        {            lock (@lock)            {                var ips = ip.Split('.');                var ip_prefix_value = int.Parse(ips[0]);                long ip2long_value = BytesToLong(byte.Parse(ips[0]), byte.Parse(ips[1]), byte.Parse(ips[2]),                    byte.Parse(ips[3]));                var start = index[ip_prefix_value];                var max_comp_len = offset - 1028;                long index_offset = -1;                var index_length = -1;                byte b = 0;                for (start = start * 8 + 1024; start < max_comp_len; start += 8)                {                    if (                        BytesToLong(indexBuffer[start + 0], indexBuffer[start + 1], indexBuffer[start + 2],                            indexBuffer[start + 3]) >= ip2long_value)                    {                        index_offset = BytesToLong(b, indexBuffer[start + 6], indexBuffer[start + 5],                            indexBuffer[start + 4]);                        index_length = 0xFF & indexBuffer[start + 7];                        break;                    }                }                var areaBytes = new byte[index_length];                Array.Copy(dataBuffer, offset + (int)index_offset - 1024, areaBytes, 0, index_length);                return Encoding.UTF8.GetString(areaBytes).Split('\t');            }        }        #endregion    }
[TestMethod()]        public void FindTest()        {            IpAndPositionHelper.EnableFileWatch = true; // 默认值为:false,如果为true将会检查ip库文件的变化自动reload数据            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "APP_Data\\tinyipdata_utf8.dat");  //合并目录            IpAndPositionHelper.Load(path);//加载            var infos = IpAndPositionHelper.Find("180.169.67.130");//查询            var result = string.Join("-", infos).TrimEnd('-');      //中国-上海        }

 

 

附IP解析文件:

 

转载地址:http://iuvpx.baihongyu.com/

你可能感兴趣的文章
Tips of ACWS Framework
查看>>
biji001
查看>>
正确删除归档日志
查看>>
Spring 3支持RESTful API/APP配置示例
查看>>
Dell R710服务器磁盘恢复数据库一例(记录)
查看>>
Zabbix 3.2.6 通过Orabbix监控Oracle数据库
查看>>
mongo shell启动配置文件.mongorc.js(二)
查看>>
document.referrer
查看>>
Coredump介绍及如何在Android中开启和使用来分析Crash等问题
查看>>
安装drbd
查看>>
如何在三年内快速成长为一名技术专家
查看>>
以css为例谈设计模式
查看>>
Good article regarding how to visualize function calls
查看>>
Android笔记(2)sqlite的用法
查看>>
第七章 Linux文件和目录相关的知识 作业题
查看>>
常用SQL语句整理中,欢迎大家补充....
查看>>
从空白开始填写
查看>>
linux下oracle10g远程安装
查看>>
SAX之错误处理类
查看>>
JUint 4 框架工具的学习,新手只需理解透这几个注解的概念就OK了!
查看>>