DEV Community

Cover image for Using Node.js to check for proxy and VPN with IP2Location.io geolocation API
Camila Morales
Camila Morales

Posted on

Using Node.js to check for proxy and VPN with IP2Location.io geolocation API

Intro

Node.js is a popular backend JavaScript runtime environment. It allows developers to run JavaScript codes outside of browsers via the V8 JavaScript engine. There are many packages available for use with Node.js and they can be installed using npm which is the Node Package Manager. One of the useful packages is the IP2Location.io Node.js SDK. This easy-to-use package is a wrapper for the IP2Location.io API that allows developers to query IP geolocation data for a given IP address. In addition, it also can tell the user whether that IP address belongs to a proxy server and if so, what kind of proxy server.

Popular uses for geolocation data

As Node.js can also be used to power websites, having this ability to query IP geolocation is very important. Websites need to be protected against online attackers and by using the IP2Location.io Node.js SDK, it is easy to block attackers by country, region, city and so forth. Marketing folks also perform sales analysis using geolocation to determine how promotional efforts are faring by region. Online stores frequently incorporate IP geolocation for online orders to validate if the user’s billing or shipping address matches the IP geolocation country. There are many uses for geolocation data and using the IP2Location.io API via the Node.js SDK allows developers to quickly leverage the real-time geolocation to accomplish their tasks.

Detecting proxy servers like VPN or residential proxies

Virtual Private Network (VPN) servers are a type of proxy server. Along with residential proxies, they are used to hide one’s identity when surfing on websites. The proxy servers act as middlemen to relay requests from the user to the website and vice-versa. As far as the website knows, the proxy server is the end user. The main difference between VPN and residential proxy is that the VPN server is usually located inside a data center while the residential proxy is running on a home user’s computer. Detecting proxy servers like VPN or residential proxies is effortless when using the IP2Location.io Node.js SDK.

Why is detecting proxies important?

Website operators need to be able to tell if someone is browsing their website while using a VPN or a residential proxy. Aside from privacy reasons, proxy servers are also commonly used by fraudsters to get up to no good. For example, fraudsters will mask their online presence using a VPN when they want to buy things online using a stolen credit card. If the merchant can tell that their IP address is coming from a VPN server, then they have the option to block the order, thereby preventing possible fraud. Same goes for when someone is using a residential proxy. Residential proxies, being home computers, are notoriously hard to label as a proxy. After all, the home user could also be surfing the web using that computer in addition to proxying external traffic. Fortunately, the IP2Location.io API can show if the user is using a residential proxy.

Let’s see how to query geolocation and proxy with the IP2Location.io Node.js SDK

First of all, make sure you have an IP2Location.io API key as the SDK will require that to function. If you don’t have one, just head to IP2Location.io to get the free API plan. For basic geolocation data, the free API plan is more than sufficient. You will also need to have Node.js and npm installed. We won’t cover that in this article. Refer to https://nodejs.org/en/download for installation steps.

Create your project first

First of all, create a folder called test_project and navigate into that folder using the command line. Then create a blank file called test.mjs where we will store our testing code.

Installing the IP2Location.io Node.js SDK

Run the below command to install the IP2Location.io Node.js SDK into the test_project folder.
npm install ip2location-io-nodejs

Sample code to query IP2Location.io

We’ll use the sample code from https://ip2location-io-nodejs.readthedocs.io/en/latest/quickstart.html#installation for our testing.

Copy & paste the below code into the test.mjs file and edit the code to put in your IP2Location.io API key. Then save the file.

import ip2locationio from "ip2location-io-nodejs";
// Configures IP2Location.io API key
let mykey = "YOUR_API_KEY";
let config = new ip2locationio.Configuration(mykey);
let ipl = new ip2locationio.IPGeolocation(config);
let myip = "8.8.8.8";
let lang = "en"; // language parameter is only available for Plus and Security plans
// Lookup ip address geolocation data
ipl.lookup(myip, lang)
  .then((data) => {
    // print the data in json format
    console.log(data)
  })
  .catch((error) => {
    // print the error
    console.log(error)
  });
