当前位置:网站首页>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
边栏推荐
- 数字ic设计——SPI
- MongoDB优化的几点原则
- JS function returns multiple values
- Isprs2021/ remote sensing image cloud detection: a geographic information driven method and a new large-scale remote sensing cloud / snow detection data set
- [etc.] what are the security objectives and implementation methods that cloud computing security expansion requires to focus on?
- JS function 返回多个值
- Thread pool reject policy best practices
- 华为镜像地址
- 室内ROS机器人导航调试记录(膨胀半径的选取经验)
- LIS longest ascending subsequence problem (dynamic programming, greed + dichotomy)
猜你喜欢
PAcP learning note 1: programming with pcap
Storage principle inside mongodb
Write it down once Net a new energy system thread surge analysis
[learning notes] zkw segment tree
Esp32 construction engineering add components
单片机学习笔记之点亮led 灯
Milkdown 控件图标
OSI seven layer model
室内ROS机器人导航调试记录(膨胀半径的选取经验)
Digital IC Design SPI
随机推荐
Fast development board pinctrl and GPIO subsystem experiment for itop-imx6ull - modify the device tree file
ESP32构解工程添加组件
Ogre入门尝鲜
Scripy tutorial classic practice [New Concept English]
How did Guotai Junan Securities open an account? Is it safe to open an account?
记一次 .NET 某新能源系统 线程疯涨 分析
提升树莓派性能的方法
Thread pool reject policy best practices
xshell连接服务器把密钥登陆改为密码登陆
将数学公式在el-table里面展示出来
LeetCode_ Binary search_ Medium_ 153. Find the minimum value in the rotation sort array
Write it down once Net a new energy system thread surge analysis
error LNK2019: 无法解析的外部符号
数字ic设计——SPI
Detr introduction
clion mingw64中文乱码
Mongodb slice summary
Vscade editor esp32 header file wavy line does not jump completely solved
Getting started with MySQL
Go language learning notes - structure