Skip to main content

AI API request/response filter gateway based on mitmproxy — transparent path desensitization for LLM prompts.

Project description

ai-filter-gateway

AI API request/response filter gateway — transparent path desensitization proxy based on mitmproxy.

A lightweight MITM proxy that automatically detects and masks real file paths in LLM API requests and responses, preventing local path information leakage to large language models.

Python 3.10+ License: MIT Status: Alpha

中文版


Features

Feature Description
Request Masking Intercepts real file paths in LLM API request bodies, replacing them with unique placeholders (e.g. /data/ws/1)
Response Restoration Intercepts JSON or SSE streaming responses and restores placeholders to real paths
Reverse Proxy Supports reverse mode, directly proxying to target AI API server
Upstream Proxy (Transit) Supports upstream mode, transit through another proxy (e.g. Clash, Shadowsocks)
Outbound Proxy Configurable HTTP/HTTPS outbound proxy (e.g. Clash, Shadowsocks)
Cross-Platform macOS / Linux / Windows

How It Works

Reverse Proxy Mode (Default)

User App                          ai-filter-gateway                      AI API Server
   |                                      |                                       |
   |── POST /chat/completions ─────────> |  <--- reverse proxy ---->       |
   |   {"prompt": "file: /Users/x/a.py"}|                                       |
   |                                      |  [Mask] /Users/x/a.py -> /data/ws/1 |
   |                                      |── POST /chat/completions ────>    |
   |                                      |   {"prompt": "file: /data/ws/1"}  |
   |                                      |   {"response": "see /data/ws/1"}  |
   |                                      |  [Restore] /data/ws/1 -> /Users/x/a.py |
   |<-- {"response": "see /Users/x/a.py"}--                                     |

Upstream Proxy Mode (Transit)

User App                          ai-filter-gateway                    Upstream Proxy                 AI API Server
   |                                      |                                   |                            |
   |── POST /chat/completions ─────────> |  <--- upstream proxy ---->  |                            |
   |   {"prompt": "file: /Users/x/a.py"}|                                   |                            |
   |                                      |  [Mask] /Users/x/a.py -> /data/ws/1 |                            |
   |                                      |── POST /chat/completions ────> |                            |
   |                                      |   {"prompt": "file: /data/ws/1"}  |                            |
   |                                      |   {"response": "see /data/ws/1"}  |                            |
   |                                      |  [Restore] /data/ws/1 -> /Users/x/a.py |                            |
   |<-- {"response": "see /Users/x/a.py"}--                                    |                            |

Installation

Prerequisites

Install

git clone https://github.com/your-username/ai-filter-gateway.git
cd ai-filter-gateway
python -m venv .venv
source .venv/bin/activate   # Windows: .venv\Scripts\activate
pip install -e .

Usage

Option 1: Bash Script (Recommended)

# Start proxy (default: reverse mode to https://api.openai.com)
./ai-proxy start

# Start in upstream mode (transit through another proxy)
AI_PROXY_MODE=upstream AI_PROXY_TARGET=http://127.0.0.1:7890 ./ai-proxy start

# Check status
./ai-proxy status

# View logs
./ai-proxy logs

# Stop proxy
./ai-proxy stop

# Restart proxy
./ai-proxy restart

Option 2: Python Module

# Start (default: reverse proxy to https://api.openai.com)
python -m ai_filter_gateway

# Reverse proxy to custom target
python -m ai_filter_gateway --mode reverse:https://api.openai.com

# Upstream proxy mode (transit through another proxy)
python -m ai_filter_gateway --mode upstream:http://127.0.0.1:7890

# Custom port
python -m ai_filter_gateway --listen-port 8080

# Configure outbound proxy (reverse mode only)
python -m ai_filter_gateway --outbound-proxy http://127.0.0.1:7890

Option 3: Direct mitmproxy Load

# Reverse proxy mode
mitmproxy -s src/ai_filter_gateway/path_mask.py --mode reverse:https://api.openai.com
mitmdump -s src/ai_filter_gateway/path_mask.py --mode reverse:https://api.openai.com

# Upstream proxy mode
mitmproxy -s src/ai_filter_gateway/path_mask.py --mode upstream:http://127.0.0.1:7890
mitmdump -s src/ai_filter_gateway/path_mask.py --mode upstream:http://127.0.0.1:7890

Client Configuration

