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

ip2location

IP2Location

Posted on December 6, 2023

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

Intro

Go or Golang, is an open-source programming language created by Google. If you are familiar with the C programming language syntax, then you’ll feel right at home using Go. It has garbage collection and memory safety, in addition to being fast. In addition, it is very useful for concurrency programming.

Developers use Go for a variety of programs like microservices, networking services, web applications, command line interfaces and more. Go developers have a huge variety of packages at their disposal to perform just about any task. Among them is the IP2Location.io Go SDK that provides a wrapper for the IP2Location.io API. The IP2Location.io API enables developers to easily check if an IP address belongs to a Virtual Private Network (VPN) or a residential proxy.

What is a VPN?

So, what is a VPN, you may ask? Well, a VPN server is essentially a proxy server that makes web requests on your behalf like a middleman. It hides your actual IP address from websites so that they are unable to track you. When a user is using a VPN server to browse a website, the user’s request is passed to the VPN server which then makes the request at the website. The response from the website is passed back along the same path. The traffic between the VPN server and the user is encrypted. That means the user’s ISP or other parties are unable to intercept any meaningful data from the network packets. It’s great for privacy reasons.

What is a residential proxy?

Another type of proxy called a residential proxy, performs similarly to the above VPN server. The only difference is that the residential proxy server is usually a home user’s computer vs. the data center machine used in VPN. People usually use residential proxies whenever they wish to hide the fact that they’re using a proxy server.

Why do we need to detect VPN or residential proxy?

As mentioned above, proxy servers serve to obscure the identities of the users. This means that they could be up to no good. Fraudsters frequently use VPN or residential proxy to fool online stores and bypass their fraud checks when buying stuff. People also use proxies to circumvent geofencing to get access to streaming video or music. Operators of such websites definitely need to be able to detect VPN and residential proxies, in order to block them.

Using IP2Location.io to detect VPN and residential proxy

Let’s take a look at how to use the IP2Location.io Go SDK to query the IP2Location.io API to determine if an IP address belongs to a VPN or a residential proxy.

Sign up for the IP2Location.io API key
But, before we can proceed, please make sure to sign up for the API key. You’ll need to subscribe to the Plus plan to be able to have access to the VPN and residential proxy data.

Once you’ve subscribed, you can login to the website and retrieve your API key. We’ll need that to query the API.

Image description

As you can see in the screenshot above, there is an example API query which you can copy & paste into your browser to see the JSON result. This will give you an idea of the raw data returned by the API. You can try out the URL below with your API key and any IP address you wish to query.

E.g., https://api.ip2location.io/?key=<YOUR _API_KEY>&ip=<IP_TO_QUERY>

But in our case, we’ll be using the IP2Location.io Go SDK to parse the raw JSON into a struct for us to easily use. We are going to create a command line program to query the API for a particular IP address and show the results.

Create your testing Go project first if you don’t have one
1) Create a folder and call it test_project.
2) In the command prompt, navigate into that folder.
3) Run go mod init test to initialize the go.mod file.
4) Next, run go get github.com/ip2location/ip2location-io-go to add IP2Location.io Go SDK into the project.
5) After that, create a sub-folder called test inside the test_project folder where we’ll store our test code.
6) Navigate into the test folder.
7) Create a blank text file called test.go which will contain our code.

Basic usage code for the IP2Location.io Go SDK
Head on over to the quick start page below:
https://ip2location-io-go.readthedocs.io/en/latest/quickstart.html

Copy the code and paste into the test.go file.

package main

import (
    "github.com/ip2location/ip2location-io-go/ip2locationio"
    "fmt"
)

