当前位置:网站首页>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
 Insert picture description here

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
 Insert picture description here

 Insert picture description here

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
原网站

版权声明
本文为[-edui]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071129058777.html