当前位置:网站首页>Realize the IP address home display function and number home query
Realize the IP address home display function and number home query
2022-07-07 13:31:00 【-edui】
adopt IP How to get an address
IP The address itself has no positioning function ,IP The address is only used to determine the relative position of a network host in the network , This position is relatively , Can pass IP Route to the destination host , It doesn't care where the physical location of the destination host is . So theoretically, it is impossible to locate IP Physical location of the address . So we have to find another way . Briefly introduce two
1、 That is through the autonomous system (AS). Simple understanding ,IP It is globally unified , With global uniqueness , In order to ensure uniqueness, we must make unified distribution , Every need in the world IP All people go to the unified distribution system to apply , That won 't do either , This unified system must be busy . So delegate the power of this distribution , Will a batch of IP Assigned to an organization , This organization will be further allocated to the next level organization ……. Who took this batch IP The address needs to be registered , When ranking, you can register the address of an organization casually , Why , Exposed the physical address . Therefore, you can obtain this batch through the registered information IP Approximate location of the address . For example, an organization applied IP The address is 1.4.127.0-1.4.127.255, By querying the registration information , Found this organization in Guangdong , So if one IP In this batch IP In the address, you can know this IP Guangdong, where it belongs .
2、 By constantly collecting location information . although IP It has no positioning function , But use IP The host of has positioning function , Such as APP Get location information at the same time IP Information , Then upload it to the server and save it , So you can build IP The physical location information base of the address .
The first one is , The registration information will not be open to everyone , It belongs to private data . The second is more , Mobile phone itself is related to user privacy , Besides, it is difficult to collect , Less people use, less information . There are still many websites to check IP Where the address belongs , Baidu comes out with a lot of . because IP The physical location of is relatively small , So some directly provide all IP All data mapped to physical address .
Realization IP Belonging display
Adjust others' interfaces
On point , The authoritative point uses the interface of a map manufacturer
Baidu map interface
Gaode map interface
There are many free ones
here
here
here
Use local data
Of course, it's cool to use someone else's interface , Send a request to resolve the request , But there are also bad . Collect money , Limit the number of calls , And the calling interface initiates once IO It's still slow . Of course, it's local , Mainly fast , And don't rely on others' interfaces .
for example star A lot of projects ip2region
https://gitee.com/lionsoul/ip2region
https://github.com/lionsoul2014/ip2region
1、 First, you need to get the data source , Find someone else to tidy up IP Data corresponding to physical address or crawled out of a website through script . For example, the data obtained are as follows 
2、 Obtain requested IP Address .
according to IP The agreement knows , Data packets will carry the source address and destination address , So as long as it's IP The request must know the source IP Address of the , It's better to hide , If you do all kinds of agents, you can only get the agents IP 了 .HTTP The protocol will also be set in the request header IP Address , So you can go straight through http Request header to get IP Address ,Java The implementation is as follows .
public static String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
// Multiple reverse proxies ip value , first ip That's the truth. ip
if (ip.indexOf(",") != -1) {
ip = ip.split(",")[0];
}
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("X-Real-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
3、 Get IP Then go directly to step 1 Find the corresponding IP Physical address is enough .
Local data query optimization
1、 Space optimization
We get IP This is the format (127.0.0.1), actually IP The address length is 32 Binary system , Saving strings directly is a waste of space , And it's not easy to compare, so first IP Change the address to byte Array , You don't have to make your own wheels ,JDK Already there. , and IPv4 and IPv6 All support .
byte[] ipByte = java.net.InetAddress.getByName("127.0.0.1").getAddress();
2、 Time optimization
Traversal is definitely not the optimal solution , The best way to avoid traversal is to use dichotomy to query after sorting .
Format data into a file after space optimization , The format is as follows 

Top two byte Data header , Record version number 、 How data is encoded 、 So the number of bytes 、 Data length range 、 Information such as the length of the index . Then there is the index , So they are arranged in order according to the primary key , And save the location of the index data . In this way, when querying, you can directly query the location of the index through dichotomy and get the location of the data , Then take out the data .
Code in github and gitee,CV The box .
https://gitee.com/eedui/location.git
https://github.com/eedui/location.git
Number ownership inquiry
The number ownership inquiry is also a similar method , Numbers have a similar pattern , front 7 Segment with sign , such as 1300000 This section is from Jinan City, Shandong Province , So you can get a number and find the place of ownership through the number segment . You can even find out which operator it is , But because it is allowed to carry numbers to transfer to the Internet , So the operators update quickly, and they are not sure . With the above data structure, you can quickly query .
Code in github and gitee,CV The box .
https://gitee.com/eedui/location.git
https://github.com/eedui/location.git
边栏推荐
- 存储过程的介绍与基本使用
- 工具箱之 IKVM.NET 项目新进展
- Problems that cannot be accessed in MySQL LAN
- About the problem of APP flash back after appium starts the app - (solved)
- Detr introduction
- Server to server (S2S) event (adjust)
- How did Guotai Junan Securities open an account? Is it safe to open an account?
- 提升树莓派性能的方法
- MySQL error 28 and solution
- User management summary of mongodb
猜你喜欢

如何让join跑得更快?

提升树莓派性能的方法
![SSRF漏洞file伪协议之[网鼎杯 2018]Fakebook1](/img/10/6de1ee8467b18ae03894a8d5ba95ff.png)
SSRF漏洞file伪协议之[网鼎杯 2018]Fakebook1

Vscade editor esp32 header file wavy line does not jump completely solved

flask session伪造之hctf admin

将数学公式在el-table里面展示出来

JS缓动动画原理教学(超细节)

Fast development board pinctrl and GPIO subsystem experiment for itop-imx6ull - modify the device tree file

Navicat运行sql文件导入数据不全或导入失败

LIS 最长上升子序列问题(动态规划、贪心+二分)
随机推荐
一文读懂数仓中的pg_stat
单片机原理期末复习笔记
My "troublesome" subordinates after 00: not bad for money, against leaders, and resist overtime
实现IP地址归属地显示功能、号码归属地查询
Esp32 ① compilation environment
Read PG in data warehouse in one article_ stat
学习突围2 - 关于高效学习的方法
Cinnamon taskbar speed
[learning notes] zkw segment tree
Show the mathematical formula in El table
MySQL入门尝鲜
JNA learning notes 1: Concepts
[QNX Hypervisor 2.2用户手册]6.3.4 虚拟寄存器(guest_shm.h)
Scripy tutorial classic practice [New Concept English]
JS slow motion animation principle teaching (super detail)
简单好用的代码规范
Why can basic data types call methods in JS
记一次 .NET 某新能源系统 线程疯涨 分析
聊聊伪共享
[QNX hypervisor 2.2 user manual]6.3.4 virtual register (guest_shm.h)