func main() {
    apikey := "YOUR_API_KEY"

    config, err := ip2locationio.OpenConfiguration(apikey)

    if err != nil {
        fmt.Print(err)
        return
    }
    ipl, err := ip2locationio.OpenIPGeolocation(config)

    if err != nil {
        fmt.Print(err)
        return
    }

    ip := "8.8.8.8"
    lang := "en" // set to blank string if not applicable
    res, err := ipl.LookUp(ip, lang) // language parameter only available with Plus and Security plans

    if err != nil {
        fmt.Print(err)
        return
    }

    fmt.Printf("IP => %+v\n", res.IP)
    fmt.Printf("CountryCode => %+v\n", res.CountryCode)
    fmt.Printf("CountryName => %+v\n", res.CountryName)
    fmt.Printf("RegionName => %+v\n", res.RegionName)
    fmt.Printf("CityName => %+v\n", res.CityName)
    fmt.Printf("Latitude => %+v\n", res.Latitude)
    fmt.Printf("Longitude => %+v\n", res.Longitude)
    fmt.Printf("ZipCode => %+v\n", res.ZipCode)
    fmt.Printf("TimeZone => %+v\n", res.TimeZone)
    fmt.Printf("Asn => %+v\n", res.Asn)
    fmt.Printf("AS => %+v\n", res.AS)
    fmt.Printf("Isp => %+v\n", res.Isp)
    fmt.Printf("Domain => %+v\n", res.Domain)
    fmt.Printf("NetSpeed => %+v\n", res.NetSpeed)
    fmt.Printf("IddCode => %+v\n", res.IddCode)
    fmt.Printf("AreaCode => %+v\n", res.AreaCode)
    fmt.Printf("WeatherStationCode => %+v\n", res.WeatherStationCode)
    fmt.Printf("WeatherStationName => %+v\n", res.WeatherStationName)
    fmt.Printf("Mcc => %+v\n", res.Mcc)
    fmt.Printf("Mnc => %+v\n", res.Mnc)
    fmt.Printf("MobileBrand => %+v\n", res.MobileBrand)
    fmt.Printf("Elevation => %+v\n", res.Elevation)
    fmt.Printf("UsageType => %+v\n", res.UsageType)
    fmt.Printf("AddressType => %+v\n", res.AddressType)
    fmt.Printf("District => %+v\n", res.District)
    fmt.Printf("AdsCategory => %+v\n", res.AdsCategory)
    fmt.Printf("AdsCategoryName => %+v\n", res.AdsCategoryName)
    fmt.Printf("IsProxy => %+v\n", res.IsProxy)

    fmt.Printf("Continent.Name => %+v\n", res.Continent.Name)
    fmt.Printf("Continent.Code => %+v\n", res.Continent.Code)
    fmt.Printf("Continent.Hemisphere => %+v\n", res.Continent.Hemisphere)
    fmt.Printf("Continent.Translation.Lang => %+v\n", res.Continent.Translation.Lang)
    fmt.Printf("Continent.Translation.Value => %+v\n", res.Continent.Translation.Value)

    fmt.Printf("Country.Name => %+v\n", res.Country.Name)
    fmt.Printf("Country.Alpha3Code => %+v\n", res.Country.Alpha3Code)
    fmt.Printf("Country.NumericCode => %+v\n", res.Country.NumericCode)
    fmt.Printf("Country.Demonym => %+v\n", res.Country.Demonym)
    fmt.Printf("Country.Flag => %+v\n", res.Country.Flag)
    fmt.Printf("Country.Capital => %+v\n", res.Country.Capital)
    fmt.Printf("Country.TotalArea => %+v\n", res.Country.TotalArea)
    fmt.Printf("Country.Population => %+v\n", res.Country.Population)
    fmt.Printf("Country.Currency.Code => %+v\n", res.Country.Currency.Code)
    fmt.Printf("Country.Currency.Name => %+v\n", res.Country.Currency.Name)
    fmt.Printf("Country.Currency.Symbol => %+v\n", res.Country.Currency.Symbol)
    fmt.Printf("Country.Language.Code => %+v\n", res.Country.Language.Code)
    fmt.Printf("Country.Language.Name => %+v\n", res.Country.Language.Name)
    fmt.Printf("Country.Tld => %+v\n", res.Country.Tld)
    fmt.Printf("Country.Translation.Lang => %+v\n", res.Country.Translation.Lang)
    fmt.Printf("Country.Translation.Value => %+v\n", res.Country.Translation.Value)

    fmt.Printf("Region.Name => %+v\n", res.Region.Name)
    fmt.Printf("Region.Code => %+v\n", res.Region.Code)
    fmt.Printf("Region.Translation.Lang => %+v\n", res.Region.Translation.Lang)
    fmt.Printf("Region.Translation.Value => %+v\n", res.Region.Translation.Value)

    fmt.Printf("City.Name => %+v\n", res.City.Name)
    fmt.Printf("City.Translation.Lang => %+v\n", res.City.Translation.Lang)
    fmt.Printf("City.Translation.Value => %+v\n", res.City.Translation.Value)

    fmt.Printf("TimeZoneInfo.Olson => %+v\n", res.TimeZoneInfo.Olson)
    fmt.Printf("TimeZoneInfo.CurrentTime => %+v\n", res.TimeZoneInfo.CurrentTime)
    fmt.Printf("TimeZoneInfo.GmtOffset => %+v\n", res.TimeZoneInfo.GmtOffset)
    fmt.Printf("TimeZoneInfo.IsDst => %+v\n", res.TimeZoneInfo.IsDst)
    fmt.Printf("TimeZoneInfo.Sunrise => %+v\n", res.TimeZoneInfo.Sunrise)
    fmt.Printf("TimeZoneInfo.Sunset => %+v\n", res.TimeZoneInfo.Sunset)

    fmt.Printf("Geotargeting.Metro => %+v\n", res.Geotargeting.Metro)

    fmt.Printf("Proxy.LastSeen => %+v\n", res.Proxy.LastSeen)
    fmt.Printf("Proxy.ProxyType => %+v\n", res.Proxy.ProxyType)
    fmt.Printf("Proxy.Threat => %+v\n", res.Proxy.Threat)
    fmt.Printf("Proxy.Provider => %+v\n", res.Proxy.Provider)
    fmt.Printf("Proxy.IsVpn => %+v\n", res.Proxy.IsVpn)
    fmt.Printf("Proxy.IsTor => %+v\n", res.Proxy.IsTor)
    fmt.Printf("Proxy.IsDataCenter => %+v\n", res.Proxy.IsDataCenter)
    fmt.Printf("Proxy.IsPublicProxy => %+v\n", res.Proxy.IsPublicProxy)
    fmt.Printf("Proxy.IsWebProxy => %+v\n", res.Proxy.IsWebProxy)
    fmt.Printf("Proxy.IsWebCrawler => %+v\n", res.Proxy.IsWebCrawler)
    fmt.Printf("Proxy.IsResidentialProxy => %+v\n", res.Proxy.IsResidentialProxy)
    fmt.Printf("Proxy.IsSpammer => %+v\n", res.Proxy.IsSpammer)
    fmt.Printf("Proxy.IsScanner => %+v\n", res.Proxy.IsScanner)
    fmt.Printf("Proxy.IsBotnet => %+v\n", res.Proxy.IsBotnet)
}
Enter fullscreen mode Exit fullscreen mode

