Repository:
Max97k/GCP-Telegram-VPN-Bot
Default Branch:main
Primary Technology Stack: Python 3.11, Google Cloud Functions Gen 2, Google Cloud Compute Engine API, Google Secret Manager
Visibility: Public
GCP-Telegram-VPN-Bot is a serverless, zero-idle-cost Telegram Bot running on Google Cloud Functions 2nd Gen. It enables authorized users to provision, monitor, and destroy on-demand disposable WireGuard VPN servers deployed on Google Cloud Platform (GCP) Compute Engine Spot f1-micro instances across 9 global regions. The bot automatically provisions the VPN, compiles client profiles, generates QR codes, and delivers them directly into the Telegram chat interface.
+-----------------------------------------------------------------------------+
| GCP-Telegram-VPN-Bot Flow |
+-----------------------------------------------------------------------------+
|
Telegram User Chat (/new, /status, /del)
|
v
+-----------------------------------+
| Cloud Functions (Gen 2) |
| Entrypoint: deploy_vpn() |
| - Webhook Router & Auth |
| - Secret Manager Integration |
+-----------------------------------+
|
+-------------------+-------------------+
| |
v v
+------------------------------+ +------------------------------+
| Google Secret Manager | | GCP Compute Engine API |
| - TELEGRAM_BOT_TOKEN | | - Provision Spot f1-micro |
| - AUTHORIZED_USER_ID | | - Inject startup script |
+------------------------------+ | - Query / Delete instances |
+------------------------------+
|
v
+------------------------------+
| Spot VM Instance |
| - Install WireGuard & qrencode|
| - Generate Keys & Config |
| - Post QR Code PNG to Chat |
+------------------------------+
| Path | Purpose / Description |
|---|---|
main.py |
Core serverless function. Implements deploy_vpn(request) HTTP webhook handler, command routing, inline keyboard generation, Secret Manager access, and Compute Engine VM lifecycle operations. |
requirements.txt |
Python dependencies: google-cloud-compute, google-cloud-secret-manager, requests, functions-framework. |
README.md / README_zh-TW.md |
Comprehensive user guide, architectural breakdown, and deployment runbook in English and Traditional Chinese. |
assets/ |
Documentation screenshots and interaction workflow diagrams. |
- Python: Python 3.11.
- Google Cloud SDK:
gcloudCLI installed and authenticated with active GCP project. - Telegram Bot: A bot created via
@BotFatherwith HTTP API token. - GCP Secret Manager: Secrets created for
TELEGRAM_BOT_TOKENandAUTHORIZED_USER_ID.
# Install dependencies
pip install -r requirements.txt
# Run locally using Functions Framework
export GCP_PROJECT_ID="your-gcp-project-id"
functions-framework --target=deploy_vpn --port=8080 --debug# Validate Python syntax
python -m py_compile main.py# Deploy to Google Cloud Functions 2nd Gen
gcloud functions deploy vpn-bot \
--gen2 \
--runtime=python311 \
--region=asia-northeast1 \
--source=. \
--entry-point=deploy_vpn \
--trigger-http \
--allow-unauthenticated \
--set-env-vars GCP_PROJECT_ID=$(gcloud config get-value project) \
--memory=512MB \
--timeout=60s
# Register Telegram Webhook
curl "https://api.telegram.org/bot<TELEGRAM_BOT_TOKEN>/setWebhook?url=<CLOUD_FUNCTION_TRIGGER_URL>"- Python Standard: PEP 8 compliance, clean function separation, type hints where applicable.
- Serverless Resilience: Handle timeouts and asynchronous VM initialization gracefully. Cloud Function responses must return HTTP 200 to Telegram quickly to prevent webhook retry storms.
- Use Telegram Inline Keyboards for interactive menu navigation (region selection, confirm deletion).
- Sanitize HTML/Markdown tags in Telegram text replies to prevent message parsing exceptions.
- Use try/except blocks around GCP API calls (
google.cloud.compute_v1.InstancesClient) with descriptive error reporting to Telegram admins. - Enforce strict authentication whitelist check at the entry of
deploy_vpn()before any API call is made.
- Secret Manager Mandatory: NEVER commit or hardcode
TELEGRAM_BOT_TOKEN, user IDs, or private keys. Always read credentials dynamically viagoogle.cloud.secretmanager. - IAM Principle of Least Privilege: Cloud Function service account only requires
roles/secretmanager.secretAccessorandroles/compute.admin.
- Spot Instances Only: Always provision
f1-microSpot/Preemptible instances (~$0.004/hour) to minimize cloud cost. - Quota Enforced: Hard cap of maximum 5 active VMs per user to prevent runaway costs or quota exhaustion.
- Firewall Rules: Ensure VPC firewall rule allows ingress UDP traffic on port
51820for WireGuard.
- Default Branch:
main. - Commit Standards: Use conventional commit messages:
feat:,fix:,docs:,refactor:,test:,chore:.