To detect if an IP address is a VPN using IP2Location.io in D, you can use the IP2Location.io API to retrieve geolocation and related data.
import std.stdio;
import std.uri;
import std.net.curl;
import std.json;
import std.conv;
void main()
{
auto key = "YOUR_API_KEY";
auto ip = "8.8.8.8";
ushort code;
auto reason = "";
auto url = "https://api.ip2location.io/?format=json&key=" ~ key ~ "&ip=" ~ ip;
string content = "";
auto client = HTTP(url); // using "HTTP" instead of just "get" due to the need to read the page content when HTTP Status <> 200
client.onReceive = (ubyte[] data) {
content = content ~ to!string(cast(char[])(data));
return data.length;
};
client.onReceiveStatusLine = (HTTP.StatusLine status) {
code = status.code;
reason = status.reason;
};
client.perform();
if (code == 200)
{
JSONValue result = parseJSON(content);
if ("proxy" in result)
{
if (result["proxy"]["is_vpn"].boolean)
{
writefln("The IP %s is a VPN.", ip);
}
else
{
writefln("The IP %s is NOT a VPN.", ip);
}
}
else
{
writeln("ERROR: The is_vpn field requires a paid subscription to the Security plan.");
}
}
else if ((code == 400) || (code == 401))
{
JSONValue result = parseJSON(content);
if ("error" in result)
{
writefln("ERROR: %s", result["error"]["error_message"].str);
}
else
{
writefln("ERROR: %s", reason);
}
}
else
{
writeln("ERROR: Unable to call API.");
}
}
dmd test.d
./test
This script will check if the specified IP address is a VPN. Make sure to replace 8.8.8.8 with the IP address you want and replace YOUR_API_KEY to your own API key.
Empower your applications with accurate IP geolocation information now.
Try It for Free