Ip2region - 基于xdb离线库的Java IP查询工具提供给脚本调用

文章目录

  • Pre
  • 效果
  • 实现
    • git clone
    • 编译测试程序
    • 将ip2region.xdb放到指定目录
    • 使用
    • 改进
    • 最终效果

在这里插入图片描述

Pre

OpenSource - Ip2region 离线IP地址定位库和IP定位数据管理框架

Ip2region - xdb java 查询客户端实现


效果

在这里插入图片描述

在这里插入图片描述

最终效果
在这里插入图片描述

实现

git clone

git clone https://github.com/lionsoul2014/ip2region.git

在这里插入图片描述

编译测试程序

cd binding/java/
mvn compile package

然后会在当前目录的 target 目录下得到一个 ip2region-{version}.jar 的打包文件。

在这里插入图片描述

将ip2region.xdb放到指定目录

在这里插入图片描述


使用

在这里插入图片描述

在这里插入图片描述


改进

// Copyright 2022 The Ip2Region Authors. All rights reserved.
// Use of this source code is governed by a Apache2.0-style
// license that can be found in the LICENSE file.
// @Author Lion <chenxin619315@gmail.com>
// @Date   2022/06/23package org.lionsoul.ip2region;import org.lionsoul.ip2region.xdb.Searcher;import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;public class SearchTest {public static void printHelp(String[] args) {System.out.print("ip2region xdb searcher\n");System.out.print("java -jar ip2region-{version}.jar [command] [command options]\n");System.out.print("Command: \n");System.out.print("  search    search input test\n");System.out.print("  bench     search bench test\n");}public static Searcher createSearcher(String dbPath, String cachePolicy) throws IOException {if ("file".equals(cachePolicy)) {return Searcher.newWithFileOnly(dbPath);} else if ("vectorIndex".equals(cachePolicy)) {byte[] vIndex = Searcher.loadVectorIndexFromFile(dbPath);return Searcher.newWithVectorIndex(dbPath, vIndex);} else if ("content".equals(cachePolicy)) {byte[] cBuff = Searcher.loadContentFromFile(dbPath);return Searcher.newWithBuffer(cBuff);} else {throw new IOException("invalid cache policy `" + cachePolicy + "`, options: file/vectorIndex/content");}}public static void searchTest(String[] args) throws IOException {String dbPath = "", cachePolicy = "vectorIndex";for (final String r : args) {if (r.length() < 5) {continue;}if (r.indexOf("--") != 0) {continue;}int sIdx = r.indexOf('=');if (sIdx < 0) {System.out.printf("missing = for args pair `%s`\n", r);return;}String key = r.substring(2, sIdx);String val = r.substring(sIdx + 1);// System.out.printf("key=%s, val=%s\n", key, val);if ("db".equals(key)) {dbPath = val;} else if ("cache-policy".equals(key)) {cachePolicy = val;} else {System.out.printf("undefined option `%s`\n", r);return;}}if (dbPath.length() < 1) {System.out.print("java -jar ip2region-{version}.jar search [command options]\n");System.out.print("options:\n");System.out.print(" --db string              ip2region binary xdb file path\n");System.out.print(" --cache-policy string    cache policy: file/vectorIndex/content\n");return;}Searcher searcher = createSearcher(dbPath, cachePolicy);Scanner scanner = new Scanner(System.in);String line = scanner.nextLine();try {String region = searcher.search(line.trim());System.out.printf("ip: %s , region: %s\n", line, region);} catch (Exception e) {System.out.printf("{err: %s, ioCount: %d}\n", e, searcher.getIOCount());}searcher.close();}public static void benchTest(String[] args) throws IOException {String dbPath = "", srcPath = "", cachePolicy = "vectorIndex";for (final String r : args) {if (r.length() < 5) {continue;}if (r.indexOf("--") != 0) {continue;}int sIdx = r.indexOf('=');if (sIdx < 0) {System.out.printf("missing = for args pair `%s`\n", r);return;}String key = r.substring(2, sIdx);String val = r.substring(sIdx + 1);if ("db".equals(key)) {dbPath = val;} else if ("src".equals(key)) {srcPath = val;} else if ("cache-policy".equals(key)) {cachePolicy = val;} else {System.out.printf("undefined option `%s`\n", r);return;}}if (dbPath.length() < 1 || srcPath.length() < 1) {System.out.print("java -jar ip2region-{version}.jar bench [command options]\n");System.out.print("options:\n");System.out.print(" --db string              ip2region binary xdb file path\n");System.out.print(" --src string             source ip text file path\n");System.out.print(" --cache-policy string    cache policy: file/vectorIndex/content\n");return;}Searcher searcher = createSearcher(dbPath, cachePolicy);long count = 0, costs = 0, tStart = System.nanoTime();String line;final Charset charset = Charset.forName("utf-8");final FileInputStream fis = new FileInputStream(srcPath);final BufferedReader reader = new BufferedReader(new InputStreamReader(fis, charset));while ((line = reader.readLine()) != null) {String l = line.trim();String[] ps = l.split("\\|", 3);if (ps.length != 3) {System.out.printf("invalid ip segment `%s`\n", l);return;}long sip;try {sip = Searcher.checkIP(ps[0]);} catch (Exception e) {System.out.printf("check start ip `%s`: %s\n", ps[0], e);return;}long eip;try {eip = Searcher.checkIP(ps[1]);} catch (Exception e) {System.out.printf("check end ip `%s`: %s\n", ps[1], e);return;}if (sip > eip) {System.out.printf("start ip(%s) should not be greater than end ip(%s)\n", ps[0], ps[1]);return;}long mip = (sip + eip) >> 1;for (final long ip : new long[]{sip, (sip + mip) >> 1, mip, (mip + eip) >> 1, eip}) {long sTime = System.nanoTime();String region = searcher.search(ip);costs += System.nanoTime() - sTime;// check the region infoif (!ps[2].equals(region)) {System.out.printf("failed search(%s) with (%s != %s)\n", Searcher.long2ip(ip), region, ps[2]);return;}count++;}}reader.close();searcher.close();long took = System.nanoTime() - tStart;System.out.printf("Bench finished, {cachePolicy: %s, total: %d, took: %ds, cost: %d μs/op}\n",cachePolicy, count, TimeUnit.NANOSECONDS.toSeconds(took),count == 0 ? 0 : TimeUnit.NANOSECONDS.toMicros(costs / count));}public static void main(String[] args) {if (args.length < 1) {printHelp(args);return;}if ("search".equals(args[0])) {try {searchTest(args);} catch (IOException e) {System.out.printf("failed running search test: %s\n", e);}} else if ("bench".equals(args[0])) {try {benchTest(args);} catch (IOException e) {System.out.printf("failed running bench test: %s\n", e);}} else {printHelp(args);}}}

