Skip to content

Add environment variable support for PORT, TO_CLT_BUFSIZE, TO_TG_BUFSIZE and optional configs#10

Merged
xrh0905 merged 2 commits into
edgefrom
copilot/add-env-variable-configs
Dec 9, 2025
Merged

Add environment variable support for PORT, TO_CLT_BUFSIZE, TO_TG_BUFSIZE and optional configs#10
xrh0905 merged 2 commits into
edgefrom
copilot/add-env-variable-configs

Conversation

Copy link
Copy Markdown

Copilot AI commented Dec 9, 2025

Several configuration options were only settable via config.py modifications, making Docker deployments less flexible. This adds environment variable support for the requested variables plus common tuning parameters.

Changes

config.py

  • Added PORT environment variable (default: 443)
  • Added TO_CLT_BUFSIZE and TO_TG_BUFSIZE with support for both integer (65536) and tuple ("16384,100,131072") formats for adaptive sizing
  • Added optional performance tunables: STATS_PRINT_PERIOD, CLIENT_KEEPALIVE, TG_CONNECT_TIMEOUT, FAST_MODE
  • Added network settings: LISTEN_ADDR_IPV4, LISTEN_ADDR_IPV6, PREFER_IPV6
  • Added metrics settings: METRICS_PORT, METRICS_EXPORT_LINKS

README.md

  • Added environment variables reference table organized by category (Core, Security, Performance, Network, Metrics)
  • Included Docker run example with new variables

docker-compose.yml

  • Updated example to include PORT

Usage

docker run -d --name mtprotoproxy \
  --network host \
  -e PORT=8443 \
  -e TO_CLT_BUFSIZE=65536 \
  -e TO_TG_BUFSIZE="16384,100,131072" \
  -e METRICS_PORT=9090 \
  ghcr.io/xrh0905/mtprotoproxy:latest

Variables only defined when environment variables are set; falls back to mtprotoproxy.py defaults via setdefault() otherwise.

Original prompt

This section details on the original issue you should resolve

<issue_title>Missing ENVIROMENT variable for configs</issue_title>
<issue_description>- TO_CLT_BUFSIZE

  • TO_TG_BUFSIZE
  • PORT
    etc.
    Optional config should also be exposed and displayed on README.</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…IZE and other optional configs

Co-authored-by: xrh0905 <29017419+xrh0905@users.noreply.github.com>
Copilot AI changed the title [WIP] Add missing environment variables for configurations Add environment variable support for PORT, TO_CLT_BUFSIZE, TO_TG_BUFSIZE and optional configs Dec 9, 2025
Copilot AI requested a review from xrh0905 December 9, 2025 16:02
@xrh0905 xrh0905 marked this pull request as ready for review December 9, 2025 16:03
Copilot AI review requested due to automatic review settings December 9, 2025 16:03
@xrh0905 xrh0905 merged commit 5a76d5d into edge Dec 9, 2025
4 checks passed
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds comprehensive environment variable support for PORT, buffer sizes (TO_CLT_BUFSIZE, TO_TG_BUFSIZE), and various optional performance/network/metrics configuration parameters to make Docker deployments more flexible. The changes allow users to configure all proxy settings via environment variables without modifying config.py directly.

Key changes:

  • Added environment variable support for 13 configuration parameters including PORT, buffer sizes, performance tunables, network settings, and Prometheus metrics
  • Documented all available environment variables in README.md with organized tables by category
  • Updated docker-compose.yml example to include PORT configuration

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 10 comments.

File Description
config.py Added environment variable parsing for PORT (always set) and 12 optional configuration parameters (only defined when env vars are set, falling back to mtprotoproxy.py defaults)
docker-compose.yml Added PORT=443 environment variable to the example configuration
README.md Added comprehensive environment variables reference documentation organized by category (Core, Security, Performance, Network, Metrics) with defaults and descriptions, plus Docker run example

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread config.py
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)

Copy link

Copilot AI Dec 9, 2025

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:487, the tuple format requires exactly 3 elements (low, margin, high). If a user provides a string like "16384,100" or "16384,100,131072,999", this will either fail unpacking later or use incorrect values.

Consider adding validation:

if "," in _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)
Suggested change
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 uses AI. Check for mistakes.
Comment thread config.py
Comment on lines +57 to +61
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)

Copy link

Copilot AI Dec 9, 2025

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")
Suggested change
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 uses AI. Check for mistakes.
Comment thread config.py
Comment on lines +64 to +66
_stats_print_period = os.environ.get("STATS_PRINT_PERIOD", None)
if _stats_print_period:
STATS_PRINT_PERIOD = int(_stats_print_period)
Copy link

Copilot AI Dec 9, 2025

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 uses AI. Check for mistakes.
Comment thread config.py
Comment on lines +58 to +61
TO_TG_BUFSIZE = tuple(int(x.strip()) for x in _to_tg_bufsize_env.split(","))
else:
TO_TG_BUFSIZE = int(_to_tg_bufsize_env)

Copy link

Copilot AI Dec 9, 2025

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.

Suggested change
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 uses AI. Check for mistakes.
Comment thread README.md

| 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 |
Copy link

Copilot AI Dec 9, 2025

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) not 16384,100,131072 (a string). According to mtprotoproxy.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.

Suggested change
| `TO_CLT_BUFSIZE` | `16384,100,131072` | Buffer size to client. Single integer or comma-separated tuple (low,users_margin,high) for adaptive sizing |
| `TO_CLT_BUFSIZE` | `(16384, 100, 131072)` | Buffer size to client. Single integer or tuple (low, users_margin, high) for adaptive sizing. Default is a tuple. |

Copilot uses AI. Check for mistakes.
Comment thread README.md
|----------|---------|-------------|
| `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 |
Copy link

Copilot AI Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The documentation states the default is "Auto-detected" based on mtprotoproxy.py:164 which sets it to socket.has_ipv6. However, the table format should clarify this is a boolean value that gets auto-detected. Consider changing to something like: Auto-detected (socket.has_ipv6) or true if IPv6 available to be clearer about the data type.

Suggested change
| `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 |

Copilot uses AI. Check for mistakes.
Comment thread config.py
Comment on lines +49 to +53
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)

Copy link

Copilot AI Dec 9, 2025

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")
Suggested change
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 uses AI. Check for mistakes.
Comment thread config.py
# Client keepalive period in seconds
_client_keepalive = os.environ.get("CLIENT_KEEPALIVE", None)
if _client_keepalive:
CLIENT_KEEPALIVE = int(_client_keepalive)
Copy link

Copilot AI Dec 9, 2025

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.

Suggested change
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 uses AI. Check for mistakes.
Comment thread config.py
Comment on lines +76 to +77
TG_CONNECT_TIMEOUT = int(_tg_connect_timeout)

Copy link

Copilot AI Dec 9, 2025

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.

Suggested change
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 uses AI. Check for mistakes.
Comment thread config.py
Comment on lines +103 to +104
METRICS_PORT = int(_metrics_port)

Copy link

Copilot AI Dec 9, 2025

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.

Suggested change
METRICS_PORT = int(_metrics_port)
try:
METRICS_PORT = int(_metrics_port)
except (ValueError, TypeError):
METRICS_PORT = None

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing ENVIROMENT variable for configs

3 participants