Foreword
During development, we need to record the IP and location information of the logged-in user, which can be achieved through ip2region!
For example:
- QQ, Gitee, etc. — when you log in from a different location, you will be alerted about a remote login.
- Food delivery apps and other software that require geographic location — if GPS is turned off, they can analyze the geographic location based on IP.
Introduction
ip2region — an offline IP address location library with 99.9% accuracy, sub-millisecond query speed. The ip2region.db database is only a few MB in size. It provides query bindings for Java, PHP, C, Python, Node.js, Go, C#, etc., and supports three query algorithms: Binary, B-tree, and Memory. Each IP data segment has a fixed format:
cityId|country|area|province|city|ISP- GitHub address: https://github.com/lionsoul2014/ip2region
Usage
- Install via NuGet
Install-Package IP2Region
- Download
ip2region.db
git clone https://github.com/lionsoul2014/ip2region.git
Then go to the data/ folder, find ip2region.db, and copy it to the resources folder in your project.
Usage is very simple — it only takes one line of code:
static void Main(string[] args)
{
using (var _search = new DbSearcher(Environment.CurrentDirectory + @"\DB\ip2region.db"))
{
Console.WriteLine(_search.MemorySearch("183.129.193.166").Region);
Console.Read();
}
}
Summary
This article was written on a whim. Although I haven’t encountered such a requirement in my current work, it’s always beneficial to understand it. Feel free to leave comments and discuss!