ip2region README

The project's README from github.com/lionsoul2014/ip2region. As one markdown file: README.md.

:globe_with_meridians: 中文简体 | English

ip2region

ip2region - is an offline IP address localization library and IP localization data management framework. It supports both IPv4 and IPv6 with query efficiency at the 10-microsecond level. It provides xdb data generation and query client implementations for many mainstream programming languages.

Features

1. Offline Localization Library

The project itself provides raw data for both IPv4 (data/ipv4_source.txt) and IPv6 (data/ipv6_source.txt), along with corresponding xdb files (data/ip2region_v4.xdb and data/ip2region_v6.xdb) to achieve city-level query localization. The field format is: Country|Province|City|ISP|iso-alpha2-code. Localization information for China is entirely in Chinese, while regional information for non-China areas is entirely in English.

2. Data Management Framework

xdb supports hundreds of millions of IP data segment rows. Region information supports full customization. The region information of the built-in data is fixed in the format: Country|Province|City|ISP|iso-alpha2-Code. You can append data for specific business needs to the region, such as: GPS information/International Standard Regional Codes/Zip codes, etc. In other words, you can fully use ip2region to manage your own IP localization data.

3. Data Deduplication and Compression

The xdb format generation program automatically processes the input raw data, checks and completes the merging of adjacent IP segments, and performs deduplication and compression of identical regional information.

4. High-Speed Query Response

Even for queries based entirely on the xdb file, the single query response time is at the 10-microsecond level. Memory-accelerated queries can be enabled through the following two methods:

  1. vIndex Index Caching: Uses a fixed 512KiB of memory to cache vector index data, reducing one disk IO operation and maintaining average query efficiency within 100 microseconds.
  2. Entire xdb File Caching: Loads the entire xdb file into memory. Memory usage is equal to the xdb file size. There is no disk IO operation, maintaining 10-microsecond level query efficiency.

5. Unified Query Interface

xdb provides version-compatible query implementations. A unified API can simultaneously provide queries for both IPv4 and IPv6 data and return unified data.

xdb Query

For API introductions, usage documentation, and test programs, please refer to the README introduction under the corresponding searcher query client. All query binding implementations are as follows:

Language Description IPv4 Support IPv6 Support
Golang golang query client :white_check_mark: :white_check_mark:
PHP php query client :white_check_mark: :white_check_mark:
Java java query client :white_check_mark: :white_check_mark:
C C[std=c99] query client :white_check_mark: :white_check_mark:
Lua_c lua c extension query client :white_check_mark: :white_check_mark:
Lua lua query client :white_check_mark: :white_check_mark:
Rust rust query client :white_check_mark: :white_check_mark:
Python python query client :white_check_mark: :white_check_mark:
Javascript javascript query client :white_check_mark: :white_check_mark:
Csharp csharp query client :white_check_mark: :white_check_mark:
Erlang erlang query client :white_check_mark: :white_check_mark:
Nginx nginx extension query client :white_check_mark: :white_check_mark:
C++ C++ query client :white_check_mark: :white_check_mark:
Cangjie Cangjie query client :white_check_mark: :white_check_mark:

The following toolchain implementations are contributed by community developers via third-party repositories:

Language Description
ip2region-composer php composer management client
ip2region-ts node.js addon management client
ruby-ip2region ruby xdb query client implementation
Ip2regionTool ip2region data conversion tool

xdb Generation

For API introductions, usage documentation, and test programs, please refer to the README documents under the following maker generation programs:

Language Description IPv4 Support IPv6 Support
Golang golang xdb generation program :white_check_mark: :white_check_mark:
Java java xdb generation program :white_check_mark: :white_check_mark:
Python python xdb generation program :white_check_mark: :x:
Csharp csharp xdb generation program :white_check_mark: :x:
Rust rust xdb generation program :white_check_mark: :white_check_mark:
C++ C++ xdb generation program :white_check_mark: :white_check_mark:

xdb Update

The core of the ip2region project lies in researching the design and implementation of IP data storage and fast querying. The raw data ./data/ipv4_source.txt and ./data/ipv6_source.txt included in the project are updated irregularly. For scenarios with high requirements for data accuracy and update frequency, it is recommended to purchase commercial offline data from the Ip2Region Community or third-party vendors. You can try to update the data yourself using the following methods:

Manual Editing and Updating