Point your AI client's API address to the proxy:

# Example: OpenAI SDK
export OPENAI_BASE_URL="http://127.0.0.1:7878"

# Example: Custom API call
curl -X POST http://127.0.0.1:7878/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{"prompt": "Analyze this file /Users/name/project/config.py"}'

Configuration

Environment Variables

Variable Default Description
AI_PROXY_MODE reverse Proxy mode: reverse or upstream
AI_PROXY_TARGET https://api.openai.com Target URL (reverse mode) or upstream proxy URL (upstream mode)
AI_PROXY_HOST 127.0.0.1 Listen address
AI_PROXY_PORT 7878 Listen port
AI_PROXY_OUTBOUND_PROXY (empty) Outbound proxy address (reverse mode only)

Code Configuration

Edit constants in src/ai_filter_gateway/path_mask.py:

# Hostname whitelist (empty = monitor all)
MONITOR_HOSTNAMES: list[str] = []
# Example: ["api.openai.com", "localhost:11434"]

# Placeholder path format
PH_PREFIX = "/data/ws/"
PH_SUFFIX = ""

Path Detection Rules

Type Example Matched
Unix absolute path /Users/name/project/file.py
Unix relative path ./data/test.txt
Windows path C:\Users\name\app.exe
URL path segment https://example.com/api/v1
Date format 2026/07/08
Pure numeric path /123/456/789

Project Structure

ai-filter-gateway/
├── src/
│   └── ai_filter_gateway/
│       ├── __init__.py          # Package definition
│       ├── __main__.py          # CLI entry point
│       └── path_mask.py         # Core filter logic
├── tests/
│   └── test_path_mask.py        # Unit tests
├── .github/workflows/
│   └── ci.yml                   # CI configuration
├── ai-proxy                     # Bash management script
├── pyproject.toml               # Project metadata
├── requirements.txt             # Dependency list
├── README.md                    # English documentation
├── README_CN.md                 # Chinese documentation
├── LICENSE                      # MIT License
├── CHANGELOG.md                 # Changelog
├── CONTRIBUTING.md              # Contribution guide
└── Dockerfile                   # Container deployment

Development

Run Tests

pip install pytest
pytest tests/ -v

Code Style

This project uses Ruff for linting:

pip install ruff
ruff check src/ tests/

FAQ

Q: How to monitor only specific AI API endpoints?

Modify the MONITOR_HOSTNAMES list:

MONITOR_HOSTNAMES = ["api.openai.com", "localhost:11434"]

Q: Does the proxy impact performance?

Text replacement overhead is minimal, adding <1ms per request. Streaming responses are processed chunk-by-chunk with no noticeable latency.

Q: Is Windows supported?

Yes. On Windows, run .\ai-proxy in PowerShell or CMD. Windows path formats (C:\path\to\file) are correctly detected and masked.

Q: How to disable path masking?

Comment out or remove the addons = [PathMask()] line. Alternatively, set MONITOR_HOSTNAMES to not include your target host.


License

MIT License — see LICENSE.


Contributing

Issues and Pull Requests are welcome! See CONTRIBUTING.md.


Related Projects

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

ai_filter_gateway-0.1.0.tar.gz (19.8 kB view details)

Uploaded Source

Built Distribution

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

ai_filter_gateway-0.1.0-py3-none-any.whl (15.7 kB view details)

Uploaded Python 3

File details

Details for the file ai_filter_gateway-0.1.0.tar.gz.

File metadata

  • Download URL: ai_filter_gateway-0.1.0.tar.gz
  • Upload date:
  • Size: 19.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for ai_filter_gateway-0.1.0.tar.gz
Algorithm Hash digest
SHA256 87d6a557a384f50b69482e62590f4805c2b2772722603cb6b93977e23af13059
MD5 ef6724dccef20f2af3430fd645b99a11
BLAKE2b-256 f8133027bba09c30be919198bf5151a63ef553f8ae681c861a2f09b164a162df

See more details on using hashes here.

File details

Details for the file ai_filter_gateway-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for ai_filter_gateway-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fede2473c509e6afdb1165edc118fc94c1fa2bd2aa2bb447f95fcabbaa14c7b0
MD5 56c04cf37d6a7220822c20c05cee3247
BLAKE2b-256 c3bfb6255d8eab04d59a3d90f9f22e1e885a8448ae115182c419753f9e5bbc19

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