VPNDetection Rust Client Library
The official Rust client library for the VPNDetection API.
The library helps you query VPNDetection's APIs for anonymity detection including VPNs, residential proxies, Tor nodes, hosting servers, CDNs, relays and more.
Getting Started
Requires Rust 1.85 or newer. Everything is async and runs on tokio.
Usage
No API key needed to start. The free tier answers ip and is_vpn, and allows 1000 requests per day per source address.
use Client;
let client = new?;
let result = client.lookup.await?;
println!; // true
With an API key
An API key raises your quota, and raises your features on a paid plan. Create one in the console, then pass it in:
let client = builder
.api_key
.build?;
let result = client.lookup.await?;
println!; // true
println!; // Some(true)
if let Some = result.vpn.as_deref
Every setting has a default, and Client::builder() is where you change one:
let client = builder.api_key.concurrency.retries.build?;
Each attempt at a request gives up after 30 seconds, and timeout changes that. A database download isn't bound by it, since a large one runs for minutes:
use Duration;
let client = builder.api_key.timeout.build?;
Your own address
let result = client.my_ip.await?;
println!; // the address we saw this call come from
Same answer lookup would give for that address, and the same cost against your allowance. It is deliberately not cached: which address you are is the whole question, and a machine that moves between networks would otherwise be told where it used to be.
Your plan and usage
let acct = client.my_entitlement.await?;
println!; // max
println!; // 580
println!; // when the allowance resets
Usage counts against the anniversary of your subscription, not the calendar month and not the billing period, and it is the same number a lookup is gated on. hard_limit is None on an uncapped plan, which is not the same as zero.
my_ip_with and my_entitlement_with take this call's own retry budget and timeout, the same as lookup_with.
Batch lookup
Look up many addresses at once. Bogons and cached answers are handled locally, and everything else goes to the batch endpoint in chunks of up to 1000 addresses, in parallel:
use BatchOptions;
let results = client
.lookup_batch
.await;
for in &results
Results are keyed by address and in the order you first listed each one, so duplicates in your list collapse into a single entry and one address failing never loses the rest: it carries its error as its value, with the status the API would have given that address on its own.
There's no limit on how many addresses you pass. How many chunks are in flight at once, how many times a failed chunk is retried, and how long each attempt may take are configurable per call:
let results = client
.lookup_batch
.await;
Caching
Answers are cached by default, so repeat lookups of the same address are free:
let client = new?;
let result = client.lookup.await?;
println!; // true, API request
let result2 = client.lookup.await?;
println!; // true, no API request, result was cached
You can change the default cache variables (max size, TTL, etc) on initialization, or even disable it:
use Duration;
let client = builder.cache.build?;
let client_no_cache = builder.no_cache.build?;
Private and reserved addresses
Private, loopback, link-local, documentation and multicast addresses (and their IPv6 equivalents, including the 6to4 and Teredo ranges) can never be VPN or proxy infrastructure. The library answers them locally, so they cost no request and no quota:
let result = client.lookup.await?;
result.is_bogon; // true, this answer was computed rather than served
result.is_vpn; // false
Because the answer is computed rather than served, it always carries every field, whatever plan you are on. Do not read your plan's shape off a private address.
The check is available on the client, which is handy when your inputs are addresses anyway:
client.is_bogon; // true
client.is_bogon; // false
It is also callable on its own, if you want it without a client:
is_bogon; // true
Errors
Failures return a vpndetection::Error carrying a kind() and a retryable() flag:
use ErrorKind;
match client.lookup.await
kind() is one of BadRequest, Unauthorized, Forbidden, RateLimited, QuotaExceeded, ServerError, Network or Io, the last of which is a database transfer that could not be written or that ended early.
Note that RateLimited and QuotaExceeded both arrive as HTTP 429 and are not the same thing. A rate limit is when the API faces extreme traffic bursts and so retrying later works; but a spent quota needs your allowance raised or the window to roll over. The library retries rate limits for you, but not if your quota is exceeded.
Database downloads
If your key carries the db.download scope, the licensed databases are available through client.database(). A license covers a database FAMILY, so the id you download comes from one of its versions:
use DatabaseFormat;
let families = client.database.list.await?;
let url = client.database.download_url.await?;
let raw = client.database.download_bytes.await?;
let written = client.database.download.await?;
download_url hands back a time-limited link so you can run the transfer yourself. download streams to disk through a neighboring .part file, so nothing bigger than a chunk is ever held in memory and a transfer that dies half way leaves no truncated file. download_bytes holds the whole file in memory, and the catalog runs from cdn_ip_v1 at 10 KB to resproxy_ip_90d_v1 at 1.79 GB, so use download for anything you have not measured.
Sign in with OAuth (device flow)
A program running on the person's own machine can let them sign in with a browser and pick one of their API keys, instead of asking them to paste it:
use ;
let client = new?;
let scope = new.scope;
let device = client.oauth.device_authorization_with.await?;
println!;
let token = client.oauth.poll_device_token.await?;
let Some = token.apikey else ;
let keyed = builder.api_key.build?;
A denied sign-in fails with OauthError::AccessDenied and a code that ran out with OauthError::ExpiredToken. Client IDs are issued on request from support@vpndetection.io, and client.oauth().revoke("your-client-id", &refresh_token) signs the machine out again.
TLS backends
rustls is the default, so the crate builds with no system libraries at all. If you would rather link the platform's TLS, or you already depend on reqwest with its own defaults and want one backend rather than two:
= { = "5", = false, = ["native-tls"] }
Calling from synchronous code
There is no blocking facade, on purpose: reqwest::blocking builds its own runtime and panics when constructed inside one, so a facade would fail for exactly the callers most likely to reach for it. If you have no runtime, make the cost visible instead:
let runtime = new?;
let client = new?;
let result = runtime.block_on?;
Absent is not false
Every field beyond ip and is_vpn is an Option, because your plan decides which of them the API sends. None means "not in your plan", which is not the same answer as Some(false), which means "checked, and no".
result.is_hosting.unwrap_or; // when you only want the flag
result.is_hosting.is_none; // not in your plan
Other Libraries
There are official VPNDetection client libraries available for many languages including PHP, Python, Go, Java, Ruby, and many popular frameworks such as Django, Rails, and Laravel. See our GitHub at https://github.com/vpndetection-io for more.
About VPNDetection
VPN Detection API: Accurate anonymity detection identifying VPNs, residential proxies, hosting servers, Tor nodes, CDNs, relays and more.
License
This project is licensed under the MIT License.