Remember to modify the above code to include your own IP2Location.io API key.
Then navigate to the test folder and run the below command to test the code.

go run test.go

If all goes well, you should see the output like below:

IP => 8.8.8.8
CountryCode => US
CountryName => United States of America
RegionName => California
CityName => Mountain View
Latitude => 37.38605
Longitude => -122.08385
ZipCode => 94035
TimeZone => -08:00
Asn => 15169
AS => Google LLC
Isp => Google LLC
Domain => google.com
NetSpeed => T1
IddCode => 1
AreaCode => 650
WeatherStationCode => USCA0746
WeatherStationName => Mountain View
Mcc => -
Mnc => -
MobileBrand => -
Elevation => 32
UsageType => DCH
AddressType => Anycast
District =>
AdsCategory =>
AdsCategoryName =>
IsProxy => false
Continent.Name => North America
Continent.Code => NA
Continent.Hemisphere => [north west]
Continent.Translation.Lang => en
Continent.Translation.Value => North America
Country.Name => United States of America
Country.Alpha3Code => USA
Country.NumericCode => 840
Country.Demonym => Americans
Country.Flag => https://cdn.ip2location.io/assets/img/flags/us.png
Country.Capital => Washington, D.C.
Country.TotalArea => 9826675
Country.Population => 331002651
Country.Currency.Code => USD
Country.Currency.Name => United States Dollar
Country.Currency.Symbol => $
Country.Language.Code => EN
Country.Language.Name => English
Country.Tld => us
Country.Translation.Lang => en
Country.Translation.Value => United States of America
Region.Name => California
Region.Code => US-CA
Region.Translation.Lang => en
Region.Translation.Value => California
City.Name => Mountain View
City.Translation.Lang => en
City.Translation.Value => Mountain View
TimeZoneInfo.Olson => America/Los_Angeles
TimeZoneInfo.CurrentTime => 2023-11-20T23:52:20-08:00
TimeZoneInfo.GmtOffset => -28800
TimeZoneInfo.IsDst => false
TimeZoneInfo.Sunrise => 06:54
TimeZoneInfo.Sunset => 16:54
Geotargeting.Metro => 807
Proxy.LastSeen => 0
Proxy.ProxyType =>
Proxy.Threat =>
Proxy.Provider =>
Proxy.IsVpn => false
Proxy.IsTor => false
Proxy.IsDataCenter => false
Proxy.IsPublicProxy => false
Proxy.IsWebProxy => false
Proxy.IsWebCrawler => false
Proxy.IsResidentialProxy => false
Proxy.IsSpammer => false
Proxy.IsScanner => false
Proxy.IsBotnet => false
Enter fullscreen mode Exit fullscreen mode

For the purpose of checking if the IP belongs to a VPN or a residential proxy, all you need to do is check the Proxy.IsVPN or the Proxy.IsResidentialProxy inside the result struct. After that, if either is true, then you can decide if you want to block that IP if you’re interested in geofencing. Or if you’re running an online store, you can choose to block them to prevent possible fraudulent transactions.

Conclusion

As you can see from the extensive amount of data returned, you can do a whole lot with the IP2Location.io Go SDK. For instance, you can audit your network security, screen website visitors, prevent fraudsters from making purchases or block online attackers. If you’re curious about what the IP2Location.io API has to offer, you can sign up for the free API plan to start with.


For more tutorials, please visit IP2Location IP Gelocation

Where can I find free IP Geolocation API?

Where can I get free IP Geolocation database?

💖 💪 🙅 🚩
ip2location
IP2Location

Posted on December 6, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related