Crate czdb

Source
Expand description

§CZDB Database Library

A Rust library for parsing and querying CZ-format IP geolocation databases.

§Features

  • Supports both IPv4 and IPv6 address queries.
  • Implements efficient binary search for IP data lookup.
  • Optimized memory usage and query performance using memory-mapped files (mmap).

§Usage

  1. Create a Czdb instance by loading the database file and providing a decryption key:
use czdb::Czdb;

let db_path = "path/to/your/czdb_file";
let key = "your_base64_key";
let czdb = Czdb::new(db_path, key).expect("Failed to load database");
  1. Search for IP address geolocation data:
use std::net::IpAddr;

let ip: IpAddr = "8.8.8.8".parse().unwrap();
if let Some(location) = czdb.search(ip) {
    println!("Location for IP {}: {}", ip, location);
} else {
    println!("No location data found for IP {}", ip);
}

§Error Handling

The following errors might occur when loading the database:

  • DatabaseFileReadError: Failed to read the database file.
  • KeyDecodingError: Invalid base64 format for the decryption key.
  • DecryptionError: Decryption failed.
  • InvalidClientId: The client ID in the database file is invalid.
  • DatabaseExpired: The database file has expired.
  • DatabaseFileCorrupted: The database file is corrupted or contains invalid data.

§Notes

  • The database file must be in a supported CZDB format, and the decryption key must be in valid Base64 format.
  • The type of IP address queried must match the database type (IPv4 or IPv6).
  • The database file and key must be obtained from www.cz88.net.

§纯真CZDB解析库

这是一个用于解析和查询 CZDB 格式 IP 地理位置数据库的 Rust 库。

§功能

  • 支持 IPv4 和 IPv6 地址查询。
  • 提供高效的二分查找算法定位 IP 数据。
  • 通过mmap优化内存占用和查询性能

§使用方法

  1. 创建 Czdb 实例,加载数据库文件并提供解密密钥:
use czdb::Czdb;

let db_path = "path/to/your/czdb_file";
let key = "your_base64_key";
let czdb = Czdb::new(db_path, key).expect("Failed to load database");
  1. 查询 IP 地址对应的地理位置数据:
use std::net::IpAddr;
let ip: IpAddr = "8.8.8.8".parse().unwrap();
if let Some(location) = czdb.search(ip) {
    println!("Location for IP {}: {}", ip, location);
} else {
    println!("No location data found for IP {}", ip);
}

§错误处理

加载数据库时可能会遇到以下错误:

  • DatabaseFileReadError: 数据库文件读取失败。
  • KeyDecodingError: 解密密钥格式无效。
  • DecryptionError: 解密失败。
  • InvalidClientId: 数据库文件的客户端 ID 无效。
  • DatabaseExpired: 数据库文件已过期。
  • DatabaseFileCorrupted: 数据库文件损坏或数据无效。

§注意事项

  • 数据库文件需要是支持的 CZDB 格式,且加密密钥需为有效的 Base64 格式。
  • 查询的 IP 地址类型必须与数据库类型 (IPv4 或 IPv6) 匹配。
  • 具体的数据库文件和密钥,请从 www.cz88.net 获取。

Structs§

Czdb
Represents a CZDB database, providing methods to load and search the database for IP geolocation data.

Enums§

CzError
Enum representing possible errors in the CZ database operations.