-
Notifications
You must be signed in to change notification settings - Fork 0
Add environment variable support for PORT, TO_CLT_BUFSIZE, TO_TG_BUFSIZE and optional configs #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -41,12 +41,18 @@ services: | |||||
| restart: unless-stopped | ||||||
| network_mode: "host" | ||||||
| environment: | ||||||
| # Replace these values with your own configuration | ||||||
| # Core configuration - replace these values with your own | ||||||
| - PORT=443 | ||||||
| - TG_KEY=00000000000000000000000000000001 | ||||||
| - SECURE_ONLY=true | ||||||
| - TLS_ONLY=true | ||||||
| - TLS_DOMAIN=www.drive.google.com | ||||||
| - AD_TAG=3c09c680b76ee91a4c25ad51f742267d | ||||||
| # Optional: Performance tuning | ||||||
| # - TO_CLT_BUFSIZE=65536 | ||||||
| # - TO_TG_BUFSIZE=65536 | ||||||
| # Optional: Metrics | ||||||
| # - METRICS_PORT=9090 | ||||||
| volumes: | ||||||
| - ./config.py:/home/tgproxy/config.py | ||||||
| ``` | ||||||
|
|
@@ -57,6 +63,80 @@ Then run: `docker-compose up -d` | |||||
|
|
||||||
| To advertise a channel get a tag from **@MTProxybot** and put it to *config.py*. | ||||||
|
|
||||||
| ## Environment Variables ## | ||||||
|
|
||||||
| All configuration options can be set via environment variables. This is particularly useful when running in Docker. | ||||||
|
|
||||||
| ### Required/Core Configuration ### | ||||||
|
|
||||||
| | Variable | Default | Description | | ||||||
| |----------|---------|-------------| | ||||||
| | `PORT` | `443` | Listening port for the proxy | | ||||||
| | `TG_KEY` | `00000000000000000000000000000001` | User secret (32 hex characters) | | ||||||
| | `AD_TAG` | `3c09c680b76ee91a4c25ad51f742267d` | Tag for advertising, obtainable from @MTProxybot | | ||||||
|
|
||||||
| ### Security Settings ### | ||||||
|
|
||||||
| | Variable | Default | Description | | ||||||
| |----------|---------|-------------| | ||||||
| | `SECURE_ONLY` | `true` | Makes the proxy harder to detect (incompatible with very old clients) | | ||||||
| | `TLS_ONLY` | `true` | Makes the proxy even harder to detect (compatible only with recent clients) | | ||||||
| | `TLS_DOMAIN` | `www.google.com` | Domain for TLS, bad clients are proxied there | | ||||||
|
|
||||||
| ### SOCKS5 Proxy Settings (Optional) ### | ||||||
|
|
||||||
| | Variable | Default | Description | | ||||||
| |----------|---------|-------------| | ||||||
| | `SOCKS5_HOST` | `None` | SOCKS5 proxy hostname or IP address | | ||||||
| | `SOCKS5_PORT` | `None` | SOCKS5 proxy port | | ||||||
| | `SOCKS5_USER` | `None` | SOCKS5 username (optional) | | ||||||
| | `SOCKS5_PASS` | `None` | SOCKS5 password (optional) | | ||||||
|
|
||||||
| **Note:** When SOCKS5 is enabled, middle proxy advertising is automatically disabled. | ||||||
|
|
||||||
| ### Performance Tuning (Optional) ### | ||||||
|
|
||||||
| | Variable | Default | Description | | ||||||
| |----------|---------|-------------| | ||||||
| | `TO_CLT_BUFSIZE` | `16384,100,131072` | Buffer size to client. Single integer or comma-separated tuple (low,users_margin,high) for adaptive sizing | | ||||||
| | `TO_TG_BUFSIZE` | `65536` | Buffer size to Telegram servers. Single integer or comma-separated tuple for adaptive sizing | | ||||||
| | `STATS_PRINT_PERIOD` | `600` | Statistics print period in seconds | | ||||||
| | `CLIENT_KEEPALIVE` | `600` | Client keepalive period in seconds (10 minutes) | | ||||||
| | `TG_CONNECT_TIMEOUT` | `10` | Telegram server connect timeout in seconds | | ||||||
| | `FAST_MODE` | `true` | Enable fast mode (disables some checks for better performance) | | ||||||
|
|
||||||
| ### Network Settings (Optional) ### | ||||||
|
|
||||||
| | Variable | Default | Description | | ||||||
| |----------|---------|-------------| | ||||||
| | `LISTEN_ADDR_IPV4` | `0.0.0.0` | IPv4 listen address | | ||||||
| | `LISTEN_ADDR_IPV6` | `::` | IPv6 listen address | | ||||||
| | `PREFER_IPV6` | Auto-detected | Prefer IPv6 for outgoing connections | | ||||||
|
||||||
| | `PREFER_IPV6` | Auto-detected | Prefer IPv6 for outgoing connections | | |
| | `PREFER_IPV6` | `true` if IPv6 available (auto-detected via socket.has_ipv6) | Prefer IPv6 for outgoing connections | |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -10,7 +10,8 @@ def str_to_bool(value): | |||||||||||||||||||||||||||||||||||||||||||
| return bool(value) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| PORT = 443 | ||||||||||||||||||||||||||||||||||||||||||||
| # Listening port for the proxy | ||||||||||||||||||||||||||||||||||||||||||||
| PORT = int(os.environ.get("PORT", 443)) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| # name -> secret (32 hex chars) | ||||||||||||||||||||||||||||||||||||||||||||
| USERS = { | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -39,3 +40,69 @@ def str_to_bool(value): | |||||||||||||||||||||||||||||||||||||||||||
| SOCKS5_PORT = int(os.environ.get("SOCKS5_PORT", 0)) if os.environ.get("SOCKS5_PORT", "").isdigit() else None | ||||||||||||||||||||||||||||||||||||||||||||
| SOCKS5_USER = os.environ.get("SOCKS5_USER", None) | ||||||||||||||||||||||||||||||||||||||||||||
| SOCKS5_PASS = os.environ.get("SOCKS5_PASS", None) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| # Buffer sizes (optional performance tuning) | ||||||||||||||||||||||||||||||||||||||||||||
| # max socket buffer size to the client direction, the more the faster, but more RAM hungry | ||||||||||||||||||||||||||||||||||||||||||||
| # Can be a single integer or a string like "16384,100,131072" for adaptive sizing (low,users_margin,high) | ||||||||||||||||||||||||||||||||||||||||||||
| _to_clt_bufsize_env = os.environ.get("TO_CLT_BUFSIZE", None) | ||||||||||||||||||||||||||||||||||||||||||||
| if _to_clt_bufsize_env: | ||||||||||||||||||||||||||||||||||||||||||||
| if "," in _to_clt_bufsize_env: | ||||||||||||||||||||||||||||||||||||||||||||
| TO_CLT_BUFSIZE = tuple(int(x.strip()) for x in _to_clt_bufsize_env.split(",")) | ||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||
| TO_CLT_BUFSIZE = int(_to_clt_bufsize_env) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+50
to
+53
|
||||||||||||||||||||||||||||||||||||||||||||
| TO_CLT_BUFSIZE = tuple(int(x.strip()) for x in _to_clt_bufsize_env.split(",")) | |
| else: | |
| TO_CLT_BUFSIZE = int(_to_clt_bufsize_env) | |
| parts = [int(x.strip()) for x in _to_clt_bufsize_env.split(",")] | |
| if len(parts) != 3: | |
| print(f"Warning: TO_CLT_BUFSIZE tuple must have exactly 3 values (low,users_margin,high), got {len(parts)}") | |
| else: | |
| TO_CLT_BUFSIZE = tuple(parts) | |
| else: | |
| TO_CLT_BUFSIZE = int(_to_clt_bufsize_env) |
Copilot
AI
Dec 9, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing error handling for invalid integer values. If TO_CLT_BUFSIZE is set to a non-numeric string (e.g., "invalid"), this will raise a ValueError. Consider wrapping the conversion in a try-except block or validating the input before conversion, similar to the existing pattern for SOCKS5_PORT at line 40 which uses .isdigit() check.
Example:
if _to_clt_bufsize_env:
try:
if "," in _to_clt_bufsize_env:
TO_CLT_BUFSIZE = tuple(int(x.strip()) for x in _to_clt_bufsize_env.split(","))
else:
TO_CLT_BUFSIZE = int(_to_clt_bufsize_env)
except ValueError:
print(f"Warning: Invalid TO_CLT_BUFSIZE value: {_to_clt_bufsize_env}, using default")| if "," in _to_clt_bufsize_env: | |
| TO_CLT_BUFSIZE = tuple(int(x.strip()) for x in _to_clt_bufsize_env.split(",")) | |
| else: | |
| TO_CLT_BUFSIZE = int(_to_clt_bufsize_env) | |
| try: | |
| if "," in _to_clt_bufsize_env: | |
| TO_CLT_BUFSIZE = tuple(int(x.strip()) for x in _to_clt_bufsize_env.split(",")) | |
| else: | |
| TO_CLT_BUFSIZE = int(_to_clt_bufsize_env) | |
| except ValueError: | |
| print(f"Warning: Invalid TO_CLT_BUFSIZE value: {_to_clt_bufsize_env}, using default") |
Copilot
AI
Dec 9, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing error handling for invalid integer values. If TO_TG_BUFSIZE is set to a non-numeric string, this will raise a ValueError. Consider adding the same error handling as suggested for TO_CLT_BUFSIZE.
Example:
if _to_tg_bufsize_env:
try:
if "," in _to_tg_bufsize_env:
TO_TG_BUFSIZE = tuple(int(x.strip()) for x in _to_tg_bufsize_env.split(","))
else:
TO_TG_BUFSIZE = int(_to_tg_bufsize_env)
except ValueError:
print(f"Warning: Invalid TO_TG_BUFSIZE value: {_to_tg_bufsize_env}, using default")| if "," in _to_tg_bufsize_env: | |
| TO_TG_BUFSIZE = tuple(int(x.strip()) for x in _to_tg_bufsize_env.split(",")) | |
| else: | |
| TO_TG_BUFSIZE = int(_to_tg_bufsize_env) | |
| try: | |
| if "," in _to_tg_bufsize_env: | |
| TO_TG_BUFSIZE = tuple(int(x.strip()) for x in _to_tg_bufsize_env.split(",")) | |
| else: | |
| TO_TG_BUFSIZE = int(_to_tg_bufsize_env) | |
| except ValueError: | |
| print(f"Warning: Invalid TO_TG_BUFSIZE value: {_to_tg_bufsize_env}, using default") |
Copilot
AI
Dec 9, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing validation for tuple format. According to the code in mtprotoproxy.py:479, the tuple format requires exactly 3 elements (low, margin, high). If a user provides a string with wrong number of elements, this will either fail unpacking later or use incorrect values.
Consider adding the same validation as suggested for TO_CLT_BUFSIZE.
| TO_TG_BUFSIZE = tuple(int(x.strip()) for x in _to_tg_bufsize_env.split(",")) | |
| else: | |
| TO_TG_BUFSIZE = int(_to_tg_bufsize_env) | |
| tg_bufsize_tuple = tuple(int(x.strip()) for x in _to_tg_bufsize_env.split(",")) | |
| if len(tg_bufsize_tuple) != 3: | |
| raise ValueError("TO_TG_BUFSIZE must have exactly 3 comma-separated values (low, margin, high)") | |
| TO_TG_BUFSIZE = tg_bufsize_tuple | |
| else: | |
| TO_TG_BUFSIZE = int(_to_tg_bufsize_env) |
Copilot
AI
Dec 9, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing error handling for invalid integer conversion. If STATS_PRINT_PERIOD is set to a non-numeric string, this will raise a ValueError. The same issue exists for CLIENT_KEEPALIVE (line 71), TG_CONNECT_TIMEOUT (line 76), and METRICS_PORT (line 103).
Consider adding try-except blocks or validation for all these integer conversions to prevent crashes on invalid input.
Copilot
AI
Dec 9, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing error handling for invalid integer conversion. If CLIENT_KEEPALIVE is set to a non-numeric string, this will raise a ValueError. Consider adding error handling as mentioned in the comment for STATS_PRINT_PERIOD.
| CLIENT_KEEPALIVE = int(_client_keepalive) | |
| try: | |
| CLIENT_KEEPALIVE = int(_client_keepalive) | |
| except (ValueError, TypeError): | |
| # Fallback to a default value, e.g., 60 seconds, and optionally log a warning | |
| CLIENT_KEEPALIVE = 60 | |
| # print(f"Warning: Invalid CLIENT_KEEPALIVE value '{_client_keepalive}', using default 60") |
Copilot
AI
Dec 9, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing error handling for invalid integer conversion. If TG_CONNECT_TIMEOUT is set to a non-numeric string, this will raise a ValueError. Consider adding error handling as mentioned in the comment for STATS_PRINT_PERIOD.
| TG_CONNECT_TIMEOUT = int(_tg_connect_timeout) | |
| try: | |
| TG_CONNECT_TIMEOUT = int(_tg_connect_timeout) | |
| except (ValueError, TypeError): | |
| print(f"Warning: Invalid TG_CONNECT_TIMEOUT value '{_tg_connect_timeout}', using default (None).") | |
| TG_CONNECT_TIMEOUT = None |
Copilot
AI
Dec 9, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing error handling for invalid integer conversion. If METRICS_PORT is set to a non-numeric string, this will raise a ValueError. Consider adding error handling as mentioned in the comment for STATS_PRINT_PERIOD.
| METRICS_PORT = int(_metrics_port) | |
| try: | |
| METRICS_PORT = int(_metrics_port) | |
| except (ValueError, TypeError): | |
| METRICS_PORT = None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default value documented here should be
(16384, 100, 131072)(a tuple) not16384,100,131072(a string). According tomtprotoproxy.py:275, the default is a tuple:conf_dict.setdefault("TO_CLT_BUFSIZE", (16384, 100, 131072)).The current documentation could be misleading as it suggests the default is a string format, when in fact the code defaults to a tuple when the variable is not set.