# bluet/proxybroker2 architecture

> Architecture diagram of the GitHub repository bluet/proxybroker2, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-02-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/bluet/proxybroker2
- Repository: https://github.com/bluet/proxybroker2

## Overview

##### Project Type & Purpose
- This is an asynchronous command-line tool that finds public proxies from multiple sources, verifies them concurrently, and can also serve as a local proxy server with automatic proxy rotation.
- Readme details point to CLI commands (“find”, “grab”, “serve”, “update-geo”), asynchronous operations (using aiohttp and aiodns), and components for proxy checking, negotiation, and resolving.

##### File Structure for Key Components
- The top-level directories/files:
- “proxybroker” directory contains the core modules (API, CLI, Checker, Negotiators, Providers, Proxy, Resolver, and Server).
- “tests” directory holds unit and integration tests that imply modularity and internal interactions.
- “examples” directory provides usage examples.
- “docs” folder contains documentation and API details.
- Configuration files (Dockerfile, setup.py, pyproject.toml, and CI workflows) indicate how the project is built, tested, and deployed.
- Notice that there is a clear division between the core business logic in “proxybroker”, external data (the GeoIP database in proxybroker/data), and the CLI/API entry points.

##### Main Components
a. User Interfaces:
- CLI: Command line entry point (proxybroker/cli.py and __main__.py) which interprets commands and options.
- API: HTTP API endpoints for operations like checking proxy info and removal.
b. Core Asynchronous Engine:
- Broker: Manages the asynchronous discovery and checking of proxies (likely coordinated in proxybroker/api.py and proxybroker/checker.py).
- Proxy Finder & Collector: Modules that locate proxies from multiple external sources (proxybroker/providers.py).
- Proxy Checker: Validates proxies asynchronously (proxybroker/checker.py, proxybroker/judge.py).
- Proxy Negotiators & Resolver: Handles the negotiation logic for proxy connections (proxybroker/negotiators.py and proxybroker/resolver.py).
c. Local Proxy Server:
- Server Module: Implements the proxy server functionality for routing requests through found proxies (proxybroker/server.py).
d. External Data & Dependencies:
- GeoIP Database: Provided via “proxybroker/data/GeoLite2-Country.mmdb” and referenced when filtering proxies by location.
- Third-party libraries such as aiohttp, aiodns, and maxminddb for asynchronous networking and data lookups.
e. Asynchronous Processing & Task Management:
- Asynchronous Queue handling and event-loop integration (seen in examples and usage instructions).

##### Relationships & Interactions
- Users trigger commands via the CLI or API endpoints.
- The CLI dispatches commands to the Broker, which orchestrates tasks such as “find”, “grab”, or “serve”.
- The Broker interacts with the Proxy Finder (Providers) to gather proxies, then passes them to the Checker for concurrent validation.
- The Checker, Judge, and Negotiators work together in an asynchronous workflow (using queues) to validate proxy speed, anonymity levels, and protocol support.
- Verified proxies are either returned to the user, stored in a queue for continuous checking, or used by the Server component that acts as a local proxy with automatic rotation.
- External GeoIP data is utilized to filter proxies by country.
- In tests and examples, developers see the async interaction pattern using asyncio queues.

##### Architectural Patterns & Design Principles
- Asynchronous and concurrent design: Multiple asynchronous workers checking proxies concurrently using asyncio.
- Separation of concerns: Different modules handle distinct responsibilities (CLI, discovery, checking, negotiation, serving).
- Modular design: Clearly divided into components that can be individually tested and maintained.
- Extensibility: Use of providers and negotiators makes it easy to add new sources or protocols.

##### Putting It All Together
- Additionally, incorporate external elements such as “GeoIP Database (MaxMind)” connected to:
- The Proxy Checker and Providers which use the data for filtering.
- Auxiliary modules or configuration files (like Docker for container deployment).
- Components used for configuration, testing, and documentation separately to show the broader ecosystem.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Interface Subgraph
    subgraph "User Interface"
        CLI["CLI"]
        MAIN["Main"]
        API["API Endpoints"]
    end

    %% Broker/Coordinator
    BROKER["Broker/Coordinator"]

    %% Asynchronous Engine Subgraph
    subgraph "Async Engine"
        PF["Proxy Finder & Collector"]
        PCJ["Proxy Checker & Judge"]
        NRES["Proxy Negotiators & Resolver"]
    end

    %% Local Proxy Server
    LPS["Local Proxy Server"]

    %% External Data
    GEO["GeoIP Database"]

    %% Connections from User Interface to Broker
    CLI -->|"commands"| BROKER
    MAIN -->|"commands"| BROKER
    API -->|"requests"| BROKER

    %% Broker dispatches to async engine and server
    BROKER -->|"collects proxies"| PF
    BROKER -->|"validates proxies"| PCJ
    BROKER -->|"negotiates connections"| NRES
    BROKER -->|"serves proxies"| LPS

    %% GeoIP connections
    GEO -->|"filters proxies"| PF
    GEO -->|"validates proxy info"| PCJ

    %% Click Events for Component Mapping
    click CLI "https://github.com/bluet/proxybroker2/blob/master/proxybroker/cli.py"
    click MAIN "https://github.com/bluet/proxybroker2/blob/master/proxybroker/__main__.py"
    click API "https://github.com/bluet/proxybroker2/blob/master/proxybroker/api.py"
    click BROKER "https://github.com/bluet/proxybroker2/blob/master/proxybroker/checker.py"
    click PF "https://github.com/bluet/proxybroker2/blob/master/proxybroker/providers.py"
    click PCJ "https://github.com/bluet/proxybroker2/blob/master/proxybroker/checker.py"
    click PCJ "https://github.com/bluet/proxybroker2/blob/master/proxybroker/judge.py"
    click NRES "https://github.com/bluet/proxybroker2/blob/master/proxybroker/negotiators.py"
    click NRES "https://github.com/bluet/proxybroker2/blob/master/proxybroker/resolver.py"
    click LPS "https://github.com/bluet/proxybroker2/blob/master/proxybroker/server.py"
    click GEO "https://github.com/bluet/proxybroker2/blob/master/proxybroker/data/GeoLite2-Country.mmdb"

    %% Styles
    classDef ui fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef broker fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef engine fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef server fill:#f8d7da,stroke:#721c24,stroke-width:2px;
    classDef external fill:#e2e3e5,stroke:#6c757d,stroke-width:2px,stroke-dasharray: 5,5;

    class CLI,MAIN,API ui;
    class BROKER broker;
    class PF,PCJ,NRES engine;
    class LPS server;
    class GEO external;
```
