How to detect Public Proxy in Flask

Introduction

Public proxies are open proxy servers that anyone can use to route their internet traffic through. While public proxies can serve legitimate purposes, they are also commonly used for malicious activities. Therefore, web administrators often work diligently to detect detect public proxy.

Flask is a lightweight web framework written in Python which makes it easy to develop a web application. It comes with a number of libraries to help web development. It is essential for developers to choose which libraries to use. This is because a good library can help in developing a good website.

Sometimes, developers or web admins will need to manage the accessibility of certain webpages. For example, they may want to block visitors who are using public proxy servers from visiting certain pages. In this case, IP2Proxy Python library may come in handy. In this article, we will show you how to redirect visitors in Flask using the IP2Proxy Python library and the free IP2Proxy LITE BIN database.

Installation

Before we start, you will be required to install the IP2Proxy Python library in your machine. You can install the library using the following command:

pip install IP2Proxy

You will also be required to download the free IP2Proxy LITE BIN database. Rest assured, you can always get the free database from here: https://lite.ip2location.com/ip2proxy-lite.

Steps

  1. Open your Flask application for editing.
  2. Import the IP2Proxy Python library like this: import IP2Proxy
  3. Declare your database path and load the database like this:
db = IP2Proxy.IP2Proxy()
db.open("/path/to/IP2PROXY-LITE-PX11.BIN")
  1. After that, you will need to create a function to listen on a specific path and catch the visitor’s IP address. Then the function will redirect visitors to a different page if the visitor is using a public proxy. For example, if you wish to let the function to check on the home path, you can do like the following:
@app.route('/', methods=['GET'])
def main():
    global record
    if request.environ.get('HTTP_X_FORWARDED_FOR') is None:
        ip = request.environ['REMOTE_ADDR']
    else:
        ip = request.environ['HTTP_X_FORWARDED_FOR']
    if ip == '127.0.0.1': # Only use if you are testing in localhost
        ip = REAL_IP_ADDRESS
    record = db.get_all(ip)
    if (record['country_short'] != '-'): # IP2PROXY-LITE-PX11 contain only PUB proxy type IP addresses.
        return redirect(LINK_TO_PAGE_FOR_VPN_VISITOR, code=302)
    else:
        return('Welcome to this site!')
  1. Next, you will need to create another function to display the page to the visitor which is using a public proxy. For example:
@app.route('/access-denied')
def access_deny():
    return ('<p>Public proxy has been detected! Please do not use any proxy or VPN to access the site.</p>')
  1. Save the changes made in the previous step. After that, in your terminal, cd to the project directory and enter the following command to run the project: flask --app <project_name> run. Substitute the <project_name> with the name of the script that handle the routing. This will run the project at the http://127.0.0.1:5000/. Noted that this will only launches a very simple built-in server and is not suitable to deploy to production environment. To do so, you can refer to https://flask.palletsprojects.com/en/2.2.x/deploying/ to get started.
  2. Now you can go to your browser, and navigate to the link http://127.0.0.1:5000/ to see the outcome. If the project detected a proxy IP, it will be redirected to another page. You will see a similar output displayed in the redirect page.

Note: The free IP2Proxy LITE BIN database contains only public proxy IP addresses. If you wish to validate for other proxy type IP addresses such as Virtual Private Network (VPN) or Search Engine Spider (SES), you will need to get the IP2Proxy commercial database from here: https://ip2location.com/database/ip2proxy

See what are the proxy types supported in IP2Proxy.

Full code of Flask application is shown as below:

import IP2Proxy
from flask import Flask, redirect, render_template, request

app = Flask(__name__)

db = IP2Proxy.IP2Proxy()
db.open("IP2PROXY-LITE-PX11.BIN")

@app.route('/', methods=['GET'])
def main():
    global record
    if request.environ.get('HTTP_X_FORWARDED_FOR') is None:
        ip = request.environ['REMOTE_ADDR']
    else:
        ip = request.environ['HTTP_X_FORWARDED_FOR']
    if ip == '127.0.0.1': # Only use if you are testing in localhost
        ip = REAL_IP_ADDRESS
    record = db.get_all(ip)
    if (record['country_short'] != '-'): # IP2PROXY-LITE-PX11 contain only PUB proxy type IP addresses.
        return redirect(LINK_TO_PAGE_FOR_VPN_VISITOR, code=302)
    else:
        return('Welcome to this site!')

@app.route('/access-denied')
def access_deny():
    return ('<p>Public proxy has been detected! Please do not use any proxy or VPN to access the site.</p>')

Was this article helpful?

Related Articles