How to detect Residential proxies with Rust and IP2Location.io API?

How to detect Residential proxies with Rust and IP2Location.io API?

Residential proxy has become popular among hackers today. Explore how to use IP2Location.io API in Rust project to prevent illegal access from hackers

·

6 min read

Introduction

Rust is a general purpose programming language, which is designed for prioritizing performance, concurrency and type safety. Rust requires all the references to be pointed to valid memory, hence eliminating the need to use a garbage collector to manage the memory automatically. Rust is widely used in systems programming by developers.

About Rust

Rust is created by a software developer called Graydon Hoare. He started it as a personal project while working at Mozilla Research back in 2006. As a part of Servo, an experimental browser engine’s development process, Mozilla sponsored Rust in 2009. The sponsorship continued until August 2020, when Mozilla had sacked 250 employees due to the effect of the Covid-19 pandemic. Among the dismissed employees were members of the Servo project. A solution was found with the formation of the Rust foundation to take over the financial responsibility of all the costs, and also the ownership of all trademarks and domain names. The formation was supported by five big companies, which are AWS, Huawei, Google, Microsoft, and Mozilla(https://foundation.rust-lang.org/news/2021-02-08-hello-world/). Today, Rust is becoming the most admired language, and more than 80% of the developers want to use it again in the coming years.(https://survey.stackoverflow.co/2023/#section-admired-and-desired-programming-scripting-and-markup-languages)

Why is Rust so popular among the developers? One of the reasons would be the enforcement of memory safety. This prevents some common programming errors like null pointer dereferences and data races. Besides that, Rust also has a robust and helpful community. Not only is its ecosystem continually growing, but it also allows developers to create and share reusable code through a package manager called Crates. By using Cargo, Rust developers can save their effort in managing the dependencies of their Rust project. Lastly, Rust is adaptable. It can be used for a variety of fields, such as web development or system programming. Hence it attracts developers who have different interests and needs.

In the previous paragraph, we get to know why developers tend to choose Rust in their projects. However, it is common that developers need some additional features that did not exist in the Rust standard libraries. For example, they may want to get location information for a certain IP address. Fortunately, they can get help from external API to achieve their goal. Let’s meet a powerful geolocation lookup tool - IP2Location.io.

About IP2Location.io

IP2Location.io API is a powerful web service that lets developers smoothly integrate precise and comprehensive IP geolocation information. This includes information about the country, region, city, proxy, coordinate, ISP and more. IP2Location.io API is useful in different scenarios, including redirection, blocking, online advertising and online security enhancement. By making use of IP2Location.io API, developers are able to verify the IP address based on the geolocation result returned. For example, they can check whether the IP address belongs to a residential proxy or not.

About residential proxy

A residential proxy is a type of proxy server that acts as an intermediary server for the internet connection between your device and the website you would like to connect to. Residential proxies use IP addresses assigned by an Internet service provider or ISP, unlike other types of proxy servers which use IP addresses from a data center. Those IP addresses used by residential proxies usually will belong to a home user, for example a landed house. Users can use a residential proxy to hide their real IP address and get access to geo-restricted content.

While residential proxies are benefiting certain users, it is bringing issues to website owners and administrators. One of the reasons would be some users used the residential proxy in a high volume when accessing the website contents, resulting in a high load on the server and may cause server resources to be not enough to cater to other normal visitors. Also, some spammers will hide their IP address when doing some illegal stuff. It gives site administrators a hard time defending when the spammers used residential proxies in their attacks. But be assured that site administrators can solve this problem easily by making use of the IP2Location.io API.

In the next section, we will go through how to use IP2Location.io API in Rust. You will be guided on how to obtain your API Key through IP2Location.io dashboard, setting up the key and calling the API in Python, and finally manipulate the result returned by the API. Before we continue, kindly make sure that you have subscribed for an IP2Location.io plan if you haven’t.

Steps on how to use IP2Location.io API in Rust

  1. Create a new Rust project by using Cargo with this command: cargo new lookup. Change the path to the project afterwards by using cd lookup.

  2. Open the cargo.toml in your editor of choice, and add the following code into the cargo.toml under the section:

     reqwest = { version = "0.11", features = ["blocking", "json"] }
     serde = { version = "1.0", features = ["derive"] }
     tokio = { version = "1", features = ["full"] }
    
  3. After that, open the src/main.rs in your editor. We will first import the library that we are going to use like this:

use reqwest;
use serde::Deserialize;
use serde::Serialize;
  1. Next, we will need to define the structure of the response from IP2Location.io API before we decode the response. We can achieve this by using the following code:
#[derive(Debug, Deserialize)]
pub struct IpGeolocationRecord {
ip: String,
country_code: String,
country_name: String,
region_name: String,
city_name: String,
latitude: Option<f32>,
longitude: Option<f32>,
zip_code: String,
time_zone: String,
asn: String,
ras: String,
isp: Option<String>,
domain: Option<String>,
net_speed: Option<String>,
idd_code: Option<String>,
area_code: Option<String>,
weather_station_code: Option<String>,
weather_station_name: Option<String>,
mcc: Option<String>,
mnc: Option<String>,
mobile_brand: Option<String>,
elevation: Option<i64>,
usage_type: Option<String>,
address_type: Option<String>,
continent: Option<Continentinforecord>,
district: Option<String>,
country: Option<Countryinforecord>,
region: Option<Regioninforecord>,
city: Option<Cityinforecord>,
time_zone_info: Option<Tzinforecord>,
geotargeting: Option<Geoinforecord>,
ads_category: Option<String>,
ads_category_name: Option<String>,
is_proxy: Option<bool>,
proxy: Option<Proxyinforecord>,
}

#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
pub struct Continentinforecord {
name: Option<String>,
code: Option<String>,
hemisphere: [Option<String>; 2],
translation: Translationrecord,
}

#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
pub struct Countryinforecord {
name: Option<String>,
alpha3_code: Option<String>,
numeric_code: Option<i32>,
demonym: Option<String>,
flag: Option<String>,
capital: Option<String>,
total_area: Option<f32>,
population: Option<i64>,
currency: Currencyrecord,
language: Langrecord,
tld: Option<String>,
translation: Translationrecord,
}

#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
pub struct Regioninforecord {
name: Option<String>,
code: Option<String>,
translation: Translationrecord,
}

#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
pub struct Cityinforecord {
name: Option<String>,
translation: Translationrecord,
}

#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
pub struct Tzinforecord {
olson: Option<String>,
current_time: Option<String>,
gmt_offset: Option<i32>,
is_dst: Option<bool>,
sunrise: Option<String>,
sunset: Option<String>,
}

#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
pub struct Geoinforecord {
metro: Option<String>,
}

#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
pub struct Proxyinforecord {
last_seen: Option<i32>,
proxy_type: Option<String>,
threat: Option<String>,
provider: Option<String>,
is_vpn: Option<bool>,
is_tor: Option<bool>,
is_data_center: Option<bool>,
is_public_proxy: Option<bool>,
is_web_proxy: Option<bool>,
is_web_crawler: Option<bool>,
is_residential_proxy: Option<bool>,
is_spammer: Option<bool>,
is_scanner: Option<bool>,
is_botnet: Option<bool>,
}

#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
pub struct Currencyrecord {
code: Option<String>,
name: Option<String>,
symbol: Option<String>
}

#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
pub struct Translationrecord {
lang: Option<String>,
value: Option<String>,
}

#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
pub struct Langrecord {
code: Option<String>,
name: Option<String>
}
  1. After that we can write the code to query from IP2Location.io API. in your main(), type the following code to do so:
let client = reqwest::Client::new();
let url = format!(           
            "https://api.ip2location.io/?key={}&ip={}&format=json",
            "PASTE_YOUR_API_KEY", "8.8.8.8"
        );

let res = client
        .get(url)
        .send()
        .await
        .expect("failed to get response")
        .json::<IpGeolocationRecord>()
        .await
        .expect("failed to get payload");
  1. You can print out the response by using this code:
    println!("{:#?}", res);

  2. Now we will try to check for residential proxy by using the API response. You can do the checking by using the following code:

     if res.proxy.clone().unwrap().is_residential_proxy.unwrap() {
     println!("The content is restricted to non-proxy visitors only.");
    

Conclusion

To summarize, Rust is a powerful programming language. It has a robust and helpful community, and a variety of standard libraries to help developers to kick off their projects. By consolidating IP2Location.io API in Rust, developers can carry out additional functions in their projects. For instance, they can verify whether an IP address is a proxy or not, and take action accordingly.