重新编译 ,执行

最终效果

在这里插入图片描述

这样就可以愉快的在脚本中调用了

当然了,启动java进程的过程,相对还是比较耗时的,这里仅提供一种思路

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/50082.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

YOLOV8源码解读-C2f模块-以及总结c2模块、Bottleneck

c2f模块是对c2模块的改进 c2模块图解解读 先给出YOLOV8中卷积的定义模块一键三连-卷积-BN-激活函数 def autopad(k, pNone, d1): # kernel, padding, dilation"""Pad to same shape outputs."""if d > 1:k d * (k - 1) 1 if isinstance…

洛谷 P7771:【模板】欧拉路径

【题目来源】https://www.luogu.com.cn/problem/P7771【题目描述】 求有向图字典序最小的欧拉路径。【输入格式】 第一行两个整数 n,m 表示有向图的点数和边数。 接下来 m 行每行两个整数 u,v 表示存在一条 u→v 的有向边。【输出格式】 如果不存在欧拉路径&#xff0c;输出一行…

在模型中bert和transform讲解

在自然语言处理&#xff08;NLP&#xff09;领域&#xff0c;BERT 和 Transformer 是两个非常重要的概念。下面是它们的简要解释&#xff1a; 一 、BERT BERT&#xff08;Bidirectional Encoder Representations from Transformers&#xff09;是由Google提出的一种预训练语言…

Docker与Kubernetes在Java微服务中的应用

引言 随着微服务架构的普及,容器化技术成为部署和管理微服务的重要手段。Docker 提供了一种轻量级的容器解决方案,而 Kubernetes 则成为了容器编排和管理的事实标准。本文将深入探讨如何将 Java 微服务容器化,并在 Kubernetes 上部署和管理这些服务。 容器化概述 1. 容器…

Linux:进程信号(二.信号的保存与处理、递达、volatile关键字、SIGCHLD信号)

上次介绍了&#xff1a;(Linux&#xff1a;进程信号&#xff08;一.认识信号、信号的产生及深层理解、Term与Core&#xff09;)[https://blog.csdn.net/qq_74415153/article/details/140624810] 文章目录 1.信号保存1.1递达、未决、阻塞等概念1.2再次理解信号产生与保存1.3信号…

Pytorch深度学习实践(9)卷积神经网络

卷积神经网络 全连接神经网络 神经网络中全部是线性模型&#xff0c;是由线性模型串联起来的 全连接网络又叫全连接层 卷积神经网络 在全连接神经网络中&#xff0c;由于输入必须是一维向量&#xff0c;因此在处理图像时必须要对图像矩阵进行拉伸成一维的形式&#xff0c;…

构建npm组件包并打包上传到npm官网

vitejsv3简略搭建&#xff08;笔记用&#xff09; 原作者&#xff1a;https://juejin.cn/post/7119827361092108301?searchId20240724094258A72138D981DC0419C33E 1.npm create vitelatest # 使用npm安装vite脚手架2.先install项目&#xff0c;然后改造&#xff1a;修改&…

【算法】布隆过滤器

一、引言 在现实世界的计算机科学问题中&#xff0c;我们经常需要判断一个元素是否属于一个集合。传统的做法是使用哈希表或者直接遍历集合&#xff0c;但这些方法在数据量较大时效率低下。布隆过滤器&#xff08;Bloom Filter&#xff09;是一种空间效率极高的概率型数据结构&…

