# IP Management Architecture

## System Architecture Diagram

```mermaid
graph TB
    subgraph "RateLoader Application"
        GUI[GUI Application]
        HTTP[HTTP Server]
        IPMgr[IP Manager]
        Config[Configuration]
        IPFile[ip_access.json]
    end

    subgraph "External Clients"
        Client1[Client 192.168.1.100]
        Client2[Client 10.0.0.50]
        Client3[Client 172.16.0.25]
    end

    subgraph "IP Management GUI"
        IPTab[IP Management Tab]
        IPList[IP Address List]
        GlobalSwitch[Global Access Switch]
        ActionButtons[Action Buttons]
    end

    Client1 --> HTTP
    Client2 --> HTTP
    Client3 --> HTTP

    HTTP --> IPMgr
    IPMgr --> IPFile
    IPMgr --> Config

    GUI --> IPTab
    IPTab --> IPList
    IPTab --> GlobalSwitch
    IPTab --> ActionButtons

    ActionButtons --> IPMgr
    IPMgr --> IPList
    Config --> IPMgr

    style IPMgr fill:#e1f5fe
    style IPFile fill:#f3e5f5
    style IPTab fill:#e8f5e8
```

## Request Flow Diagram

```mermaid
sequenceDiagram
    participant Client
    participant HTTP
    participant IPMgr
    participant IPFile
    participant RateLoader

    Client->>HTTP: HTTP Request
    HTTP->>IPMgr: is_ip_allowed(client_ip)
    IPMgr->>IPFile: Load IP data
    IPFile-->>IPMgr: Return IP data
    IPMgr->>IPMgr: Check access rules
    IPMgr-->>HTTP: Return access decision

    alt Access Allowed
        HTTP->>IPMgr: record_request(client_ip, request_type)
        IPMgr->>IPFile: Update IP metadata
        HTTP->>RateLoader: Process request
        RateLoader-->>HTTP: Return response
        HTTP-->>Client: HTTP Response
    else Access Denied
        HTTP-->>Client: 403 Forbidden
    end
```

## IP Management Data Flow

```mermaid
graph LR
    subgraph "IP Data Storage"
        JSON[ip_access.json]
    end

    subgraph "IP Manager Operations"
        Check[Check Access]
        Record[Record Request]
        Update[Update Status]
        Global[Set Global Mode]
    end

    subgraph "GUI Operations"
        Display[Display IP List]
        Toggle[Toggle IP Status]
        Remove[Remove IP]
        Export[Export List]
    end

    Check --> JSON
    Record --> JSON
    Update --> JSON
    Global --> JSON

    Display --> JSON
    Toggle --> Update
    Remove --> JSON
    Export --> JSON

    style JSON fill:#f3e5f5
    style Check fill:#e1f5fe
    style Record fill:#e1f5fe
    style Update fill:#e1f5fe
    style Global fill:#e1f5fe
```

## Access Control Logic

```mermaid
graph TD
    Start[Incoming Request] --> CheckGlobal{Global Access Mode}

    CheckGlobal -->|Restricted| CheckWhitelist{IP in Whitelist?}
    CheckGlobal -->|Allowed| CheckBlacklist{IP in Blacklist?}

    CheckWhitelist -->|Yes| Allow[Allow Request]
    CheckWhitelist -->|No| Deny[Deny Request]

    CheckBlacklist -->|Yes| Deny
    CheckBlacklist -->|No| Allow

    Allow --> Record[Record Request]
    Deny --> Log[Log Denied Request]

    Record --> Process[Process Normally]
    Log --> End[End]
    Process --> End
```
