Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

29 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

VPN Service with VLESS Protocol

A complete VPN service infrastructure with embedded Xray-core, user management, traffic monitoring, and metrics collection.

πŸ‡·πŸ‡Ί Русская вСрсия: README.ru.md

πŸ—οΈ Architecture

  • 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

✨ Features

  • βœ… 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

πŸ“ Project Structure

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

πŸš€ Quick Start

Prerequisites

  • Docker & Docker Compose
  • make (optional, for convenience)

1. Setup

# Clone the repository
git clone <repo-url>
cd vpn-service

# Generate keys and create .env
make setup

# Edit .env file
nano .env

Set required variables:

  • XRAY_PRIVATE_KEY - Private key from make generate-keys
  • SERVER_IP - Your server IP address

2. Start Services

# Build and start
make build
make up

# Check status
make status

# View logs
make logs

3. Create First User

# 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"
  }'

4. Access Services

πŸ“Š Monitoring

Grafana Dashboard

  1. Open Grafana at http://localhost:3000
  2. Login with admin/admin
  3. Navigate to Dashboards β†’ VPN Service Metrics
  4. View real-time VPN metrics:
    • Active users
    • Connection rates
    • Bandwidth usage
    • Total connections

Prometheus Metrics

View raw metrics at: http://localhost:8080/metrics

Available metrics:

  • vpn_active_users - Number of active users
  • vpn_total_connections - Total connections count
  • vpn_bandwidth_bytes{user_id, direction} - Bandwidth per user
  • vpn_connection_duration_seconds - Connection duration histogram

πŸ”§ API Endpoints

Users

Create User

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
}

List Users

GET /api/users
GET /api/users?active=true  // only active users

Get User

GET /api/users/{id}

Update User

PATCH /api/users/{id}
Content-Type: application/json

{
  "traffic_limit": 21474836480,  // 20GB
  "is_active": true
}

Delete User

DELETE /api/users/{id}

Get User Config

GET /api/users/{id}/config

Returns:

  • JSON config for v2rayN, Nekoray
  • VLESS URI
  • QR code (base64)
  • Traffic statistics

Reset Traffic

POST /api/users/{id}/reset-traffic

System

Health Check

GET /health

Statistics

GET /stats

Prometheus Metrics

GET /metrics

πŸ” Protocol Configuration

The service uses VLESS + Reality + xHTTP protocol for maximum security and censorship resistance.

Client configuration is generated automatically via API:

GET /api/users/{id}/config

This returns JSON config, VLESS URI, and QR code ready for import into VPN clients.

πŸ› οΈ Makefile Commands

Main Commands

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 data

User Management

make 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 user

Testing

make test-health   # Check health
make test-stats    # Check statistics
make metrics       # Show metrics

Backup

make db-backup     # Backup database
make db-restore    # Restore database

πŸ“± Client Applications

Android

  • v2rayNG
  • Nekoray

iOS

  • Shadowrocket
  • V2Box

Windows/macOS/Linux

  • v2rayN / v2rayNG
  • Nekoray
  • Qv2ray

Connection

  1. Get config: GET /api/users/{id}/config
  2. Use QR code or VLESS URI
  3. Import into client application

πŸ› οΈ Development

Rebuild Go Service

docker-compose build app
docker-compose up -d app

View Logs

# All services
docker-compose logs -f

# Specific service
docker-compose logs -f app
docker-compose logs -f xray

Stop Services

docker-compose down

Clean All Data

docker-compose down -v

πŸ” Troubleshooting

Xray Not Starting

  1. Check if private key is set in xray/config.json
  2. Verify port 443 is not in use: lsof -i :443
  3. Check logs: docker-compose logs xray

Go Service Can't Connect to Prometheus

  1. Ensure all services are in the same Docker network
  2. Check docker-compose ps - all services should be "Up"
  3. Restart: docker-compose restart

Grafana Shows No Data

  1. Check Prometheus is collecting metrics: http://localhost:9090/targets
  2. Verify Go app metrics endpoint: http://localhost:8080/metrics
  3. Check Grafana datasource connection: Configuration β†’ Data Sources

πŸ“ˆ Scaling

To scale the Go service:

docker-compose up -d --scale app=3

Add a load balancer (nginx/traefik) in front of multiple instances.

πŸ”’ Security Notes

  1. Change default passwords in docker-compose.yml (Grafana)
  2. Generate unique keys for Xray Reality
  3. Use HTTPS for Go API in production (add reverse proxy)
  4. Implement authentication for API endpoints
  5. Restrict ports using firewall rules
  6. Regular updates: docker-compose pull && docker-compose up -d

πŸ“š Technologies

  • 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

πŸ“„ License

MIT License - Use freely for personal and commercial projects.

🀝 Contributing

Contributions welcome! Please submit pull requests or open issues.


Happy VPN-ing! πŸš€

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages