Skip to main content

Async IP rotation using AWS API Gateway with httpx support

Project description

async-ip-rotator

An async-first Python library that utilizes AWS API Gateway's large IP pool as a proxy to generate pseudo-infinite IPs for web scraping and automated requests. This is an async implementation inspired by requests-ip-rotator, rebuilt with httpx for modern async support.

This library allows you to bypass IP-based rate-limits for sites and services using async/await syntax.

X-Forwarded-For headers are automatically randomized and applied unless given. This is because otherwise, AWS will send the client's true IP address in this header.

AWS' ApiGateway sends its requests from any available IP - and since the AWS infrastructure is so large, it is almost guaranteed to be different each time. By using ApiGateway as a proxy, we can take advantage of this to send requests from different IPs each time. Please note that these requests can be easily identified and blocked, since they are sent with unique AWS headers (i.e. "X-Amzn-Trace-Id").

Installation

This package is on pypi so you can install via any of the following:

  • pip install async-aws-ip-rotator
  • python -m pip install async-aws-ip-rotator

Quick Start

import asyncio
import httpx
from async_aws_ip_rotator import ApiGateway

async def main():
    # Create and use gateway with async context manager
    async with ApiGateway("https://site.com") as gateway:
        # Create async client with gateway
        async with httpx.AsyncClient(transport=gateway) as client:
            # Send request (IP will be randomized)
            response = await client.get("https://site.com/index.php", params={"theme": "light"})
            print(response.status_code)

asyncio.run(main())

Note: The gateway will automatically clean up its resources when the context manager exits.

Costs

API Gateway is free for the first million requests per region, which means that for most use cases this should be completely free. At the time of writing, AWS charges ~$3 per million requests after the free tier has been exceeded. If your requests involve data stream, AWS would charge data transfer fee at $0.09 per GB.

Documentation

AWS Authentication

It is recommended to setup authentication via environment variables. With awscli, you can run aws configure to do this, or alternatively, you can simply set the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY variables yourself.

You can find your access key ID and secret by following the official AWS tutorial.

Creating ApiGateway object

The ApiGateway class can be created with the following optional parameters:

Name Description Required Default
site The site (without path) requests will be sent to True
regions An array of AWS regions to setup gateways in False DEFAULT_REGIONS
access_key_id AWS Access Key ID (will override env variables) False Relies on env variables
access_key_secret AWS Access Key Secret (will override env variables) False Relies on env variables
verbose Include status and error messages False True
from async_ip_rotator import ApiGateway, EXTRA_REGIONS, ALL_REGIONS

# Gateway to outbound HTTP IP and port for only two regions
gateway_1 = ApiGateway("http://1.1.1.1:8080", regions=["eu-west-1", "eu-west-2"])

# Gateway to HTTPS google for the extra regions pack, with specified access key pair
gateway_2 = ApiGateway(
    "https://www.google.com", 
    regions=EXTRA_REGIONS, 
    access_key_id="ID", 
    access_key_secret="SECRET"
)

Starting API gateway

The ApiGateway can be used as an async context manager, which will handle initialization and cleanup automatically:

# Using context manager (recommended)
async with ApiGateway("https://site.com") as gateway:
    # Gateway is ready to use here
    async with httpx.AsyncClient(transport=gateway) as client:
        response = await client.get("https://site.com/path")

# For manual control, you can still use start/shutdown methods
gateway = ApiGateway("https://site.com")
await gateway.start(force=True)  # force=True creates new endpoints even if some exist
# ... use gateway ...
await gateway.shutdown()

Sending Requests

Requests are sent using the httpx AsyncClient with the ApiGateway as transport:

async with httpx.AsyncClient(transport=gateway) as client:
    # Send request with random IP
    response = await client.get("https://site.com/path")
    
    # Send with custom X-Forwarded-For header
    response = await client.get(
        "https://site.com/path",
        headers={"X-Forwarded-For": "127.0.0.1"}
    )

Closing ApiGateway Resources

It's important to shutdown the ApiGateway resources once you have finished with them, to prevent dangling public endpoints that can cause excess charges to your account.

# Shutdown all gateways
await gateway.shutdown()

# Shutdown specific endpoints only
await gateway.shutdown(endpoints=["endpoint1", "endpoint2"])

Please note that any gateways started with require_manual_deletion=True will not be deleted via the shutdown method, and must be deleted manually through either the AWS CLI or Website.

Credit

This project is a fork of requests-ip-rotator by Ge0rg3, reimplemented with async support using httpx.

The core gateway creation and organisation code was adapted from RhinoSecurityLabs' IPRotate Burp Extension.

The X-My-X-Forwarded-For header forwarding concept was originally conceptualised by ustayready in his fireprox proxy.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

async_aws_ip_rotator-1.0.0.tar.gz (19.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

async_aws_ip_rotator-1.0.0-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

Details for the file async_aws_ip_rotator-1.0.0.tar.gz.

File metadata

  • Download URL: async_aws_ip_rotator-1.0.0.tar.gz
  • Upload date:
  • Size: 19.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.21

File hashes

Hashes for async_aws_ip_rotator-1.0.0.tar.gz
Algorithm Hash digest
SHA256 7dce3a3c2372ec9877cae94447b2c726e66b1a31598baea2c92a10e1fb41f977
MD5 8764e9ed74be51cb87434637ece04349
BLAKE2b-256 766c6de1b5e9685f822137832b5dfb67c28a1c74861176c97b4ebe559f3ab399

See more details on using hashes here.

File details

Details for the file async_aws_ip_rotator-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for async_aws_ip_rotator-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 767dd523fe65254c344e03ef46ad30517f980847d084a768720b80185d5bafb6
MD5 770ba49c07cd93d3348eda0ee0487bbb
BLAKE2b-256 08cec02ab5722b33396b27d1a9d272e6ebacf5d82bae9ea9bfd5a84dbafa4c1e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page