Enter fullscreen mode Exit fullscreen mode

Run the below command to execute the script and retrieve geolocation & proxy data for the 8.8.8.8 IP address.
node test.mjs

Please note that we are using the Security plan for our API key. Hence, you’ll see all available data fields including proxy details in the results below. You will see less fields if you’re not subscribed to the Security plan.

{
  ip: '8.8.8.8',
  country_code: 'US',
  country_name: 'United States of America',
  region_name: 'California',
  city_name: 'Mountain View',
  latitude: 37.38605,
  longitude: -122.08385,
  zip_code: '94035',
  time_zone: '-08:00',
  asn: '15169',
  as: 'Google LLC',
  isp: 'Google LLC',
  domain: 'google.com',
  net_speed: 'T1',
  idd_code: '1',
  area_code: '650',
  weather_station_code: 'USCA0746',
  weather_station_name: 'Mountain View',
  mcc: '-',
  mnc: '-',
  mobile_brand: '-',
  elevation: 32,
  usage_type: 'DCH',
  address_type: 'Anycast',
  continent: {
    name: 'North America',
    code: 'NA',
    hemisphere: [ 'north', 'west' ],
    translation: { lang: 'en', value: 'North America' }
  },
  district: 'Santa Clara County',
  country: {
    name: 'United States of America',
    alpha3_code: 'USA',
    numeric_code: 840,
    demonym: 'Americans',
    flag: 'https://cdn.ip2location.io/assets/img/flags/us.png',
    capital: 'Washington, D.C.',
    total_area: 9826675,
    population: 331002651,
    currency: { code: 'USD', name: 'United States Dollar', symbol: '$' },
    language: { code: 'EN', name: 'English' },
    tld: 'us',
    translation: { lang: 'en', value: 'United States of America' }
  },
  region: {
    name: 'California',
    code: 'US-CA',
    translation: { lang: 'en', value: 'California' }
  },
  city: {
    name: 'Mountain View',
    translation: { lang: 'en', value: 'Mountain View' }
  },
  time_zone_info: {
    olson: 'America/Los_Angeles',
    current_time: '2023-11-21T22:43:22-08:00',
    gmt_offset: -28800,
    is_dst: false,
    sunrise: '06:55',
    sunset: '16:53'
  },
  geotargeting: { metro: '807' },
  ads_category: 'IAB19-11',
  ads_category_name: 'Data Centers',
  is_proxy: false,
  proxy: {
    last_seen: 21,
    proxy_type: 'DCH',
    threat: '-',
    provider: '-',
    is_vpn: false,
    is_tor: false,
    is_data_center: true,
    is_public_proxy: false,
    is_web_proxy: false,
    is_web_crawler: false,
    is_residential_proxy: false,
    is_spammer: false,
    is_scanner: false,
    is_botnet: false
  }
}
Enter fullscreen mode Exit fullscreen mode

If you want the geolocation details, you can get all the details from the top-level fields.
E.g.
console.log(data.country_code);
console.log(data.region_name);
console.log(data.city_name);

To access the proxy detection results, the proxy field contains all the relevant proxy details.

Specifically, for detecting VPN and residential proxies, your code can check the data.proxy.is_vpn and the data.proxy.is_residential_proxy fields.
E.g.
console.log(data.proxy.is_vpn);
console.log(data.proxy.is_residential_proxy);

If either is true, then you can decide if you wish to block that user or not.

Conclusion

Being able to integrate IP geolocation and proxy data into your website or program in real-time is very useful. These data are key in maintaining security, performing analytics, for regulatory compliance and so much more. Using the IP2Location.io Node.js SDK makes it easy to quickly introduce such features within your own codes.

Top comments (0)