Get Started With IP2Proxy Zig Module

Dependencies

This library requires an IP2Proxy BIN database to function. You may download the BIN database at

Note

A sample BIN file from https://github.com/ip2location/sample-databases is provided in the test/testdata folder for testing. You are recommended to visit the above links to download the latest BIN database.

This library requires Zig 0.16.0. It memory-maps the BIN file, so the database may be larger than available RAM.


Installation

zig fetch --save git+https://github.com/ip2location/ip2proxy-zig

Then add the module in build.zig:

const ip2proxy = b.dependency("ip2proxy", .{ .target = target, .optimize = optimize });
exe.root_module.addImport("ip2proxy", ip2proxy.module("ip2proxy"));

Sample Codes

Detect proxy information from BIN database

You can query the proxy information from the IP2Proxy BIN database as below:

const std = @import("std");
const ip2proxy = @import("ip2proxy");

pub fn main(init: std.process.Init) !void {
 var db = try ip2proxy.Database.open(init.io, "PX12.BIN");
 defer db.deinit();

 const record = try db.lookup("4.0.0.47");
 std.debug.print("is_proxy={s} type={s} country={s} city={s}\n", .{
 @tagName(record.is_proxy),
 record.proxy_type orelse "-",
 record.country_code orelse "-",
 record.city orelse "-",
 });
}

record.is_proxy is .not_proxy, .proxy or .data_center. Database.open memory-maps the file read-only, and db.deinit releases the mapping. A Record borrows the database’s memory, so keep the database open while you use it.


IP2Proxy Sample Database (BIN)

Sample Packages - BIN File IPv4 + IPv6
IP2Proxy PX1 Download (444.79 kB)
IP2Proxy PX2 Download (469.16 kB)
IP2Proxy PX3 Download (513.00 kB)
IP2Proxy PX4 Download (560.82 kB)
IP2Proxy PX5 Download (589.29 kB)
IP2Proxy PX6 Download (592.90 kB)
IP2Proxy PX7 Download (653.70 kB)
IP2Proxy PX8 Download (665.31 kB)
IP2Proxy PX9 Download (664.74 kB)
IP2Proxy PX10 Download (910.63 kB)
IP2Proxy PX11 Download (920.85 kB)
IP2Proxy PX12 Download (926.18 kB)