You can modify the data yourself based on the raw IP data provided by ip2region in ./data/ipv4_source.txt and ./data/ipv6_source.txt using the editing tools provided by ip2region. Currently, the data sources include:

  1. Data provided by the ip2region community (please refer to the official account at the bottom for community notifications)
  2. Project Issues tagged with [Data_Updates]
  3. Other custom data: e.g., data provided by customers, data obtained through GPS and WIFI positioning, or legal and compliant data from other platforms.

For instructions on using the raw IP data editing tools, please refer to the README documents under the following maker generation programs:

Language Description IPv4 Support IPv6 Support
Golang golang IP raw data editor :white_check_mark: :white_check_mark:
C++ C++ IP raw data editor :white_check_mark: :white_check_mark:

Detection Automatic Update

If you want to update data via your own API or data source, you can refer to the update algorithm based on the "Detection Algorithm" shared in the following videos to write your own update program:

  1. Data Update Implementation Video Sharing - part1
  2. Data Update Implementation Video Sharing - part2

Official Community

The Ip2Region official community was officially launched on 2025/06/12. On one hand, it provides stable commercial offline data services. On the other hand, it facilitates the strengthening of the IP toolchain and data services outside the core code, such as usage documentation, query testing, and data correction. For more information and services regarding the community, please visit the Ip2Region Official Community.

Related Remarks

1. xdb Technical Documents:

  1. xdb Data Structure Analysis: "ip2region xdb - Data Structure Description"
  2. xdb Query Process Analysis: "ip2region xdb - Query Process Description"
  3. xdb Generation Process Analysis: "ip2region xdb - Generation Process Description"
  4. xdb File Generation Tutorial: "ip2region xdb - File Generation Tutorial"
  5. xdb Concurrent Safety Query: "ip2region xdb - Concurrent Safety Query"
  6. xdb Data Update Method: "ip2region Data Update and Use of xdb Data Editor"

3. Technical Information Blogs

  1. WeChat Official Account - lionsoul-org, the author's active technical sharing channel
  2. Ip2Region Official Community

ip2region (lionsoul2014/ip2region)

Searching Database…

19.7K ·GitHub
这是什么?一个离线的 IP 归属地定位库和数据管理框架:它把 IPv4 和 IPv6 地址映射到城市级别的区域,查询耗时仅 10 微秒,并附带 xdb 数据文件以及支持多种编程语言的查询客户端。
它是需要我部署在某个地方运行的服务,还是嵌入到我应用里的库?是一个库加一个本地数据文件:你的代码用 searcher 客户端打开 xdb 文件,在进程内直接查询,不需要部署任何东西。仓库还提供了用于生成自己的 xdb 文件的 maker 程序。
我怎么做第一次查询?从仓库中取出 data/ip2regionv4.xdb 或 data/ip2regionv6.xdb,用你所使用语言的 searcher 客户端打开它;每个客户端的 README 都写明了 API、用法和测试程序。
查询的时候需要数据库服务器或联网吗?不需要。查询完全基于本地 xdb 文件完成——没有服务器,也没有网络请求,这正是它能做到微秒级响应的原因。
自带的 IP 数据有多新?原始数据的更新并不定期,所以新鲜度没有保证。对于对准确性要求高的场景,README 建议使用 Ip2Region Community 或第三方厂商提供的商业数据;你也可以自己更新数据。
查询结果实际上返回的是什么?一个固定格式的区域字符串:Country|Province|City|ISP|iso-alpha2-code,精确到城市级。中国的区域信息全部是中文,非中国地区则全部用英文给出。
一个 API 能同时查 IPv4 和 IPv6 吗?可以。xdb 提供了版本兼容的查询实现,所以同一个统一的 API 既能查 IPv4 数据也能查 IPv6 数据,并以统一的格式返回结果。
哪些语言有现成的查询客户端?官方提供 14 个客户端,全部支持 IPv4 和 IPv6:Go、PHP、Java、C、Lua(C 扩展和纯 Lua)、Rust、Python、JavaScript、C#、Erlang、nginx、C++ 和 Cangjie。社区项目还提供了 PHP Composer 客户端、Node.js addon 和 Ruby 客户端。
我可以用它管理自己的 IP 数据,而不用自带的那套吗?可以——它是一个完整的数据管理框架。xdb 能处理数亿条 IP 段记录,区域信息完全可以自定义(比如附加 GPS 坐标、邮编等),生成时还会自动合并相邻的 IP 段并去除重复的区域。
Size
308.4 MB
Stars
19.7K
Age
10y
Commits · 90 Days