To detect if an IP address is a VPN using IP2Location.io in Erlang, you can use the IP2Location.io API to retrieve geolocation and related data.
sudo apt install erlang-jiffy
-module(test).
-export([runtest/0]).
runtest() ->
Key = "YOUR_API_KEY",
IP = "8.8.8.8",
ssl:start(),
inets:start(),
MyParams = uri_string:compose_query([{"format", "json"}, {"key", Key}, {"ip", IP}]),
case httpc:request(get, {"https://api.ip2location.io/?" ++ MyParams, []}, [{ssl, [{versions, ['tlsv1.2']}]}, {autoredirect, false}], []) of
{ok, {{_, 200, _}, _, Body}} ->
Result = jiffy:decode(unicode:characters_to_binary(Body,unicode,utf8),[return_maps]),
case maps:is_key(<<"proxy">>, Result) of
true ->
Proxy = maps:get(<<"proxy">>, Result),
IsVpn = maps:get(<<"is_vpn">>, Proxy),
case IsVpn of
true ->
io:format("The IP ~s is a VPN.~n", [IP]);
_ ->
io:format("The IP ~s is NOT a VPN.~n", [IP])
end;
_ ->
io:format("ERROR: The is_vpn field requires a paid subscription to the Security plan.~n", [])
end;
{ok, {{_, 400, _}, _, Body}} ->
Result = jiffy:decode(unicode:characters_to_binary(Body,unicode,utf8),[return_maps]),
case maps:is_key(<<"error">>, Result) of
true ->
Error = maps:get(<<"error">>, Result),
io:format("ERROR: ~s~n", [maps:get(<<"error_message">>, Error)]);
_ ->
io:format("ERROR: ~p~n", [Result])
end;
{ok, {{_, 401, _}, _, Body}} ->
Result = jiffy:decode(unicode:characters_to_binary(Body,unicode,utf8),[return_maps]),
case maps:is_key(<<"error">>, Result) of
true ->
Error = maps:get(<<"error">>, Result),
io:format("ERROR: ~s~n", [maps:get(<<"error_message">>, Error)]);
_ ->
io:format("ERROR: ~p~n", [Result])
end;
{error, Reason} ->
io:format("ERROR ~p~n", [Reason])
end.
erl
c(test).
test:runtest().
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