Home >Computer Tutorials >Computer Knowledge >How to query Douyin's IP location (how to display Douyin's IP location)
php editor Xiaoxin will introduce to you the query method about Douyin IP territory. When using Douyin APP, we often want to know where a certain user’s IP address belongs. By querying the IP location, we can learn the user's approximate location information. So, how to check the IP location of Douyin? Next, we will give you detailed answers.
Next, let’s focus on how the Java backend implements the IP attribution function. In fact, it only requires the following two steps:
Anyone who has done web development knows that whether it is a mobile terminal or a PC terminal, the request interface will be encapsulated into an HttpServletRequest object. This object contains various information requested by the client, such as request address, parameters, submitted data, etc.
If the server directly exposes the IP, then request.getRemoteAddr() can get the client IP.
In the current popular architecture, most servers do not directly expose their IP addresses, but process requests through one or more layers of reverse proxies. A common reverse proxy tool is nginx. By introducing a proxy, there is an additional layer between the server and the client, so the IP address obtained using the request.getRemoteAddr() method will be the IP address of the proxy server, not the client's IP address. In order to track the original client's IP address, information such as X-Forwarded-For is usually added to the forwarding header. This information can be used to determine the true source of the request.
X-Forwarded-For is a field developed by Squid and is only added when passing through an HTTP proxy or load balancing server. Its format is X-Forwarded-For:client1,proxy1,proxy2. Normally, the first IP address is the real IP of the client, and the subsequent IP addresses are the IPs of the proxy servers passed through. According to the above code comments, the first IP address can be intercepted directly. Proxy-Client-IP/WL-Proxy-Client-IP is a header that only exists when requested by the Apache HTTP server. When using Apache HTTP as a proxy, the Proxy-Client-IP request header is usually added, and WL-Proxy-Client-IP is a header added by the WebLogic plug-in. In this case, the header information can also be obtained directly. HTTP_CLIENT_IP is a request header that some proxy servers may add. X-Real-IP is a request header usually used by Nginx.
In daily development, it is not clearly specified which header information should be used to track the client. You need to try one by one until you get it. The following is the corresponding code:
ini
Copy code
@Slf4jpublic class IpUtils { private static final String UNKNOWN_VALUE = “unknown”; private static final String LOCALHOST_V4 = “127.0. 0.1”; private static final String LOCALHOST_V6 = “0:0:0:0:0:0:0:1”; private static final String X_FORWARDED_FOR = “X-Forwarded- For”; private static final String X_REAL_IP = “X-Real-IP”; private static final String PROXY_CLIENT_IP = “Proxy-Client-IP”; private static final String WL_PROXY_CLIENT_IP = “WL-Proxy-Client-IP”; private static final String HTTP_CLIENT_IP = “HTTP_CLIENT_IP”; private static final String IP_DATA_PATH = “
/Users/ shepherdmy/Desktop/ip2region.xdb”; private static byte[] contentBuff; /*** Get the client ip address * @param request * @return*/ public static String getRemoteHost(HttpServletRequest request) { String ip = request. getHeader(X_FORWARDED_FOR); if (StringUtils.isNotEmpty(ip) && !
UNKNOWN_VALUE.equalsIgnoreCase(ip)) { // After multiple reverse proxies, there will be multiple ip values, the first one ip is the real ip int index = ip.indexOf(“,”); if (index != -1) { return ip.substring(0, index); } else { return ip; } }ip = request.getHeader(X_REAL_IP); if (StringUtils.isNotEmpty(ip) && !
UNKNOWN_VALUE.equalsIgnoreCase(ip)) { return ip ; } if (StringUtils.isBlank(ip) ||
UNKNOWN_VALUE.equalsIgnoreCase(ip)) {ip = request.getHeader(PROXY_CLIENT_IP); } if (StringUtils.isBlank (ip) ||
UNKNOWN_VALUE.equalsIgnoreCase(ip)) {ip = request.getHeader(WL_PROXY_CLIENT_IP); } if (StringUtils.isBlank(ip) ||
UNKNOWN_VALUE.equalsIgnoreCase(ip )) {ip = request.getRemoteAddr(); } if (StringUtils.isBlank(ip) ||
UNKNOWN_VALUE.equalsIgnoreCase(ip)) {ip = request.getHeader(HTTP_CLIENT_IP) ; } if (StringUtils.isBlank(ip) ||
UNKNOWN_VALUE.equalsIgnoreCase(ip)) {ip = request.getRemoteAddr(); } return ip.equals(LOCALHOST_V6) ? LOCALHOST_V4 : ip; } }
项目推荐:基于SpringBoot2.x、SpringCloud和SpringCloudAlibaba企业级系统架构底层框架封装,解决业务开发时常见的非功能性需求,防止重复造轮子,方便业务快速开发和企业技术栈框架统一管理。引入组件化的思想实现高内聚低耦合并且高度可配置化,做到可插拔。严格控制包依赖和统一版本管理,做到最少化依赖。注重代码规范和注释,非常适合个人学习和企业使用
Github地址:github.com/plasticene/…
Gitee地址:gitee.com/plasticene3…
微信公众号:Shepherd进阶笔记
交流探讨群:Shepherd_126
通过上面我们就能获取到客户端用户的ip地址,接下来就可以通过ip解析获取归属地了。
如果我们在网上搜索资料教程,大部分都是说基于各大平台(eg:淘宝,新浪)提供的ip库进行查询,不过不难发现这些平台已经不怎么维护这个功能,现在处于“半死不活”的状态,根本不靠谱,当然有些平台提供可靠的获取ip属地接口,但是收费、收费、收费。
本着作为一个程序员的严谨:“能白嫖的就白嫖,避免出现要买的是你,不会用也是你的尴尬遭遇”。扯远了言归正传,为了寻求可靠有效的解决方案,只能去看看github有没有什么项目能满足需求,果然功夫不负有心人,发现一个宝藏级项目:ip2region,一个准确率 99.9% 的离线 IP 地址定位库,0.0x 毫秒级查询,ip2region.db 数据库只有数 MB的项目,提供了众多主流编程语言的 xdb 数据生成和查询客户端实现,这里只能说:开源真香,开源万岁。
标准化的数据格式
每个 ip 数据段的 region 信息都固定了格式:国家|区域|省份|城市|ISP,只有中国的数据绝大部分精确到了城市,其他国家部分数据只能定位到国家,其余选项全部是0。
数据去重和压缩
xdb 格式生成程序会自动去重和压缩部分数据,默认的全部 IP 数据,生成的 ip2region.xdb 数据库是 11MiB,随着数据的详细度增加数据库的大小也慢慢增大。
极速查询响应
即使是完全基于 xdb 文件的查询,单次查询响应时间在十微秒级别,可通过如下两种方式开启内存加速查询:
vIndex 索引缓存 :使用固定的 512KiB 的内存空间缓存 vector index 数据,减少一次 IO 磁盘操作,保持平均查询效率稳定在10-20微秒之间。xdb 整个文件缓存:将整个 xdb 文件全部加载到内存,内存占用等同于 xdb 文件大小,无磁盘 IO 操作,保持微秒级别的查询效率。IP 数据管理框架
v2.0 格式的 xdb 支持亿级别的 IP 数据段行数,region 信息也可以完全自定义,例如:你可以在 region 中追加特定业务需求的数据,例如:GPS信息/国际统一地域信息编码/邮编等。也就是你完全可以使用 ip2region 来管理你自己的 IP 定位数据。
99.9% 准确率
数据聚合了一些知名 ip 到地名查询提供商的数据,这些是他们官方的的准确率,经测试着实比经典的纯真 IP 定位准确一些。
ip2region 的数据聚合自以下服务商的开放 API 或者数据(升级程序每秒请求次数 2 到 4 次):
01,>80%,淘宝IP地址库,ip.taobao.com/%5C02,≈10%,GeoIP,geoip.com/%5C03,≈2%,纯真 IP 库,www.cz88.net/%5C备注:如果上述开放 API 或者数据都不给开放数据时 ip2region 将停止数据的更新服务。
提供了众多主流编程语言的 xdb 数据生成和查询客户端实现,已经集成的客户端有:java、C#、php、c、python、nodejs、php扩展(php5 和 php7)、golang、rust、lua、lua_c,nginx。这里讲一下java的客户端。
首先我们需要引入依赖:
xml
复制代码
org.lionsoulip2region2.6.5
接下来我们需要先去下载数据文件ip2region.xdb到本地,然后基于数据文件进行查询,下面查询方法文件路径改为你本地路径即可,ip2region提供三种查询方式:
完全基于文件的查询
java
复制代码
import
org.lionsoul.ip2region.xdb.Searcher;import java.io.*;import
java.util.concurrent.TimeUnit;publicclassSearcherTest {publicstaticvoidmain(String[] args) {// 1、创建 searcher 对象StringdbPath=”ip2region.xdb file path”;Searchersearcher=null;try { searcher = Searcher.newWithFileOnly(dbPath); } catch (IOException e) { System.out.printf(“failed to create searcher with `%s`: %s\\n”, dbPath, e);return; }// 2、查询try {Stringip=”1.2.3.4″;longsTime= System.nanoTime();Stringregion= searcher.search(ip);longcost=
TimeUnit.NANOSECONDS.toMicros((long) (System.nanoTime() – sTime)); System.out.printf(“{region: %s, ioCount: %d, took: %d μs}\\n”, region, searcher.getIOCount(), cost); } catch (Exception e) { System.out.printf(“failed to search(%s): %s\\n”, ip, e); }// 3、关闭资源 searcher.close();// 备注:并发使用,每个线程需要创建一个独立的 searcher 对象单独使用。 }}
缓存 VectorIndex 索引
我们可以提前从 xdb 文件中加载出来 VectorIndex 数据,然后全局缓存,每次创建 Searcher 对象的时候使用全局的 VectorIndex 缓存可以减少一次固定的 IO 操作,从而加速查询,减少 IO 压力。
java
复制代码
import
org.lionsoul.ip2region.xdb.Searcher;import java.io.*;import
java.util.concurrent.TimeUnit;publicclassSearcherTest {publicstaticvoidmain(String[] args) {StringdbPath=”ip2region.xdb file path”;// 1、从 dbPath 中预先加载 VectorIndex 缓存,并且把这个得到的数据作为全局变量,后续反复使用。byte[] vIndex;try { vIndex =
Searcher.loadVectorIndexFromFile(dbPath); } catch (Exception e) { System.out.printf(“failed to load vector index from `%s`: %s\\n”, dbPath, e);return; }// 2、使用全局的 vIndex 创建带 VectorIndex 缓存的查询对象。 Searcher searcher;try { searcher =
Searcher.newWithVectorIndex(dbPath, vIndex); } catch (Exception e) { System.out.printf(“failed to create vectorIndex cached searcher with `%s`: %s\\n”, dbPath, e);return; }// 3、查询try {Stringip=”1.2.3.4″;longsTime= System.nanoTime();Stringregion= searcher.search(ip);longcost=
TimeUnit.NANOSECONDS.toMicros((long) (System.nanoTime() – sTime)); System.out.printf(“{region: %s, ioCount: %d, took: %d μs}\\n”, region, searcher.getIOCount(), cost); } catch (Exception e) { System.out.printf(“failed to search(%s): %s\\n”, ip, e); }// 4、关闭资源 searcher.close();// 备注:每个线程需要单独创建一个独立的 Searcher 对象,但是都共享全局的制度 vIndex 缓存。 }}
缓存整个 xdb 数据
我们也可以预先加载整个 ip2region.xdb 的数据到内存,然后基于这个数据创建查询对象来实现完全基于文件的查询,类似之前的 memory search。
java
复制代码
import
org.lionsoul.ip2region.xdb.Searcher;import java.io.*;import
java.util.concurrent.TimeUnit;publicclassSearcherTest {publicstaticvoidmain(String[] args) {StringdbPath=”ip2region.xdb file path”;// 1、从 dbPath 加载整个 xdb 到内存。byte[] cBuff;try { cBuff =
Searcher.loadContentFromFile(dbPath); } catch (Exception e) { System.out.printf(“failed to load content from `%s`: %s\\n”, dbPath, e);return; }// 2、使用上述的 cBuff 创建一个完全基于内存的查询对象。 Searcher searcher;try { searcher = Searcher.newWithBuffer(cBuff); } catch (Exception e) { System.out.printf(“failed to create content cached searcher: %s\\n”, e);return; }// 3、查询try {Stringip=”1.2.3.4″;longsTime= System.nanoTime();Stringregion= searcher.search(ip);longcost=
TimeUnit.NANOSECONDS.toMicros((long) (System.nanoTime() – sTime)); System.out.printf(“{region: %s, ioCount: %d, took: %d μs}\\n”, region, searcher.getIOCount(), cost); } catch (Exception e) { System.out.printf(“failed to search(%s): %s\\n”, ip, e); }// 4、关闭资源 – 该 searcher 对象可以安全用于并发,等整个服务关闭的时候再关闭 searcher// searcher.close();// 备注:并发使用,用整个 xdb 数据缓存创建的查询对象可以安全的用于并发,也就是你可以把这个 searcher 对象做成全局对象去跨线程访问。 }}
首先我们也需要像上面一样引入maven依赖。然后就可以基于上面的查询方式进行封装成工具类了,我这里选择了上面的第三种方式:缓存整个 xdb 数据
ini
复制代码
@Slf4jpublic class IpUtils { private static final String IP_DATA_PATH = ”
/Users/shepherdmy/Desktop/ip2region.xdb”; private static byte[] contentBuff; static { try { // 从 dbPath 加载整个 xdb 到内存。contentBuff =
Searcher.loadContentFromFile(IP_DATA_PATH); } catch (IOException e) { e.printStackTrace(); } } /*** Query the location based on ip, fixed format: China|0|Zhejiang Province|Hangzhou City|Telecom * @param ip * @return*/ public static IpRegion getIpRegion(String ip) { Searcher searcher = null; IpRegion ipRegion = new IpRegion(); try {searcher = Searcher.newWithBuffer(contentBuff); String region = searcher.search(ip); String[]info = StringUtils.split(region, “|”); ipRegion.setCountry(info[0]); ipRegion.setArea(info[1]); ipRegion.setProvince(info[2]); ipRegion.setCity(info[3]); ipRegion.setIsp(info[4]); } catch (Exception e) { log.error(“get ip region error: “, e); } finally { if (searcher != null) { try { searcher.close(); } catch (IOException e) { log.error(“close searcher error:”, e); } } } return ipRegion; }}
The above is the detailed content of How to query Douyin's IP location (how to display Douyin's IP location). For more information, please follow other related articles on the PHP Chinese website!