|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +go-sql-proxy is a MySQL proxy server that acts as an intermediary between MySQL clients and servers. It provides transparent traffic forwarding with protocol decoding capabilities, metrics collection, health checks, and connection management. |
| 8 | + |
| 9 | +## Development Commands |
| 10 | + |
| 11 | +### Build and Run |
| 12 | +```bash |
| 13 | +# Run locally |
| 14 | +go run main.go |
| 15 | + |
| 16 | +# Build binary |
| 17 | +go build -o go-sql-proxy |
| 18 | + |
| 19 | +# Build Docker image |
| 20 | +make build TAG=1.0.0 |
| 21 | + |
| 22 | +# Push to registry |
| 23 | +make push TAG=1.0.0 |
| 24 | + |
| 25 | +# Build and push |
| 26 | +make all TAG=1.0.0 |
| 27 | +``` |
| 28 | + |
| 29 | +### Testing |
| 30 | +Currently, there are no unit tests in the codebase. When adding new functionality, consider creating appropriate test files. |
| 31 | + |
| 32 | +## Architecture |
| 33 | + |
| 34 | +### Core Components |
| 35 | + |
| 36 | +1. **main.go**: Entry point that initializes configuration, starts metrics server, creates proxy instance, and handles graceful shutdown |
| 37 | +2. **pkg/proxy**: Core proxy logic - accepts client connections, establishes upstream connections, and manages bidirectional data transfer |
| 38 | +3. **pkg/protocol**: MySQL protocol decoding/encoding for packet inspection |
| 39 | +4. **pkg/metrics**: Prometheus metrics collection and HTTP endpoints |
| 40 | +5. **pkg/health**: Health check endpoints that verify proxy and upstream connectivity |
| 41 | +6. **pkg/config**: Environment-based configuration management |
| 42 | + |
| 43 | +### Connection Flow |
| 44 | + |
| 45 | +1. Client connects to proxy on `BIND_PORT` (default: 3306) |
| 46 | +2. Proxy establishes connection to `SOURCE_DATABASE_SERVER:SOURCE_DATABASE_PORT` |
| 47 | +3. Data is transferred bidirectionally using `io.Copy` with optional protocol decoding |
| 48 | +4. Metrics are collected for bytes transferred and connection counts |
| 49 | + |
| 50 | +### Key Design Patterns |
| 51 | + |
| 52 | +- **Context-based lifecycle management**: Uses Go contexts for graceful shutdown |
| 53 | +- **Concurrent connection handling**: Each client connection runs in its own goroutine |
| 54 | +- **Centralized logging**: All packages use the shared Logrus logger from pkg/logging |
| 55 | +- **Environment configuration**: All settings come from environment variables |
| 56 | + |
| 57 | +### Environment Variables |
| 58 | + |
| 59 | +- `DEBUG`: Enable debug logging |
| 60 | +- `METRICS_PORT`: Port for metrics/health endpoints (default: 9090) |
| 61 | +- `SOURCE_DATABASE_SERVER`: Target MySQL server hostname |
| 62 | +- `SOURCE_DATABASE_PORT`: Target MySQL server port (default: 25060) |
| 63 | +- `SOURCE_DATABASE_USER`: MySQL username |
| 64 | +- `SOURCE_DATABASE_PASSWORD`: MySQL password |
| 65 | +- `SOURCE_DATABASE_NAME`: Default database name |
| 66 | +- `BIND_ADDRESS`: Proxy bind address (default: 0.0.0.0) |
| 67 | +- `BIND_PORT`: Proxy listening port (default: 3306) |
| 68 | +- `USE_SSL`: Enable SSL/TLS connection to upstream MySQL (default: false) |
| 69 | +- `SSL_SKIP_VERIFY`: Skip SSL certificate verification (default: false) |
| 70 | +- `SSL_CA_FILE`: Path to CA certificate file for SSL verification |
| 71 | +- `SSL_CERT_FILE`: Path to client certificate file for mutual TLS |
| 72 | +- `SSL_KEY_FILE`: Path to client key file for mutual TLS |
| 73 | + |
| 74 | +### Metrics and Health Endpoints |
| 75 | + |
| 76 | +Available on `METRICS_PORT`: |
| 77 | +- `/metrics`: Prometheus metrics |
| 78 | +- `/healthz`: Liveness check (tests proxy connectivity) |
| 79 | +- `/readyz`: Readiness check (tests upstream MySQL connectivity) |
| 80 | +- `/version`: Version information |
| 81 | + |
| 82 | +### Important Implementation Details |
| 83 | + |
| 84 | +- The proxy uses `io.Copy` for efficient data transfer between connections |
| 85 | +- Protocol decoding is optional and controlled by configuration |
| 86 | +- Connection errors are logged but don't crash the proxy |
| 87 | +- Each connection tracks bytes transferred in both directions |
| 88 | +- Version information is injected at build time using LDFLAGS |
| 89 | +- SSL/TLS support is controlled by the `USE_SSL` flag instead of port-based logic |
| 90 | +- Health checks also respect SSL settings when connecting to the database |
0 commit comments