To detect if an IP address is a VPN using IP2Location.io in OCaml, you can use the IP2Location.io API to retrieve geolocation and related data.
dune init proj example
cd example
opam install lwt cohttp cohttp-lwt-unix yojson
(executable
(public_name example)
(name main)
(libraries example lwt cohttp cohttp-lwt-unix yojson))
open Printf
open Lwt
open Cohttp
open Cohttp_lwt_unix
open Yojson
let () =
let ip = "8.8.8.8" in
let key = "YOUR_API_KEY" in
let uri =
Uri.of_string
("https://api.ip2location.io/?format=json&key=" ^ key ^ "&ip=" ^ ip)
in
Lwt_main.run
( Client.get uri >>= fun (resp, body) ->
let code = resp |> Response.status |> Code.code_of_status in
let json_promise = body |> Cohttp_lwt.Body.to_string in
json_promise >|= fun json_string ->
let json = Basic.from_string json_string in
let open Yojson.Basic.Util in
match code with
| 200 when member "proxy" json <> `Null -> (
match json |> member "proxy" |> member "is_vpn" |> to_bool with
| true -> printf "The IP %s is a VPN.\n" ip
| _ -> printf "The IP %s is NOT a VPN.\n" ip)
| 200 ->
printf
"ERROR: The is_vpn field requires a paid subscription to the \
Security plan.\n"
| (400 | 401) when member "error" json <> `Null ->
json |> member "error" |> member "error_message" |> to_string
|> printf "ERROR: %s\n"
| _ -> printf "HTTP Code: %d\n" code )
dune build
dune exec example
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