17monipdb根据IP获得区域
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[];
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, , dataBuffer.Length);
} var indexLength = BytesToLong(dataBuffer[], dataBuffer[], dataBuffer[], dataBuffer[]);
indexBuffer = new byte[indexLength];
Array.Copy(dataBuffer, , indexBuffer, , indexLength);
offset = (int)indexLength; for (var loop = ; loop < ; loop++)
{
index[loop] = BytesToLong(indexBuffer[loop * + ], indexBuffer[loop * + ],
indexBuffer[loop * + ],
indexBuffer[loop * ]);
}
}
catch (Exception ex)
{
throw ex;
}
}
} private static uint BytesToLong(byte a, byte b, byte c, byte d)
{
return ((uint)a << ) | ((uint)b << ) | ((uint)c << ) | 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[]);
long ip2long_value = BytesToLong(byte.Parse(ips[]), byte.Parse(ips[]), byte.Parse(ips[]),
byte.Parse(ips[]));
var start = index[ip_prefix_value];
var max_comp_len = offset - ;
long index_offset = -;
var index_length = -;
byte b = ;
for (start = start * + ; start < max_comp_len; start += )
{
if (
BytesToLong(indexBuffer[start + ], indexBuffer[start + ], indexBuffer[start + ],
indexBuffer[start + ]) >= ip2long_value)
{
index_offset = BytesToLong(b, indexBuffer[start + ], indexBuffer[start + ],
indexBuffer[start + ]);
index_length = 0xFF & indexBuffer[start + ];
break;
}
}
var areaBytes = new byte[index_length];
Array.Copy(dataBuffer, offset + (int)index_offset - , areaBytes, , 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解析文件:
17monipdb根据IP获得区域的更多相关文章
- 网站定位之---根据IP获得区域
记得以前做一个培训机构网站时候需要定位,那时候用的搜狐的api,不是很精准. demo:https://github.com/dunitian/LoTCodeBase/tree/master/NetC ...
- 超好用的ip归属区域查询
源码如下: #!/usr/bin/env python#-*-coding:utf-8-*- import requestsfrom bs4 import BeautifulSoupimport re ...
- java web获取客户端外网ip和所在区域
@参考文章1.@参考文章2.@参考文章3.@参考文章4,@之前同事的项目 controller @Controller @RequestMapping("/home") publi ...
- 网络通信分享(二):外网ip和内网ip
一.内网ip包括两类: 1:tcp/ip协议中,专门保留了三个IP地址区域作为私有地址,其地址范围如下: 10.0.0.0/8:10.0.0.0-10.255.255.255 172.16.0.0/ ...
- TCP/IP详解学习笔记(6)-- IP选路
1.概述 路由算法是用于获取路由表中的路由项目.它是路由选择协议的核心. 2.路由算法的分类 从路由算法能否随网络的通信量或拓扑自适应的进行调整变化来分,可以分为两类. 静态路由选 ...
- TCP/IP——内外网IP+子网掩码作用+PING(网络总结)
目录: 1.如何区分内网IP和外网IP? 保留字段 2.子网掩码是起什么作用的? 将DNS和IP异或,表示哪段起作用 3.ping到底起什么作用? ping本地.ping远程 下面针对上面三个问题分别 ...
- IP选路
IP选路 1.概述 路由算法是用于获取路由表中的路由项目.它是路由选择协议的核心. 2.路由算法的分类 从路由算法能否随网络的通信量或拓扑自适应的进行调整变化来分,可以分为两类. ...
- ip识别运用
tcp/ip协议中,专门保留了三个IP地址区域作为私有地址,其地址范围如下: 10.0.0.0/8:10.0.0.0-10.255.255.255 172.16.0.0/12:172.16.0.0-1 ...
- PHP 获取IP地址位置信息「聚合数据API」
聚合数据 提供了[查询IP所属区域]的服务接口,只需要以 GET 请求的方式向 API 传入 IP地址 和 APPKEY 即可获得查询结果. 这里的难点主要在于如何通过PHP获取客户端IP地址,以及如 ...
随机推荐
- linux centos7 安装git
1.下载git wget https://github.com/git/git/archive/v2.14.1.zip 2.安装依赖 yum -y install zlib-devel openssl ...
- Windows10下安装pytorch并导入pycharm
1.安装Anaconda 下载:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ 安装Anaconda3,最新版本的就可以了,我安装的是5. ...
- [mysql] C++操作mysql方法总结(1)
From: http://www.cnblogs.com/magicsoar/p/3817518.html C++通过mysql的c api和通过mysql的Connector C++ 1.1.3操作 ...
- 你使用 Web 平台安装程序命令行工具
你使用 Web 平台安装程序命令行工具 获取的软件由其所有者授权给你.Microsoft 未授予你第三方软件的任何权利.已成功加载主源: https://go.microsoft.com/?linki ...
- Jmeter-Maven-Plugin高级应用:Modifying Properties
Modifying Properties Pages 12 Home Adding additional libraries to the classpath Advanced Configurati ...
- 【问题&解决】还原数据库提示“介质集有2个介质簇,但只提供了1个。必须提供所有成员”的解决办法
今天在对数据库备份与还原的过程中,我遇到一个问题“介质集有2个介质簇,但只提供了1个.必须提供所有成员”,下面详细的介绍一下遇到问题的经过与问题解决的方法! 一.备份与还原遇到的问题描述与解决方法: ...
- 推荐20个很有帮助的web前端开发教程
1. CSS Vocabulary 一个伟大的指向和点击的小应用程序,让你加快速度掌握 CSS 语法的各个不同部分,学习各个属性的正确的名称. 2. Liquidapsive 一个简单的信息化布局,通 ...
- C#.NET常见问题(FAQ)-get set属性有什么意义
使用get,set可以让类定义的更加规范,因为正常情况下,如果我们写一个自定义类,他的属性要么是public,要么是private,但是如果public的属性又要做限制,比如人年龄不允许负数,也不允许 ...
- 设置MAC 下 Vim 语法高亮显示
一:终端进入 cd /usr/share/vim/ 二: sudo vim vimrc 三:添加 syntax enablesyntax on 四:保存退出
- Java面试处理高并发
经过查资料,方案如下所示. 1 从最基础的地方做起,优化我们写的代码,减少必要的资源浪费. a.避免频繁的使用new对象,对于整个应用只需要存在一个实例的类,我们可以使用单例模 ...