【NPU 系列专栏 2.8 -- 特斯拉 FDS NPU 详细介绍 】

请阅读【嵌入式及芯片开发学必备专栏】 文章目录 特斯拉 NPU 芯片介绍FSD(Full Self-Driving)芯片 简介FSD主要特点FSD 详细参数FSD 应用场景特斯拉 Hardware 3.0 芯片 简介Hardware 3.0主要特点Hardware 3.0 详细参数Hardware 3.0应用场景特斯拉自研 NPU 的优势优化设计高度…

【数学建模】——matplotlib简单应用

目录 1.绘制带有中文标签和图例的正弦和余弦曲线 2. 绘制散点图 1.修改散点符号与大小 2.修改颜色 3.绘制饼状图 4.在图例中显示公式 5.多个图形单独显示 6.绘制有描边和填充效果的柱状图 7.使用雷达图展示学生成绩 8.绘制三维曲面 9.绘制三维曲线 10.设置…

定制化即时通讯企业级移动门户解决方案,WorkPlus IM系统让工作事半功倍

随着移动设备的普及和移动办公的兴起&#xff0c;企业越来越需要一种定制化的即时通讯企业级移动门户解决方案来提高工作效率和团队协作效果。WorkPlus IM系统作为一种创新的解决方案&#xff0c;为企业提供了一个个性化定制、高度安全和高效便捷的移动门户平台。本文将对定制化…

BFF:优化前后端协作设计模式

BFF&#xff1a;优化前后端协作设计模式 BFF是什么 BFF即 Backends For Frontends (服务于前端的后端)。是一种介于前端和后端之间一种重要的通信设计模式。它旨在解决前端与后端协作中的复杂性问题。 背景 行业背景&#xff1a;传统前端应用&#xff08;如Web应用、移动应…

微服务-MybatisPlus下

微服务-MybatisPlus下 文章目录 微服务-MybatisPlus下1 MybatisPlus扩展功能1.1 代码生成1.2 静态工具1.3 逻辑删除1.4 枚举处理器1.5 JSON处理器**1.5.1.定义实体****1.5.2.使用类型处理器** **1.6 配置加密&#xff08;选学&#xff09;**1.6.1.生成秘钥**1.6.2.修改配置****…

网络安全防御【IPsec VPN搭建】

目录 一、实验拓扑图 二、实验要求 三、实验思路 四、实验步骤&#xff1a; 修改双机热备的为主备模式&#xff1a; 2、配置交换机LSW6新增的配置&#xff1a; 3、防火墙&#xff08;FW4&#xff09;做相关的基础配置&#xff1a; 4、搭建IPsec VPN通道 &#xff08;1…

GLSL教程 第10章:高级渲染技术

目录 10.1 后处理效果 10.1.1 色彩校正 示例代码&#xff1a;色彩校正 解释&#xff1a; 10.1.2 亮度对比度调整 示例代码&#xff1a;亮度对比度调整 解释&#xff1a; 10.1.3 模糊效果 示例代码&#xff1a;高斯模糊 解释&#xff1a; 10.1.4 边缘锐化 示例代码&a…

Java代码基础算法练习-求杨辉三角第n行的值-2024.07.27

任务描述&#xff1a; 给定一个非负整数n&#xff0c;生成「杨辉三角」的第n行。&#xff08;1<n<10&#xff09;在「杨辉三角」中&#xff0c;每 个数是它左上方和右上方的数的和。 &#xff08;提示&#xff0c;第一列数值为1&#xff0c;如数组下标用i,j表示&#xf…

VoIP所在的协议层次

VoIP&#xff08;Voice over Internet Protocol&#xff09;本身不是一种协议&#xff0c;而是一种技术或通信方式。虽然VoIP技术本身不是协议&#xff0c;但它依赖于多种协议来实现其功能。所以&#xff0c;其并不严格地工作在网络通信的某一层&#xff0c;而是跨越了多个层次…

独占电脑资源来执行一个应用

1. 背景 在人工智能时代&#xff0c;随着神经网络的发展&#xff0c;训练人工智能模型需要越来越多的硬件资源&#xff0c;例如&#xff0c;利用10万条棋局数据、使用一台PC电脑、完整地训练一次确定性神经网络五子棋模型&#xff0c;需要花费一年半的时间。随着训练数据的增长…

人工智能在医疗领域的应用及未来展望

随着科技的不断发展&#xff0c;人工智能&#xff08;AI&#xff09;逐渐成为人们关注的焦点。在众多领域中&#xff0c;医疗行业与AI的结合备受瞩目&#xff0c;为现代医疗带来了前所未有的变革。本文将探讨人工智能在医疗领域的应用及其未来发展。 一、人工智能在医疗领域的应…

APP逆向 day23司小宝逆向

一.前言 今天也是讲最后一个基础知识点了&#xff0c;ptrace占坑&#xff0c;这个也算是一个坑&#xff0c;今天通过这个案例和大家讲一下&#xff0c;今天这个案例我们来整验证码登录&#xff0c;版本选择4.7.8 二.抓包分析 抓包发现&#xff0c;请求头里的东西通过改包发现…