A complete VPN service infrastructure with embedded Xray-core, user management, traffic monitoring, and metrics collection.
π·πΊ Π ΡΡΡΠΊΠ°Ρ Π²Π΅ΡΡΠΈΡ: README.ru.md
- Go Application - Embedded Xray-core, REST API, User Management
- SQLite - User database
- Prometheus - Metrics collection
- Grafana - Metrics visualization
- VLESS + Reality + xHTTP - Modern secure VPN protocol
- β REST API for user management
- β Traffic limits and subscription expiry
- β Automatic traffic tracking
- β Client config generation (JSON, URI, QR code)
- β Real-time monitoring (Prometheus + Grafana)
- β VLESS protocol with Reality and xHTTP
- β SQLite database for user storage
vpn-service/
βββ app/
β βββ main.go # Entry point
β βββ database/ # Database layer
β β βββ models.go # User model
β β βββ database.go # DB connection
β β βββ repository.go # CRUD operations
β βββ xray/ # Xray integration
β β βββ manager.go # Xray instance management
β β βββ config.go # Config generation
β β βββ client_config.go # Client configs
β βββ api/ # REST API
β β βββ handlers.go # HTTP handlers
β β βββ middleware.go # Middleware
β β βββ responses.go # JSON responses
β βββ monitoring/ # Monitoring
β β βββ log_parser.go # Log parser
β β βββ metrics.go # Prometheus metrics
β βββ utils/ # Utilities
β βββ crypto.go # Password hashing
β βββ qrcode.go # QR code generation
βββ prometheus/
β βββ prometheus.yml # Prometheus config
βββ grafana/
β βββ datasources/ # Data sources
β βββ dashboards/ # Dashboards
βββ examples/ # Usage examples
β βββ api_examples.sh # API examples
β βββ create_test_users.sh # Test users
β βββ python_client.py # Python client
βββ docker-compose.yml # Docker Compose
βββ Makefile # Automation
βββ README.md # Documentation
- Docker & Docker Compose
- make (optional, for convenience)
# Clone the repository
git clone <repo-url>
cd vpn-service
# Generate keys and create .env
make setup
# Edit .env file
nano .envSet required variables:
XRAY_PRIVATE_KEY- Private key frommake generate-keysSERVER_IP- Your server IP address
# Build and start
make build
make up
# Check status
make status
# View logs
make logs# Using Makefile
make create-user
# Or directly with curl
curl -X POST http://localhost:8080/api/users \
-H "Content-Type: application/json" \
-d '{
"username": "john",
"password": "secret123",
"traffic_limit": 10737418240,
"expires_at": "2025-12-31T23:59:59Z"
}'- API: http://localhost:8080
- Prometheus: http://localhost:9090
- Grafana: http://localhost:3000 (admin/admin)
- VLESS VPN: Port 443
- Open Grafana at http://localhost:3000
- Login with
admin/admin - Navigate to Dashboards β VPN Service Metrics
- View real-time VPN metrics:
- Active users
- Connection rates
- Bandwidth usage
- Total connections
View raw metrics at: http://localhost:8080/metrics
Available metrics:
vpn_active_users- Number of active usersvpn_total_connections- Total connections countvpn_bandwidth_bytes{user_id, direction}- Bandwidth per uservpn_connection_duration_seconds- Connection duration histogram
POST /api/users
Content-Type: application/json
{
"username": "john_doe",
"password": "securepass123",
"traffic_limit": 10737418240, // 10GB in bytes, 0 = unlimited
"expires_at": "2025-12-31T23:59:59Z" // optional
}GET /api/users
GET /api/users?active=true // only active usersGET /api/users/{id}PATCH /api/users/{id}
Content-Type: application/json
{
"traffic_limit": 21474836480, // 20GB
"is_active": true
}DELETE /api/users/{id}GET /api/users/{id}/configReturns:
- JSON config for v2rayN, Nekoray
- VLESS URI
- QR code (base64)
- Traffic statistics
POST /api/users/{id}/reset-trafficGET /healthGET /statsGET /metricsThe service uses VLESS + Reality + xHTTP protocol for maximum security and censorship resistance.
Client configuration is generated automatically via API:
GET /api/users/{id}/configThis returns JSON config, VLESS URI, and QR code ready for import into VPN clients.
make help # Show help
make setup # Initial setup
make up # Start services
make down # Stop services
make restart # Restart services
make logs # View logs
make status # Service status
make clean # Remove all datamake create-user # Create test user
make list-users # List users
make get-user-config USER_ID=1 # Get config
make delete-user USER_ID=1 # Delete usermake test-health # Check health
make test-stats # Check statistics
make metrics # Show metricsmake db-backup # Backup database
make db-restore # Restore database- v2rayNG
- Nekoray
- Shadowrocket
- V2Box
- v2rayN / v2rayNG
- Nekoray
- Qv2ray
- Get config:
GET /api/users/{id}/config - Use QR code or VLESS URI
- Import into client application
docker-compose build app
docker-compose up -d app# All services
docker-compose logs -f
# Specific service
docker-compose logs -f app
docker-compose logs -f xraydocker-compose downdocker-compose down -v- Check if private key is set in
xray/config.json - Verify port 443 is not in use:
lsof -i :443 - Check logs:
docker-compose logs xray
- Ensure all services are in the same Docker network
- Check
docker-compose ps- all services should be "Up" - Restart:
docker-compose restart
- Check Prometheus is collecting metrics: http://localhost:9090/targets
- Verify Go app metrics endpoint: http://localhost:8080/metrics
- Check Grafana datasource connection: Configuration β Data Sources
To scale the Go service:
docker-compose up -d --scale app=3Add a load balancer (nginx/traefik) in front of multiple instances.
- Change default passwords in
docker-compose.yml(Grafana) - Generate unique keys for Xray Reality
- Use HTTPS for Go API in production (add reverse proxy)
- Implement authentication for API endpoints
- Restrict ports using firewall rules
- Regular updates:
docker-compose pull && docker-compose up -d
- Xray-core: Modern VPN proxy with Reality protocol
- Go: High-performance user management service
- Prometheus: Metrics collection and storage
- Grafana: Beautiful metrics visualization
- Docker: Containerized deployment
MIT License - Use freely for personal and commercial projects.
Contributions welcome! Please submit pull requests or open issues.
Happy VPN-ing! π