proxy-queue
https://github.com/thanhlv-com/proxy-queue
962
A high-performance Go-based middleware proxy server that supports HTTP, HTTPS, and raw socket connections with advanced queue-based request processing, comprehensive monitoring, and flexible configuration options.
# Build for current platform
go build -o proxy-queue main.go
# Or use Makefile
make build
Use the provided Makefile to build for different platforms:
# Build for specific platforms
make build-linux # Linux x86_64
make build-linux-arm64 # Linux ARM64
make build-ubuntu # Ubuntu (same as Linux x86_64)
make build-macos # macOS x86_64
make build-macos-arm64 # macOS ARM64 (Apple Silicon)
make build-windows # Windows x86_64
make build-windows-arm64 # Windows ARM64
# Build for all platforms
make build-all
# Create distribution packages
make dist
make help # Show all available targets
make clean # Clean build artifacts
make deps # Install dependencies
make test # Run tests
make test-race # Run tests with race detection
make lint # Run linter (requires golangci-lint)
make fmt # Format code
make vet # Vet code
make dev # Development build with debug info
make install # Install to GOPATH/bin
make info # Show build information
make check-version # Check Go version compatibility
# Run with default settings (proxy to localhost:443 with HTTPS)
./proxy-queue
# Run with custom target
./proxy-queue -target-host=example.com -target-port=8080
# Run with HTTPS target
./proxy-queue -target-host=api.example.com -target-port=443 -https=true
# Configure delays and queue size
./proxy-queue -delay-min=1000 -delay-max=3000 -queue-size=500
# Configure header-based queues for AWS requests
./proxy-queue -header-queues="X-Amz-Security-Token,Authorization" -target-host=api.example.com
# Full configuration example
./proxy-queue -target-host=thanhlv.com -target-port=443 -port=6789 -delay-min=1 -delay-max=5
# Quick run with Makefile
make run # Build and run with defaults
make run-example # Run with example configuration
The application supports both environment variables (prioritized) and command line flags. Environment variables take precedence when both are provided.
| Environment Variable | Command Line Flag | Default | Description |
|---|---|---|---|
PROXY_LISTEN_PORT | -port | 6789 | Port to listen on |
PROXY_TARGET_HOST | -target-host | localhost | Target host to proxy to |
PROXY_TARGET_PORT | -target-port | 443 | Target port to proxy to |
PROXY_DELAY_MIN | -delay-min | 1000 | Minimum delay between requests (ms) |
PROXY_DELAY_MAX | -delay-max | 5000 | Maximum delay between requests (ms) |
PROXY_USE_HTTPS | -https | true | Use HTTPS for target connections |
PROXY_MAX_QUEUE_SIZE | -queue-size | 1000 | Maximum queue size |
PROXY_METRICS_PORT | -metrics-port | 9090 | Port for Prometheus metrics |
PROXY_HEALTH_PORT | -health-port | 8081 | Port for health checks |
PROXY_LOG_LEVEL | -log-level | info | Log level (debug, info, warn, error) |
PROXY_SHARED_HEALTH_PORT | -shared-health-port | false | Serve health checks on HTTP proxy port 🚩 |
PROXY_SHARED_METRICS_PORT | -shared-metrics-port | false | Serve metrics on HTTP proxy port 📊 |
PROXY_HEADER_QUEUES | -header-queues | X-Amz-Security-Token | Comma-separated headers for dedicated queues |
The proxy supports routing requests to dedicated queues based on specific HTTP headers. This is particularly useful for managing different authentication systems or API providers that require rate limiting.
| System | Headers | Description |
|---|---|---|
| AWS Services, claude cli 🔑 | X-Amz-Security-Token | Routes AWS API requests with temporary credentials or IAM authentication |
| ChatGPT/OpenAI 🤖 | Authorization, OpenAI-Organization | Handles OpenAI API requests with API keys and organization routing |
| Claude/Anthropic 🌐 | Authorization, anthropic-version | Manages Anthropic API requests with proper versioning |
| Google Cloud ☁️ | Authorization, X-Goog-User-Project | Routes Google Cloud API requests with project-specific billing |
| Azure 🟦 | Authorization, Ocp-Apim-Subscription-Key | Handles Azure API Management requests |
| Custom APIs 🔧 | X-API-Key, Authorization, X-Custom-Token | Generic headers for custom authentication systems |
# AWS-specific configuration
./proxy-queue -header-queues="X-Amz-Security-Token,Authorization" -target-host=xxx
# Environment variable approach
export PROXY_HEADER_QUEUES="Authorization,X-Amz-Security-Token,OpenAI-Organization"
./proxy-queue
# Copy and modify the example environment file
cp .env.example .env
# Or set environment variables directly
export PROXY_TARGET_HOST=api.example.com
export PROXY_TARGET_PORT=443
export PROXY_LOG_LEVEL=debug
export PROXY_HEADER_QUEUES=X-Amz-Security-Token
./proxy-queue
-shared-health-port=true
🚩-shared-metrics-port=true 📊# Start proxy
./proxy-queue -target-host=httpbin.org -target-port=80 -https=false
# Test with curl
curl http://localhost:6789/get
# Start HTTPS proxy
./proxy-queue -target-host=httpbin.org -target-port=443 -https=true
# Test with curl
curl http://localhost:6789/get
# Start proxy
./proxy-queue -target-host=example.com -target-port=22
# Test with telnet or netcat (socket proxy runs on port + 10)
telnet localhost 6799
# Check application health
curl http://localhost:8081/health
# Check readiness
curl http://localhost:8081/ready
# View Prometheus metrics
curl http://localhost:9090/metrics
# Start proxy with shared ports
./proxy-queue -shared-health-port=true -shared-metrics-port=true
# Check application health (now on proxy port)
curl http://localhost:6789/health
# Check readiness (now on proxy port)
curl http://localhost:6789/ready
# View Prometheus metrics (now on proxy port)
curl http://localhost:6789/metrics
The application exposes the following Prometheus metrics:
proxy_requests_total (Counter): Total number of proxy requests processedproxy_request_duration_seconds (Histogram): Duration of proxy requests in secondsproxy_queue_size (Gauge): Current number of requests in queueproxy_errors_total (Counter): Total number of proxy errorsproxy_concurrent_requests (Gauge): Number of concurrent requests being processedThese metrics can be scraped by Prometheus and visualized using Grafana dashboards.
The application uses structured JSON logging with the following features:
debug, info, warn, errorExample debug log output:
{
"level": "debug",
"msg": "📥 HTTP Request Details",
"request_id": "http-1672531200123456789",
"method": "GET",
"url": "/api/data",
"headers": { "User-Agent": "curl/7.81.0" },
"remote_ip": "192.168.1.100",
"timestamp": "2023-01-01T12:00:00.123456789Z"
}
# Only health checks on shared port, metrics separate
./proxy-queue -shared-health-port=true
# Health checks on proxy port: http://localhost:6789/health
# Metrics still on separate port: http://localhost:9090/metrics
# Development build with debug info
make dev
# Run tests
make test
# Run tests with race detection
make test-race
# Format and lint code
make fmt
make lint
make vet
X-Amz-Security-Token, Authorization)
can be routed to dedicated queues for isolated processing# Build and run with default configuration
docker-compose up --build
# Run in background
docker-compose up -d
# View logs
docker-compose logs -f proxy-queue
# Stop services
docker-compose down
# Run with local development profile
docker-compose --profile local up
# Run with monitoring stack (Prometheus + Grafana)
docker-compose --profile monitoring up
# Run all services
docker-compose --profile local --profile monitoring up
# Build custom image
docker build -t my-proxy-queue .
# Run with custom environment variables
docker run -d \
-p 6789:6789 \
-p 6799:6799 \
-e PROXY_TARGET_HOST=api.example.com \
-e PROXY_TARGET_PORT=443 \
-e PROXY_LOG_LEVEL=debug \
my-proxy-queue
# Copy and modify environment file
cp .env.example .env
# Use with docker-compose
docker-compose --env-file .env up
Access the monitoring stack:
QueueManager: Main orchestrator managing multiple queues (main + header-based queues)ProxyQueue: Individual queue manager with thread-safe operations, metrics collection, and request
processingConfig: Configuration structure supporting both environment variables and command-line flagsHTTPRequestData/SocketRequestData: Type-safe request data structures with client informationProxyRequest/ProxyResponse: Queue communication protocol with response channelsMetrics: Prometheus metrics collection with counters, histograms, and gauges (shared across queues)The project uses minimal external dependencies to ensure reliability and security:
All dependencies are automatically managed through Go modules (go.mod/go.sum).
InsecureSkipVerify optionX-Forwarded-For and X-Real-IP headersPROXY_MAX_QUEUE_SIZE based on expected load and available memoryPROXY_DELAY_MIN/PROXY_DELAY_MAX according to target server capacitydebug level cautiously in production as it logs full request/response bodies/health and /ready endpointsContent type
Image
Digest
sha256:6098173f4…
Size
10.8 MB
Last updated
3 months ago
docker pull thanhlvcom/proxy-queue