mirror of
https://github.com/telemt/telemt.git
synced 2026-04-15 01:24:09 +03:00
Compare commits
145 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9b3ba2e1c6 | ||
|
|
dbadbf0221 | ||
|
|
173624c838 | ||
|
|
de2047adf2 | ||
|
|
5df2fe9f97 | ||
|
|
2510ebaa79 | ||
|
|
314f30a434 | ||
|
|
c86a511638 | ||
|
|
f1efaf4491 | ||
|
|
716b4adef2 | ||
|
|
5876623bb0 | ||
|
|
6b9c7f7862 | ||
|
|
7ea6387278 | ||
|
|
4c2bc2f41f | ||
|
|
c86f35f059 | ||
|
|
3492566842 | ||
|
|
349bbbb8fa | ||
|
|
ead08981e7 | ||
|
|
068cf825b9 | ||
|
|
7269dfbdc5 | ||
|
|
533708f885 | ||
|
|
5e93ce258f | ||
|
|
1236505502 | ||
|
|
f7d451e689 | ||
|
|
e11da6d2ae | ||
|
|
d31b4cd6c8 | ||
|
|
f4ec6bb303 | ||
|
|
a6132bac38 | ||
|
|
624870109e | ||
|
|
cdf829de91 | ||
|
|
6ef51dbfb0 | ||
|
|
af5f0b9692 | ||
|
|
bd0dcfff15 | ||
|
|
ec4e48808e | ||
|
|
c293901669 | ||
|
|
f4e5a08614 | ||
|
|
430a0ae6b4 | ||
|
|
53d93880ad | ||
|
|
1706698a83 | ||
|
|
cb0832b803 | ||
|
|
c01ca40b6d | ||
|
|
cfec6dbb3c | ||
|
|
1fe1acadd4 | ||
|
|
225fc3e4ea | ||
|
|
4a0d88ad43 | ||
|
|
58ff0c7971 | ||
|
|
7d39bf1698 | ||
|
|
3b8eea762b | ||
|
|
07ec84d071 | ||
|
|
235642459a | ||
|
|
3799fc13c4 | ||
|
|
71261522bd | ||
|
|
762deac511 | ||
|
|
4300720d35 | ||
|
|
b7a8e759eb | ||
|
|
1a68dc1c2d | ||
|
|
a6d22e8a57 | ||
|
|
9477103f89 | ||
|
|
e589891706 | ||
|
|
fad4b652c4 | ||
|
|
96bfc223fe | ||
|
|
265b9a5f11 | ||
|
|
74ad9037de | ||
|
|
49f4a7bb22 | ||
|
|
ac453638b8 | ||
|
|
e7773b2bda | ||
|
|
6f1980dfd7 | ||
|
|
427fbef50f | ||
|
|
08609f4b6d | ||
|
|
501d802b8d | ||
|
|
e8ff39d2ae | ||
|
|
6c1b837d5b | ||
|
|
b112908c86 | ||
|
|
1e400d4cc2 | ||
|
|
a11c8b659b | ||
|
|
bc432f06e2 | ||
|
|
338636ede6 | ||
|
|
c05779208e | ||
|
|
7ba21ec5a8 | ||
|
|
d997c0b216 | ||
|
|
62cf4f0a1c | ||
|
|
e710fefed2 | ||
|
|
edef06edb5 | ||
|
|
7a0b015e65 | ||
|
|
8b2ec35c46 | ||
|
|
d324d84ec7 | ||
|
|
47b12f9489 | ||
|
|
a5967d0ca3 | ||
|
|
44cdfd4b23 | ||
|
|
25ffcf6081 | ||
|
|
60322807b6 | ||
|
|
ed93b0a030 | ||
|
|
2370c8d5e4 | ||
|
|
a3197b0fe1 | ||
|
|
e27ef04c3d | ||
|
|
cf7e2ebf4b | ||
|
|
685bfafe74 | ||
|
|
0f6fcf49a7 | ||
|
|
036f0e1569 | ||
|
|
291c22583f | ||
|
|
ee5b01bb31 | ||
|
|
ccacf78890 | ||
|
|
42db1191a8 | ||
|
|
9ce26d16cb | ||
|
|
12e68f805f | ||
|
|
62bf31fc73 | ||
|
|
29d4636249 | ||
|
|
9afaa28add | ||
|
|
6c12af2b94 | ||
|
|
8b39a4ef6d | ||
|
|
fa2423dadf | ||
|
|
449a87d2e3 | ||
|
|
a61882af6e | ||
|
|
bf11ebbaa3 | ||
|
|
e0d5561095 | ||
|
|
6b8aa7270e | ||
|
|
372f477927 | ||
|
|
05edbab06c | ||
|
|
3d9660f83e | ||
|
|
ac064fe773 | ||
|
|
eba158ff8b | ||
|
|
54ee6ff810 | ||
|
|
6d6cd30227 | ||
|
|
60231224ac | ||
|
|
144f81c473 | ||
|
|
04e6135935 | ||
|
|
4eebb4feb2 | ||
|
|
1f255d0aa4 | ||
|
|
9d2ff25bf5 | ||
|
|
7782336264 | ||
|
|
92a3529733 | ||
|
|
8ce8348cd5 | ||
|
|
e25b7f5ff8 | ||
|
|
d7182ae817 | ||
|
|
97f2dc8489 | ||
|
|
fb1f85559c | ||
|
|
da684b11fe | ||
|
|
896e129155 | ||
|
|
7ead0cd753 | ||
|
|
6cf9687dd6 | ||
|
|
4e30a4999c | ||
|
|
4af40f7121 | ||
|
|
1e4ba2eb56 | ||
|
|
eb921e2b17 | ||
|
|
76f1b51018 |
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -2087,7 +2087,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "telemt"
|
name = "telemt"
|
||||||
version = "3.0.13"
|
version = "3.1.3"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"aes",
|
"aes",
|
||||||
"anyhow",
|
"anyhow",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "telemt"
|
name = "telemt"
|
||||||
version = "3.1.0"
|
version = "3.2.1"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|||||||
14
README.md
14
README.md
@@ -1,7 +1,11 @@
|
|||||||
# Telemt - MTProxy on Rust + Tokio
|
# Telemt - MTProxy on Rust + Tokio
|
||||||
|
|
||||||
|
***Löst Probleme, bevor andere überhaupt wissen, dass sie existieren*** / ***It solves problems before others even realize they exist***
|
||||||
|
|
||||||
**Telemt** is a fast, secure, and feature-rich server written in Rust: it fully implements the official Telegram proxy algo and adds many production-ready improvements such as connection pooling, replay protection, detailed statistics, masking from "prying" eyes
|
**Telemt** is a fast, secure, and feature-rich server written in Rust: it fully implements the official Telegram proxy algo and adds many production-ready improvements such as connection pooling, replay protection, detailed statistics, masking from "prying" eyes
|
||||||
|
|
||||||
|
[**Telemt Chat in Telegram**](https://t.me/telemtrs)
|
||||||
|
|
||||||
## NEWS and EMERGENCY
|
## NEWS and EMERGENCY
|
||||||
### ✈️ Telemt 3 is released!
|
### ✈️ Telemt 3 is released!
|
||||||
<table>
|
<table>
|
||||||
@@ -189,6 +193,8 @@ then Ctrl+X -> Y -> Enter to save
|
|||||||
|
|
||||||
**5.** In Shell type `systemctl enable telemt` - then telemt will start with system startup, after the network is up
|
**5.** In Shell type `systemctl enable telemt` - then telemt will start with system startup, after the network is up
|
||||||
|
|
||||||
|
**6.** In Shell type `journalctl -u telemt -n -g "links" --no-pager -o cat | tac` - get the connection links
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
### Minimal Configuration for First Start
|
### Minimal Configuration for First Start
|
||||||
```toml
|
```toml
|
||||||
@@ -211,10 +217,12 @@ hello = "00000000000000000000000000000000"
|
|||||||
|
|
||||||
```
|
```
|
||||||
### Advanced
|
### Advanced
|
||||||
#### Adtag
|
#### Adtag (per-user)
|
||||||
To use channel advertising and usage statistics from Telegram, get Adtag from [@mtproxybot](https://t.me/mtproxybot), add this parameter to section `[General]`
|
To use channel advertising and usage statistics from Telegram, get an Adtag from [@mtproxybot](https://t.me/mtproxybot). Set it per user in `[access.user_ad_tags]` (32 hex chars):
|
||||||
```toml
|
```toml
|
||||||
ad_tag = "00000000000000000000000000000000" # Replace zeros to your adtag from @mtproxybot
|
[access.user_ad_tags]
|
||||||
|
username1 = "11111111111111111111111111111111" # Replace with your tag from @mtproxybot
|
||||||
|
username2 = "22222222222222222222222222222222"
|
||||||
```
|
```
|
||||||
#### Listening and Announce IPs
|
#### Listening and Announce IPs
|
||||||
To specify listening address and/or address in links, add to section `[[server.listeners]]` of config.toml:
|
To specify listening address and/or address in links, add to section `[[server.listeners]]` of config.toml:
|
||||||
|
|||||||
675
config.full.toml
675
config.full.toml
@@ -1,175 +1,669 @@
|
|||||||
# Telemt full config with default values.
|
# ==============================================================================
|
||||||
# Examples are kept in comments after '#'.
|
#
|
||||||
|
# TELEMT — Advanced Rust-based Telegram MTProto Proxy
|
||||||
|
# Full Configuration Reference
|
||||||
|
#
|
||||||
|
# This file is both a working config and a complete documentation.
|
||||||
|
# Every parameter is explained. Read it top to bottom before deploying.
|
||||||
|
#
|
||||||
|
# Quick Start:
|
||||||
|
# 1. Set [server].port to your desired port (443 recommended)
|
||||||
|
# 2. Generate a secret: openssl rand -hex 16
|
||||||
|
# 3. Put it in [access.users] under a name you choose
|
||||||
|
# 4. Set [censorship].tls_domain to a popular unblocked HTTPS site
|
||||||
|
# 5. Set your public IP in [general].middle_proxy_nat_ip
|
||||||
|
# and [general.links].public_host
|
||||||
|
# 6. Set announce IP in [[server.listeners]]
|
||||||
|
# 7. Run Telemt. It prints a tg:// link. Send it to your users.
|
||||||
|
#
|
||||||
|
# Modes of Operation:
|
||||||
|
# Direct Mode (use_middle_proxy = false)
|
||||||
|
# Connects straight to Telegram DCs via TCP. Simple, fast, low overhead.
|
||||||
|
# No ad_tag support. No CDN DC support (203, etc).
|
||||||
|
#
|
||||||
|
# Middle-Proxy Mode (use_middle_proxy = true)
|
||||||
|
# Connects to Telegram Middle-End servers via RPC protocol.
|
||||||
|
# Required for ad_tag monetization and CDN support.
|
||||||
|
# Requires proxy_secret_path and a valid public IP.
|
||||||
|
#
|
||||||
|
# ==============================================================================
|
||||||
|
|
||||||
# Top-level legacy field.
|
|
||||||
show_link = [] # example: "*" or ["alice", "bob"]
|
# ==============================================================================
|
||||||
# default_dc = 2 # example: default DC for unmapped non-standard DCs
|
# LEGACY TOP-LEVEL FIELDS
|
||||||
|
# ==============================================================================
|
||||||
|
|
||||||
|
# Deprecated. Use [general.links].show instead.
|
||||||
|
# Accepts "*" for all users, or an array like ["alice", "bob"].
|
||||||
|
show_link = ["0"]
|
||||||
|
|
||||||
|
# Fallback Datacenter index (1-5) when a client requests an unknown DC ID.
|
||||||
|
# DC 2 is Amsterdam (Europe), closest for most CIS users.
|
||||||
|
# default_dc = 2
|
||||||
|
|
||||||
|
|
||||||
|
# ==============================================================================
|
||||||
|
# GENERAL SETTINGS
|
||||||
|
# ==============================================================================
|
||||||
|
|
||||||
[general]
|
[general]
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# Core Protocol
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# Coalesce the MTProto handshake and first data payload into a single TCP packet.
|
||||||
|
# Significantly reduces connection latency. No reason to disable.
|
||||||
fast_mode = true
|
fast_mode = true
|
||||||
use_middle_proxy = false
|
|
||||||
# ad_tag = "00000000000000000000000000000000" # example
|
# How the proxy connects to Telegram servers.
|
||||||
# proxy_secret_path = "proxy-secret" # example custom path
|
# false = Direct TCP to Telegram DCs (simple, low overhead)
|
||||||
# middle_proxy_nat_ip = "203.0.113.10" # example public NAT IP override
|
# true = Middle-End RPC protocol (required for ad_tag and CDN DCs)
|
||||||
|
use_middle_proxy = true
|
||||||
|
|
||||||
|
# 32-char hex Ad-Tag from @MTProxybot for sponsored channel injection.
|
||||||
|
# Only works when use_middle_proxy = true.
|
||||||
|
# Obtain yours: message @MTProxybot on Telegram, register your proxy.
|
||||||
|
# ad_tag = "00000000000000000000000000000000"
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# Middle-End Authentication
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# Path to the Telegram infrastructure AES key file.
|
||||||
|
# Auto-downloaded from https://core.telegram.org/getProxySecret on first run.
|
||||||
|
# This key authenticates your proxy with Middle-End servers.
|
||||||
|
proxy_secret_path = "proxy-secret"
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# Public IP Configuration (Critical for Middle-Proxy Mode)
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# Your server's PUBLIC IPv4 address.
|
||||||
|
# Middle-End servers need this for the cryptographic Key Derivation Function.
|
||||||
|
# If your server has a direct public IP, set it here.
|
||||||
|
# If behind NAT (AWS, Docker, etc.), this MUST be your external IP.
|
||||||
|
# If omitted, Telemt uses STUN to auto-detect (see middle_proxy_nat_probe).
|
||||||
|
# middle_proxy_nat_ip = "203.0.113.10"
|
||||||
|
|
||||||
|
# Auto-detect public IP via STUN servers defined in [network].
|
||||||
|
# Set to false if you hardcoded middle_proxy_nat_ip above.
|
||||||
|
# Set to true if you want automatic detection.
|
||||||
middle_proxy_nat_probe = true
|
middle_proxy_nat_probe = true
|
||||||
# middle_proxy_nat_stun = "stun.l.google.com:19302" # example
|
|
||||||
# middle_proxy_nat_stun_servers = [] # example: ["stun1.l.google.com:19302", "stun2.l.google.com:19302"]
|
# ------------------------------------------------------------------------------
|
||||||
|
# Middle-End Connection Pool
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# Number of persistent multiplexed RPC connections to ME servers.
|
||||||
|
# All client traffic is routed through these "fat pipes".
|
||||||
|
# 8 handles thousands of concurrent users comfortably.
|
||||||
middle_proxy_pool_size = 8
|
middle_proxy_pool_size = 8
|
||||||
|
|
||||||
|
# Legacy field. Connections kept initialized but idle as warm standby.
|
||||||
middle_proxy_warm_standby = 16
|
middle_proxy_warm_standby = 16
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# Middle-End Keepalive
|
||||||
|
# Telegram ME servers aggressively kill idle TCP connections.
|
||||||
|
# These settings send periodic RPC_PING frames to keep pipes alive.
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
me_keepalive_enabled = true
|
me_keepalive_enabled = true
|
||||||
|
|
||||||
|
# Base interval between pings in seconds.
|
||||||
me_keepalive_interval_secs = 25
|
me_keepalive_interval_secs = 25
|
||||||
|
|
||||||
|
# Random jitter added to interval to prevent all connections pinging simultaneously.
|
||||||
me_keepalive_jitter_secs = 5
|
me_keepalive_jitter_secs = 5
|
||||||
|
|
||||||
|
# Randomize ping payload bytes to prevent DPI from fingerprinting ping patterns.
|
||||||
me_keepalive_payload_random = true
|
me_keepalive_payload_random = true
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# Client-Side Limits
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# Max buffered ciphertext per client (bytes) when upstream is slow.
|
||||||
|
# Acts as backpressure to prevent memory exhaustion. 256KB is safe.
|
||||||
crypto_pending_buffer = 262144
|
crypto_pending_buffer = 262144
|
||||||
|
|
||||||
|
# Maximum single MTProto frame size from client. 16MB is protocol standard.
|
||||||
max_client_frame = 16777216
|
max_client_frame = 16777216
|
||||||
desync_all_full = false
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# Crypto Desynchronization Logging
|
||||||
|
# Desync errors usually mean DPI/GFW is tampering with connections.
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# true = full forensics (trace ID, IP hash, hex dumps) for EVERY desync event
|
||||||
|
# false = deduplicated logging, one entry per time window (prevents log spam)
|
||||||
|
# Set true if you are actively debugging DPI interference.
|
||||||
|
desync_all_full = true
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# Beobachten — Built-in Honeypot / Active Probe Tracker
|
||||||
|
# Tracks IPs that fail handshakes or behave like TLS scanners.
|
||||||
|
# Output file can be fed into fail2ban or iptables for auto-blocking.
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
beobachten = true
|
beobachten = true
|
||||||
beobachten_minutes = 10
|
|
||||||
|
# How long (minutes) to remember a suspicious IP before expiring it.
|
||||||
|
beobachten_minutes = 30
|
||||||
|
|
||||||
|
# How often (seconds) to flush tracker state to disk.
|
||||||
beobachten_flush_secs = 15
|
beobachten_flush_secs = 15
|
||||||
|
|
||||||
|
# File path for the tracker output.
|
||||||
beobachten_file = "cache/beobachten.txt"
|
beobachten_file = "cache/beobachten.txt"
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# Hardswap — Zero-Downtime ME Pool Rotation
|
||||||
|
# When Telegram updates ME server IPs, Hardswap creates a completely new pool,
|
||||||
|
# waits until it is fully ready, migrates traffic, then kills the old pool.
|
||||||
|
# Users experience zero interruption.
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
hardswap = true
|
hardswap = true
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# ME Pool Warmup Staggering
|
||||||
|
# When creating a new pool, connections are opened one by one with delays
|
||||||
|
# to avoid a burst of SYN packets that could trigger ISP flood protection.
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
me_warmup_stagger_enabled = true
|
me_warmup_stagger_enabled = true
|
||||||
|
|
||||||
|
# Delay between each connection creation (milliseconds).
|
||||||
me_warmup_step_delay_ms = 500
|
me_warmup_step_delay_ms = 500
|
||||||
|
|
||||||
|
# Random jitter added to the delay (milliseconds).
|
||||||
me_warmup_step_jitter_ms = 300
|
me_warmup_step_jitter_ms = 300
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# ME Reconnect Backoff
|
||||||
|
# If an ME server drops the connection, Telemt retries with this strategy.
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# Max simultaneous reconnect attempts per DC.
|
||||||
me_reconnect_max_concurrent_per_dc = 8
|
me_reconnect_max_concurrent_per_dc = 8
|
||||||
|
|
||||||
|
# Exponential backoff base (milliseconds).
|
||||||
me_reconnect_backoff_base_ms = 500
|
me_reconnect_backoff_base_ms = 500
|
||||||
|
|
||||||
|
# Backoff ceiling (milliseconds). Will never wait longer than this.
|
||||||
me_reconnect_backoff_cap_ms = 30000
|
me_reconnect_backoff_cap_ms = 30000
|
||||||
|
|
||||||
|
# Number of instant retries before switching to exponential backoff.
|
||||||
me_reconnect_fast_retry_count = 12
|
me_reconnect_fast_retry_count = 12
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# NAT Mismatch Behavior
|
||||||
|
# If STUN-detected IP differs from local interface IP (you are behind NAT).
|
||||||
|
# false = abort ME mode (safe default)
|
||||||
|
# true = force ME mode anyway (use if you know your NAT setup is correct)
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
stun_iface_mismatch_ignore = false
|
stun_iface_mismatch_ignore = false
|
||||||
unknown_dc_log_path = "unknown-dc.txt" # to disable: set to null
|
|
||||||
log_level = "normal" # debug | verbose | normal | silent
|
# ------------------------------------------------------------------------------
|
||||||
|
# Logging
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# File to log unknown DC requests (DC IDs outside standard 1-5).
|
||||||
|
unknown_dc_log_path = "unknown-dc.txt"
|
||||||
|
|
||||||
|
# Verbosity: "debug" | "verbose" | "normal" | "silent"
|
||||||
|
log_level = "normal"
|
||||||
|
|
||||||
|
# Disable ANSI color codes in log output (useful for file logging).
|
||||||
disable_colors = false
|
disable_colors = false
|
||||||
fast_mode_min_tls_record = 0
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# FakeTLS Record Sizing
|
||||||
|
# Buffer small MTProto packets into larger TLS records to mimic real HTTPS.
|
||||||
|
# Real HTTPS servers send records close to MTU size (~1400 bytes).
|
||||||
|
# A stream of tiny TLS records is a strong DPI signal.
|
||||||
|
# Set to 0 to disable. Set to 1400 for realistic HTTPS emulation.
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
fast_mode_min_tls_record = 1400
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# Periodic Updates
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# How often (seconds) to re-fetch ME server lists and proxy secrets
|
||||||
|
# from core.telegram.org. Keeps your proxy in sync with Telegram infrastructure.
|
||||||
update_every = 300
|
update_every = 300
|
||||||
me_reinit_every_secs = 900
|
|
||||||
|
# How often (seconds) to force a Hardswap even if the ME map is unchanged.
|
||||||
|
# Shorter intervals mean shorter-lived TCP flows, harder for DPI to profile.
|
||||||
|
me_reinit_every_secs = 600
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# Hardswap Warmup Tuning
|
||||||
|
# Fine-grained control over how the new pool is warmed up before traffic switch.
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
me_hardswap_warmup_delay_min_ms = 1000
|
me_hardswap_warmup_delay_min_ms = 1000
|
||||||
me_hardswap_warmup_delay_max_ms = 2000
|
me_hardswap_warmup_delay_max_ms = 2000
|
||||||
me_hardswap_warmup_extra_passes = 3
|
me_hardswap_warmup_extra_passes = 3
|
||||||
me_hardswap_warmup_pass_backoff_base_ms = 500
|
me_hardswap_warmup_pass_backoff_base_ms = 500
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# Config Update Debouncing
|
||||||
|
# Telegram sometimes pushes transient/broken configs. Debouncing requires
|
||||||
|
# N consecutive identical fetches before applying a change.
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# ME server list must be identical for this many fetches before applying.
|
||||||
me_config_stable_snapshots = 2
|
me_config_stable_snapshots = 2
|
||||||
|
|
||||||
|
# Minimum seconds between config applications.
|
||||||
me_config_apply_cooldown_secs = 300
|
me_config_apply_cooldown_secs = 300
|
||||||
|
|
||||||
|
# Proxy secret must be identical for this many fetches before applying.
|
||||||
proxy_secret_stable_snapshots = 2
|
proxy_secret_stable_snapshots = 2
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# Proxy Secret Rotation
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# Apply newly downloaded secrets at runtime without restart.
|
||||||
proxy_secret_rotate_runtime = true
|
proxy_secret_rotate_runtime = true
|
||||||
|
|
||||||
|
# Maximum acceptable secret length (bytes). Rejects abnormally large secrets.
|
||||||
proxy_secret_len_max = 256
|
proxy_secret_len_max = 256
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# Hardswap Drain Settings
|
||||||
|
# Controls graceful shutdown of old ME connections during pool rotation.
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# Seconds to keep old connections alive for in-flight data before force-closing.
|
||||||
me_pool_drain_ttl_secs = 90
|
me_pool_drain_ttl_secs = 90
|
||||||
|
|
||||||
|
# Minimum ratio of healthy connections in new pool before draining old pool.
|
||||||
|
# 0.8 = at least 80% of new pool must be ready.
|
||||||
me_pool_min_fresh_ratio = 0.8
|
me_pool_min_fresh_ratio = 0.8
|
||||||
|
|
||||||
|
# Maximum seconds to wait for drain to complete before force-killing.
|
||||||
me_reinit_drain_timeout_secs = 120
|
me_reinit_drain_timeout_secs = 120
|
||||||
# Legacy compatibility fields used when update_every is omitted.
|
|
||||||
proxy_secret_auto_reload_secs = 3600
|
# ------------------------------------------------------------------------------
|
||||||
proxy_config_auto_reload_secs = 3600
|
# NTP Clock Check
|
||||||
|
# MTProto uses timestamps. Clock drift > 30 seconds breaks handshakes.
|
||||||
|
# Telemt checks on startup and warns if out of sync.
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
ntp_check = true
|
ntp_check = true
|
||||||
ntp_servers = ["pool.ntp.org"] # example: ["pool.ntp.org", "time.cloudflare.com"]
|
ntp_servers = ["pool.ntp.org"]
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# Auto-Degradation
|
||||||
|
# If ME servers become completely unreachable (ISP blocking),
|
||||||
|
# automatically fall back to Direct Mode so users stay connected.
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
auto_degradation_enabled = true
|
auto_degradation_enabled = true
|
||||||
|
|
||||||
|
# Number of DC groups that must be unreachable before triggering fallback.
|
||||||
degradation_min_unavailable_dc_groups = 2
|
degradation_min_unavailable_dc_groups = 2
|
||||||
|
|
||||||
|
|
||||||
|
# ==============================================================================
|
||||||
|
# ALLOWED CLIENT PROTOCOLS
|
||||||
|
# Only enable what you need. In censored regions, TLS-only is safest.
|
||||||
|
# ==============================================================================
|
||||||
|
|
||||||
[general.modes]
|
[general.modes]
|
||||||
|
|
||||||
|
# Classic MTProto. Unobfuscated length prefixes. Trivially detected by DPI.
|
||||||
|
# No reason to enable unless you have ancient clients.
|
||||||
classic = false
|
classic = false
|
||||||
|
|
||||||
|
# Obfuscated MTProto with randomized padding. Better than classic, but
|
||||||
|
# still detectable by statistical analysis of packet sizes.
|
||||||
secure = false
|
secure = false
|
||||||
|
|
||||||
|
# FakeTLS (ee-secrets). Wraps MTProto in TLS 1.3 framing.
|
||||||
|
# To DPI, it looks like a normal HTTPS connection.
|
||||||
|
# This should be the ONLY enabled mode in censored environments.
|
||||||
tls = true
|
tls = true
|
||||||
|
|
||||||
|
|
||||||
|
# ==============================================================================
|
||||||
|
# STARTUP LINK GENERATION
|
||||||
|
# Controls what tg:// invite links are printed to console on startup.
|
||||||
|
# ==============================================================================
|
||||||
|
|
||||||
[general.links]
|
[general.links]
|
||||||
show ="*" # example: "*" or ["alice", "bob"]
|
|
||||||
# public_host = "proxy.example.com" # example explicit host/IP for tg:// links
|
# Which users to generate links for.
|
||||||
# public_port = 443 # example explicit port for tg:// links
|
# "*" = all users, or an array like ["alice", "bob"].
|
||||||
|
show = "*"
|
||||||
|
|
||||||
|
# IP or domain to embed in the tg:// link.
|
||||||
|
# If omitted, Telemt uses STUN to auto-detect.
|
||||||
|
# Set this to your server's public IP or domain for reliable links.
|
||||||
|
# public_host = "proxy.example.com"
|
||||||
|
|
||||||
|
# Port to embed in the tg:// link.
|
||||||
|
# If omitted, uses [server].port.
|
||||||
|
# public_port = 443
|
||||||
|
|
||||||
|
|
||||||
|
# ==============================================================================
|
||||||
|
# NETWORK & IP RESOLUTION
|
||||||
|
# ==============================================================================
|
||||||
|
|
||||||
[network]
|
[network]
|
||||||
|
|
||||||
|
# Enable IPv4 for outbound connections to Telegram.
|
||||||
ipv4 = true
|
ipv4 = true
|
||||||
ipv6 = false # set true to enable IPv6
|
|
||||||
prefer = 4 # 4 or 6
|
# Enable IPv6 for outbound connections to Telegram.
|
||||||
|
ipv6 = false
|
||||||
|
|
||||||
|
# Prefer IPv4 (4) or IPv6 (6) when both are available.
|
||||||
|
prefer = 4
|
||||||
|
|
||||||
|
# Experimental: use both IPv4 and IPv6 ME servers simultaneously.
|
||||||
|
# May improve reliability but doubles connection count.
|
||||||
multipath = false
|
multipath = false
|
||||||
|
|
||||||
|
# STUN servers for external IP discovery.
|
||||||
|
# Used for Middle-Proxy KDF (if nat_probe=true) and link generation.
|
||||||
stun_servers = [
|
stun_servers = [
|
||||||
"stun.l.google.com:5349",
|
"stun.l.google.com:5349",
|
||||||
"stun1.l.google.com:3478",
|
"stun1.l.google.com:3478",
|
||||||
"stun.gmx.net:3478",
|
"stun.gmx.net:3478",
|
||||||
"stun.l.google.com:19302",
|
"stun.l.google.com:19302"
|
||||||
"stun.1und1.de:3478",
|
|
||||||
"stun1.l.google.com:19302",
|
|
||||||
"stun2.l.google.com:19302",
|
|
||||||
"stun3.l.google.com:19302",
|
|
||||||
"stun4.l.google.com:19302",
|
|
||||||
"stun.services.mozilla.com:3478",
|
|
||||||
"stun.stunprotocol.org:3478",
|
|
||||||
"stun.nextcloud.com:3478",
|
|
||||||
"stun.voip.eutelia.it:3478",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
# If UDP STUN is blocked, attempt TCP-based STUN as fallback.
|
||||||
stun_tcp_fallback = true
|
stun_tcp_fallback = true
|
||||||
http_ip_detect_urls = ["https://ifconfig.me/ip", "https://api.ipify.org"]
|
|
||||||
|
# If all STUN fails, use HTTP APIs to discover public IP.
|
||||||
|
http_ip_detect_urls = [
|
||||||
|
"https://ifconfig.me/ip",
|
||||||
|
"https://api.ipify.org"
|
||||||
|
]
|
||||||
|
|
||||||
|
# Cache discovered public IP to this file to survive restarts.
|
||||||
cache_public_ip_path = "cache/public_ip.txt"
|
cache_public_ip_path = "cache/public_ip.txt"
|
||||||
|
|
||||||
|
|
||||||
|
# ==============================================================================
|
||||||
|
# SERVER BINDING & METRICS
|
||||||
|
# ==============================================================================
|
||||||
|
|
||||||
[server]
|
[server]
|
||||||
|
|
||||||
|
# TCP port to listen on.
|
||||||
|
# 443 is recommended (looks like normal HTTPS traffic).
|
||||||
port = 443
|
port = 443
|
||||||
|
|
||||||
|
# IPv4 bind address. "0.0.0.0" = all interfaces.
|
||||||
listen_addr_ipv4 = "0.0.0.0"
|
listen_addr_ipv4 = "0.0.0.0"
|
||||||
|
|
||||||
|
# IPv6 bind address. "::" = all interfaces.
|
||||||
listen_addr_ipv6 = "::"
|
listen_addr_ipv6 = "::"
|
||||||
# listen_unix_sock = "/var/run/telemt.sock" # example
|
|
||||||
# listen_unix_sock_perm = "0660" # example unix socket mode
|
# Unix socket listener (for reverse proxy setups with Nginx/HAProxy).
|
||||||
# listen_tcp = true # example explicit override (auto-detected when omitted)
|
# listen_unix_sock = "/var/run/telemt.sock"
|
||||||
|
# listen_unix_sock_perm = "0660"
|
||||||
|
|
||||||
|
# Enable PROXY protocol header parsing.
|
||||||
|
# Set true ONLY if Telemt is behind HAProxy/Nginx that injects PROXY headers.
|
||||||
|
# If enabled without a proxy in front, clients will fail to connect.
|
||||||
proxy_protocol = false
|
proxy_protocol = false
|
||||||
# metrics_port = 9090 # example
|
|
||||||
metrics_whitelist = ["127.0.0.1/32", "::1/128"]
|
# Prometheus metrics HTTP endpoint port.
|
||||||
# Example explicit listeners (default: omitted, auto-generated from listen_addr_*):
|
# Uncomment to enable. Access at http://your-server:9090/metrics
|
||||||
|
# metrics_port = 9090
|
||||||
|
|
||||||
|
# IP ranges allowed to access the metrics endpoint.
|
||||||
|
metrics_whitelist = [
|
||||||
|
"127.0.0.1/32",
|
||||||
|
"::1/128"
|
||||||
|
]
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# Listener Overrides
|
||||||
|
# Define explicit listeners with specific bind IPs and announce IPs.
|
||||||
|
# The announce IP is what gets embedded in tg:// links and sent to ME servers.
|
||||||
|
# You MUST set announce to your server's public IP for ME mode to work.
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
# [[server.listeners]]
|
# [[server.listeners]]
|
||||||
# ip = "0.0.0.0"
|
# ip = "0.0.0.0"
|
||||||
# announce = "proxy-v4.example.com"
|
# announce = "203.0.113.10"
|
||||||
# # announce_ip = "203.0.113.10" # deprecated alias
|
|
||||||
# proxy_protocol = false
|
|
||||||
# reuse_allow = false
|
|
||||||
#
|
|
||||||
# [[server.listeners]]
|
|
||||||
# ip = "::"
|
|
||||||
# announce = "proxy-v6.example.com"
|
|
||||||
# proxy_protocol = false
|
|
||||||
# reuse_allow = false
|
# reuse_allow = false
|
||||||
|
|
||||||
|
|
||||||
|
# ==============================================================================
|
||||||
|
# TIMEOUTS (seconds unless noted)
|
||||||
|
# ==============================================================================
|
||||||
|
|
||||||
[timeouts]
|
[timeouts]
|
||||||
|
|
||||||
|
# Maximum time for client to complete FakeTLS + MTProto handshake.
|
||||||
client_handshake = 15
|
client_handshake = 15
|
||||||
|
|
||||||
|
# Maximum time to establish TCP connection to upstream Telegram DC.
|
||||||
tg_connect = 10
|
tg_connect = 10
|
||||||
|
|
||||||
|
# TCP keepalive interval for client connections.
|
||||||
client_keepalive = 60
|
client_keepalive = 60
|
||||||
|
|
||||||
|
# Maximum client inactivity before dropping the connection.
|
||||||
client_ack = 300
|
client_ack = 300
|
||||||
|
|
||||||
|
# Instant retry count for a single ME endpoint before giving up on it.
|
||||||
me_one_retry = 3
|
me_one_retry = 3
|
||||||
|
|
||||||
|
# Timeout (milliseconds) for a single ME endpoint connection attempt.
|
||||||
me_one_timeout_ms = 1500
|
me_one_timeout_ms = 1500
|
||||||
|
|
||||||
|
|
||||||
|
# ==============================================================================
|
||||||
|
# ANTI-CENSORSHIP / FAKETLS / MASKING
|
||||||
|
# This is where Telemt becomes invisible to Deep Packet Inspection.
|
||||||
|
# ==============================================================================
|
||||||
|
|
||||||
[censorship]
|
[censorship]
|
||||||
tls_domain = "petrovich.ru"
|
|
||||||
# tls_domains = ["example.com", "cdn.example.net"] # Additional domains for EE links
|
# ------------------------------------------------------------------------------
|
||||||
|
# TLS Domain Fronting
|
||||||
|
# The SNI (Server Name Indication) your proxy presents to connecting clients.
|
||||||
|
# Must be a popular, unblocked HTTPS website in your target country.
|
||||||
|
# DPI sees traffic to this domain. Choose carefully.
|
||||||
|
# Good choices: major CDNs, banks, government sites, search engines.
|
||||||
|
# Bad choices: obscure sites, already-blocked domains.
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
tls_domain = "www.google.com"
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# Active Probe Masking
|
||||||
|
# When someone connects but fails the MTProto handshake (wrong secret),
|
||||||
|
# they might be an ISP active prober testing if this is a proxy.
|
||||||
|
#
|
||||||
|
# mask = false: drop the connection (prober knows something is here)
|
||||||
|
# mask = true: transparently proxy them to mask_host (prober sees a real website)
|
||||||
|
#
|
||||||
|
# With mask enabled, your server is indistinguishable from a real web server
|
||||||
|
# to anyone who doesn't have the correct secret.
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
mask = true
|
mask = true
|
||||||
# mask_host = "www.google.com" # example, defaults to tls_domain when both mask_host/mask_unix_sock are unset
|
|
||||||
# mask_unix_sock = "/var/run/nginx.sock" # example, mutually exclusive with mask_host
|
# The real web server to forward failed handshakes to.
|
||||||
|
# If omitted, defaults to tls_domain.
|
||||||
|
# mask_host = "www.google.com"
|
||||||
|
|
||||||
|
# Port on the mask host to connect to.
|
||||||
mask_port = 443
|
mask_port = 443
|
||||||
fake_cert_len = 2048 # if tls_emulation=false and default value is used, loader may randomize this value at runtime
|
|
||||||
|
# Inject PROXY protocol header when forwarding to mask host.
|
||||||
|
# 0 = disabled, 1 = v1, 2 = v2. Leave disabled unless mask_host expects it.
|
||||||
|
# mask_proxy_protocol = 0
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# TLS Certificate Emulation
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# Size (bytes) of the locally generated fake TLS certificate.
|
||||||
|
# Only used when tls_emulation is disabled.
|
||||||
|
fake_cert_len = 2048
|
||||||
|
|
||||||
|
# KILLER FEATURE: Real-Time TLS Emulation.
|
||||||
|
# Telemt connects to tls_domain, fetches its actual TLS 1.3 certificate chain,
|
||||||
|
# and exactly replicates the byte sizes of ServerHello and Certificate records.
|
||||||
|
# Defeats DPI that uses TLS record length heuristics to detect proxies.
|
||||||
|
# Strongly recommended in censored environments.
|
||||||
tls_emulation = true
|
tls_emulation = true
|
||||||
|
|
||||||
|
# Directory to cache fetched TLS certificates.
|
||||||
tls_front_dir = "tlsfront"
|
tls_front_dir = "tlsfront"
|
||||||
server_hello_delay_min_ms = 0
|
|
||||||
server_hello_delay_max_ms = 0
|
# ------------------------------------------------------------------------------
|
||||||
tls_new_session_tickets = 0
|
# ServerHello Timing
|
||||||
tls_full_cert_ttl_secs = 90
|
# Real web servers take 30-150ms to respond to ClientHello due to network
|
||||||
|
# latency and crypto processing. A proxy responding in <1ms is suspicious.
|
||||||
|
# These settings add realistic delay to mimic genuine server behavior.
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# Minimum delay before sending ServerHello (milliseconds).
|
||||||
|
server_hello_delay_min_ms = 50
|
||||||
|
|
||||||
|
# Maximum delay before sending ServerHello (milliseconds).
|
||||||
|
server_hello_delay_max_ms = 150
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# TLS Session Tickets
|
||||||
|
# Real TLS 1.3 servers send 1-2 NewSessionTicket messages after handshake.
|
||||||
|
# A server that sends zero tickets is anomalous and may trigger DPI flags.
|
||||||
|
# Set this to match your tls_domain's behavior (usually 2).
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# tls_new_session_tickets = 0
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# Full Certificate Frequency
|
||||||
|
# When tls_emulation is enabled, this controls how often (per client IP)
|
||||||
|
# to send the complete emulated certificate chain.
|
||||||
|
#
|
||||||
|
# > 0: Subsequent connections within TTL seconds get a smaller cached version.
|
||||||
|
# Saves bandwidth but creates a detectable size difference between
|
||||||
|
# first and repeat connections.
|
||||||
|
#
|
||||||
|
# = 0: Every connection gets the full certificate. More bandwidth but
|
||||||
|
# perfectly consistent behavior, no anomalies for DPI to detect.
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
tls_full_cert_ttl_secs = 0
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# ALPN Enforcement
|
||||||
|
# Ensure ServerHello responds with the exact ALPN protocol the client requested.
|
||||||
|
# Mismatched ALPN (e.g., client asks h2, server says http/1.1) is a DPI red flag.
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
alpn_enforce = true
|
alpn_enforce = true
|
||||||
|
|
||||||
|
|
||||||
|
# ==============================================================================
|
||||||
|
# ACCESS CONTROL & USERS
|
||||||
|
# ==============================================================================
|
||||||
|
|
||||||
[access]
|
[access]
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# Replay Attack Protection
|
||||||
|
# DPI can record a legitimate user's handshake and replay it later to probe
|
||||||
|
# whether the server is a proxy. Telemt remembers recent handshake nonces
|
||||||
|
# and rejects duplicates.
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# Number of nonce slots in the replay detection buffer.
|
||||||
replay_check_len = 65536
|
replay_check_len = 65536
|
||||||
|
|
||||||
|
# How long (seconds) to remember nonces before expiring them.
|
||||||
replay_window_secs = 1800
|
replay_window_secs = 1800
|
||||||
|
|
||||||
|
# Allow clients with incorrect system clocks to connect.
|
||||||
|
# false = reject clients with significant time skew (more secure)
|
||||||
|
# true = accept anyone regardless of clock (more permissive)
|
||||||
ignore_time_skew = false
|
ignore_time_skew = false
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# User Secrets
|
||||||
|
# Each user needs a unique 32-character hex string as their secret.
|
||||||
|
# Generate with: openssl rand -hex 16
|
||||||
|
#
|
||||||
|
# This secret is embedded in the tg:// link. Anyone with it can connect.
|
||||||
|
# Format: username = "hex_secret"
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
[access.users]
|
[access.users]
|
||||||
# format: "username" = "32_hex_chars_secret"
|
# alice = "0123456789abcdef0123456789abcdef"
|
||||||
hello = "00000000000000000000000000000000"
|
# bob = "fedcba9876543210fedcba9876543210"
|
||||||
# alice = "11111111111111111111111111111111" # example
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# Per-User Connection Limits
|
||||||
|
# Limits concurrent TCP connections per user to prevent secret sharing.
|
||||||
|
# Uncomment and set for each user as needed.
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
[access.user_max_tcp_conns]
|
[access.user_max_tcp_conns]
|
||||||
# alice = 100 # example
|
# alice = 100
|
||||||
|
# bob = 50
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# Per-User Expiration Dates
|
||||||
|
# Automatically revoke access after the specified date (ISO 8601 format).
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
[access.user_expirations]
|
[access.user_expirations]
|
||||||
# alice = "2078-01-01T00:00:00Z" # example
|
# alice = "2025-12-31T23:59:59Z"
|
||||||
|
# bob = "2026-06-15T00:00:00Z"
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# Per-User Data Quotas
|
||||||
|
# Maximum total bytes transferred per user. Connection refused after limit.
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
[access.user_data_quota]
|
[access.user_data_quota]
|
||||||
# hello = 10737418240 # example bytes
|
# alice = 107374182400
|
||||||
# alice = 10737418240 # example bytes
|
# bob = 53687091200
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# Per-User Unique IP Limits
|
||||||
|
# Maximum number of different IP addresses that can use this secret
|
||||||
|
# at the same time. Highly effective against secret leaking/sharing.
|
||||||
|
# Set to 1 for single-device, 2-3 for phone+desktop, etc.
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
[access.user_max_unique_ips]
|
[access.user_max_unique_ips]
|
||||||
# hello = 10 # example
|
# alice = 3
|
||||||
# alice = 100 # example
|
# bob = 2
|
||||||
|
|
||||||
|
|
||||||
|
# ==============================================================================
|
||||||
|
# UPSTREAM ROUTING
|
||||||
|
# Controls how Telemt connects to Telegram servers (or ME servers).
|
||||||
|
# If omitted entirely, uses the OS default route.
|
||||||
|
# ==============================================================================
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# Direct upstream: use the server's own network interface.
|
||||||
|
# You can optionally bind to a specific interface or local IP.
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
# Default behavior if [[upstreams]] is omitted: loader injects one direct upstream.
|
|
||||||
# Example explicit upstreams:
|
|
||||||
# [[upstreams]]
|
# [[upstreams]]
|
||||||
# type = "direct"
|
# type = "direct"
|
||||||
# interface = "eth0"
|
# interface = "eth0"
|
||||||
@@ -177,28 +671,27 @@ hello = "00000000000000000000000000000000"
|
|||||||
# weight = 1
|
# weight = 1
|
||||||
# enabled = true
|
# enabled = true
|
||||||
# scopes = "*"
|
# scopes = "*"
|
||||||
#
|
|
||||||
# [[upstreams]]
|
# ------------------------------------------------------------------------------
|
||||||
# type = "socks4"
|
# SOCKS5 upstream: route Telegram traffic through a SOCKS5 proxy.
|
||||||
# address = "198.51.100.20:1080"
|
# Useful if your server's IP is blocked from reaching Telegram DCs.
|
||||||
# interface = "eth0"
|
# ------------------------------------------------------------------------------
|
||||||
# user_id = "telemt"
|
|
||||||
# weight = 1
|
|
||||||
# enabled = true
|
|
||||||
# scopes = "*"
|
|
||||||
#
|
|
||||||
# [[upstreams]]
|
# [[upstreams]]
|
||||||
# type = "socks5"
|
# type = "socks5"
|
||||||
# address = "198.51.100.30:1080"
|
# address = "198.51.100.30:1080"
|
||||||
# interface = "eth0"
|
|
||||||
# username = "proxy-user"
|
# username = "proxy-user"
|
||||||
# password = "proxy-pass"
|
# password = "proxy-pass"
|
||||||
# weight = 1
|
# weight = 1
|
||||||
# enabled = true
|
# enabled = true
|
||||||
# scopes = "*"
|
|
||||||
|
|
||||||
# === DC Address Overrides ===
|
|
||||||
|
# ==============================================================================
|
||||||
|
# DATACENTER OVERRIDES
|
||||||
|
# Force specific DC IDs to route to specific IP:Port combinations.
|
||||||
|
# DC 203 (CDN) is auto-injected by Telemt if not specified here.
|
||||||
|
# ==============================================================================
|
||||||
|
|
||||||
# [dc_overrides]
|
# [dc_overrides]
|
||||||
# "201" = "149.154.175.50:443" # example
|
# "201" = "149.154.175.50:443"
|
||||||
# "202" = ["149.154.167.51:443", "149.154.175.100:443"] # example
|
# "202" = ["149.154.167.51:443", "149.154.175.100:443"]
|
||||||
# "203" = "91.105.192.100:443" # loader auto-adds this one when omitted
|
|
||||||
|
|||||||
26
config.toml
26
config.toml
@@ -1,7 +1,13 @@
|
|||||||
|
### Telemt Based Config.toml
|
||||||
|
# We believe that these settings are sufficient for most scenarios
|
||||||
|
# where cutting-egde methods and parameters or special solutions are not needed
|
||||||
|
|
||||||
# === General Settings ===
|
# === General Settings ===
|
||||||
[general]
|
[general]
|
||||||
use_middle_proxy = true
|
use_middle_proxy = false
|
||||||
|
# Global ad_tag fallback when user has no per-user tag in [access.user_ad_tags]
|
||||||
# ad_tag = "00000000000000000000000000000000"
|
# ad_tag = "00000000000000000000000000000000"
|
||||||
|
# Per-user ad_tag in [access.user_ad_tags] (32 hex from @MTProxybot)
|
||||||
|
|
||||||
# === Log Level ===
|
# === Log Level ===
|
||||||
# Log level: debug | verbose | normal | silent
|
# Log level: debug | verbose | normal | silent
|
||||||
@@ -14,12 +20,26 @@ classic = false
|
|||||||
secure = false
|
secure = false
|
||||||
tls = true
|
tls = true
|
||||||
|
|
||||||
|
[general.links]
|
||||||
|
show = "*"
|
||||||
|
# show = ["alice", "bob"] # Only show links for alice and bob
|
||||||
|
# show = "*" # Show links for all users
|
||||||
|
# public_host = "proxy.example.com" # Host (IP or domain) for tg:// links
|
||||||
|
# public_port = 443 # Port for tg:// links (default: server.port)
|
||||||
|
|
||||||
# === Server Binding ===
|
# === Server Binding ===
|
||||||
[server]
|
[server]
|
||||||
port = 443
|
port = 443
|
||||||
# proxy_protocol = false # Enable if behind HAProxy/nginx with PROXY protocol
|
# proxy_protocol = false # Enable if behind HAProxy/nginx with PROXY protocol
|
||||||
metrics_port = 9090
|
# metrics_port = 9090
|
||||||
metrics_whitelist = ["127.0.0.1", "::1", "0.0.0.0/0"]
|
# metrics_whitelist = ["127.0.0.1", "::1", "0.0.0.0/0"]
|
||||||
|
|
||||||
|
[server.api]
|
||||||
|
enabled = true
|
||||||
|
listen = "0.0.0.0:9091"
|
||||||
|
whitelist = ["127.0.0.0/8"]
|
||||||
|
minimal_runtime_enabled = false
|
||||||
|
minimal_runtime_cache_ttl_ms = 1000
|
||||||
|
|
||||||
# Listen on multiple interfaces/IPs - IPv4
|
# Listen on multiple interfaces/IPs - IPv4
|
||||||
[[server.listeners]]
|
[[server.listeners]]
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
services:
|
services:
|
||||||
telemt:
|
telemt:
|
||||||
|
image: ghcr.io/telemt/telemt:latest
|
||||||
build: .
|
build: .
|
||||||
container_name: telemt
|
container_name: telemt
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
ports:
|
ports:
|
||||||
- "443:443"
|
- "443:443"
|
||||||
- "9090:9090"
|
- "127.0.0.1:9090:9090"
|
||||||
# Allow caching 'proxy-secret' in read-only container
|
# Allow caching 'proxy-secret' in read-only container
|
||||||
working_dir: /run/telemt
|
working_dir: /run/telemt
|
||||||
volumes:
|
volumes:
|
||||||
|
|||||||
427
docs/API.md
Normal file
427
docs/API.md
Normal file
@@ -0,0 +1,427 @@
|
|||||||
|
# Telemt Control API
|
||||||
|
|
||||||
|
## Purpose
|
||||||
|
Control-plane HTTP API for runtime visibility and user/config management.
|
||||||
|
Data-plane MTProto traffic is out of scope.
|
||||||
|
|
||||||
|
## Runtime Configuration
|
||||||
|
API runtime is configured in `[server.api]`.
|
||||||
|
|
||||||
|
| Field | Type | Default | Description |
|
||||||
|
| --- | --- | --- | --- |
|
||||||
|
| `enabled` | `bool` | `false` | Enables REST API listener. |
|
||||||
|
| `listen` | `string` (`IP:PORT`) | `127.0.0.1:9091` | API bind address. |
|
||||||
|
| `whitelist` | `CIDR[]` | `127.0.0.1/32, ::1/128` | Source IP allowlist. Empty list means allow all. |
|
||||||
|
| `auth_header` | `string` | `""` | Exact value for `Authorization` header. Empty disables header auth. |
|
||||||
|
| `request_body_limit_bytes` | `usize` | `65536` | Maximum request body size. |
|
||||||
|
| `minimal_runtime_enabled` | `bool` | `false` | Enables runtime snapshot endpoints requiring ME pool read-lock aggregation. |
|
||||||
|
| `minimal_runtime_cache_ttl_ms` | `u64` | `1000` | Cache TTL for minimal snapshots. `0` disables cache. |
|
||||||
|
| `read_only` | `bool` | `false` | Disables mutating endpoints. |
|
||||||
|
|
||||||
|
`server.admin_api` is accepted as an alias for backward compatibility.
|
||||||
|
|
||||||
|
## Protocol Contract
|
||||||
|
|
||||||
|
| Item | Value |
|
||||||
|
| --- | --- |
|
||||||
|
| Transport | HTTP/1.1 |
|
||||||
|
| Content type | `application/json; charset=utf-8` |
|
||||||
|
| Prefix | `/v1` |
|
||||||
|
| Optimistic concurrency | `If-Match: <revision>` on mutating requests (optional) |
|
||||||
|
| Revision format | SHA-256 hex of current `config.toml` content |
|
||||||
|
|
||||||
|
### Success Envelope
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"ok": true,
|
||||||
|
"data": {},
|
||||||
|
"revision": "sha256-hex"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Error Envelope
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"ok": false,
|
||||||
|
"error": {
|
||||||
|
"code": "machine_code",
|
||||||
|
"message": "human-readable"
|
||||||
|
},
|
||||||
|
"request_id": 1
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Endpoint Matrix
|
||||||
|
|
||||||
|
| Method | Path | Body | Success | `data` contract |
|
||||||
|
| --- | --- | --- | --- | --- |
|
||||||
|
| `GET` | `/v1/health` | none | `200` | `HealthData` |
|
||||||
|
| `GET` | `/v1/stats/summary` | none | `200` | `SummaryData` |
|
||||||
|
| `GET` | `/v1/stats/zero/all` | none | `200` | `ZeroAllData` |
|
||||||
|
| `GET` | `/v1/stats/minimal/all` | none | `200` | `MinimalAllData` |
|
||||||
|
| `GET` | `/v1/stats/me-writers` | none | `200` | `MeWritersData` |
|
||||||
|
| `GET` | `/v1/stats/dcs` | none | `200` | `DcStatusData` |
|
||||||
|
| `GET` | `/v1/stats/users` | none | `200` | `UserInfo[]` |
|
||||||
|
| `GET` | `/v1/users` | none | `200` | `UserInfo[]` |
|
||||||
|
| `POST` | `/v1/users` | `CreateUserRequest` | `201` | `CreateUserResponse` |
|
||||||
|
| `GET` | `/v1/users/{username}` | none | `200` | `UserInfo` |
|
||||||
|
| `PATCH` | `/v1/users/{username}` | `PatchUserRequest` | `200` | `UserInfo` |
|
||||||
|
| `DELETE` | `/v1/users/{username}` | none | `200` | `string` (deleted username) |
|
||||||
|
| `POST` | `/v1/users/{username}/rotate-secret` | `RotateSecretRequest` or empty body | `200` | `CreateUserResponse` |
|
||||||
|
|
||||||
|
## Common Error Codes
|
||||||
|
|
||||||
|
| HTTP | `error.code` | Trigger |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `400` | `bad_request` | Invalid JSON, validation failures, malformed request body. |
|
||||||
|
| `401` | `unauthorized` | Missing/invalid `Authorization` when `auth_header` is configured. |
|
||||||
|
| `403` | `forbidden` | Source IP is not allowed by whitelist. |
|
||||||
|
| `403` | `read_only` | Mutating endpoint called while `read_only=true`. |
|
||||||
|
| `404` | `not_found` | Unknown route or unknown user. |
|
||||||
|
| `405` | `method_not_allowed` | Unsupported method for an existing user route. |
|
||||||
|
| `409` | `revision_conflict` | `If-Match` revision mismatch. |
|
||||||
|
| `409` | `user_exists` | User already exists on create. |
|
||||||
|
| `409` | `last_user_forbidden` | Attempt to delete last configured user. |
|
||||||
|
| `413` | `payload_too_large` | Body exceeds `request_body_limit_bytes`. |
|
||||||
|
| `500` | `internal_error` | Internal error (I/O, serialization, config load/save). |
|
||||||
|
| `503` | `api_disabled` | API disabled in config. |
|
||||||
|
|
||||||
|
## Request Contracts
|
||||||
|
|
||||||
|
### `CreateUserRequest`
|
||||||
|
| Field | Type | Required | Description |
|
||||||
|
| --- | --- | --- | --- |
|
||||||
|
| `username` | `string` | yes | `[A-Za-z0-9_.-]`, length `1..64`. |
|
||||||
|
| `secret` | `string` | no | Exactly 32 hex chars. If missing, generated automatically. |
|
||||||
|
| `user_ad_tag` | `string` | no | Exactly 32 hex chars. |
|
||||||
|
| `max_tcp_conns` | `usize` | no | Per-user concurrent TCP limit. |
|
||||||
|
| `expiration_rfc3339` | `string` | no | RFC3339 expiration timestamp. |
|
||||||
|
| `data_quota_bytes` | `u64` | no | Per-user traffic quota. |
|
||||||
|
| `max_unique_ips` | `usize` | no | Per-user unique source IP limit. |
|
||||||
|
|
||||||
|
### `PatchUserRequest`
|
||||||
|
| Field | Type | Required | Description |
|
||||||
|
| --- | --- | --- | --- |
|
||||||
|
| `secret` | `string` | no | Exactly 32 hex chars. |
|
||||||
|
| `user_ad_tag` | `string` | no | Exactly 32 hex chars. |
|
||||||
|
| `max_tcp_conns` | `usize` | no | Per-user concurrent TCP limit. |
|
||||||
|
| `expiration_rfc3339` | `string` | no | RFC3339 expiration timestamp. |
|
||||||
|
| `data_quota_bytes` | `u64` | no | Per-user traffic quota. |
|
||||||
|
| `max_unique_ips` | `usize` | no | Per-user unique source IP limit. |
|
||||||
|
|
||||||
|
### `RotateSecretRequest`
|
||||||
|
| Field | Type | Required | Description |
|
||||||
|
| --- | --- | --- | --- |
|
||||||
|
| `secret` | `string` | no | Exactly 32 hex chars. If missing, generated automatically. |
|
||||||
|
|
||||||
|
## Response Data Contracts
|
||||||
|
|
||||||
|
### `HealthData`
|
||||||
|
| Field | Type | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `status` | `string` | Always `"ok"`. |
|
||||||
|
| `read_only` | `bool` | Mirrors current API `read_only` mode. |
|
||||||
|
|
||||||
|
### `SummaryData`
|
||||||
|
| Field | Type | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `uptime_seconds` | `f64` | Process uptime in seconds. |
|
||||||
|
| `connections_total` | `u64` | Total accepted client connections. |
|
||||||
|
| `connections_bad_total` | `u64` | Failed/invalid client connections. |
|
||||||
|
| `handshake_timeouts_total` | `u64` | Handshake timeout count. |
|
||||||
|
| `configured_users` | `usize` | Number of configured users in config. |
|
||||||
|
|
||||||
|
### `ZeroAllData`
|
||||||
|
| Field | Type | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `generated_at_epoch_secs` | `u64` | Snapshot time (Unix epoch seconds). |
|
||||||
|
| `core` | `ZeroCoreData` | Core counters and telemetry policy snapshot. |
|
||||||
|
| `upstream` | `ZeroUpstreamData` | Upstream connect counters/histogram buckets. |
|
||||||
|
| `middle_proxy` | `ZeroMiddleProxyData` | ME protocol/health counters. |
|
||||||
|
| `pool` | `ZeroPoolData` | ME pool lifecycle counters. |
|
||||||
|
| `desync` | `ZeroDesyncData` | Frame desync counters. |
|
||||||
|
|
||||||
|
#### `ZeroCoreData`
|
||||||
|
| Field | Type | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `uptime_seconds` | `f64` | Process uptime. |
|
||||||
|
| `connections_total` | `u64` | Total accepted connections. |
|
||||||
|
| `connections_bad_total` | `u64` | Failed/invalid connections. |
|
||||||
|
| `handshake_timeouts_total` | `u64` | Handshake timeouts. |
|
||||||
|
| `configured_users` | `usize` | Configured user count. |
|
||||||
|
| `telemetry_core_enabled` | `bool` | Core telemetry toggle. |
|
||||||
|
| `telemetry_user_enabled` | `bool` | User telemetry toggle. |
|
||||||
|
| `telemetry_me_level` | `string` | ME telemetry level (`off|normal|verbose`). |
|
||||||
|
|
||||||
|
#### `ZeroUpstreamData`
|
||||||
|
| Field | Type | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `connect_attempt_total` | `u64` | Total upstream connect attempts. |
|
||||||
|
| `connect_success_total` | `u64` | Successful upstream connects. |
|
||||||
|
| `connect_fail_total` | `u64` | Failed upstream connects. |
|
||||||
|
| `connect_failfast_hard_error_total` | `u64` | Fail-fast hard errors. |
|
||||||
|
| `connect_attempts_bucket_1` | `u64` | Connect attempts resolved in 1 try. |
|
||||||
|
| `connect_attempts_bucket_2` | `u64` | Connect attempts resolved in 2 tries. |
|
||||||
|
| `connect_attempts_bucket_3_4` | `u64` | Connect attempts resolved in 3-4 tries. |
|
||||||
|
| `connect_attempts_bucket_gt_4` | `u64` | Connect attempts requiring more than 4 tries. |
|
||||||
|
| `connect_duration_success_bucket_le_100ms` | `u64` | Successful connects <=100 ms. |
|
||||||
|
| `connect_duration_success_bucket_101_500ms` | `u64` | Successful connects 101-500 ms. |
|
||||||
|
| `connect_duration_success_bucket_501_1000ms` | `u64` | Successful connects 501-1000 ms. |
|
||||||
|
| `connect_duration_success_bucket_gt_1000ms` | `u64` | Successful connects >1000 ms. |
|
||||||
|
| `connect_duration_fail_bucket_le_100ms` | `u64` | Failed connects <=100 ms. |
|
||||||
|
| `connect_duration_fail_bucket_101_500ms` | `u64` | Failed connects 101-500 ms. |
|
||||||
|
| `connect_duration_fail_bucket_501_1000ms` | `u64` | Failed connects 501-1000 ms. |
|
||||||
|
| `connect_duration_fail_bucket_gt_1000ms` | `u64` | Failed connects >1000 ms. |
|
||||||
|
|
||||||
|
#### `ZeroMiddleProxyData`
|
||||||
|
| Field | Type | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `keepalive_sent_total` | `u64` | ME keepalive packets sent. |
|
||||||
|
| `keepalive_failed_total` | `u64` | ME keepalive send failures. |
|
||||||
|
| `keepalive_pong_total` | `u64` | Keepalive pong responses received. |
|
||||||
|
| `keepalive_timeout_total` | `u64` | Keepalive timeout events. |
|
||||||
|
| `rpc_proxy_req_signal_sent_total` | `u64` | RPC proxy activity signals sent. |
|
||||||
|
| `rpc_proxy_req_signal_failed_total` | `u64` | RPC proxy activity signal failures. |
|
||||||
|
| `rpc_proxy_req_signal_skipped_no_meta_total` | `u64` | Signals skipped due to missing metadata. |
|
||||||
|
| `rpc_proxy_req_signal_response_total` | `u64` | RPC proxy signal responses received. |
|
||||||
|
| `rpc_proxy_req_signal_close_sent_total` | `u64` | RPC proxy close signals sent. |
|
||||||
|
| `reconnect_attempt_total` | `u64` | ME reconnect attempts. |
|
||||||
|
| `reconnect_success_total` | `u64` | Successful reconnects. |
|
||||||
|
| `handshake_reject_total` | `u64` | ME handshake rejects. |
|
||||||
|
| `handshake_error_codes` | `ZeroCodeCount[]` | Handshake rejects grouped by code. |
|
||||||
|
| `reader_eof_total` | `u64` | ME reader EOF events. |
|
||||||
|
| `idle_close_by_peer_total` | `u64` | Idle closes initiated by peer. |
|
||||||
|
| `route_drop_no_conn_total` | `u64` | Route drops due to missing bound connection. |
|
||||||
|
| `route_drop_channel_closed_total` | `u64` | Route drops due to closed channel. |
|
||||||
|
| `route_drop_queue_full_total` | `u64` | Route drops due to full queue (total). |
|
||||||
|
| `route_drop_queue_full_base_total` | `u64` | Route drops in base queue mode. |
|
||||||
|
| `route_drop_queue_full_high_total` | `u64` | Route drops in high queue mode. |
|
||||||
|
| `socks_kdf_strict_reject_total` | `u64` | SOCKS KDF strict rejects. |
|
||||||
|
| `socks_kdf_compat_fallback_total` | `u64` | SOCKS KDF compat fallbacks. |
|
||||||
|
| `endpoint_quarantine_total` | `u64` | Endpoint quarantine activations. |
|
||||||
|
| `kdf_drift_total` | `u64` | KDF drift detections. |
|
||||||
|
| `kdf_port_only_drift_total` | `u64` | KDF port-only drift detections. |
|
||||||
|
| `hardswap_pending_reuse_total` | `u64` | Pending hardswap reused events. |
|
||||||
|
| `hardswap_pending_ttl_expired_total` | `u64` | Pending hardswap TTL expiry events. |
|
||||||
|
| `single_endpoint_outage_enter_total` | `u64` | Entered single-endpoint outage mode. |
|
||||||
|
| `single_endpoint_outage_exit_total` | `u64` | Exited single-endpoint outage mode. |
|
||||||
|
| `single_endpoint_outage_reconnect_attempt_total` | `u64` | Reconnect attempts in outage mode. |
|
||||||
|
| `single_endpoint_outage_reconnect_success_total` | `u64` | Reconnect successes in outage mode. |
|
||||||
|
| `single_endpoint_quarantine_bypass_total` | `u64` | Quarantine bypasses in outage mode. |
|
||||||
|
| `single_endpoint_shadow_rotate_total` | `u64` | Shadow writer rotations. |
|
||||||
|
| `single_endpoint_shadow_rotate_skipped_quarantine_total` | `u64` | Shadow rotations skipped because of quarantine. |
|
||||||
|
| `floor_mode_switch_total` | `u64` | Total floor mode switches. |
|
||||||
|
| `floor_mode_switch_static_to_adaptive_total` | `u64` | Static -> adaptive switches. |
|
||||||
|
| `floor_mode_switch_adaptive_to_static_total` | `u64` | Adaptive -> static switches. |
|
||||||
|
|
||||||
|
#### `ZeroCodeCount`
|
||||||
|
| Field | Type | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `code` | `i32` | Handshake error code. |
|
||||||
|
| `total` | `u64` | Events with this code. |
|
||||||
|
|
||||||
|
#### `ZeroPoolData`
|
||||||
|
| Field | Type | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `pool_swap_total` | `u64` | Pool swap count. |
|
||||||
|
| `pool_drain_active` | `u64` | Current active draining pools. |
|
||||||
|
| `pool_force_close_total` | `u64` | Forced pool closes by timeout. |
|
||||||
|
| `pool_stale_pick_total` | `u64` | Stale writer picks for binding. |
|
||||||
|
| `writer_removed_total` | `u64` | Writer removals total. |
|
||||||
|
| `writer_removed_unexpected_total` | `u64` | Unexpected writer removals. |
|
||||||
|
| `refill_triggered_total` | `u64` | Refill triggers. |
|
||||||
|
| `refill_skipped_inflight_total` | `u64` | Refill skipped because refill already in-flight. |
|
||||||
|
| `refill_failed_total` | `u64` | Refill failures. |
|
||||||
|
| `writer_restored_same_endpoint_total` | `u64` | Restores on same endpoint. |
|
||||||
|
| `writer_restored_fallback_total` | `u64` | Restores on fallback endpoint. |
|
||||||
|
|
||||||
|
#### `ZeroDesyncData`
|
||||||
|
| Field | Type | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `secure_padding_invalid_total` | `u64` | Invalid secure padding events. |
|
||||||
|
| `desync_total` | `u64` | Desync events total. |
|
||||||
|
| `desync_full_logged_total` | `u64` | Fully logged desync events. |
|
||||||
|
| `desync_suppressed_total` | `u64` | Suppressed desync logs. |
|
||||||
|
| `desync_frames_bucket_0` | `u64` | Desync frames bucket 0. |
|
||||||
|
| `desync_frames_bucket_1_2` | `u64` | Desync frames bucket 1-2. |
|
||||||
|
| `desync_frames_bucket_3_10` | `u64` | Desync frames bucket 3-10. |
|
||||||
|
| `desync_frames_bucket_gt_10` | `u64` | Desync frames bucket >10. |
|
||||||
|
|
||||||
|
### `MinimalAllData`
|
||||||
|
| Field | Type | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `enabled` | `bool` | Whether minimal runtime snapshots are enabled by config. |
|
||||||
|
| `reason` | `string?` | `feature_disabled` or `source_unavailable` when applicable. |
|
||||||
|
| `generated_at_epoch_secs` | `u64` | Snapshot generation time. |
|
||||||
|
| `data` | `MinimalAllPayload?` | Null when disabled; fallback payload when source unavailable. |
|
||||||
|
|
||||||
|
#### `MinimalAllPayload`
|
||||||
|
| Field | Type | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `me_writers` | `MeWritersData` | ME writer status block. |
|
||||||
|
| `dcs` | `DcStatusData` | DC aggregate status block. |
|
||||||
|
| `me_runtime` | `MinimalMeRuntimeData?` | Runtime ME control snapshot. |
|
||||||
|
| `network_path` | `MinimalDcPathData[]` | Active IP path selection per DC. |
|
||||||
|
|
||||||
|
#### `MinimalMeRuntimeData`
|
||||||
|
| Field | Type | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `active_generation` | `u64` | Active pool generation. |
|
||||||
|
| `warm_generation` | `u64` | Warm pool generation. |
|
||||||
|
| `pending_hardswap_generation` | `u64` | Pending hardswap generation. |
|
||||||
|
| `pending_hardswap_age_secs` | `u64?` | Pending hardswap age in seconds. |
|
||||||
|
| `hardswap_enabled` | `bool` | Hardswap mode toggle. |
|
||||||
|
| `floor_mode` | `string` | Writer floor mode. |
|
||||||
|
| `adaptive_floor_idle_secs` | `u64` | Idle threshold for adaptive floor. |
|
||||||
|
| `adaptive_floor_min_writers_single_endpoint` | `u8` | Minimum writers for single-endpoint DC in adaptive mode. |
|
||||||
|
| `adaptive_floor_recover_grace_secs` | `u64` | Grace period for floor recovery. |
|
||||||
|
| `me_keepalive_enabled` | `bool` | ME keepalive toggle. |
|
||||||
|
| `me_keepalive_interval_secs` | `u64` | Keepalive period. |
|
||||||
|
| `me_keepalive_jitter_secs` | `u64` | Keepalive jitter. |
|
||||||
|
| `me_keepalive_payload_random` | `bool` | Randomized keepalive payload toggle. |
|
||||||
|
| `rpc_proxy_req_every_secs` | `u64` | Period for RPC proxy request signal. |
|
||||||
|
| `me_reconnect_max_concurrent_per_dc` | `u32` | Reconnect concurrency per DC. |
|
||||||
|
| `me_reconnect_backoff_base_ms` | `u64` | Base reconnect backoff. |
|
||||||
|
| `me_reconnect_backoff_cap_ms` | `u64` | Max reconnect backoff. |
|
||||||
|
| `me_reconnect_fast_retry_count` | `u32` | Fast retry attempts before normal backoff. |
|
||||||
|
| `me_pool_drain_ttl_secs` | `u64` | Pool drain TTL. |
|
||||||
|
| `me_pool_force_close_secs` | `u64` | Hard close timeout for draining writers. |
|
||||||
|
| `me_pool_min_fresh_ratio` | `f32` | Minimum fresh ratio before swap. |
|
||||||
|
| `me_bind_stale_mode` | `string` | Stale writer bind policy. |
|
||||||
|
| `me_bind_stale_ttl_secs` | `u64` | Stale writer TTL. |
|
||||||
|
| `me_single_endpoint_shadow_writers` | `u8` | Shadow writers for single-endpoint DCs. |
|
||||||
|
| `me_single_endpoint_outage_mode_enabled` | `bool` | Outage mode toggle for single-endpoint DCs. |
|
||||||
|
| `me_single_endpoint_outage_disable_quarantine` | `bool` | Quarantine behavior in outage mode. |
|
||||||
|
| `me_single_endpoint_outage_backoff_min_ms` | `u64` | Outage mode min reconnect backoff. |
|
||||||
|
| `me_single_endpoint_outage_backoff_max_ms` | `u64` | Outage mode max reconnect backoff. |
|
||||||
|
| `me_single_endpoint_shadow_rotate_every_secs` | `u64` | Shadow rotation interval. |
|
||||||
|
| `me_deterministic_writer_sort` | `bool` | Deterministic writer ordering toggle. |
|
||||||
|
| `me_socks_kdf_policy` | `string` | Current SOCKS KDF policy mode. |
|
||||||
|
| `quarantined_endpoints_total` | `usize` | Total quarantined endpoints. |
|
||||||
|
| `quarantined_endpoints` | `MinimalQuarantineData[]` | Quarantine details. |
|
||||||
|
|
||||||
|
#### `MinimalQuarantineData`
|
||||||
|
| Field | Type | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `endpoint` | `string` | Endpoint (`ip:port`). |
|
||||||
|
| `remaining_ms` | `u64` | Remaining quarantine duration. |
|
||||||
|
|
||||||
|
#### `MinimalDcPathData`
|
||||||
|
| Field | Type | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `dc` | `i16` | Telegram DC identifier. |
|
||||||
|
| `ip_preference` | `string?` | Runtime IP family preference. |
|
||||||
|
| `selected_addr_v4` | `string?` | Selected IPv4 endpoint for this DC. |
|
||||||
|
| `selected_addr_v6` | `string?` | Selected IPv6 endpoint for this DC. |
|
||||||
|
|
||||||
|
### `MeWritersData`
|
||||||
|
| Field | Type | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `middle_proxy_enabled` | `bool` | `false` when minimal runtime is disabled or source unavailable. |
|
||||||
|
| `reason` | `string?` | `feature_disabled` or `source_unavailable` when not fully available. |
|
||||||
|
| `generated_at_epoch_secs` | `u64` | Snapshot generation time. |
|
||||||
|
| `summary` | `MeWritersSummary` | Coverage/availability summary. |
|
||||||
|
| `writers` | `MeWriterStatus[]` | Per-writer statuses. |
|
||||||
|
|
||||||
|
#### `MeWritersSummary`
|
||||||
|
| Field | Type | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `configured_dc_groups` | `usize` | Number of configured DC groups. |
|
||||||
|
| `configured_endpoints` | `usize` | Total configured ME endpoints. |
|
||||||
|
| `available_endpoints` | `usize` | Endpoints currently available. |
|
||||||
|
| `available_pct` | `f64` | `available_endpoints / configured_endpoints * 100`. |
|
||||||
|
| `required_writers` | `usize` | Required writers based on current floor policy. |
|
||||||
|
| `alive_writers` | `usize` | Writers currently alive. |
|
||||||
|
| `coverage_pct` | `f64` | `alive_writers / required_writers * 100`. |
|
||||||
|
|
||||||
|
#### `MeWriterStatus`
|
||||||
|
| Field | Type | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `writer_id` | `u64` | Runtime writer identifier. |
|
||||||
|
| `dc` | `i16?` | DC id if mapped. |
|
||||||
|
| `endpoint` | `string` | Endpoint (`ip:port`). |
|
||||||
|
| `generation` | `u64` | Pool generation owning this writer. |
|
||||||
|
| `state` | `string` | Writer state (`warm`, `active`, `draining`). |
|
||||||
|
| `draining` | `bool` | Draining flag. |
|
||||||
|
| `degraded` | `bool` | Degraded flag. |
|
||||||
|
| `bound_clients` | `usize` | Number of currently bound clients. |
|
||||||
|
| `idle_for_secs` | `u64?` | Idle age in seconds if idle. |
|
||||||
|
| `rtt_ema_ms` | `f64?` | RTT exponential moving average. |
|
||||||
|
|
||||||
|
### `DcStatusData`
|
||||||
|
| Field | Type | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `middle_proxy_enabled` | `bool` | `false` when minimal runtime is disabled or source unavailable. |
|
||||||
|
| `reason` | `string?` | `feature_disabled` or `source_unavailable` when not fully available. |
|
||||||
|
| `generated_at_epoch_secs` | `u64` | Snapshot generation time. |
|
||||||
|
| `dcs` | `DcStatus[]` | Per-DC status rows. |
|
||||||
|
|
||||||
|
#### `DcStatus`
|
||||||
|
| Field | Type | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `dc` | `i16` | Telegram DC id. |
|
||||||
|
| `endpoints` | `string[]` | Endpoints in this DC (`ip:port`). |
|
||||||
|
| `available_endpoints` | `usize` | Endpoints currently available in this DC. |
|
||||||
|
| `available_pct` | `f64` | `available_endpoints / endpoints_total * 100`. |
|
||||||
|
| `required_writers` | `usize` | Required writer count for this DC. |
|
||||||
|
| `alive_writers` | `usize` | Alive writers in this DC. |
|
||||||
|
| `coverage_pct` | `f64` | `alive_writers / required_writers * 100`. |
|
||||||
|
| `rtt_ms` | `f64?` | Aggregated RTT for DC. |
|
||||||
|
| `load` | `usize` | Active client sessions bound to this DC. |
|
||||||
|
|
||||||
|
### `UserInfo`
|
||||||
|
| Field | Type | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `username` | `string` | Username. |
|
||||||
|
| `user_ad_tag` | `string?` | Optional ad tag (32 hex chars). |
|
||||||
|
| `max_tcp_conns` | `usize?` | Optional max concurrent TCP limit. |
|
||||||
|
| `expiration_rfc3339` | `string?` | Optional expiration timestamp. |
|
||||||
|
| `data_quota_bytes` | `u64?` | Optional data quota. |
|
||||||
|
| `max_unique_ips` | `usize?` | Optional unique IP limit. |
|
||||||
|
| `current_connections` | `u64` | Current live connections. |
|
||||||
|
| `active_unique_ips` | `usize` | Current active unique source IPs. |
|
||||||
|
| `total_octets` | `u64` | Total traffic octets for this user. |
|
||||||
|
| `links` | `UserLinks` | Active connection links derived from current config. |
|
||||||
|
|
||||||
|
#### `UserLinks`
|
||||||
|
| Field | Type | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `classic` | `string[]` | Active `tg://proxy` links for classic mode. |
|
||||||
|
| `secure` | `string[]` | Active `tg://proxy` links for secure/DD mode. |
|
||||||
|
| `tls` | `string[]` | Active `tg://proxy` links for EE-TLS mode (for each host+TLS domain). |
|
||||||
|
|
||||||
|
Link generation uses active config and enabled modes:
|
||||||
|
- `[general.links].public_host/public_port` have priority.
|
||||||
|
- Fallback host sources: listener `announce`, `announce_ip`, explicit listener `ip`.
|
||||||
|
- Legacy fallback: `listen_addr_ipv4` and `listen_addr_ipv6` when routable.
|
||||||
|
|
||||||
|
### `CreateUserResponse`
|
||||||
|
| Field | Type | Description |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `user` | `UserInfo` | Created or updated user view. |
|
||||||
|
| `secret` | `string` | Effective user secret. |
|
||||||
|
|
||||||
|
## Mutation Semantics
|
||||||
|
|
||||||
|
| Endpoint | Notes |
|
||||||
|
| --- | --- |
|
||||||
|
| `POST /v1/users` | Creates user and validates resulting config before atomic save. |
|
||||||
|
| `PATCH /v1/users/{username}` | Partial update of provided fields only. Missing fields remain unchanged. |
|
||||||
|
| `POST /v1/users/{username}/rotate-secret` | Replaces secret. Empty body is allowed and auto-generates secret. |
|
||||||
|
| `DELETE /v1/users/{username}` | Deletes user and related optional settings. Last user deletion is blocked. |
|
||||||
|
|
||||||
|
All mutating endpoints:
|
||||||
|
- Respect `read_only` mode.
|
||||||
|
- Accept optional `If-Match` for optimistic concurrency.
|
||||||
|
- Return new `revision` after successful write.
|
||||||
|
|
||||||
|
## Operational Notes
|
||||||
|
|
||||||
|
| Topic | Details |
|
||||||
|
| --- | --- |
|
||||||
|
| API startup | API binds only when `[server.api].enabled=true`. |
|
||||||
|
| Restart requirements | Changes in `server.api` settings require process restart. |
|
||||||
|
| Runtime apply path | Successful writes are picked up by existing config watcher/hot-reload path. |
|
||||||
|
| Exposure | Built-in TLS/mTLS is not provided. Use loopback bind + reverse proxy if needed. |
|
||||||
|
| Pagination | User list currently has no pagination/filtering. |
|
||||||
|
| Serialization side effect | Config comments/manual formatting are not preserved on write. |
|
||||||
65
docs/FAQ.ru.md
Normal file
65
docs/FAQ.ru.md
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
## Как настроить канал "спонсор прокси"
|
||||||
|
|
||||||
|
1. Зайти в бота @MTProxybot.
|
||||||
|
2. Ввести команду `/newproxy`
|
||||||
|
3. Отправить IP и порт сервера. Например: 1.2.3.4:443
|
||||||
|
4. Открыть конфиг `nano /etc/telemt.toml`.
|
||||||
|
5. Скопировать и отправить боту секрет пользователя из раздела [access.users].
|
||||||
|
6. Скопировать полученный tag у бота. Например 1234567890abcdef1234567890abcdef.
|
||||||
|
> [!WARNING]
|
||||||
|
> Ссылка, которую выдает бот, не будет работать. Не копируйте и не используйте её!
|
||||||
|
7. Раскомментировать параметр ad_tag и вписать tag, полученный у бота.
|
||||||
|
8. Раскомментировать/добавить параметр use_middle_proxy = true.
|
||||||
|
|
||||||
|
Пример конфига:
|
||||||
|
```toml
|
||||||
|
[general]
|
||||||
|
ad_tag = "1234567890abcdef1234567890abcdef"
|
||||||
|
use_middle_proxy = true
|
||||||
|
```
|
||||||
|
9. Сохранить конфиг. Ctrl+S -> Ctrl+X.
|
||||||
|
10. Перезапустить telemt `systemctl restart telemt`.
|
||||||
|
11. В боте отправить команду /myproxies и выбрать добавленный сервер.
|
||||||
|
12. Нажать кнопку "Set promotion".
|
||||||
|
13. Отправить **публичную ссылку** на канал. Приватный канал добавить нельзя!
|
||||||
|
14. Подождать примерно 1 час, пока информация обновится на серверах Telegram.
|
||||||
|
> [!WARNING]
|
||||||
|
> У вас не будет отображаться "спонсор прокси" если вы уже подписаны на канал.
|
||||||
|
|
||||||
|
## Сколько человек может пользоваться 1 ссылкой
|
||||||
|
|
||||||
|
По умолчанию 1 ссылкой может пользоваться сколько угодно человек.
|
||||||
|
Вы можете ограничить число IP, использующих прокси.
|
||||||
|
```toml
|
||||||
|
[access.user_max_unique_ips]
|
||||||
|
hello = 1
|
||||||
|
```
|
||||||
|
Этот параметр ограничивает, сколько уникальных IP может использовать 1 ссылку одновременно. Если один пользователь отключится, второй сможет подключиться. Также с одного IP может сидеть несколько пользователей.
|
||||||
|
|
||||||
|
## Как сделать несколько разных ссылок
|
||||||
|
|
||||||
|
1. Сгенерируйте нужное число секретов `openssl rand -hex 16`
|
||||||
|
2. Открыть конфиг `nano /etc/telemt.toml`
|
||||||
|
3. Добавить новых пользователей.
|
||||||
|
```toml
|
||||||
|
[access.users]
|
||||||
|
user1 = "00000000000000000000000000000001"
|
||||||
|
user2 = "00000000000000000000000000000002"
|
||||||
|
user3 = "00000000000000000000000000000003"
|
||||||
|
```
|
||||||
|
4. Сохранить конфиг. Ctrl+S -> Ctrl+X. Перезапускать telemt не нужно.
|
||||||
|
5. Получить ссылки через `journalctl -u telemt -n -g "links" --no-pager -o cat | tac`
|
||||||
|
|
||||||
|
## Как посмотреть метрики
|
||||||
|
|
||||||
|
1. Открыть конфиг `nano /etc/telemt.toml`
|
||||||
|
2. Добавить следующие параметры
|
||||||
|
```toml
|
||||||
|
[server]
|
||||||
|
metrics_port = 9090
|
||||||
|
metrics_whitelist = ["127.0.0.1/32", "::1/128", "0.0.0.0/0"]
|
||||||
|
```
|
||||||
|
3. Сохранить конфиг. Ctrl+S -> Ctrl+X.
|
||||||
|
4. Метрики доступны по адресу SERVER_IP:9090/metrics.
|
||||||
|
> [!WARNING]
|
||||||
|
> "0.0.0.0/0" в metrics_whitelist открывает доступ с любого IP. Замените на свой ip. Например "1.2.3.4"
|
||||||
40
docs/MIDDLE-END-KDF.de.md
Normal file
40
docs/MIDDLE-END-KDF.de.md
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
# Middle-End Proxy
|
||||||
|
|
||||||
|
## KDF-Adressierung — Implementierungs-FAQ
|
||||||
|
|
||||||
|
### Benötigt die C-Referenzimplementierung sowohl externe IP-Adresse als auch Port für die KDF?
|
||||||
|
|
||||||
|
Ja.
|
||||||
|
|
||||||
|
In der C-Referenzimplementierung werden **sowohl IP-Adresse als auch Port in die KDF einbezogen** — auf beiden Seiten der Verbindung.
|
||||||
|
|
||||||
|
In `aes_create_keys()` enthält der KDF-Input:
|
||||||
|
|
||||||
|
- `server_ip + client_port`
|
||||||
|
- `client_ip + server_port`
|
||||||
|
- sowie Secret / Nonces
|
||||||
|
|
||||||
|
Für IPv6:
|
||||||
|
|
||||||
|
- IPv4-Felder werden auf 0 gesetzt
|
||||||
|
- IPv6-Adressen werden ergänzt
|
||||||
|
|
||||||
|
Die **Ports bleiben weiterhin Bestandteil der KDF**.
|
||||||
|
|
||||||
|
> Wenn sich externe IP oder Port (z. B. durch NAT, SOCKS oder Proxy) von den erwarteten Werten unterscheiden, entstehen unterschiedliche Schlüssel — der Handshake schlägt fehl.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Kann der Port aus der KDF ausgeschlossen werden (z. B. durch Port = 0)?
|
||||||
|
|
||||||
|
**Nein!**
|
||||||
|
|
||||||
|
Die C-Referenzimplementierung enthält **keine Möglichkeit, den Port zu ignorieren**:
|
||||||
|
- `client_port` und `server_port` sind fester Bestandteil der KDF
|
||||||
|
- Es werden immer reale Socket-Ports übergeben:
|
||||||
|
- `c->our_port`
|
||||||
|
- `c->remote_port`
|
||||||
|
|
||||||
|
Falls ein Port den Wert `0` hat, wird er dennoch als `0` in die KDF übernommen.
|
||||||
|
|
||||||
|
Eine „Port-Ignore“-Logik existiert nicht.
|
||||||
41
docs/MIDDLE-END-KDF.en.md
Normal file
41
docs/MIDDLE-END-KDF.en.md
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
# Middle-End Proxy
|
||||||
|
|
||||||
|
## KDF Addressing — Implementation FAQ
|
||||||
|
|
||||||
|
### Does the C-implementation require both external IP address and port for the KDF?
|
||||||
|
|
||||||
|
**Yes!**
|
||||||
|
|
||||||
|
In the C reference implementation, **both IP address and port are included in the KDF input** from both sides of the connection.
|
||||||
|
|
||||||
|
Inside `aes_create_keys()`, the KDF input explicitly contains:
|
||||||
|
|
||||||
|
- `server_ip + client_port`
|
||||||
|
- `client_ip + server_port`
|
||||||
|
- followed by shared secret / nonces
|
||||||
|
|
||||||
|
For IPv6:
|
||||||
|
|
||||||
|
- IPv4 fields are zeroed
|
||||||
|
- IPv6 addresses are inserted
|
||||||
|
|
||||||
|
However, **client_port and server_port remain part of the KDF regardless of IP version**.
|
||||||
|
|
||||||
|
> If externally observed IP or port (e.g. due to NAT, SOCKS, or proxy traversal) differs from what the peer expects, the derived keys will not match and the handshake will fail.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Can port be excluded from KDF (e.g. by using port = 0)?
|
||||||
|
|
||||||
|
**No!**
|
||||||
|
|
||||||
|
The C-implementation provides **no mechanism to ignore the port**:
|
||||||
|
|
||||||
|
- `client_port` and `server_port` are explicitly included in the KDF input
|
||||||
|
- Real socket ports are always passed:
|
||||||
|
- `c->our_port`
|
||||||
|
- `c->remote_port`
|
||||||
|
|
||||||
|
If a port is `0`, it is still incorporated into the KDF as `0`.
|
||||||
|
|
||||||
|
There is **no conditional logic to exclude ports**
|
||||||
41
docs/MIDDLE-END-KDF.ru.md
Normal file
41
docs/MIDDLE-END-KDF.ru.md
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
# Middle-End Proxy
|
||||||
|
|
||||||
|
## KDF Addressing — FAQ по реализации
|
||||||
|
|
||||||
|
### Требует ли C-референсная реализация KDF внешний IP и порт?
|
||||||
|
|
||||||
|
**Да**
|
||||||
|
|
||||||
|
В C-референсе **в KDF участвуют и IP-адрес, и порт** — с обеих сторон соединения.
|
||||||
|
|
||||||
|
В `aes_create_keys()` в строку KDF входят:
|
||||||
|
|
||||||
|
- `server_ip + client_port`
|
||||||
|
- `client_ip + server_port`
|
||||||
|
- далее secret / nonces
|
||||||
|
|
||||||
|
Для IPv6:
|
||||||
|
|
||||||
|
- IPv4-поля заполняются нулями
|
||||||
|
- добавляются IPv6-адреса
|
||||||
|
|
||||||
|
Однако **порты client_port и server_port всё равно участвуют в KDF**.
|
||||||
|
|
||||||
|
> Если внешний IP или порт (например, из-за NAT, SOCKS или прокси) не совпадает с ожидаемым другой стороной — ключи расходятся и handshake ломается.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Можно ли исключить порт из KDF (например, установив порт = 0)?
|
||||||
|
|
||||||
|
**Нет.**
|
||||||
|
|
||||||
|
В C-референсе **нет механики отключения порта**.
|
||||||
|
|
||||||
|
- `client_port` и `server_port` явно включены в KDF
|
||||||
|
- Передаются реальные порты сокета:
|
||||||
|
- `c->our_port`
|
||||||
|
- `c->remote_port`
|
||||||
|
|
||||||
|
Если порт равен `0`, он всё равно попадёт в KDF как `0`.
|
||||||
|
|
||||||
|
Отдельной логики «игнорировать порт» не предусмотрено.
|
||||||
152
docs/QUICK_START_GUIDE.en.md
Normal file
152
docs/QUICK_START_GUIDE.en.md
Normal file
@@ -0,0 +1,152 @@
|
|||||||
|
# Telemt via Systemd
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
This software is designed for Debian-based OS: in addition to Debian, these are Ubuntu, Mint, Kali, MX and many other Linux
|
||||||
|
|
||||||
|
**1. Download**
|
||||||
|
```bash
|
||||||
|
wget -qO- "https://github.com/telemt/telemt/releases/latest/download/telemt-$(uname -m)-linux-$(ldd --version 2>&1 | grep -iq musl && echo musl || echo gnu).tar.gz" | tar -xz
|
||||||
|
```
|
||||||
|
**2. Move to the Bin folder**
|
||||||
|
```bash
|
||||||
|
mv telemt /bin
|
||||||
|
```
|
||||||
|
**3. Make the file executable**
|
||||||
|
```bash
|
||||||
|
chmod +x /bin/telemt
|
||||||
|
```
|
||||||
|
|
||||||
|
## How to use?
|
||||||
|
|
||||||
|
**This guide "assumes" that you:**
|
||||||
|
- logged in as root or executed `su -` / `sudo su`
|
||||||
|
- Already have the "telemt" executable file in the /bin folder. Read the **[Installation](#Installation)** section.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**0. Check port and generate secrets**
|
||||||
|
|
||||||
|
The port you have selected for use should be MISSING from the list, when:
|
||||||
|
```bash
|
||||||
|
netstat -lnp
|
||||||
|
```
|
||||||
|
|
||||||
|
Generate 16 bytes/32 characters HEX with OpenSSL or another way:
|
||||||
|
```bash
|
||||||
|
openssl rand -hex 16
|
||||||
|
```
|
||||||
|
OR
|
||||||
|
```bash
|
||||||
|
xxd -l 16 -p /dev/urandom
|
||||||
|
```
|
||||||
|
OR
|
||||||
|
```bash
|
||||||
|
python3 -c 'import os; print(os.urandom(16).hex())'
|
||||||
|
```
|
||||||
|
Save the obtained result somewhere. You will need it later!
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**1. Place your config to /etc/telemt.toml**
|
||||||
|
|
||||||
|
Open nano
|
||||||
|
```bash
|
||||||
|
nano /etc/telemt.toml
|
||||||
|
```
|
||||||
|
paste your config
|
||||||
|
|
||||||
|
```toml
|
||||||
|
# === General Settings ===
|
||||||
|
[general]
|
||||||
|
# ad_tag = "00000000000000000000000000000000"
|
||||||
|
|
||||||
|
[general.modes]
|
||||||
|
classic = false
|
||||||
|
secure = false
|
||||||
|
tls = true
|
||||||
|
|
||||||
|
# === Anti-Censorship & Masking ===
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "petrovich.ru"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
# format: "username" = "32_hex_chars_secret"
|
||||||
|
hello = "00000000000000000000000000000000"
|
||||||
|
```
|
||||||
|
then Ctrl+S -> Ctrl+X to save
|
||||||
|
|
||||||
|
> [!WARNING]
|
||||||
|
> Replace the value of the hello parameter with the value you obtained in step 0.
|
||||||
|
> Replace the value of the tls_domain parameter with another website.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**2. Create service on /etc/systemd/system/telemt.service**
|
||||||
|
|
||||||
|
Open nano
|
||||||
|
```bash
|
||||||
|
nano /etc/systemd/system/telemt.service
|
||||||
|
```
|
||||||
|
|
||||||
|
paste this Systemd Module
|
||||||
|
```bash
|
||||||
|
[Unit]
|
||||||
|
Description=Telemt
|
||||||
|
After=network.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=simple
|
||||||
|
WorkingDirectory=/bin
|
||||||
|
ExecStart=/bin/telemt /etc/telemt.toml
|
||||||
|
Restart=on-failure
|
||||||
|
LimitNOFILE=65536
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
|
```
|
||||||
|
then Ctrl+S -> Ctrl+X to save
|
||||||
|
|
||||||
|
|
||||||
|
**3.** To start it, enter the command `systemctl start telemt`
|
||||||
|
|
||||||
|
**4.** To get status information, enter `systemctl status telemt`
|
||||||
|
|
||||||
|
**5.** For automatic startup at system boot, enter `systemctl enable telemt`
|
||||||
|
|
||||||
|
**6.** To get the links, enter `journalctl -u telemt -n -g "links" --no-pager -o cat | tac`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
# Telemt via Docker Compose
|
||||||
|
|
||||||
|
**1. Edit `config.toml` in repo root (at least: port, users secrets, tls_domain)**
|
||||||
|
**2. Start container:**
|
||||||
|
```bash
|
||||||
|
docker compose up -d --build
|
||||||
|
```
|
||||||
|
**3. Check logs:**
|
||||||
|
```bash
|
||||||
|
docker compose logs -f telemt
|
||||||
|
```
|
||||||
|
**4. Stop:**
|
||||||
|
```bash
|
||||||
|
docker compose down
|
||||||
|
```
|
||||||
|
> [!NOTE]
|
||||||
|
> - `docker-compose.yml` maps `./config.toml` to `/app/config.toml` (read-only)
|
||||||
|
> - By default it publishes `443:443` and runs with dropped capabilities (only `NET_BIND_SERVICE` is added)
|
||||||
|
> - If you really need host networking (usually only for some IPv6 setups) uncomment `network_mode: host`
|
||||||
|
|
||||||
|
**Run without Compose**
|
||||||
|
```bash
|
||||||
|
docker build -t telemt:local .
|
||||||
|
docker run --name telemt --restart unless-stopped \
|
||||||
|
-p 443:443 \
|
||||||
|
-e RUST_LOG=info \
|
||||||
|
-v "$PWD/config.toml:/app/config.toml:ro" \
|
||||||
|
--read-only \
|
||||||
|
--cap-drop ALL --cap-add NET_BIND_SERVICE \
|
||||||
|
--ulimit nofile=65536:65536 \
|
||||||
|
telemt:local
|
||||||
|
```
|
||||||
154
docs/QUICK_START_GUIDE.ru.md
Normal file
154
docs/QUICK_START_GUIDE.ru.md
Normal file
@@ -0,0 +1,154 @@
|
|||||||
|
# Telemt через Systemd
|
||||||
|
|
||||||
|
## Установка
|
||||||
|
|
||||||
|
Это программное обеспечение разработано для ОС на базе Debian: помимо Debian, это Ubuntu, Mint, Kali, MX и многие другие Linux
|
||||||
|
|
||||||
|
**1. Скачать**
|
||||||
|
```bash
|
||||||
|
wget -qO- "https://github.com/telemt/telemt/releases/latest/download/telemt-$(uname -m)-linux-$(ldd --version 2>&1 | grep -iq musl && echo musl || echo gnu).tar.gz" | tar -xz
|
||||||
|
```
|
||||||
|
**2. Переместить в папку Bin**
|
||||||
|
```bash
|
||||||
|
mv telemt /bin
|
||||||
|
```
|
||||||
|
**3. Сделать файл исполняемым**
|
||||||
|
```bash
|
||||||
|
chmod +x /bin/telemt
|
||||||
|
```
|
||||||
|
|
||||||
|
## Как правильно использовать?
|
||||||
|
|
||||||
|
**Эта инструкция "предполагает", что вы:**
|
||||||
|
- Авторизовались как пользователь root или выполнил `su -` / `sudo su`
|
||||||
|
- У вас уже есть исполняемый файл "telemt" в папке /bin. Читайте раздел **[Установка](#установка)**
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**0. Проверьте порт и сгенерируйте секреты**
|
||||||
|
|
||||||
|
Порт, который вы выбрали для использования, должен отсутствовать в списке:
|
||||||
|
```bash
|
||||||
|
netstat -lnp
|
||||||
|
```
|
||||||
|
|
||||||
|
Сгенерируйте 16 bytes/32 символа в шестнадцатеричном формате с помощью OpenSSL или другим способом:
|
||||||
|
```bash
|
||||||
|
openssl rand -hex 16
|
||||||
|
```
|
||||||
|
ИЛИ
|
||||||
|
```bash
|
||||||
|
xxd -l 16 -p /dev/urandom
|
||||||
|
```
|
||||||
|
ИЛИ
|
||||||
|
```bash
|
||||||
|
python3 -c 'import os; print(os.urandom(16).hex())'
|
||||||
|
```
|
||||||
|
Полученный результат сохраняем где-нибудь. Он понадобиться вам дальше!
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**1. Поместите свою конфигурацию в файл /etc/telemt.toml**
|
||||||
|
|
||||||
|
Открываем nano
|
||||||
|
```bash
|
||||||
|
nano /etc/telemt.toml
|
||||||
|
```
|
||||||
|
Вставьте свою конфигурацию
|
||||||
|
|
||||||
|
```toml
|
||||||
|
# === General Settings ===
|
||||||
|
[general]
|
||||||
|
# ad_tag = "00000000000000000000000000000000"
|
||||||
|
|
||||||
|
[general.modes]
|
||||||
|
classic = false
|
||||||
|
secure = false
|
||||||
|
tls = true
|
||||||
|
|
||||||
|
# === Anti-Censorship & Masking ===
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "petrovich.ru"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
# format: "username" = "32_hex_chars_secret"
|
||||||
|
hello = "00000000000000000000000000000000"
|
||||||
|
```
|
||||||
|
Затем нажмите Ctrl+S -> Ctrl+X, чтобы сохранить
|
||||||
|
|
||||||
|
> [!WARNING]
|
||||||
|
> Замените значение параметра hello на значение, которое вы получили в пункте 0.
|
||||||
|
> Так же замените значение параметра tls_domain на другой сайт.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**2. Создайте службу в /etc/systemd/system/telemt.service**
|
||||||
|
|
||||||
|
Открываем nano
|
||||||
|
```bash
|
||||||
|
nano /etc/systemd/system/telemt.service
|
||||||
|
```
|
||||||
|
|
||||||
|
Вставьте этот модуль Systemd
|
||||||
|
```bash
|
||||||
|
[Unit]
|
||||||
|
Description=Telemt
|
||||||
|
After=network.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=simple
|
||||||
|
WorkingDirectory=/bin
|
||||||
|
ExecStart=/bin/telemt /etc/telemt.toml
|
||||||
|
Restart=on-failure
|
||||||
|
LimitNOFILE=65536
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
|
```
|
||||||
|
Затем нажмите Ctrl+S -> Ctrl+X, чтобы сохранить
|
||||||
|
|
||||||
|
|
||||||
|
**3.** Для запуска введите команду `systemctl start telemt`
|
||||||
|
|
||||||
|
**4.** Для получения информации о статусе введите `systemctl status telemt`
|
||||||
|
|
||||||
|
**5.** Для автоматического запуска при запуске системы в введите `systemctl enable telemt`
|
||||||
|
|
||||||
|
**6.** Для получения ссылки введите `journalctl -u telemt -n -g "links" --no-pager -o cat | tac`
|
||||||
|
> [!WARNING]
|
||||||
|
> Рабочую ссылку может выдать только команда из 6 пункта. Не пытайтесь делать ее самостоятельно или копировать откуда-либо!
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
# Telemt через Docker Compose
|
||||||
|
|
||||||
|
**1. Отредактируйте `config.toml` в корневом каталоге репозитория (как минимум: порт, пользовательские секреты, tls_domain)**
|
||||||
|
**2. Запустите контейнер:**
|
||||||
|
```bash
|
||||||
|
docker compose up -d --build
|
||||||
|
```
|
||||||
|
**3. Проверьте логи:**
|
||||||
|
```bash
|
||||||
|
docker compose logs -f telemt
|
||||||
|
```
|
||||||
|
**4. Остановите контейнер:**
|
||||||
|
```bash
|
||||||
|
docker compose down
|
||||||
|
```
|
||||||
|
> [!NOTE]
|
||||||
|
> - В `docker-compose.yml` файл `./config.toml` монтируется в `/app/config.toml` (доступно только для чтения)
|
||||||
|
> - По умолчанию публикуются порты 443:443, а контейнер запускается со сброшенными привилегиями (добавлена только `NET_BIND_SERVICE`)
|
||||||
|
> - Если вам действительно нужна сеть хоста (обычно это требуется только для некоторых конфигураций IPv6), раскомментируйте `network_mode: host`
|
||||||
|
|
||||||
|
**Запуск в Docker Compose**
|
||||||
|
```bash
|
||||||
|
docker build -t telemt:local .
|
||||||
|
docker run --name telemt --restart unless-stopped \
|
||||||
|
-p 443:443 \
|
||||||
|
-e RUST_LOG=info \
|
||||||
|
-v "$PWD/config.toml:/app/config.toml:ro" \
|
||||||
|
--read-only \
|
||||||
|
--cap-drop ALL --cap-add NET_BIND_SERVICE \
|
||||||
|
--ulimit nofile=65536:65536 \
|
||||||
|
telemt:local
|
||||||
|
```
|
||||||
219
docs/TUNING.de.md
Normal file
219
docs/TUNING.de.md
Normal file
@@ -0,0 +1,219 @@
|
|||||||
|
# Telemt Tuning-Leitfaden: Middle-End und Upstreams
|
||||||
|
|
||||||
|
Dieses Dokument beschreibt das aktuelle Laufzeitverhalten für Middle-End (ME) und Upstream-Routing basierend auf:
|
||||||
|
- `src/config/types.rs`
|
||||||
|
- `src/config/defaults.rs`
|
||||||
|
- `src/config/load.rs`
|
||||||
|
- `src/transport/upstream.rs`
|
||||||
|
|
||||||
|
Die unten angegebenen `Default`-Werte sind Code-Defaults (bei fehlendem Schlüssel), nicht zwingend die Werte aus `config.full.toml`.
|
||||||
|
|
||||||
|
## Middle-End-Parameter
|
||||||
|
|
||||||
|
### 1) ME-Grundmodus, NAT und STUN
|
||||||
|
|
||||||
|
| Parameter | Typ | Default | Einschränkungen / Validierung | Laufzeiteffekt | Beispiel |
|
||||||
|
|---|---|---:|---|---|---|
|
||||||
|
| `general.use_middle_proxy` | `bool` | `true` | keine | Aktiviert den ME-Transportmodus. Bei `false` wird Direct-Modus verwendet. | `use_middle_proxy = true` |
|
||||||
|
| `general.proxy_secret_path` | `Option<String>` | `"proxy-secret"` | Pfad kann `null` sein | Pfad zur Telegram-Infrastrukturdatei `proxy-secret`. | `proxy_secret_path = "proxy-secret"` |
|
||||||
|
| `general.middle_proxy_nat_ip` | `Option<IpAddr>` | `null` | gültige IP bei gesetztem Wert | Manueller Override der öffentlichen NAT-IP für ME-Adressmaterial. | `middle_proxy_nat_ip = "203.0.113.10"` |
|
||||||
|
| `general.middle_proxy_nat_probe` | `bool` | `true` | wird auf `true` erzwungen, wenn `use_middle_proxy=true` | Aktiviert NAT-Probing für ME. | `middle_proxy_nat_probe = true` |
|
||||||
|
| `general.stun_nat_probe_concurrency` | `usize` | `8` | muss `> 0` sein | Maximale parallele STUN-Probes während NAT-Erkennung. | `stun_nat_probe_concurrency = 16` |
|
||||||
|
| `network.stun_use` | `bool` | `true` | keine | Globaler STUN-Schalter. Bei `false` wird STUN deaktiviert. | `stun_use = true` |
|
||||||
|
| `network.stun_servers` | `Vec<String>` | integrierter öffentlicher Pool | Duplikate/leer werden entfernt | Primäre STUN-Serverliste für NAT/Public-Endpoint-Erkennung. | `stun_servers = ["stun1.l.google.com:19302"]` |
|
||||||
|
| `network.stun_tcp_fallback` | `bool` | `true` | keine | Aktiviert TCP-Fallback, wenn UDP-STUN blockiert ist. | `stun_tcp_fallback = true` |
|
||||||
|
| `network.http_ip_detect_urls` | `Vec<String>` | `ifconfig.me` + `api.ipify.org` | keine | HTTP-Fallback zur öffentlichen IPv4-Erkennung, falls STUN ausfällt. | `http_ip_detect_urls = ["https://api.ipify.org"]` |
|
||||||
|
| `general.stun_iface_mismatch_ignore` | `bool` | `false` | keine | Reserviertes Feld in der aktuellen Revision (derzeit kein aktiver Runtime-Verbrauch). | `stun_iface_mismatch_ignore = false` |
|
||||||
|
| `timeouts.me_one_retry` | `u8` | `12` | keine | Anzahl schneller Reconnect-Versuche bei Single-Endpoint-DC-Fällen. | `me_one_retry = 6` |
|
||||||
|
| `timeouts.me_one_timeout_ms` | `u64` | `1200` | keine | Timeout pro schnellem Einzelversuch (ms). | `me_one_timeout_ms = 1500` |
|
||||||
|
|
||||||
|
### 2) Poolgröße, Keepalive und Reconnect-Policy
|
||||||
|
|
||||||
|
| Parameter | Typ | Default | Einschränkungen / Validierung | Laufzeiteffekt | Beispiel |
|
||||||
|
|---|---|---:|---|---|---|
|
||||||
|
| `general.middle_proxy_pool_size` | `usize` | `8` | keine | Zielgröße des aktiven ME-Writer-Pools. | `middle_proxy_pool_size = 12` |
|
||||||
|
| `general.middle_proxy_warm_standby` | `usize` | `16` | keine | Reserviertes Kompatibilitätsfeld in der aktuellen Revision (kein aktiver Runtime-Consumer). | `middle_proxy_warm_standby = 16` |
|
||||||
|
| `general.me_keepalive_enabled` | `bool` | `true` | keine | Aktiviert periodischen ME-Keepalive/Ping-Traffic. | `me_keepalive_enabled = true` |
|
||||||
|
| `general.me_keepalive_interval_secs` | `u64` | `25` | keine | Basisintervall für Keepalive (Sekunden). | `me_keepalive_interval_secs = 20` |
|
||||||
|
| `general.me_keepalive_jitter_secs` | `u64` | `5` | keine | Keepalive-Jitter zur Vermeidung synchroner Peaks. | `me_keepalive_jitter_secs = 3` |
|
||||||
|
| `general.me_keepalive_payload_random` | `bool` | `true` | keine | Randomisiert Keepalive-Payload-Bytes. | `me_keepalive_payload_random = true` |
|
||||||
|
| `general.me_warmup_stagger_enabled` | `bool` | `true` | keine | Aktiviert gestaffeltes Warmup zusätzlicher ME-Verbindungen. | `me_warmup_stagger_enabled = true` |
|
||||||
|
| `general.me_warmup_step_delay_ms` | `u64` | `500` | keine | Basisverzögerung zwischen Warmup-Schritten (ms). | `me_warmup_step_delay_ms = 300` |
|
||||||
|
| `general.me_warmup_step_jitter_ms` | `u64` | `300` | keine | Zusätzlicher zufälliger Warmup-Jitter (ms). | `me_warmup_step_jitter_ms = 200` |
|
||||||
|
| `general.me_reconnect_max_concurrent_per_dc` | `u32` | `8` | keine | Begrenzung paralleler Reconnect-Worker pro DC. | `me_reconnect_max_concurrent_per_dc = 12` |
|
||||||
|
| `general.me_reconnect_backoff_base_ms` | `u64` | `500` | keine | Initiales Reconnect-Backoff (ms). | `me_reconnect_backoff_base_ms = 250` |
|
||||||
|
| `general.me_reconnect_backoff_cap_ms` | `u64` | `30000` | keine | Maximales Reconnect-Backoff (ms). | `me_reconnect_backoff_cap_ms = 10000` |
|
||||||
|
| `general.me_reconnect_fast_retry_count` | `u32` | `16` | keine | Budget für Sofort-Retries vor längerem Backoff. | `me_reconnect_fast_retry_count = 8` |
|
||||||
|
|
||||||
|
### 3) Reinit/Hardswap, Secret-Rotation und Degradation
|
||||||
|
|
||||||
|
| Parameter | Typ | Default | Einschränkungen / Validierung | Laufzeiteffekt | Beispiel |
|
||||||
|
|---|---|---:|---|---|---|
|
||||||
|
| `general.hardswap` | `bool` | `true` | keine | Aktiviert generation-basierte Hardswap-Strategie für den ME-Pool. | `hardswap = true` |
|
||||||
|
| `general.me_reinit_every_secs` | `u64` | `900` | muss `> 0` sein | Intervall für periodische ME-Reinitialisierung. | `me_reinit_every_secs = 600` |
|
||||||
|
| `general.me_hardswap_warmup_delay_min_ms` | `u64` | `1000` | muss `<= me_hardswap_warmup_delay_max_ms` sein | Untere Grenze für Warmup-Dial-Abstände. | `me_hardswap_warmup_delay_min_ms = 500` |
|
||||||
|
| `general.me_hardswap_warmup_delay_max_ms` | `u64` | `2000` | muss `> 0` sein | Obere Grenze für Warmup-Dial-Abstände. | `me_hardswap_warmup_delay_max_ms = 1200` |
|
||||||
|
| `general.me_hardswap_warmup_extra_passes` | `u8` | `3` | Bereich `[0,10]` | Zusätzliche Warmup-Pässe nach dem Basispass. | `me_hardswap_warmup_extra_passes = 2` |
|
||||||
|
| `general.me_hardswap_warmup_pass_backoff_base_ms` | `u64` | `500` | muss `> 0` sein | Basis-Backoff zwischen zusätzlichen Warmup-Pässen. | `me_hardswap_warmup_pass_backoff_base_ms = 400` |
|
||||||
|
| `general.me_config_stable_snapshots` | `u8` | `2` | muss `> 0` sein | Anzahl identischer ME-Config-Snapshots vor Apply. | `me_config_stable_snapshots = 3` |
|
||||||
|
| `general.me_config_apply_cooldown_secs` | `u64` | `300` | keine | Cooldown zwischen angewendeten ME-Map-Updates. | `me_config_apply_cooldown_secs = 120` |
|
||||||
|
| `general.proxy_secret_stable_snapshots` | `u8` | `2` | muss `> 0` sein | Anzahl identischer Secret-Snapshots vor Rotation. | `proxy_secret_stable_snapshots = 3` |
|
||||||
|
| `general.proxy_secret_rotate_runtime` | `bool` | `true` | keine | Aktiviert Runtime-Rotation des Proxy-Secrets. | `proxy_secret_rotate_runtime = true` |
|
||||||
|
| `general.proxy_secret_len_max` | `usize` | `256` | Bereich `[32,4096]` | Obergrenze für akzeptierte Secret-Länge. | `proxy_secret_len_max = 512` |
|
||||||
|
| `general.update_every` | `Option<u64>` | `300` | wenn gesetzt: `> 0`; bei `null`: Legacy-Min-Fallback | Einheitliches Refresh-Intervall für ME-Config + Secret-Updater. | `update_every = 300` |
|
||||||
|
| `general.me_pool_drain_ttl_secs` | `u64` | `90` | keine | Zeitraum, in dem stale Writer noch als Fallback zulässig sind. | `me_pool_drain_ttl_secs = 120` |
|
||||||
|
| `general.me_pool_min_fresh_ratio` | `f32` | `0.8` | Bereich `[0.0,1.0]` | Coverage-Schwelle vor Drain der alten Generation. | `me_pool_min_fresh_ratio = 0.9` |
|
||||||
|
| `general.me_reinit_drain_timeout_secs` | `u64` | `120` | `0` = kein Force-Close; wenn `>0 && < TTL`, dann auf TTL angehoben | Force-Close-Timeout für draining stale Writer. | `me_reinit_drain_timeout_secs = 0` |
|
||||||
|
| `general.auto_degradation_enabled` | `bool` | `true` | keine | Reserviertes Kompatibilitätsfeld in aktueller Revision (kein aktiver Runtime-Consumer). | `auto_degradation_enabled = true` |
|
||||||
|
| `general.degradation_min_unavailable_dc_groups` | `u8` | `2` | keine | Reservierter Kompatibilitäts-Schwellenwert in aktueller Revision (kein aktiver Runtime-Consumer). | `degradation_min_unavailable_dc_groups = 2` |
|
||||||
|
|
||||||
|
## Deprecated / Legacy Parameter
|
||||||
|
|
||||||
|
| Parameter | Status | Ersatz | Aktuelles Verhalten | Migrationshinweis |
|
||||||
|
|---|---|---|---|---|
|
||||||
|
| `general.middle_proxy_nat_stun` | Deprecated | `network.stun_servers` | Wird nur dann in `network.stun_servers` gemerged, wenn `network.stun_servers` nicht explizit gesetzt ist. | Wert nach `network.stun_servers` verschieben, Legacy-Key entfernen. |
|
||||||
|
| `general.middle_proxy_nat_stun_servers` | Deprecated | `network.stun_servers` | Wird nur dann in `network.stun_servers` gemerged, wenn `network.stun_servers` nicht explizit gesetzt ist. | Werte nach `network.stun_servers` verschieben, Legacy-Key entfernen. |
|
||||||
|
| `general.proxy_secret_auto_reload_secs` | Deprecated | `general.update_every` | Nur aktiv, wenn `update_every = null` (Legacy-Fallback). | `general.update_every` explizit setzen, Legacy-Key entfernen. |
|
||||||
|
| `general.proxy_config_auto_reload_secs` | Deprecated | `general.update_every` | Nur aktiv, wenn `update_every = null` (Legacy-Fallback). | `general.update_every` explizit setzen, Legacy-Key entfernen. |
|
||||||
|
|
||||||
|
## Wie Upstreams konfiguriert werden
|
||||||
|
|
||||||
|
### Upstream-Schema
|
||||||
|
|
||||||
|
| Feld | Gilt für | Typ | Pflicht | Default | Bedeutung |
|
||||||
|
|---|---|---|---|---|---|
|
||||||
|
| `[[upstreams]].type` | alle Upstreams | `"direct" \| "socks4" \| "socks5"` | ja | n/a | Upstream-Transporttyp. |
|
||||||
|
| `[[upstreams]].weight` | alle Upstreams | `u16` | nein | `1` | Basisgewicht für weighted-random Auswahl. |
|
||||||
|
| `[[upstreams]].enabled` | alle Upstreams | `bool` | nein | `true` | Deaktivierte Einträge werden beim Start ignoriert. |
|
||||||
|
| `[[upstreams]].scopes` | alle Upstreams | `String` | nein | `""` | Komma-separierte Scope-Tags für Request-Routing. |
|
||||||
|
| `interface` | `direct` | `Option<String>` | nein | `null` | Interface-Name (z. B. `eth0`) oder lokale Literal-IP. |
|
||||||
|
| `bind_addresses` | `direct` | `Option<Vec<IpAddr>>` | nein | `null` | Explizite Source-IP-Kandidaten (strikter Vorrang vor `interface`). |
|
||||||
|
| `address` | `socks4` | `String` | ja | n/a | SOCKS4-Server (`ip:port` oder `host:port`). |
|
||||||
|
| `interface` | `socks4` | `Option<String>` | nein | `null` | Wird nur genutzt, wenn `address` als `ip:port` angegeben ist. |
|
||||||
|
| `user_id` | `socks4` | `Option<String>` | nein | `null` | SOCKS4 User-ID für CONNECT. |
|
||||||
|
| `address` | `socks5` | `String` | ja | n/a | SOCKS5-Server (`ip:port` oder `host:port`). |
|
||||||
|
| `interface` | `socks5` | `Option<String>` | nein | `null` | Wird nur genutzt, wenn `address` als `ip:port` angegeben ist. |
|
||||||
|
| `username` | `socks5` | `Option<String>` | nein | `null` | SOCKS5 Benutzername. |
|
||||||
|
| `password` | `socks5` | `Option<String>` | nein | `null` | SOCKS5 Passwort. |
|
||||||
|
|
||||||
|
### Runtime-Regeln (wichtig)
|
||||||
|
|
||||||
|
1. Wenn `[[upstreams]]` fehlt, injiziert der Loader einen Default-`direct`-Upstream.
|
||||||
|
2. Scope-Filterung basiert auf exaktem Token-Match:
|
||||||
|
- mit Request-Scope -> nur Einträge, deren `scopes` genau dieses Token enthält;
|
||||||
|
- ohne Request-Scope -> nur Einträge mit leerem `scopes`.
|
||||||
|
3. Unter healthy Upstreams erfolgt die Auswahl per weighted random: `weight * latency_factor`.
|
||||||
|
4. Gibt es im gefilterten Set keinen healthy Upstream, wird zufällig aus dem gefilterten Set gewählt.
|
||||||
|
5. `direct`-Bind-Auflösung:
|
||||||
|
- zuerst `bind_addresses` (nur gleiche IP-Familie wie Target);
|
||||||
|
- bei `interface` (Name) + `bind_addresses` wird jede Candidate-IP gegen Interface-Adressen validiert;
|
||||||
|
- ungültige Kandidaten werden mit `WARN` verworfen;
|
||||||
|
- bleiben keine gültigen Kandidaten übrig, erfolgt unbound direct connect (`bind_ip=None`);
|
||||||
|
- wenn `bind_addresses` nicht passt, wird `interface` verwendet (Literal-IP oder Interface-Primäradresse).
|
||||||
|
6. Für `socks4/socks5` mit Hostname-`address` ist Interface-Binding nicht unterstützt und wird mit Warnung ignoriert.
|
||||||
|
7. Runtime DNS Overrides werden für Hostname-Auflösung bei Upstream-Verbindungen genutzt.
|
||||||
|
8. Im ME-Modus wird der gewählte Upstream auch für den ME-TCP-Dial-Pfad verwendet.
|
||||||
|
9. Im ME-Modus ist bei `direct` mit bind/interface die STUN-Reflection bind-aware für KDF-Adressmaterial.
|
||||||
|
10. Im ME-Modus werden bei SOCKS-Upstream `BND.ADDR/BND.PORT` für KDF verwendet, wenn gültig/öffentlich und gleiche IP-Familie.
|
||||||
|
|
||||||
|
## Upstream-Konfigurationsbeispiele
|
||||||
|
|
||||||
|
### Beispiel 1: Minimaler direct Upstream
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[[upstreams]]
|
||||||
|
type = "direct"
|
||||||
|
weight = 1
|
||||||
|
enabled = true
|
||||||
|
```
|
||||||
|
|
||||||
|
### Beispiel 2: direct mit Interface + expliziten bind IPs
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[[upstreams]]
|
||||||
|
type = "direct"
|
||||||
|
interface = "eth0"
|
||||||
|
bind_addresses = ["192.168.1.100", "192.168.1.101"]
|
||||||
|
weight = 3
|
||||||
|
enabled = true
|
||||||
|
```
|
||||||
|
|
||||||
|
### Beispiel 3: SOCKS5 Upstream mit Authentifizierung
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[[upstreams]]
|
||||||
|
type = "socks5"
|
||||||
|
address = "198.51.100.30:1080"
|
||||||
|
username = "proxy-user"
|
||||||
|
password = "proxy-pass"
|
||||||
|
weight = 2
|
||||||
|
enabled = true
|
||||||
|
```
|
||||||
|
|
||||||
|
### Beispiel 4: Gemischte Upstreams mit Scopes
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[[upstreams]]
|
||||||
|
type = "direct"
|
||||||
|
weight = 5
|
||||||
|
enabled = true
|
||||||
|
scopes = ""
|
||||||
|
|
||||||
|
[[upstreams]]
|
||||||
|
type = "socks5"
|
||||||
|
address = "203.0.113.40:1080"
|
||||||
|
username = "edge"
|
||||||
|
password = "edgepass"
|
||||||
|
weight = 3
|
||||||
|
enabled = true
|
||||||
|
scopes = "premium,me"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Beispiel 5: ME-orientiertes Tuning-Profil
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[general]
|
||||||
|
use_middle_proxy = true
|
||||||
|
proxy_secret_path = "proxy-secret"
|
||||||
|
middle_proxy_nat_probe = true
|
||||||
|
stun_nat_probe_concurrency = 16
|
||||||
|
middle_proxy_pool_size = 12
|
||||||
|
me_keepalive_enabled = true
|
||||||
|
me_keepalive_interval_secs = 20
|
||||||
|
me_keepalive_jitter_secs = 4
|
||||||
|
me_reconnect_max_concurrent_per_dc = 12
|
||||||
|
me_reconnect_backoff_base_ms = 300
|
||||||
|
me_reconnect_backoff_cap_ms = 10000
|
||||||
|
me_reconnect_fast_retry_count = 10
|
||||||
|
hardswap = true
|
||||||
|
me_reinit_every_secs = 600
|
||||||
|
me_hardswap_warmup_delay_min_ms = 500
|
||||||
|
me_hardswap_warmup_delay_max_ms = 1200
|
||||||
|
me_hardswap_warmup_extra_passes = 2
|
||||||
|
me_hardswap_warmup_pass_backoff_base_ms = 400
|
||||||
|
me_config_stable_snapshots = 3
|
||||||
|
me_config_apply_cooldown_secs = 120
|
||||||
|
proxy_secret_stable_snapshots = 3
|
||||||
|
proxy_secret_rotate_runtime = true
|
||||||
|
proxy_secret_len_max = 512
|
||||||
|
update_every = 300
|
||||||
|
me_pool_drain_ttl_secs = 120
|
||||||
|
me_pool_min_fresh_ratio = 0.9
|
||||||
|
me_reinit_drain_timeout_secs = 180
|
||||||
|
|
||||||
|
[timeouts]
|
||||||
|
me_one_retry = 8
|
||||||
|
me_one_timeout_ms = 1200
|
||||||
|
|
||||||
|
[network]
|
||||||
|
stun_use = true
|
||||||
|
stun_tcp_fallback = true
|
||||||
|
stun_servers = [
|
||||||
|
"stun1.l.google.com:19302",
|
||||||
|
"stun2.l.google.com:19302"
|
||||||
|
]
|
||||||
|
http_ip_detect_urls = [
|
||||||
|
"https://api.ipify.org",
|
||||||
|
"https://ifconfig.me/ip"
|
||||||
|
]
|
||||||
|
```
|
||||||
219
docs/TUNING.en.md
Normal file
219
docs/TUNING.en.md
Normal file
@@ -0,0 +1,219 @@
|
|||||||
|
# Telemt Tuning Guide: Middle-End and Upstreams
|
||||||
|
|
||||||
|
This document describes the current runtime behavior for Middle-End (ME) and upstream routing based on:
|
||||||
|
- `src/config/types.rs`
|
||||||
|
- `src/config/defaults.rs`
|
||||||
|
- `src/config/load.rs`
|
||||||
|
- `src/transport/upstream.rs`
|
||||||
|
|
||||||
|
Defaults below are code defaults (used when a key is omitted), not necessarily values from `config.full.toml` examples.
|
||||||
|
|
||||||
|
## Middle-End Parameters
|
||||||
|
|
||||||
|
### 1) Core ME mode, NAT, and STUN
|
||||||
|
|
||||||
|
| Parameter | Type | Default | Constraints / validation | Runtime effect | Example |
|
||||||
|
|---|---|---:|---|---|---|
|
||||||
|
| `general.use_middle_proxy` | `bool` | `true` | none | Enables ME transport mode. If `false`, Direct mode is used. | `use_middle_proxy = true` |
|
||||||
|
| `general.proxy_secret_path` | `Option<String>` | `"proxy-secret"` | path may be `null` | Path to Telegram infrastructure proxy-secret file. | `proxy_secret_path = "proxy-secret"` |
|
||||||
|
| `general.middle_proxy_nat_ip` | `Option<IpAddr>` | `null` | valid IP when set | Manual public NAT IP override for ME address material. | `middle_proxy_nat_ip = "203.0.113.10"` |
|
||||||
|
| `general.middle_proxy_nat_probe` | `bool` | `true` | auto-forced to `true` when `use_middle_proxy=true` | Enables ME NAT probing. | `middle_proxy_nat_probe = true` |
|
||||||
|
| `general.stun_nat_probe_concurrency` | `usize` | `8` | must be `> 0` | Max parallel STUN probes during NAT discovery. | `stun_nat_probe_concurrency = 16` |
|
||||||
|
| `network.stun_use` | `bool` | `true` | none | Global STUN switch. If `false`, STUN probing is disabled. | `stun_use = true` |
|
||||||
|
| `network.stun_servers` | `Vec<String>` | built-in public pool | deduplicated + empty values removed | Primary STUN server list for NAT/public endpoint discovery. | `stun_servers = ["stun1.l.google.com:19302"]` |
|
||||||
|
| `network.stun_tcp_fallback` | `bool` | `true` | none | Enables TCP fallback path when UDP STUN is blocked. | `stun_tcp_fallback = true` |
|
||||||
|
| `network.http_ip_detect_urls` | `Vec<String>` | `ifconfig.me` + `api.ipify.org` | none | HTTP fallback for public IPv4 detection if STUN is unavailable. | `http_ip_detect_urls = ["https://api.ipify.org"]` |
|
||||||
|
| `general.stun_iface_mismatch_ignore` | `bool` | `false` | none | Reserved flag in current revision (not consumed by runtime path). | `stun_iface_mismatch_ignore = false` |
|
||||||
|
| `timeouts.me_one_retry` | `u8` | `12` | none | Fast reconnect attempts for single-endpoint DC cases. | `me_one_retry = 6` |
|
||||||
|
| `timeouts.me_one_timeout_ms` | `u64` | `1200` | none | Timeout per quick single-endpoint attempt (ms). | `me_one_timeout_ms = 1500` |
|
||||||
|
|
||||||
|
### 2) Pool size, keepalive, and reconnect policy
|
||||||
|
|
||||||
|
| Parameter | Type | Default | Constraints / validation | Runtime effect | Example |
|
||||||
|
|---|---|---:|---|---|---|
|
||||||
|
| `general.middle_proxy_pool_size` | `usize` | `8` | none | Target active ME writer pool size. | `middle_proxy_pool_size = 12` |
|
||||||
|
| `general.middle_proxy_warm_standby` | `usize` | `16` | none | Reserved compatibility field in current revision (no active runtime consumer). | `middle_proxy_warm_standby = 16` |
|
||||||
|
| `general.me_keepalive_enabled` | `bool` | `true` | none | Enables periodic ME keepalive/ping traffic. | `me_keepalive_enabled = true` |
|
||||||
|
| `general.me_keepalive_interval_secs` | `u64` | `25` | none | Base keepalive interval (seconds). | `me_keepalive_interval_secs = 20` |
|
||||||
|
| `general.me_keepalive_jitter_secs` | `u64` | `5` | none | Keepalive jitter to avoid synchronization bursts. | `me_keepalive_jitter_secs = 3` |
|
||||||
|
| `general.me_keepalive_payload_random` | `bool` | `true` | none | Randomizes keepalive payload bytes. | `me_keepalive_payload_random = true` |
|
||||||
|
| `general.me_warmup_stagger_enabled` | `bool` | `true` | none | Staggers extra ME warmup dials to avoid spikes. | `me_warmup_stagger_enabled = true` |
|
||||||
|
| `general.me_warmup_step_delay_ms` | `u64` | `500` | none | Base delay between warmup dial steps (ms). | `me_warmup_step_delay_ms = 300` |
|
||||||
|
| `general.me_warmup_step_jitter_ms` | `u64` | `300` | none | Additional random delay for warmup steps (ms). | `me_warmup_step_jitter_ms = 200` |
|
||||||
|
| `general.me_reconnect_max_concurrent_per_dc` | `u32` | `8` | none | Limits concurrent reconnect workers per DC in health recovery. | `me_reconnect_max_concurrent_per_dc = 12` |
|
||||||
|
| `general.me_reconnect_backoff_base_ms` | `u64` | `500` | none | Initial reconnect backoff (ms). | `me_reconnect_backoff_base_ms = 250` |
|
||||||
|
| `general.me_reconnect_backoff_cap_ms` | `u64` | `30000` | none | Maximum reconnect backoff (ms). | `me_reconnect_backoff_cap_ms = 10000` |
|
||||||
|
| `general.me_reconnect_fast_retry_count` | `u32` | `16` | none | Immediate retry budget before long backoff behavior. | `me_reconnect_fast_retry_count = 8` |
|
||||||
|
|
||||||
|
### 3) Reinit/hardswap, secret rotation, and degradation
|
||||||
|
|
||||||
|
| Parameter | Type | Default | Constraints / validation | Runtime effect | Example |
|
||||||
|
|---|---|---:|---|---|---|
|
||||||
|
| `general.hardswap` | `bool` | `true` | none | Enables generation-based ME hardswap strategy. | `hardswap = true` |
|
||||||
|
| `general.me_reinit_every_secs` | `u64` | `900` | must be `> 0` | Periodic ME reinit interval. | `me_reinit_every_secs = 600` |
|
||||||
|
| `general.me_hardswap_warmup_delay_min_ms` | `u64` | `1000` | must be `<= me_hardswap_warmup_delay_max_ms` | Lower bound for hardswap warmup dial spacing. | `me_hardswap_warmup_delay_min_ms = 500` |
|
||||||
|
| `general.me_hardswap_warmup_delay_max_ms` | `u64` | `2000` | must be `> 0` | Upper bound for hardswap warmup dial spacing. | `me_hardswap_warmup_delay_max_ms = 1200` |
|
||||||
|
| `general.me_hardswap_warmup_extra_passes` | `u8` | `3` | must be within `[0,10]` | Additional warmup passes after base pass. | `me_hardswap_warmup_extra_passes = 2` |
|
||||||
|
| `general.me_hardswap_warmup_pass_backoff_base_ms` | `u64` | `500` | must be `> 0` | Base backoff between extra warmup passes. | `me_hardswap_warmup_pass_backoff_base_ms = 400` |
|
||||||
|
| `general.me_config_stable_snapshots` | `u8` | `2` | must be `> 0` | Number of identical ME config snapshots required before apply. | `me_config_stable_snapshots = 3` |
|
||||||
|
| `general.me_config_apply_cooldown_secs` | `u64` | `300` | none | Cooldown between applied ME map updates. | `me_config_apply_cooldown_secs = 120` |
|
||||||
|
| `general.proxy_secret_stable_snapshots` | `u8` | `2` | must be `> 0` | Number of identical proxy-secret snapshots required before rotation. | `proxy_secret_stable_snapshots = 3` |
|
||||||
|
| `general.proxy_secret_rotate_runtime` | `bool` | `true` | none | Enables runtime proxy-secret rotation. | `proxy_secret_rotate_runtime = true` |
|
||||||
|
| `general.proxy_secret_len_max` | `usize` | `256` | must be within `[32,4096]` | Upper limit for accepted proxy-secret length. | `proxy_secret_len_max = 512` |
|
||||||
|
| `general.update_every` | `Option<u64>` | `300` | if set: must be `> 0`; if `null`: legacy min fallback | Unified refresh interval for ME config + secret updater. | `update_every = 300` |
|
||||||
|
| `general.me_pool_drain_ttl_secs` | `u64` | `90` | none | Time window where stale writers remain fallback-eligible. | `me_pool_drain_ttl_secs = 120` |
|
||||||
|
| `general.me_pool_min_fresh_ratio` | `f32` | `0.8` | must be within `[0.0,1.0]` | Coverage threshold before stale generation can be drained. | `me_pool_min_fresh_ratio = 0.9` |
|
||||||
|
| `general.me_reinit_drain_timeout_secs` | `u64` | `120` | `0` means no force-close; if `>0 && < TTL` it is bumped to TTL | Force-close timeout for draining stale writers. | `me_reinit_drain_timeout_secs = 0` |
|
||||||
|
| `general.auto_degradation_enabled` | `bool` | `true` | none | Reserved compatibility flag in current revision (no active runtime consumer). | `auto_degradation_enabled = true` |
|
||||||
|
| `general.degradation_min_unavailable_dc_groups` | `u8` | `2` | none | Reserved compatibility threshold in current revision (no active runtime consumer). | `degradation_min_unavailable_dc_groups = 2` |
|
||||||
|
|
||||||
|
## Deprecated / Legacy Parameters
|
||||||
|
|
||||||
|
| Parameter | Status | Replacement | Current behavior | Migration recommendation |
|
||||||
|
|---|---|---|---|---|
|
||||||
|
| `general.middle_proxy_nat_stun` | Deprecated | `network.stun_servers` | Merged into `network.stun_servers` only when `network.stun_servers` is not explicitly set. | Move value into `network.stun_servers` and remove legacy key. |
|
||||||
|
| `general.middle_proxy_nat_stun_servers` | Deprecated | `network.stun_servers` | Merged into `network.stun_servers` only when `network.stun_servers` is not explicitly set. | Move values into `network.stun_servers` and remove legacy key. |
|
||||||
|
| `general.proxy_secret_auto_reload_secs` | Deprecated | `general.update_every` | Used only when `update_every = null` (legacy fallback path). | Set `general.update_every` explicitly and remove legacy key. |
|
||||||
|
| `general.proxy_config_auto_reload_secs` | Deprecated | `general.update_every` | Used only when `update_every = null` (legacy fallback path). | Set `general.update_every` explicitly and remove legacy key. |
|
||||||
|
|
||||||
|
## How Upstreams Are Configured
|
||||||
|
|
||||||
|
### Upstream schema
|
||||||
|
|
||||||
|
| Field | Applies to | Type | Required | Default | Meaning |
|
||||||
|
|---|---|---|---|---|---|
|
||||||
|
| `[[upstreams]].type` | all upstreams | `"direct" \| "socks4" \| "socks5"` | yes | n/a | Upstream transport type. |
|
||||||
|
| `[[upstreams]].weight` | all upstreams | `u16` | no | `1` | Base weight for weighted-random selection. |
|
||||||
|
| `[[upstreams]].enabled` | all upstreams | `bool` | no | `true` | Disabled entries are ignored at startup. |
|
||||||
|
| `[[upstreams]].scopes` | all upstreams | `String` | no | `""` | Comma-separated scope tags for request-level routing. |
|
||||||
|
| `interface` | `direct` | `Option<String>` | no | `null` | Interface name (e.g. `eth0`) or literal local IP for bind selection. |
|
||||||
|
| `bind_addresses` | `direct` | `Option<Vec<IpAddr>>` | no | `null` | Explicit local source IP candidates (strict priority over `interface`). |
|
||||||
|
| `address` | `socks4` | `String` | yes | n/a | SOCKS4 server endpoint (`ip:port` or `host:port`). |
|
||||||
|
| `interface` | `socks4` | `Option<String>` | no | `null` | Used only for SOCKS server `ip:port` dial path. |
|
||||||
|
| `user_id` | `socks4` | `Option<String>` | no | `null` | SOCKS4 user ID for CONNECT request. |
|
||||||
|
| `address` | `socks5` | `String` | yes | n/a | SOCKS5 server endpoint (`ip:port` or `host:port`). |
|
||||||
|
| `interface` | `socks5` | `Option<String>` | no | `null` | Used only for SOCKS server `ip:port` dial path. |
|
||||||
|
| `username` | `socks5` | `Option<String>` | no | `null` | SOCKS5 username auth. |
|
||||||
|
| `password` | `socks5` | `Option<String>` | no | `null` | SOCKS5 password auth. |
|
||||||
|
|
||||||
|
### Runtime rules (important)
|
||||||
|
|
||||||
|
1. If `[[upstreams]]` is omitted, loader injects one default `direct` upstream.
|
||||||
|
2. Scope filtering is exact-token based:
|
||||||
|
- when request scope is set -> only entries whose `scopes` contains that exact token;
|
||||||
|
- when request scope is not set -> only entries with empty `scopes`.
|
||||||
|
3. Healthy upstreams are selected by weighted random using: `weight * latency_factor`.
|
||||||
|
4. If no healthy upstream exists in filtered set, random selection is used among filtered entries.
|
||||||
|
5. `direct` bind resolution order:
|
||||||
|
- `bind_addresses` candidates (same IP family as target) first;
|
||||||
|
- if `interface` is an interface name and `bind_addresses` is set, each candidate IP is validated against addresses currently assigned to that interface;
|
||||||
|
- invalid candidates are dropped with `WARN`;
|
||||||
|
- if no valid candidate remains, connection falls back to unbound direct connect (`bind_ip=None`);
|
||||||
|
- if no `bind_addresses` candidate, `interface` is used (literal IP or resolved interface primary IP).
|
||||||
|
6. For `socks4/socks5` with `address` as hostname, interface binding is not supported and is ignored with warning.
|
||||||
|
7. Runtime DNS overrides are used for upstream hostname resolution.
|
||||||
|
8. In ME mode, the selected upstream is also used for ME TCP dial path.
|
||||||
|
9. In ME mode for `direct` upstream with bind/interface, STUN reflection logic is bind-aware for KDF source material.
|
||||||
|
10. In ME mode for SOCKS upstream, SOCKS `BND.ADDR/BND.PORT` is used for KDF when it is valid/public for the same family.
|
||||||
|
|
||||||
|
## Upstream Configuration Examples
|
||||||
|
|
||||||
|
### Example 1: Minimal direct upstream
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[[upstreams]]
|
||||||
|
type = "direct"
|
||||||
|
weight = 1
|
||||||
|
enabled = true
|
||||||
|
```
|
||||||
|
|
||||||
|
### Example 2: Direct with interface + explicit bind addresses
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[[upstreams]]
|
||||||
|
type = "direct"
|
||||||
|
interface = "eth0"
|
||||||
|
bind_addresses = ["192.168.1.100", "192.168.1.101"]
|
||||||
|
weight = 3
|
||||||
|
enabled = true
|
||||||
|
```
|
||||||
|
|
||||||
|
### Example 3: SOCKS5 upstream with authentication
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[[upstreams]]
|
||||||
|
type = "socks5"
|
||||||
|
address = "198.51.100.30:1080"
|
||||||
|
username = "proxy-user"
|
||||||
|
password = "proxy-pass"
|
||||||
|
weight = 2
|
||||||
|
enabled = true
|
||||||
|
```
|
||||||
|
|
||||||
|
### Example 4: Mixed upstreams with scopes
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[[upstreams]]
|
||||||
|
type = "direct"
|
||||||
|
weight = 5
|
||||||
|
enabled = true
|
||||||
|
scopes = ""
|
||||||
|
|
||||||
|
[[upstreams]]
|
||||||
|
type = "socks5"
|
||||||
|
address = "203.0.113.40:1080"
|
||||||
|
username = "edge"
|
||||||
|
password = "edgepass"
|
||||||
|
weight = 3
|
||||||
|
enabled = true
|
||||||
|
scopes = "premium,me"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Example 5: ME-focused tuning profile
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[general]
|
||||||
|
use_middle_proxy = true
|
||||||
|
proxy_secret_path = "proxy-secret"
|
||||||
|
middle_proxy_nat_probe = true
|
||||||
|
stun_nat_probe_concurrency = 16
|
||||||
|
middle_proxy_pool_size = 12
|
||||||
|
me_keepalive_enabled = true
|
||||||
|
me_keepalive_interval_secs = 20
|
||||||
|
me_keepalive_jitter_secs = 4
|
||||||
|
me_reconnect_max_concurrent_per_dc = 12
|
||||||
|
me_reconnect_backoff_base_ms = 300
|
||||||
|
me_reconnect_backoff_cap_ms = 10000
|
||||||
|
me_reconnect_fast_retry_count = 10
|
||||||
|
hardswap = true
|
||||||
|
me_reinit_every_secs = 600
|
||||||
|
me_hardswap_warmup_delay_min_ms = 500
|
||||||
|
me_hardswap_warmup_delay_max_ms = 1200
|
||||||
|
me_hardswap_warmup_extra_passes = 2
|
||||||
|
me_hardswap_warmup_pass_backoff_base_ms = 400
|
||||||
|
me_config_stable_snapshots = 3
|
||||||
|
me_config_apply_cooldown_secs = 120
|
||||||
|
proxy_secret_stable_snapshots = 3
|
||||||
|
proxy_secret_rotate_runtime = true
|
||||||
|
proxy_secret_len_max = 512
|
||||||
|
update_every = 300
|
||||||
|
me_pool_drain_ttl_secs = 120
|
||||||
|
me_pool_min_fresh_ratio = 0.9
|
||||||
|
me_reinit_drain_timeout_secs = 180
|
||||||
|
|
||||||
|
[timeouts]
|
||||||
|
me_one_retry = 8
|
||||||
|
me_one_timeout_ms = 1200
|
||||||
|
|
||||||
|
[network]
|
||||||
|
stun_use = true
|
||||||
|
stun_tcp_fallback = true
|
||||||
|
stun_servers = [
|
||||||
|
"stun1.l.google.com:19302",
|
||||||
|
"stun2.l.google.com:19302"
|
||||||
|
]
|
||||||
|
http_ip_detect_urls = [
|
||||||
|
"https://api.ipify.org",
|
||||||
|
"https://ifconfig.me/ip"
|
||||||
|
]
|
||||||
|
```
|
||||||
219
docs/TUNING.ru.md
Normal file
219
docs/TUNING.ru.md
Normal file
@@ -0,0 +1,219 @@
|
|||||||
|
# Руководство по тюнингу Telemt: Middle-End и Upstreams
|
||||||
|
|
||||||
|
Документ описывает актуальное поведение Middle-End (ME) и маршрутизации через upstream на основе:
|
||||||
|
- `src/config/types.rs`
|
||||||
|
- `src/config/defaults.rs`
|
||||||
|
- `src/config/load.rs`
|
||||||
|
- `src/transport/upstream.rs`
|
||||||
|
|
||||||
|
Значения `Default` ниже — это значения из кода при отсутствии ключа в конфиге, а не обязательно значения из примеров `config.full.toml`.
|
||||||
|
|
||||||
|
## Параметры Middle-End
|
||||||
|
|
||||||
|
### 1) Базовый режим ME, NAT и STUN
|
||||||
|
|
||||||
|
| Параметр | Тип | Default | Ограничения / валидация | Влияние на runtime | Пример |
|
||||||
|
|---|---|---:|---|---|---|
|
||||||
|
| `general.use_middle_proxy` | `bool` | `true` | нет | Включает транспорт ME. При `false` используется Direct-режим. | `use_middle_proxy = true` |
|
||||||
|
| `general.proxy_secret_path` | `Option<String>` | `"proxy-secret"` | путь может быть `null` | Путь к инфраструктурному proxy-secret Telegram. | `proxy_secret_path = "proxy-secret"` |
|
||||||
|
| `general.middle_proxy_nat_ip` | `Option<IpAddr>` | `null` | валидный IP при задании | Ручной override публичного NAT IP для адресного материала ME. | `middle_proxy_nat_ip = "203.0.113.10"` |
|
||||||
|
| `general.middle_proxy_nat_probe` | `bool` | `true` | авто-принудительно `true`, если `use_middle_proxy=true` | Включает NAT probing для ME. | `middle_proxy_nat_probe = true` |
|
||||||
|
| `general.stun_nat_probe_concurrency` | `usize` | `8` | должно быть `> 0` | Максимум параллельных STUN-проб при NAT-детекте. | `stun_nat_probe_concurrency = 16` |
|
||||||
|
| `network.stun_use` | `bool` | `true` | нет | Глобальный переключатель STUN. При `false` STUN отключен. | `stun_use = true` |
|
||||||
|
| `network.stun_servers` | `Vec<String>` | встроенный публичный пул | удаляются дубликаты и пустые значения | Основной список STUN-серверов для NAT/public endpoint discovery. | `stun_servers = ["stun1.l.google.com:19302"]` |
|
||||||
|
| `network.stun_tcp_fallback` | `bool` | `true` | нет | Включает TCP fallback, если UDP STUN недоступен. | `stun_tcp_fallback = true` |
|
||||||
|
| `network.http_ip_detect_urls` | `Vec<String>` | `ifconfig.me` + `api.ipify.org` | нет | HTTP fallback для определения публичного IPv4 при недоступности STUN. | `http_ip_detect_urls = ["https://api.ipify.org"]` |
|
||||||
|
| `general.stun_iface_mismatch_ignore` | `bool` | `false` | нет | Зарезервированный флаг в текущей ревизии (runtime его не использует). | `stun_iface_mismatch_ignore = false` |
|
||||||
|
| `timeouts.me_one_retry` | `u8` | `12` | нет | Количество быстрых reconnect-попыток для DC с одним endpoint. | `me_one_retry = 6` |
|
||||||
|
| `timeouts.me_one_timeout_ms` | `u64` | `1200` | нет | Таймаут одной быстрой попытки (мс). | `me_one_timeout_ms = 1500` |
|
||||||
|
|
||||||
|
### 2) Размер пула, keepalive и reconnect-политика
|
||||||
|
|
||||||
|
| Параметр | Тип | Default | Ограничения / валидация | Влияние на runtime | Пример |
|
||||||
|
|---|---|---:|---|---|---|
|
||||||
|
| `general.middle_proxy_pool_size` | `usize` | `8` | нет | Целевой размер активного пула ME-writer соединений. | `middle_proxy_pool_size = 12` |
|
||||||
|
| `general.middle_proxy_warm_standby` | `usize` | `16` | нет | Зарезервированное поле совместимости в текущей ревизии (активного runtime-consumer нет). | `middle_proxy_warm_standby = 16` |
|
||||||
|
| `general.me_keepalive_enabled` | `bool` | `true` | нет | Включает периодические keepalive/ping кадры ME. | `me_keepalive_enabled = true` |
|
||||||
|
| `general.me_keepalive_interval_secs` | `u64` | `25` | нет | Базовый интервал keepalive (сек). | `me_keepalive_interval_secs = 20` |
|
||||||
|
| `general.me_keepalive_jitter_secs` | `u64` | `5` | нет | Джиттер keepalive для предотвращения синхронных всплесков. | `me_keepalive_jitter_secs = 3` |
|
||||||
|
| `general.me_keepalive_payload_random` | `bool` | `true` | нет | Рандомизирует payload keepalive-кадров. | `me_keepalive_payload_random = true` |
|
||||||
|
| `general.me_warmup_stagger_enabled` | `bool` | `true` | нет | Включает staggered warmup дополнительных ME-коннектов. | `me_warmup_stagger_enabled = true` |
|
||||||
|
| `general.me_warmup_step_delay_ms` | `u64` | `500` | нет | Базовая задержка между шагами warmup (мс). | `me_warmup_step_delay_ms = 300` |
|
||||||
|
| `general.me_warmup_step_jitter_ms` | `u64` | `300` | нет | Дополнительный случайный warmup-джиттер (мс). | `me_warmup_step_jitter_ms = 200` |
|
||||||
|
| `general.me_reconnect_max_concurrent_per_dc` | `u32` | `8` | нет | Ограничивает параллельные reconnect worker'ы на один DC. | `me_reconnect_max_concurrent_per_dc = 12` |
|
||||||
|
| `general.me_reconnect_backoff_base_ms` | `u64` | `500` | нет | Начальный backoff reconnect (мс). | `me_reconnect_backoff_base_ms = 250` |
|
||||||
|
| `general.me_reconnect_backoff_cap_ms` | `u64` | `30000` | нет | Верхняя граница backoff reconnect (мс). | `me_reconnect_backoff_cap_ms = 10000` |
|
||||||
|
| `general.me_reconnect_fast_retry_count` | `u32` | `16` | нет | Бюджет быстрых retry до длинного backoff. | `me_reconnect_fast_retry_count = 8` |
|
||||||
|
|
||||||
|
### 3) Reinit/hardswap, ротация секрета и деградация
|
||||||
|
|
||||||
|
| Параметр | Тип | Default | Ограничения / валидация | Влияние на runtime | Пример |
|
||||||
|
|---|---|---:|---|---|---|
|
||||||
|
| `general.hardswap` | `bool` | `true` | нет | Включает generation-based стратегию hardswap для ME-пула. | `hardswap = true` |
|
||||||
|
| `general.me_reinit_every_secs` | `u64` | `900` | должно быть `> 0` | Интервал периодического reinit ME-пула. | `me_reinit_every_secs = 600` |
|
||||||
|
| `general.me_hardswap_warmup_delay_min_ms` | `u64` | `1000` | должно быть `<= me_hardswap_warmup_delay_max_ms` | Нижняя граница пауз между warmup dial попытками. | `me_hardswap_warmup_delay_min_ms = 500` |
|
||||||
|
| `general.me_hardswap_warmup_delay_max_ms` | `u64` | `2000` | должно быть `> 0` | Верхняя граница пауз между warmup dial попытками. | `me_hardswap_warmup_delay_max_ms = 1200` |
|
||||||
|
| `general.me_hardswap_warmup_extra_passes` | `u8` | `3` | диапазон `[0,10]` | Дополнительные warmup-проходы после базового. | `me_hardswap_warmup_extra_passes = 2` |
|
||||||
|
| `general.me_hardswap_warmup_pass_backoff_base_ms` | `u64` | `500` | должно быть `> 0` | Базовый backoff между extra-pass в warmup. | `me_hardswap_warmup_pass_backoff_base_ms = 400` |
|
||||||
|
| `general.me_config_stable_snapshots` | `u8` | `2` | должно быть `> 0` | Количество одинаковых snapshot перед применением ME map update. | `me_config_stable_snapshots = 3` |
|
||||||
|
| `general.me_config_apply_cooldown_secs` | `u64` | `300` | нет | Cooldown между применёнными обновлениями ME map. | `me_config_apply_cooldown_secs = 120` |
|
||||||
|
| `general.proxy_secret_stable_snapshots` | `u8` | `2` | должно быть `> 0` | Количество одинаковых snapshot перед runtime-rotation proxy-secret. | `proxy_secret_stable_snapshots = 3` |
|
||||||
|
| `general.proxy_secret_rotate_runtime` | `bool` | `true` | нет | Включает runtime-ротацию proxy-secret. | `proxy_secret_rotate_runtime = true` |
|
||||||
|
| `general.proxy_secret_len_max` | `usize` | `256` | диапазон `[32,4096]` | Верхний лимит длины принимаемого proxy-secret. | `proxy_secret_len_max = 512` |
|
||||||
|
| `general.update_every` | `Option<u64>` | `300` | если задано: `> 0`; если `null`: fallback на legacy минимум | Единый интервал refresh для ME config + secret updater. | `update_every = 300` |
|
||||||
|
| `general.me_pool_drain_ttl_secs` | `u64` | `90` | нет | Время, когда stale writer ещё может использоваться как fallback. | `me_pool_drain_ttl_secs = 120` |
|
||||||
|
| `general.me_pool_min_fresh_ratio` | `f32` | `0.8` | диапазон `[0.0,1.0]` | Порог покрытия fresh-поколения перед drain старого поколения. | `me_pool_min_fresh_ratio = 0.9` |
|
||||||
|
| `general.me_reinit_drain_timeout_secs` | `u64` | `120` | `0` = без force-close; если `>0 && < TTL`, поднимается до TTL | Таймаут force-close для draining stale writer. | `me_reinit_drain_timeout_secs = 0` |
|
||||||
|
| `general.auto_degradation_enabled` | `bool` | `true` | нет | Зарезервированный флаг совместимости в текущей ревизии (активного runtime-consumer нет). | `auto_degradation_enabled = true` |
|
||||||
|
| `general.degradation_min_unavailable_dc_groups` | `u8` | `2` | нет | Зарезервированный порог совместимости в текущей ревизии (активного runtime-consumer нет). | `degradation_min_unavailable_dc_groups = 2` |
|
||||||
|
|
||||||
|
## Устаревшие / legacy параметры
|
||||||
|
|
||||||
|
| Параметр | Статус | Замена | Текущее поведение | Рекомендация миграции |
|
||||||
|
|---|---|---|---|---|
|
||||||
|
| `general.middle_proxy_nat_stun` | Deprecated | `network.stun_servers` | Добавляется в `network.stun_servers`, только если `network.stun_servers` не задан явно. | Перенести значение в `network.stun_servers`, legacy-ключ удалить. |
|
||||||
|
| `general.middle_proxy_nat_stun_servers` | Deprecated | `network.stun_servers` | Добавляется в `network.stun_servers`, только если `network.stun_servers` не задан явно. | Перенести значения в `network.stun_servers`, legacy-ключ удалить. |
|
||||||
|
| `general.proxy_secret_auto_reload_secs` | Deprecated | `general.update_every` | Используется только если `update_every = null` (legacy fallback). | Явно задать `general.update_every`, legacy-ключ удалить. |
|
||||||
|
| `general.proxy_config_auto_reload_secs` | Deprecated | `general.update_every` | Используется только если `update_every = null` (legacy fallback). | Явно задать `general.update_every`, legacy-ключ удалить. |
|
||||||
|
|
||||||
|
## Как конфигурируются Upstreams
|
||||||
|
|
||||||
|
### Схема upstream
|
||||||
|
|
||||||
|
| Поле | Применимость | Тип | Обязательно | Default | Назначение |
|
||||||
|
|---|---|---|---|---|---|
|
||||||
|
| `[[upstreams]].type` | все upstream | `"direct" \| "socks4" \| "socks5"` | да | n/a | Тип upstream транспорта. |
|
||||||
|
| `[[upstreams]].weight` | все upstream | `u16` | нет | `1` | Базовый вес в weighted-random выборе. |
|
||||||
|
| `[[upstreams]].enabled` | все upstream | `bool` | нет | `true` | Выключенные записи игнорируются на старте. |
|
||||||
|
| `[[upstreams]].scopes` | все upstream | `String` | нет | `""` | Список scope-токенов через запятую для маршрутизации. |
|
||||||
|
| `interface` | `direct` | `Option<String>` | нет | `null` | Имя интерфейса (например `eth0`) или literal локальный IP. |
|
||||||
|
| `bind_addresses` | `direct` | `Option<Vec<IpAddr>>` | нет | `null` | Явные кандидаты source IP (имеют приоритет над `interface`). |
|
||||||
|
| `address` | `socks4` | `String` | да | n/a | Адрес SOCKS4 сервера (`ip:port` или `host:port`). |
|
||||||
|
| `interface` | `socks4` | `Option<String>` | нет | `null` | Используется только если `address` задан как `ip:port`. |
|
||||||
|
| `user_id` | `socks4` | `Option<String>` | нет | `null` | SOCKS4 user ID в CONNECT-запросе. |
|
||||||
|
| `address` | `socks5` | `String` | да | n/a | Адрес SOCKS5 сервера (`ip:port` или `host:port`). |
|
||||||
|
| `interface` | `socks5` | `Option<String>` | нет | `null` | Используется только если `address` задан как `ip:port`. |
|
||||||
|
| `username` | `socks5` | `Option<String>` | нет | `null` | Логин SOCKS5 auth. |
|
||||||
|
| `password` | `socks5` | `Option<String>` | нет | `null` | Пароль SOCKS5 auth. |
|
||||||
|
|
||||||
|
### Runtime-правила
|
||||||
|
|
||||||
|
1. Если `[[upstreams]]` отсутствует, loader добавляет один upstream `direct` по умолчанию.
|
||||||
|
2. Scope-фильтрация — по точному совпадению токена:
|
||||||
|
- если scope запроса задан -> используются только записи, где `scopes` содержит такой же токен;
|
||||||
|
- если scope запроса не задан -> используются только записи с пустым `scopes`.
|
||||||
|
3. Среди healthy upstream используется weighted-random выбор: `weight * latency_factor`.
|
||||||
|
4. Если в отфильтрованном наборе нет healthy upstream, выбирается случайный из отфильтрованных.
|
||||||
|
5. Порядок выбора bind для `direct`:
|
||||||
|
- сначала `bind_addresses` (только IP нужного семейства);
|
||||||
|
- если одновременно заданы `interface` (имя) и `bind_addresses`, каждый IP проверяется на принадлежность интерфейсу;
|
||||||
|
- несовпадающие IP отбрасываются с `WARN`;
|
||||||
|
- если валидных IP не осталось, используется unbound direct connect (`bind_ip=None`);
|
||||||
|
- если `bind_addresses` не подходит, применяется `interface` (literal IP или адрес интерфейса).
|
||||||
|
6. Для `socks4/socks5` с `address` в виде hostname интерфейсный bind не поддерживается и игнорируется с предупреждением.
|
||||||
|
7. Runtime DNS overrides применяются к резолвингу hostname в upstream-подключениях.
|
||||||
|
8. В ME-режиме выбранный upstream также используется для ME TCP dial path.
|
||||||
|
9. В ME-режиме для `direct` upstream с bind/interface STUN-рефлексия выполняется bind-aware для KDF материала.
|
||||||
|
10. В ME-режиме для SOCKS upstream используются `BND.ADDR/BND.PORT` для KDF, если адрес валиден/публичен и соответствует IP family.
|
||||||
|
|
||||||
|
## Примеры конфигурации Upstreams
|
||||||
|
|
||||||
|
### Пример 1: минимальный direct upstream
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[[upstreams]]
|
||||||
|
type = "direct"
|
||||||
|
weight = 1
|
||||||
|
enabled = true
|
||||||
|
```
|
||||||
|
|
||||||
|
### Пример 2: direct с interface + явными bind IP
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[[upstreams]]
|
||||||
|
type = "direct"
|
||||||
|
interface = "eth0"
|
||||||
|
bind_addresses = ["192.168.1.100", "192.168.1.101"]
|
||||||
|
weight = 3
|
||||||
|
enabled = true
|
||||||
|
```
|
||||||
|
|
||||||
|
### Пример 3: SOCKS5 upstream с аутентификацией
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[[upstreams]]
|
||||||
|
type = "socks5"
|
||||||
|
address = "198.51.100.30:1080"
|
||||||
|
username = "proxy-user"
|
||||||
|
password = "proxy-pass"
|
||||||
|
weight = 2
|
||||||
|
enabled = true
|
||||||
|
```
|
||||||
|
|
||||||
|
### Пример 4: смешанные upstream с scopes
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[[upstreams]]
|
||||||
|
type = "direct"
|
||||||
|
weight = 5
|
||||||
|
enabled = true
|
||||||
|
scopes = ""
|
||||||
|
|
||||||
|
[[upstreams]]
|
||||||
|
type = "socks5"
|
||||||
|
address = "203.0.113.40:1080"
|
||||||
|
username = "edge"
|
||||||
|
password = "edgepass"
|
||||||
|
weight = 3
|
||||||
|
enabled = true
|
||||||
|
scopes = "premium,me"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Пример 5: профиль тюнинга под ME
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[general]
|
||||||
|
use_middle_proxy = true
|
||||||
|
proxy_secret_path = "proxy-secret"
|
||||||
|
middle_proxy_nat_probe = true
|
||||||
|
stun_nat_probe_concurrency = 16
|
||||||
|
middle_proxy_pool_size = 12
|
||||||
|
me_keepalive_enabled = true
|
||||||
|
me_keepalive_interval_secs = 20
|
||||||
|
me_keepalive_jitter_secs = 4
|
||||||
|
me_reconnect_max_concurrent_per_dc = 12
|
||||||
|
me_reconnect_backoff_base_ms = 300
|
||||||
|
me_reconnect_backoff_cap_ms = 10000
|
||||||
|
me_reconnect_fast_retry_count = 10
|
||||||
|
hardswap = true
|
||||||
|
me_reinit_every_secs = 600
|
||||||
|
me_hardswap_warmup_delay_min_ms = 500
|
||||||
|
me_hardswap_warmup_delay_max_ms = 1200
|
||||||
|
me_hardswap_warmup_extra_passes = 2
|
||||||
|
me_hardswap_warmup_pass_backoff_base_ms = 400
|
||||||
|
me_config_stable_snapshots = 3
|
||||||
|
me_config_apply_cooldown_secs = 120
|
||||||
|
proxy_secret_stable_snapshots = 3
|
||||||
|
proxy_secret_rotate_runtime = true
|
||||||
|
proxy_secret_len_max = 512
|
||||||
|
update_every = 300
|
||||||
|
me_pool_drain_ttl_secs = 120
|
||||||
|
me_pool_min_fresh_ratio = 0.9
|
||||||
|
me_reinit_drain_timeout_secs = 180
|
||||||
|
|
||||||
|
[timeouts]
|
||||||
|
me_one_retry = 8
|
||||||
|
me_one_timeout_ms = 1200
|
||||||
|
|
||||||
|
[network]
|
||||||
|
stun_use = true
|
||||||
|
stun_tcp_fallback = true
|
||||||
|
stun_servers = [
|
||||||
|
"stun1.l.google.com:19302",
|
||||||
|
"stun2.l.google.com:19302"
|
||||||
|
]
|
||||||
|
http_ip_detect_urls = [
|
||||||
|
"https://api.ipify.org",
|
||||||
|
"https://ifconfig.me/ip"
|
||||||
|
]
|
||||||
|
```
|
||||||
321
docs/XRAY-SINGBOX-ROUTING.ru.md
Normal file
321
docs/XRAY-SINGBOX-ROUTING.ru.md
Normal file
@@ -0,0 +1,321 @@
|
|||||||
|
# SNI-маршрутизация в xray-core / sing-box + TLS-fronting
|
||||||
|
|
||||||
|
## Термины (в контексте этого кейса)
|
||||||
|
|
||||||
|
- **TLS-fronting домен** — домен, который фигурирует в TLS ClientHello как **SNI** (например, `petrovich.ru`): он используется как "маска" на L7 и как ключ маршрутизации в прокси-роутере.
|
||||||
|
- **xray-core / sing-box** — локальный или удалённый L7/TLS-роутер (прокси), который:
|
||||||
|
1) принимает входящее TCP/TLS-соединение,
|
||||||
|
2) читает TLS ClientHello,
|
||||||
|
3) извлекает SNI,
|
||||||
|
4) по SNI выбирает outbound/апстрим,
|
||||||
|
5) устанавливает новое TCP-соединение к целевому хосту уже **от себя**.
|
||||||
|
- **SNI (Server Name Indication)** — поле в TLS ClientHello, где клиент Telegram сообщает доменное имя для "маскировки"
|
||||||
|
- **DNS-resolve на стороне L7-роутера** — если выходной адрес задан доменом (или роутер решил "всё равно идти по SNI"), то DNS резолвится **на стороне xray/sing-box**, а не на стороне Telegram-клиента
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Ключевая идея: куда на самом деле идёт соединение решает не то, что вы указали клиенту, а то как L7-роутер трактует SNI
|
||||||
|
|
||||||
|
Механика:
|
||||||
|
|
||||||
|
1) Telegram-клиенту вы можете указать **IP/домен telemt**,как "сервер".
|
||||||
|
2) Между клиентом и telemt стоит xray-core/sing-box, который принимает TCP, читает TLS ClientHello и видит **SNI=petrovich.ru**
|
||||||
|
3) Дальше роутер говорит: "Вижу SNI - направить на апстрим/маршрут N"
|
||||||
|
4) И устанавливает исходящее соединение не "по тому IP, который пользователь подразумевал", а **по домену из SNI** (или по сопоставлению SNI→outbound), используя для определния его IP собственный DNS-кеш или резолвер
|
||||||
|
5) `petrovich.ru` по A-записи указывает **не на IP telemt**, а значит при L7-маршрутизации трафик уйдёт на "оригинальный" сайт за этим доменом, а не в telemt: Telegram-клиент, естественно, не сможет получить ожидаемое поведение, потому что ответить с handshake на той стороне некому
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Схема №1 "Как это НЕ работает"
|
||||||
|
|
||||||
|
```text
|
||||||
|
Telegram Client
|
||||||
|
|
|
||||||
|
| (указан IP/домен telemt)
|
||||||
|
v
|
||||||
|
telemt instance
|
||||||
|
````
|
||||||
|
|
||||||
|
Ожидание: "я указал telemt -> значит трафик попадёт в telemt" - **нет!**
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Схема №2. "Как это реально работает с TLS/L7-роутером и SNI"
|
||||||
|
|
||||||
|
```text
|
||||||
|
Telegram Client
|
||||||
|
|
|
||||||
|
| 1) TCP/TLS connection:
|
||||||
|
| - ClientHello:
|
||||||
|
| - SNI=petrovich.ru
|
||||||
|
v
|
||||||
|
xray-core / sing-box / любой L7 router
|
||||||
|
|
|
||||||
|
| 2) читает ClientHello -> вытаскивает SNI
|
||||||
|
| 3) выбирает маршрут по SNI
|
||||||
|
| 4) делает DNS для petrovich.ru
|
||||||
|
| 5) подключается к полученному IP по TLS с этим SNI
|
||||||
|
v
|
||||||
|
"Оригинальный" сайт, A-запись которого не на telemt
|
||||||
|
|
|
||||||
|
X не telemt -> Telegram-клиент не коннектится как ожидалось
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Почему указанный в клиенте IP/домен telemt "не спасает"
|
||||||
|
|
||||||
|
Потому что в таком режиме xray/sing-box выступает как **точка терминации TCP/TLS**, можно сказать - TLS-инспектор на уровне ClientHello, это означает:
|
||||||
|
|
||||||
|
* TCP-сессия от Telegram-клиента заканчивается на xray/sing-box
|
||||||
|
* Дальше создаётся **новая** TCP-сессия "от имени" xray/sing-box к апстриму
|
||||||
|
* Выбор апстрима делается правилами роутинга, а в TLS-сценариях самый удобный и распространённый ключ — **SNI**
|
||||||
|
|
||||||
|
То есть, "куда идти дальше" определяется логикой L7-роутера:
|
||||||
|
|
||||||
|
* либо правилами вида `if SNI == petrovich.ru -> outbound X`,
|
||||||
|
* либо более "автоматическим" поведением: `подключаться к тому хосту, который указан в SNI`,
|
||||||
|
* плюс кэш DNS и собственные резолверы роутера
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Что именно извлекается из TLS ClientHello и почему этого достаточно
|
||||||
|
|
||||||
|
TLS ClientHello отправляется **в начале** TLS-сессии и, в классическом TLS без ECH, содержит SNI в открытом виде.
|
||||||
|
|
||||||
|
Упрощённо:
|
||||||
|
|
||||||
|
```text
|
||||||
|
ClientHello:
|
||||||
|
- supported_versions
|
||||||
|
- cipher_suites
|
||||||
|
- extensions:
|
||||||
|
- server_name: petrovich.ru <-- SNI
|
||||||
|
- alpn: h2/http1.1/...
|
||||||
|
- ...
|
||||||
|
```
|
||||||
|
|
||||||
|
Роутеру не нужно расшифровывать трафик и завершать TLS "как сервер" — часто достаточно просто прочитать первые пакеты и распарсить ClientHello, чтобы получить SNI и принять решение
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Типовой алгоритм SNI-роутинга
|
||||||
|
|
||||||
|
1. Принять входящий TCP.
|
||||||
|
2. Подождать первые байты.
|
||||||
|
3. Определить протокол:
|
||||||
|
|
||||||
|
* если видим TLS ClientHello → парсим SNI/ALPN
|
||||||
|
4. Применить route rules:
|
||||||
|
|
||||||
|
* match по `server_name` / `domain` / `tls.sni`
|
||||||
|
5. Выбрать outbound:
|
||||||
|
|
||||||
|
* direct / proxy / specific upstream / detour
|
||||||
|
6. Установить исходящее соединение:
|
||||||
|
|
||||||
|
* либо на фиксированный IP:порт,
|
||||||
|
* либо на домен через DNS-resolve на стороне роутера
|
||||||
|
7. Начать проксирование данных между входом и выходом
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Почему "A-запись фронтинг-домена не на telemt" ломает кейс
|
||||||
|
|
||||||
|
### Ситуация
|
||||||
|
|
||||||
|
* В ClientHello: `SNI = petrovich.ru`
|
||||||
|
* DNS: `petrovich.ru -> 203.0.113.77` - "оригинальный" сайт
|
||||||
|
* telemt живёт на: `198.51.100.10`
|
||||||
|
|
||||||
|
### Что делает роутер
|
||||||
|
|
||||||
|
* Видит SNI `petrovich.ru`
|
||||||
|
* Либо:
|
||||||
|
|
||||||
|
* (а) напрямую коннектится к `petrovich.ru:443`, резолвя A-запись в `203.0.113.77`,
|
||||||
|
* либо:
|
||||||
|
* (б) выбирает outbound, который указывает на `petrovich.ru` как destination,
|
||||||
|
* либо:
|
||||||
|
* (в) делает sniffing/override destination по SNI
|
||||||
|
|
||||||
|
В итоге исходящий коннект идёт на `203.0.113.77:443`, а не на telemt!
|
||||||
|
Другой сервер, другой протокол, другая логика, где telemt не участвует
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## "Где именно происходит подмена destination на SNI"
|
||||||
|
|
||||||
|
Это зависит от конфигурации, но типовые варианты:
|
||||||
|
|
||||||
|
### Вариант A: outbound задан доменом (и он совпадает с SNI)
|
||||||
|
|
||||||
|
Правило по SNI выбирает outbound, у которого destination задан доменом фронтинга,
|
||||||
|
тогда DNS резолвится на стороне роутера и вы уходите на "оригинальный" хост
|
||||||
|
|
||||||
|
### Вариант B: destination override / sniffing
|
||||||
|
|
||||||
|
Роутер "снифает" SNI и **перезаписывает** destination на домен из SNI (даже если вход изначально был на IP telemt),
|
||||||
|
это особенно коварно: пользователь видит "я подключаюсь к IP telemt", но роутер после sniffing решает иначе
|
||||||
|
|
||||||
|
### Вариант C: split DNS / кеш / независимый резолвер
|
||||||
|
|
||||||
|
Даже если клиент "где-то" резолвит иначе, это не важно: конечный DNS для исходящего коннекта — на стороне xray/sing-box,
|
||||||
|
который может иметь:
|
||||||
|
|
||||||
|
* свой DoH/DoT,
|
||||||
|
* свой кеш,
|
||||||
|
* свои правила fake-ip / system resolver,
|
||||||
|
* и, как следствие, своя "карта" **домен/SNI -> IP**
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Признаки того, что трафик "утёк на оригинал", а не попал в telemt
|
||||||
|
|
||||||
|
* На стороне telemt отсутствуют входящие соединения/логи
|
||||||
|
* На стороне роутера видно, что destination — домен фронтинга, а IP соответствует публичному сайту
|
||||||
|
* TLS-метрики/сертификат на выходе соответствует "оригинальному" сайту в записах трафика
|
||||||
|
* Telegram-клиент получает неожиданный тип ответов/ошибку handshaking/timeout в debug-режиме
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Best-practice решение для этого кейса: свой домен фронтинга + заглушка на telemt + Let's Encrypt
|
||||||
|
|
||||||
|
### Цель
|
||||||
|
|
||||||
|
Сделать так, чтобы:
|
||||||
|
|
||||||
|
* SNI (фронтинг-домен) **резолвился в IP telemt**,
|
||||||
|
* на IP telemt реально был TLS-сервис с валидным сертификатом под этот домен,
|
||||||
|
* даже если кто-то "попробует открыть домен как сайт", он увидит нормальную заглушку, а не "пустоту"
|
||||||
|
|
||||||
|
### Что это даёт
|
||||||
|
|
||||||
|
* xray/sing-box, маршрутизируя по SNI, будет неизбежно приходить на telemt, потому что DNS(SNI-домен) → IP telemt
|
||||||
|
* Внешний вид будет правдоподобным: обычный домен с обычным сертификатом
|
||||||
|
* Устойчивость: меньше сюрпризов от DNS-кеша/перерезолва/"умных" правил роутера
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Рекомендуемая схема (целевое состояние)
|
||||||
|
|
||||||
|
```text
|
||||||
|
Telegram Client
|
||||||
|
|
|
||||||
|
| TLS ClientHello: SNI = hello.example.com
|
||||||
|
v
|
||||||
|
xray-core / sing-box
|
||||||
|
|
|
||||||
|
| Route by SNI -> outbound -> connect to hello.example.com:443
|
||||||
|
| DNS(hello.example.com) = IP telemt
|
||||||
|
v
|
||||||
|
telemt instance (IP telemt)
|
||||||
|
|
|
||||||
|
| TLS cert for hello.example.com (Let's Encrypt)
|
||||||
|
| + сайт-заглушка / health endpoint
|
||||||
|
v
|
||||||
|
OK
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Практический чеклист (минимальный)
|
||||||
|
|
||||||
|
1. Купить/иметь домен: `hello.example.com`
|
||||||
|
2. В DNS:
|
||||||
|
|
||||||
|
* `A hello.example.com -> <IP telemt>`
|
||||||
|
* (опционально) AAAA, если используете IPv6 и он стабилен
|
||||||
|
3. На telemt-хосте:
|
||||||
|
|
||||||
|
* поднять TLS endpoint на 443 с валидным сертификатом LE под `hello.example.com`
|
||||||
|
* отдать "заглушку" (например, статический сайт), чтобы домен выглядел как обычный веб-сервис
|
||||||
|
4. В xray/sing-box правилах:
|
||||||
|
|
||||||
|
* маршрутизировать нужный трафик по SNI = `hello.example.com` в "правильный" outbound (к telemt)
|
||||||
|
* избегать конфигураций, где destination override уводит на чужой домен
|
||||||
|
5. Важно:
|
||||||
|
|
||||||
|
* если вы используете кеш DNS на роутере — сбросить/обновить его после смены A-записи
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Пояснение про сайт-заглушку
|
||||||
|
|
||||||
|
Для эмуляции TLS, telemt имеет подсистему TLS-F в `src/tls_front`:
|
||||||
|
- её модуль - fetcher, собирает TLS-профили, чтоб максимально поведенчески корректно повторять TLS конкретно указанного сайта
|
||||||
|
|
||||||
|
Когда вы указываете сайт, который не отвечает по TLS:
|
||||||
|
- fetcher не может собрать TLS-профиль и происходит fallback на `fake_cert_len` - примитивный алгоритм,
|
||||||
|
- он забивает служебную информацию TLS рандомными байтами,
|
||||||
|
- простые системы DPI не распознают это
|
||||||
|
- однако, продвинутые системы, такие как nEdge или Fraud Control в сетях мобильной связи легко заблокируют или замедлят такой трафик
|
||||||
|
|
||||||
|
Создав сайт-заглушку с Let's Encrypt сертификатом, вы даёте TLS-F возможность получить данные сертификата и корректно его "повторять" в дальнейшем
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Вариант конфиг-подхода: "SNI строго привязываем к telemt - фиксированный IP"
|
||||||
|
|
||||||
|
Чтобы полностью исключить зависимость от DNS если вам это нужно, можно сделать outbound, который ходит на **фиксированный IP telemt**, но при этом выставляет SNI/Host как `hello.example.com`.
|
||||||
|
|
||||||
|
Идея:
|
||||||
|
|
||||||
|
* destination: `IP:443`
|
||||||
|
* SNI: `hello.example.com`
|
||||||
|
* сертификат на telemt именно под `hello.example.com`
|
||||||
|
|
||||||
|
Так вы получаете:
|
||||||
|
|
||||||
|
* TLS выглядит корректно, ведь SNI совпадает с сертификатом,
|
||||||
|
* а routing никогда не уйдёт на "оригинал", потому что A-запись указывает на telemt и контроллируется вами!
|
||||||
|
|
||||||
|
Но в вашем описании проблема как раз в том, что роутер "сам решает по SNI и резолвит домен", поэтому самый универсальный вариант — сделать так, чтобы DNS всегда приводил в telemt
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Пример логики правил на псевдоконфиге L7-роутера
|
||||||
|
|
||||||
|
```text
|
||||||
|
if inbound is TLS and sni == "hello.example.com":
|
||||||
|
route -> outbound "telemt"
|
||||||
|
else:
|
||||||
|
route -> outbound "default"
|
||||||
|
```
|
||||||
|
|
||||||
|
Outbound `telemt`:
|
||||||
|
|
||||||
|
* destination: `hello.example.com:443`
|
||||||
|
* TLS enabled
|
||||||
|
* SNI: `hello.example.com`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Отдельно: что может неожиданно сломать даже "правильный" DNS
|
||||||
|
|
||||||
|
* **Кеширование DNS** на xray/sing-box или на системном резолвере, особенно при смене A-записи
|
||||||
|
* **Split-horizon DNS**: разные ответы внутри/снаружи, попытки подмены/терминирования в других точках
|
||||||
|
* **IPv6**: если есть AAAA и он указывает не туда, роутер может предпочесть IPv6: помните, что поддержка v6 нестабильна и не рекомендуется в prod
|
||||||
|
* **DoH/DoT** на роутере: он может резолвить не тем резолвером, которым вы проверяли
|
||||||
|
|
||||||
|
Минимальная гигиена:
|
||||||
|
|
||||||
|
* контролировать A/AAAA,
|
||||||
|
* держать TTL разумным,
|
||||||
|
* проверять, каким резолвером пользуется именно роутер,
|
||||||
|
* при необходимости отключить/ограничить destination override
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Итог
|
||||||
|
|
||||||
|
В режиме TLS-fronting с xray-core/sing-box как L7/TLS-роутером **SNI становится приоритетным "source-of-truth" для маршрутизации**
|
||||||
|
|
||||||
|
Если фронтинг-домен по DNS указывает не на IP telemt, роутер честно уводит трафик на "оригинальный" сайт, потому что он строит исходящее соединение "по SNI"
|
||||||
|
|
||||||
|
Надёжное решение для этого кейса:
|
||||||
|
|
||||||
|
* использовать **свой домен** для фронтинга,
|
||||||
|
* направить его **A/AAAA** на IP telemt,
|
||||||
|
* поднять на telemt **TLS-сервис с Let’s Encrypt сертификатом** под этот домен,
|
||||||
|
* (желательно) держать **сайт-заглушку**, чтобы 443 выглядел как обычный HTTPS
|
||||||
73
install.sh
Normal file
73
install.sh
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
sudo bash -c '
|
||||||
|
set -e
|
||||||
|
|
||||||
|
# --- Проверка на существующую установку ---
|
||||||
|
if systemctl list-unit-files | grep -q telemt.service; then
|
||||||
|
# --- РЕЖИМ ОБНОВЛЕНИЯ ---
|
||||||
|
echo "--- Обнаружена существующая установка Telemt. Запускаю обновление... ---"
|
||||||
|
|
||||||
|
echo "[*] Остановка службы telemt..."
|
||||||
|
systemctl stop telemt || true # Игнорируем ошибку, если служба уже остановлена
|
||||||
|
|
||||||
|
echo "[1/2] Скачивание последней версии Telemt..."
|
||||||
|
wget -qO- "https://github.com/telemt/telemt/releases/latest/download/telemt-$(uname -m)-linux-$(ldd --version 2>&1 | grep -iq musl && echo musl || echo gnu).tar.gz" | tar -xz
|
||||||
|
|
||||||
|
echo "[1/2] Замена исполняемого файла в /usr/local/bin..."
|
||||||
|
mv telemt /usr/local/bin/telemt
|
||||||
|
chmod +x /usr/local/bin/telemt
|
||||||
|
|
||||||
|
echo "[2/2] Запуск службы..."
|
||||||
|
systemctl start telemt
|
||||||
|
|
||||||
|
echo "--- Обновление Telemt успешно завершено! ---"
|
||||||
|
echo
|
||||||
|
echo "Для проверки статуса службы выполните:"
|
||||||
|
echo " systemctl status telemt"
|
||||||
|
|
||||||
|
else
|
||||||
|
# --- РЕЖИМ НОВОЙ УСТАНОВКИ ---
|
||||||
|
echo "--- Начало автоматической установки Telemt ---"
|
||||||
|
|
||||||
|
# Шаг 1: Скачивание и установка бинарного файла
|
||||||
|
echo "[1/5] Скачивание последней версии Telemt..."
|
||||||
|
wget -qO- "https://github.com/telemt/telemt/releases/latest/download/telemt-$(uname -m)-linux-$(ldd --version 2>&1 | grep -iq musl && echo musl || echo gnu).tar.gz" | tar -xz
|
||||||
|
|
||||||
|
echo "[1/5] Перемещение исполняемого файла в /usr/local/bin и установка прав..."
|
||||||
|
mv telemt /usr/local/bin/telemt
|
||||||
|
chmod +x /usr/local/bin/telemt
|
||||||
|
|
||||||
|
# Шаг 2: Генерация секрета
|
||||||
|
echo "[2/5] Генерация секретного ключа..."
|
||||||
|
SECRET=$(openssl rand -hex 16)
|
||||||
|
|
||||||
|
# Шаг 3: Создание файла конфигурации
|
||||||
|
echo "[3/5] Создание файла конфигурации /etc/telemt.toml..."
|
||||||
|
printf "# === General Settings ===\n[general]\n[general.modes]\nclassic = false\nsecure = false\ntls = true\n\n# === Anti-Censorship & Masking ===\n[censorship]\n# !!! ВАЖНО: Замените на ваш домен или домен, который вы хотите использовать для маскировки !!!\ntls_domain = \"petrovich.ru\"\n\n[access.users]\nhello = \"%s\"\n" "$SECRET" > /etc/telemt.toml
|
||||||
|
|
||||||
|
# Шаг 4: Создание службы Systemd
|
||||||
|
echo "[4/5] Создание службы systemd..."
|
||||||
|
printf "[Unit]\nDescription=Telemt Proxy\nAfter=network.target\n\n[Service]\nType=simple\nExecStart=/usr/local/bin/telemt /etc/telemt.toml\nRestart=on-failure\nRestartSec=5\nLimitNOFILE=65536\n\n[Install]\nWantedBy=multi-user.target\n" > /etc/systemd/system/telemt.service
|
||||||
|
|
||||||
|
# Шаг 5: Запуск службы
|
||||||
|
echo "[5/5] Перезагрузка systemd, запуск и включение службы telemt..."
|
||||||
|
systemctl daemon-reload
|
||||||
|
systemctl start telemt
|
||||||
|
systemctl enable telemt
|
||||||
|
|
||||||
|
echo "--- Установка и запуск Telemt успешно завершены! ---"
|
||||||
|
echo
|
||||||
|
echo "ВАЖНАЯ ИНФОРМАЦИЯ:"
|
||||||
|
echo "==================="
|
||||||
|
echo "1. Вам НЕОБХОДИМО отредактировать файл /etc/telemt.toml и заменить '\''petrovich.ru'\'' на другой домен"
|
||||||
|
echo " с помощью команды:"
|
||||||
|
echo " nano /etc/telemt.toml"
|
||||||
|
echo " После редактирования файла перезапустите службу командой:"
|
||||||
|
echo " sudo systemctl restart telemt"
|
||||||
|
echo
|
||||||
|
echo "2. Для проверки статуса службы выполните команду:"
|
||||||
|
echo " systemctl status telemt"
|
||||||
|
echo
|
||||||
|
echo "3. Для получения ссылок на подключение выполните команду:"
|
||||||
|
echo " journalctl -u telemt -n -g '\''links'\'' --no-pager -o cat | tac"
|
||||||
|
fi
|
||||||
|
'
|
||||||
107
src/api/config_store.rs
Normal file
107
src/api/config_store.rs
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
use std::io::Write;
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
|
use hyper::header::IF_MATCH;
|
||||||
|
use sha2::{Digest, Sha256};
|
||||||
|
|
||||||
|
use crate::config::ProxyConfig;
|
||||||
|
|
||||||
|
use super::model::ApiFailure;
|
||||||
|
|
||||||
|
pub(super) fn parse_if_match(headers: &hyper::HeaderMap) -> Option<String> {
|
||||||
|
headers
|
||||||
|
.get(IF_MATCH)
|
||||||
|
.and_then(|value| value.to_str().ok())
|
||||||
|
.map(str::trim)
|
||||||
|
.filter(|value| !value.is_empty())
|
||||||
|
.map(|value| value.trim_matches('"').to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) async fn ensure_expected_revision(
|
||||||
|
config_path: &Path,
|
||||||
|
expected_revision: Option<&str>,
|
||||||
|
) -> Result<(), ApiFailure> {
|
||||||
|
let Some(expected) = expected_revision else {
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
let current = current_revision(config_path).await?;
|
||||||
|
if current != expected {
|
||||||
|
return Err(ApiFailure::new(
|
||||||
|
hyper::StatusCode::CONFLICT,
|
||||||
|
"revision_conflict",
|
||||||
|
"Config revision mismatch",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) async fn current_revision(config_path: &Path) -> Result<String, ApiFailure> {
|
||||||
|
let content = tokio::fs::read_to_string(config_path)
|
||||||
|
.await
|
||||||
|
.map_err(|e| ApiFailure::internal(format!("failed to read config: {}", e)))?;
|
||||||
|
Ok(compute_revision(&content))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) fn compute_revision(content: &str) -> String {
|
||||||
|
let mut hasher = Sha256::new();
|
||||||
|
hasher.update(content.as_bytes());
|
||||||
|
hex::encode(hasher.finalize())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) async fn load_config_from_disk(config_path: &Path) -> Result<ProxyConfig, ApiFailure> {
|
||||||
|
let config_path = config_path.to_path_buf();
|
||||||
|
tokio::task::spawn_blocking(move || ProxyConfig::load(config_path))
|
||||||
|
.await
|
||||||
|
.map_err(|e| ApiFailure::internal(format!("failed to join config loader: {}", e)))?
|
||||||
|
.map_err(|e| ApiFailure::internal(format!("failed to load config: {}", e)))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) async fn save_config_to_disk(
|
||||||
|
config_path: &Path,
|
||||||
|
cfg: &ProxyConfig,
|
||||||
|
) -> Result<String, ApiFailure> {
|
||||||
|
let serialized = toml::to_string_pretty(cfg)
|
||||||
|
.map_err(|e| ApiFailure::internal(format!("failed to serialize config: {}", e)))?;
|
||||||
|
write_atomic(config_path.to_path_buf(), serialized.clone()).await?;
|
||||||
|
Ok(compute_revision(&serialized))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn write_atomic(path: PathBuf, contents: String) -> Result<(), ApiFailure> {
|
||||||
|
tokio::task::spawn_blocking(move || write_atomic_sync(&path, &contents))
|
||||||
|
.await
|
||||||
|
.map_err(|e| ApiFailure::internal(format!("failed to join writer: {}", e)))?
|
||||||
|
.map_err(|e| ApiFailure::internal(format!("failed to write config: {}", e)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_atomic_sync(path: &Path, contents: &str) -> std::io::Result<()> {
|
||||||
|
let parent = path.parent().unwrap_or_else(|| Path::new("."));
|
||||||
|
std::fs::create_dir_all(parent)?;
|
||||||
|
|
||||||
|
let tmp_name = format!(
|
||||||
|
".{}.tmp-{}",
|
||||||
|
path.file_name()
|
||||||
|
.and_then(|s| s.to_str())
|
||||||
|
.unwrap_or("config.toml"),
|
||||||
|
rand::random::<u64>()
|
||||||
|
);
|
||||||
|
let tmp_path = parent.join(tmp_name);
|
||||||
|
|
||||||
|
let write_result = (|| {
|
||||||
|
let mut file = std::fs::OpenOptions::new()
|
||||||
|
.create_new(true)
|
||||||
|
.write(true)
|
||||||
|
.open(&tmp_path)?;
|
||||||
|
file.write_all(contents.as_bytes())?;
|
||||||
|
file.sync_all()?;
|
||||||
|
std::fs::rename(&tmp_path, path)?;
|
||||||
|
if let Ok(dir) = std::fs::File::open(parent) {
|
||||||
|
let _ = dir.sync_all();
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
})();
|
||||||
|
|
||||||
|
if write_result.is_err() {
|
||||||
|
let _ = std::fs::remove_file(&tmp_path);
|
||||||
|
}
|
||||||
|
write_result
|
||||||
|
}
|
||||||
443
src/api/mod.rs
Normal file
443
src/api/mod.rs
Normal file
@@ -0,0 +1,443 @@
|
|||||||
|
use std::convert::Infallible;
|
||||||
|
use std::net::{IpAddr, SocketAddr};
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::sync::atomic::{AtomicU64, Ordering};
|
||||||
|
|
||||||
|
use http_body_util::{BodyExt, Full};
|
||||||
|
use hyper::body::{Bytes, Incoming};
|
||||||
|
use hyper::header::AUTHORIZATION;
|
||||||
|
use hyper::server::conn::http1;
|
||||||
|
use hyper::service::service_fn;
|
||||||
|
use hyper::{Method, Request, Response, StatusCode};
|
||||||
|
use serde::Serialize;
|
||||||
|
use serde::de::DeserializeOwned;
|
||||||
|
use tokio::net::TcpListener;
|
||||||
|
use tokio::sync::{Mutex, watch};
|
||||||
|
use tracing::{debug, info, warn};
|
||||||
|
|
||||||
|
use crate::config::ProxyConfig;
|
||||||
|
use crate::ip_tracker::UserIpTracker;
|
||||||
|
use crate::stats::Stats;
|
||||||
|
use crate::transport::middle_proxy::MePool;
|
||||||
|
use crate::transport::UpstreamManager;
|
||||||
|
|
||||||
|
mod config_store;
|
||||||
|
mod model;
|
||||||
|
mod runtime_stats;
|
||||||
|
mod users;
|
||||||
|
|
||||||
|
use config_store::{current_revision, parse_if_match};
|
||||||
|
use model::{
|
||||||
|
ApiFailure, CreateUserRequest, ErrorBody, ErrorResponse, HealthData, PatchUserRequest,
|
||||||
|
RotateSecretRequest, SuccessResponse, SummaryData,
|
||||||
|
};
|
||||||
|
use runtime_stats::{
|
||||||
|
MinimalCacheEntry, build_dcs_data, build_me_writers_data, build_minimal_all_data,
|
||||||
|
build_upstreams_data, build_zero_all_data,
|
||||||
|
};
|
||||||
|
use users::{create_user, delete_user, patch_user, rotate_secret, users_from_config};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub(super) struct ApiShared {
|
||||||
|
pub(super) stats: Arc<Stats>,
|
||||||
|
pub(super) ip_tracker: Arc<UserIpTracker>,
|
||||||
|
pub(super) me_pool: Option<Arc<MePool>>,
|
||||||
|
pub(super) upstream_manager: Arc<UpstreamManager>,
|
||||||
|
pub(super) config_path: PathBuf,
|
||||||
|
pub(super) startup_detected_ip_v4: Option<IpAddr>,
|
||||||
|
pub(super) startup_detected_ip_v6: Option<IpAddr>,
|
||||||
|
pub(super) mutation_lock: Arc<Mutex<()>>,
|
||||||
|
pub(super) minimal_cache: Arc<Mutex<Option<MinimalCacheEntry>>>,
|
||||||
|
pub(super) request_id: Arc<AtomicU64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ApiShared {
|
||||||
|
fn next_request_id(&self) -> u64 {
|
||||||
|
self.request_id.fetch_add(1, Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn serve(
|
||||||
|
listen: SocketAddr,
|
||||||
|
stats: Arc<Stats>,
|
||||||
|
ip_tracker: Arc<UserIpTracker>,
|
||||||
|
me_pool: Option<Arc<MePool>>,
|
||||||
|
upstream_manager: Arc<UpstreamManager>,
|
||||||
|
config_rx: watch::Receiver<Arc<ProxyConfig>>,
|
||||||
|
config_path: PathBuf,
|
||||||
|
startup_detected_ip_v4: Option<IpAddr>,
|
||||||
|
startup_detected_ip_v6: Option<IpAddr>,
|
||||||
|
) {
|
||||||
|
let listener = match TcpListener::bind(listen).await {
|
||||||
|
Ok(listener) => listener,
|
||||||
|
Err(error) => {
|
||||||
|
warn!(
|
||||||
|
error = %error,
|
||||||
|
listen = %listen,
|
||||||
|
"Failed to bind API listener"
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
info!("API endpoint: http://{}/v1/*", listen);
|
||||||
|
|
||||||
|
let shared = Arc::new(ApiShared {
|
||||||
|
stats,
|
||||||
|
ip_tracker,
|
||||||
|
me_pool,
|
||||||
|
upstream_manager,
|
||||||
|
config_path,
|
||||||
|
startup_detected_ip_v4,
|
||||||
|
startup_detected_ip_v6,
|
||||||
|
mutation_lock: Arc::new(Mutex::new(())),
|
||||||
|
minimal_cache: Arc::new(Mutex::new(None)),
|
||||||
|
request_id: Arc::new(AtomicU64::new(1)),
|
||||||
|
});
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let (stream, peer) = match listener.accept().await {
|
||||||
|
Ok(v) => v,
|
||||||
|
Err(error) => {
|
||||||
|
warn!(error = %error, "API accept error");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let shared_conn = shared.clone();
|
||||||
|
let config_rx_conn = config_rx.clone();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
let svc = service_fn(move |req: Request<Incoming>| {
|
||||||
|
let shared_req = shared_conn.clone();
|
||||||
|
let config_rx_req = config_rx_conn.clone();
|
||||||
|
async move { handle(req, peer, shared_req, config_rx_req).await }
|
||||||
|
});
|
||||||
|
if let Err(error) = http1::Builder::new()
|
||||||
|
.serve_connection(hyper_util::rt::TokioIo::new(stream), svc)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
debug!(error = %error, "API connection error");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handle(
|
||||||
|
req: Request<Incoming>,
|
||||||
|
peer: SocketAddr,
|
||||||
|
shared: Arc<ApiShared>,
|
||||||
|
config_rx: watch::Receiver<Arc<ProxyConfig>>,
|
||||||
|
) -> Result<Response<Full<Bytes>>, Infallible> {
|
||||||
|
let request_id = shared.next_request_id();
|
||||||
|
let cfg = config_rx.borrow().clone();
|
||||||
|
let api_cfg = &cfg.server.api;
|
||||||
|
|
||||||
|
if !api_cfg.enabled {
|
||||||
|
return Ok(error_response(
|
||||||
|
request_id,
|
||||||
|
ApiFailure::new(
|
||||||
|
StatusCode::SERVICE_UNAVAILABLE,
|
||||||
|
"api_disabled",
|
||||||
|
"API is disabled",
|
||||||
|
),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if !api_cfg.whitelist.is_empty()
|
||||||
|
&& !api_cfg
|
||||||
|
.whitelist
|
||||||
|
.iter()
|
||||||
|
.any(|net| net.contains(peer.ip()))
|
||||||
|
{
|
||||||
|
return Ok(error_response(
|
||||||
|
request_id,
|
||||||
|
ApiFailure::new(StatusCode::FORBIDDEN, "forbidden", "Source IP is not allowed"),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if !api_cfg.auth_header.is_empty() {
|
||||||
|
let auth_ok = req
|
||||||
|
.headers()
|
||||||
|
.get(AUTHORIZATION)
|
||||||
|
.and_then(|v| v.to_str().ok())
|
||||||
|
.map(|v| v == api_cfg.auth_header)
|
||||||
|
.unwrap_or(false);
|
||||||
|
if !auth_ok {
|
||||||
|
return Ok(error_response(
|
||||||
|
request_id,
|
||||||
|
ApiFailure::new(
|
||||||
|
StatusCode::UNAUTHORIZED,
|
||||||
|
"unauthorized",
|
||||||
|
"Missing or invalid Authorization header",
|
||||||
|
),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let method = req.method().clone();
|
||||||
|
let path = req.uri().path().to_string();
|
||||||
|
let body_limit = api_cfg.request_body_limit_bytes;
|
||||||
|
|
||||||
|
let result: Result<Response<Full<Bytes>>, ApiFailure> = async {
|
||||||
|
match (method.as_str(), path.as_str()) {
|
||||||
|
("GET", "/v1/health") => {
|
||||||
|
let revision = current_revision(&shared.config_path).await?;
|
||||||
|
let data = HealthData {
|
||||||
|
status: "ok",
|
||||||
|
read_only: api_cfg.read_only,
|
||||||
|
};
|
||||||
|
Ok(success_response(StatusCode::OK, data, revision))
|
||||||
|
}
|
||||||
|
("GET", "/v1/stats/summary") => {
|
||||||
|
let revision = current_revision(&shared.config_path).await?;
|
||||||
|
let data = SummaryData {
|
||||||
|
uptime_seconds: shared.stats.uptime_secs(),
|
||||||
|
connections_total: shared.stats.get_connects_all(),
|
||||||
|
connections_bad_total: shared.stats.get_connects_bad(),
|
||||||
|
handshake_timeouts_total: shared.stats.get_handshake_timeouts(),
|
||||||
|
configured_users: cfg.access.users.len(),
|
||||||
|
};
|
||||||
|
Ok(success_response(StatusCode::OK, data, revision))
|
||||||
|
}
|
||||||
|
("GET", "/v1/stats/zero/all") => {
|
||||||
|
let revision = current_revision(&shared.config_path).await?;
|
||||||
|
let data = build_zero_all_data(&shared.stats, cfg.access.users.len());
|
||||||
|
Ok(success_response(StatusCode::OK, data, revision))
|
||||||
|
}
|
||||||
|
("GET", "/v1/stats/upstreams") => {
|
||||||
|
let revision = current_revision(&shared.config_path).await?;
|
||||||
|
let data = build_upstreams_data(shared.as_ref(), api_cfg);
|
||||||
|
Ok(success_response(StatusCode::OK, data, revision))
|
||||||
|
}
|
||||||
|
("GET", "/v1/stats/minimal/all") => {
|
||||||
|
let revision = current_revision(&shared.config_path).await?;
|
||||||
|
let data = build_minimal_all_data(shared.as_ref(), api_cfg).await;
|
||||||
|
Ok(success_response(StatusCode::OK, data, revision))
|
||||||
|
}
|
||||||
|
("GET", "/v1/stats/me-writers") => {
|
||||||
|
let revision = current_revision(&shared.config_path).await?;
|
||||||
|
let data = build_me_writers_data(shared.as_ref(), api_cfg).await;
|
||||||
|
Ok(success_response(StatusCode::OK, data, revision))
|
||||||
|
}
|
||||||
|
("GET", "/v1/stats/dcs") => {
|
||||||
|
let revision = current_revision(&shared.config_path).await?;
|
||||||
|
let data = build_dcs_data(shared.as_ref(), api_cfg).await;
|
||||||
|
Ok(success_response(StatusCode::OK, data, revision))
|
||||||
|
}
|
||||||
|
("GET", "/v1/stats/users") | ("GET", "/v1/users") => {
|
||||||
|
let revision = current_revision(&shared.config_path).await?;
|
||||||
|
let users = users_from_config(
|
||||||
|
&cfg,
|
||||||
|
&shared.stats,
|
||||||
|
&shared.ip_tracker,
|
||||||
|
shared.startup_detected_ip_v4,
|
||||||
|
shared.startup_detected_ip_v6,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
Ok(success_response(StatusCode::OK, users, revision))
|
||||||
|
}
|
||||||
|
("POST", "/v1/users") => {
|
||||||
|
if api_cfg.read_only {
|
||||||
|
return Ok(error_response(
|
||||||
|
request_id,
|
||||||
|
ApiFailure::new(
|
||||||
|
StatusCode::FORBIDDEN,
|
||||||
|
"read_only",
|
||||||
|
"API runs in read-only mode",
|
||||||
|
),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
let expected_revision = parse_if_match(req.headers());
|
||||||
|
let body = read_json::<CreateUserRequest>(req.into_body(), body_limit).await?;
|
||||||
|
let (data, revision) = create_user(body, expected_revision, &shared).await?;
|
||||||
|
Ok(success_response(StatusCode::CREATED, data, revision))
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
if let Some(user) = path.strip_prefix("/v1/users/")
|
||||||
|
&& !user.is_empty()
|
||||||
|
&& !user.contains('/')
|
||||||
|
{
|
||||||
|
if method == Method::GET {
|
||||||
|
let revision = current_revision(&shared.config_path).await?;
|
||||||
|
let users = users_from_config(
|
||||||
|
&cfg,
|
||||||
|
&shared.stats,
|
||||||
|
&shared.ip_tracker,
|
||||||
|
shared.startup_detected_ip_v4,
|
||||||
|
shared.startup_detected_ip_v6,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
if let Some(user_info) = users.into_iter().find(|entry| entry.username == user)
|
||||||
|
{
|
||||||
|
return Ok(success_response(StatusCode::OK, user_info, revision));
|
||||||
|
}
|
||||||
|
return Ok(error_response(
|
||||||
|
request_id,
|
||||||
|
ApiFailure::new(StatusCode::NOT_FOUND, "not_found", "User not found"),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
if method == Method::PATCH {
|
||||||
|
if api_cfg.read_only {
|
||||||
|
return Ok(error_response(
|
||||||
|
request_id,
|
||||||
|
ApiFailure::new(
|
||||||
|
StatusCode::FORBIDDEN,
|
||||||
|
"read_only",
|
||||||
|
"API runs in read-only mode",
|
||||||
|
),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
let expected_revision = parse_if_match(req.headers());
|
||||||
|
let body = read_json::<PatchUserRequest>(req.into_body(), body_limit).await?;
|
||||||
|
let (data, revision) =
|
||||||
|
patch_user(user, body, expected_revision, &shared).await?;
|
||||||
|
return Ok(success_response(StatusCode::OK, data, revision));
|
||||||
|
}
|
||||||
|
if method == Method::DELETE {
|
||||||
|
if api_cfg.read_only {
|
||||||
|
return Ok(error_response(
|
||||||
|
request_id,
|
||||||
|
ApiFailure::new(
|
||||||
|
StatusCode::FORBIDDEN,
|
||||||
|
"read_only",
|
||||||
|
"API runs in read-only mode",
|
||||||
|
),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
let expected_revision = parse_if_match(req.headers());
|
||||||
|
let (deleted_user, revision) =
|
||||||
|
delete_user(user, expected_revision, &shared).await?;
|
||||||
|
return Ok(success_response(StatusCode::OK, deleted_user, revision));
|
||||||
|
}
|
||||||
|
if method == Method::POST
|
||||||
|
&& let Some(base_user) = user.strip_suffix("/rotate-secret")
|
||||||
|
&& !base_user.is_empty()
|
||||||
|
&& !base_user.contains('/')
|
||||||
|
{
|
||||||
|
if api_cfg.read_only {
|
||||||
|
return Ok(error_response(
|
||||||
|
request_id,
|
||||||
|
ApiFailure::new(
|
||||||
|
StatusCode::FORBIDDEN,
|
||||||
|
"read_only",
|
||||||
|
"API runs in read-only mode",
|
||||||
|
),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
let expected_revision = parse_if_match(req.headers());
|
||||||
|
let body =
|
||||||
|
read_optional_json::<RotateSecretRequest>(req.into_body(), body_limit)
|
||||||
|
.await?;
|
||||||
|
let (data, revision) =
|
||||||
|
rotate_secret(base_user, body.unwrap_or_default(), expected_revision, &shared)
|
||||||
|
.await?;
|
||||||
|
return Ok(success_response(StatusCode::OK, data, revision));
|
||||||
|
}
|
||||||
|
if method == Method::POST {
|
||||||
|
return Ok(error_response(
|
||||||
|
request_id,
|
||||||
|
ApiFailure::new(StatusCode::NOT_FOUND, "not_found", "Route not found"),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
return Ok(error_response(
|
||||||
|
request_id,
|
||||||
|
ApiFailure::new(
|
||||||
|
StatusCode::METHOD_NOT_ALLOWED,
|
||||||
|
"method_not_allowed",
|
||||||
|
"Unsupported HTTP method for this route",
|
||||||
|
),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
Ok(error_response(
|
||||||
|
request_id,
|
||||||
|
ApiFailure::new(StatusCode::NOT_FOUND, "not_found", "Route not found"),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.await;
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Ok(resp) => Ok(resp),
|
||||||
|
Err(error) => Ok(error_response(request_id, error)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn success_response<T: Serialize>(
|
||||||
|
status: StatusCode,
|
||||||
|
data: T,
|
||||||
|
revision: String,
|
||||||
|
) -> Response<Full<Bytes>> {
|
||||||
|
let payload = SuccessResponse {
|
||||||
|
ok: true,
|
||||||
|
data,
|
||||||
|
revision,
|
||||||
|
};
|
||||||
|
let body = serde_json::to_vec(&payload).unwrap_or_else(|_| b"{\"ok\":false}".to_vec());
|
||||||
|
Response::builder()
|
||||||
|
.status(status)
|
||||||
|
.header("content-type", "application/json; charset=utf-8")
|
||||||
|
.body(Full::new(Bytes::from(body)))
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn error_response(request_id: u64, failure: ApiFailure) -> Response<Full<Bytes>> {
|
||||||
|
let payload = ErrorResponse {
|
||||||
|
ok: false,
|
||||||
|
error: ErrorBody {
|
||||||
|
code: failure.code,
|
||||||
|
message: failure.message,
|
||||||
|
},
|
||||||
|
request_id,
|
||||||
|
};
|
||||||
|
let body = serde_json::to_vec(&payload).unwrap_or_else(|_| {
|
||||||
|
format!(
|
||||||
|
"{{\"ok\":false,\"error\":{{\"code\":\"internal_error\",\"message\":\"serialization failed\"}},\"request_id\":{}}}",
|
||||||
|
request_id
|
||||||
|
)
|
||||||
|
.into_bytes()
|
||||||
|
});
|
||||||
|
Response::builder()
|
||||||
|
.status(failure.status)
|
||||||
|
.header("content-type", "application/json; charset=utf-8")
|
||||||
|
.body(Full::new(Bytes::from(body)))
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn read_json<T: DeserializeOwned>(body: Incoming, limit: usize) -> Result<T, ApiFailure> {
|
||||||
|
let bytes = read_body_with_limit(body, limit).await?;
|
||||||
|
serde_json::from_slice(&bytes).map_err(|_| ApiFailure::bad_request("Invalid JSON body"))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn read_optional_json<T: DeserializeOwned>(
|
||||||
|
body: Incoming,
|
||||||
|
limit: usize,
|
||||||
|
) -> Result<Option<T>, ApiFailure> {
|
||||||
|
let bytes = read_body_with_limit(body, limit).await?;
|
||||||
|
if bytes.is_empty() {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
serde_json::from_slice(&bytes)
|
||||||
|
.map(Some)
|
||||||
|
.map_err(|_| ApiFailure::bad_request("Invalid JSON body"))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn read_body_with_limit(body: Incoming, limit: usize) -> Result<Vec<u8>, ApiFailure> {
|
||||||
|
let mut collected = Vec::new();
|
||||||
|
let mut body = body;
|
||||||
|
while let Some(frame_result) = body.frame().await {
|
||||||
|
let frame = frame_result.map_err(|_| ApiFailure::bad_request("Invalid request body"))?;
|
||||||
|
if let Some(chunk) = frame.data_ref() {
|
||||||
|
if collected.len().saturating_add(chunk.len()) > limit {
|
||||||
|
return Err(ApiFailure::new(
|
||||||
|
StatusCode::PAYLOAD_TOO_LARGE,
|
||||||
|
"payload_too_large",
|
||||||
|
format!("Body exceeds {} bytes", limit),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
collected.extend_from_slice(chunk);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(collected)
|
||||||
|
}
|
||||||
439
src/api/model.rs
Normal file
439
src/api/model.rs
Normal file
@@ -0,0 +1,439 @@
|
|||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
use hyper::StatusCode;
|
||||||
|
use rand::Rng;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
const MAX_USERNAME_LEN: usize = 64;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub(super) struct ApiFailure {
|
||||||
|
pub(super) status: StatusCode,
|
||||||
|
pub(super) code: &'static str,
|
||||||
|
pub(super) message: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ApiFailure {
|
||||||
|
pub(super) fn new(status: StatusCode, code: &'static str, message: impl Into<String>) -> Self {
|
||||||
|
Self {
|
||||||
|
status,
|
||||||
|
code,
|
||||||
|
message: message.into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) fn internal(message: impl Into<String>) -> Self {
|
||||||
|
Self::new(StatusCode::INTERNAL_SERVER_ERROR, "internal_error", message)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) fn bad_request(message: impl Into<String>) -> Self {
|
||||||
|
Self::new(StatusCode::BAD_REQUEST, "bad_request", message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub(super) struct ErrorBody {
|
||||||
|
pub(super) code: &'static str,
|
||||||
|
pub(super) message: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub(super) struct ErrorResponse {
|
||||||
|
pub(super) ok: bool,
|
||||||
|
pub(super) error: ErrorBody,
|
||||||
|
pub(super) request_id: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub(super) struct SuccessResponse<T> {
|
||||||
|
pub(super) ok: bool,
|
||||||
|
pub(super) data: T,
|
||||||
|
pub(super) revision: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub(super) struct HealthData {
|
||||||
|
pub(super) status: &'static str,
|
||||||
|
pub(super) read_only: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub(super) struct SummaryData {
|
||||||
|
pub(super) uptime_seconds: f64,
|
||||||
|
pub(super) connections_total: u64,
|
||||||
|
pub(super) connections_bad_total: u64,
|
||||||
|
pub(super) handshake_timeouts_total: u64,
|
||||||
|
pub(super) configured_users: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Clone)]
|
||||||
|
pub(super) struct ZeroCodeCount {
|
||||||
|
pub(super) code: i32,
|
||||||
|
pub(super) total: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Clone)]
|
||||||
|
pub(super) struct ZeroCoreData {
|
||||||
|
pub(super) uptime_seconds: f64,
|
||||||
|
pub(super) connections_total: u64,
|
||||||
|
pub(super) connections_bad_total: u64,
|
||||||
|
pub(super) handshake_timeouts_total: u64,
|
||||||
|
pub(super) configured_users: usize,
|
||||||
|
pub(super) telemetry_core_enabled: bool,
|
||||||
|
pub(super) telemetry_user_enabled: bool,
|
||||||
|
pub(super) telemetry_me_level: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Clone)]
|
||||||
|
pub(super) struct ZeroUpstreamData {
|
||||||
|
pub(super) connect_attempt_total: u64,
|
||||||
|
pub(super) connect_success_total: u64,
|
||||||
|
pub(super) connect_fail_total: u64,
|
||||||
|
pub(super) connect_failfast_hard_error_total: u64,
|
||||||
|
pub(super) connect_attempts_bucket_1: u64,
|
||||||
|
pub(super) connect_attempts_bucket_2: u64,
|
||||||
|
pub(super) connect_attempts_bucket_3_4: u64,
|
||||||
|
pub(super) connect_attempts_bucket_gt_4: u64,
|
||||||
|
pub(super) connect_duration_success_bucket_le_100ms: u64,
|
||||||
|
pub(super) connect_duration_success_bucket_101_500ms: u64,
|
||||||
|
pub(super) connect_duration_success_bucket_501_1000ms: u64,
|
||||||
|
pub(super) connect_duration_success_bucket_gt_1000ms: u64,
|
||||||
|
pub(super) connect_duration_fail_bucket_le_100ms: u64,
|
||||||
|
pub(super) connect_duration_fail_bucket_101_500ms: u64,
|
||||||
|
pub(super) connect_duration_fail_bucket_501_1000ms: u64,
|
||||||
|
pub(super) connect_duration_fail_bucket_gt_1000ms: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Clone)]
|
||||||
|
pub(super) struct UpstreamDcStatus {
|
||||||
|
pub(super) dc: i16,
|
||||||
|
pub(super) latency_ema_ms: Option<f64>,
|
||||||
|
pub(super) ip_preference: &'static str,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Clone)]
|
||||||
|
pub(super) struct UpstreamStatus {
|
||||||
|
pub(super) upstream_id: usize,
|
||||||
|
pub(super) route_kind: &'static str,
|
||||||
|
pub(super) address: String,
|
||||||
|
pub(super) weight: u16,
|
||||||
|
pub(super) scopes: String,
|
||||||
|
pub(super) healthy: bool,
|
||||||
|
pub(super) fails: u32,
|
||||||
|
pub(super) last_check_age_secs: u64,
|
||||||
|
pub(super) effective_latency_ms: Option<f64>,
|
||||||
|
pub(super) dc: Vec<UpstreamDcStatus>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Clone)]
|
||||||
|
pub(super) struct UpstreamSummaryData {
|
||||||
|
pub(super) configured_total: usize,
|
||||||
|
pub(super) healthy_total: usize,
|
||||||
|
pub(super) unhealthy_total: usize,
|
||||||
|
pub(super) direct_total: usize,
|
||||||
|
pub(super) socks4_total: usize,
|
||||||
|
pub(super) socks5_total: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Clone)]
|
||||||
|
pub(super) struct UpstreamsData {
|
||||||
|
pub(super) enabled: bool,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub(super) reason: Option<&'static str>,
|
||||||
|
pub(super) generated_at_epoch_secs: u64,
|
||||||
|
pub(super) zero: ZeroUpstreamData,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub(super) summary: Option<UpstreamSummaryData>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub(super) upstreams: Option<Vec<UpstreamStatus>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Clone)]
|
||||||
|
pub(super) struct ZeroMiddleProxyData {
|
||||||
|
pub(super) keepalive_sent_total: u64,
|
||||||
|
pub(super) keepalive_failed_total: u64,
|
||||||
|
pub(super) keepalive_pong_total: u64,
|
||||||
|
pub(super) keepalive_timeout_total: u64,
|
||||||
|
pub(super) rpc_proxy_req_signal_sent_total: u64,
|
||||||
|
pub(super) rpc_proxy_req_signal_failed_total: u64,
|
||||||
|
pub(super) rpc_proxy_req_signal_skipped_no_meta_total: u64,
|
||||||
|
pub(super) rpc_proxy_req_signal_response_total: u64,
|
||||||
|
pub(super) rpc_proxy_req_signal_close_sent_total: u64,
|
||||||
|
pub(super) reconnect_attempt_total: u64,
|
||||||
|
pub(super) reconnect_success_total: u64,
|
||||||
|
pub(super) handshake_reject_total: u64,
|
||||||
|
pub(super) handshake_error_codes: Vec<ZeroCodeCount>,
|
||||||
|
pub(super) reader_eof_total: u64,
|
||||||
|
pub(super) idle_close_by_peer_total: u64,
|
||||||
|
pub(super) route_drop_no_conn_total: u64,
|
||||||
|
pub(super) route_drop_channel_closed_total: u64,
|
||||||
|
pub(super) route_drop_queue_full_total: u64,
|
||||||
|
pub(super) route_drop_queue_full_base_total: u64,
|
||||||
|
pub(super) route_drop_queue_full_high_total: u64,
|
||||||
|
pub(super) socks_kdf_strict_reject_total: u64,
|
||||||
|
pub(super) socks_kdf_compat_fallback_total: u64,
|
||||||
|
pub(super) endpoint_quarantine_total: u64,
|
||||||
|
pub(super) kdf_drift_total: u64,
|
||||||
|
pub(super) kdf_port_only_drift_total: u64,
|
||||||
|
pub(super) hardswap_pending_reuse_total: u64,
|
||||||
|
pub(super) hardswap_pending_ttl_expired_total: u64,
|
||||||
|
pub(super) single_endpoint_outage_enter_total: u64,
|
||||||
|
pub(super) single_endpoint_outage_exit_total: u64,
|
||||||
|
pub(super) single_endpoint_outage_reconnect_attempt_total: u64,
|
||||||
|
pub(super) single_endpoint_outage_reconnect_success_total: u64,
|
||||||
|
pub(super) single_endpoint_quarantine_bypass_total: u64,
|
||||||
|
pub(super) single_endpoint_shadow_rotate_total: u64,
|
||||||
|
pub(super) single_endpoint_shadow_rotate_skipped_quarantine_total: u64,
|
||||||
|
pub(super) floor_mode_switch_total: u64,
|
||||||
|
pub(super) floor_mode_switch_static_to_adaptive_total: u64,
|
||||||
|
pub(super) floor_mode_switch_adaptive_to_static_total: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Clone)]
|
||||||
|
pub(super) struct ZeroPoolData {
|
||||||
|
pub(super) pool_swap_total: u64,
|
||||||
|
pub(super) pool_drain_active: u64,
|
||||||
|
pub(super) pool_force_close_total: u64,
|
||||||
|
pub(super) pool_stale_pick_total: u64,
|
||||||
|
pub(super) writer_removed_total: u64,
|
||||||
|
pub(super) writer_removed_unexpected_total: u64,
|
||||||
|
pub(super) refill_triggered_total: u64,
|
||||||
|
pub(super) refill_skipped_inflight_total: u64,
|
||||||
|
pub(super) refill_failed_total: u64,
|
||||||
|
pub(super) writer_restored_same_endpoint_total: u64,
|
||||||
|
pub(super) writer_restored_fallback_total: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Clone)]
|
||||||
|
pub(super) struct ZeroDesyncData {
|
||||||
|
pub(super) secure_padding_invalid_total: u64,
|
||||||
|
pub(super) desync_total: u64,
|
||||||
|
pub(super) desync_full_logged_total: u64,
|
||||||
|
pub(super) desync_suppressed_total: u64,
|
||||||
|
pub(super) desync_frames_bucket_0: u64,
|
||||||
|
pub(super) desync_frames_bucket_1_2: u64,
|
||||||
|
pub(super) desync_frames_bucket_3_10: u64,
|
||||||
|
pub(super) desync_frames_bucket_gt_10: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Clone)]
|
||||||
|
pub(super) struct ZeroAllData {
|
||||||
|
pub(super) generated_at_epoch_secs: u64,
|
||||||
|
pub(super) core: ZeroCoreData,
|
||||||
|
pub(super) upstream: ZeroUpstreamData,
|
||||||
|
pub(super) middle_proxy: ZeroMiddleProxyData,
|
||||||
|
pub(super) pool: ZeroPoolData,
|
||||||
|
pub(super) desync: ZeroDesyncData,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Clone)]
|
||||||
|
pub(super) struct MeWritersSummary {
|
||||||
|
pub(super) configured_dc_groups: usize,
|
||||||
|
pub(super) configured_endpoints: usize,
|
||||||
|
pub(super) available_endpoints: usize,
|
||||||
|
pub(super) available_pct: f64,
|
||||||
|
pub(super) required_writers: usize,
|
||||||
|
pub(super) alive_writers: usize,
|
||||||
|
pub(super) coverage_pct: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Clone)]
|
||||||
|
pub(super) struct MeWriterStatus {
|
||||||
|
pub(super) writer_id: u64,
|
||||||
|
pub(super) dc: Option<i16>,
|
||||||
|
pub(super) endpoint: String,
|
||||||
|
pub(super) generation: u64,
|
||||||
|
pub(super) state: &'static str,
|
||||||
|
pub(super) draining: bool,
|
||||||
|
pub(super) degraded: bool,
|
||||||
|
pub(super) bound_clients: usize,
|
||||||
|
pub(super) idle_for_secs: Option<u64>,
|
||||||
|
pub(super) rtt_ema_ms: Option<f64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Clone)]
|
||||||
|
pub(super) struct MeWritersData {
|
||||||
|
pub(super) middle_proxy_enabled: bool,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub(super) reason: Option<&'static str>,
|
||||||
|
pub(super) generated_at_epoch_secs: u64,
|
||||||
|
pub(super) summary: MeWritersSummary,
|
||||||
|
pub(super) writers: Vec<MeWriterStatus>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Clone)]
|
||||||
|
pub(super) struct DcStatus {
|
||||||
|
pub(super) dc: i16,
|
||||||
|
pub(super) endpoints: Vec<String>,
|
||||||
|
pub(super) available_endpoints: usize,
|
||||||
|
pub(super) available_pct: f64,
|
||||||
|
pub(super) required_writers: usize,
|
||||||
|
pub(super) alive_writers: usize,
|
||||||
|
pub(super) coverage_pct: f64,
|
||||||
|
pub(super) rtt_ms: Option<f64>,
|
||||||
|
pub(super) load: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Clone)]
|
||||||
|
pub(super) struct DcStatusData {
|
||||||
|
pub(super) middle_proxy_enabled: bool,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub(super) reason: Option<&'static str>,
|
||||||
|
pub(super) generated_at_epoch_secs: u64,
|
||||||
|
pub(super) dcs: Vec<DcStatus>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Clone)]
|
||||||
|
pub(super) struct MinimalQuarantineData {
|
||||||
|
pub(super) endpoint: String,
|
||||||
|
pub(super) remaining_ms: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Clone)]
|
||||||
|
pub(super) struct MinimalDcPathData {
|
||||||
|
pub(super) dc: i16,
|
||||||
|
pub(super) ip_preference: Option<&'static str>,
|
||||||
|
pub(super) selected_addr_v4: Option<String>,
|
||||||
|
pub(super) selected_addr_v6: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Clone)]
|
||||||
|
pub(super) struct MinimalMeRuntimeData {
|
||||||
|
pub(super) active_generation: u64,
|
||||||
|
pub(super) warm_generation: u64,
|
||||||
|
pub(super) pending_hardswap_generation: u64,
|
||||||
|
pub(super) pending_hardswap_age_secs: Option<u64>,
|
||||||
|
pub(super) hardswap_enabled: bool,
|
||||||
|
pub(super) floor_mode: &'static str,
|
||||||
|
pub(super) adaptive_floor_idle_secs: u64,
|
||||||
|
pub(super) adaptive_floor_min_writers_single_endpoint: u8,
|
||||||
|
pub(super) adaptive_floor_recover_grace_secs: u64,
|
||||||
|
pub(super) me_keepalive_enabled: bool,
|
||||||
|
pub(super) me_keepalive_interval_secs: u64,
|
||||||
|
pub(super) me_keepalive_jitter_secs: u64,
|
||||||
|
pub(super) me_keepalive_payload_random: bool,
|
||||||
|
pub(super) rpc_proxy_req_every_secs: u64,
|
||||||
|
pub(super) me_reconnect_max_concurrent_per_dc: u32,
|
||||||
|
pub(super) me_reconnect_backoff_base_ms: u64,
|
||||||
|
pub(super) me_reconnect_backoff_cap_ms: u64,
|
||||||
|
pub(super) me_reconnect_fast_retry_count: u32,
|
||||||
|
pub(super) me_pool_drain_ttl_secs: u64,
|
||||||
|
pub(super) me_pool_force_close_secs: u64,
|
||||||
|
pub(super) me_pool_min_fresh_ratio: f32,
|
||||||
|
pub(super) me_bind_stale_mode: &'static str,
|
||||||
|
pub(super) me_bind_stale_ttl_secs: u64,
|
||||||
|
pub(super) me_single_endpoint_shadow_writers: u8,
|
||||||
|
pub(super) me_single_endpoint_outage_mode_enabled: bool,
|
||||||
|
pub(super) me_single_endpoint_outage_disable_quarantine: bool,
|
||||||
|
pub(super) me_single_endpoint_outage_backoff_min_ms: u64,
|
||||||
|
pub(super) me_single_endpoint_outage_backoff_max_ms: u64,
|
||||||
|
pub(super) me_single_endpoint_shadow_rotate_every_secs: u64,
|
||||||
|
pub(super) me_deterministic_writer_sort: bool,
|
||||||
|
pub(super) me_socks_kdf_policy: &'static str,
|
||||||
|
pub(super) quarantined_endpoints_total: usize,
|
||||||
|
pub(super) quarantined_endpoints: Vec<MinimalQuarantineData>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Clone)]
|
||||||
|
pub(super) struct MinimalAllPayload {
|
||||||
|
pub(super) me_writers: MeWritersData,
|
||||||
|
pub(super) dcs: DcStatusData,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub(super) me_runtime: Option<MinimalMeRuntimeData>,
|
||||||
|
pub(super) network_path: Vec<MinimalDcPathData>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Clone)]
|
||||||
|
pub(super) struct MinimalAllData {
|
||||||
|
pub(super) enabled: bool,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub(super) reason: Option<&'static str>,
|
||||||
|
pub(super) generated_at_epoch_secs: u64,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub(super) data: Option<MinimalAllPayload>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub(super) struct UserLinks {
|
||||||
|
pub(super) classic: Vec<String>,
|
||||||
|
pub(super) secure: Vec<String>,
|
||||||
|
pub(super) tls: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub(super) struct UserInfo {
|
||||||
|
pub(super) username: String,
|
||||||
|
pub(super) user_ad_tag: Option<String>,
|
||||||
|
pub(super) max_tcp_conns: Option<usize>,
|
||||||
|
pub(super) expiration_rfc3339: Option<String>,
|
||||||
|
pub(super) data_quota_bytes: Option<u64>,
|
||||||
|
pub(super) max_unique_ips: Option<usize>,
|
||||||
|
pub(super) current_connections: u64,
|
||||||
|
pub(super) active_unique_ips: usize,
|
||||||
|
pub(super) total_octets: u64,
|
||||||
|
pub(super) links: UserLinks,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub(super) struct CreateUserResponse {
|
||||||
|
pub(super) user: UserInfo,
|
||||||
|
pub(super) secret: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub(super) struct CreateUserRequest {
|
||||||
|
pub(super) username: String,
|
||||||
|
pub(super) secret: Option<String>,
|
||||||
|
pub(super) user_ad_tag: Option<String>,
|
||||||
|
pub(super) max_tcp_conns: Option<usize>,
|
||||||
|
pub(super) expiration_rfc3339: Option<String>,
|
||||||
|
pub(super) data_quota_bytes: Option<u64>,
|
||||||
|
pub(super) max_unique_ips: Option<usize>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub(super) struct PatchUserRequest {
|
||||||
|
pub(super) secret: Option<String>,
|
||||||
|
pub(super) user_ad_tag: Option<String>,
|
||||||
|
pub(super) max_tcp_conns: Option<usize>,
|
||||||
|
pub(super) expiration_rfc3339: Option<String>,
|
||||||
|
pub(super) data_quota_bytes: Option<u64>,
|
||||||
|
pub(super) max_unique_ips: Option<usize>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Deserialize)]
|
||||||
|
pub(super) struct RotateSecretRequest {
|
||||||
|
pub(super) secret: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) fn parse_optional_expiration(
|
||||||
|
value: Option<&str>,
|
||||||
|
) -> Result<Option<DateTime<Utc>>, ApiFailure> {
|
||||||
|
let Some(raw) = value else {
|
||||||
|
return Ok(None);
|
||||||
|
};
|
||||||
|
let parsed = DateTime::parse_from_rfc3339(raw)
|
||||||
|
.map_err(|_| ApiFailure::bad_request("expiration_rfc3339 must be valid RFC3339"))?;
|
||||||
|
Ok(Some(parsed.with_timezone(&Utc)))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) fn is_valid_user_secret(secret: &str) -> bool {
|
||||||
|
secret.len() == 32 && secret.chars().all(|c| c.is_ascii_hexdigit())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) fn is_valid_ad_tag(tag: &str) -> bool {
|
||||||
|
tag.len() == 32 && tag.chars().all(|c| c.is_ascii_hexdigit())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) fn is_valid_username(user: &str) -> bool {
|
||||||
|
!user.is_empty()
|
||||||
|
&& user.len() <= MAX_USERNAME_LEN
|
||||||
|
&& user
|
||||||
|
.chars()
|
||||||
|
.all(|ch| ch.is_ascii_alphanumeric() || matches!(ch, '_' | '-' | '.'))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) fn random_user_secret() -> String {
|
||||||
|
let mut bytes = [0u8; 16];
|
||||||
|
rand::rng().fill(&mut bytes);
|
||||||
|
hex::encode(bytes)
|
||||||
|
}
|
||||||
484
src/api/runtime_stats.rs
Normal file
484
src/api/runtime_stats.rs
Normal file
@@ -0,0 +1,484 @@
|
|||||||
|
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
|
use crate::config::ApiConfig;
|
||||||
|
use crate::stats::Stats;
|
||||||
|
use crate::transport::upstream::IpPreference;
|
||||||
|
use crate::transport::UpstreamRouteKind;
|
||||||
|
|
||||||
|
use super::ApiShared;
|
||||||
|
use super::model::{
|
||||||
|
DcStatus, DcStatusData, MeWriterStatus, MeWritersData, MeWritersSummary, MinimalAllData,
|
||||||
|
MinimalAllPayload, MinimalDcPathData, MinimalMeRuntimeData, MinimalQuarantineData,
|
||||||
|
UpstreamDcStatus, UpstreamStatus, UpstreamSummaryData, UpstreamsData, ZeroAllData,
|
||||||
|
ZeroCodeCount, ZeroCoreData, ZeroDesyncData, ZeroMiddleProxyData, ZeroPoolData,
|
||||||
|
ZeroUpstreamData,
|
||||||
|
};
|
||||||
|
|
||||||
|
const FEATURE_DISABLED_REASON: &str = "feature_disabled";
|
||||||
|
const SOURCE_UNAVAILABLE_REASON: &str = "source_unavailable";
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub(crate) struct MinimalCacheEntry {
|
||||||
|
pub(super) expires_at: Instant,
|
||||||
|
pub(super) payload: MinimalAllPayload,
|
||||||
|
pub(super) generated_at_epoch_secs: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) fn build_zero_all_data(stats: &Stats, configured_users: usize) -> ZeroAllData {
|
||||||
|
let telemetry = stats.telemetry_policy();
|
||||||
|
let handshake_error_codes = stats
|
||||||
|
.get_me_handshake_error_code_counts()
|
||||||
|
.into_iter()
|
||||||
|
.map(|(code, total)| ZeroCodeCount { code, total })
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
ZeroAllData {
|
||||||
|
generated_at_epoch_secs: now_epoch_secs(),
|
||||||
|
core: ZeroCoreData {
|
||||||
|
uptime_seconds: stats.uptime_secs(),
|
||||||
|
connections_total: stats.get_connects_all(),
|
||||||
|
connections_bad_total: stats.get_connects_bad(),
|
||||||
|
handshake_timeouts_total: stats.get_handshake_timeouts(),
|
||||||
|
configured_users,
|
||||||
|
telemetry_core_enabled: telemetry.core_enabled,
|
||||||
|
telemetry_user_enabled: telemetry.user_enabled,
|
||||||
|
telemetry_me_level: telemetry.me_level.to_string(),
|
||||||
|
},
|
||||||
|
upstream: build_zero_upstream_data(stats),
|
||||||
|
middle_proxy: ZeroMiddleProxyData {
|
||||||
|
keepalive_sent_total: stats.get_me_keepalive_sent(),
|
||||||
|
keepalive_failed_total: stats.get_me_keepalive_failed(),
|
||||||
|
keepalive_pong_total: stats.get_me_keepalive_pong(),
|
||||||
|
keepalive_timeout_total: stats.get_me_keepalive_timeout(),
|
||||||
|
rpc_proxy_req_signal_sent_total: stats.get_me_rpc_proxy_req_signal_sent_total(),
|
||||||
|
rpc_proxy_req_signal_failed_total: stats.get_me_rpc_proxy_req_signal_failed_total(),
|
||||||
|
rpc_proxy_req_signal_skipped_no_meta_total: stats
|
||||||
|
.get_me_rpc_proxy_req_signal_skipped_no_meta_total(),
|
||||||
|
rpc_proxy_req_signal_response_total: stats.get_me_rpc_proxy_req_signal_response_total(),
|
||||||
|
rpc_proxy_req_signal_close_sent_total: stats
|
||||||
|
.get_me_rpc_proxy_req_signal_close_sent_total(),
|
||||||
|
reconnect_attempt_total: stats.get_me_reconnect_attempts(),
|
||||||
|
reconnect_success_total: stats.get_me_reconnect_success(),
|
||||||
|
handshake_reject_total: stats.get_me_handshake_reject_total(),
|
||||||
|
handshake_error_codes,
|
||||||
|
reader_eof_total: stats.get_me_reader_eof_total(),
|
||||||
|
idle_close_by_peer_total: stats.get_me_idle_close_by_peer_total(),
|
||||||
|
route_drop_no_conn_total: stats.get_me_route_drop_no_conn(),
|
||||||
|
route_drop_channel_closed_total: stats.get_me_route_drop_channel_closed(),
|
||||||
|
route_drop_queue_full_total: stats.get_me_route_drop_queue_full(),
|
||||||
|
route_drop_queue_full_base_total: stats.get_me_route_drop_queue_full_base(),
|
||||||
|
route_drop_queue_full_high_total: stats.get_me_route_drop_queue_full_high(),
|
||||||
|
socks_kdf_strict_reject_total: stats.get_me_socks_kdf_strict_reject(),
|
||||||
|
socks_kdf_compat_fallback_total: stats.get_me_socks_kdf_compat_fallback(),
|
||||||
|
endpoint_quarantine_total: stats.get_me_endpoint_quarantine_total(),
|
||||||
|
kdf_drift_total: stats.get_me_kdf_drift_total(),
|
||||||
|
kdf_port_only_drift_total: stats.get_me_kdf_port_only_drift_total(),
|
||||||
|
hardswap_pending_reuse_total: stats.get_me_hardswap_pending_reuse_total(),
|
||||||
|
hardswap_pending_ttl_expired_total: stats.get_me_hardswap_pending_ttl_expired_total(),
|
||||||
|
single_endpoint_outage_enter_total: stats.get_me_single_endpoint_outage_enter_total(),
|
||||||
|
single_endpoint_outage_exit_total: stats.get_me_single_endpoint_outage_exit_total(),
|
||||||
|
single_endpoint_outage_reconnect_attempt_total: stats
|
||||||
|
.get_me_single_endpoint_outage_reconnect_attempt_total(),
|
||||||
|
single_endpoint_outage_reconnect_success_total: stats
|
||||||
|
.get_me_single_endpoint_outage_reconnect_success_total(),
|
||||||
|
single_endpoint_quarantine_bypass_total: stats
|
||||||
|
.get_me_single_endpoint_quarantine_bypass_total(),
|
||||||
|
single_endpoint_shadow_rotate_total: stats.get_me_single_endpoint_shadow_rotate_total(),
|
||||||
|
single_endpoint_shadow_rotate_skipped_quarantine_total: stats
|
||||||
|
.get_me_single_endpoint_shadow_rotate_skipped_quarantine_total(),
|
||||||
|
floor_mode_switch_total: stats.get_me_floor_mode_switch_total(),
|
||||||
|
floor_mode_switch_static_to_adaptive_total: stats
|
||||||
|
.get_me_floor_mode_switch_static_to_adaptive_total(),
|
||||||
|
floor_mode_switch_adaptive_to_static_total: stats
|
||||||
|
.get_me_floor_mode_switch_adaptive_to_static_total(),
|
||||||
|
},
|
||||||
|
pool: ZeroPoolData {
|
||||||
|
pool_swap_total: stats.get_pool_swap_total(),
|
||||||
|
pool_drain_active: stats.get_pool_drain_active(),
|
||||||
|
pool_force_close_total: stats.get_pool_force_close_total(),
|
||||||
|
pool_stale_pick_total: stats.get_pool_stale_pick_total(),
|
||||||
|
writer_removed_total: stats.get_me_writer_removed_total(),
|
||||||
|
writer_removed_unexpected_total: stats.get_me_writer_removed_unexpected_total(),
|
||||||
|
refill_triggered_total: stats.get_me_refill_triggered_total(),
|
||||||
|
refill_skipped_inflight_total: stats.get_me_refill_skipped_inflight_total(),
|
||||||
|
refill_failed_total: stats.get_me_refill_failed_total(),
|
||||||
|
writer_restored_same_endpoint_total: stats.get_me_writer_restored_same_endpoint_total(),
|
||||||
|
writer_restored_fallback_total: stats.get_me_writer_restored_fallback_total(),
|
||||||
|
},
|
||||||
|
desync: ZeroDesyncData {
|
||||||
|
secure_padding_invalid_total: stats.get_secure_padding_invalid(),
|
||||||
|
desync_total: stats.get_desync_total(),
|
||||||
|
desync_full_logged_total: stats.get_desync_full_logged(),
|
||||||
|
desync_suppressed_total: stats.get_desync_suppressed(),
|
||||||
|
desync_frames_bucket_0: stats.get_desync_frames_bucket_0(),
|
||||||
|
desync_frames_bucket_1_2: stats.get_desync_frames_bucket_1_2(),
|
||||||
|
desync_frames_bucket_3_10: stats.get_desync_frames_bucket_3_10(),
|
||||||
|
desync_frames_bucket_gt_10: stats.get_desync_frames_bucket_gt_10(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_zero_upstream_data(stats: &Stats) -> ZeroUpstreamData {
|
||||||
|
ZeroUpstreamData {
|
||||||
|
connect_attempt_total: stats.get_upstream_connect_attempt_total(),
|
||||||
|
connect_success_total: stats.get_upstream_connect_success_total(),
|
||||||
|
connect_fail_total: stats.get_upstream_connect_fail_total(),
|
||||||
|
connect_failfast_hard_error_total: stats.get_upstream_connect_failfast_hard_error_total(),
|
||||||
|
connect_attempts_bucket_1: stats.get_upstream_connect_attempts_bucket_1(),
|
||||||
|
connect_attempts_bucket_2: stats.get_upstream_connect_attempts_bucket_2(),
|
||||||
|
connect_attempts_bucket_3_4: stats.get_upstream_connect_attempts_bucket_3_4(),
|
||||||
|
connect_attempts_bucket_gt_4: stats.get_upstream_connect_attempts_bucket_gt_4(),
|
||||||
|
connect_duration_success_bucket_le_100ms: stats
|
||||||
|
.get_upstream_connect_duration_success_bucket_le_100ms(),
|
||||||
|
connect_duration_success_bucket_101_500ms: stats
|
||||||
|
.get_upstream_connect_duration_success_bucket_101_500ms(),
|
||||||
|
connect_duration_success_bucket_501_1000ms: stats
|
||||||
|
.get_upstream_connect_duration_success_bucket_501_1000ms(),
|
||||||
|
connect_duration_success_bucket_gt_1000ms: stats
|
||||||
|
.get_upstream_connect_duration_success_bucket_gt_1000ms(),
|
||||||
|
connect_duration_fail_bucket_le_100ms: stats.get_upstream_connect_duration_fail_bucket_le_100ms(),
|
||||||
|
connect_duration_fail_bucket_101_500ms: stats
|
||||||
|
.get_upstream_connect_duration_fail_bucket_101_500ms(),
|
||||||
|
connect_duration_fail_bucket_501_1000ms: stats
|
||||||
|
.get_upstream_connect_duration_fail_bucket_501_1000ms(),
|
||||||
|
connect_duration_fail_bucket_gt_1000ms: stats
|
||||||
|
.get_upstream_connect_duration_fail_bucket_gt_1000ms(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) fn build_upstreams_data(shared: &ApiShared, api_cfg: &ApiConfig) -> UpstreamsData {
|
||||||
|
let generated_at_epoch_secs = now_epoch_secs();
|
||||||
|
let zero = build_zero_upstream_data(&shared.stats);
|
||||||
|
if !api_cfg.minimal_runtime_enabled {
|
||||||
|
return UpstreamsData {
|
||||||
|
enabled: false,
|
||||||
|
reason: Some(FEATURE_DISABLED_REASON),
|
||||||
|
generated_at_epoch_secs,
|
||||||
|
zero,
|
||||||
|
summary: None,
|
||||||
|
upstreams: None,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
let Some(snapshot) = shared.upstream_manager.try_api_snapshot() else {
|
||||||
|
return UpstreamsData {
|
||||||
|
enabled: true,
|
||||||
|
reason: Some(SOURCE_UNAVAILABLE_REASON),
|
||||||
|
generated_at_epoch_secs,
|
||||||
|
zero,
|
||||||
|
summary: None,
|
||||||
|
upstreams: None,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
let summary = UpstreamSummaryData {
|
||||||
|
configured_total: snapshot.summary.configured_total,
|
||||||
|
healthy_total: snapshot.summary.healthy_total,
|
||||||
|
unhealthy_total: snapshot.summary.unhealthy_total,
|
||||||
|
direct_total: snapshot.summary.direct_total,
|
||||||
|
socks4_total: snapshot.summary.socks4_total,
|
||||||
|
socks5_total: snapshot.summary.socks5_total,
|
||||||
|
};
|
||||||
|
let upstreams = snapshot
|
||||||
|
.upstreams
|
||||||
|
.into_iter()
|
||||||
|
.map(|upstream| UpstreamStatus {
|
||||||
|
upstream_id: upstream.upstream_id,
|
||||||
|
route_kind: map_route_kind(upstream.route_kind),
|
||||||
|
address: upstream.address,
|
||||||
|
weight: upstream.weight,
|
||||||
|
scopes: upstream.scopes,
|
||||||
|
healthy: upstream.healthy,
|
||||||
|
fails: upstream.fails,
|
||||||
|
last_check_age_secs: upstream.last_check_age_secs,
|
||||||
|
effective_latency_ms: upstream.effective_latency_ms,
|
||||||
|
dc: upstream
|
||||||
|
.dc
|
||||||
|
.into_iter()
|
||||||
|
.map(|dc| UpstreamDcStatus {
|
||||||
|
dc: dc.dc,
|
||||||
|
latency_ema_ms: dc.latency_ema_ms,
|
||||||
|
ip_preference: map_ip_preference(dc.ip_preference),
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
UpstreamsData {
|
||||||
|
enabled: true,
|
||||||
|
reason: None,
|
||||||
|
generated_at_epoch_secs,
|
||||||
|
zero,
|
||||||
|
summary: Some(summary),
|
||||||
|
upstreams: Some(upstreams),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) async fn build_minimal_all_data(
|
||||||
|
shared: &ApiShared,
|
||||||
|
api_cfg: &ApiConfig,
|
||||||
|
) -> MinimalAllData {
|
||||||
|
let now = now_epoch_secs();
|
||||||
|
if !api_cfg.minimal_runtime_enabled {
|
||||||
|
return MinimalAllData {
|
||||||
|
enabled: false,
|
||||||
|
reason: Some(FEATURE_DISABLED_REASON),
|
||||||
|
generated_at_epoch_secs: now,
|
||||||
|
data: None,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
let Some((generated_at_epoch_secs, payload)) =
|
||||||
|
get_minimal_payload_cached(shared, api_cfg.minimal_runtime_cache_ttl_ms).await
|
||||||
|
else {
|
||||||
|
return MinimalAllData {
|
||||||
|
enabled: true,
|
||||||
|
reason: Some(SOURCE_UNAVAILABLE_REASON),
|
||||||
|
generated_at_epoch_secs: now,
|
||||||
|
data: Some(MinimalAllPayload {
|
||||||
|
me_writers: disabled_me_writers(now, SOURCE_UNAVAILABLE_REASON),
|
||||||
|
dcs: disabled_dcs(now, SOURCE_UNAVAILABLE_REASON),
|
||||||
|
me_runtime: None,
|
||||||
|
network_path: Vec::new(),
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
MinimalAllData {
|
||||||
|
enabled: true,
|
||||||
|
reason: None,
|
||||||
|
generated_at_epoch_secs,
|
||||||
|
data: Some(payload),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) async fn build_me_writers_data(
|
||||||
|
shared: &ApiShared,
|
||||||
|
api_cfg: &ApiConfig,
|
||||||
|
) -> MeWritersData {
|
||||||
|
let now = now_epoch_secs();
|
||||||
|
if !api_cfg.minimal_runtime_enabled {
|
||||||
|
return disabled_me_writers(now, FEATURE_DISABLED_REASON);
|
||||||
|
}
|
||||||
|
|
||||||
|
let Some((_, payload)) =
|
||||||
|
get_minimal_payload_cached(shared, api_cfg.minimal_runtime_cache_ttl_ms).await
|
||||||
|
else {
|
||||||
|
return disabled_me_writers(now, SOURCE_UNAVAILABLE_REASON);
|
||||||
|
};
|
||||||
|
payload.me_writers
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) async fn build_dcs_data(shared: &ApiShared, api_cfg: &ApiConfig) -> DcStatusData {
|
||||||
|
let now = now_epoch_secs();
|
||||||
|
if !api_cfg.minimal_runtime_enabled {
|
||||||
|
return disabled_dcs(now, FEATURE_DISABLED_REASON);
|
||||||
|
}
|
||||||
|
|
||||||
|
let Some((_, payload)) =
|
||||||
|
get_minimal_payload_cached(shared, api_cfg.minimal_runtime_cache_ttl_ms).await
|
||||||
|
else {
|
||||||
|
return disabled_dcs(now, SOURCE_UNAVAILABLE_REASON);
|
||||||
|
};
|
||||||
|
payload.dcs
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_minimal_payload_cached(
|
||||||
|
shared: &ApiShared,
|
||||||
|
cache_ttl_ms: u64,
|
||||||
|
) -> Option<(u64, MinimalAllPayload)> {
|
||||||
|
if cache_ttl_ms > 0 {
|
||||||
|
let now = Instant::now();
|
||||||
|
let cached = shared.minimal_cache.lock().await.clone();
|
||||||
|
if let Some(entry) = cached
|
||||||
|
&& now < entry.expires_at
|
||||||
|
{
|
||||||
|
return Some((entry.generated_at_epoch_secs, entry.payload));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let pool = shared.me_pool.as_ref()?;
|
||||||
|
let status = pool.api_status_snapshot().await;
|
||||||
|
let runtime = pool.api_runtime_snapshot().await;
|
||||||
|
let generated_at_epoch_secs = status.generated_at_epoch_secs;
|
||||||
|
|
||||||
|
let me_writers = MeWritersData {
|
||||||
|
middle_proxy_enabled: true,
|
||||||
|
reason: None,
|
||||||
|
generated_at_epoch_secs,
|
||||||
|
summary: MeWritersSummary {
|
||||||
|
configured_dc_groups: status.configured_dc_groups,
|
||||||
|
configured_endpoints: status.configured_endpoints,
|
||||||
|
available_endpoints: status.available_endpoints,
|
||||||
|
available_pct: status.available_pct,
|
||||||
|
required_writers: status.required_writers,
|
||||||
|
alive_writers: status.alive_writers,
|
||||||
|
coverage_pct: status.coverage_pct,
|
||||||
|
},
|
||||||
|
writers: status
|
||||||
|
.writers
|
||||||
|
.into_iter()
|
||||||
|
.map(|entry| MeWriterStatus {
|
||||||
|
writer_id: entry.writer_id,
|
||||||
|
dc: entry.dc,
|
||||||
|
endpoint: entry.endpoint.to_string(),
|
||||||
|
generation: entry.generation,
|
||||||
|
state: entry.state,
|
||||||
|
draining: entry.draining,
|
||||||
|
degraded: entry.degraded,
|
||||||
|
bound_clients: entry.bound_clients,
|
||||||
|
idle_for_secs: entry.idle_for_secs,
|
||||||
|
rtt_ema_ms: entry.rtt_ema_ms,
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
};
|
||||||
|
let dcs = DcStatusData {
|
||||||
|
middle_proxy_enabled: true,
|
||||||
|
reason: None,
|
||||||
|
generated_at_epoch_secs,
|
||||||
|
dcs: status
|
||||||
|
.dcs
|
||||||
|
.into_iter()
|
||||||
|
.map(|entry| DcStatus {
|
||||||
|
dc: entry.dc,
|
||||||
|
endpoints: entry
|
||||||
|
.endpoints
|
||||||
|
.into_iter()
|
||||||
|
.map(|value| value.to_string())
|
||||||
|
.collect(),
|
||||||
|
available_endpoints: entry.available_endpoints,
|
||||||
|
available_pct: entry.available_pct,
|
||||||
|
required_writers: entry.required_writers,
|
||||||
|
alive_writers: entry.alive_writers,
|
||||||
|
coverage_pct: entry.coverage_pct,
|
||||||
|
rtt_ms: entry.rtt_ms,
|
||||||
|
load: entry.load,
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
};
|
||||||
|
let me_runtime = MinimalMeRuntimeData {
|
||||||
|
active_generation: runtime.active_generation,
|
||||||
|
warm_generation: runtime.warm_generation,
|
||||||
|
pending_hardswap_generation: runtime.pending_hardswap_generation,
|
||||||
|
pending_hardswap_age_secs: runtime.pending_hardswap_age_secs,
|
||||||
|
hardswap_enabled: runtime.hardswap_enabled,
|
||||||
|
floor_mode: runtime.floor_mode,
|
||||||
|
adaptive_floor_idle_secs: runtime.adaptive_floor_idle_secs,
|
||||||
|
adaptive_floor_min_writers_single_endpoint: runtime
|
||||||
|
.adaptive_floor_min_writers_single_endpoint,
|
||||||
|
adaptive_floor_recover_grace_secs: runtime.adaptive_floor_recover_grace_secs,
|
||||||
|
me_keepalive_enabled: runtime.me_keepalive_enabled,
|
||||||
|
me_keepalive_interval_secs: runtime.me_keepalive_interval_secs,
|
||||||
|
me_keepalive_jitter_secs: runtime.me_keepalive_jitter_secs,
|
||||||
|
me_keepalive_payload_random: runtime.me_keepalive_payload_random,
|
||||||
|
rpc_proxy_req_every_secs: runtime.rpc_proxy_req_every_secs,
|
||||||
|
me_reconnect_max_concurrent_per_dc: runtime.me_reconnect_max_concurrent_per_dc,
|
||||||
|
me_reconnect_backoff_base_ms: runtime.me_reconnect_backoff_base_ms,
|
||||||
|
me_reconnect_backoff_cap_ms: runtime.me_reconnect_backoff_cap_ms,
|
||||||
|
me_reconnect_fast_retry_count: runtime.me_reconnect_fast_retry_count,
|
||||||
|
me_pool_drain_ttl_secs: runtime.me_pool_drain_ttl_secs,
|
||||||
|
me_pool_force_close_secs: runtime.me_pool_force_close_secs,
|
||||||
|
me_pool_min_fresh_ratio: runtime.me_pool_min_fresh_ratio,
|
||||||
|
me_bind_stale_mode: runtime.me_bind_stale_mode,
|
||||||
|
me_bind_stale_ttl_secs: runtime.me_bind_stale_ttl_secs,
|
||||||
|
me_single_endpoint_shadow_writers: runtime.me_single_endpoint_shadow_writers,
|
||||||
|
me_single_endpoint_outage_mode_enabled: runtime.me_single_endpoint_outage_mode_enabled,
|
||||||
|
me_single_endpoint_outage_disable_quarantine: runtime
|
||||||
|
.me_single_endpoint_outage_disable_quarantine,
|
||||||
|
me_single_endpoint_outage_backoff_min_ms: runtime.me_single_endpoint_outage_backoff_min_ms,
|
||||||
|
me_single_endpoint_outage_backoff_max_ms: runtime.me_single_endpoint_outage_backoff_max_ms,
|
||||||
|
me_single_endpoint_shadow_rotate_every_secs: runtime
|
||||||
|
.me_single_endpoint_shadow_rotate_every_secs,
|
||||||
|
me_deterministic_writer_sort: runtime.me_deterministic_writer_sort,
|
||||||
|
me_socks_kdf_policy: runtime.me_socks_kdf_policy,
|
||||||
|
quarantined_endpoints_total: runtime.quarantined_endpoints.len(),
|
||||||
|
quarantined_endpoints: runtime
|
||||||
|
.quarantined_endpoints
|
||||||
|
.into_iter()
|
||||||
|
.map(|entry| MinimalQuarantineData {
|
||||||
|
endpoint: entry.endpoint.to_string(),
|
||||||
|
remaining_ms: entry.remaining_ms,
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
};
|
||||||
|
let network_path = runtime
|
||||||
|
.network_path
|
||||||
|
.into_iter()
|
||||||
|
.map(|entry| MinimalDcPathData {
|
||||||
|
dc: entry.dc,
|
||||||
|
ip_preference: entry.ip_preference,
|
||||||
|
selected_addr_v4: entry.selected_addr_v4.map(|value| value.to_string()),
|
||||||
|
selected_addr_v6: entry.selected_addr_v6.map(|value| value.to_string()),
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let payload = MinimalAllPayload {
|
||||||
|
me_writers,
|
||||||
|
dcs,
|
||||||
|
me_runtime: Some(me_runtime),
|
||||||
|
network_path,
|
||||||
|
};
|
||||||
|
|
||||||
|
if cache_ttl_ms > 0 {
|
||||||
|
let entry = MinimalCacheEntry {
|
||||||
|
expires_at: Instant::now() + Duration::from_millis(cache_ttl_ms),
|
||||||
|
payload: payload.clone(),
|
||||||
|
generated_at_epoch_secs,
|
||||||
|
};
|
||||||
|
*shared.minimal_cache.lock().await = Some(entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
Some((generated_at_epoch_secs, payload))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn disabled_me_writers(now_epoch_secs: u64, reason: &'static str) -> MeWritersData {
|
||||||
|
MeWritersData {
|
||||||
|
middle_proxy_enabled: false,
|
||||||
|
reason: Some(reason),
|
||||||
|
generated_at_epoch_secs: now_epoch_secs,
|
||||||
|
summary: MeWritersSummary {
|
||||||
|
configured_dc_groups: 0,
|
||||||
|
configured_endpoints: 0,
|
||||||
|
available_endpoints: 0,
|
||||||
|
available_pct: 0.0,
|
||||||
|
required_writers: 0,
|
||||||
|
alive_writers: 0,
|
||||||
|
coverage_pct: 0.0,
|
||||||
|
},
|
||||||
|
writers: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn disabled_dcs(now_epoch_secs: u64, reason: &'static str) -> DcStatusData {
|
||||||
|
DcStatusData {
|
||||||
|
middle_proxy_enabled: false,
|
||||||
|
reason: Some(reason),
|
||||||
|
generated_at_epoch_secs: now_epoch_secs,
|
||||||
|
dcs: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn map_route_kind(value: UpstreamRouteKind) -> &'static str {
|
||||||
|
match value {
|
||||||
|
UpstreamRouteKind::Direct => "direct",
|
||||||
|
UpstreamRouteKind::Socks4 => "socks4",
|
||||||
|
UpstreamRouteKind::Socks5 => "socks5",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn map_ip_preference(value: IpPreference) -> &'static str {
|
||||||
|
match value {
|
||||||
|
IpPreference::Unknown => "unknown",
|
||||||
|
IpPreference::PreferV6 => "prefer_v6",
|
||||||
|
IpPreference::PreferV4 => "prefer_v4",
|
||||||
|
IpPreference::BothWork => "both_work",
|
||||||
|
IpPreference::Unavailable => "unavailable",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn now_epoch_secs() -> u64 {
|
||||||
|
SystemTime::now()
|
||||||
|
.duration_since(UNIX_EPOCH)
|
||||||
|
.unwrap_or_default()
|
||||||
|
.as_secs()
|
||||||
|
}
|
||||||
490
src/api/users.rs
Normal file
490
src/api/users.rs
Normal file
@@ -0,0 +1,490 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
use std::net::IpAddr;
|
||||||
|
|
||||||
|
use hyper::StatusCode;
|
||||||
|
|
||||||
|
use crate::config::ProxyConfig;
|
||||||
|
use crate::ip_tracker::UserIpTracker;
|
||||||
|
use crate::stats::Stats;
|
||||||
|
|
||||||
|
use super::ApiShared;
|
||||||
|
use super::config_store::{
|
||||||
|
ensure_expected_revision, load_config_from_disk, save_config_to_disk,
|
||||||
|
};
|
||||||
|
use super::model::{
|
||||||
|
ApiFailure, CreateUserRequest, CreateUserResponse, PatchUserRequest, RotateSecretRequest,
|
||||||
|
UserInfo, UserLinks, is_valid_ad_tag, is_valid_user_secret, is_valid_username,
|
||||||
|
parse_optional_expiration, random_user_secret,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub(super) async fn create_user(
|
||||||
|
body: CreateUserRequest,
|
||||||
|
expected_revision: Option<String>,
|
||||||
|
shared: &ApiShared,
|
||||||
|
) -> Result<(CreateUserResponse, String), ApiFailure> {
|
||||||
|
if !is_valid_username(&body.username) {
|
||||||
|
return Err(ApiFailure::bad_request(
|
||||||
|
"username must match [A-Za-z0-9_.-] and be 1..64 chars",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let secret = match body.secret {
|
||||||
|
Some(secret) => {
|
||||||
|
if !is_valid_user_secret(&secret) {
|
||||||
|
return Err(ApiFailure::bad_request(
|
||||||
|
"secret must be exactly 32 hex characters",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
secret
|
||||||
|
}
|
||||||
|
None => random_user_secret(),
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(ad_tag) = body.user_ad_tag.as_ref() && !is_valid_ad_tag(ad_tag) {
|
||||||
|
return Err(ApiFailure::bad_request(
|
||||||
|
"user_ad_tag must be exactly 32 hex characters",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let expiration = parse_optional_expiration(body.expiration_rfc3339.as_deref())?;
|
||||||
|
let _guard = shared.mutation_lock.lock().await;
|
||||||
|
let mut cfg = load_config_from_disk(&shared.config_path).await?;
|
||||||
|
ensure_expected_revision(&shared.config_path, expected_revision.as_deref()).await?;
|
||||||
|
|
||||||
|
if cfg.access.users.contains_key(&body.username) {
|
||||||
|
return Err(ApiFailure::new(
|
||||||
|
StatusCode::CONFLICT,
|
||||||
|
"user_exists",
|
||||||
|
"User already exists",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg.access.users.insert(body.username.clone(), secret.clone());
|
||||||
|
if let Some(ad_tag) = body.user_ad_tag {
|
||||||
|
cfg.access.user_ad_tags.insert(body.username.clone(), ad_tag);
|
||||||
|
}
|
||||||
|
if let Some(limit) = body.max_tcp_conns {
|
||||||
|
cfg.access.user_max_tcp_conns.insert(body.username.clone(), limit);
|
||||||
|
}
|
||||||
|
if let Some(expiration) = expiration {
|
||||||
|
cfg.access
|
||||||
|
.user_expirations
|
||||||
|
.insert(body.username.clone(), expiration);
|
||||||
|
}
|
||||||
|
if let Some(quota) = body.data_quota_bytes {
|
||||||
|
cfg.access.user_data_quota.insert(body.username.clone(), quota);
|
||||||
|
}
|
||||||
|
|
||||||
|
let updated_limit = body.max_unique_ips;
|
||||||
|
if let Some(limit) = updated_limit {
|
||||||
|
cfg.access
|
||||||
|
.user_max_unique_ips
|
||||||
|
.insert(body.username.clone(), limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg.validate()
|
||||||
|
.map_err(|e| ApiFailure::bad_request(format!("config validation failed: {}", e)))?;
|
||||||
|
|
||||||
|
let revision = save_config_to_disk(&shared.config_path, &cfg).await?;
|
||||||
|
drop(_guard);
|
||||||
|
|
||||||
|
if let Some(limit) = updated_limit {
|
||||||
|
shared.ip_tracker.set_user_limit(&body.username, limit).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
let users = users_from_config(
|
||||||
|
&cfg,
|
||||||
|
&shared.stats,
|
||||||
|
&shared.ip_tracker,
|
||||||
|
shared.startup_detected_ip_v4,
|
||||||
|
shared.startup_detected_ip_v6,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
let user = users
|
||||||
|
.into_iter()
|
||||||
|
.find(|entry| entry.username == body.username)
|
||||||
|
.unwrap_or(UserInfo {
|
||||||
|
username: body.username.clone(),
|
||||||
|
user_ad_tag: None,
|
||||||
|
max_tcp_conns: None,
|
||||||
|
expiration_rfc3339: None,
|
||||||
|
data_quota_bytes: None,
|
||||||
|
max_unique_ips: updated_limit,
|
||||||
|
current_connections: 0,
|
||||||
|
active_unique_ips: 0,
|
||||||
|
total_octets: 0,
|
||||||
|
links: build_user_links(
|
||||||
|
&cfg,
|
||||||
|
&secret,
|
||||||
|
shared.startup_detected_ip_v4,
|
||||||
|
shared.startup_detected_ip_v6,
|
||||||
|
),
|
||||||
|
});
|
||||||
|
|
||||||
|
Ok((CreateUserResponse { user, secret }, revision))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) async fn patch_user(
|
||||||
|
user: &str,
|
||||||
|
body: PatchUserRequest,
|
||||||
|
expected_revision: Option<String>,
|
||||||
|
shared: &ApiShared,
|
||||||
|
) -> Result<(UserInfo, String), ApiFailure> {
|
||||||
|
if let Some(secret) = body.secret.as_ref() && !is_valid_user_secret(secret) {
|
||||||
|
return Err(ApiFailure::bad_request(
|
||||||
|
"secret must be exactly 32 hex characters",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
if let Some(ad_tag) = body.user_ad_tag.as_ref() && !is_valid_ad_tag(ad_tag) {
|
||||||
|
return Err(ApiFailure::bad_request(
|
||||||
|
"user_ad_tag must be exactly 32 hex characters",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
let expiration = parse_optional_expiration(body.expiration_rfc3339.as_deref())?;
|
||||||
|
let _guard = shared.mutation_lock.lock().await;
|
||||||
|
let mut cfg = load_config_from_disk(&shared.config_path).await?;
|
||||||
|
ensure_expected_revision(&shared.config_path, expected_revision.as_deref()).await?;
|
||||||
|
|
||||||
|
if !cfg.access.users.contains_key(user) {
|
||||||
|
return Err(ApiFailure::new(
|
||||||
|
StatusCode::NOT_FOUND,
|
||||||
|
"not_found",
|
||||||
|
"User not found",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(secret) = body.secret {
|
||||||
|
cfg.access.users.insert(user.to_string(), secret);
|
||||||
|
}
|
||||||
|
if let Some(ad_tag) = body.user_ad_tag {
|
||||||
|
cfg.access.user_ad_tags.insert(user.to_string(), ad_tag);
|
||||||
|
}
|
||||||
|
if let Some(limit) = body.max_tcp_conns {
|
||||||
|
cfg.access.user_max_tcp_conns.insert(user.to_string(), limit);
|
||||||
|
}
|
||||||
|
if let Some(expiration) = expiration {
|
||||||
|
cfg.access.user_expirations.insert(user.to_string(), expiration);
|
||||||
|
}
|
||||||
|
if let Some(quota) = body.data_quota_bytes {
|
||||||
|
cfg.access.user_data_quota.insert(user.to_string(), quota);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut updated_limit = None;
|
||||||
|
if let Some(limit) = body.max_unique_ips {
|
||||||
|
cfg.access.user_max_unique_ips.insert(user.to_string(), limit);
|
||||||
|
updated_limit = Some(limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg.validate()
|
||||||
|
.map_err(|e| ApiFailure::bad_request(format!("config validation failed: {}", e)))?;
|
||||||
|
|
||||||
|
let revision = save_config_to_disk(&shared.config_path, &cfg).await?;
|
||||||
|
drop(_guard);
|
||||||
|
if let Some(limit) = updated_limit {
|
||||||
|
shared.ip_tracker.set_user_limit(user, limit).await;
|
||||||
|
}
|
||||||
|
let users = users_from_config(
|
||||||
|
&cfg,
|
||||||
|
&shared.stats,
|
||||||
|
&shared.ip_tracker,
|
||||||
|
shared.startup_detected_ip_v4,
|
||||||
|
shared.startup_detected_ip_v6,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
let user_info = users
|
||||||
|
.into_iter()
|
||||||
|
.find(|entry| entry.username == user)
|
||||||
|
.ok_or_else(|| ApiFailure::internal("failed to build updated user view"))?;
|
||||||
|
|
||||||
|
Ok((user_info, revision))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) async fn rotate_secret(
|
||||||
|
user: &str,
|
||||||
|
body: RotateSecretRequest,
|
||||||
|
expected_revision: Option<String>,
|
||||||
|
shared: &ApiShared,
|
||||||
|
) -> Result<(CreateUserResponse, String), ApiFailure> {
|
||||||
|
let secret = body.secret.unwrap_or_else(random_user_secret);
|
||||||
|
if !is_valid_user_secret(&secret) {
|
||||||
|
return Err(ApiFailure::bad_request(
|
||||||
|
"secret must be exactly 32 hex characters",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let _guard = shared.mutation_lock.lock().await;
|
||||||
|
let mut cfg = load_config_from_disk(&shared.config_path).await?;
|
||||||
|
ensure_expected_revision(&shared.config_path, expected_revision.as_deref()).await?;
|
||||||
|
|
||||||
|
if !cfg.access.users.contains_key(user) {
|
||||||
|
return Err(ApiFailure::new(
|
||||||
|
StatusCode::NOT_FOUND,
|
||||||
|
"not_found",
|
||||||
|
"User not found",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg.access.users.insert(user.to_string(), secret.clone());
|
||||||
|
cfg.validate()
|
||||||
|
.map_err(|e| ApiFailure::bad_request(format!("config validation failed: {}", e)))?;
|
||||||
|
let revision = save_config_to_disk(&shared.config_path, &cfg).await?;
|
||||||
|
drop(_guard);
|
||||||
|
|
||||||
|
let users = users_from_config(
|
||||||
|
&cfg,
|
||||||
|
&shared.stats,
|
||||||
|
&shared.ip_tracker,
|
||||||
|
shared.startup_detected_ip_v4,
|
||||||
|
shared.startup_detected_ip_v6,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
let user_info = users
|
||||||
|
.into_iter()
|
||||||
|
.find(|entry| entry.username == user)
|
||||||
|
.ok_or_else(|| ApiFailure::internal("failed to build updated user view"))?;
|
||||||
|
|
||||||
|
Ok((
|
||||||
|
CreateUserResponse {
|
||||||
|
user: user_info,
|
||||||
|
secret,
|
||||||
|
},
|
||||||
|
revision,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) async fn delete_user(
|
||||||
|
user: &str,
|
||||||
|
expected_revision: Option<String>,
|
||||||
|
shared: &ApiShared,
|
||||||
|
) -> Result<(String, String), ApiFailure> {
|
||||||
|
let _guard = shared.mutation_lock.lock().await;
|
||||||
|
let mut cfg = load_config_from_disk(&shared.config_path).await?;
|
||||||
|
ensure_expected_revision(&shared.config_path, expected_revision.as_deref()).await?;
|
||||||
|
|
||||||
|
if !cfg.access.users.contains_key(user) {
|
||||||
|
return Err(ApiFailure::new(
|
||||||
|
StatusCode::NOT_FOUND,
|
||||||
|
"not_found",
|
||||||
|
"User not found",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
if cfg.access.users.len() <= 1 {
|
||||||
|
return Err(ApiFailure::new(
|
||||||
|
StatusCode::CONFLICT,
|
||||||
|
"last_user_forbidden",
|
||||||
|
"Cannot delete the last configured user",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg.access.users.remove(user);
|
||||||
|
cfg.access.user_ad_tags.remove(user);
|
||||||
|
cfg.access.user_max_tcp_conns.remove(user);
|
||||||
|
cfg.access.user_expirations.remove(user);
|
||||||
|
cfg.access.user_data_quota.remove(user);
|
||||||
|
cfg.access.user_max_unique_ips.remove(user);
|
||||||
|
|
||||||
|
cfg.validate()
|
||||||
|
.map_err(|e| ApiFailure::bad_request(format!("config validation failed: {}", e)))?;
|
||||||
|
let revision = save_config_to_disk(&shared.config_path, &cfg).await?;
|
||||||
|
drop(_guard);
|
||||||
|
shared.ip_tracker.clear_user_ips(user).await;
|
||||||
|
|
||||||
|
Ok((user.to_string(), revision))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) async fn users_from_config(
|
||||||
|
cfg: &ProxyConfig,
|
||||||
|
stats: &Stats,
|
||||||
|
ip_tracker: &UserIpTracker,
|
||||||
|
startup_detected_ip_v4: Option<IpAddr>,
|
||||||
|
startup_detected_ip_v6: Option<IpAddr>,
|
||||||
|
) -> Vec<UserInfo> {
|
||||||
|
let ip_counts = ip_tracker
|
||||||
|
.get_stats()
|
||||||
|
.await
|
||||||
|
.into_iter()
|
||||||
|
.map(|(user, count, _)| (user, count))
|
||||||
|
.collect::<HashMap<_, _>>();
|
||||||
|
|
||||||
|
let mut names = cfg.access.users.keys().cloned().collect::<Vec<_>>();
|
||||||
|
names.sort();
|
||||||
|
|
||||||
|
let mut users = Vec::with_capacity(names.len());
|
||||||
|
for username in names {
|
||||||
|
let links = cfg
|
||||||
|
.access
|
||||||
|
.users
|
||||||
|
.get(&username)
|
||||||
|
.map(|secret| {
|
||||||
|
build_user_links(
|
||||||
|
cfg,
|
||||||
|
secret,
|
||||||
|
startup_detected_ip_v4,
|
||||||
|
startup_detected_ip_v6,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.unwrap_or(UserLinks {
|
||||||
|
classic: Vec::new(),
|
||||||
|
secure: Vec::new(),
|
||||||
|
tls: Vec::new(),
|
||||||
|
});
|
||||||
|
users.push(UserInfo {
|
||||||
|
user_ad_tag: cfg.access.user_ad_tags.get(&username).cloned(),
|
||||||
|
max_tcp_conns: cfg.access.user_max_tcp_conns.get(&username).copied(),
|
||||||
|
expiration_rfc3339: cfg
|
||||||
|
.access
|
||||||
|
.user_expirations
|
||||||
|
.get(&username)
|
||||||
|
.map(chrono::DateTime::<chrono::Utc>::to_rfc3339),
|
||||||
|
data_quota_bytes: cfg.access.user_data_quota.get(&username).copied(),
|
||||||
|
max_unique_ips: cfg.access.user_max_unique_ips.get(&username).copied(),
|
||||||
|
current_connections: stats.get_user_curr_connects(&username),
|
||||||
|
active_unique_ips: ip_counts.get(&username).copied().unwrap_or(0),
|
||||||
|
total_octets: stats.get_user_total_octets(&username),
|
||||||
|
links,
|
||||||
|
username,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
users
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_user_links(
|
||||||
|
cfg: &ProxyConfig,
|
||||||
|
secret: &str,
|
||||||
|
startup_detected_ip_v4: Option<IpAddr>,
|
||||||
|
startup_detected_ip_v6: Option<IpAddr>,
|
||||||
|
) -> UserLinks {
|
||||||
|
let hosts = resolve_link_hosts(cfg, startup_detected_ip_v4, startup_detected_ip_v6);
|
||||||
|
let port = cfg.general.links.public_port.unwrap_or(cfg.server.port);
|
||||||
|
let tls_domains = resolve_tls_domains(cfg);
|
||||||
|
|
||||||
|
let mut classic = Vec::new();
|
||||||
|
let mut secure = Vec::new();
|
||||||
|
let mut tls = Vec::new();
|
||||||
|
|
||||||
|
for host in &hosts {
|
||||||
|
if cfg.general.modes.classic {
|
||||||
|
classic.push(format!(
|
||||||
|
"tg://proxy?server={}&port={}&secret={}",
|
||||||
|
host, port, secret
|
||||||
|
));
|
||||||
|
}
|
||||||
|
if cfg.general.modes.secure {
|
||||||
|
secure.push(format!(
|
||||||
|
"tg://proxy?server={}&port={}&secret=dd{}",
|
||||||
|
host, port, secret
|
||||||
|
));
|
||||||
|
}
|
||||||
|
if cfg.general.modes.tls {
|
||||||
|
for domain in &tls_domains {
|
||||||
|
let domain_hex = hex::encode(domain);
|
||||||
|
tls.push(format!(
|
||||||
|
"tg://proxy?server={}&port={}&secret=ee{}{}",
|
||||||
|
host, port, secret, domain_hex
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
UserLinks {
|
||||||
|
classic,
|
||||||
|
secure,
|
||||||
|
tls,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn resolve_link_hosts(
|
||||||
|
cfg: &ProxyConfig,
|
||||||
|
startup_detected_ip_v4: Option<IpAddr>,
|
||||||
|
startup_detected_ip_v6: Option<IpAddr>,
|
||||||
|
) -> Vec<String> {
|
||||||
|
if let Some(host) = cfg
|
||||||
|
.general
|
||||||
|
.links
|
||||||
|
.public_host
|
||||||
|
.as_deref()
|
||||||
|
.map(str::trim)
|
||||||
|
.filter(|value| !value.is_empty())
|
||||||
|
{
|
||||||
|
return vec![host.to_string()];
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut startup_hosts = Vec::new();
|
||||||
|
if let Some(ip) = startup_detected_ip_v4 {
|
||||||
|
push_unique_host(&mut startup_hosts, &ip.to_string());
|
||||||
|
}
|
||||||
|
if let Some(ip) = startup_detected_ip_v6 {
|
||||||
|
push_unique_host(&mut startup_hosts, &ip.to_string());
|
||||||
|
}
|
||||||
|
if !startup_hosts.is_empty() {
|
||||||
|
return startup_hosts;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut hosts = Vec::new();
|
||||||
|
for listener in &cfg.server.listeners {
|
||||||
|
if let Some(host) = listener
|
||||||
|
.announce
|
||||||
|
.as_deref()
|
||||||
|
.map(str::trim)
|
||||||
|
.filter(|value| !value.is_empty())
|
||||||
|
{
|
||||||
|
push_unique_host(&mut hosts, host);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if let Some(ip) = listener.announce_ip {
|
||||||
|
if !ip.is_unspecified() {
|
||||||
|
push_unique_host(&mut hosts, &ip.to_string());
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if !listener.ip.is_unspecified() {
|
||||||
|
push_unique_host(&mut hosts, &listener.ip.to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if hosts.is_empty() {
|
||||||
|
if let Some(host) = cfg.server.listen_addr_ipv4.as_deref() {
|
||||||
|
push_host_from_legacy_listen(&mut hosts, host);
|
||||||
|
}
|
||||||
|
if let Some(host) = cfg.server.listen_addr_ipv6.as_deref() {
|
||||||
|
push_host_from_legacy_listen(&mut hosts, host);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
hosts
|
||||||
|
}
|
||||||
|
|
||||||
|
fn push_host_from_legacy_listen(hosts: &mut Vec<String>, raw: &str) {
|
||||||
|
let candidate = raw.trim();
|
||||||
|
if candidate.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
match candidate.parse::<IpAddr>() {
|
||||||
|
Ok(ip) if ip.is_unspecified() => {}
|
||||||
|
Ok(ip) => push_unique_host(hosts, &ip.to_string()),
|
||||||
|
Err(_) => push_unique_host(hosts, candidate),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn push_unique_host(hosts: &mut Vec<String>, candidate: &str) {
|
||||||
|
if !hosts.iter().any(|existing| existing == candidate) {
|
||||||
|
hosts.push(candidate.to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn resolve_tls_domains(cfg: &ProxyConfig) -> Vec<&str> {
|
||||||
|
let mut domains = Vec::with_capacity(1 + cfg.censorship.tls_domains.len());
|
||||||
|
let primary = cfg.censorship.tls_domain.as_str();
|
||||||
|
if !primary.is_empty() {
|
||||||
|
domains.push(primary);
|
||||||
|
}
|
||||||
|
for domain in &cfg.censorship.tls_domains {
|
||||||
|
let value = domain.as_str();
|
||||||
|
if value.is_empty() || domains.contains(&value) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
domains.push(value);
|
||||||
|
}
|
||||||
|
domains
|
||||||
|
}
|
||||||
@@ -7,7 +7,13 @@ const DEFAULT_NETWORK_IPV6: Option<bool> = Some(false);
|
|||||||
const DEFAULT_STUN_TCP_FALLBACK: bool = true;
|
const DEFAULT_STUN_TCP_FALLBACK: bool = true;
|
||||||
const DEFAULT_MIDDLE_PROXY_WARM_STANDBY: usize = 16;
|
const DEFAULT_MIDDLE_PROXY_WARM_STANDBY: usize = 16;
|
||||||
const DEFAULT_ME_RECONNECT_MAX_CONCURRENT_PER_DC: u32 = 8;
|
const DEFAULT_ME_RECONNECT_MAX_CONCURRENT_PER_DC: u32 = 8;
|
||||||
const DEFAULT_ME_RECONNECT_FAST_RETRY_COUNT: u32 = 12;
|
const DEFAULT_ME_RECONNECT_FAST_RETRY_COUNT: u32 = 16;
|
||||||
|
const DEFAULT_ME_SINGLE_ENDPOINT_SHADOW_WRITERS: u8 = 2;
|
||||||
|
const DEFAULT_ME_ADAPTIVE_FLOOR_IDLE_SECS: u64 = 90;
|
||||||
|
const DEFAULT_ME_ADAPTIVE_FLOOR_MIN_WRITERS_SINGLE_ENDPOINT: u8 = 1;
|
||||||
|
const DEFAULT_ME_ADAPTIVE_FLOOR_RECOVER_GRACE_SECS: u64 = 180;
|
||||||
|
const DEFAULT_UPSTREAM_CONNECT_RETRY_ATTEMPTS: u32 = 2;
|
||||||
|
const DEFAULT_UPSTREAM_UNHEALTHY_FAIL_THRESHOLD: u32 = 5;
|
||||||
const DEFAULT_LISTEN_ADDR_IPV6: &str = "::";
|
const DEFAULT_LISTEN_ADDR_IPV6: &str = "::";
|
||||||
const DEFAULT_ACCESS_USER: &str = "default";
|
const DEFAULT_ACCESS_USER: &str = "default";
|
||||||
const DEFAULT_ACCESS_SECRET: &str = "00000000000000000000000000000000";
|
const DEFAULT_ACCESS_SECRET: &str = "00000000000000000000000000000000";
|
||||||
@@ -21,7 +27,7 @@ pub(crate) fn default_port() -> u16 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn default_tls_domain() -> String {
|
pub(crate) fn default_tls_domain() -> String {
|
||||||
"www.google.com".to_string()
|
"petrovich.ru".to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn default_mask_port() -> u16 {
|
pub(crate) fn default_mask_port() -> u16 {
|
||||||
@@ -45,7 +51,7 @@ pub(crate) fn default_replay_window_secs() -> u64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn default_handshake_timeout() -> u64 {
|
pub(crate) fn default_handshake_timeout() -> u64 {
|
||||||
15
|
30
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn default_connect_timeout() -> u64 {
|
pub(crate) fn default_connect_timeout() -> u64 {
|
||||||
@@ -60,17 +66,21 @@ pub(crate) fn default_ack_timeout() -> u64 {
|
|||||||
300
|
300
|
||||||
}
|
}
|
||||||
pub(crate) fn default_me_one_retry() -> u8 {
|
pub(crate) fn default_me_one_retry() -> u8 {
|
||||||
3
|
12
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn default_me_one_timeout() -> u64 {
|
pub(crate) fn default_me_one_timeout() -> u64 {
|
||||||
1500
|
1200
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn default_listen_addr() -> String {
|
pub(crate) fn default_listen_addr() -> String {
|
||||||
"0.0.0.0".to_string()
|
"0.0.0.0".to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_listen_addr_ipv4() -> Option<String> {
|
||||||
|
Some(default_listen_addr())
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn default_weight() -> u16 {
|
pub(crate) fn default_weight() -> u16 {
|
||||||
1
|
1
|
||||||
}
|
}
|
||||||
@@ -82,6 +92,26 @@ pub(crate) fn default_metrics_whitelist() -> Vec<IpNetwork> {
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_api_listen() -> String {
|
||||||
|
"127.0.0.1:9091".to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_api_whitelist() -> Vec<IpNetwork> {
|
||||||
|
default_metrics_whitelist()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_api_request_body_limit_bytes() -> usize {
|
||||||
|
64 * 1024
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_api_minimal_runtime_enabled() -> bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_api_minimal_runtime_cache_ttl_ms() -> u64 {
|
||||||
|
1000
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn default_prefer_4() -> u8 {
|
pub(crate) fn default_prefer_4() -> u8 {
|
||||||
4
|
4
|
||||||
}
|
}
|
||||||
@@ -102,16 +132,32 @@ pub(crate) fn default_pool_size() -> usize {
|
|||||||
8
|
8
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_proxy_secret_path() -> Option<String> {
|
||||||
|
Some("proxy-secret".to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_middle_proxy_nat_stun() -> Option<String> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_middle_proxy_nat_stun_servers() -> Vec<String> {
|
||||||
|
Vec::new()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_stun_nat_probe_concurrency() -> usize {
|
||||||
|
8
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn default_middle_proxy_warm_standby() -> usize {
|
pub(crate) fn default_middle_proxy_warm_standby() -> usize {
|
||||||
DEFAULT_MIDDLE_PROXY_WARM_STANDBY
|
DEFAULT_MIDDLE_PROXY_WARM_STANDBY
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn default_keepalive_interval() -> u64 {
|
pub(crate) fn default_keepalive_interval() -> u64 {
|
||||||
25
|
8
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn default_keepalive_jitter() -> u64 {
|
pub(crate) fn default_keepalive_jitter() -> u64 {
|
||||||
5
|
2
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn default_warmup_step_delay_ms() -> u64 {
|
pub(crate) fn default_warmup_step_delay_ms() -> u64 {
|
||||||
@@ -138,6 +184,62 @@ pub(crate) fn default_me_reconnect_fast_retry_count() -> u32 {
|
|||||||
DEFAULT_ME_RECONNECT_FAST_RETRY_COUNT
|
DEFAULT_ME_RECONNECT_FAST_RETRY_COUNT
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_single_endpoint_shadow_writers() -> u8 {
|
||||||
|
DEFAULT_ME_SINGLE_ENDPOINT_SHADOW_WRITERS
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_single_endpoint_outage_mode_enabled() -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_single_endpoint_outage_disable_quarantine() -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_single_endpoint_outage_backoff_min_ms() -> u64 {
|
||||||
|
250
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_single_endpoint_outage_backoff_max_ms() -> u64 {
|
||||||
|
3000
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_single_endpoint_shadow_rotate_every_secs() -> u64 {
|
||||||
|
900
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_adaptive_floor_idle_secs() -> u64 {
|
||||||
|
DEFAULT_ME_ADAPTIVE_FLOOR_IDLE_SECS
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_adaptive_floor_min_writers_single_endpoint() -> u8 {
|
||||||
|
DEFAULT_ME_ADAPTIVE_FLOOR_MIN_WRITERS_SINGLE_ENDPOINT
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_adaptive_floor_recover_grace_secs() -> u64 {
|
||||||
|
DEFAULT_ME_ADAPTIVE_FLOOR_RECOVER_GRACE_SECS
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_upstream_connect_retry_attempts() -> u32 {
|
||||||
|
DEFAULT_UPSTREAM_CONNECT_RETRY_ATTEMPTS
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_upstream_connect_retry_backoff_ms() -> u64 {
|
||||||
|
100
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_upstream_unhealthy_fail_threshold() -> u32 {
|
||||||
|
DEFAULT_UPSTREAM_UNHEALTHY_FAIL_THRESHOLD
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_upstream_connect_failfast_hard_errors() -> bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_rpc_proxy_req_every() -> u64 {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn default_crypto_pending_buffer() -> usize {
|
pub(crate) fn default_crypto_pending_buffer() -> usize {
|
||||||
256 * 1024
|
256 * 1024
|
||||||
}
|
}
|
||||||
@@ -150,6 +252,18 @@ pub(crate) fn default_desync_all_full() -> bool {
|
|||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_route_backpressure_base_timeout_ms() -> u64 {
|
||||||
|
25
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_route_backpressure_high_timeout_ms() -> u64 {
|
||||||
|
120
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_route_backpressure_high_watermark_pct() -> u8 {
|
||||||
|
80
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn default_beobachten_minutes() -> u64 {
|
pub(crate) fn default_beobachten_minutes() -> u64 {
|
||||||
10
|
10
|
||||||
}
|
}
|
||||||
@@ -231,6 +345,18 @@ pub(crate) fn default_me_reinit_every_secs() -> u64 {
|
|||||||
15 * 60
|
15 * 60
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_reinit_singleflight() -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_reinit_trigger_channel() -> usize {
|
||||||
|
64
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_reinit_coalesce_window_ms() -> u64 {
|
||||||
|
200
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn default_me_hardswap_warmup_delay_min_ms() -> u64 {
|
pub(crate) fn default_me_hardswap_warmup_delay_min_ms() -> u64 {
|
||||||
1000
|
1000
|
||||||
}
|
}
|
||||||
@@ -255,6 +381,18 @@ pub(crate) fn default_me_config_apply_cooldown_secs() -> u64 {
|
|||||||
300
|
300
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_snapshot_require_http_2xx() -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_snapshot_reject_empty_map() -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_snapshot_min_proxy_for_lines() -> u32 {
|
||||||
|
1
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn default_proxy_secret_stable_snapshots() -> u8 {
|
pub(crate) fn default_proxy_secret_stable_snapshots() -> u8 {
|
||||||
2
|
2
|
||||||
}
|
}
|
||||||
@@ -263,6 +401,10 @@ pub(crate) fn default_proxy_secret_rotate_runtime() -> bool {
|
|||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_secret_atomic_snapshot() -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn default_proxy_secret_len_max() -> usize {
|
pub(crate) fn default_proxy_secret_len_max() -> usize {
|
||||||
256
|
256
|
||||||
}
|
}
|
||||||
@@ -275,10 +417,18 @@ pub(crate) fn default_me_pool_drain_ttl_secs() -> u64 {
|
|||||||
90
|
90
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_bind_stale_ttl_secs() -> u64 {
|
||||||
|
default_me_pool_drain_ttl_secs()
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn default_me_pool_min_fresh_ratio() -> f32 {
|
pub(crate) fn default_me_pool_min_fresh_ratio() -> f32 {
|
||||||
0.8
|
0.8
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_deterministic_writer_sort() -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn default_hardswap() -> bool {
|
pub(crate) fn default_hardswap() -> bool {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
@@ -303,6 +453,10 @@ pub(crate) fn default_listen_addr_ipv6() -> String {
|
|||||||
DEFAULT_LISTEN_ADDR_IPV6.to_string()
|
DEFAULT_LISTEN_ADDR_IPV6.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_listen_addr_ipv6_opt() -> Option<String> {
|
||||||
|
Some(default_listen_addr_ipv6())
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn default_access_users() -> HashMap<String, String> {
|
pub(crate) fn default_access_users() -> HashMap<String, String> {
|
||||||
HashMap::from([(
|
HashMap::from([(
|
||||||
DEFAULT_ACCESS_USER.to_string(),
|
DEFAULT_ACCESS_USER.to_string(),
|
||||||
|
|||||||
@@ -4,19 +4,22 @@
|
|||||||
//!
|
//!
|
||||||
//! # What can be reloaded without restart
|
//! # What can be reloaded without restart
|
||||||
//!
|
//!
|
||||||
//! | Section | Field | Effect |
|
//! | Section | Field | Effect |
|
||||||
//! |-----------|-------------------------------|-----------------------------------|
|
//! |-----------|--------------------------------|------------------------------------------------|
|
||||||
//! | `general` | `log_level` | Filter updated via `log_level_tx` |
|
//! | `general` | `log_level` | Filter updated via `log_level_tx` |
|
||||||
//! | `general` | `ad_tag` | Passed on next connection |
|
//! | `access` | `user_ad_tags` | Passed on next connection |
|
||||||
//! | `general` | `middle_proxy_pool_size` | Passed on next connection |
|
//! | `general` | `ad_tag` | Passed on next connection (fallback per-user) |
|
||||||
//! | `general` | `me_keepalive_*` | Passed on next connection |
|
//! | `general` | `middle_proxy_pool_size` | Passed on next connection |
|
||||||
//! | `general` | `desync_all_full` | Applied immediately |
|
//! | `general` | `me_keepalive_*` | Passed on next connection |
|
||||||
//! | `general` | `update_every` | Applied to ME updater immediately |
|
//! | `general` | `desync_all_full` | Applied immediately |
|
||||||
//! | `general` | `hardswap` | Applied on next ME map update |
|
//! | `general` | `update_every` | Applied to ME updater immediately |
|
||||||
//! | `general` | `me_pool_drain_ttl_secs` | Applied on next ME map update |
|
//! | `general` | `hardswap` | Applied on next ME map update |
|
||||||
//! | `general` | `me_pool_min_fresh_ratio` | Applied on next ME map update |
|
//! | `general` | `me_pool_drain_ttl_secs` | Applied on next ME map update |
|
||||||
//! | `general` | `me_reinit_drain_timeout_secs`| Applied on next ME map update |
|
//! | `general` | `me_pool_min_fresh_ratio` | Applied on next ME map update |
|
||||||
//! | `access` | All user/quota fields | Effective immediately |
|
//! | `general` | `me_reinit_drain_timeout_secs` | Applied on next ME map update |
|
||||||
|
//! | `general` | `telemetry` / `me_*_policy` | Applied immediately |
|
||||||
|
//! | `network` | `dns_overrides` | Applied immediately |
|
||||||
|
//! | `access` | All user/quota fields | Effective immediately |
|
||||||
//!
|
//!
|
||||||
//! Fields that require re-binding sockets (`server.port`, `censorship.*`,
|
//! Fields that require re-binding sockets (`server.port`, `censorship.*`,
|
||||||
//! `network.*`, `use_middle_proxy`) are **not** applied; a warning is emitted.
|
//! `network.*`, `use_middle_proxy`) are **not** applied; a warning is emitted.
|
||||||
@@ -29,7 +32,7 @@ use notify::{EventKind, RecursiveMode, Watcher, recommended_watcher};
|
|||||||
use tokio::sync::{mpsc, watch};
|
use tokio::sync::{mpsc, watch};
|
||||||
use tracing::{error, info, warn};
|
use tracing::{error, info, warn};
|
||||||
|
|
||||||
use crate::config::LogLevel;
|
use crate::config::{LogLevel, MeFloorMode, MeSocksKdfPolicy, MeTelemetryLevel};
|
||||||
use super::load::ProxyConfig;
|
use super::load::ProxyConfig;
|
||||||
|
|
||||||
// ── Hot fields ────────────────────────────────────────────────────────────────
|
// ── Hot fields ────────────────────────────────────────────────────────────────
|
||||||
@@ -39,6 +42,7 @@ use super::load::ProxyConfig;
|
|||||||
pub struct HotFields {
|
pub struct HotFields {
|
||||||
pub log_level: LogLevel,
|
pub log_level: LogLevel,
|
||||||
pub ad_tag: Option<String>,
|
pub ad_tag: Option<String>,
|
||||||
|
pub dns_overrides: Vec<String>,
|
||||||
pub middle_proxy_pool_size: usize,
|
pub middle_proxy_pool_size: usize,
|
||||||
pub desync_all_full: bool,
|
pub desync_all_full: bool,
|
||||||
pub update_every_secs: u64,
|
pub update_every_secs: u64,
|
||||||
@@ -50,6 +54,17 @@ pub struct HotFields {
|
|||||||
pub me_keepalive_interval_secs: u64,
|
pub me_keepalive_interval_secs: u64,
|
||||||
pub me_keepalive_jitter_secs: u64,
|
pub me_keepalive_jitter_secs: u64,
|
||||||
pub me_keepalive_payload_random: bool,
|
pub me_keepalive_payload_random: bool,
|
||||||
|
pub telemetry_core_enabled: bool,
|
||||||
|
pub telemetry_user_enabled: bool,
|
||||||
|
pub telemetry_me_level: MeTelemetryLevel,
|
||||||
|
pub me_socks_kdf_policy: MeSocksKdfPolicy,
|
||||||
|
pub me_floor_mode: MeFloorMode,
|
||||||
|
pub me_adaptive_floor_idle_secs: u64,
|
||||||
|
pub me_adaptive_floor_min_writers_single_endpoint: u8,
|
||||||
|
pub me_adaptive_floor_recover_grace_secs: u64,
|
||||||
|
pub me_route_backpressure_base_timeout_ms: u64,
|
||||||
|
pub me_route_backpressure_high_timeout_ms: u64,
|
||||||
|
pub me_route_backpressure_high_watermark_pct: u8,
|
||||||
pub access: crate::config::AccessConfig,
|
pub access: crate::config::AccessConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,6 +73,7 @@ impl HotFields {
|
|||||||
Self {
|
Self {
|
||||||
log_level: cfg.general.log_level.clone(),
|
log_level: cfg.general.log_level.clone(),
|
||||||
ad_tag: cfg.general.ad_tag.clone(),
|
ad_tag: cfg.general.ad_tag.clone(),
|
||||||
|
dns_overrides: cfg.network.dns_overrides.clone(),
|
||||||
middle_proxy_pool_size: cfg.general.middle_proxy_pool_size,
|
middle_proxy_pool_size: cfg.general.middle_proxy_pool_size,
|
||||||
desync_all_full: cfg.general.desync_all_full,
|
desync_all_full: cfg.general.desync_all_full,
|
||||||
update_every_secs: cfg.general.effective_update_every_secs(),
|
update_every_secs: cfg.general.effective_update_every_secs(),
|
||||||
@@ -69,6 +85,21 @@ impl HotFields {
|
|||||||
me_keepalive_interval_secs: cfg.general.me_keepalive_interval_secs,
|
me_keepalive_interval_secs: cfg.general.me_keepalive_interval_secs,
|
||||||
me_keepalive_jitter_secs: cfg.general.me_keepalive_jitter_secs,
|
me_keepalive_jitter_secs: cfg.general.me_keepalive_jitter_secs,
|
||||||
me_keepalive_payload_random: cfg.general.me_keepalive_payload_random,
|
me_keepalive_payload_random: cfg.general.me_keepalive_payload_random,
|
||||||
|
telemetry_core_enabled: cfg.general.telemetry.core_enabled,
|
||||||
|
telemetry_user_enabled: cfg.general.telemetry.user_enabled,
|
||||||
|
telemetry_me_level: cfg.general.telemetry.me_level,
|
||||||
|
me_socks_kdf_policy: cfg.general.me_socks_kdf_policy,
|
||||||
|
me_floor_mode: cfg.general.me_floor_mode,
|
||||||
|
me_adaptive_floor_idle_secs: cfg.general.me_adaptive_floor_idle_secs,
|
||||||
|
me_adaptive_floor_min_writers_single_endpoint: cfg
|
||||||
|
.general
|
||||||
|
.me_adaptive_floor_min_writers_single_endpoint,
|
||||||
|
me_adaptive_floor_recover_grace_secs: cfg
|
||||||
|
.general
|
||||||
|
.me_adaptive_floor_recover_grace_secs,
|
||||||
|
me_route_backpressure_base_timeout_ms: cfg.general.me_route_backpressure_base_timeout_ms,
|
||||||
|
me_route_backpressure_high_timeout_ms: cfg.general.me_route_backpressure_high_timeout_ms,
|
||||||
|
me_route_backpressure_high_watermark_pct: cfg.general.me_route_backpressure_high_watermark_pct,
|
||||||
access: cfg.access.clone(),
|
access: cfg.access.clone(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -84,6 +115,18 @@ fn warn_non_hot_changes(old: &ProxyConfig, new: &ProxyConfig) {
|
|||||||
old.server.port, new.server.port
|
old.server.port, new.server.port
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
if old.server.api.enabled != new.server.api.enabled
|
||||||
|
|| old.server.api.listen != new.server.api.listen
|
||||||
|
|| old.server.api.whitelist != new.server.api.whitelist
|
||||||
|
|| old.server.api.auth_header != new.server.api.auth_header
|
||||||
|
|| old.server.api.request_body_limit_bytes != new.server.api.request_body_limit_bytes
|
||||||
|
|| old.server.api.minimal_runtime_enabled != new.server.api.minimal_runtime_enabled
|
||||||
|
|| old.server.api.minimal_runtime_cache_ttl_ms
|
||||||
|
!= new.server.api.minimal_runtime_cache_ttl_ms
|
||||||
|
|| old.server.api.read_only != new.server.api.read_only
|
||||||
|
{
|
||||||
|
warn!("config reload: server.api changed; restart required");
|
||||||
|
}
|
||||||
if old.censorship.tls_domain != new.censorship.tls_domain {
|
if old.censorship.tls_domain != new.censorship.tls_domain {
|
||||||
warn!(
|
warn!(
|
||||||
"config reload: censorship.tls_domain changed ('{}' → '{}'); restart required",
|
"config reload: censorship.tls_domain changed ('{}' → '{}'); restart required",
|
||||||
@@ -96,6 +139,20 @@ fn warn_non_hot_changes(old: &ProxyConfig, new: &ProxyConfig) {
|
|||||||
if old.general.use_middle_proxy != new.general.use_middle_proxy {
|
if old.general.use_middle_proxy != new.general.use_middle_proxy {
|
||||||
warn!("config reload: use_middle_proxy changed; restart required");
|
warn!("config reload: use_middle_proxy changed; restart required");
|
||||||
}
|
}
|
||||||
|
if old.general.stun_nat_probe_concurrency != new.general.stun_nat_probe_concurrency {
|
||||||
|
warn!("config reload: general.stun_nat_probe_concurrency changed; restart required");
|
||||||
|
}
|
||||||
|
if old.general.upstream_connect_retry_attempts != new.general.upstream_connect_retry_attempts
|
||||||
|
|| old.general.upstream_connect_retry_backoff_ms
|
||||||
|
!= new.general.upstream_connect_retry_backoff_ms
|
||||||
|
|| old.general.upstream_unhealthy_fail_threshold
|
||||||
|
!= new.general.upstream_unhealthy_fail_threshold
|
||||||
|
|| old.general.upstream_connect_failfast_hard_errors
|
||||||
|
!= new.general.upstream_connect_failfast_hard_errors
|
||||||
|
|| old.general.rpc_proxy_req_every != new.general.rpc_proxy_req_every
|
||||||
|
{
|
||||||
|
warn!("config reload: general.upstream_* changed; restart required");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Resolve the public host for link generation — mirrors the logic in main.rs.
|
/// Resolve the public host for link generation — mirrors the logic in main.rs.
|
||||||
@@ -178,11 +235,21 @@ fn log_changes(
|
|||||||
log_tx.send(new_hot.log_level.clone()).ok();
|
log_tx.send(new_hot.log_level.clone()).ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
if old_hot.ad_tag != new_hot.ad_tag {
|
if old_hot.access.user_ad_tags != new_hot.access.user_ad_tags {
|
||||||
info!(
|
info!(
|
||||||
"config reload: ad_tag: {} → {}",
|
"config reload: user_ad_tags updated ({} entries)",
|
||||||
old_hot.ad_tag.as_deref().unwrap_or("none"),
|
new_hot.access.user_ad_tags.len(),
|
||||||
new_hot.ad_tag.as_deref().unwrap_or("none"),
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if old_hot.ad_tag != new_hot.ad_tag {
|
||||||
|
info!("config reload: general.ad_tag updated (applied on next connection)");
|
||||||
|
}
|
||||||
|
|
||||||
|
if old_hot.dns_overrides != new_hot.dns_overrides {
|
||||||
|
info!(
|
||||||
|
"config reload: network.dns_overrides updated ({} entries)",
|
||||||
|
new_hot.dns_overrides.len()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -249,6 +316,57 @@ fn log_changes(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if old_hot.telemetry_core_enabled != new_hot.telemetry_core_enabled
|
||||||
|
|| old_hot.telemetry_user_enabled != new_hot.telemetry_user_enabled
|
||||||
|
|| old_hot.telemetry_me_level != new_hot.telemetry_me_level
|
||||||
|
{
|
||||||
|
info!(
|
||||||
|
"config reload: telemetry: core_enabled={} user_enabled={} me_level={}",
|
||||||
|
new_hot.telemetry_core_enabled,
|
||||||
|
new_hot.telemetry_user_enabled,
|
||||||
|
new_hot.telemetry_me_level,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if old_hot.me_socks_kdf_policy != new_hot.me_socks_kdf_policy {
|
||||||
|
info!(
|
||||||
|
"config reload: me_socks_kdf_policy: {:?} → {:?}",
|
||||||
|
old_hot.me_socks_kdf_policy,
|
||||||
|
new_hot.me_socks_kdf_policy,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if old_hot.me_floor_mode != new_hot.me_floor_mode
|
||||||
|
|| old_hot.me_adaptive_floor_idle_secs != new_hot.me_adaptive_floor_idle_secs
|
||||||
|
|| old_hot.me_adaptive_floor_min_writers_single_endpoint
|
||||||
|
!= new_hot.me_adaptive_floor_min_writers_single_endpoint
|
||||||
|
|| old_hot.me_adaptive_floor_recover_grace_secs
|
||||||
|
!= new_hot.me_adaptive_floor_recover_grace_secs
|
||||||
|
{
|
||||||
|
info!(
|
||||||
|
"config reload: me_floor: mode={:?} idle={}s min_single={} recover_grace={}s",
|
||||||
|
new_hot.me_floor_mode,
|
||||||
|
new_hot.me_adaptive_floor_idle_secs,
|
||||||
|
new_hot.me_adaptive_floor_min_writers_single_endpoint,
|
||||||
|
new_hot.me_adaptive_floor_recover_grace_secs,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if old_hot.me_route_backpressure_base_timeout_ms
|
||||||
|
!= new_hot.me_route_backpressure_base_timeout_ms
|
||||||
|
|| old_hot.me_route_backpressure_high_timeout_ms
|
||||||
|
!= new_hot.me_route_backpressure_high_timeout_ms
|
||||||
|
|| old_hot.me_route_backpressure_high_watermark_pct
|
||||||
|
!= new_hot.me_route_backpressure_high_watermark_pct
|
||||||
|
{
|
||||||
|
info!(
|
||||||
|
"config reload: me_route_backpressure: base={}ms high={}ms watermark={}%",
|
||||||
|
new_hot.me_route_backpressure_base_timeout_ms,
|
||||||
|
new_hot.me_route_backpressure_high_timeout_ms,
|
||||||
|
new_hot.me_route_backpressure_high_watermark_pct,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if old_hot.access.users != new_hot.access.users {
|
if old_hot.access.users != new_hot.access.users {
|
||||||
let mut added: Vec<&String> = new_hot.access.users.keys()
|
let mut added: Vec<&String> = new_hot.access.users.keys()
|
||||||
.filter(|u| !old_hot.access.users.contains_key(*u))
|
.filter(|u| !old_hot.access.users.contains_key(*u))
|
||||||
@@ -351,6 +469,16 @@ fn reload_config(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if old_hot.dns_overrides != new_hot.dns_overrides
|
||||||
|
&& let Err(e) = crate::network::dns_overrides::install_entries(&new_hot.dns_overrides)
|
||||||
|
{
|
||||||
|
error!(
|
||||||
|
"config reload: invalid network.dns_overrides: {}; keeping old config",
|
||||||
|
e
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
warn_non_hot_changes(&old_cfg, &new_cfg);
|
warn_non_hot_changes(&old_cfg, &new_cfg);
|
||||||
log_changes(&old_hot, &new_hot, &new_cfg, log_tx, detected_ip_v4, detected_ip_v6);
|
log_changes(&old_hot, &new_hot, &new_cfg, log_tx, detected_ip_v4, detected_ip_v6);
|
||||||
config_tx.send(Arc::new(new_cfg)).ok();
|
config_tx.send(Arc::new(new_cfg)).ok();
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#![allow(deprecated)]
|
#![allow(deprecated)]
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::net::IpAddr;
|
use std::net::{IpAddr, SocketAddr};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
@@ -65,6 +65,33 @@ fn validate_network_cfg(net: &mut NetworkConfig) -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn push_unique_nonempty(target: &mut Vec<String>, value: String) {
|
||||||
|
let trimmed = value.trim();
|
||||||
|
if trimmed.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if !target.iter().any(|existing| existing == trimmed) {
|
||||||
|
target.push(trimmed.to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_valid_ad_tag(tag: &str) -> bool {
|
||||||
|
tag.len() == 32 && tag.chars().all(|ch| ch.is_ascii_hexdigit())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sanitize_ad_tag(ad_tag: &mut Option<String>) {
|
||||||
|
let Some(tag) = ad_tag.as_ref() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
if !is_valid_ad_tag(tag) {
|
||||||
|
warn!(
|
||||||
|
"Invalid general.ad_tag value, expected exactly 32 hex chars; ad_tag is disabled"
|
||||||
|
);
|
||||||
|
*ad_tag = None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ============= Main Config =============
|
// ============= Main Config =============
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||||
@@ -116,8 +143,65 @@ impl ProxyConfig {
|
|||||||
let base_dir = path.as_ref().parent().unwrap_or(Path::new("."));
|
let base_dir = path.as_ref().parent().unwrap_or(Path::new("."));
|
||||||
let processed = preprocess_includes(&content, base_dir, 0)?;
|
let processed = preprocess_includes(&content, base_dir, 0)?;
|
||||||
|
|
||||||
let mut config: ProxyConfig =
|
let parsed_toml: toml::Value =
|
||||||
toml::from_str(&processed).map_err(|e| ProxyError::Config(e.to_string()))?;
|
toml::from_str(&processed).map_err(|e| ProxyError::Config(e.to_string()))?;
|
||||||
|
let general_table = parsed_toml
|
||||||
|
.get("general")
|
||||||
|
.and_then(|value| value.as_table());
|
||||||
|
let network_table = parsed_toml
|
||||||
|
.get("network")
|
||||||
|
.and_then(|value| value.as_table());
|
||||||
|
let update_every_is_explicit = general_table
|
||||||
|
.map(|table| table.contains_key("update_every"))
|
||||||
|
.unwrap_or(false);
|
||||||
|
let legacy_secret_is_explicit = general_table
|
||||||
|
.map(|table| table.contains_key("proxy_secret_auto_reload_secs"))
|
||||||
|
.unwrap_or(false);
|
||||||
|
let legacy_config_is_explicit = general_table
|
||||||
|
.map(|table| table.contains_key("proxy_config_auto_reload_secs"))
|
||||||
|
.unwrap_or(false);
|
||||||
|
let stun_servers_is_explicit = network_table
|
||||||
|
.map(|table| table.contains_key("stun_servers"))
|
||||||
|
.unwrap_or(false);
|
||||||
|
|
||||||
|
let mut config: ProxyConfig =
|
||||||
|
parsed_toml.try_into().map_err(|e| ProxyError::Config(e.to_string()))?;
|
||||||
|
|
||||||
|
if !update_every_is_explicit && (legacy_secret_is_explicit || legacy_config_is_explicit) {
|
||||||
|
config.general.update_every = None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let legacy_nat_stun = config.general.middle_proxy_nat_stun.take();
|
||||||
|
let legacy_nat_stun_servers = std::mem::take(&mut config.general.middle_proxy_nat_stun_servers);
|
||||||
|
let legacy_nat_stun_used = legacy_nat_stun.is_some() || !legacy_nat_stun_servers.is_empty();
|
||||||
|
if stun_servers_is_explicit {
|
||||||
|
let mut explicit_stun_servers = Vec::new();
|
||||||
|
for stun in std::mem::take(&mut config.network.stun_servers) {
|
||||||
|
push_unique_nonempty(&mut explicit_stun_servers, stun);
|
||||||
|
}
|
||||||
|
config.network.stun_servers = explicit_stun_servers;
|
||||||
|
|
||||||
|
if legacy_nat_stun_used {
|
||||||
|
warn!("general.middle_proxy_nat_stun and general.middle_proxy_nat_stun_servers are ignored because network.stun_servers is explicitly set");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Keep the default STUN pool unless network.stun_servers is explicitly overridden.
|
||||||
|
let mut unified_stun_servers = default_stun_servers();
|
||||||
|
if let Some(stun) = legacy_nat_stun {
|
||||||
|
push_unique_nonempty(&mut unified_stun_servers, stun);
|
||||||
|
}
|
||||||
|
for stun in legacy_nat_stun_servers {
|
||||||
|
push_unique_nonempty(&mut unified_stun_servers, stun);
|
||||||
|
}
|
||||||
|
|
||||||
|
config.network.stun_servers = unified_stun_servers;
|
||||||
|
|
||||||
|
if legacy_nat_stun_used {
|
||||||
|
warn!("general.middle_proxy_nat_stun and general.middle_proxy_nat_stun_servers are deprecated; use network.stun_servers");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sanitize_ad_tag(&mut config.general.ad_tag);
|
||||||
|
|
||||||
if let Some(update_every) = config.general.update_every {
|
if let Some(update_every) = config.general.update_every {
|
||||||
if update_every == 0 {
|
if update_every == 0 {
|
||||||
@@ -147,12 +231,73 @@ impl ProxyConfig {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if config.general.stun_nat_probe_concurrency == 0 {
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"general.stun_nat_probe_concurrency must be > 0".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.general.upstream_connect_retry_attempts == 0 {
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"general.upstream_connect_retry_attempts must be > 0".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.general.upstream_unhealthy_fail_threshold == 0 {
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"general.upstream_unhealthy_fail_threshold must be > 0".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.general.rpc_proxy_req_every != 0
|
||||||
|
&& !(10..=300).contains(&config.general.rpc_proxy_req_every)
|
||||||
|
{
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"general.rpc_proxy_req_every must be 0 or within [10, 300]".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
if config.general.me_reinit_every_secs == 0 {
|
if config.general.me_reinit_every_secs == 0 {
|
||||||
return Err(ProxyError::Config(
|
return Err(ProxyError::Config(
|
||||||
"general.me_reinit_every_secs must be > 0".to_string(),
|
"general.me_reinit_every_secs must be > 0".to_string(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if config.general.me_single_endpoint_shadow_writers > 32 {
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"general.me_single_endpoint_shadow_writers must be within [0, 32]".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.general.me_adaptive_floor_min_writers_single_endpoint == 0
|
||||||
|
|| config.general.me_adaptive_floor_min_writers_single_endpoint > 32
|
||||||
|
{
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"general.me_adaptive_floor_min_writers_single_endpoint must be within [1, 32]"
|
||||||
|
.to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.general.me_single_endpoint_outage_backoff_min_ms == 0 {
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"general.me_single_endpoint_outage_backoff_min_ms must be > 0".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.general.me_single_endpoint_outage_backoff_max_ms == 0 {
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"general.me_single_endpoint_outage_backoff_max_ms must be > 0".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.general.me_single_endpoint_outage_backoff_min_ms
|
||||||
|
> config.general.me_single_endpoint_outage_backoff_max_ms
|
||||||
|
{
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"general.me_single_endpoint_outage_backoff_min_ms must be <= general.me_single_endpoint_outage_backoff_max_ms".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
if config.general.beobachten_minutes == 0 {
|
if config.general.beobachten_minutes == 0 {
|
||||||
return Err(ProxyError::Config(
|
return Err(ProxyError::Config(
|
||||||
"general.beobachten_minutes must be > 0".to_string(),
|
"general.beobachten_minutes must be > 0".to_string(),
|
||||||
@@ -203,12 +348,24 @@ impl ProxyConfig {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if config.general.me_snapshot_min_proxy_for_lines == 0 {
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"general.me_snapshot_min_proxy_for_lines must be > 0".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
if config.general.proxy_secret_stable_snapshots == 0 {
|
if config.general.proxy_secret_stable_snapshots == 0 {
|
||||||
return Err(ProxyError::Config(
|
return Err(ProxyError::Config(
|
||||||
"general.proxy_secret_stable_snapshots must be > 0".to_string(),
|
"general.proxy_secret_stable_snapshots must be > 0".to_string(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if config.general.me_reinit_trigger_channel == 0 {
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"general.me_reinit_trigger_channel must be > 0".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
if !(32..=4096).contains(&config.general.proxy_secret_len_max) {
|
if !(32..=4096).contains(&config.general.proxy_secret_len_max) {
|
||||||
return Err(ProxyError::Config(
|
return Err(ProxyError::Config(
|
||||||
"general.proxy_secret_len_max must be within [32, 4096]".to_string(),
|
"general.proxy_secret_len_max must be within [32, 4096]".to_string(),
|
||||||
@@ -221,6 +378,44 @@ impl ProxyConfig {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if config.general.me_route_backpressure_base_timeout_ms == 0 {
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"general.me_route_backpressure_base_timeout_ms must be > 0".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.general.me_route_backpressure_high_timeout_ms
|
||||||
|
< config.general.me_route_backpressure_base_timeout_ms
|
||||||
|
{
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"general.me_route_backpressure_high_timeout_ms must be >= general.me_route_backpressure_base_timeout_ms".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if !(1..=100).contains(&config.general.me_route_backpressure_high_watermark_pct) {
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"general.me_route_backpressure_high_watermark_pct must be within [1, 100]".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.server.api.request_body_limit_bytes == 0 {
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"server.api.request_body_limit_bytes must be > 0".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.server.api.minimal_runtime_cache_ttl_ms > 60_000 {
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"server.api.minimal_runtime_cache_ttl_ms must be within [0, 60000]".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.server.api.listen.parse::<SocketAddr>().is_err() {
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"server.api.listen must be in IP:PORT format".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
if config.general.effective_me_pool_force_close_secs() > 0
|
if config.general.effective_me_pool_force_close_secs() > 0
|
||||||
&& config.general.effective_me_pool_force_close_secs()
|
&& config.general.effective_me_pool_force_close_secs()
|
||||||
< config.general.me_pool_drain_ttl_secs
|
< config.general.me_pool_drain_ttl_secs
|
||||||
@@ -309,6 +504,7 @@ impl ProxyConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
validate_network_cfg(&mut config.network)?;
|
validate_network_cfg(&mut config.network)?;
|
||||||
|
crate::network::dns_overrides::validate_entries(&config.network.dns_overrides)?;
|
||||||
|
|
||||||
if config.general.use_middle_proxy && config.network.ipv6 == Some(true) {
|
if config.general.use_middle_proxy && config.network.ipv6 == Some(true) {
|
||||||
warn!("IPv6 with Middle Proxy is experimental and may cause KDF address mismatch; consider disabling IPv6 or ME");
|
warn!("IPv6 with Middle Proxy is experimental and may cause KDF address mismatch; consider disabling IPv6 or ME");
|
||||||
@@ -409,16 +605,21 @@ impl ProxyConfig {
|
|||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(tag) = &self.general.ad_tag {
|
for (user, tag) in &self.access.user_ad_tags {
|
||||||
let zeros = "00000000000000000000000000000000";
|
let zeros = "00000000000000000000000000000000";
|
||||||
if tag == zeros {
|
if !is_valid_ad_tag(tag) {
|
||||||
warn!("ad_tag is all zeros; register a valid proxy tag via @MTProxybot to enable sponsored channel");
|
return Err(ProxyError::Config(format!(
|
||||||
|
"access.user_ad_tags['{}'] must be exactly 32 hex characters",
|
||||||
|
user
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
if tag.len() != 32 || tag.chars().any(|c| !c.is_ascii_hexdigit()) {
|
if tag == zeros {
|
||||||
warn!("ad_tag is not a 32-char hex string; ensure you use value issued by @MTProxybot");
|
warn!(user = %user, "user ad_tag is all zeros; register a valid proxy tag via @MTProxybot to enable sponsored channel");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
crate::network::dns_overrides::validate_entries(&self.network.dns_overrides)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -437,21 +638,103 @@ mod tests {
|
|||||||
"#;
|
"#;
|
||||||
let cfg: ProxyConfig = toml::from_str(toml).unwrap();
|
let cfg: ProxyConfig = toml::from_str(toml).unwrap();
|
||||||
|
|
||||||
assert_eq!(cfg.network.ipv6, None);
|
assert_eq!(cfg.network.ipv6, default_network_ipv6());
|
||||||
assert!(!cfg.network.stun_tcp_fallback);
|
assert_eq!(cfg.network.stun_use, default_true());
|
||||||
assert_eq!(cfg.general.middle_proxy_warm_standby, 0);
|
assert_eq!(cfg.network.stun_tcp_fallback, default_stun_tcp_fallback());
|
||||||
assert_eq!(cfg.general.me_reconnect_max_concurrent_per_dc, 0);
|
assert_eq!(
|
||||||
assert_eq!(cfg.general.me_reconnect_fast_retry_count, 0);
|
cfg.general.middle_proxy_warm_standby,
|
||||||
assert_eq!(cfg.general.update_every, None);
|
default_middle_proxy_warm_standby()
|
||||||
assert_eq!(cfg.server.listen_addr_ipv4, None);
|
);
|
||||||
assert_eq!(cfg.server.listen_addr_ipv6, None);
|
assert_eq!(
|
||||||
assert!(cfg.access.users.is_empty());
|
cfg.general.me_reconnect_max_concurrent_per_dc,
|
||||||
|
default_me_reconnect_max_concurrent_per_dc()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
cfg.general.me_reconnect_fast_retry_count,
|
||||||
|
default_me_reconnect_fast_retry_count()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
cfg.general.me_single_endpoint_shadow_writers,
|
||||||
|
default_me_single_endpoint_shadow_writers()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
cfg.general.me_single_endpoint_outage_mode_enabled,
|
||||||
|
default_me_single_endpoint_outage_mode_enabled()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
cfg.general.me_single_endpoint_outage_disable_quarantine,
|
||||||
|
default_me_single_endpoint_outage_disable_quarantine()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
cfg.general.me_single_endpoint_outage_backoff_min_ms,
|
||||||
|
default_me_single_endpoint_outage_backoff_min_ms()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
cfg.general.me_single_endpoint_outage_backoff_max_ms,
|
||||||
|
default_me_single_endpoint_outage_backoff_max_ms()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
cfg.general.me_single_endpoint_shadow_rotate_every_secs,
|
||||||
|
default_me_single_endpoint_shadow_rotate_every_secs()
|
||||||
|
);
|
||||||
|
assert_eq!(cfg.general.me_floor_mode, MeFloorMode::default());
|
||||||
|
assert_eq!(
|
||||||
|
cfg.general.me_adaptive_floor_idle_secs,
|
||||||
|
default_me_adaptive_floor_idle_secs()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
cfg.general.me_adaptive_floor_min_writers_single_endpoint,
|
||||||
|
default_me_adaptive_floor_min_writers_single_endpoint()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
cfg.general.me_adaptive_floor_recover_grace_secs,
|
||||||
|
default_me_adaptive_floor_recover_grace_secs()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
cfg.general.upstream_connect_retry_attempts,
|
||||||
|
default_upstream_connect_retry_attempts()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
cfg.general.upstream_connect_retry_backoff_ms,
|
||||||
|
default_upstream_connect_retry_backoff_ms()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
cfg.general.upstream_unhealthy_fail_threshold,
|
||||||
|
default_upstream_unhealthy_fail_threshold()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
cfg.general.upstream_connect_failfast_hard_errors,
|
||||||
|
default_upstream_connect_failfast_hard_errors()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
cfg.general.rpc_proxy_req_every,
|
||||||
|
default_rpc_proxy_req_every()
|
||||||
|
);
|
||||||
|
assert_eq!(cfg.general.update_every, default_update_every());
|
||||||
|
assert_eq!(cfg.server.listen_addr_ipv4, default_listen_addr_ipv4());
|
||||||
|
assert_eq!(cfg.server.listen_addr_ipv6, default_listen_addr_ipv6_opt());
|
||||||
|
assert_eq!(cfg.server.api.listen, default_api_listen());
|
||||||
|
assert_eq!(cfg.server.api.whitelist, default_api_whitelist());
|
||||||
|
assert_eq!(
|
||||||
|
cfg.server.api.request_body_limit_bytes,
|
||||||
|
default_api_request_body_limit_bytes()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
cfg.server.api.minimal_runtime_enabled,
|
||||||
|
default_api_minimal_runtime_enabled()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
cfg.server.api.minimal_runtime_cache_ttl_ms,
|
||||||
|
default_api_minimal_runtime_cache_ttl_ms()
|
||||||
|
);
|
||||||
|
assert_eq!(cfg.access.users, default_access_users());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn impl_defaults_are_sourced_from_default_helpers() {
|
fn impl_defaults_are_sourced_from_default_helpers() {
|
||||||
let network = NetworkConfig::default();
|
let network = NetworkConfig::default();
|
||||||
assert_eq!(network.ipv6, default_network_ipv6());
|
assert_eq!(network.ipv6, default_network_ipv6());
|
||||||
|
assert_eq!(network.stun_use, default_true());
|
||||||
assert_eq!(network.stun_tcp_fallback, default_stun_tcp_fallback());
|
assert_eq!(network.stun_tcp_fallback, default_stun_tcp_fallback());
|
||||||
|
|
||||||
let general = GeneralConfig::default();
|
let general = GeneralConfig::default();
|
||||||
@@ -467,10 +750,78 @@ mod tests {
|
|||||||
general.me_reconnect_fast_retry_count,
|
general.me_reconnect_fast_retry_count,
|
||||||
default_me_reconnect_fast_retry_count()
|
default_me_reconnect_fast_retry_count()
|
||||||
);
|
);
|
||||||
|
assert_eq!(
|
||||||
|
general.me_single_endpoint_shadow_writers,
|
||||||
|
default_me_single_endpoint_shadow_writers()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
general.me_single_endpoint_outage_mode_enabled,
|
||||||
|
default_me_single_endpoint_outage_mode_enabled()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
general.me_single_endpoint_outage_disable_quarantine,
|
||||||
|
default_me_single_endpoint_outage_disable_quarantine()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
general.me_single_endpoint_outage_backoff_min_ms,
|
||||||
|
default_me_single_endpoint_outage_backoff_min_ms()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
general.me_single_endpoint_outage_backoff_max_ms,
|
||||||
|
default_me_single_endpoint_outage_backoff_max_ms()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
general.me_single_endpoint_shadow_rotate_every_secs,
|
||||||
|
default_me_single_endpoint_shadow_rotate_every_secs()
|
||||||
|
);
|
||||||
|
assert_eq!(general.me_floor_mode, MeFloorMode::default());
|
||||||
|
assert_eq!(
|
||||||
|
general.me_adaptive_floor_idle_secs,
|
||||||
|
default_me_adaptive_floor_idle_secs()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
general.me_adaptive_floor_min_writers_single_endpoint,
|
||||||
|
default_me_adaptive_floor_min_writers_single_endpoint()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
general.me_adaptive_floor_recover_grace_secs,
|
||||||
|
default_me_adaptive_floor_recover_grace_secs()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
general.upstream_connect_retry_attempts,
|
||||||
|
default_upstream_connect_retry_attempts()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
general.upstream_connect_retry_backoff_ms,
|
||||||
|
default_upstream_connect_retry_backoff_ms()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
general.upstream_unhealthy_fail_threshold,
|
||||||
|
default_upstream_unhealthy_fail_threshold()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
general.upstream_connect_failfast_hard_errors,
|
||||||
|
default_upstream_connect_failfast_hard_errors()
|
||||||
|
);
|
||||||
|
assert_eq!(general.rpc_proxy_req_every, default_rpc_proxy_req_every());
|
||||||
assert_eq!(general.update_every, default_update_every());
|
assert_eq!(general.update_every, default_update_every());
|
||||||
|
|
||||||
let server = ServerConfig::default();
|
let server = ServerConfig::default();
|
||||||
assert_eq!(server.listen_addr_ipv6, Some(default_listen_addr_ipv6()));
|
assert_eq!(server.listen_addr_ipv6, Some(default_listen_addr_ipv6()));
|
||||||
|
assert_eq!(server.api.listen, default_api_listen());
|
||||||
|
assert_eq!(server.api.whitelist, default_api_whitelist());
|
||||||
|
assert_eq!(
|
||||||
|
server.api.request_body_limit_bytes,
|
||||||
|
default_api_request_body_limit_bytes()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
server.api.minimal_runtime_enabled,
|
||||||
|
default_api_minimal_runtime_enabled()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
server.api.minimal_runtime_cache_ttl_ms,
|
||||||
|
default_api_minimal_runtime_cache_ttl_ms()
|
||||||
|
);
|
||||||
|
|
||||||
let access = AccessConfig::default();
|
let access = AccessConfig::default();
|
||||||
assert_eq!(access.users, default_access_users());
|
assert_eq!(access.users, default_access_users());
|
||||||
@@ -579,6 +930,26 @@ mod tests {
|
|||||||
let _ = std::fs::remove_file(path);
|
let _ = std::fs::remove_file(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn stun_nat_probe_concurrency_zero_is_rejected() {
|
||||||
|
let toml = r#"
|
||||||
|
[general]
|
||||||
|
stun_nat_probe_concurrency = 0
|
||||||
|
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "example.com"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
user = "00000000000000000000000000000000"
|
||||||
|
"#;
|
||||||
|
let dir = std::env::temp_dir();
|
||||||
|
let path = dir.join("telemt_stun_nat_probe_concurrency_zero_test.toml");
|
||||||
|
std::fs::write(&path, toml).unwrap();
|
||||||
|
let err = ProxyConfig::load(&path).unwrap_err().to_string();
|
||||||
|
assert!(err.contains("general.stun_nat_probe_concurrency must be > 0"));
|
||||||
|
let _ = std::fs::remove_file(path);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn me_reinit_every_default_is_set() {
|
fn me_reinit_every_default_is_set() {
|
||||||
let toml = r#"
|
let toml = r#"
|
||||||
@@ -619,6 +990,189 @@ mod tests {
|
|||||||
let _ = std::fs::remove_file(path);
|
let _ = std::fs::remove_file(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn me_single_endpoint_outage_backoff_range_is_validated() {
|
||||||
|
let toml = r#"
|
||||||
|
[general]
|
||||||
|
me_single_endpoint_outage_backoff_min_ms = 4000
|
||||||
|
me_single_endpoint_outage_backoff_max_ms = 3000
|
||||||
|
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "example.com"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
user = "00000000000000000000000000000000"
|
||||||
|
"#;
|
||||||
|
let dir = std::env::temp_dir();
|
||||||
|
let path = dir.join("telemt_me_single_endpoint_outage_backoff_range_test.toml");
|
||||||
|
std::fs::write(&path, toml).unwrap();
|
||||||
|
let err = ProxyConfig::load(&path).unwrap_err().to_string();
|
||||||
|
assert!(err.contains(
|
||||||
|
"general.me_single_endpoint_outage_backoff_min_ms must be <= general.me_single_endpoint_outage_backoff_max_ms"
|
||||||
|
));
|
||||||
|
let _ = std::fs::remove_file(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn me_single_endpoint_shadow_writers_too_large_is_rejected() {
|
||||||
|
let toml = r#"
|
||||||
|
[general]
|
||||||
|
me_single_endpoint_shadow_writers = 33
|
||||||
|
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "example.com"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
user = "00000000000000000000000000000000"
|
||||||
|
"#;
|
||||||
|
let dir = std::env::temp_dir();
|
||||||
|
let path = dir.join("telemt_me_single_endpoint_shadow_writers_limit_test.toml");
|
||||||
|
std::fs::write(&path, toml).unwrap();
|
||||||
|
let err = ProxyConfig::load(&path).unwrap_err().to_string();
|
||||||
|
assert!(err.contains("general.me_single_endpoint_shadow_writers must be within [0, 32]"));
|
||||||
|
let _ = std::fs::remove_file(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn me_adaptive_floor_min_writers_out_of_range_is_rejected() {
|
||||||
|
let toml = r#"
|
||||||
|
[general]
|
||||||
|
me_adaptive_floor_min_writers_single_endpoint = 0
|
||||||
|
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "example.com"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
user = "00000000000000000000000000000000"
|
||||||
|
"#;
|
||||||
|
let dir = std::env::temp_dir();
|
||||||
|
let path = dir.join("telemt_me_adaptive_floor_min_writers_out_of_range_test.toml");
|
||||||
|
std::fs::write(&path, toml).unwrap();
|
||||||
|
let err = ProxyConfig::load(&path).unwrap_err().to_string();
|
||||||
|
assert!(
|
||||||
|
err.contains(
|
||||||
|
"general.me_adaptive_floor_min_writers_single_endpoint must be within [1, 32]"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
let _ = std::fs::remove_file(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn me_floor_mode_adaptive_is_parsed() {
|
||||||
|
let toml = r#"
|
||||||
|
[general]
|
||||||
|
me_floor_mode = "adaptive"
|
||||||
|
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "example.com"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
user = "00000000000000000000000000000000"
|
||||||
|
"#;
|
||||||
|
let dir = std::env::temp_dir();
|
||||||
|
let path = dir.join("telemt_me_floor_mode_adaptive_test.toml");
|
||||||
|
std::fs::write(&path, toml).unwrap();
|
||||||
|
let cfg = ProxyConfig::load(&path).unwrap();
|
||||||
|
assert_eq!(cfg.general.me_floor_mode, MeFloorMode::Adaptive);
|
||||||
|
let _ = std::fs::remove_file(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn upstream_connect_retry_attempts_zero_is_rejected() {
|
||||||
|
let toml = r#"
|
||||||
|
[general]
|
||||||
|
upstream_connect_retry_attempts = 0
|
||||||
|
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "example.com"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
user = "00000000000000000000000000000000"
|
||||||
|
"#;
|
||||||
|
let dir = std::env::temp_dir();
|
||||||
|
let path = dir.join("telemt_upstream_connect_retry_attempts_zero_test.toml");
|
||||||
|
std::fs::write(&path, toml).unwrap();
|
||||||
|
let err = ProxyConfig::load(&path).unwrap_err().to_string();
|
||||||
|
assert!(err.contains("general.upstream_connect_retry_attempts must be > 0"));
|
||||||
|
let _ = std::fs::remove_file(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn upstream_unhealthy_fail_threshold_zero_is_rejected() {
|
||||||
|
let toml = r#"
|
||||||
|
[general]
|
||||||
|
upstream_unhealthy_fail_threshold = 0
|
||||||
|
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "example.com"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
user = "00000000000000000000000000000000"
|
||||||
|
"#;
|
||||||
|
let dir = std::env::temp_dir();
|
||||||
|
let path = dir.join("telemt_upstream_unhealthy_fail_threshold_zero_test.toml");
|
||||||
|
std::fs::write(&path, toml).unwrap();
|
||||||
|
let err = ProxyConfig::load(&path).unwrap_err().to_string();
|
||||||
|
assert!(err.contains("general.upstream_unhealthy_fail_threshold must be > 0"));
|
||||||
|
let _ = std::fs::remove_file(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn rpc_proxy_req_every_out_of_range_is_rejected() {
|
||||||
|
let toml = r#"
|
||||||
|
[general]
|
||||||
|
rpc_proxy_req_every = 9
|
||||||
|
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "example.com"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
user = "00000000000000000000000000000000"
|
||||||
|
"#;
|
||||||
|
let dir = std::env::temp_dir();
|
||||||
|
let path = dir.join("telemt_rpc_proxy_req_every_out_of_range_test.toml");
|
||||||
|
std::fs::write(&path, toml).unwrap();
|
||||||
|
let err = ProxyConfig::load(&path).unwrap_err().to_string();
|
||||||
|
assert!(err.contains("general.rpc_proxy_req_every must be 0 or within [10, 300]"));
|
||||||
|
let _ = std::fs::remove_file(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn rpc_proxy_req_every_zero_and_valid_range_are_accepted() {
|
||||||
|
let toml_zero = r#"
|
||||||
|
[general]
|
||||||
|
rpc_proxy_req_every = 0
|
||||||
|
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "example.com"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
user = "00000000000000000000000000000000"
|
||||||
|
"#;
|
||||||
|
let dir = std::env::temp_dir();
|
||||||
|
let path_zero = dir.join("telemt_rpc_proxy_req_every_zero_ok_test.toml");
|
||||||
|
std::fs::write(&path_zero, toml_zero).unwrap();
|
||||||
|
let cfg_zero = ProxyConfig::load(&path_zero).unwrap();
|
||||||
|
assert_eq!(cfg_zero.general.rpc_proxy_req_every, 0);
|
||||||
|
let _ = std::fs::remove_file(path_zero);
|
||||||
|
|
||||||
|
let toml_valid = r#"
|
||||||
|
[general]
|
||||||
|
rpc_proxy_req_every = 40
|
||||||
|
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "example.com"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
user = "00000000000000000000000000000000"
|
||||||
|
"#;
|
||||||
|
let path_valid = dir.join("telemt_rpc_proxy_req_every_valid_ok_test.toml");
|
||||||
|
std::fs::write(&path_valid, toml_valid).unwrap();
|
||||||
|
let cfg_valid = ProxyConfig::load(&path_valid).unwrap();
|
||||||
|
assert_eq!(cfg_valid.general.rpc_proxy_req_every, 40);
|
||||||
|
let _ = std::fs::remove_file(path_valid);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn me_hardswap_warmup_defaults_are_set() {
|
fn me_hardswap_warmup_defaults_are_set() {
|
||||||
let toml = r#"
|
let toml = r#"
|
||||||
@@ -814,6 +1368,28 @@ mod tests {
|
|||||||
let _ = std::fs::remove_file(path);
|
let _ = std::fs::remove_file(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn api_minimal_runtime_cache_ttl_out_of_range_is_rejected() {
|
||||||
|
let toml = r#"
|
||||||
|
[server.api]
|
||||||
|
enabled = true
|
||||||
|
listen = "127.0.0.1:9091"
|
||||||
|
minimal_runtime_cache_ttl_ms = 70000
|
||||||
|
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "example.com"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
user = "00000000000000000000000000000000"
|
||||||
|
"#;
|
||||||
|
let dir = std::env::temp_dir();
|
||||||
|
let path = dir.join("telemt_api_minimal_runtime_cache_ttl_invalid_test.toml");
|
||||||
|
std::fs::write(&path, toml).unwrap();
|
||||||
|
let err = ProxyConfig::load(&path).unwrap_err().to_string();
|
||||||
|
assert!(err.contains("server.api.minimal_runtime_cache_ttl_ms must be within [0, 60000]"));
|
||||||
|
let _ = std::fs::remove_file(path);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn force_close_bumped_when_below_drain_ttl() {
|
fn force_close_bumped_when_below_drain_ttl() {
|
||||||
let toml = r#"
|
let toml = r#"
|
||||||
@@ -834,4 +1410,108 @@ mod tests {
|
|||||||
assert_eq!(cfg.general.me_reinit_drain_timeout_secs, 90);
|
assert_eq!(cfg.general.me_reinit_drain_timeout_secs, 90);
|
||||||
let _ = std::fs::remove_file(path);
|
let _ = std::fs::remove_file(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn invalid_ad_tag_is_disabled_during_load() {
|
||||||
|
let toml = r#"
|
||||||
|
[general]
|
||||||
|
ad_tag = "not_hex"
|
||||||
|
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "example.com"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
user = "00000000000000000000000000000000"
|
||||||
|
"#;
|
||||||
|
let dir = std::env::temp_dir();
|
||||||
|
let path = dir.join("telemt_invalid_ad_tag_test.toml");
|
||||||
|
std::fs::write(&path, toml).unwrap();
|
||||||
|
let cfg = ProxyConfig::load(&path).unwrap();
|
||||||
|
assert!(cfg.general.ad_tag.is_none());
|
||||||
|
let _ = std::fs::remove_file(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn valid_ad_tag_is_preserved_during_load() {
|
||||||
|
let toml = r#"
|
||||||
|
[general]
|
||||||
|
ad_tag = "00112233445566778899aabbccddeeff"
|
||||||
|
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "example.com"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
user = "00000000000000000000000000000000"
|
||||||
|
"#;
|
||||||
|
let dir = std::env::temp_dir();
|
||||||
|
let path = dir.join("telemt_valid_ad_tag_test.toml");
|
||||||
|
std::fs::write(&path, toml).unwrap();
|
||||||
|
let cfg = ProxyConfig::load(&path).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
cfg.general.ad_tag.as_deref(),
|
||||||
|
Some("00112233445566778899aabbccddeeff")
|
||||||
|
);
|
||||||
|
let _ = std::fs::remove_file(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn invalid_user_ad_tag_reports_access_user_ad_tags_key() {
|
||||||
|
let toml = r#"
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "example.com"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
alice = "00000000000000000000000000000000"
|
||||||
|
|
||||||
|
[access.user_ad_tags]
|
||||||
|
alice = "not_hex"
|
||||||
|
"#;
|
||||||
|
let dir = std::env::temp_dir();
|
||||||
|
let path = dir.join("telemt_invalid_user_ad_tag_message_test.toml");
|
||||||
|
std::fs::write(&path, toml).unwrap();
|
||||||
|
let cfg = ProxyConfig::load(&path).unwrap();
|
||||||
|
let err = cfg.validate().unwrap_err().to_string();
|
||||||
|
assert!(err.contains("access.user_ad_tags['alice'] must be exactly 32 hex characters"));
|
||||||
|
let _ = std::fs::remove_file(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn invalid_dns_override_is_rejected() {
|
||||||
|
let toml = r#"
|
||||||
|
[network]
|
||||||
|
dns_overrides = ["example.com:443:2001:db8::10"]
|
||||||
|
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "example.com"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
user = "00000000000000000000000000000000"
|
||||||
|
"#;
|
||||||
|
let dir = std::env::temp_dir();
|
||||||
|
let path = dir.join("telemt_invalid_dns_override_test.toml");
|
||||||
|
std::fs::write(&path, toml).unwrap();
|
||||||
|
let err = ProxyConfig::load(&path).unwrap_err().to_string();
|
||||||
|
assert!(err.contains("must be bracketed"));
|
||||||
|
let _ = std::fs::remove_file(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn valid_dns_override_is_accepted() {
|
||||||
|
let toml = r#"
|
||||||
|
[network]
|
||||||
|
dns_overrides = ["example.com:443:127.0.0.1", "example.net:443:[2001:db8::10]"]
|
||||||
|
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "example.com"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
user = "00000000000000000000000000000000"
|
||||||
|
"#;
|
||||||
|
let dir = std::env::temp_dir();
|
||||||
|
let path = dir.join("telemt_valid_dns_override_test.toml");
|
||||||
|
std::fs::write(&path, toml).unwrap();
|
||||||
|
let cfg = ProxyConfig::load(&path).unwrap();
|
||||||
|
assert_eq!(cfg.network.dns_overrides.len(), 2);
|
||||||
|
let _ = std::fs::remove_file(path);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,6 +59,151 @@ impl std::fmt::Display for LogLevel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Middle-End telemetry verbosity level.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
|
||||||
|
#[serde(rename_all = "lowercase")]
|
||||||
|
pub enum MeTelemetryLevel {
|
||||||
|
#[default]
|
||||||
|
Normal,
|
||||||
|
Silent,
|
||||||
|
Debug,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MeTelemetryLevel {
|
||||||
|
pub fn as_u8(self) -> u8 {
|
||||||
|
match self {
|
||||||
|
MeTelemetryLevel::Silent => 0,
|
||||||
|
MeTelemetryLevel::Normal => 1,
|
||||||
|
MeTelemetryLevel::Debug => 2,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_u8(raw: u8) -> Self {
|
||||||
|
match raw {
|
||||||
|
0 => MeTelemetryLevel::Silent,
|
||||||
|
2 => MeTelemetryLevel::Debug,
|
||||||
|
_ => MeTelemetryLevel::Normal,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn allows_normal(self) -> bool {
|
||||||
|
!matches!(self, MeTelemetryLevel::Silent)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn allows_debug(self) -> bool {
|
||||||
|
matches!(self, MeTelemetryLevel::Debug)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for MeTelemetryLevel {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
MeTelemetryLevel::Silent => write!(f, "silent"),
|
||||||
|
MeTelemetryLevel::Normal => write!(f, "normal"),
|
||||||
|
MeTelemetryLevel::Debug => write!(f, "debug"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Middle-End SOCKS KDF fallback policy.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
|
||||||
|
#[serde(rename_all = "lowercase")]
|
||||||
|
pub enum MeSocksKdfPolicy {
|
||||||
|
#[default]
|
||||||
|
Strict,
|
||||||
|
Compat,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MeSocksKdfPolicy {
|
||||||
|
pub fn as_u8(self) -> u8 {
|
||||||
|
match self {
|
||||||
|
MeSocksKdfPolicy::Strict => 0,
|
||||||
|
MeSocksKdfPolicy::Compat => 1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_u8(raw: u8) -> Self {
|
||||||
|
match raw {
|
||||||
|
1 => MeSocksKdfPolicy::Compat,
|
||||||
|
_ => MeSocksKdfPolicy::Strict,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Stale ME writer bind policy during drain window.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
|
||||||
|
#[serde(rename_all = "lowercase")]
|
||||||
|
pub enum MeBindStaleMode {
|
||||||
|
Never,
|
||||||
|
#[default]
|
||||||
|
Ttl,
|
||||||
|
Always,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MeBindStaleMode {
|
||||||
|
pub fn as_u8(self) -> u8 {
|
||||||
|
match self {
|
||||||
|
MeBindStaleMode::Never => 0,
|
||||||
|
MeBindStaleMode::Ttl => 1,
|
||||||
|
MeBindStaleMode::Always => 2,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_u8(raw: u8) -> Self {
|
||||||
|
match raw {
|
||||||
|
0 => MeBindStaleMode::Never,
|
||||||
|
2 => MeBindStaleMode::Always,
|
||||||
|
_ => MeBindStaleMode::Ttl,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Middle-End writer floor policy mode.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
|
||||||
|
#[serde(rename_all = "lowercase")]
|
||||||
|
pub enum MeFloorMode {
|
||||||
|
Static,
|
||||||
|
#[default]
|
||||||
|
Adaptive,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MeFloorMode {
|
||||||
|
pub fn as_u8(self) -> u8 {
|
||||||
|
match self {
|
||||||
|
MeFloorMode::Static => 0,
|
||||||
|
MeFloorMode::Adaptive => 1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_u8(raw: u8) -> Self {
|
||||||
|
match raw {
|
||||||
|
1 => MeFloorMode::Adaptive,
|
||||||
|
_ => MeFloorMode::Static,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Telemetry controls for hot-path counters and ME diagnostics.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
pub struct TelemetryConfig {
|
||||||
|
#[serde(default = "default_true")]
|
||||||
|
pub core_enabled: bool,
|
||||||
|
#[serde(default = "default_true")]
|
||||||
|
pub user_enabled: bool,
|
||||||
|
#[serde(default)]
|
||||||
|
pub me_level: MeTelemetryLevel,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for TelemetryConfig {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
core_enabled: default_true(),
|
||||||
|
user_enabled: default_true(),
|
||||||
|
me_level: MeTelemetryLevel::Normal,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ============= Sub-Configs =============
|
// ============= Sub-Configs =============
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
@@ -87,7 +232,7 @@ pub struct NetworkConfig {
|
|||||||
pub ipv4: bool,
|
pub ipv4: bool,
|
||||||
|
|
||||||
/// None = auto-detect IPv6 availability.
|
/// None = auto-detect IPv6 availability.
|
||||||
#[serde(default)]
|
#[serde(default = "default_network_ipv6")]
|
||||||
pub ipv6: Option<bool>,
|
pub ipv6: Option<bool>,
|
||||||
|
|
||||||
/// 4 or 6.
|
/// 4 or 6.
|
||||||
@@ -97,12 +242,17 @@ pub struct NetworkConfig {
|
|||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub multipath: bool,
|
pub multipath: bool,
|
||||||
|
|
||||||
|
/// Global switch for STUN probing.
|
||||||
|
/// When false, STUN is fully disabled and only non-STUN detection remains.
|
||||||
|
#[serde(default = "default_true")]
|
||||||
|
pub stun_use: bool,
|
||||||
|
|
||||||
/// STUN servers list for public IP discovery.
|
/// STUN servers list for public IP discovery.
|
||||||
#[serde(default = "default_stun_servers")]
|
#[serde(default = "default_stun_servers")]
|
||||||
pub stun_servers: Vec<String>,
|
pub stun_servers: Vec<String>,
|
||||||
|
|
||||||
/// Enable TCP STUN fallback when UDP is blocked.
|
/// Enable TCP STUN fallback when UDP is blocked.
|
||||||
#[serde(default)]
|
#[serde(default = "default_stun_tcp_fallback")]
|
||||||
pub stun_tcp_fallback: bool,
|
pub stun_tcp_fallback: bool,
|
||||||
|
|
||||||
/// HTTP-based public IP detection endpoints (fallback after STUN).
|
/// HTTP-based public IP detection endpoints (fallback after STUN).
|
||||||
@@ -112,6 +262,11 @@ pub struct NetworkConfig {
|
|||||||
/// Cache file path for detected public IP.
|
/// Cache file path for detected public IP.
|
||||||
#[serde(default = "default_cache_public_ip_path")]
|
#[serde(default = "default_cache_public_ip_path")]
|
||||||
pub cache_public_ip_path: String,
|
pub cache_public_ip_path: String,
|
||||||
|
|
||||||
|
/// Runtime DNS overrides in `host:port:ip` format.
|
||||||
|
/// IPv6 IP values must be bracketed: `[2001:db8::1]`.
|
||||||
|
#[serde(default)]
|
||||||
|
pub dns_overrides: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for NetworkConfig {
|
impl Default for NetworkConfig {
|
||||||
@@ -121,10 +276,12 @@ impl Default for NetworkConfig {
|
|||||||
ipv6: default_network_ipv6(),
|
ipv6: default_network_ipv6(),
|
||||||
prefer: default_prefer_4(),
|
prefer: default_prefer_4(),
|
||||||
multipath: false,
|
multipath: false,
|
||||||
|
stun_use: default_true(),
|
||||||
stun_servers: default_stun_servers(),
|
stun_servers: default_stun_servers(),
|
||||||
stun_tcp_fallback: default_stun_tcp_fallback(),
|
stun_tcp_fallback: default_stun_tcp_fallback(),
|
||||||
http_ip_detect_urls: default_http_ip_detect_urls(),
|
http_ip_detect_urls: default_http_ip_detect_urls(),
|
||||||
cache_public_ip_path: default_cache_public_ip_path(),
|
cache_public_ip_path: default_cache_public_ip_path(),
|
||||||
|
dns_overrides: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -140,40 +297,47 @@ pub struct GeneralConfig {
|
|||||||
#[serde(default = "default_true")]
|
#[serde(default = "default_true")]
|
||||||
pub fast_mode: bool,
|
pub fast_mode: bool,
|
||||||
|
|
||||||
#[serde(default)]
|
#[serde(default = "default_true")]
|
||||||
pub use_middle_proxy: bool,
|
pub use_middle_proxy: bool,
|
||||||
|
|
||||||
#[serde(default)]
|
|
||||||
pub ad_tag: Option<String>,
|
|
||||||
|
|
||||||
/// Path to proxy-secret binary file (auto-downloaded if absent).
|
/// Path to proxy-secret binary file (auto-downloaded if absent).
|
||||||
/// Infrastructure secret from https://core.telegram.org/getProxySecret.
|
/// Infrastructure secret from https://core.telegram.org/getProxySecret.
|
||||||
#[serde(default)]
|
#[serde(default = "default_proxy_secret_path")]
|
||||||
pub proxy_secret_path: Option<String>,
|
pub proxy_secret_path: Option<String>,
|
||||||
|
|
||||||
|
/// Global ad_tag (32 hex chars from @MTProxybot). Fallback when user has no per-user tag in access.user_ad_tags.
|
||||||
|
#[serde(default)]
|
||||||
|
pub ad_tag: Option<String>,
|
||||||
|
|
||||||
/// Public IP override for middle-proxy NAT environments.
|
/// Public IP override for middle-proxy NAT environments.
|
||||||
/// When set, this IP is used in ME key derivation and RPC_PROXY_REQ "our_addr".
|
/// When set, this IP is used in ME key derivation and RPC_PROXY_REQ "our_addr".
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub middle_proxy_nat_ip: Option<IpAddr>,
|
pub middle_proxy_nat_ip: Option<IpAddr>,
|
||||||
|
|
||||||
/// Enable STUN-based NAT probing to discover public IP:port for ME KDF.
|
/// Enable STUN-based NAT probing to discover public IP:port for ME KDF.
|
||||||
#[serde(default)]
|
#[serde(default = "default_true")]
|
||||||
pub middle_proxy_nat_probe: bool,
|
pub middle_proxy_nat_probe: bool,
|
||||||
|
|
||||||
/// Optional STUN server address (host:port) for NAT probing.
|
/// Deprecated legacy single STUN server for NAT probing.
|
||||||
#[serde(default)]
|
/// Use `network.stun_servers` instead.
|
||||||
|
#[serde(default = "default_middle_proxy_nat_stun")]
|
||||||
pub middle_proxy_nat_stun: Option<String>,
|
pub middle_proxy_nat_stun: Option<String>,
|
||||||
|
|
||||||
/// Optional list of STUN servers for NAT probing fallback.
|
/// Deprecated legacy STUN list for NAT probing fallback.
|
||||||
#[serde(default)]
|
/// Use `network.stun_servers` instead.
|
||||||
|
#[serde(default = "default_middle_proxy_nat_stun_servers")]
|
||||||
pub middle_proxy_nat_stun_servers: Vec<String>,
|
pub middle_proxy_nat_stun_servers: Vec<String>,
|
||||||
|
|
||||||
|
/// Maximum number of concurrent STUN probes during NAT detection.
|
||||||
|
#[serde(default = "default_stun_nat_probe_concurrency")]
|
||||||
|
pub stun_nat_probe_concurrency: usize,
|
||||||
|
|
||||||
/// Desired size of active Middle-Proxy writer pool.
|
/// Desired size of active Middle-Proxy writer pool.
|
||||||
#[serde(default = "default_pool_size")]
|
#[serde(default = "default_pool_size")]
|
||||||
pub middle_proxy_pool_size: usize,
|
pub middle_proxy_pool_size: usize,
|
||||||
|
|
||||||
/// Number of warm standby ME connections kept pre-initialized.
|
/// Number of warm standby ME connections kept pre-initialized.
|
||||||
#[serde(default)]
|
#[serde(default = "default_middle_proxy_warm_standby")]
|
||||||
pub middle_proxy_warm_standby: usize,
|
pub middle_proxy_warm_standby: usize,
|
||||||
|
|
||||||
/// Enable ME keepalive padding frames.
|
/// Enable ME keepalive padding frames.
|
||||||
@@ -192,6 +356,11 @@ pub struct GeneralConfig {
|
|||||||
#[serde(default = "default_true")]
|
#[serde(default = "default_true")]
|
||||||
pub me_keepalive_payload_random: bool,
|
pub me_keepalive_payload_random: bool,
|
||||||
|
|
||||||
|
/// Interval in seconds for service RPC_PROXY_REQ activity signals to ME.
|
||||||
|
/// 0 disables service activity signals.
|
||||||
|
#[serde(default = "default_rpc_proxy_req_every")]
|
||||||
|
pub rpc_proxy_req_every: u64,
|
||||||
|
|
||||||
/// Max pending ciphertext buffer per client writer (bytes).
|
/// Max pending ciphertext buffer per client writer (bytes).
|
||||||
/// Controls FakeTLS backpressure vs throughput.
|
/// Controls FakeTLS backpressure vs throughput.
|
||||||
#[serde(default = "default_crypto_pending_buffer")]
|
#[serde(default = "default_crypto_pending_buffer")]
|
||||||
@@ -207,7 +376,7 @@ pub struct GeneralConfig {
|
|||||||
pub desync_all_full: bool,
|
pub desync_all_full: bool,
|
||||||
|
|
||||||
/// Enable per-IP forensic observation buckets for scanners and handshake failures.
|
/// Enable per-IP forensic observation buckets for scanners and handshake failures.
|
||||||
#[serde(default)]
|
#[serde(default = "default_true")]
|
||||||
pub beobachten: bool,
|
pub beobachten: bool,
|
||||||
|
|
||||||
/// Observation retention window in minutes for per-IP forensic buckets.
|
/// Observation retention window in minutes for per-IP forensic buckets.
|
||||||
@@ -240,7 +409,7 @@ pub struct GeneralConfig {
|
|||||||
pub me_warmup_step_jitter_ms: u64,
|
pub me_warmup_step_jitter_ms: u64,
|
||||||
|
|
||||||
/// Max concurrent reconnect attempts per DC.
|
/// Max concurrent reconnect attempts per DC.
|
||||||
#[serde(default)]
|
#[serde(default = "default_me_reconnect_max_concurrent_per_dc")]
|
||||||
pub me_reconnect_max_concurrent_per_dc: u32,
|
pub me_reconnect_max_concurrent_per_dc: u32,
|
||||||
|
|
||||||
/// Base backoff in ms for reconnect.
|
/// Base backoff in ms for reconnect.
|
||||||
@@ -252,9 +421,66 @@ pub struct GeneralConfig {
|
|||||||
pub me_reconnect_backoff_cap_ms: u64,
|
pub me_reconnect_backoff_cap_ms: u64,
|
||||||
|
|
||||||
/// Fast retry attempts before backoff.
|
/// Fast retry attempts before backoff.
|
||||||
#[serde(default)]
|
#[serde(default = "default_me_reconnect_fast_retry_count")]
|
||||||
pub me_reconnect_fast_retry_count: u32,
|
pub me_reconnect_fast_retry_count: u32,
|
||||||
|
|
||||||
|
/// Number of additional reserve writers for DC groups with exactly one endpoint.
|
||||||
|
#[serde(default = "default_me_single_endpoint_shadow_writers")]
|
||||||
|
pub me_single_endpoint_shadow_writers: u8,
|
||||||
|
|
||||||
|
/// Enable aggressive outage recovery mode for single-endpoint DC groups.
|
||||||
|
#[serde(default = "default_me_single_endpoint_outage_mode_enabled")]
|
||||||
|
pub me_single_endpoint_outage_mode_enabled: bool,
|
||||||
|
|
||||||
|
/// Ignore endpoint quarantine while in single-endpoint outage mode.
|
||||||
|
#[serde(default = "default_me_single_endpoint_outage_disable_quarantine")]
|
||||||
|
pub me_single_endpoint_outage_disable_quarantine: bool,
|
||||||
|
|
||||||
|
/// Minimum reconnect backoff in ms for single-endpoint outage mode.
|
||||||
|
#[serde(default = "default_me_single_endpoint_outage_backoff_min_ms")]
|
||||||
|
pub me_single_endpoint_outage_backoff_min_ms: u64,
|
||||||
|
|
||||||
|
/// Maximum reconnect backoff in ms for single-endpoint outage mode.
|
||||||
|
#[serde(default = "default_me_single_endpoint_outage_backoff_max_ms")]
|
||||||
|
pub me_single_endpoint_outage_backoff_max_ms: u64,
|
||||||
|
|
||||||
|
/// Periodic shadow writer rotation interval in seconds for single-endpoint DC groups.
|
||||||
|
/// Set to 0 to disable periodic shadow rotation.
|
||||||
|
#[serde(default = "default_me_single_endpoint_shadow_rotate_every_secs")]
|
||||||
|
pub me_single_endpoint_shadow_rotate_every_secs: u64,
|
||||||
|
|
||||||
|
/// Floor policy mode for ME writer targets.
|
||||||
|
#[serde(default)]
|
||||||
|
pub me_floor_mode: MeFloorMode,
|
||||||
|
|
||||||
|
/// Idle time in seconds before adaptive floor can reduce single-endpoint writer target.
|
||||||
|
#[serde(default = "default_me_adaptive_floor_idle_secs")]
|
||||||
|
pub me_adaptive_floor_idle_secs: u64,
|
||||||
|
|
||||||
|
/// Minimum writer target for single-endpoint DC groups in adaptive floor mode.
|
||||||
|
#[serde(default = "default_me_adaptive_floor_min_writers_single_endpoint")]
|
||||||
|
pub me_adaptive_floor_min_writers_single_endpoint: u8,
|
||||||
|
|
||||||
|
/// Grace period in seconds to hold static floor after activity in adaptive mode.
|
||||||
|
#[serde(default = "default_me_adaptive_floor_recover_grace_secs")]
|
||||||
|
pub me_adaptive_floor_recover_grace_secs: u64,
|
||||||
|
|
||||||
|
/// Connect attempts for the selected upstream before returning error/fallback.
|
||||||
|
#[serde(default = "default_upstream_connect_retry_attempts")]
|
||||||
|
pub upstream_connect_retry_attempts: u32,
|
||||||
|
|
||||||
|
/// Delay in milliseconds between upstream connect attempts.
|
||||||
|
#[serde(default = "default_upstream_connect_retry_backoff_ms")]
|
||||||
|
pub upstream_connect_retry_backoff_ms: u64,
|
||||||
|
|
||||||
|
/// Consecutive failed requests before upstream is marked unhealthy.
|
||||||
|
#[serde(default = "default_upstream_unhealthy_fail_threshold")]
|
||||||
|
pub upstream_unhealthy_fail_threshold: u32,
|
||||||
|
|
||||||
|
/// Skip additional retries for hard non-transient upstream connect errors.
|
||||||
|
#[serde(default = "default_upstream_connect_failfast_hard_errors")]
|
||||||
|
pub upstream_connect_failfast_hard_errors: bool,
|
||||||
|
|
||||||
/// Ignore STUN/interface IP mismatch (keep using Middle Proxy even if NAT detected).
|
/// Ignore STUN/interface IP mismatch (keep using Middle Proxy even if NAT detected).
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub stun_iface_mismatch_ignore: bool,
|
pub stun_iface_mismatch_ignore: bool,
|
||||||
@@ -270,6 +496,26 @@ pub struct GeneralConfig {
|
|||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub disable_colors: bool,
|
pub disable_colors: bool,
|
||||||
|
|
||||||
|
/// Runtime telemetry controls for counters/metrics in hot paths.
|
||||||
|
#[serde(default)]
|
||||||
|
pub telemetry: TelemetryConfig,
|
||||||
|
|
||||||
|
/// SOCKS-bound KDF policy for Middle-End handshake.
|
||||||
|
#[serde(default)]
|
||||||
|
pub me_socks_kdf_policy: MeSocksKdfPolicy,
|
||||||
|
|
||||||
|
/// Base backpressure timeout in milliseconds for ME route channel send.
|
||||||
|
#[serde(default = "default_me_route_backpressure_base_timeout_ms")]
|
||||||
|
pub me_route_backpressure_base_timeout_ms: u64,
|
||||||
|
|
||||||
|
/// High backpressure timeout in milliseconds when queue occupancy is above watermark.
|
||||||
|
#[serde(default = "default_me_route_backpressure_high_timeout_ms")]
|
||||||
|
pub me_route_backpressure_high_timeout_ms: u64,
|
||||||
|
|
||||||
|
/// Queue occupancy percent threshold for high backpressure timeout.
|
||||||
|
#[serde(default = "default_me_route_backpressure_high_watermark_pct")]
|
||||||
|
pub me_route_backpressure_high_watermark_pct: u8,
|
||||||
|
|
||||||
/// [general.links] — proxy link generation overrides.
|
/// [general.links] — proxy link generation overrides.
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub links: LinksConfig,
|
pub links: LinksConfig,
|
||||||
@@ -280,7 +526,7 @@ pub struct GeneralConfig {
|
|||||||
|
|
||||||
/// Unified ME updater interval in seconds for getProxyConfig/getProxyConfigV6/getProxySecret.
|
/// Unified ME updater interval in seconds for getProxyConfig/getProxyConfigV6/getProxySecret.
|
||||||
/// When omitted, effective value falls back to legacy proxy_*_auto_reload_secs fields.
|
/// When omitted, effective value falls back to legacy proxy_*_auto_reload_secs fields.
|
||||||
#[serde(default)]
|
#[serde(default = "default_update_every")]
|
||||||
pub update_every: Option<u64>,
|
pub update_every: Option<u64>,
|
||||||
|
|
||||||
/// Periodic ME pool reinitialization interval in seconds.
|
/// Periodic ME pool reinitialization interval in seconds.
|
||||||
@@ -311,6 +557,18 @@ pub struct GeneralConfig {
|
|||||||
#[serde(default = "default_me_config_apply_cooldown_secs")]
|
#[serde(default = "default_me_config_apply_cooldown_secs")]
|
||||||
pub me_config_apply_cooldown_secs: u64,
|
pub me_config_apply_cooldown_secs: u64,
|
||||||
|
|
||||||
|
/// Ensure getProxyConfig snapshots are applied only for 2xx HTTP responses.
|
||||||
|
#[serde(default = "default_me_snapshot_require_http_2xx")]
|
||||||
|
pub me_snapshot_require_http_2xx: bool,
|
||||||
|
|
||||||
|
/// Reject empty getProxyConfig snapshots instead of marking them applied.
|
||||||
|
#[serde(default = "default_me_snapshot_reject_empty_map")]
|
||||||
|
pub me_snapshot_reject_empty_map: bool,
|
||||||
|
|
||||||
|
/// Minimum parsed `proxy_for` rows required to accept a snapshot.
|
||||||
|
#[serde(default = "default_me_snapshot_min_proxy_for_lines")]
|
||||||
|
pub me_snapshot_min_proxy_for_lines: u32,
|
||||||
|
|
||||||
/// Number of identical getProxySecret snapshots required before runtime secret rotation.
|
/// Number of identical getProxySecret snapshots required before runtime secret rotation.
|
||||||
#[serde(default = "default_proxy_secret_stable_snapshots")]
|
#[serde(default = "default_proxy_secret_stable_snapshots")]
|
||||||
pub proxy_secret_stable_snapshots: u8,
|
pub proxy_secret_stable_snapshots: u8,
|
||||||
@@ -319,6 +577,10 @@ pub struct GeneralConfig {
|
|||||||
#[serde(default = "default_proxy_secret_rotate_runtime")]
|
#[serde(default = "default_proxy_secret_rotate_runtime")]
|
||||||
pub proxy_secret_rotate_runtime: bool,
|
pub proxy_secret_rotate_runtime: bool,
|
||||||
|
|
||||||
|
/// Keep key-selector and secret bytes from one snapshot during ME handshake.
|
||||||
|
#[serde(default = "default_me_secret_atomic_snapshot")]
|
||||||
|
pub me_secret_atomic_snapshot: bool,
|
||||||
|
|
||||||
/// Maximum allowed proxy-secret length in bytes for startup and runtime refresh.
|
/// Maximum allowed proxy-secret length in bytes for startup and runtime refresh.
|
||||||
#[serde(default = "default_proxy_secret_len_max")]
|
#[serde(default = "default_proxy_secret_len_max")]
|
||||||
pub proxy_secret_len_max: usize,
|
pub proxy_secret_len_max: usize,
|
||||||
@@ -328,6 +590,14 @@ pub struct GeneralConfig {
|
|||||||
#[serde(default = "default_me_pool_drain_ttl_secs")]
|
#[serde(default = "default_me_pool_drain_ttl_secs")]
|
||||||
pub me_pool_drain_ttl_secs: u64,
|
pub me_pool_drain_ttl_secs: u64,
|
||||||
|
|
||||||
|
/// Policy for new binds on stale draining writers.
|
||||||
|
#[serde(default)]
|
||||||
|
pub me_bind_stale_mode: MeBindStaleMode,
|
||||||
|
|
||||||
|
/// TTL for stale bind allowance when `me_bind_stale_mode = \"ttl\"`.
|
||||||
|
#[serde(default = "default_me_bind_stale_ttl_secs")]
|
||||||
|
pub me_bind_stale_ttl_secs: u64,
|
||||||
|
|
||||||
/// Minimum desired-DC coverage ratio required before draining stale writers.
|
/// Minimum desired-DC coverage ratio required before draining stale writers.
|
||||||
/// Range: 0.0..=1.0.
|
/// Range: 0.0..=1.0.
|
||||||
#[serde(default = "default_me_pool_min_fresh_ratio")]
|
#[serde(default = "default_me_pool_min_fresh_ratio")]
|
||||||
@@ -348,6 +618,22 @@ pub struct GeneralConfig {
|
|||||||
#[serde(default = "default_proxy_config_reload_secs")]
|
#[serde(default = "default_proxy_config_reload_secs")]
|
||||||
pub proxy_config_auto_reload_secs: u64,
|
pub proxy_config_auto_reload_secs: u64,
|
||||||
|
|
||||||
|
/// Serialize ME reinit cycles across all trigger sources.
|
||||||
|
#[serde(default = "default_me_reinit_singleflight")]
|
||||||
|
pub me_reinit_singleflight: bool,
|
||||||
|
|
||||||
|
/// Trigger queue capacity for reinit scheduler.
|
||||||
|
#[serde(default = "default_me_reinit_trigger_channel")]
|
||||||
|
pub me_reinit_trigger_channel: usize,
|
||||||
|
|
||||||
|
/// Trigger coalescing window before starting a reinit cycle.
|
||||||
|
#[serde(default = "default_me_reinit_coalesce_window_ms")]
|
||||||
|
pub me_reinit_coalesce_window_ms: u64,
|
||||||
|
|
||||||
|
/// Deterministic candidate sort for ME writer binding path.
|
||||||
|
#[serde(default = "default_me_deterministic_writer_sort")]
|
||||||
|
pub me_deterministic_writer_sort: bool,
|
||||||
|
|
||||||
/// Enable NTP drift check at startup.
|
/// Enable NTP drift check at startup.
|
||||||
#[serde(default = "default_ntp_check")]
|
#[serde(default = "default_ntp_check")]
|
||||||
pub ntp_check: bool,
|
pub ntp_check: bool,
|
||||||
@@ -371,19 +657,21 @@ impl Default for GeneralConfig {
|
|||||||
modes: ProxyModes::default(),
|
modes: ProxyModes::default(),
|
||||||
prefer_ipv6: false,
|
prefer_ipv6: false,
|
||||||
fast_mode: default_true(),
|
fast_mode: default_true(),
|
||||||
use_middle_proxy: false,
|
use_middle_proxy: default_true(),
|
||||||
ad_tag: None,
|
ad_tag: None,
|
||||||
proxy_secret_path: None,
|
proxy_secret_path: default_proxy_secret_path(),
|
||||||
middle_proxy_nat_ip: None,
|
middle_proxy_nat_ip: None,
|
||||||
middle_proxy_nat_probe: true,
|
middle_proxy_nat_probe: default_true(),
|
||||||
middle_proxy_nat_stun: None,
|
middle_proxy_nat_stun: default_middle_proxy_nat_stun(),
|
||||||
middle_proxy_nat_stun_servers: Vec::new(),
|
middle_proxy_nat_stun_servers: default_middle_proxy_nat_stun_servers(),
|
||||||
|
stun_nat_probe_concurrency: default_stun_nat_probe_concurrency(),
|
||||||
middle_proxy_pool_size: default_pool_size(),
|
middle_proxy_pool_size: default_pool_size(),
|
||||||
middle_proxy_warm_standby: default_middle_proxy_warm_standby(),
|
middle_proxy_warm_standby: default_middle_proxy_warm_standby(),
|
||||||
me_keepalive_enabled: default_true(),
|
me_keepalive_enabled: default_true(),
|
||||||
me_keepalive_interval_secs: default_keepalive_interval(),
|
me_keepalive_interval_secs: default_keepalive_interval(),
|
||||||
me_keepalive_jitter_secs: default_keepalive_jitter(),
|
me_keepalive_jitter_secs: default_keepalive_jitter(),
|
||||||
me_keepalive_payload_random: default_true(),
|
me_keepalive_payload_random: default_true(),
|
||||||
|
rpc_proxy_req_every: default_rpc_proxy_req_every(),
|
||||||
me_warmup_stagger_enabled: default_true(),
|
me_warmup_stagger_enabled: default_true(),
|
||||||
me_warmup_step_delay_ms: default_warmup_step_delay_ms(),
|
me_warmup_step_delay_ms: default_warmup_step_delay_ms(),
|
||||||
me_warmup_step_jitter_ms: default_warmup_step_jitter_ms(),
|
me_warmup_step_jitter_ms: default_warmup_step_jitter_ms(),
|
||||||
@@ -391,15 +679,34 @@ impl Default for GeneralConfig {
|
|||||||
me_reconnect_backoff_base_ms: default_reconnect_backoff_base_ms(),
|
me_reconnect_backoff_base_ms: default_reconnect_backoff_base_ms(),
|
||||||
me_reconnect_backoff_cap_ms: default_reconnect_backoff_cap_ms(),
|
me_reconnect_backoff_cap_ms: default_reconnect_backoff_cap_ms(),
|
||||||
me_reconnect_fast_retry_count: default_me_reconnect_fast_retry_count(),
|
me_reconnect_fast_retry_count: default_me_reconnect_fast_retry_count(),
|
||||||
|
me_single_endpoint_shadow_writers: default_me_single_endpoint_shadow_writers(),
|
||||||
|
me_single_endpoint_outage_mode_enabled: default_me_single_endpoint_outage_mode_enabled(),
|
||||||
|
me_single_endpoint_outage_disable_quarantine: default_me_single_endpoint_outage_disable_quarantine(),
|
||||||
|
me_single_endpoint_outage_backoff_min_ms: default_me_single_endpoint_outage_backoff_min_ms(),
|
||||||
|
me_single_endpoint_outage_backoff_max_ms: default_me_single_endpoint_outage_backoff_max_ms(),
|
||||||
|
me_single_endpoint_shadow_rotate_every_secs: default_me_single_endpoint_shadow_rotate_every_secs(),
|
||||||
|
me_floor_mode: MeFloorMode::default(),
|
||||||
|
me_adaptive_floor_idle_secs: default_me_adaptive_floor_idle_secs(),
|
||||||
|
me_adaptive_floor_min_writers_single_endpoint: default_me_adaptive_floor_min_writers_single_endpoint(),
|
||||||
|
me_adaptive_floor_recover_grace_secs: default_me_adaptive_floor_recover_grace_secs(),
|
||||||
|
upstream_connect_retry_attempts: default_upstream_connect_retry_attempts(),
|
||||||
|
upstream_connect_retry_backoff_ms: default_upstream_connect_retry_backoff_ms(),
|
||||||
|
upstream_unhealthy_fail_threshold: default_upstream_unhealthy_fail_threshold(),
|
||||||
|
upstream_connect_failfast_hard_errors: default_upstream_connect_failfast_hard_errors(),
|
||||||
stun_iface_mismatch_ignore: false,
|
stun_iface_mismatch_ignore: false,
|
||||||
unknown_dc_log_path: default_unknown_dc_log_path(),
|
unknown_dc_log_path: default_unknown_dc_log_path(),
|
||||||
log_level: LogLevel::Normal,
|
log_level: LogLevel::Normal,
|
||||||
disable_colors: false,
|
disable_colors: false,
|
||||||
|
telemetry: TelemetryConfig::default(),
|
||||||
|
me_socks_kdf_policy: MeSocksKdfPolicy::Strict,
|
||||||
|
me_route_backpressure_base_timeout_ms: default_me_route_backpressure_base_timeout_ms(),
|
||||||
|
me_route_backpressure_high_timeout_ms: default_me_route_backpressure_high_timeout_ms(),
|
||||||
|
me_route_backpressure_high_watermark_pct: default_me_route_backpressure_high_watermark_pct(),
|
||||||
links: LinksConfig::default(),
|
links: LinksConfig::default(),
|
||||||
crypto_pending_buffer: default_crypto_pending_buffer(),
|
crypto_pending_buffer: default_crypto_pending_buffer(),
|
||||||
max_client_frame: default_max_client_frame(),
|
max_client_frame: default_max_client_frame(),
|
||||||
desync_all_full: default_desync_all_full(),
|
desync_all_full: default_desync_all_full(),
|
||||||
beobachten: true,
|
beobachten: default_true(),
|
||||||
beobachten_minutes: default_beobachten_minutes(),
|
beobachten_minutes: default_beobachten_minutes(),
|
||||||
beobachten_flush_secs: default_beobachten_flush_secs(),
|
beobachten_flush_secs: default_beobachten_flush_secs(),
|
||||||
beobachten_file: default_beobachten_file(),
|
beobachten_file: default_beobachten_file(),
|
||||||
@@ -413,14 +720,24 @@ impl Default for GeneralConfig {
|
|||||||
me_hardswap_warmup_pass_backoff_base_ms: default_me_hardswap_warmup_pass_backoff_base_ms(),
|
me_hardswap_warmup_pass_backoff_base_ms: default_me_hardswap_warmup_pass_backoff_base_ms(),
|
||||||
me_config_stable_snapshots: default_me_config_stable_snapshots(),
|
me_config_stable_snapshots: default_me_config_stable_snapshots(),
|
||||||
me_config_apply_cooldown_secs: default_me_config_apply_cooldown_secs(),
|
me_config_apply_cooldown_secs: default_me_config_apply_cooldown_secs(),
|
||||||
|
me_snapshot_require_http_2xx: default_me_snapshot_require_http_2xx(),
|
||||||
|
me_snapshot_reject_empty_map: default_me_snapshot_reject_empty_map(),
|
||||||
|
me_snapshot_min_proxy_for_lines: default_me_snapshot_min_proxy_for_lines(),
|
||||||
proxy_secret_stable_snapshots: default_proxy_secret_stable_snapshots(),
|
proxy_secret_stable_snapshots: default_proxy_secret_stable_snapshots(),
|
||||||
proxy_secret_rotate_runtime: default_proxy_secret_rotate_runtime(),
|
proxy_secret_rotate_runtime: default_proxy_secret_rotate_runtime(),
|
||||||
|
me_secret_atomic_snapshot: default_me_secret_atomic_snapshot(),
|
||||||
proxy_secret_len_max: default_proxy_secret_len_max(),
|
proxy_secret_len_max: default_proxy_secret_len_max(),
|
||||||
me_pool_drain_ttl_secs: default_me_pool_drain_ttl_secs(),
|
me_pool_drain_ttl_secs: default_me_pool_drain_ttl_secs(),
|
||||||
|
me_bind_stale_mode: MeBindStaleMode::default(),
|
||||||
|
me_bind_stale_ttl_secs: default_me_bind_stale_ttl_secs(),
|
||||||
me_pool_min_fresh_ratio: default_me_pool_min_fresh_ratio(),
|
me_pool_min_fresh_ratio: default_me_pool_min_fresh_ratio(),
|
||||||
me_reinit_drain_timeout_secs: default_me_reinit_drain_timeout_secs(),
|
me_reinit_drain_timeout_secs: default_me_reinit_drain_timeout_secs(),
|
||||||
proxy_secret_auto_reload_secs: default_proxy_secret_reload_secs(),
|
proxy_secret_auto_reload_secs: default_proxy_secret_reload_secs(),
|
||||||
proxy_config_auto_reload_secs: default_proxy_config_reload_secs(),
|
proxy_config_auto_reload_secs: default_proxy_config_reload_secs(),
|
||||||
|
me_reinit_singleflight: default_me_reinit_singleflight(),
|
||||||
|
me_reinit_trigger_channel: default_me_reinit_trigger_channel(),
|
||||||
|
me_reinit_coalesce_window_ms: default_me_reinit_coalesce_window_ms(),
|
||||||
|
me_deterministic_writer_sort: default_me_deterministic_writer_sort(),
|
||||||
ntp_check: default_ntp_check(),
|
ntp_check: default_ntp_check(),
|
||||||
ntp_servers: default_ntp_servers(),
|
ntp_servers: default_ntp_servers(),
|
||||||
auto_degradation_enabled: default_true(),
|
auto_degradation_enabled: default_true(),
|
||||||
@@ -450,11 +767,11 @@ impl GeneralConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// `[general.links]` — proxy link generation settings.
|
/// `[general.links]` — proxy link generation settings.
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct LinksConfig {
|
pub struct LinksConfig {
|
||||||
/// List of usernames whose tg:// links to display at startup.
|
/// List of usernames whose tg:// links to display at startup.
|
||||||
/// `"*"` = all users, `["alice", "bob"]` = specific users.
|
/// `"*"` = all users, `["alice", "bob"]` = specific users.
|
||||||
#[serde(default)]
|
#[serde(default = "default_links_show")]
|
||||||
pub show: ShowLink,
|
pub show: ShowLink,
|
||||||
|
|
||||||
/// Public hostname/IP for tg:// link generation (overrides detected IP).
|
/// Public hostname/IP for tg:// link generation (overrides detected IP).
|
||||||
@@ -466,15 +783,77 @@ pub struct LinksConfig {
|
|||||||
pub public_port: Option<u16>,
|
pub public_port: Option<u16>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for LinksConfig {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
show: default_links_show(),
|
||||||
|
public_host: None,
|
||||||
|
public_port: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// API settings for control-plane endpoints.
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||||
|
pub struct ApiConfig {
|
||||||
|
/// Enable or disable REST API.
|
||||||
|
#[serde(default)]
|
||||||
|
pub enabled: bool,
|
||||||
|
|
||||||
|
/// Listen address for API in `IP:PORT` format.
|
||||||
|
#[serde(default = "default_api_listen")]
|
||||||
|
pub listen: String,
|
||||||
|
|
||||||
|
/// CIDR whitelist allowed to access API.
|
||||||
|
#[serde(default = "default_api_whitelist")]
|
||||||
|
pub whitelist: Vec<IpNetwork>,
|
||||||
|
|
||||||
|
/// Optional static value for `Authorization` header validation.
|
||||||
|
/// Empty string disables header auth.
|
||||||
|
#[serde(default)]
|
||||||
|
pub auth_header: String,
|
||||||
|
|
||||||
|
/// Maximum accepted HTTP request body size in bytes.
|
||||||
|
#[serde(default = "default_api_request_body_limit_bytes")]
|
||||||
|
pub request_body_limit_bytes: usize,
|
||||||
|
|
||||||
|
/// Enable runtime snapshots that require read-lock aggregation on API request path.
|
||||||
|
#[serde(default = "default_api_minimal_runtime_enabled")]
|
||||||
|
pub minimal_runtime_enabled: bool,
|
||||||
|
|
||||||
|
/// Cache TTL for minimal runtime snapshots in milliseconds (0 disables caching).
|
||||||
|
#[serde(default = "default_api_minimal_runtime_cache_ttl_ms")]
|
||||||
|
pub minimal_runtime_cache_ttl_ms: u64,
|
||||||
|
|
||||||
|
/// Read-only mode: mutating endpoints are rejected.
|
||||||
|
#[serde(default)]
|
||||||
|
pub read_only: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for ApiConfig {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
enabled: false,
|
||||||
|
listen: default_api_listen(),
|
||||||
|
whitelist: default_api_whitelist(),
|
||||||
|
auth_header: String::new(),
|
||||||
|
request_body_limit_bytes: default_api_request_body_limit_bytes(),
|
||||||
|
minimal_runtime_enabled: default_api_minimal_runtime_enabled(),
|
||||||
|
minimal_runtime_cache_ttl_ms: default_api_minimal_runtime_cache_ttl_ms(),
|
||||||
|
read_only: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct ServerConfig {
|
pub struct ServerConfig {
|
||||||
#[serde(default = "default_port")]
|
#[serde(default = "default_port")]
|
||||||
pub port: u16,
|
pub port: u16,
|
||||||
|
|
||||||
#[serde(default)]
|
#[serde(default = "default_listen_addr_ipv4")]
|
||||||
pub listen_addr_ipv4: Option<String>,
|
pub listen_addr_ipv4: Option<String>,
|
||||||
|
|
||||||
#[serde(default)]
|
#[serde(default = "default_listen_addr_ipv6_opt")]
|
||||||
pub listen_addr_ipv6: Option<String>,
|
pub listen_addr_ipv6: Option<String>,
|
||||||
|
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
@@ -501,6 +880,9 @@ pub struct ServerConfig {
|
|||||||
#[serde(default = "default_metrics_whitelist")]
|
#[serde(default = "default_metrics_whitelist")]
|
||||||
pub metrics_whitelist: Vec<IpNetwork>,
|
pub metrics_whitelist: Vec<IpNetwork>,
|
||||||
|
|
||||||
|
#[serde(default, alias = "admin_api")]
|
||||||
|
pub api: ApiConfig,
|
||||||
|
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub listeners: Vec<ListenerConfig>,
|
pub listeners: Vec<ListenerConfig>,
|
||||||
}
|
}
|
||||||
@@ -509,14 +891,15 @@ impl Default for ServerConfig {
|
|||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
port: default_port(),
|
port: default_port(),
|
||||||
listen_addr_ipv4: Some(default_listen_addr()),
|
listen_addr_ipv4: default_listen_addr_ipv4(),
|
||||||
listen_addr_ipv6: Some(default_listen_addr_ipv6()),
|
listen_addr_ipv6: default_listen_addr_ipv6_opt(),
|
||||||
listen_unix_sock: None,
|
listen_unix_sock: None,
|
||||||
listen_unix_sock_perm: None,
|
listen_unix_sock_perm: None,
|
||||||
listen_tcp: None,
|
listen_tcp: None,
|
||||||
proxy_protocol: false,
|
proxy_protocol: false,
|
||||||
metrics_port: None,
|
metrics_port: None,
|
||||||
metrics_whitelist: default_metrics_whitelist(),
|
metrics_whitelist: default_metrics_whitelist(),
|
||||||
|
api: ApiConfig::default(),
|
||||||
listeners: Vec::new(),
|
listeners: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -583,7 +966,7 @@ pub struct AntiCensorshipConfig {
|
|||||||
pub fake_cert_len: usize,
|
pub fake_cert_len: usize,
|
||||||
|
|
||||||
/// Enable TLS certificate emulation using cached real certificates.
|
/// Enable TLS certificate emulation using cached real certificates.
|
||||||
#[serde(default)]
|
#[serde(default = "default_true")]
|
||||||
pub tls_emulation: bool,
|
pub tls_emulation: bool,
|
||||||
|
|
||||||
/// Directory to store TLS front cache (on disk).
|
/// Directory to store TLS front cache (on disk).
|
||||||
@@ -611,6 +994,12 @@ pub struct AntiCensorshipConfig {
|
|||||||
/// Enforce ALPN echo of client preference.
|
/// Enforce ALPN echo of client preference.
|
||||||
#[serde(default = "default_alpn_enforce")]
|
#[serde(default = "default_alpn_enforce")]
|
||||||
pub alpn_enforce: bool,
|
pub alpn_enforce: bool,
|
||||||
|
|
||||||
|
/// Send PROXY protocol header when connecting to mask_host.
|
||||||
|
/// 0 = disabled, 1 = v1 (text), 2 = v2 (binary).
|
||||||
|
/// Allows the backend to see the real client IP.
|
||||||
|
#[serde(default)]
|
||||||
|
pub mask_proxy_protocol: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for AntiCensorshipConfig {
|
impl Default for AntiCensorshipConfig {
|
||||||
@@ -630,15 +1019,20 @@ impl Default for AntiCensorshipConfig {
|
|||||||
tls_new_session_tickets: default_tls_new_session_tickets(),
|
tls_new_session_tickets: default_tls_new_session_tickets(),
|
||||||
tls_full_cert_ttl_secs: default_tls_full_cert_ttl_secs(),
|
tls_full_cert_ttl_secs: default_tls_full_cert_ttl_secs(),
|
||||||
alpn_enforce: default_alpn_enforce(),
|
alpn_enforce: default_alpn_enforce(),
|
||||||
|
mask_proxy_protocol: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||||
pub struct AccessConfig {
|
pub struct AccessConfig {
|
||||||
#[serde(default)]
|
#[serde(default = "default_access_users")]
|
||||||
pub users: HashMap<String, String>,
|
pub users: HashMap<String, String>,
|
||||||
|
|
||||||
|
/// Per-user ad_tag (32 hex chars from @MTProxybot).
|
||||||
|
#[serde(default)]
|
||||||
|
pub user_ad_tags: HashMap<String, String>,
|
||||||
|
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub user_max_tcp_conns: HashMap<String, usize>,
|
pub user_max_tcp_conns: HashMap<String, usize>,
|
||||||
|
|
||||||
@@ -665,6 +1059,7 @@ impl Default for AccessConfig {
|
|||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
users: default_access_users(),
|
users: default_access_users(),
|
||||||
|
user_ad_tags: HashMap::new(),
|
||||||
user_max_tcp_conns: HashMap::new(),
|
user_max_tcp_conns: HashMap::new(),
|
||||||
user_expirations: HashMap::new(),
|
user_expirations: HashMap::new(),
|
||||||
user_data_quota: HashMap::new(),
|
user_data_quota: HashMap::new(),
|
||||||
@@ -746,7 +1141,7 @@ pub struct ListenerConfig {
|
|||||||
/// In TOML, this can be:
|
/// In TOML, this can be:
|
||||||
/// - `show_link = "*"` — show links for all users
|
/// - `show_link = "*"` — show links for all users
|
||||||
/// - `show_link = ["a", "b"]` — show links for specific users
|
/// - `show_link = ["a", "b"]` — show links for specific users
|
||||||
/// - omitted — show no links (default)
|
/// - omitted — default depends on the owning config field
|
||||||
#[derive(Debug, Clone, Default)]
|
#[derive(Debug, Clone, Default)]
|
||||||
pub enum ShowLink {
|
pub enum ShowLink {
|
||||||
/// Don't show any links (default when omitted).
|
/// Don't show any links (default when omitted).
|
||||||
@@ -758,6 +1153,10 @@ pub enum ShowLink {
|
|||||||
Specific(Vec<String>),
|
Specific(Vec<String>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn default_links_show() -> ShowLink {
|
||||||
|
ShowLink::All
|
||||||
|
}
|
||||||
|
|
||||||
impl ShowLink {
|
impl ShowLink {
|
||||||
/// Returns true if no links should be shown.
|
/// Returns true if no links should be shown.
|
||||||
pub fn is_empty(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
|
|||||||
442
src/main.rs
442
src/main.rs
@@ -8,13 +8,14 @@ use std::time::Duration;
|
|||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use tokio::net::TcpListener;
|
use tokio::net::TcpListener;
|
||||||
use tokio::signal;
|
use tokio::signal;
|
||||||
use tokio::sync::Semaphore;
|
use tokio::sync::{Semaphore, mpsc};
|
||||||
use tracing::{debug, error, info, warn};
|
use tracing::{debug, error, info, warn};
|
||||||
use tracing_subscriber::{EnvFilter, fmt, prelude::*, reload};
|
use tracing_subscriber::{EnvFilter, fmt, prelude::*, reload};
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
use tokio::net::UnixListener;
|
use tokio::net::UnixListener;
|
||||||
|
|
||||||
mod cli;
|
mod cli;
|
||||||
|
mod api;
|
||||||
mod config;
|
mod config;
|
||||||
mod crypto;
|
mod crypto;
|
||||||
mod error;
|
mod error;
|
||||||
@@ -36,10 +37,12 @@ use crate::ip_tracker::UserIpTracker;
|
|||||||
use crate::network::probe::{decide_network_capabilities, log_probe_result, run_probe};
|
use crate::network::probe::{decide_network_capabilities, log_probe_result, run_probe};
|
||||||
use crate::proxy::ClientHandler;
|
use crate::proxy::ClientHandler;
|
||||||
use crate::stats::beobachten::BeobachtenStore;
|
use crate::stats::beobachten::BeobachtenStore;
|
||||||
|
use crate::stats::telemetry::TelemetryPolicy;
|
||||||
use crate::stats::{ReplayChecker, Stats};
|
use crate::stats::{ReplayChecker, Stats};
|
||||||
use crate::stream::BufferPool;
|
use crate::stream::BufferPool;
|
||||||
use crate::transport::middle_proxy::{
|
use crate::transport::middle_proxy::{
|
||||||
MePool, fetch_proxy_config, run_me_ping, MePingFamily, MePingSample, format_sample_line,
|
MePool, fetch_proxy_config, run_me_ping, MePingFamily, MePingSample, MeReinitTrigger, format_sample_line,
|
||||||
|
format_me_route,
|
||||||
};
|
};
|
||||||
use crate::transport::{ListenOptions, UpstreamManager, create_listener, find_listener_processes};
|
use crate::transport::{ListenOptions, UpstreamManager, create_listener, find_listener_processes};
|
||||||
use crate::tls_front::TlsFrontCache;
|
use crate::tls_front::TlsFrontCache;
|
||||||
@@ -193,6 +196,11 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
|
|||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Err(e) = crate::network::dns_overrides::install_entries(&config.network.dns_overrides) {
|
||||||
|
eprintln!("[telemt] Invalid network.dns_overrides: {}", e);
|
||||||
|
std::process::exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
let has_rust_log = std::env::var("RUST_LOG").is_ok();
|
let has_rust_log = std::env::var("RUST_LOG").is_ok();
|
||||||
let effective_log_level = if cli_silent {
|
let effective_log_level = if cli_silent {
|
||||||
LogLevel::Silent
|
LogLevel::Silent
|
||||||
@@ -254,10 +262,154 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
|
|||||||
warn!("Using default tls_domain. Consider setting a custom domain.");
|
warn!("Using default tls_domain. Consider setting a custom domain.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let stats = Arc::new(Stats::new());
|
||||||
|
stats.apply_telemetry_policy(TelemetryPolicy::from_config(&config.general.telemetry));
|
||||||
|
|
||||||
|
let upstream_manager = Arc::new(UpstreamManager::new(
|
||||||
|
config.upstreams.clone(),
|
||||||
|
config.general.upstream_connect_retry_attempts,
|
||||||
|
config.general.upstream_connect_retry_backoff_ms,
|
||||||
|
config.general.upstream_unhealthy_fail_threshold,
|
||||||
|
config.general.upstream_connect_failfast_hard_errors,
|
||||||
|
stats.clone(),
|
||||||
|
));
|
||||||
|
|
||||||
|
let mut tls_domains = Vec::with_capacity(1 + config.censorship.tls_domains.len());
|
||||||
|
tls_domains.push(config.censorship.tls_domain.clone());
|
||||||
|
for d in &config.censorship.tls_domains {
|
||||||
|
if !tls_domains.contains(d) {
|
||||||
|
tls_domains.push(d.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start TLS front fetching in background immediately, in parallel with STUN probing.
|
||||||
|
let tls_cache: Option<Arc<TlsFrontCache>> = if config.censorship.tls_emulation {
|
||||||
|
let cache = Arc::new(TlsFrontCache::new(
|
||||||
|
&tls_domains,
|
||||||
|
config.censorship.fake_cert_len,
|
||||||
|
&config.censorship.tls_front_dir,
|
||||||
|
));
|
||||||
|
cache.load_from_disk().await;
|
||||||
|
|
||||||
|
let port = config.censorship.mask_port;
|
||||||
|
let proxy_protocol = config.censorship.mask_proxy_protocol;
|
||||||
|
let mask_host = config
|
||||||
|
.censorship
|
||||||
|
.mask_host
|
||||||
|
.clone()
|
||||||
|
.unwrap_or_else(|| config.censorship.tls_domain.clone());
|
||||||
|
let mask_unix_sock = config.censorship.mask_unix_sock.clone();
|
||||||
|
let fetch_timeout = Duration::from_secs(5);
|
||||||
|
|
||||||
|
let cache_initial = cache.clone();
|
||||||
|
let domains_initial = tls_domains.clone();
|
||||||
|
let host_initial = mask_host.clone();
|
||||||
|
let unix_sock_initial = mask_unix_sock.clone();
|
||||||
|
let upstream_initial = upstream_manager.clone();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
let mut join = tokio::task::JoinSet::new();
|
||||||
|
for domain in domains_initial {
|
||||||
|
let cache_domain = cache_initial.clone();
|
||||||
|
let host_domain = host_initial.clone();
|
||||||
|
let unix_sock_domain = unix_sock_initial.clone();
|
||||||
|
let upstream_domain = upstream_initial.clone();
|
||||||
|
join.spawn(async move {
|
||||||
|
match crate::tls_front::fetcher::fetch_real_tls(
|
||||||
|
&host_domain,
|
||||||
|
port,
|
||||||
|
&domain,
|
||||||
|
fetch_timeout,
|
||||||
|
Some(upstream_domain),
|
||||||
|
proxy_protocol,
|
||||||
|
unix_sock_domain.as_deref(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(res) => cache_domain.update_from_fetch(&domain, res).await,
|
||||||
|
Err(e) => {
|
||||||
|
warn!(domain = %domain, error = %e, "TLS emulation initial fetch failed")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
while let Some(res) = join.join_next().await {
|
||||||
|
if let Err(e) = res {
|
||||||
|
warn!(error = %e, "TLS emulation initial fetch task join failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let cache_timeout = cache.clone();
|
||||||
|
let domains_timeout = tls_domains.clone();
|
||||||
|
let fake_cert_len = config.censorship.fake_cert_len;
|
||||||
|
tokio::spawn(async move {
|
||||||
|
tokio::time::sleep(fetch_timeout).await;
|
||||||
|
for domain in domains_timeout {
|
||||||
|
let cached = cache_timeout.get(&domain).await;
|
||||||
|
if cached.domain == "default" {
|
||||||
|
warn!(
|
||||||
|
domain = %domain,
|
||||||
|
timeout_secs = fetch_timeout.as_secs(),
|
||||||
|
fake_cert_len,
|
||||||
|
"TLS-front fetch not ready within timeout; using cache/default fake cert fallback"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Periodic refresh with jitter.
|
||||||
|
let cache_refresh = cache.clone();
|
||||||
|
let domains_refresh = tls_domains.clone();
|
||||||
|
let host_refresh = mask_host.clone();
|
||||||
|
let unix_sock_refresh = mask_unix_sock.clone();
|
||||||
|
let upstream_refresh = upstream_manager.clone();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
loop {
|
||||||
|
let base_secs = rand::rng().random_range(4 * 3600..=6 * 3600);
|
||||||
|
let jitter_secs = rand::rng().random_range(0..=7200);
|
||||||
|
tokio::time::sleep(Duration::from_secs(base_secs + jitter_secs)).await;
|
||||||
|
|
||||||
|
let mut join = tokio::task::JoinSet::new();
|
||||||
|
for domain in domains_refresh.clone() {
|
||||||
|
let cache_domain = cache_refresh.clone();
|
||||||
|
let host_domain = host_refresh.clone();
|
||||||
|
let unix_sock_domain = unix_sock_refresh.clone();
|
||||||
|
let upstream_domain = upstream_refresh.clone();
|
||||||
|
join.spawn(async move {
|
||||||
|
match crate::tls_front::fetcher::fetch_real_tls(
|
||||||
|
&host_domain,
|
||||||
|
port,
|
||||||
|
&domain,
|
||||||
|
fetch_timeout,
|
||||||
|
Some(upstream_domain),
|
||||||
|
proxy_protocol,
|
||||||
|
unix_sock_domain.as_deref(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(res) => cache_domain.update_from_fetch(&domain, res).await,
|
||||||
|
Err(e) => warn!(domain = %domain, error = %e, "TLS emulation refresh failed"),
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
while let Some(res) = join.join_next().await {
|
||||||
|
if let Err(e) = res {
|
||||||
|
warn!(error = %e, "TLS emulation refresh task join failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Some(cache)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
let probe = run_probe(
|
let probe = run_probe(
|
||||||
&config.network,
|
&config.network,
|
||||||
config.general.middle_proxy_nat_stun.clone(),
|
|
||||||
config.general.middle_proxy_nat_probe,
|
config.general.middle_proxy_nat_probe,
|
||||||
|
config.general.stun_nat_probe_concurrency,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
let decision = decide_network_capabilities(&config.network, &probe);
|
let decision = decide_network_capabilities(&config.network, &probe);
|
||||||
@@ -265,7 +417,6 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
|
|||||||
|
|
||||||
let prefer_ipv6 = decision.prefer_ipv6();
|
let prefer_ipv6 = decision.prefer_ipv6();
|
||||||
let mut use_middle_proxy = config.general.use_middle_proxy && (decision.ipv4_me || decision.ipv6_me);
|
let mut use_middle_proxy = config.general.use_middle_proxy && (decision.ipv4_me || decision.ipv6_me);
|
||||||
let stats = Arc::new(Stats::new());
|
|
||||||
let beobachten = Arc::new(BeobachtenStore::new());
|
let beobachten = Arc::new(BeobachtenStore::new());
|
||||||
let rng = Arc::new(SecureRandom::new());
|
let rng = Arc::new(SecureRandom::new());
|
||||||
|
|
||||||
@@ -276,6 +427,12 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
|
|||||||
if !config.access.user_max_unique_ips.is_empty() {
|
if !config.access.user_max_unique_ips.is_empty() {
|
||||||
info!("IP limits configured for {} users", config.access.user_max_unique_ips.len());
|
info!("IP limits configured for {} users", config.access.user_max_unique_ips.len());
|
||||||
}
|
}
|
||||||
|
if !config.network.dns_overrides.is_empty() {
|
||||||
|
info!(
|
||||||
|
"Runtime DNS overrides configured: {} entries",
|
||||||
|
config.network.dns_overrides.len()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Connection concurrency limit
|
// Connection concurrency limit
|
||||||
let max_connections = Arc::new(Semaphore::new(10_000));
|
let max_connections = Arc::new(Semaphore::new(10_000));
|
||||||
@@ -290,14 +447,17 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
|
|||||||
// =====================================================================
|
// =====================================================================
|
||||||
let me_pool: Option<Arc<MePool>> = if use_middle_proxy {
|
let me_pool: Option<Arc<MePool>> = if use_middle_proxy {
|
||||||
info!("=== Middle Proxy Mode ===");
|
info!("=== Middle Proxy Mode ===");
|
||||||
|
let me_nat_probe = config.general.middle_proxy_nat_probe && config.network.stun_use;
|
||||||
|
if config.general.middle_proxy_nat_probe && !config.network.stun_use {
|
||||||
|
info!("Middle-proxy STUN probing disabled by network.stun_use=false");
|
||||||
|
}
|
||||||
|
|
||||||
// ad_tag (proxy_tag) for advertising
|
// Global ad_tag (pool default). Used when user has no per-user tag in access.user_ad_tags.
|
||||||
let proxy_tag = config.general.ad_tag.as_ref().map(|tag| {
|
let proxy_tag = config
|
||||||
hex::decode(tag).unwrap_or_else(|_| {
|
.general
|
||||||
warn!("Invalid ad_tag hex, middle proxy ad_tag will be empty");
|
.ad_tag
|
||||||
Vec::new()
|
.as_ref()
|
||||||
})
|
.map(|tag| hex::decode(tag).expect("general.ad_tag must be validated before startup"));
|
||||||
});
|
|
||||||
|
|
||||||
// =============================================================
|
// =============================================================
|
||||||
// CRITICAL: Download Telegram proxy-secret (NOT user secret!)
|
// CRITICAL: Download Telegram proxy-secret (NOT user secret!)
|
||||||
@@ -357,9 +517,10 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
|
|||||||
proxy_tag,
|
proxy_tag,
|
||||||
proxy_secret,
|
proxy_secret,
|
||||||
config.general.middle_proxy_nat_ip,
|
config.general.middle_proxy_nat_ip,
|
||||||
config.general.middle_proxy_nat_probe,
|
me_nat_probe,
|
||||||
config.general.middle_proxy_nat_stun.clone(),
|
None,
|
||||||
config.general.middle_proxy_nat_stun_servers.clone(),
|
config.network.stun_servers.clone(),
|
||||||
|
config.general.stun_nat_probe_concurrency,
|
||||||
probe.detected_ipv6,
|
probe.detected_ipv6,
|
||||||
config.timeouts.me_one_retry,
|
config.timeouts.me_one_retry,
|
||||||
config.timeouts.me_one_timeout_ms,
|
config.timeouts.me_one_timeout_ms,
|
||||||
@@ -367,12 +528,14 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
|
|||||||
cfg_v6.map.clone(),
|
cfg_v6.map.clone(),
|
||||||
cfg_v4.default_dc.or(cfg_v6.default_dc),
|
cfg_v4.default_dc.or(cfg_v6.default_dc),
|
||||||
decision.clone(),
|
decision.clone(),
|
||||||
|
Some(upstream_manager.clone()),
|
||||||
rng.clone(),
|
rng.clone(),
|
||||||
stats.clone(),
|
stats.clone(),
|
||||||
config.general.me_keepalive_enabled,
|
config.general.me_keepalive_enabled,
|
||||||
config.general.me_keepalive_interval_secs,
|
config.general.me_keepalive_interval_secs,
|
||||||
config.general.me_keepalive_jitter_secs,
|
config.general.me_keepalive_jitter_secs,
|
||||||
config.general.me_keepalive_payload_random,
|
config.general.me_keepalive_payload_random,
|
||||||
|
config.general.rpc_proxy_req_every,
|
||||||
config.general.me_warmup_stagger_enabled,
|
config.general.me_warmup_stagger_enabled,
|
||||||
config.general.me_warmup_step_delay_ms,
|
config.general.me_warmup_step_delay_ms,
|
||||||
config.general.me_warmup_step_jitter_ms,
|
config.general.me_warmup_step_jitter_ms,
|
||||||
@@ -380,6 +543,16 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
|
|||||||
config.general.me_reconnect_backoff_base_ms,
|
config.general.me_reconnect_backoff_base_ms,
|
||||||
config.general.me_reconnect_backoff_cap_ms,
|
config.general.me_reconnect_backoff_cap_ms,
|
||||||
config.general.me_reconnect_fast_retry_count,
|
config.general.me_reconnect_fast_retry_count,
|
||||||
|
config.general.me_single_endpoint_shadow_writers,
|
||||||
|
config.general.me_single_endpoint_outage_mode_enabled,
|
||||||
|
config.general.me_single_endpoint_outage_disable_quarantine,
|
||||||
|
config.general.me_single_endpoint_outage_backoff_min_ms,
|
||||||
|
config.general.me_single_endpoint_outage_backoff_max_ms,
|
||||||
|
config.general.me_single_endpoint_shadow_rotate_every_secs,
|
||||||
|
config.general.me_floor_mode,
|
||||||
|
config.general.me_adaptive_floor_idle_secs,
|
||||||
|
config.general.me_adaptive_floor_min_writers_single_endpoint,
|
||||||
|
config.general.me_adaptive_floor_recover_grace_secs,
|
||||||
config.general.hardswap,
|
config.general.hardswap,
|
||||||
config.general.me_pool_drain_ttl_secs,
|
config.general.me_pool_drain_ttl_secs,
|
||||||
config.general.effective_me_pool_force_close_secs(),
|
config.general.effective_me_pool_force_close_secs(),
|
||||||
@@ -388,29 +561,44 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
|
|||||||
config.general.me_hardswap_warmup_delay_max_ms,
|
config.general.me_hardswap_warmup_delay_max_ms,
|
||||||
config.general.me_hardswap_warmup_extra_passes,
|
config.general.me_hardswap_warmup_extra_passes,
|
||||||
config.general.me_hardswap_warmup_pass_backoff_base_ms,
|
config.general.me_hardswap_warmup_pass_backoff_base_ms,
|
||||||
|
config.general.me_bind_stale_mode,
|
||||||
|
config.general.me_bind_stale_ttl_secs,
|
||||||
|
config.general.me_secret_atomic_snapshot,
|
||||||
|
config.general.me_deterministic_writer_sort,
|
||||||
|
config.general.me_socks_kdf_policy,
|
||||||
|
config.general.me_route_backpressure_base_timeout_ms,
|
||||||
|
config.general.me_route_backpressure_high_timeout_ms,
|
||||||
|
config.general.me_route_backpressure_high_watermark_pct,
|
||||||
);
|
);
|
||||||
|
|
||||||
let pool_size = config.general.middle_proxy_pool_size.max(1);
|
let pool_size = config.general.middle_proxy_pool_size.max(1);
|
||||||
match pool.init(pool_size, &rng).await {
|
loop {
|
||||||
Ok(()) => {
|
match pool.init(pool_size, &rng).await {
|
||||||
info!("Middle-End pool initialized successfully");
|
Ok(()) => {
|
||||||
|
info!("Middle-End pool initialized successfully");
|
||||||
|
|
||||||
// Phase 4: Start health monitor
|
// Phase 4: Start health monitor
|
||||||
let pool_clone = pool.clone();
|
let pool_clone = pool.clone();
|
||||||
let rng_clone = rng.clone();
|
let rng_clone = rng.clone();
|
||||||
let min_conns = pool_size;
|
let min_conns = pool_size;
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
crate::transport::middle_proxy::me_health_monitor(
|
crate::transport::middle_proxy::me_health_monitor(
|
||||||
pool_clone, rng_clone, min_conns,
|
pool_clone, rng_clone, min_conns,
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
});
|
});
|
||||||
|
|
||||||
Some(pool)
|
break Some(pool);
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!(error = %e, "Failed to initialize ME pool. Falling back to direct mode.");
|
warn!(
|
||||||
None
|
error = %e,
|
||||||
|
retry_in_secs = 2,
|
||||||
|
"ME pool is not ready yet; retrying startup initialization"
|
||||||
|
);
|
||||||
|
pool.reset_stun_state();
|
||||||
|
tokio::time::sleep(Duration::from_secs(2)).await;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -442,77 +630,8 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
|
|||||||
Duration::from_secs(config.access.replay_window_secs),
|
Duration::from_secs(config.access.replay_window_secs),
|
||||||
));
|
));
|
||||||
|
|
||||||
let upstream_manager = Arc::new(UpstreamManager::new(config.upstreams.clone()));
|
|
||||||
let buffer_pool = Arc::new(BufferPool::with_config(16 * 1024, 4096));
|
let buffer_pool = Arc::new(BufferPool::with_config(16 * 1024, 4096));
|
||||||
|
|
||||||
// TLS front cache (optional emulation)
|
|
||||||
let mut tls_domains = Vec::with_capacity(1 + config.censorship.tls_domains.len());
|
|
||||||
tls_domains.push(config.censorship.tls_domain.clone());
|
|
||||||
for d in &config.censorship.tls_domains {
|
|
||||||
if !tls_domains.contains(d) {
|
|
||||||
tls_domains.push(d.clone());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let tls_cache: Option<Arc<TlsFrontCache>> = if config.censorship.tls_emulation {
|
|
||||||
let cache = Arc::new(TlsFrontCache::new(
|
|
||||||
&tls_domains,
|
|
||||||
config.censorship.fake_cert_len,
|
|
||||||
&config.censorship.tls_front_dir,
|
|
||||||
));
|
|
||||||
|
|
||||||
cache.load_from_disk().await;
|
|
||||||
|
|
||||||
let port = config.censorship.mask_port;
|
|
||||||
let mask_host = config.censorship.mask_host.clone()
|
|
||||||
.unwrap_or_else(|| config.censorship.tls_domain.clone());
|
|
||||||
// Initial synchronous fetch to warm cache before serving clients.
|
|
||||||
for domain in tls_domains.clone() {
|
|
||||||
match crate::tls_front::fetcher::fetch_real_tls(
|
|
||||||
&mask_host,
|
|
||||||
port,
|
|
||||||
&domain,
|
|
||||||
Duration::from_secs(5),
|
|
||||||
Some(upstream_manager.clone()),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(res) => cache.update_from_fetch(&domain, res).await,
|
|
||||||
Err(e) => warn!(domain = %domain, error = %e, "TLS emulation fetch failed"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Periodic refresh with jitter.
|
|
||||||
let cache_clone = cache.clone();
|
|
||||||
let domains = tls_domains.clone();
|
|
||||||
let upstream_for_task = upstream_manager.clone();
|
|
||||||
tokio::spawn(async move {
|
|
||||||
loop {
|
|
||||||
let base_secs = rand::rng().random_range(4 * 3600..=6 * 3600);
|
|
||||||
let jitter_secs = rand::rng().random_range(0..=7200);
|
|
||||||
tokio::time::sleep(Duration::from_secs(base_secs + jitter_secs)).await;
|
|
||||||
for domain in &domains {
|
|
||||||
match crate::tls_front::fetcher::fetch_real_tls(
|
|
||||||
&mask_host,
|
|
||||||
port,
|
|
||||||
domain,
|
|
||||||
Duration::from_secs(5),
|
|
||||||
Some(upstream_for_task.clone()),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(res) => cache_clone.update_from_fetch(domain, res).await,
|
|
||||||
Err(e) => warn!(domain = %domain, error = %e, "TLS emulation refresh failed"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Some(cache)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
|
|
||||||
// Middle-End ping before DC connectivity
|
// Middle-End ping before DC connectivity
|
||||||
if let Some(ref pool) = me_pool {
|
if let Some(ref pool) = me_pool {
|
||||||
let me_results = run_me_ping(pool, &rng).await;
|
let me_results = run_me_ping(pool, &rng).await;
|
||||||
@@ -536,7 +655,15 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
|
|||||||
} else {
|
} else {
|
||||||
info!(" No ME connectivity");
|
info!(" No ME connectivity");
|
||||||
}
|
}
|
||||||
info!(" via direct");
|
let me_route = format_me_route(
|
||||||
|
&config.upstreams,
|
||||||
|
&me_results,
|
||||||
|
prefer_ipv6,
|
||||||
|
v4_ok,
|
||||||
|
v6_ok,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
info!(" via {}", me_route);
|
||||||
info!("============================================================");
|
info!("============================================================");
|
||||||
|
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
@@ -662,12 +789,14 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
|
|||||||
// Background tasks
|
// Background tasks
|
||||||
let um_clone = upstream_manager.clone();
|
let um_clone = upstream_manager.clone();
|
||||||
let decision_clone = decision.clone();
|
let decision_clone = decision.clone();
|
||||||
|
let dc_overrides_for_health = config.dc_overrides.clone();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
um_clone
|
um_clone
|
||||||
.run_health_checks(
|
.run_health_checks(
|
||||||
prefer_ipv6,
|
prefer_ipv6,
|
||||||
decision_clone.ipv4_dc,
|
decision_clone.ipv4_dc,
|
||||||
decision_clone.ipv6_dc,
|
decision_clone.ipv6_dc,
|
||||||
|
dc_overrides_for_health,
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
});
|
});
|
||||||
@@ -697,6 +826,27 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
|
|||||||
detected_ip_v6,
|
detected_ip_v6,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let stats_policy = stats.clone();
|
||||||
|
let mut config_rx_policy = config_rx.clone();
|
||||||
|
let me_pool_policy = me_pool.clone();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
loop {
|
||||||
|
if config_rx_policy.changed().await.is_err() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let cfg = config_rx_policy.borrow_and_update().clone();
|
||||||
|
stats_policy.apply_telemetry_policy(TelemetryPolicy::from_config(&cfg.general.telemetry));
|
||||||
|
if let Some(pool) = &me_pool_policy {
|
||||||
|
pool.update_runtime_transport_policy(
|
||||||
|
cfg.general.me_socks_kdf_policy,
|
||||||
|
cfg.general.me_route_backpressure_base_timeout_ms,
|
||||||
|
cfg.general.me_route_backpressure_high_timeout_ms,
|
||||||
|
cfg.general.me_route_backpressure_high_watermark_pct,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
let beobachten_writer = beobachten.clone();
|
let beobachten_writer = beobachten.clone();
|
||||||
let config_rx_beobachten = config_rx.clone();
|
let config_rx_beobachten = config_rx.clone();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
@@ -718,26 +868,43 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if let Some(ref pool) = me_pool {
|
if let Some(ref pool) = me_pool {
|
||||||
let pool_clone = pool.clone();
|
let reinit_trigger_capacity = config
|
||||||
let rng_clone = rng.clone();
|
.general
|
||||||
let config_rx_clone = config_rx.clone();
|
.me_reinit_trigger_channel
|
||||||
|
.max(1);
|
||||||
|
let (reinit_tx, reinit_rx) = mpsc::channel::<MeReinitTrigger>(reinit_trigger_capacity);
|
||||||
|
|
||||||
|
let pool_clone_sched = pool.clone();
|
||||||
|
let rng_clone_sched = rng.clone();
|
||||||
|
let config_rx_clone_sched = config_rx.clone();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
crate::transport::middle_proxy::me_config_updater(
|
crate::transport::middle_proxy::me_reinit_scheduler(
|
||||||
pool_clone,
|
pool_clone_sched,
|
||||||
rng_clone,
|
rng_clone_sched,
|
||||||
config_rx_clone,
|
config_rx_clone_sched,
|
||||||
|
reinit_rx,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
});
|
||||||
|
|
||||||
|
let pool_clone = pool.clone();
|
||||||
|
let config_rx_clone = config_rx.clone();
|
||||||
|
let reinit_tx_updater = reinit_tx.clone();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
crate::transport::middle_proxy::me_config_updater(
|
||||||
|
pool_clone,
|
||||||
|
config_rx_clone,
|
||||||
|
reinit_tx_updater,
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
});
|
});
|
||||||
|
|
||||||
let pool_clone_rot = pool.clone();
|
|
||||||
let rng_clone_rot = rng.clone();
|
|
||||||
let config_rx_clone_rot = config_rx.clone();
|
let config_rx_clone_rot = config_rx.clone();
|
||||||
|
let reinit_tx_rotation = reinit_tx.clone();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
crate::transport::middle_proxy::me_rotation_task(
|
crate::transport::middle_proxy::me_rotation_task(
|
||||||
pool_clone_rot,
|
|
||||||
rng_clone_rot,
|
|
||||||
config_rx_clone_rot,
|
config_rx_clone_rot,
|
||||||
|
reinit_tx_rotation,
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
});
|
});
|
||||||
@@ -971,12 +1138,59 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
|
|||||||
let stats = stats.clone();
|
let stats = stats.clone();
|
||||||
let beobachten = beobachten.clone();
|
let beobachten = beobachten.clone();
|
||||||
let config_rx_metrics = config_rx.clone();
|
let config_rx_metrics = config_rx.clone();
|
||||||
|
let ip_tracker_metrics = ip_tracker.clone();
|
||||||
let whitelist = config.server.metrics_whitelist.clone();
|
let whitelist = config.server.metrics_whitelist.clone();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
metrics::serve(port, stats, beobachten, config_rx_metrics, whitelist).await;
|
metrics::serve(
|
||||||
|
port,
|
||||||
|
stats,
|
||||||
|
beobachten,
|
||||||
|
ip_tracker_metrics,
|
||||||
|
config_rx_metrics,
|
||||||
|
whitelist,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if config.server.api.enabled {
|
||||||
|
let listen = match config.server.api.listen.parse::<SocketAddr>() {
|
||||||
|
Ok(listen) => listen,
|
||||||
|
Err(error) => {
|
||||||
|
warn!(
|
||||||
|
error = %error,
|
||||||
|
listen = %config.server.api.listen,
|
||||||
|
"Invalid server.api.listen; API is disabled"
|
||||||
|
);
|
||||||
|
SocketAddr::from(([127, 0, 0, 1], 0))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if listen.port() != 0 {
|
||||||
|
let stats = stats.clone();
|
||||||
|
let ip_tracker_api = ip_tracker.clone();
|
||||||
|
let me_pool_api = me_pool.clone();
|
||||||
|
let upstream_manager_api = upstream_manager.clone();
|
||||||
|
let config_rx_api = config_rx.clone();
|
||||||
|
let config_path_api = std::path::PathBuf::from(&config_path);
|
||||||
|
let startup_detected_ip_v4 = detected_ip_v4;
|
||||||
|
let startup_detected_ip_v6 = detected_ip_v6;
|
||||||
|
tokio::spawn(async move {
|
||||||
|
api::serve(
|
||||||
|
listen,
|
||||||
|
stats,
|
||||||
|
ip_tracker_api,
|
||||||
|
me_pool_api,
|
||||||
|
upstream_manager_api,
|
||||||
|
config_rx_api,
|
||||||
|
config_path_api,
|
||||||
|
startup_detected_ip_v4,
|
||||||
|
startup_detected_ip_v6,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (listener, listener_proxy_protocol) in listeners {
|
for (listener, listener_proxy_protocol) in listeners {
|
||||||
let mut config_rx: tokio::sync::watch::Receiver<Arc<ProxyConfig>> = config_rx.clone();
|
let mut config_rx: tokio::sync::watch::Receiver<Arc<ProxyConfig>> = config_rx.clone();
|
||||||
let stats = stats.clone();
|
let stats = stats.clone();
|
||||||
|
|||||||
1141
src/metrics.rs
1141
src/metrics.rs
File diff suppressed because it is too large
Load Diff
197
src/network/dns_overrides.rs
Normal file
197
src/network/dns_overrides.rs
Normal file
@@ -0,0 +1,197 @@
|
|||||||
|
//! Runtime DNS overrides for `host:port` targets.
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::net::{IpAddr, Ipv6Addr, SocketAddr};
|
||||||
|
use std::sync::{OnceLock, RwLock};
|
||||||
|
|
||||||
|
use crate::error::{ProxyError, Result};
|
||||||
|
|
||||||
|
type OverrideMap = HashMap<(String, u16), IpAddr>;
|
||||||
|
|
||||||
|
static DNS_OVERRIDES: OnceLock<RwLock<OverrideMap>> = OnceLock::new();
|
||||||
|
|
||||||
|
fn overrides_store() -> &'static RwLock<OverrideMap> {
|
||||||
|
DNS_OVERRIDES.get_or_init(|| RwLock::new(HashMap::new()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_ip_spec(ip_spec: &str) -> Result<IpAddr> {
|
||||||
|
if ip_spec.starts_with('[') && ip_spec.ends_with(']') {
|
||||||
|
let inner = &ip_spec[1..ip_spec.len() - 1];
|
||||||
|
let ipv6 = inner.parse::<Ipv6Addr>().map_err(|_| {
|
||||||
|
ProxyError::Config(format!(
|
||||||
|
"network.dns_overrides IPv6 override is invalid: '{ip_spec}'"
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
return Ok(IpAddr::V6(ipv6));
|
||||||
|
}
|
||||||
|
|
||||||
|
let ip = ip_spec.parse::<IpAddr>().map_err(|_| {
|
||||||
|
ProxyError::Config(format!(
|
||||||
|
"network.dns_overrides IP is invalid: '{ip_spec}'"
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
if matches!(ip, IpAddr::V6(_)) {
|
||||||
|
return Err(ProxyError::Config(format!(
|
||||||
|
"network.dns_overrides IPv6 must be bracketed: '{ip_spec}'"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
Ok(ip)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_entry(entry: &str) -> Result<((String, u16), IpAddr)> {
|
||||||
|
let trimmed = entry.trim();
|
||||||
|
if trimmed.is_empty() {
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"network.dns_overrides entry cannot be empty".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let first_sep = trimmed.find(':').ok_or_else(|| {
|
||||||
|
ProxyError::Config(format!(
|
||||||
|
"network.dns_overrides entry must use host:port:ip format: '{trimmed}'"
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
let second_sep = trimmed[first_sep + 1..]
|
||||||
|
.find(':')
|
||||||
|
.map(|idx| first_sep + 1 + idx)
|
||||||
|
.ok_or_else(|| {
|
||||||
|
ProxyError::Config(format!(
|
||||||
|
"network.dns_overrides entry must use host:port:ip format: '{trimmed}'"
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let host = trimmed[..first_sep].trim();
|
||||||
|
let port_str = trimmed[first_sep + 1..second_sep].trim();
|
||||||
|
let ip_str = trimmed[second_sep + 1..].trim();
|
||||||
|
|
||||||
|
if host.is_empty() {
|
||||||
|
return Err(ProxyError::Config(format!(
|
||||||
|
"network.dns_overrides host cannot be empty: '{trimmed}'"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
if host.contains(':') {
|
||||||
|
return Err(ProxyError::Config(format!(
|
||||||
|
"network.dns_overrides host must be a domain name without ':' in this format: '{trimmed}'"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
let port = port_str.parse::<u16>().map_err(|_| {
|
||||||
|
ProxyError::Config(format!(
|
||||||
|
"network.dns_overrides port is invalid: '{trimmed}'"
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
let ip = parse_ip_spec(ip_str)?;
|
||||||
|
|
||||||
|
Ok(((host.to_ascii_lowercase(), port), ip))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_entries(entries: &[String]) -> Result<OverrideMap> {
|
||||||
|
let mut parsed = HashMap::new();
|
||||||
|
for entry in entries {
|
||||||
|
let (key, ip) = parse_entry(entry)?;
|
||||||
|
parsed.insert(key, ip);
|
||||||
|
}
|
||||||
|
Ok(parsed)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Validate `network.dns_overrides` entries without updating runtime state.
|
||||||
|
pub fn validate_entries(entries: &[String]) -> Result<()> {
|
||||||
|
let _ = parse_entries(entries)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Replace runtime DNS overrides with a new validated snapshot.
|
||||||
|
pub fn install_entries(entries: &[String]) -> Result<()> {
|
||||||
|
let parsed = parse_entries(entries)?;
|
||||||
|
let mut guard = overrides_store()
|
||||||
|
.write()
|
||||||
|
.map_err(|_| ProxyError::Config("network.dns_overrides runtime lock is poisoned".to_string()))?;
|
||||||
|
*guard = parsed;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Resolve a hostname override for `(host, port)` if present.
|
||||||
|
pub fn resolve(host: &str, port: u16) -> Option<IpAddr> {
|
||||||
|
let key = (host.to_ascii_lowercase(), port);
|
||||||
|
overrides_store()
|
||||||
|
.read()
|
||||||
|
.ok()
|
||||||
|
.and_then(|guard| guard.get(&key).copied())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Resolve a hostname override and construct a socket address when present.
|
||||||
|
pub fn resolve_socket_addr(host: &str, port: u16) -> Option<SocketAddr> {
|
||||||
|
resolve(host, port).map(|ip| SocketAddr::new(ip, port))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parse a runtime endpoint in `host:port` format.
|
||||||
|
///
|
||||||
|
/// Supports:
|
||||||
|
/// - `example.com:443`
|
||||||
|
/// - `[2001:db8::1]:443`
|
||||||
|
pub fn split_host_port(endpoint: &str) -> Option<(String, u16)> {
|
||||||
|
if endpoint.starts_with('[') {
|
||||||
|
let bracket_end = endpoint.find(']')?;
|
||||||
|
if endpoint.as_bytes().get(bracket_end + 1) != Some(&b':') {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let host = endpoint[1..bracket_end].trim();
|
||||||
|
let port = endpoint[bracket_end + 2..].trim().parse::<u16>().ok()?;
|
||||||
|
if host.is_empty() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
return Some((host.to_ascii_lowercase(), port));
|
||||||
|
}
|
||||||
|
|
||||||
|
let split_idx = endpoint.rfind(':')?;
|
||||||
|
let host = endpoint[..split_idx].trim();
|
||||||
|
let port = endpoint[split_idx + 1..].trim().parse::<u16>().ok()?;
|
||||||
|
if host.is_empty() || host.contains(':') {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
Some((host.to_ascii_lowercase(), port))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn validate_accepts_ipv4_and_bracketed_ipv6() {
|
||||||
|
let entries = vec![
|
||||||
|
"example.com:443:127.0.0.1".to_string(),
|
||||||
|
"example.net:8443:[2001:db8::10]".to_string(),
|
||||||
|
];
|
||||||
|
assert!(validate_entries(&entries).is_ok());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn validate_rejects_unbracketed_ipv6() {
|
||||||
|
let entries = vec!["example.net:443:2001:db8::10".to_string()];
|
||||||
|
let err = validate_entries(&entries).unwrap_err().to_string();
|
||||||
|
assert!(err.contains("must be bracketed"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn install_and_resolve_are_case_insensitive_for_host() {
|
||||||
|
let entries = vec!["MyPetrovich.ru:8443:127.0.0.1".to_string()];
|
||||||
|
install_entries(&entries).unwrap();
|
||||||
|
|
||||||
|
let resolved = resolve("mypetrovich.ru", 8443);
|
||||||
|
assert_eq!(resolved, Some("127.0.0.1".parse().unwrap()));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn split_host_port_parses_supported_shapes() {
|
||||||
|
assert_eq!(
|
||||||
|
split_host_port("example.com:443"),
|
||||||
|
Some(("example.com".to_string(), 443))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
split_host_port("[2001:db8::1]:443"),
|
||||||
|
Some(("2001:db8::1".to_string(), 443))
|
||||||
|
);
|
||||||
|
assert_eq!(split_host_port("2001:db8::1:443"), None);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
pub mod dns_overrides;
|
||||||
pub mod probe;
|
pub mod probe;
|
||||||
pub mod stun;
|
pub mod stun;
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,16 @@
|
|||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, UdpSocket};
|
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, UdpSocket};
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
use tracing::{info, warn};
|
use tokio::task::JoinSet;
|
||||||
|
use tokio::time::timeout;
|
||||||
|
use tracing::{debug, info, warn};
|
||||||
|
|
||||||
use crate::config::NetworkConfig;
|
use crate::config::NetworkConfig;
|
||||||
use crate::error::Result;
|
use crate::error::Result;
|
||||||
use crate::network::stun::{stun_probe_dual, DualStunResult, IpFamily};
|
use crate::network::stun::{stun_probe_dual, DualStunResult, IpFamily, StunProbeResult};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default)]
|
#[derive(Debug, Clone, Default)]
|
||||||
pub struct NetworkProbe {
|
pub struct NetworkProbe {
|
||||||
@@ -49,7 +53,13 @@ impl NetworkDecision {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn run_probe(config: &NetworkConfig, stun_addr: Option<String>, nat_probe: bool) -> Result<NetworkProbe> {
|
const STUN_BATCH_TIMEOUT: Duration = Duration::from_secs(5);
|
||||||
|
|
||||||
|
pub async fn run_probe(
|
||||||
|
config: &NetworkConfig,
|
||||||
|
nat_probe: bool,
|
||||||
|
stun_nat_probe_concurrency: usize,
|
||||||
|
) -> Result<NetworkProbe> {
|
||||||
let mut probe = NetworkProbe::default();
|
let mut probe = NetworkProbe::default();
|
||||||
|
|
||||||
probe.detected_ipv4 = detect_local_ip_v4();
|
probe.detected_ipv4 = detect_local_ip_v4();
|
||||||
@@ -58,21 +68,38 @@ pub async fn run_probe(config: &NetworkConfig, stun_addr: Option<String>, nat_pr
|
|||||||
probe.ipv4_is_bogon = probe.detected_ipv4.map(is_bogon_v4).unwrap_or(false);
|
probe.ipv4_is_bogon = probe.detected_ipv4.map(is_bogon_v4).unwrap_or(false);
|
||||||
probe.ipv6_is_bogon = probe.detected_ipv6.map(is_bogon_v6).unwrap_or(false);
|
probe.ipv6_is_bogon = probe.detected_ipv6.map(is_bogon_v6).unwrap_or(false);
|
||||||
|
|
||||||
let stun_server = stun_addr.unwrap_or_else(|| "stun.l.google.com:19302".to_string());
|
let stun_res = if nat_probe && config.stun_use {
|
||||||
let stun_res = if nat_probe {
|
let servers = collect_stun_servers(config);
|
||||||
match stun_probe_dual(&stun_server).await {
|
if servers.is_empty() {
|
||||||
Ok(res) => res,
|
warn!("STUN probe is enabled but network.stun_servers is empty");
|
||||||
Err(e) => {
|
DualStunResult::default()
|
||||||
warn!(error = %e, "STUN probe failed, continuing without reflection");
|
} else {
|
||||||
DualStunResult::default()
|
probe_stun_servers_parallel(
|
||||||
}
|
&servers,
|
||||||
|
stun_nat_probe_concurrency.max(1),
|
||||||
|
)
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
|
} else if nat_probe {
|
||||||
|
info!("STUN probe is disabled by network.stun_use=false");
|
||||||
|
DualStunResult::default()
|
||||||
} else {
|
} else {
|
||||||
DualStunResult::default()
|
DualStunResult::default()
|
||||||
};
|
};
|
||||||
probe.reflected_ipv4 = stun_res.v4.map(|r| r.reflected_addr);
|
probe.reflected_ipv4 = stun_res.v4.map(|r| r.reflected_addr);
|
||||||
probe.reflected_ipv6 = stun_res.v6.map(|r| r.reflected_addr);
|
probe.reflected_ipv6 = stun_res.v6.map(|r| r.reflected_addr);
|
||||||
|
|
||||||
|
// If STUN is blocked but IPv4 is private, try HTTP public-IP fallback.
|
||||||
|
if nat_probe
|
||||||
|
&& probe.reflected_ipv4.is_none()
|
||||||
|
&& probe.detected_ipv4.map(is_bogon_v4).unwrap_or(false)
|
||||||
|
{
|
||||||
|
if let Some(public_ip) = detect_public_ipv4_http(&config.http_ip_detect_urls).await {
|
||||||
|
probe.reflected_ipv4 = Some(SocketAddr::new(IpAddr::V4(public_ip), 0));
|
||||||
|
info!(public_ip = %public_ip, "STUN unavailable, using HTTP public IPv4 fallback");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
probe.ipv4_nat_detected = match (probe.detected_ipv4, probe.reflected_ipv4) {
|
probe.ipv4_nat_detected = match (probe.detected_ipv4, probe.reflected_ipv4) {
|
||||||
(Some(det), Some(reflected)) => det != reflected.ip(),
|
(Some(det), Some(reflected)) => det != reflected.ip(),
|
||||||
_ => false,
|
_ => false,
|
||||||
@@ -94,6 +121,111 @@ pub async fn run_probe(config: &NetworkConfig, stun_addr: Option<String>, nat_pr
|
|||||||
Ok(probe)
|
Ok(probe)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn detect_public_ipv4_http(urls: &[String]) -> Option<Ipv4Addr> {
|
||||||
|
let client = reqwest::Client::builder()
|
||||||
|
.timeout(Duration::from_secs(3))
|
||||||
|
.build()
|
||||||
|
.ok()?;
|
||||||
|
|
||||||
|
for url in urls {
|
||||||
|
let response = match client.get(url).send().await {
|
||||||
|
Ok(response) => response,
|
||||||
|
Err(_) => continue,
|
||||||
|
};
|
||||||
|
|
||||||
|
let body = match response.text().await {
|
||||||
|
Ok(body) => body,
|
||||||
|
Err(_) => continue,
|
||||||
|
};
|
||||||
|
|
||||||
|
let Ok(ip) = body.trim().parse::<Ipv4Addr>() else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
if !is_bogon_v4(ip) {
|
||||||
|
return Some(ip);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn collect_stun_servers(config: &NetworkConfig) -> Vec<String> {
|
||||||
|
let mut out = Vec::new();
|
||||||
|
for s in &config.stun_servers {
|
||||||
|
if !s.is_empty() && !out.contains(s) {
|
||||||
|
out.push(s.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn probe_stun_servers_parallel(
|
||||||
|
servers: &[String],
|
||||||
|
concurrency: usize,
|
||||||
|
) -> DualStunResult {
|
||||||
|
let mut join_set = JoinSet::new();
|
||||||
|
let mut next_idx = 0usize;
|
||||||
|
let mut best_v4_by_ip: HashMap<IpAddr, (usize, StunProbeResult)> = HashMap::new();
|
||||||
|
let mut best_v6_by_ip: HashMap<IpAddr, (usize, StunProbeResult)> = HashMap::new();
|
||||||
|
|
||||||
|
while next_idx < servers.len() || !join_set.is_empty() {
|
||||||
|
while next_idx < servers.len() && join_set.len() < concurrency {
|
||||||
|
let stun_addr = servers[next_idx].clone();
|
||||||
|
next_idx += 1;
|
||||||
|
join_set.spawn(async move {
|
||||||
|
let res = timeout(STUN_BATCH_TIMEOUT, stun_probe_dual(&stun_addr)).await;
|
||||||
|
(stun_addr, res)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let Some(task) = join_set.join_next().await else {
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
|
||||||
|
match task {
|
||||||
|
Ok((stun_addr, Ok(Ok(result)))) => {
|
||||||
|
if let Some(v4) = result.v4 {
|
||||||
|
let entry = best_v4_by_ip.entry(v4.reflected_addr.ip()).or_insert((0, v4));
|
||||||
|
entry.0 += 1;
|
||||||
|
}
|
||||||
|
if let Some(v6) = result.v6 {
|
||||||
|
let entry = best_v6_by_ip.entry(v6.reflected_addr.ip()).or_insert((0, v6));
|
||||||
|
entry.0 += 1;
|
||||||
|
}
|
||||||
|
if result.v4.is_some() || result.v6.is_some() {
|
||||||
|
debug!(stun = %stun_addr, "STUN server responded within probe timeout");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok((stun_addr, Ok(Err(e)))) => {
|
||||||
|
debug!(error = %e, stun = %stun_addr, "STUN probe failed");
|
||||||
|
}
|
||||||
|
Ok((stun_addr, Err(_))) => {
|
||||||
|
debug!(stun = %stun_addr, "STUN probe timeout");
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
debug!(error = %e, "STUN probe task join failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut out = DualStunResult::default();
|
||||||
|
if let Some((_, best)) = best_v4_by_ip
|
||||||
|
.into_values()
|
||||||
|
.max_by_key(|(count, _)| *count)
|
||||||
|
{
|
||||||
|
info!("STUN-Quorum reached, IP: {}", best.reflected_addr.ip());
|
||||||
|
out.v4 = Some(best);
|
||||||
|
}
|
||||||
|
if let Some((_, best)) = best_v6_by_ip
|
||||||
|
.into_values()
|
||||||
|
.max_by_key(|(count, _)| *count)
|
||||||
|
{
|
||||||
|
info!("STUN-Quorum reached, IP: {}", best.reflected_addr.ip());
|
||||||
|
out.v6 = Some(best);
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
pub fn decide_network_capabilities(config: &NetworkConfig, probe: &NetworkProbe) -> NetworkDecision {
|
pub fn decide_network_capabilities(config: &NetworkConfig, probe: &NetworkProbe) -> NetworkDecision {
|
||||||
let ipv4_dc = config.ipv4 && probe.detected_ipv4.is_some();
|
let ipv4_dc = config.ipv4 && probe.detected_ipv4.is_some();
|
||||||
let ipv6_dc = config.ipv6.unwrap_or(probe.detected_ipv6.is_some()) && probe.detected_ipv6.is_some();
|
let ipv6_dc = config.ipv6.unwrap_or(probe.detected_ipv6.is_some()) && probe.detected_ipv6.is_some();
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ use tokio::net::{lookup_host, UdpSocket};
|
|||||||
use tokio::time::{timeout, Duration, sleep};
|
use tokio::time::{timeout, Duration, sleep};
|
||||||
|
|
||||||
use crate::error::{ProxyError, Result};
|
use crate::error::{ProxyError, Result};
|
||||||
|
use crate::network::dns_overrides::{resolve, split_host_port};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub enum IpFamily {
|
pub enum IpFamily {
|
||||||
@@ -40,16 +41,31 @@ pub async fn stun_probe_dual(stun_addr: &str) -> Result<DualStunResult> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn stun_probe_family(stun_addr: &str, family: IpFamily) -> Result<Option<StunProbeResult>> {
|
pub async fn stun_probe_family(stun_addr: &str, family: IpFamily) -> Result<Option<StunProbeResult>> {
|
||||||
|
stun_probe_family_with_bind(stun_addr, family, None).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn stun_probe_family_with_bind(
|
||||||
|
stun_addr: &str,
|
||||||
|
family: IpFamily,
|
||||||
|
bind_ip: Option<IpAddr>,
|
||||||
|
) -> Result<Option<StunProbeResult>> {
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
|
|
||||||
let bind_addr = match family {
|
let bind_addr = match (family, bind_ip) {
|
||||||
IpFamily::V4 => "0.0.0.0:0",
|
(IpFamily::V4, Some(IpAddr::V4(ip))) => SocketAddr::new(IpAddr::V4(ip), 0),
|
||||||
IpFamily::V6 => "[::]:0",
|
(IpFamily::V6, Some(IpAddr::V6(ip))) => SocketAddr::new(IpAddr::V6(ip), 0),
|
||||||
|
(IpFamily::V4, Some(IpAddr::V6(_))) | (IpFamily::V6, Some(IpAddr::V4(_))) => {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
(IpFamily::V4, None) => SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), 0),
|
||||||
|
(IpFamily::V6, None) => SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), 0),
|
||||||
};
|
};
|
||||||
|
|
||||||
let socket = UdpSocket::bind(bind_addr)
|
let socket = match UdpSocket::bind(bind_addr).await {
|
||||||
.await
|
Ok(socket) => socket,
|
||||||
.map_err(|e| ProxyError::Proxy(format!("STUN bind failed: {e}")))?;
|
Err(_) if bind_ip.is_some() => return Ok(None),
|
||||||
|
Err(e) => return Err(ProxyError::Proxy(format!("STUN bind failed: {e}"))),
|
||||||
|
};
|
||||||
|
|
||||||
let target_addr = resolve_stun_addr(stun_addr, family).await?;
|
let target_addr = resolve_stun_addr(stun_addr, family).await?;
|
||||||
if let Some(addr) = target_addr {
|
if let Some(addr) = target_addr {
|
||||||
@@ -198,6 +214,16 @@ async fn resolve_stun_addr(stun_addr: &str, family: IpFamily) -> Result<Option<S
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some((host, port)) = split_host_port(stun_addr)
|
||||||
|
&& let Some(ip) = resolve(&host, port)
|
||||||
|
{
|
||||||
|
let addr = SocketAddr::new(ip, port);
|
||||||
|
return Ok(match (addr.is_ipv4(), family) {
|
||||||
|
(true, IpFamily::V4) | (false, IpFamily::V6) => Some(addr),
|
||||||
|
_ => None,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
let mut addrs = lookup_host(stun_addr)
|
let mut addrs = lookup_host(stun_addr)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| ProxyError::Proxy(format!("STUN resolve failed: {e}")))?;
|
.map_err(|e| ProxyError::Proxy(format!("STUN resolve failed: {e}")))?;
|
||||||
|
|||||||
@@ -91,6 +91,11 @@ where
|
|||||||
stats.increment_connects_all();
|
stats.increment_connects_all();
|
||||||
let mut real_peer = normalize_ip(peer);
|
let mut real_peer = normalize_ip(peer);
|
||||||
|
|
||||||
|
// For non-TCP streams, use a synthetic local address; may be overridden by PROXY protocol dst
|
||||||
|
let mut local_addr: SocketAddr = format!("0.0.0.0:{}", config.server.port)
|
||||||
|
.parse()
|
||||||
|
.unwrap_or_else(|_| "0.0.0.0:443".parse().unwrap());
|
||||||
|
|
||||||
if proxy_protocol_enabled {
|
if proxy_protocol_enabled {
|
||||||
match parse_proxy_protocol(&mut stream, peer).await {
|
match parse_proxy_protocol(&mut stream, peer).await {
|
||||||
Ok(info) => {
|
Ok(info) => {
|
||||||
@@ -101,6 +106,9 @@ where
|
|||||||
"PROXY protocol header parsed"
|
"PROXY protocol header parsed"
|
||||||
);
|
);
|
||||||
real_peer = normalize_ip(info.src_addr);
|
real_peer = normalize_ip(info.src_addr);
|
||||||
|
if let Some(dst) = info.dst_addr {
|
||||||
|
local_addr = dst;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
stats.increment_connects_bad();
|
stats.increment_connects_bad();
|
||||||
@@ -119,11 +127,6 @@ where
|
|||||||
let beobachten_for_timeout = beobachten.clone();
|
let beobachten_for_timeout = beobachten.clone();
|
||||||
let peer_for_timeout = real_peer.ip();
|
let peer_for_timeout = real_peer.ip();
|
||||||
|
|
||||||
// For non-TCP streams, use a synthetic local address
|
|
||||||
let local_addr: SocketAddr = format!("0.0.0.0:{}", config.server.port)
|
|
||||||
.parse()
|
|
||||||
.unwrap_or_else(|_| "0.0.0.0:443".parse().unwrap());
|
|
||||||
|
|
||||||
// Phase 1: handshake (with timeout)
|
// Phase 1: handshake (with timeout)
|
||||||
let outcome = match timeout(handshake_timeout, async {
|
let outcome = match timeout(handshake_timeout, async {
|
||||||
let mut first_bytes = [0u8; 5];
|
let mut first_bytes = [0u8; 5];
|
||||||
@@ -143,7 +146,8 @@ where
|
|||||||
reader,
|
reader,
|
||||||
writer,
|
writer,
|
||||||
&first_bytes,
|
&first_bytes,
|
||||||
real_peer.ip(),
|
real_peer,
|
||||||
|
local_addr,
|
||||||
&config,
|
&config,
|
||||||
&beobachten,
|
&beobachten,
|
||||||
)
|
)
|
||||||
@@ -168,7 +172,8 @@ where
|
|||||||
reader,
|
reader,
|
||||||
writer,
|
writer,
|
||||||
&handshake,
|
&handshake,
|
||||||
real_peer.ip(),
|
real_peer,
|
||||||
|
local_addr,
|
||||||
&config,
|
&config,
|
||||||
&beobachten,
|
&beobachten,
|
||||||
)
|
)
|
||||||
@@ -212,7 +217,8 @@ where
|
|||||||
reader,
|
reader,
|
||||||
writer,
|
writer,
|
||||||
&first_bytes,
|
&first_bytes,
|
||||||
real_peer.ip(),
|
real_peer,
|
||||||
|
local_addr,
|
||||||
&config,
|
&config,
|
||||||
&beobachten,
|
&beobachten,
|
||||||
)
|
)
|
||||||
@@ -237,7 +243,8 @@ where
|
|||||||
reader,
|
reader,
|
||||||
writer,
|
writer,
|
||||||
&handshake,
|
&handshake,
|
||||||
real_peer.ip(),
|
real_peer,
|
||||||
|
local_addr,
|
||||||
&config,
|
&config,
|
||||||
&beobachten,
|
&beobachten,
|
||||||
)
|
)
|
||||||
@@ -405,6 +412,8 @@ impl RunningClientHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn do_handshake(mut self) -> Result<HandshakeOutcome> {
|
async fn do_handshake(mut self) -> Result<HandshakeOutcome> {
|
||||||
|
let mut local_addr = self.stream.local_addr().map_err(ProxyError::Io)?;
|
||||||
|
|
||||||
if self.proxy_protocol_enabled {
|
if self.proxy_protocol_enabled {
|
||||||
match parse_proxy_protocol(&mut self.stream, self.peer).await {
|
match parse_proxy_protocol(&mut self.stream, self.peer).await {
|
||||||
Ok(info) => {
|
Ok(info) => {
|
||||||
@@ -415,6 +424,9 @@ impl RunningClientHandler {
|
|||||||
"PROXY protocol header parsed"
|
"PROXY protocol header parsed"
|
||||||
);
|
);
|
||||||
self.peer = normalize_ip(info.src_addr);
|
self.peer = normalize_ip(info.src_addr);
|
||||||
|
if let Some(dst) = info.dst_addr {
|
||||||
|
local_addr = dst;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
self.stats.increment_connects_bad();
|
self.stats.increment_connects_bad();
|
||||||
@@ -440,13 +452,13 @@ impl RunningClientHandler {
|
|||||||
debug!(peer = %peer, is_tls = is_tls, "Handshake type detected");
|
debug!(peer = %peer, is_tls = is_tls, "Handshake type detected");
|
||||||
|
|
||||||
if is_tls {
|
if is_tls {
|
||||||
self.handle_tls_client(first_bytes).await
|
self.handle_tls_client(first_bytes, local_addr).await
|
||||||
} else {
|
} else {
|
||||||
self.handle_direct_client(first_bytes).await
|
self.handle_direct_client(first_bytes, local_addr).await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_tls_client(mut self, first_bytes: [u8; 5]) -> Result<HandshakeOutcome> {
|
async fn handle_tls_client(mut self, first_bytes: [u8; 5], local_addr: SocketAddr) -> Result<HandshakeOutcome> {
|
||||||
let peer = self.peer;
|
let peer = self.peer;
|
||||||
let _ip_tracker = self.ip_tracker.clone();
|
let _ip_tracker = self.ip_tracker.clone();
|
||||||
|
|
||||||
@@ -462,7 +474,8 @@ impl RunningClientHandler {
|
|||||||
reader,
|
reader,
|
||||||
writer,
|
writer,
|
||||||
&first_bytes,
|
&first_bytes,
|
||||||
peer.ip(),
|
peer,
|
||||||
|
local_addr,
|
||||||
&self.config,
|
&self.config,
|
||||||
&self.beobachten,
|
&self.beobachten,
|
||||||
)
|
)
|
||||||
@@ -479,7 +492,6 @@ impl RunningClientHandler {
|
|||||||
let stats = self.stats.clone();
|
let stats = self.stats.clone();
|
||||||
let buffer_pool = self.buffer_pool.clone();
|
let buffer_pool = self.buffer_pool.clone();
|
||||||
|
|
||||||
let local_addr = self.stream.local_addr().map_err(ProxyError::Io)?;
|
|
||||||
let (read_half, write_half) = self.stream.into_split();
|
let (read_half, write_half) = self.stream.into_split();
|
||||||
|
|
||||||
let (mut tls_reader, tls_writer, _tls_user) = match handle_tls_handshake(
|
let (mut tls_reader, tls_writer, _tls_user) = match handle_tls_handshake(
|
||||||
@@ -501,7 +513,8 @@ impl RunningClientHandler {
|
|||||||
reader,
|
reader,
|
||||||
writer,
|
writer,
|
||||||
&handshake,
|
&handshake,
|
||||||
peer.ip(),
|
peer,
|
||||||
|
local_addr,
|
||||||
&config,
|
&config,
|
||||||
&self.beobachten,
|
&self.beobachten,
|
||||||
)
|
)
|
||||||
@@ -558,7 +571,7 @@ impl RunningClientHandler {
|
|||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_direct_client(mut self, first_bytes: [u8; 5]) -> Result<HandshakeOutcome> {
|
async fn handle_direct_client(mut self, first_bytes: [u8; 5], local_addr: SocketAddr) -> Result<HandshakeOutcome> {
|
||||||
let peer = self.peer;
|
let peer = self.peer;
|
||||||
let _ip_tracker = self.ip_tracker.clone();
|
let _ip_tracker = self.ip_tracker.clone();
|
||||||
|
|
||||||
@@ -570,7 +583,8 @@ impl RunningClientHandler {
|
|||||||
reader,
|
reader,
|
||||||
writer,
|
writer,
|
||||||
&first_bytes,
|
&first_bytes,
|
||||||
peer.ip(),
|
peer,
|
||||||
|
local_addr,
|
||||||
&self.config,
|
&self.config,
|
||||||
&self.beobachten,
|
&self.beobachten,
|
||||||
)
|
)
|
||||||
@@ -587,7 +601,6 @@ impl RunningClientHandler {
|
|||||||
let stats = self.stats.clone();
|
let stats = self.stats.clone();
|
||||||
let buffer_pool = self.buffer_pool.clone();
|
let buffer_pool = self.buffer_pool.clone();
|
||||||
|
|
||||||
let local_addr = self.stream.local_addr().map_err(ProxyError::Io)?;
|
|
||||||
let (read_half, write_half) = self.stream.into_split();
|
let (read_half, write_half) = self.stream.into_split();
|
||||||
|
|
||||||
let (crypto_reader, crypto_writer, success) = match handle_mtproto_handshake(
|
let (crypto_reader, crypto_writer, success) = match handle_mtproto_handshake(
|
||||||
@@ -608,7 +621,8 @@ impl RunningClientHandler {
|
|||||||
reader,
|
reader,
|
||||||
writer,
|
writer,
|
||||||
&handshake,
|
&handshake,
|
||||||
peer.ip(),
|
peer,
|
||||||
|
local_addr,
|
||||||
&config,
|
&config,
|
||||||
&self.beobachten,
|
&self.beobachten,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//! Masking - forward unrecognized traffic to mask host
|
//! Masking - forward unrecognized traffic to mask host
|
||||||
|
|
||||||
use std::str;
|
use std::str;
|
||||||
use std::net::IpAddr;
|
use std::net::SocketAddr;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use tokio::net::TcpStream;
|
use tokio::net::TcpStream;
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
@@ -10,7 +10,9 @@ use tokio::io::{AsyncRead, AsyncWrite, AsyncReadExt, AsyncWriteExt};
|
|||||||
use tokio::time::timeout;
|
use tokio::time::timeout;
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
use crate::config::ProxyConfig;
|
use crate::config::ProxyConfig;
|
||||||
|
use crate::network::dns_overrides::resolve_socket_addr;
|
||||||
use crate::stats::beobachten::BeobachtenStore;
|
use crate::stats::beobachten::BeobachtenStore;
|
||||||
|
use crate::transport::proxy_protocol::{ProxyProtocolV1Builder, ProxyProtocolV2Builder};
|
||||||
|
|
||||||
const MASK_TIMEOUT: Duration = Duration::from_secs(5);
|
const MASK_TIMEOUT: Duration = Duration::from_secs(5);
|
||||||
/// Maximum duration for the entire masking relay.
|
/// Maximum duration for the entire masking relay.
|
||||||
@@ -52,7 +54,8 @@ pub async fn handle_bad_client<R, W>(
|
|||||||
reader: R,
|
reader: R,
|
||||||
writer: W,
|
writer: W,
|
||||||
initial_data: &[u8],
|
initial_data: &[u8],
|
||||||
peer_ip: IpAddr,
|
peer: SocketAddr,
|
||||||
|
local_addr: SocketAddr,
|
||||||
config: &ProxyConfig,
|
config: &ProxyConfig,
|
||||||
beobachten: &BeobachtenStore,
|
beobachten: &BeobachtenStore,
|
||||||
)
|
)
|
||||||
@@ -63,7 +66,7 @@ where
|
|||||||
let client_type = detect_client_type(initial_data);
|
let client_type = detect_client_type(initial_data);
|
||||||
if config.general.beobachten {
|
if config.general.beobachten {
|
||||||
let ttl = Duration::from_secs(config.general.beobachten_minutes.saturating_mul(60));
|
let ttl = Duration::from_secs(config.general.beobachten_minutes.saturating_mul(60));
|
||||||
beobachten.record(client_type, peer_ip, ttl);
|
beobachten.record(client_type, peer.ip(), ttl);
|
||||||
}
|
}
|
||||||
|
|
||||||
if !config.censorship.mask {
|
if !config.censorship.mask {
|
||||||
@@ -85,7 +88,29 @@ where
|
|||||||
let connect_result = timeout(MASK_TIMEOUT, UnixStream::connect(sock_path)).await;
|
let connect_result = timeout(MASK_TIMEOUT, UnixStream::connect(sock_path)).await;
|
||||||
match connect_result {
|
match connect_result {
|
||||||
Ok(Ok(stream)) => {
|
Ok(Ok(stream)) => {
|
||||||
let (mask_read, mask_write) = stream.into_split();
|
let (mask_read, mut mask_write) = stream.into_split();
|
||||||
|
let proxy_header: Option<Vec<u8>> = match config.censorship.mask_proxy_protocol {
|
||||||
|
0 => None,
|
||||||
|
version => {
|
||||||
|
let header = match version {
|
||||||
|
2 => ProxyProtocolV2Builder::new().with_addrs(peer, local_addr).build(),
|
||||||
|
_ => match (peer, local_addr) {
|
||||||
|
(SocketAddr::V4(src), SocketAddr::V4(dst)) =>
|
||||||
|
ProxyProtocolV1Builder::new().tcp4(src.into(), dst.into()).build(),
|
||||||
|
(SocketAddr::V6(src), SocketAddr::V6(dst)) =>
|
||||||
|
ProxyProtocolV1Builder::new().tcp6(src.into(), dst.into()).build(),
|
||||||
|
_ =>
|
||||||
|
ProxyProtocolV1Builder::new().build(),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
Some(header)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if let Some(header) = proxy_header {
|
||||||
|
if mask_write.write_all(&header).await.is_err() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
if timeout(MASK_RELAY_TIMEOUT, relay_to_mask(reader, writer, mask_read, mask_write, initial_data)).await.is_err() {
|
if timeout(MASK_RELAY_TIMEOUT, relay_to_mask(reader, writer, mask_read, mask_write, initial_data)).await.is_err() {
|
||||||
debug!("Mask relay timed out (unix socket)");
|
debug!("Mask relay timed out (unix socket)");
|
||||||
}
|
}
|
||||||
@@ -114,12 +139,37 @@ where
|
|||||||
"Forwarding bad client to mask host"
|
"Forwarding bad client to mask host"
|
||||||
);
|
);
|
||||||
|
|
||||||
// Connect to mask host
|
// Apply runtime DNS override for mask target when configured.
|
||||||
let mask_addr = format!("{}:{}", mask_host, mask_port);
|
let mask_addr = resolve_socket_addr(mask_host, mask_port)
|
||||||
|
.map(|addr| addr.to_string())
|
||||||
|
.unwrap_or_else(|| format!("{}:{}", mask_host, mask_port));
|
||||||
let connect_result = timeout(MASK_TIMEOUT, TcpStream::connect(&mask_addr)).await;
|
let connect_result = timeout(MASK_TIMEOUT, TcpStream::connect(&mask_addr)).await;
|
||||||
match connect_result {
|
match connect_result {
|
||||||
Ok(Ok(stream)) => {
|
Ok(Ok(stream)) => {
|
||||||
let (mask_read, mask_write) = stream.into_split();
|
let proxy_header: Option<Vec<u8>> = match config.censorship.mask_proxy_protocol {
|
||||||
|
0 => None,
|
||||||
|
version => {
|
||||||
|
let header = match version {
|
||||||
|
2 => ProxyProtocolV2Builder::new().with_addrs(peer, local_addr).build(),
|
||||||
|
_ => match (peer, local_addr) {
|
||||||
|
(SocketAddr::V4(src), SocketAddr::V4(dst)) =>
|
||||||
|
ProxyProtocolV1Builder::new().tcp4(src.into(), dst.into()).build(),
|
||||||
|
(SocketAddr::V6(src), SocketAddr::V6(dst)) =>
|
||||||
|
ProxyProtocolV1Builder::new().tcp6(src.into(), dst.into()).build(),
|
||||||
|
_ =>
|
||||||
|
ProxyProtocolV1Builder::new().build(),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
Some(header)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let (mask_read, mut mask_write) = stream.into_split();
|
||||||
|
if let Some(header) = proxy_header {
|
||||||
|
if mask_write.write_all(&header).await.is_err() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
if timeout(MASK_RELAY_TIMEOUT, relay_to_mask(reader, writer, mask_read, mask_write, initial_data)).await.is_err() {
|
if timeout(MASK_RELAY_TIMEOUT, relay_to_mask(reader, writer, mask_read, mask_write, initial_data)).await.is_err() {
|
||||||
debug!("Mask relay timed out");
|
debug!("Mask relay timed out");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,9 @@ enum C2MeCommand {
|
|||||||
|
|
||||||
const DESYNC_DEDUP_WINDOW: Duration = Duration::from_secs(60);
|
const DESYNC_DEDUP_WINDOW: Duration = Duration::from_secs(60);
|
||||||
const DESYNC_ERROR_CLASS: &str = "frame_too_large_crypto_desync";
|
const DESYNC_ERROR_CLASS: &str = "frame_too_large_crypto_desync";
|
||||||
|
const C2ME_CHANNEL_CAPACITY: usize = 1024;
|
||||||
|
const C2ME_SOFT_PRESSURE_MIN_FREE_SLOTS: usize = 64;
|
||||||
|
const C2ME_SENDER_FAIRNESS_BUDGET: usize = 32;
|
||||||
static DESYNC_DEDUP: OnceLock<Mutex<HashMap<u64, Instant>>> = OnceLock::new();
|
static DESYNC_DEDUP: OnceLock<Mutex<HashMap<u64, Instant>>> = OnceLock::new();
|
||||||
|
|
||||||
struct RelayForensicsState {
|
struct RelayForensicsState {
|
||||||
@@ -166,6 +169,27 @@ fn report_desync_frame_too_large(
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn should_yield_c2me_sender(sent_since_yield: usize, has_backlog: bool) -> bool {
|
||||||
|
has_backlog && sent_since_yield >= C2ME_SENDER_FAIRNESS_BUDGET
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn enqueue_c2me_command(
|
||||||
|
tx: &mpsc::Sender<C2MeCommand>,
|
||||||
|
cmd: C2MeCommand,
|
||||||
|
) -> std::result::Result<(), mpsc::error::SendError<C2MeCommand>> {
|
||||||
|
match tx.try_send(cmd) {
|
||||||
|
Ok(()) => Ok(()),
|
||||||
|
Err(mpsc::error::TrySendError::Closed(cmd)) => Err(mpsc::error::SendError(cmd)),
|
||||||
|
Err(mpsc::error::TrySendError::Full(cmd)) => {
|
||||||
|
// Cooperative yield reduces burst catch-up when the per-conn queue is near saturation.
|
||||||
|
if tx.capacity() <= C2ME_SOFT_PRESSURE_MIN_FREE_SLOTS {
|
||||||
|
tokio::task::yield_now().await;
|
||||||
|
}
|
||||||
|
tx.send(cmd).await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) async fn handle_via_middle_proxy<R, W>(
|
pub(crate) async fn handle_via_middle_proxy<R, W>(
|
||||||
mut crypto_reader: CryptoReader<R>,
|
mut crypto_reader: CryptoReader<R>,
|
||||||
crypto_writer: CryptoWriter<W>,
|
crypto_writer: CryptoWriter<W>,
|
||||||
@@ -214,7 +238,22 @@ where
|
|||||||
stats.increment_user_connects(&user);
|
stats.increment_user_connects(&user);
|
||||||
stats.increment_user_curr_connects(&user);
|
stats.increment_user_curr_connects(&user);
|
||||||
|
|
||||||
let proto_flags = proto_flags_for_tag(proto_tag, me_pool.has_proxy_tag());
|
// Per-user ad_tag from access.user_ad_tags; fallback to general.ad_tag (hot-reloadable)
|
||||||
|
let user_tag: Option<Vec<u8>> = config
|
||||||
|
.access
|
||||||
|
.user_ad_tags
|
||||||
|
.get(&user)
|
||||||
|
.and_then(|s| hex::decode(s).ok())
|
||||||
|
.filter(|v| v.len() == 16);
|
||||||
|
let global_tag: Option<Vec<u8>> = config
|
||||||
|
.general
|
||||||
|
.ad_tag
|
||||||
|
.as_ref()
|
||||||
|
.and_then(|s| hex::decode(s).ok())
|
||||||
|
.filter(|v| v.len() == 16);
|
||||||
|
let effective_tag = user_tag.or(global_tag);
|
||||||
|
|
||||||
|
let proto_flags = proto_flags_for_tag(proto_tag, effective_tag.is_some());
|
||||||
debug!(
|
debug!(
|
||||||
trace_id = format_args!("0x{:016x}", trace_id),
|
trace_id = format_args!("0x{:016x}", trace_id),
|
||||||
user = %user,
|
user = %user,
|
||||||
@@ -230,9 +269,11 @@ where
|
|||||||
|
|
||||||
let frame_limit = config.general.max_client_frame;
|
let frame_limit = config.general.max_client_frame;
|
||||||
|
|
||||||
let (c2me_tx, mut c2me_rx) = mpsc::channel::<C2MeCommand>(1024);
|
let (c2me_tx, mut c2me_rx) = mpsc::channel::<C2MeCommand>(C2ME_CHANNEL_CAPACITY);
|
||||||
let me_pool_c2me = me_pool.clone();
|
let me_pool_c2me = me_pool.clone();
|
||||||
|
let effective_tag = effective_tag;
|
||||||
let c2me_sender = tokio::spawn(async move {
|
let c2me_sender = tokio::spawn(async move {
|
||||||
|
let mut sent_since_yield = 0usize;
|
||||||
while let Some(cmd) = c2me_rx.recv().await {
|
while let Some(cmd) = c2me_rx.recv().await {
|
||||||
match cmd {
|
match cmd {
|
||||||
C2MeCommand::Data { payload, flags } => {
|
C2MeCommand::Data { payload, flags } => {
|
||||||
@@ -243,7 +284,13 @@ where
|
|||||||
translated_local_addr,
|
translated_local_addr,
|
||||||
&payload,
|
&payload,
|
||||||
flags,
|
flags,
|
||||||
|
effective_tag.as_deref(),
|
||||||
).await?;
|
).await?;
|
||||||
|
sent_since_yield = sent_since_yield.saturating_add(1);
|
||||||
|
if should_yield_c2me_sender(sent_since_yield, !c2me_rx.is_empty()) {
|
||||||
|
sent_since_yield = 0;
|
||||||
|
tokio::task::yield_now().await;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
C2MeCommand::Close => {
|
C2MeCommand::Close => {
|
||||||
let _ = me_pool_c2me.send_close(conn_id).await;
|
let _ = me_pool_c2me.send_close(conn_id).await;
|
||||||
@@ -360,8 +407,7 @@ where
|
|||||||
flags |= RPC_FLAG_NOT_ENCRYPTED;
|
flags |= RPC_FLAG_NOT_ENCRYPTED;
|
||||||
}
|
}
|
||||||
// Keep client read loop lightweight: route heavy ME send path via a dedicated task.
|
// Keep client read loop lightweight: route heavy ME send path via a dedicated task.
|
||||||
if c2me_tx
|
if enqueue_c2me_command(&c2me_tx, C2MeCommand::Data { payload, flags })
|
||||||
.send(C2MeCommand::Data { payload, flags })
|
|
||||||
.await
|
.await
|
||||||
.is_err()
|
.is_err()
|
||||||
{
|
{
|
||||||
@@ -372,7 +418,7 @@ where
|
|||||||
Ok(None) => {
|
Ok(None) => {
|
||||||
debug!(conn_id, "Client EOF");
|
debug!(conn_id, "Client EOF");
|
||||||
client_closed = true;
|
client_closed = true;
|
||||||
let _ = c2me_tx.send(C2MeCommand::Close).await;
|
let _ = enqueue_c2me_command(&c2me_tx, C2MeCommand::Close).await;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
@@ -647,3 +693,84 @@ where
|
|||||||
// ACK should remain low-latency.
|
// ACK should remain low-latency.
|
||||||
client_writer.flush().await.map_err(ProxyError::Io)
|
client_writer.flush().await.map_err(ProxyError::Io)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use tokio::time::{Duration as TokioDuration, timeout};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn should_yield_sender_only_on_budget_with_backlog() {
|
||||||
|
assert!(!should_yield_c2me_sender(0, true));
|
||||||
|
assert!(!should_yield_c2me_sender(C2ME_SENDER_FAIRNESS_BUDGET - 1, true));
|
||||||
|
assert!(!should_yield_c2me_sender(C2ME_SENDER_FAIRNESS_BUDGET, false));
|
||||||
|
assert!(should_yield_c2me_sender(C2ME_SENDER_FAIRNESS_BUDGET, true));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn enqueue_c2me_command_uses_try_send_fast_path() {
|
||||||
|
let (tx, mut rx) = mpsc::channel::<C2MeCommand>(2);
|
||||||
|
enqueue_c2me_command(
|
||||||
|
&tx,
|
||||||
|
C2MeCommand::Data {
|
||||||
|
payload: vec![1, 2, 3],
|
||||||
|
flags: 0,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let recv = timeout(TokioDuration::from_millis(50), rx.recv())
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
match recv {
|
||||||
|
C2MeCommand::Data { payload, flags } => {
|
||||||
|
assert_eq!(payload, vec![1, 2, 3]);
|
||||||
|
assert_eq!(flags, 0);
|
||||||
|
}
|
||||||
|
C2MeCommand::Close => panic!("unexpected close command"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn enqueue_c2me_command_falls_back_to_send_when_queue_is_full() {
|
||||||
|
let (tx, mut rx) = mpsc::channel::<C2MeCommand>(1);
|
||||||
|
tx.send(C2MeCommand::Data {
|
||||||
|
payload: vec![9],
|
||||||
|
flags: 9,
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let tx2 = tx.clone();
|
||||||
|
let producer = tokio::spawn(async move {
|
||||||
|
enqueue_c2me_command(
|
||||||
|
&tx2,
|
||||||
|
C2MeCommand::Data {
|
||||||
|
payload: vec![7, 7],
|
||||||
|
flags: 7,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
});
|
||||||
|
|
||||||
|
let _ = timeout(TokioDuration::from_millis(100), rx.recv())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
producer.await.unwrap();
|
||||||
|
|
||||||
|
let recv = timeout(TokioDuration::from_millis(100), rx.recv())
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
match recv {
|
||||||
|
C2MeCommand::Data { payload, flags } => {
|
||||||
|
assert_eq!(payload, vec![7, 7]);
|
||||||
|
assert_eq!(flags, 7);
|
||||||
|
}
|
||||||
|
C2MeCommand::Close => panic!("unexpected close command"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
723
src/stats/mod.rs
723
src/stats/mod.rs
@@ -3,8 +3,9 @@
|
|||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
|
|
||||||
pub mod beobachten;
|
pub mod beobachten;
|
||||||
|
pub mod telemetry;
|
||||||
|
|
||||||
use std::sync::atomic::{AtomicU64, Ordering};
|
use std::sync::atomic::{AtomicBool, AtomicU8, AtomicU64, Ordering};
|
||||||
use std::time::{Instant, Duration};
|
use std::time::{Instant, Duration};
|
||||||
use dashmap::DashMap;
|
use dashmap::DashMap;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
@@ -15,6 +16,9 @@ use std::collections::hash_map::DefaultHasher;
|
|||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
|
|
||||||
|
use crate::config::MeTelemetryLevel;
|
||||||
|
use self::telemetry::TelemetryPolicy;
|
||||||
|
|
||||||
// ============= Stats =============
|
// ============= Stats =============
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
@@ -22,17 +26,61 @@ pub struct Stats {
|
|||||||
connects_all: AtomicU64,
|
connects_all: AtomicU64,
|
||||||
connects_bad: AtomicU64,
|
connects_bad: AtomicU64,
|
||||||
handshake_timeouts: AtomicU64,
|
handshake_timeouts: AtomicU64,
|
||||||
|
upstream_connect_attempt_total: AtomicU64,
|
||||||
|
upstream_connect_success_total: AtomicU64,
|
||||||
|
upstream_connect_fail_total: AtomicU64,
|
||||||
|
upstream_connect_failfast_hard_error_total: AtomicU64,
|
||||||
|
upstream_connect_attempts_bucket_1: AtomicU64,
|
||||||
|
upstream_connect_attempts_bucket_2: AtomicU64,
|
||||||
|
upstream_connect_attempts_bucket_3_4: AtomicU64,
|
||||||
|
upstream_connect_attempts_bucket_gt_4: AtomicU64,
|
||||||
|
upstream_connect_duration_success_bucket_le_100ms: AtomicU64,
|
||||||
|
upstream_connect_duration_success_bucket_101_500ms: AtomicU64,
|
||||||
|
upstream_connect_duration_success_bucket_501_1000ms: AtomicU64,
|
||||||
|
upstream_connect_duration_success_bucket_gt_1000ms: AtomicU64,
|
||||||
|
upstream_connect_duration_fail_bucket_le_100ms: AtomicU64,
|
||||||
|
upstream_connect_duration_fail_bucket_101_500ms: AtomicU64,
|
||||||
|
upstream_connect_duration_fail_bucket_501_1000ms: AtomicU64,
|
||||||
|
upstream_connect_duration_fail_bucket_gt_1000ms: AtomicU64,
|
||||||
me_keepalive_sent: AtomicU64,
|
me_keepalive_sent: AtomicU64,
|
||||||
me_keepalive_failed: AtomicU64,
|
me_keepalive_failed: AtomicU64,
|
||||||
me_keepalive_pong: AtomicU64,
|
me_keepalive_pong: AtomicU64,
|
||||||
me_keepalive_timeout: AtomicU64,
|
me_keepalive_timeout: AtomicU64,
|
||||||
|
me_rpc_proxy_req_signal_sent_total: AtomicU64,
|
||||||
|
me_rpc_proxy_req_signal_failed_total: AtomicU64,
|
||||||
|
me_rpc_proxy_req_signal_skipped_no_meta_total: AtomicU64,
|
||||||
|
me_rpc_proxy_req_signal_response_total: AtomicU64,
|
||||||
|
me_rpc_proxy_req_signal_close_sent_total: AtomicU64,
|
||||||
me_reconnect_attempts: AtomicU64,
|
me_reconnect_attempts: AtomicU64,
|
||||||
me_reconnect_success: AtomicU64,
|
me_reconnect_success: AtomicU64,
|
||||||
|
me_handshake_reject_total: AtomicU64,
|
||||||
|
me_reader_eof_total: AtomicU64,
|
||||||
|
me_idle_close_by_peer_total: AtomicU64,
|
||||||
me_crc_mismatch: AtomicU64,
|
me_crc_mismatch: AtomicU64,
|
||||||
me_seq_mismatch: AtomicU64,
|
me_seq_mismatch: AtomicU64,
|
||||||
|
me_endpoint_quarantine_total: AtomicU64,
|
||||||
|
me_kdf_drift_total: AtomicU64,
|
||||||
|
me_kdf_port_only_drift_total: AtomicU64,
|
||||||
|
me_hardswap_pending_reuse_total: AtomicU64,
|
||||||
|
me_hardswap_pending_ttl_expired_total: AtomicU64,
|
||||||
|
me_single_endpoint_outage_enter_total: AtomicU64,
|
||||||
|
me_single_endpoint_outage_exit_total: AtomicU64,
|
||||||
|
me_single_endpoint_outage_reconnect_attempt_total: AtomicU64,
|
||||||
|
me_single_endpoint_outage_reconnect_success_total: AtomicU64,
|
||||||
|
me_single_endpoint_quarantine_bypass_total: AtomicU64,
|
||||||
|
me_single_endpoint_shadow_rotate_total: AtomicU64,
|
||||||
|
me_single_endpoint_shadow_rotate_skipped_quarantine_total: AtomicU64,
|
||||||
|
me_floor_mode_switch_total: AtomicU64,
|
||||||
|
me_floor_mode_switch_static_to_adaptive_total: AtomicU64,
|
||||||
|
me_floor_mode_switch_adaptive_to_static_total: AtomicU64,
|
||||||
|
me_handshake_error_codes: DashMap<i32, AtomicU64>,
|
||||||
me_route_drop_no_conn: AtomicU64,
|
me_route_drop_no_conn: AtomicU64,
|
||||||
me_route_drop_channel_closed: AtomicU64,
|
me_route_drop_channel_closed: AtomicU64,
|
||||||
me_route_drop_queue_full: AtomicU64,
|
me_route_drop_queue_full: AtomicU64,
|
||||||
|
me_route_drop_queue_full_base: AtomicU64,
|
||||||
|
me_route_drop_queue_full_high: AtomicU64,
|
||||||
|
me_socks_kdf_strict_reject: AtomicU64,
|
||||||
|
me_socks_kdf_compat_fallback: AtomicU64,
|
||||||
secure_padding_invalid: AtomicU64,
|
secure_padding_invalid: AtomicU64,
|
||||||
desync_total: AtomicU64,
|
desync_total: AtomicU64,
|
||||||
desync_full_logged: AtomicU64,
|
desync_full_logged: AtomicU64,
|
||||||
@@ -52,6 +100,9 @@ pub struct Stats {
|
|||||||
me_refill_failed_total: AtomicU64,
|
me_refill_failed_total: AtomicU64,
|
||||||
me_writer_restored_same_endpoint_total: AtomicU64,
|
me_writer_restored_same_endpoint_total: AtomicU64,
|
||||||
me_writer_restored_fallback_total: AtomicU64,
|
me_writer_restored_fallback_total: AtomicU64,
|
||||||
|
telemetry_core_enabled: AtomicBool,
|
||||||
|
telemetry_user_enabled: AtomicBool,
|
||||||
|
telemetry_me_level: AtomicU8,
|
||||||
user_stats: DashMap<String, UserStats>,
|
user_stats: DashMap<String, UserStats>,
|
||||||
start_time: parking_lot::RwLock<Option<Instant>>,
|
start_time: parking_lot::RwLock<Option<Instant>>,
|
||||||
}
|
}
|
||||||
@@ -69,44 +120,316 @@ pub struct UserStats {
|
|||||||
impl Stats {
|
impl Stats {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let stats = Self::default();
|
let stats = Self::default();
|
||||||
|
stats.apply_telemetry_policy(TelemetryPolicy::default());
|
||||||
*stats.start_time.write() = Some(Instant::now());
|
*stats.start_time.write() = Some(Instant::now());
|
||||||
stats
|
stats
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn increment_connects_all(&self) { self.connects_all.fetch_add(1, Ordering::Relaxed); }
|
fn telemetry_me_level(&self) -> MeTelemetryLevel {
|
||||||
pub fn increment_connects_bad(&self) { self.connects_bad.fetch_add(1, Ordering::Relaxed); }
|
MeTelemetryLevel::from_u8(self.telemetry_me_level.load(Ordering::Relaxed))
|
||||||
pub fn increment_handshake_timeouts(&self) { self.handshake_timeouts.fetch_add(1, Ordering::Relaxed); }
|
}
|
||||||
pub fn increment_me_keepalive_sent(&self) { self.me_keepalive_sent.fetch_add(1, Ordering::Relaxed); }
|
|
||||||
pub fn increment_me_keepalive_failed(&self) { self.me_keepalive_failed.fetch_add(1, Ordering::Relaxed); }
|
fn telemetry_core_enabled(&self) -> bool {
|
||||||
pub fn increment_me_keepalive_pong(&self) { self.me_keepalive_pong.fetch_add(1, Ordering::Relaxed); }
|
self.telemetry_core_enabled.load(Ordering::Relaxed)
|
||||||
pub fn increment_me_keepalive_timeout(&self) { self.me_keepalive_timeout.fetch_add(1, Ordering::Relaxed); }
|
}
|
||||||
pub fn increment_me_keepalive_timeout_by(&self, value: u64) {
|
|
||||||
self.me_keepalive_timeout.fetch_add(value, Ordering::Relaxed);
|
fn telemetry_user_enabled(&self) -> bool {
|
||||||
|
self.telemetry_user_enabled.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn telemetry_me_allows_normal(&self) -> bool {
|
||||||
|
self.telemetry_me_level().allows_normal()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn telemetry_me_allows_debug(&self) -> bool {
|
||||||
|
self.telemetry_me_level().allows_debug()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn apply_telemetry_policy(&self, policy: TelemetryPolicy) {
|
||||||
|
self.telemetry_core_enabled
|
||||||
|
.store(policy.core_enabled, Ordering::Relaxed);
|
||||||
|
self.telemetry_user_enabled
|
||||||
|
.store(policy.user_enabled, Ordering::Relaxed);
|
||||||
|
self.telemetry_me_level
|
||||||
|
.store(policy.me_level.as_u8(), Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn telemetry_policy(&self) -> TelemetryPolicy {
|
||||||
|
TelemetryPolicy {
|
||||||
|
core_enabled: self.telemetry_core_enabled(),
|
||||||
|
user_enabled: self.telemetry_user_enabled(),
|
||||||
|
me_level: self.telemetry_me_level(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn increment_connects_all(&self) {
|
||||||
|
if self.telemetry_core_enabled() {
|
||||||
|
self.connects_all.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_connects_bad(&self) {
|
||||||
|
if self.telemetry_core_enabled() {
|
||||||
|
self.connects_bad.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_handshake_timeouts(&self) {
|
||||||
|
if self.telemetry_core_enabled() {
|
||||||
|
self.handshake_timeouts.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_upstream_connect_attempt_total(&self) {
|
||||||
|
if self.telemetry_core_enabled() {
|
||||||
|
self.upstream_connect_attempt_total
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_upstream_connect_success_total(&self) {
|
||||||
|
if self.telemetry_core_enabled() {
|
||||||
|
self.upstream_connect_success_total
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_upstream_connect_fail_total(&self) {
|
||||||
|
if self.telemetry_core_enabled() {
|
||||||
|
self.upstream_connect_fail_total
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_upstream_connect_failfast_hard_error_total(&self) {
|
||||||
|
if self.telemetry_core_enabled() {
|
||||||
|
self.upstream_connect_failfast_hard_error_total
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn observe_upstream_connect_attempts_per_request(&self, attempts: u32) {
|
||||||
|
if !self.telemetry_core_enabled() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
match attempts {
|
||||||
|
0 => {}
|
||||||
|
1 => {
|
||||||
|
self.upstream_connect_attempts_bucket_1
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
2 => {
|
||||||
|
self.upstream_connect_attempts_bucket_2
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
3..=4 => {
|
||||||
|
self.upstream_connect_attempts_bucket_3_4
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
self.upstream_connect_attempts_bucket_gt_4
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn observe_upstream_connect_duration_ms(&self, duration_ms: u64, success: bool) {
|
||||||
|
if !self.telemetry_core_enabled() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let bucket = match duration_ms {
|
||||||
|
0..=100 => 0u8,
|
||||||
|
101..=500 => 1u8,
|
||||||
|
501..=1000 => 2u8,
|
||||||
|
_ => 3u8,
|
||||||
|
};
|
||||||
|
match (success, bucket) {
|
||||||
|
(true, 0) => {
|
||||||
|
self.upstream_connect_duration_success_bucket_le_100ms
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
(true, 1) => {
|
||||||
|
self.upstream_connect_duration_success_bucket_101_500ms
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
(true, 2) => {
|
||||||
|
self.upstream_connect_duration_success_bucket_501_1000ms
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
(true, _) => {
|
||||||
|
self.upstream_connect_duration_success_bucket_gt_1000ms
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
(false, 0) => {
|
||||||
|
self.upstream_connect_duration_fail_bucket_le_100ms
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
(false, 1) => {
|
||||||
|
self.upstream_connect_duration_fail_bucket_101_500ms
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
(false, 2) => {
|
||||||
|
self.upstream_connect_duration_fail_bucket_501_1000ms
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
(false, _) => {
|
||||||
|
self.upstream_connect_duration_fail_bucket_gt_1000ms
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_keepalive_sent(&self) {
|
||||||
|
if self.telemetry_me_allows_debug() {
|
||||||
|
self.me_keepalive_sent.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_keepalive_failed(&self) {
|
||||||
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_keepalive_failed.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_keepalive_pong(&self) {
|
||||||
|
if self.telemetry_me_allows_debug() {
|
||||||
|
self.me_keepalive_pong.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_keepalive_timeout(&self) {
|
||||||
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_keepalive_timeout.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_keepalive_timeout_by(&self, value: u64) {
|
||||||
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_keepalive_timeout.fetch_add(value, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_rpc_proxy_req_signal_sent_total(&self) {
|
||||||
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_rpc_proxy_req_signal_sent_total
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_rpc_proxy_req_signal_failed_total(&self) {
|
||||||
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_rpc_proxy_req_signal_failed_total
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_rpc_proxy_req_signal_skipped_no_meta_total(&self) {
|
||||||
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_rpc_proxy_req_signal_skipped_no_meta_total
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_rpc_proxy_req_signal_response_total(&self) {
|
||||||
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_rpc_proxy_req_signal_response_total
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_rpc_proxy_req_signal_close_sent_total(&self) {
|
||||||
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_rpc_proxy_req_signal_close_sent_total
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_reconnect_attempt(&self) {
|
||||||
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_reconnect_attempts.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_reconnect_success(&self) {
|
||||||
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_reconnect_success.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_handshake_reject_total(&self) {
|
||||||
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_handshake_reject_total.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_handshake_error_code(&self, code: i32) {
|
||||||
|
if !self.telemetry_me_allows_normal() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let entry = self
|
||||||
|
.me_handshake_error_codes
|
||||||
|
.entry(code)
|
||||||
|
.or_insert_with(|| AtomicU64::new(0));
|
||||||
|
entry.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
pub fn increment_me_reader_eof_total(&self) {
|
||||||
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_reader_eof_total.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_idle_close_by_peer_total(&self) {
|
||||||
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_idle_close_by_peer_total
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_crc_mismatch(&self) {
|
||||||
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_crc_mismatch.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_seq_mismatch(&self) {
|
||||||
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_seq_mismatch.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_route_drop_no_conn(&self) {
|
||||||
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_route_drop_no_conn.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pub fn increment_me_reconnect_attempt(&self) { self.me_reconnect_attempts.fetch_add(1, Ordering::Relaxed); }
|
|
||||||
pub fn increment_me_reconnect_success(&self) { self.me_reconnect_success.fetch_add(1, Ordering::Relaxed); }
|
|
||||||
pub fn increment_me_crc_mismatch(&self) { self.me_crc_mismatch.fetch_add(1, Ordering::Relaxed); }
|
|
||||||
pub fn increment_me_seq_mismatch(&self) { self.me_seq_mismatch.fetch_add(1, Ordering::Relaxed); }
|
|
||||||
pub fn increment_me_route_drop_no_conn(&self) { self.me_route_drop_no_conn.fetch_add(1, Ordering::Relaxed); }
|
|
||||||
pub fn increment_me_route_drop_channel_closed(&self) {
|
pub fn increment_me_route_drop_channel_closed(&self) {
|
||||||
self.me_route_drop_channel_closed.fetch_add(1, Ordering::Relaxed);
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_route_drop_channel_closed.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pub fn increment_me_route_drop_queue_full(&self) {
|
pub fn increment_me_route_drop_queue_full(&self) {
|
||||||
self.me_route_drop_queue_full.fetch_add(1, Ordering::Relaxed);
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_route_drop_queue_full.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_route_drop_queue_full_base(&self) {
|
||||||
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_route_drop_queue_full_base.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_route_drop_queue_full_high(&self) {
|
||||||
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_route_drop_queue_full_high.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_socks_kdf_strict_reject(&self) {
|
||||||
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_socks_kdf_strict_reject.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_socks_kdf_compat_fallback(&self) {
|
||||||
|
if self.telemetry_me_allows_debug() {
|
||||||
|
self.me_socks_kdf_compat_fallback.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pub fn increment_secure_padding_invalid(&self) {
|
pub fn increment_secure_padding_invalid(&self) {
|
||||||
self.secure_padding_invalid.fetch_add(1, Ordering::Relaxed);
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.secure_padding_invalid.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pub fn increment_desync_total(&self) {
|
pub fn increment_desync_total(&self) {
|
||||||
self.desync_total.fetch_add(1, Ordering::Relaxed);
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.desync_total.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pub fn increment_desync_full_logged(&self) {
|
pub fn increment_desync_full_logged(&self) {
|
||||||
self.desync_full_logged.fetch_add(1, Ordering::Relaxed);
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.desync_full_logged.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pub fn increment_desync_suppressed(&self) {
|
pub fn increment_desync_suppressed(&self) {
|
||||||
self.desync_suppressed.fetch_add(1, Ordering::Relaxed);
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.desync_suppressed.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pub fn observe_desync_frames_ok(&self, frames_ok: u64) {
|
pub fn observe_desync_frames_ok(&self, frames_ok: u64) {
|
||||||
|
if !self.telemetry_me_allows_normal() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
match frames_ok {
|
match frames_ok {
|
||||||
0 => {
|
0 => {
|
||||||
self.desync_frames_bucket_0.fetch_add(1, Ordering::Relaxed);
|
self.desync_frames_bucket_0.fetch_add(1, Ordering::Relaxed);
|
||||||
@@ -123,12 +446,19 @@ impl Stats {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn increment_pool_swap_total(&self) {
|
pub fn increment_pool_swap_total(&self) {
|
||||||
self.pool_swap_total.fetch_add(1, Ordering::Relaxed);
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.pool_swap_total.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pub fn increment_pool_drain_active(&self) {
|
pub fn increment_pool_drain_active(&self) {
|
||||||
self.pool_drain_active.fetch_add(1, Ordering::Relaxed);
|
if self.telemetry_me_allows_debug() {
|
||||||
|
self.pool_drain_active.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pub fn decrement_pool_drain_active(&self) {
|
pub fn decrement_pool_drain_active(&self) {
|
||||||
|
if !self.telemetry_me_allows_debug() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
let mut current = self.pool_drain_active.load(Ordering::Relaxed);
|
let mut current = self.pool_drain_active.load(Ordering::Relaxed);
|
||||||
loop {
|
loop {
|
||||||
if current == 0 {
|
if current == 0 {
|
||||||
@@ -146,31 +476,140 @@ impl Stats {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn increment_pool_force_close_total(&self) {
|
pub fn increment_pool_force_close_total(&self) {
|
||||||
self.pool_force_close_total.fetch_add(1, Ordering::Relaxed);
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.pool_force_close_total.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pub fn increment_pool_stale_pick_total(&self) {
|
pub fn increment_pool_stale_pick_total(&self) {
|
||||||
self.pool_stale_pick_total.fetch_add(1, Ordering::Relaxed);
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.pool_stale_pick_total.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pub fn increment_me_writer_removed_total(&self) {
|
pub fn increment_me_writer_removed_total(&self) {
|
||||||
self.me_writer_removed_total.fetch_add(1, Ordering::Relaxed);
|
if self.telemetry_me_allows_debug() {
|
||||||
|
self.me_writer_removed_total.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pub fn increment_me_writer_removed_unexpected_total(&self) {
|
pub fn increment_me_writer_removed_unexpected_total(&self) {
|
||||||
self.me_writer_removed_unexpected_total.fetch_add(1, Ordering::Relaxed);
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_writer_removed_unexpected_total.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pub fn increment_me_refill_triggered_total(&self) {
|
pub fn increment_me_refill_triggered_total(&self) {
|
||||||
self.me_refill_triggered_total.fetch_add(1, Ordering::Relaxed);
|
if self.telemetry_me_allows_debug() {
|
||||||
|
self.me_refill_triggered_total.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pub fn increment_me_refill_skipped_inflight_total(&self) {
|
pub fn increment_me_refill_skipped_inflight_total(&self) {
|
||||||
self.me_refill_skipped_inflight_total.fetch_add(1, Ordering::Relaxed);
|
if self.telemetry_me_allows_debug() {
|
||||||
|
self.me_refill_skipped_inflight_total.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pub fn increment_me_refill_failed_total(&self) {
|
pub fn increment_me_refill_failed_total(&self) {
|
||||||
self.me_refill_failed_total.fetch_add(1, Ordering::Relaxed);
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_refill_failed_total.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pub fn increment_me_writer_restored_same_endpoint_total(&self) {
|
pub fn increment_me_writer_restored_same_endpoint_total(&self) {
|
||||||
self.me_writer_restored_same_endpoint_total.fetch_add(1, Ordering::Relaxed);
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_writer_restored_same_endpoint_total
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pub fn increment_me_writer_restored_fallback_total(&self) {
|
pub fn increment_me_writer_restored_fallback_total(&self) {
|
||||||
self.me_writer_restored_fallback_total.fetch_add(1, Ordering::Relaxed);
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_writer_restored_fallback_total
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_endpoint_quarantine_total(&self) {
|
||||||
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_endpoint_quarantine_total
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_kdf_drift_total(&self) {
|
||||||
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_kdf_drift_total.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_kdf_port_only_drift_total(&self) {
|
||||||
|
if self.telemetry_me_allows_debug() {
|
||||||
|
self.me_kdf_port_only_drift_total
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_hardswap_pending_reuse_total(&self) {
|
||||||
|
if self.telemetry_me_allows_debug() {
|
||||||
|
self.me_hardswap_pending_reuse_total
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_hardswap_pending_ttl_expired_total(&self) {
|
||||||
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_hardswap_pending_ttl_expired_total
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_single_endpoint_outage_enter_total(&self) {
|
||||||
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_single_endpoint_outage_enter_total
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_single_endpoint_outage_exit_total(&self) {
|
||||||
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_single_endpoint_outage_exit_total
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_single_endpoint_outage_reconnect_attempt_total(&self) {
|
||||||
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_single_endpoint_outage_reconnect_attempt_total
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_single_endpoint_outage_reconnect_success_total(&self) {
|
||||||
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_single_endpoint_outage_reconnect_success_total
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_single_endpoint_quarantine_bypass_total(&self) {
|
||||||
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_single_endpoint_quarantine_bypass_total
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_single_endpoint_shadow_rotate_total(&self) {
|
||||||
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_single_endpoint_shadow_rotate_total
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_single_endpoint_shadow_rotate_skipped_quarantine_total(&self) {
|
||||||
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_single_endpoint_shadow_rotate_skipped_quarantine_total
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_floor_mode_switch_total(&self) {
|
||||||
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_floor_mode_switch_total
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_floor_mode_switch_static_to_adaptive_total(&self) {
|
||||||
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_floor_mode_switch_static_to_adaptive_total
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_me_floor_mode_switch_adaptive_to_static_total(&self) {
|
||||||
|
if self.telemetry_me_allows_normal() {
|
||||||
|
self.me_floor_mode_switch_adaptive_to_static_total
|
||||||
|
.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pub fn get_connects_all(&self) -> u64 { self.connects_all.load(Ordering::Relaxed) }
|
pub fn get_connects_all(&self) -> u64 { self.connects_all.load(Ordering::Relaxed) }
|
||||||
pub fn get_connects_bad(&self) -> u64 { self.connects_bad.load(Ordering::Relaxed) }
|
pub fn get_connects_bad(&self) -> u64 { self.connects_bad.load(Ordering::Relaxed) }
|
||||||
@@ -178,10 +617,104 @@ impl Stats {
|
|||||||
pub fn get_me_keepalive_failed(&self) -> u64 { self.me_keepalive_failed.load(Ordering::Relaxed) }
|
pub fn get_me_keepalive_failed(&self) -> u64 { self.me_keepalive_failed.load(Ordering::Relaxed) }
|
||||||
pub fn get_me_keepalive_pong(&self) -> u64 { self.me_keepalive_pong.load(Ordering::Relaxed) }
|
pub fn get_me_keepalive_pong(&self) -> u64 { self.me_keepalive_pong.load(Ordering::Relaxed) }
|
||||||
pub fn get_me_keepalive_timeout(&self) -> u64 { self.me_keepalive_timeout.load(Ordering::Relaxed) }
|
pub fn get_me_keepalive_timeout(&self) -> u64 { self.me_keepalive_timeout.load(Ordering::Relaxed) }
|
||||||
|
pub fn get_me_rpc_proxy_req_signal_sent_total(&self) -> u64 {
|
||||||
|
self.me_rpc_proxy_req_signal_sent_total
|
||||||
|
.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_me_rpc_proxy_req_signal_failed_total(&self) -> u64 {
|
||||||
|
self.me_rpc_proxy_req_signal_failed_total
|
||||||
|
.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_me_rpc_proxy_req_signal_skipped_no_meta_total(&self) -> u64 {
|
||||||
|
self.me_rpc_proxy_req_signal_skipped_no_meta_total
|
||||||
|
.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_me_rpc_proxy_req_signal_response_total(&self) -> u64 {
|
||||||
|
self.me_rpc_proxy_req_signal_response_total
|
||||||
|
.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_me_rpc_proxy_req_signal_close_sent_total(&self) -> u64 {
|
||||||
|
self.me_rpc_proxy_req_signal_close_sent_total
|
||||||
|
.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
pub fn get_me_reconnect_attempts(&self) -> u64 { self.me_reconnect_attempts.load(Ordering::Relaxed) }
|
pub fn get_me_reconnect_attempts(&self) -> u64 { self.me_reconnect_attempts.load(Ordering::Relaxed) }
|
||||||
pub fn get_me_reconnect_success(&self) -> u64 { self.me_reconnect_success.load(Ordering::Relaxed) }
|
pub fn get_me_reconnect_success(&self) -> u64 { self.me_reconnect_success.load(Ordering::Relaxed) }
|
||||||
|
pub fn get_me_handshake_reject_total(&self) -> u64 {
|
||||||
|
self.me_handshake_reject_total.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_me_reader_eof_total(&self) -> u64 {
|
||||||
|
self.me_reader_eof_total.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_me_idle_close_by_peer_total(&self) -> u64 {
|
||||||
|
self.me_idle_close_by_peer_total.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
pub fn get_me_crc_mismatch(&self) -> u64 { self.me_crc_mismatch.load(Ordering::Relaxed) }
|
pub fn get_me_crc_mismatch(&self) -> u64 { self.me_crc_mismatch.load(Ordering::Relaxed) }
|
||||||
pub fn get_me_seq_mismatch(&self) -> u64 { self.me_seq_mismatch.load(Ordering::Relaxed) }
|
pub fn get_me_seq_mismatch(&self) -> u64 { self.me_seq_mismatch.load(Ordering::Relaxed) }
|
||||||
|
pub fn get_me_endpoint_quarantine_total(&self) -> u64 {
|
||||||
|
self.me_endpoint_quarantine_total.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_me_kdf_drift_total(&self) -> u64 {
|
||||||
|
self.me_kdf_drift_total.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_me_kdf_port_only_drift_total(&self) -> u64 {
|
||||||
|
self.me_kdf_port_only_drift_total.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_me_hardswap_pending_reuse_total(&self) -> u64 {
|
||||||
|
self.me_hardswap_pending_reuse_total
|
||||||
|
.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_me_hardswap_pending_ttl_expired_total(&self) -> u64 {
|
||||||
|
self.me_hardswap_pending_ttl_expired_total
|
||||||
|
.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_me_single_endpoint_outage_enter_total(&self) -> u64 {
|
||||||
|
self.me_single_endpoint_outage_enter_total
|
||||||
|
.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_me_single_endpoint_outage_exit_total(&self) -> u64 {
|
||||||
|
self.me_single_endpoint_outage_exit_total
|
||||||
|
.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_me_single_endpoint_outage_reconnect_attempt_total(&self) -> u64 {
|
||||||
|
self.me_single_endpoint_outage_reconnect_attempt_total
|
||||||
|
.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_me_single_endpoint_outage_reconnect_success_total(&self) -> u64 {
|
||||||
|
self.me_single_endpoint_outage_reconnect_success_total
|
||||||
|
.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_me_single_endpoint_quarantine_bypass_total(&self) -> u64 {
|
||||||
|
self.me_single_endpoint_quarantine_bypass_total
|
||||||
|
.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_me_single_endpoint_shadow_rotate_total(&self) -> u64 {
|
||||||
|
self.me_single_endpoint_shadow_rotate_total
|
||||||
|
.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_me_single_endpoint_shadow_rotate_skipped_quarantine_total(&self) -> u64 {
|
||||||
|
self.me_single_endpoint_shadow_rotate_skipped_quarantine_total
|
||||||
|
.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_me_floor_mode_switch_total(&self) -> u64 {
|
||||||
|
self.me_floor_mode_switch_total.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_me_floor_mode_switch_static_to_adaptive_total(&self) -> u64 {
|
||||||
|
self.me_floor_mode_switch_static_to_adaptive_total
|
||||||
|
.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_me_floor_mode_switch_adaptive_to_static_total(&self) -> u64 {
|
||||||
|
self.me_floor_mode_switch_adaptive_to_static_total
|
||||||
|
.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_me_handshake_error_code_counts(&self) -> Vec<(i32, u64)> {
|
||||||
|
let mut out: Vec<(i32, u64)> = self
|
||||||
|
.me_handshake_error_codes
|
||||||
|
.iter()
|
||||||
|
.map(|entry| (*entry.key(), entry.value().load(Ordering::Relaxed)))
|
||||||
|
.collect();
|
||||||
|
out.sort_by_key(|(code, _)| *code);
|
||||||
|
out
|
||||||
|
}
|
||||||
pub fn get_me_route_drop_no_conn(&self) -> u64 { self.me_route_drop_no_conn.load(Ordering::Relaxed) }
|
pub fn get_me_route_drop_no_conn(&self) -> u64 { self.me_route_drop_no_conn.load(Ordering::Relaxed) }
|
||||||
pub fn get_me_route_drop_channel_closed(&self) -> u64 {
|
pub fn get_me_route_drop_channel_closed(&self) -> u64 {
|
||||||
self.me_route_drop_channel_closed.load(Ordering::Relaxed)
|
self.me_route_drop_channel_closed.load(Ordering::Relaxed)
|
||||||
@@ -189,6 +722,18 @@ impl Stats {
|
|||||||
pub fn get_me_route_drop_queue_full(&self) -> u64 {
|
pub fn get_me_route_drop_queue_full(&self) -> u64 {
|
||||||
self.me_route_drop_queue_full.load(Ordering::Relaxed)
|
self.me_route_drop_queue_full.load(Ordering::Relaxed)
|
||||||
}
|
}
|
||||||
|
pub fn get_me_route_drop_queue_full_base(&self) -> u64 {
|
||||||
|
self.me_route_drop_queue_full_base.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_me_route_drop_queue_full_high(&self) -> u64 {
|
||||||
|
self.me_route_drop_queue_full_high.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_me_socks_kdf_strict_reject(&self) -> u64 {
|
||||||
|
self.me_socks_kdf_strict_reject.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_me_socks_kdf_compat_fallback(&self) -> u64 {
|
||||||
|
self.me_socks_kdf_compat_fallback.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
pub fn get_secure_padding_invalid(&self) -> u64 {
|
pub fn get_secure_padding_invalid(&self) -> u64 {
|
||||||
self.secure_padding_invalid.load(Ordering::Relaxed)
|
self.secure_padding_invalid.load(Ordering::Relaxed)
|
||||||
}
|
}
|
||||||
@@ -248,11 +793,17 @@ impl Stats {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn increment_user_connects(&self, user: &str) {
|
pub fn increment_user_connects(&self, user: &str) {
|
||||||
|
if !self.telemetry_user_enabled() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
self.user_stats.entry(user.to_string()).or_default()
|
self.user_stats.entry(user.to_string()).or_default()
|
||||||
.connects.fetch_add(1, Ordering::Relaxed);
|
.connects.fetch_add(1, Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn increment_user_curr_connects(&self, user: &str) {
|
pub fn increment_user_curr_connects(&self, user: &str) {
|
||||||
|
if !self.telemetry_user_enabled() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
self.user_stats.entry(user.to_string()).or_default()
|
self.user_stats.entry(user.to_string()).or_default()
|
||||||
.curr_connects.fetch_add(1, Ordering::Relaxed);
|
.curr_connects.fetch_add(1, Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
@@ -285,21 +836,33 @@ impl Stats {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_user_octets_from(&self, user: &str, bytes: u64) {
|
pub fn add_user_octets_from(&self, user: &str, bytes: u64) {
|
||||||
|
if !self.telemetry_user_enabled() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
self.user_stats.entry(user.to_string()).or_default()
|
self.user_stats.entry(user.to_string()).or_default()
|
||||||
.octets_from_client.fetch_add(bytes, Ordering::Relaxed);
|
.octets_from_client.fetch_add(bytes, Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_user_octets_to(&self, user: &str, bytes: u64) {
|
pub fn add_user_octets_to(&self, user: &str, bytes: u64) {
|
||||||
|
if !self.telemetry_user_enabled() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
self.user_stats.entry(user.to_string()).or_default()
|
self.user_stats.entry(user.to_string()).or_default()
|
||||||
.octets_to_client.fetch_add(bytes, Ordering::Relaxed);
|
.octets_to_client.fetch_add(bytes, Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn increment_user_msgs_from(&self, user: &str) {
|
pub fn increment_user_msgs_from(&self, user: &str) {
|
||||||
|
if !self.telemetry_user_enabled() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
self.user_stats.entry(user.to_string()).or_default()
|
self.user_stats.entry(user.to_string()).or_default()
|
||||||
.msgs_from_client.fetch_add(1, Ordering::Relaxed);
|
.msgs_from_client.fetch_add(1, Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn increment_user_msgs_to(&self, user: &str) {
|
pub fn increment_user_msgs_to(&self, user: &str) {
|
||||||
|
if !self.telemetry_user_enabled() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
self.user_stats.entry(user.to_string()).or_default()
|
self.user_stats.entry(user.to_string()).or_default()
|
||||||
.msgs_to_client.fetch_add(1, Ordering::Relaxed);
|
.msgs_to_client.fetch_add(1, Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
@@ -314,6 +877,65 @@ impl Stats {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_handshake_timeouts(&self) -> u64 { self.handshake_timeouts.load(Ordering::Relaxed) }
|
pub fn get_handshake_timeouts(&self) -> u64 { self.handshake_timeouts.load(Ordering::Relaxed) }
|
||||||
|
pub fn get_upstream_connect_attempt_total(&self) -> u64 {
|
||||||
|
self.upstream_connect_attempt_total.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_upstream_connect_success_total(&self) -> u64 {
|
||||||
|
self.upstream_connect_success_total.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_upstream_connect_fail_total(&self) -> u64 {
|
||||||
|
self.upstream_connect_fail_total.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_upstream_connect_failfast_hard_error_total(&self) -> u64 {
|
||||||
|
self.upstream_connect_failfast_hard_error_total
|
||||||
|
.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_upstream_connect_attempts_bucket_1(&self) -> u64 {
|
||||||
|
self.upstream_connect_attempts_bucket_1.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_upstream_connect_attempts_bucket_2(&self) -> u64 {
|
||||||
|
self.upstream_connect_attempts_bucket_2.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_upstream_connect_attempts_bucket_3_4(&self) -> u64 {
|
||||||
|
self.upstream_connect_attempts_bucket_3_4
|
||||||
|
.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_upstream_connect_attempts_bucket_gt_4(&self) -> u64 {
|
||||||
|
self.upstream_connect_attempts_bucket_gt_4
|
||||||
|
.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_upstream_connect_duration_success_bucket_le_100ms(&self) -> u64 {
|
||||||
|
self.upstream_connect_duration_success_bucket_le_100ms
|
||||||
|
.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_upstream_connect_duration_success_bucket_101_500ms(&self) -> u64 {
|
||||||
|
self.upstream_connect_duration_success_bucket_101_500ms
|
||||||
|
.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_upstream_connect_duration_success_bucket_501_1000ms(&self) -> u64 {
|
||||||
|
self.upstream_connect_duration_success_bucket_501_1000ms
|
||||||
|
.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_upstream_connect_duration_success_bucket_gt_1000ms(&self) -> u64 {
|
||||||
|
self.upstream_connect_duration_success_bucket_gt_1000ms
|
||||||
|
.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_upstream_connect_duration_fail_bucket_le_100ms(&self) -> u64 {
|
||||||
|
self.upstream_connect_duration_fail_bucket_le_100ms
|
||||||
|
.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_upstream_connect_duration_fail_bucket_101_500ms(&self) -> u64 {
|
||||||
|
self.upstream_connect_duration_fail_bucket_101_500ms
|
||||||
|
.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_upstream_connect_duration_fail_bucket_501_1000ms(&self) -> u64 {
|
||||||
|
self.upstream_connect_duration_fail_bucket_501_1000ms
|
||||||
|
.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_upstream_connect_duration_fail_bucket_gt_1000ms(&self) -> u64 {
|
||||||
|
self.upstream_connect_duration_fail_bucket_gt_1000ms
|
||||||
|
.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn iter_user_stats(&self) -> dashmap::iter::Iter<'_, String, UserStats> {
|
pub fn iter_user_stats(&self) -> dashmap::iter::Iter<'_, String, UserStats> {
|
||||||
self.user_stats.iter()
|
self.user_stats.iter()
|
||||||
@@ -548,6 +1170,7 @@ impl ReplayStats {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::config::MeTelemetryLevel;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -558,6 +1181,40 @@ mod tests {
|
|||||||
stats.increment_connects_all();
|
stats.increment_connects_all();
|
||||||
assert_eq!(stats.get_connects_all(), 3);
|
assert_eq!(stats.get_connects_all(), 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_telemetry_policy_disables_core_and_user_counters() {
|
||||||
|
let stats = Stats::new();
|
||||||
|
stats.apply_telemetry_policy(TelemetryPolicy {
|
||||||
|
core_enabled: false,
|
||||||
|
user_enabled: false,
|
||||||
|
me_level: MeTelemetryLevel::Normal,
|
||||||
|
});
|
||||||
|
|
||||||
|
stats.increment_connects_all();
|
||||||
|
stats.increment_user_connects("alice");
|
||||||
|
stats.add_user_octets_from("alice", 1024);
|
||||||
|
assert_eq!(stats.get_connects_all(), 0);
|
||||||
|
assert_eq!(stats.get_user_curr_connects("alice"), 0);
|
||||||
|
assert_eq!(stats.get_user_total_octets("alice"), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_telemetry_policy_me_silent_blocks_me_counters() {
|
||||||
|
let stats = Stats::new();
|
||||||
|
stats.apply_telemetry_policy(TelemetryPolicy {
|
||||||
|
core_enabled: true,
|
||||||
|
user_enabled: true,
|
||||||
|
me_level: MeTelemetryLevel::Silent,
|
||||||
|
});
|
||||||
|
|
||||||
|
stats.increment_me_crc_mismatch();
|
||||||
|
stats.increment_me_keepalive_sent();
|
||||||
|
stats.increment_me_route_drop_queue_full();
|
||||||
|
assert_eq!(stats.get_me_crc_mismatch(), 0);
|
||||||
|
assert_eq!(stats.get_me_keepalive_sent(), 0);
|
||||||
|
assert_eq!(stats.get_me_route_drop_queue_full(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_replay_checker_basic() {
|
fn test_replay_checker_basic() {
|
||||||
|
|||||||
29
src/stats/telemetry.rs
Normal file
29
src/stats/telemetry.rs
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
use crate::config::{MeTelemetryLevel, TelemetryConfig};
|
||||||
|
|
||||||
|
/// Runtime telemetry policy used by hot-path counters.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub struct TelemetryPolicy {
|
||||||
|
pub core_enabled: bool,
|
||||||
|
pub user_enabled: bool,
|
||||||
|
pub me_level: MeTelemetryLevel,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for TelemetryPolicy {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
core_enabled: true,
|
||||||
|
user_enabled: true,
|
||||||
|
me_level: MeTelemetryLevel::Normal,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TelemetryPolicy {
|
||||||
|
pub fn from_config(cfg: &TelemetryConfig) -> Self {
|
||||||
|
Self {
|
||||||
|
core_enabled: cfg.core_enabled,
|
||||||
|
user_enabled: cfg.user_enabled,
|
||||||
|
me_level: cfg.me_level,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -336,22 +336,35 @@ impl PendingCiphertext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn remaining_capacity(&self) -> usize {
|
fn remaining_capacity(&self) -> usize {
|
||||||
self.max_len.saturating_sub(self.buf.len())
|
self.max_len.saturating_sub(self.pending_len())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn compact_consumed_prefix(&mut self) {
|
||||||
|
if self.pos == 0 {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.pos >= self.buf.len() {
|
||||||
|
self.buf.clear();
|
||||||
|
self.pos = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let _ = self.buf.split_to(self.pos);
|
||||||
|
self.pos = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn advance(&mut self, n: usize) {
|
fn advance(&mut self, n: usize) {
|
||||||
self.pos = (self.pos + n).min(self.buf.len());
|
self.pos = (self.pos + n).min(self.buf.len());
|
||||||
|
|
||||||
if self.pos == self.buf.len() {
|
if self.pos == self.buf.len() {
|
||||||
self.buf.clear();
|
self.compact_consumed_prefix();
|
||||||
self.pos = 0;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compact when a large prefix was consumed.
|
// Compact when a large prefix was consumed.
|
||||||
if self.pos >= 16 * 1024 {
|
if self.pos >= 16 * 1024 {
|
||||||
let _ = self.buf.split_to(self.pos);
|
self.compact_consumed_prefix();
|
||||||
self.pos = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -379,6 +392,11 @@ impl PendingCiphertext {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reclaim consumed prefix when physical storage is the only limiter.
|
||||||
|
if self.pos > 0 && self.buf.len() + plaintext.len() > self.max_len {
|
||||||
|
self.compact_consumed_prefix();
|
||||||
|
}
|
||||||
|
|
||||||
let start = self.buf.len();
|
let start = self.buf.len();
|
||||||
self.buf.reserve(plaintext.len());
|
self.buf.reserve(plaintext.len());
|
||||||
self.buf.extend_from_slice(plaintext);
|
self.buf.extend_from_slice(plaintext);
|
||||||
@@ -777,3 +795,70 @@ impl<S: AsyncWrite + Unpin> AsyncWrite for PassthroughStream<S> {
|
|||||||
Pin::new(&mut self.inner).poll_shutdown(cx)
|
Pin::new(&mut self.inner).poll_shutdown(cx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
fn test_ctr() -> AesCtr {
|
||||||
|
AesCtr::new(&[0x11; 32], 0x0102_0304_0506_0708_1112_1314_1516_1718)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn pending_capacity_reclaims_after_partial_advance_without_compaction_threshold() {
|
||||||
|
let mut pending = PendingCiphertext::new(1024);
|
||||||
|
let mut ctr = test_ctr();
|
||||||
|
let payload = vec![0x41; 900];
|
||||||
|
pending.push_encrypted(&mut ctr, &payload).unwrap();
|
||||||
|
|
||||||
|
// Keep position below compaction threshold to validate logical-capacity accounting.
|
||||||
|
pending.advance(800);
|
||||||
|
assert_eq!(pending.pending_len(), 100);
|
||||||
|
assert_eq!(pending.remaining_capacity(), 924);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn push_encrypted_respects_pending_limit() {
|
||||||
|
let mut pending = PendingCiphertext::new(64);
|
||||||
|
let mut ctr = test_ctr();
|
||||||
|
|
||||||
|
pending.push_encrypted(&mut ctr, &[0x10; 64]).unwrap();
|
||||||
|
let err = pending.push_encrypted(&mut ctr, &[0x20]).unwrap_err();
|
||||||
|
assert_eq!(err.kind(), ErrorKind::WouldBlock);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn push_encrypted_compacts_prefix_when_physical_buffer_would_overflow() {
|
||||||
|
let mut pending = PendingCiphertext::new(64);
|
||||||
|
let mut ctr = test_ctr();
|
||||||
|
|
||||||
|
pending.push_encrypted(&mut ctr, &[0x22; 60]).unwrap();
|
||||||
|
pending.advance(30);
|
||||||
|
pending.push_encrypted(&mut ctr, &[0x33; 30]).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(pending.pending_len(), 60);
|
||||||
|
assert!(pending.buf.len() <= 64);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn pending_ciphertext_preserves_stream_order_across_drain_and_append() {
|
||||||
|
let mut pending = PendingCiphertext::new(128);
|
||||||
|
let mut ctr = test_ctr();
|
||||||
|
|
||||||
|
let first = vec![0xA1; 80];
|
||||||
|
let second = vec![0xB2; 40];
|
||||||
|
|
||||||
|
pending.push_encrypted(&mut ctr, &first).unwrap();
|
||||||
|
pending.advance(50);
|
||||||
|
pending.push_encrypted(&mut ctr, &second).unwrap();
|
||||||
|
|
||||||
|
let mut baseline_ctr = test_ctr();
|
||||||
|
let mut baseline_plain = Vec::with_capacity(first.len() + second.len());
|
||||||
|
baseline_plain.extend_from_slice(&first);
|
||||||
|
baseline_plain.extend_from_slice(&second);
|
||||||
|
baseline_ctr.apply(&mut baseline_plain);
|
||||||
|
|
||||||
|
let expected = &baseline_plain[50..];
|
||||||
|
assert_eq!(pending.pending_slice(), expected);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,8 +2,10 @@ use std::sync::Arc;
|
|||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use anyhow::{Result, anyhow};
|
use anyhow::{Result, anyhow};
|
||||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
|
||||||
use tokio::net::TcpStream;
|
use tokio::net::TcpStream;
|
||||||
|
#[cfg(unix)]
|
||||||
|
use tokio::net::UnixStream;
|
||||||
use tokio::time::timeout;
|
use tokio::time::timeout;
|
||||||
use tokio_rustls::client::TlsStream;
|
use tokio_rustls::client::TlsStream;
|
||||||
use tokio_rustls::TlsConnector;
|
use tokio_rustls::TlsConnector;
|
||||||
@@ -18,7 +20,9 @@ use x509_parser::prelude::FromDer;
|
|||||||
use x509_parser::certificate::X509Certificate;
|
use x509_parser::certificate::X509Certificate;
|
||||||
|
|
||||||
use crate::crypto::SecureRandom;
|
use crate::crypto::SecureRandom;
|
||||||
|
use crate::network::dns_overrides::resolve_socket_addr;
|
||||||
use crate::protocol::constants::{TLS_RECORD_APPLICATION, TLS_RECORD_HANDSHAKE};
|
use crate::protocol::constants::{TLS_RECORD_APPLICATION, TLS_RECORD_HANDSHAKE};
|
||||||
|
use crate::transport::proxy_protocol::{ProxyProtocolV1Builder, ProxyProtocolV2Builder};
|
||||||
use crate::tls_front::types::{
|
use crate::tls_front::types::{
|
||||||
ParsedCertificateInfo,
|
ParsedCertificateInfo,
|
||||||
ParsedServerHello,
|
ParsedServerHello,
|
||||||
@@ -210,7 +214,10 @@ fn gen_key_share(rng: &SecureRandom) -> [u8; 32] {
|
|||||||
key
|
key
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn read_tls_record(stream: &mut TcpStream) -> Result<(u8, Vec<u8>)> {
|
async fn read_tls_record<S>(stream: &mut S) -> Result<(u8, Vec<u8>)>
|
||||||
|
where
|
||||||
|
S: AsyncRead + Unpin,
|
||||||
|
{
|
||||||
let mut header = [0u8; 5];
|
let mut header = [0u8; 5];
|
||||||
stream.read_exact(&mut header).await?;
|
stream.read_exact(&mut header).await?;
|
||||||
let len = u16::from_be_bytes([header[3], header[4]]) as usize;
|
let len = u16::from_be_bytes([header[3], header[4]]) as usize;
|
||||||
@@ -332,6 +339,55 @@ fn u24_bytes(value: usize) -> Option<[u8; 3]> {
|
|||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn connect_with_dns_override(
|
||||||
|
host: &str,
|
||||||
|
port: u16,
|
||||||
|
connect_timeout: Duration,
|
||||||
|
) -> Result<TcpStream> {
|
||||||
|
if let Some(addr) = resolve_socket_addr(host, port) {
|
||||||
|
return Ok(timeout(connect_timeout, TcpStream::connect(addr)).await??);
|
||||||
|
}
|
||||||
|
Ok(timeout(connect_timeout, TcpStream::connect((host, port))).await??)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn connect_tcp_with_upstream(
|
||||||
|
host: &str,
|
||||||
|
port: u16,
|
||||||
|
connect_timeout: Duration,
|
||||||
|
upstream: Option<std::sync::Arc<crate::transport::UpstreamManager>>,
|
||||||
|
) -> Result<TcpStream> {
|
||||||
|
if let Some(manager) = upstream {
|
||||||
|
if let Some(addr) = resolve_socket_addr(host, port) {
|
||||||
|
match manager.connect(addr, None, None).await {
|
||||||
|
Ok(stream) => return Ok(stream),
|
||||||
|
Err(e) => {
|
||||||
|
warn!(
|
||||||
|
host = %host,
|
||||||
|
port = port,
|
||||||
|
error = %e,
|
||||||
|
"Upstream connect failed, using direct connect"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if let Ok(mut addrs) = tokio::net::lookup_host((host, port)).await {
|
||||||
|
if let Some(addr) = addrs.find(|a| a.is_ipv4()) {
|
||||||
|
match manager.connect(addr, None, None).await {
|
||||||
|
Ok(stream) => return Ok(stream),
|
||||||
|
Err(e) => {
|
||||||
|
warn!(
|
||||||
|
host = %host,
|
||||||
|
port = port,
|
||||||
|
error = %e,
|
||||||
|
"Upstream connect failed, using direct connect"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
connect_with_dns_override(host, port, connect_timeout).await
|
||||||
|
}
|
||||||
|
|
||||||
fn encode_tls13_certificate_message(cert_chain_der: &[Vec<u8>]) -> Option<Vec<u8>> {
|
fn encode_tls13_certificate_message(cert_chain_der: &[Vec<u8>]) -> Option<Vec<u8>> {
|
||||||
if cert_chain_der.is_empty() {
|
if cert_chain_der.is_empty() {
|
||||||
return None;
|
return None;
|
||||||
@@ -361,18 +417,25 @@ fn encode_tls13_certificate_message(cert_chain_der: &[Vec<u8>]) -> Option<Vec<u8
|
|||||||
Some(message)
|
Some(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn fetch_via_raw_tls(
|
async fn fetch_via_raw_tls_stream<S>(
|
||||||
host: &str,
|
mut stream: S,
|
||||||
port: u16,
|
|
||||||
sni: &str,
|
sni: &str,
|
||||||
connect_timeout: Duration,
|
connect_timeout: Duration,
|
||||||
) -> Result<TlsFetchResult> {
|
proxy_protocol: u8,
|
||||||
let addr = format!("{host}:{port}");
|
) -> Result<TlsFetchResult>
|
||||||
let mut stream = timeout(connect_timeout, TcpStream::connect(addr)).await??;
|
where
|
||||||
|
S: AsyncRead + AsyncWrite + Unpin,
|
||||||
|
{
|
||||||
let rng = SecureRandom::new();
|
let rng = SecureRandom::new();
|
||||||
let client_hello = build_client_hello(sni, &rng);
|
let client_hello = build_client_hello(sni, &rng);
|
||||||
timeout(connect_timeout, async {
|
timeout(connect_timeout, async {
|
||||||
|
if proxy_protocol > 0 {
|
||||||
|
let header = match proxy_protocol {
|
||||||
|
2 => ProxyProtocolV2Builder::new().build(),
|
||||||
|
_ => ProxyProtocolV1Builder::new().build(),
|
||||||
|
};
|
||||||
|
stream.write_all(&header).await?;
|
||||||
|
}
|
||||||
stream.write_all(&client_hello).await?;
|
stream.write_all(&client_hello).await?;
|
||||||
stream.flush().await?;
|
stream.flush().await?;
|
||||||
Ok::<(), std::io::Error>(())
|
Ok::<(), std::io::Error>(())
|
||||||
@@ -418,34 +481,69 @@ async fn fetch_via_raw_tls(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn fetch_via_rustls(
|
async fn fetch_via_raw_tls(
|
||||||
host: &str,
|
host: &str,
|
||||||
port: u16,
|
port: u16,
|
||||||
sni: &str,
|
sni: &str,
|
||||||
connect_timeout: Duration,
|
connect_timeout: Duration,
|
||||||
upstream: Option<std::sync::Arc<crate::transport::UpstreamManager>>,
|
upstream: Option<std::sync::Arc<crate::transport::UpstreamManager>>,
|
||||||
|
proxy_protocol: u8,
|
||||||
|
unix_sock: Option<&str>,
|
||||||
) -> Result<TlsFetchResult> {
|
) -> Result<TlsFetchResult> {
|
||||||
// rustls handshake path for certificate and basic negotiated metadata.
|
#[cfg(unix)]
|
||||||
let stream = if let Some(manager) = upstream {
|
if let Some(sock_path) = unix_sock {
|
||||||
// Resolve host to SocketAddr
|
match timeout(connect_timeout, UnixStream::connect(sock_path)).await {
|
||||||
if let Ok(mut addrs) = tokio::net::lookup_host((host, port)).await {
|
Ok(Ok(stream)) => {
|
||||||
if let Some(addr) = addrs.find(|a| a.is_ipv4()) {
|
debug!(
|
||||||
match manager.connect(addr, None, None).await {
|
sni = %sni,
|
||||||
Ok(s) => s,
|
sock = %sock_path,
|
||||||
Err(e) => {
|
"Raw TLS fetch using mask unix socket"
|
||||||
warn!(sni = %sni, error = %e, "Upstream connect failed, using direct connect");
|
);
|
||||||
timeout(connect_timeout, TcpStream::connect((host, port))).await??
|
return fetch_via_raw_tls_stream(stream, sni, connect_timeout, proxy_protocol).await;
|
||||||
}
|
}
|
||||||
}
|
Ok(Err(e)) => {
|
||||||
} else {
|
warn!(
|
||||||
timeout(connect_timeout, TcpStream::connect((host, port))).await??
|
sni = %sni,
|
||||||
|
sock = %sock_path,
|
||||||
|
error = %e,
|
||||||
|
"Raw TLS unix socket connect failed, falling back to TCP"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
warn!(
|
||||||
|
sni = %sni,
|
||||||
|
sock = %sock_path,
|
||||||
|
"Raw TLS unix socket connect timed out, falling back to TCP"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
timeout(connect_timeout, TcpStream::connect((host, port))).await??
|
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
timeout(connect_timeout, TcpStream::connect((host, port))).await??
|
|
||||||
};
|
#[cfg(not(unix))]
|
||||||
|
let _ = unix_sock;
|
||||||
|
|
||||||
|
let stream = connect_tcp_with_upstream(host, port, connect_timeout, upstream).await?;
|
||||||
|
fetch_via_raw_tls_stream(stream, sni, connect_timeout, proxy_protocol).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn fetch_via_rustls_stream<S>(
|
||||||
|
mut stream: S,
|
||||||
|
host: &str,
|
||||||
|
sni: &str,
|
||||||
|
proxy_protocol: u8,
|
||||||
|
) -> Result<TlsFetchResult>
|
||||||
|
where
|
||||||
|
S: AsyncRead + AsyncWrite + Unpin,
|
||||||
|
{
|
||||||
|
// rustls handshake path for certificate and basic negotiated metadata.
|
||||||
|
if proxy_protocol > 0 {
|
||||||
|
let header = match proxy_protocol {
|
||||||
|
2 => ProxyProtocolV2Builder::new().build(),
|
||||||
|
_ => ProxyProtocolV1Builder::new().build(),
|
||||||
|
};
|
||||||
|
stream.write_all(&header).await?;
|
||||||
|
stream.flush().await?;
|
||||||
|
}
|
||||||
|
|
||||||
let config = build_client_config();
|
let config = build_client_config();
|
||||||
let connector = TlsConnector::from(config);
|
let connector = TlsConnector::from(config);
|
||||||
@@ -454,7 +552,7 @@ async fn fetch_via_rustls(
|
|||||||
.or_else(|_| ServerName::try_from(host.to_owned()))
|
.or_else(|_| ServerName::try_from(host.to_owned()))
|
||||||
.map_err(|_| RustlsError::General("invalid SNI".into()))?;
|
.map_err(|_| RustlsError::General("invalid SNI".into()))?;
|
||||||
|
|
||||||
let tls_stream: TlsStream<TcpStream> = connector.connect(server_name, stream).await?;
|
let tls_stream: TlsStream<S> = connector.connect(server_name, stream).await?;
|
||||||
|
|
||||||
// Extract negotiated parameters and certificates
|
// Extract negotiated parameters and certificates
|
||||||
let (_io, session) = tls_stream.get_ref();
|
let (_io, session) = tls_stream.get_ref();
|
||||||
@@ -515,6 +613,51 @@ async fn fetch_via_rustls(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn fetch_via_rustls(
|
||||||
|
host: &str,
|
||||||
|
port: u16,
|
||||||
|
sni: &str,
|
||||||
|
connect_timeout: Duration,
|
||||||
|
upstream: Option<std::sync::Arc<crate::transport::UpstreamManager>>,
|
||||||
|
proxy_protocol: u8,
|
||||||
|
unix_sock: Option<&str>,
|
||||||
|
) -> Result<TlsFetchResult> {
|
||||||
|
#[cfg(unix)]
|
||||||
|
if let Some(sock_path) = unix_sock {
|
||||||
|
match timeout(connect_timeout, UnixStream::connect(sock_path)).await {
|
||||||
|
Ok(Ok(stream)) => {
|
||||||
|
debug!(
|
||||||
|
sni = %sni,
|
||||||
|
sock = %sock_path,
|
||||||
|
"Rustls fetch using mask unix socket"
|
||||||
|
);
|
||||||
|
return fetch_via_rustls_stream(stream, host, sni, proxy_protocol).await;
|
||||||
|
}
|
||||||
|
Ok(Err(e)) => {
|
||||||
|
warn!(
|
||||||
|
sni = %sni,
|
||||||
|
sock = %sock_path,
|
||||||
|
error = %e,
|
||||||
|
"Rustls unix socket connect failed, falling back to TCP"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
warn!(
|
||||||
|
sni = %sni,
|
||||||
|
sock = %sock_path,
|
||||||
|
"Rustls unix socket connect timed out, falling back to TCP"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(unix))]
|
||||||
|
let _ = unix_sock;
|
||||||
|
|
||||||
|
let stream = connect_tcp_with_upstream(host, port, connect_timeout, upstream).await?;
|
||||||
|
fetch_via_rustls_stream(stream, host, sni, proxy_protocol).await
|
||||||
|
}
|
||||||
|
|
||||||
/// Fetch real TLS metadata for the given SNI.
|
/// Fetch real TLS metadata for the given SNI.
|
||||||
///
|
///
|
||||||
/// Strategy:
|
/// Strategy:
|
||||||
@@ -527,8 +670,20 @@ pub async fn fetch_real_tls(
|
|||||||
sni: &str,
|
sni: &str,
|
||||||
connect_timeout: Duration,
|
connect_timeout: Duration,
|
||||||
upstream: Option<std::sync::Arc<crate::transport::UpstreamManager>>,
|
upstream: Option<std::sync::Arc<crate::transport::UpstreamManager>>,
|
||||||
|
proxy_protocol: u8,
|
||||||
|
unix_sock: Option<&str>,
|
||||||
) -> Result<TlsFetchResult> {
|
) -> Result<TlsFetchResult> {
|
||||||
let raw_result = match fetch_via_raw_tls(host, port, sni, connect_timeout).await {
|
let raw_result = match fetch_via_raw_tls(
|
||||||
|
host,
|
||||||
|
port,
|
||||||
|
sni,
|
||||||
|
connect_timeout,
|
||||||
|
upstream.clone(),
|
||||||
|
proxy_protocol,
|
||||||
|
unix_sock,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
Ok(res) => Some(res),
|
Ok(res) => Some(res),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
warn!(sni = %sni, error = %e, "Raw TLS fetch failed");
|
warn!(sni = %sni, error = %e, "Raw TLS fetch failed");
|
||||||
@@ -536,7 +691,17 @@ pub async fn fetch_real_tls(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
match fetch_via_rustls(host, port, sni, connect_timeout, upstream).await {
|
match fetch_via_rustls(
|
||||||
|
host,
|
||||||
|
port,
|
||||||
|
sni,
|
||||||
|
connect_timeout,
|
||||||
|
upstream,
|
||||||
|
proxy_protocol,
|
||||||
|
unix_sock,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
Ok(rustls_result) => {
|
Ok(rustls_result) => {
|
||||||
if let Some(mut raw) = raw_result {
|
if let Some(mut raw) = raw_result {
|
||||||
raw.cert_info = rustls_result.cert_info;
|
raw.cert_info = rustls_result.cert_info;
|
||||||
|
|||||||
@@ -5,15 +5,15 @@ use std::sync::Arc;
|
|||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use httpdate;
|
use httpdate;
|
||||||
use tokio::sync::watch;
|
use tokio::sync::{mpsc, watch};
|
||||||
use tracing::{debug, info, warn};
|
use tracing::{debug, info, warn};
|
||||||
|
|
||||||
use crate::config::ProxyConfig;
|
use crate::config::ProxyConfig;
|
||||||
use crate::error::Result;
|
use crate::error::Result;
|
||||||
|
|
||||||
use super::MePool;
|
use super::MePool;
|
||||||
|
use super::rotation::{MeReinitTrigger, enqueue_reinit_trigger};
|
||||||
use super::secret::download_proxy_secret_with_max_len;
|
use super::secret::download_proxy_secret_with_max_len;
|
||||||
use crate::crypto::SecureRandom;
|
|
||||||
use std::time::SystemTime;
|
use std::time::SystemTime;
|
||||||
|
|
||||||
async fn retry_fetch(url: &str) -> Option<ProxyConfigData> {
|
async fn retry_fetch(url: &str) -> Option<ProxyConfigData> {
|
||||||
@@ -38,6 +38,8 @@ async fn retry_fetch(url: &str) -> Option<ProxyConfigData> {
|
|||||||
pub struct ProxyConfigData {
|
pub struct ProxyConfigData {
|
||||||
pub map: HashMap<i32, Vec<(IpAddr, u16)>>,
|
pub map: HashMap<i32, Vec<(IpAddr, u16)>>,
|
||||||
pub default_dc: Option<i32>,
|
pub default_dc: Option<i32>,
|
||||||
|
pub http_status: u16,
|
||||||
|
pub proxy_for_lines: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
@@ -172,6 +174,7 @@ pub async fn fetch_proxy_config(url: &str) -> Result<ProxyConfigData> {
|
|||||||
.await
|
.await
|
||||||
.map_err(|e| crate::error::ProxyError::Proxy(format!("fetch_proxy_config GET failed: {e}")))?
|
.map_err(|e| crate::error::ProxyError::Proxy(format!("fetch_proxy_config GET failed: {e}")))?
|
||||||
;
|
;
|
||||||
|
let http_status = resp.status().as_u16();
|
||||||
|
|
||||||
if let Some(date) = resp.headers().get(reqwest::header::DATE)
|
if let Some(date) = resp.headers().get(reqwest::header::DATE)
|
||||||
&& let Ok(date_str) = date.to_str()
|
&& let Ok(date_str) = date.to_str()
|
||||||
@@ -194,9 +197,11 @@ pub async fn fetch_proxy_config(url: &str) -> Result<ProxyConfigData> {
|
|||||||
.map_err(|e| crate::error::ProxyError::Proxy(format!("fetch_proxy_config read failed: {e}")))?;
|
.map_err(|e| crate::error::ProxyError::Proxy(format!("fetch_proxy_config read failed: {e}")))?;
|
||||||
|
|
||||||
let mut map: HashMap<i32, Vec<(IpAddr, u16)>> = HashMap::new();
|
let mut map: HashMap<i32, Vec<(IpAddr, u16)>> = HashMap::new();
|
||||||
|
let mut proxy_for_lines: u32 = 0;
|
||||||
for line in text.lines() {
|
for line in text.lines() {
|
||||||
if let Some((dc, ip, port)) = parse_proxy_line(line) {
|
if let Some((dc, ip, port)) = parse_proxy_line(line) {
|
||||||
map.entry(dc).or_default().push((ip, port));
|
map.entry(dc).or_default().push((ip, port));
|
||||||
|
proxy_for_lines = proxy_for_lines.saturating_add(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -214,14 +219,49 @@ pub async fn fetch_proxy_config(url: &str) -> Result<ProxyConfigData> {
|
|||||||
None
|
None
|
||||||
});
|
});
|
||||||
|
|
||||||
Ok(ProxyConfigData { map, default_dc })
|
Ok(ProxyConfigData {
|
||||||
|
map,
|
||||||
|
default_dc,
|
||||||
|
http_status,
|
||||||
|
proxy_for_lines,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn snapshot_passes_guards(
|
||||||
|
cfg: &ProxyConfig,
|
||||||
|
snapshot: &ProxyConfigData,
|
||||||
|
snapshot_name: &'static str,
|
||||||
|
) -> bool {
|
||||||
|
if cfg.general.me_snapshot_require_http_2xx
|
||||||
|
&& !(200..=299).contains(&snapshot.http_status)
|
||||||
|
{
|
||||||
|
warn!(
|
||||||
|
snapshot = snapshot_name,
|
||||||
|
http_status = snapshot.http_status,
|
||||||
|
"ME snapshot rejected by non-2xx HTTP status"
|
||||||
|
);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
let min_proxy_for = cfg.general.me_snapshot_min_proxy_for_lines;
|
||||||
|
if snapshot.proxy_for_lines < min_proxy_for {
|
||||||
|
warn!(
|
||||||
|
snapshot = snapshot_name,
|
||||||
|
parsed_proxy_for_lines = snapshot.proxy_for_lines,
|
||||||
|
min_proxy_for_lines = min_proxy_for,
|
||||||
|
"ME snapshot rejected by proxy_for line floor"
|
||||||
|
);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn run_update_cycle(
|
async fn run_update_cycle(
|
||||||
pool: &Arc<MePool>,
|
pool: &Arc<MePool>,
|
||||||
rng: &Arc<SecureRandom>,
|
|
||||||
cfg: &ProxyConfig,
|
cfg: &ProxyConfig,
|
||||||
state: &mut UpdaterState,
|
state: &mut UpdaterState,
|
||||||
|
reinit_tx: &mpsc::Sender<MeReinitTrigger>,
|
||||||
) {
|
) {
|
||||||
pool.update_runtime_reinit_policy(
|
pool.update_runtime_reinit_policy(
|
||||||
cfg.general.hardswap,
|
cfg.general.hardswap,
|
||||||
@@ -232,6 +272,20 @@ async fn run_update_cycle(
|
|||||||
cfg.general.me_hardswap_warmup_delay_max_ms,
|
cfg.general.me_hardswap_warmup_delay_max_ms,
|
||||||
cfg.general.me_hardswap_warmup_extra_passes,
|
cfg.general.me_hardswap_warmup_extra_passes,
|
||||||
cfg.general.me_hardswap_warmup_pass_backoff_base_ms,
|
cfg.general.me_hardswap_warmup_pass_backoff_base_ms,
|
||||||
|
cfg.general.me_bind_stale_mode,
|
||||||
|
cfg.general.me_bind_stale_ttl_secs,
|
||||||
|
cfg.general.me_secret_atomic_snapshot,
|
||||||
|
cfg.general.me_deterministic_writer_sort,
|
||||||
|
cfg.general.me_single_endpoint_shadow_writers,
|
||||||
|
cfg.general.me_single_endpoint_outage_mode_enabled,
|
||||||
|
cfg.general.me_single_endpoint_outage_disable_quarantine,
|
||||||
|
cfg.general.me_single_endpoint_outage_backoff_min_ms,
|
||||||
|
cfg.general.me_single_endpoint_outage_backoff_max_ms,
|
||||||
|
cfg.general.me_single_endpoint_shadow_rotate_every_secs,
|
||||||
|
cfg.general.me_floor_mode,
|
||||||
|
cfg.general.me_adaptive_floor_idle_secs,
|
||||||
|
cfg.general.me_adaptive_floor_min_writers_single_endpoint,
|
||||||
|
cfg.general.me_adaptive_floor_recover_grace_secs,
|
||||||
);
|
);
|
||||||
|
|
||||||
let required_cfg_snapshots = cfg.general.me_config_stable_snapshots.max(1);
|
let required_cfg_snapshots = cfg.general.me_config_stable_snapshots.max(1);
|
||||||
@@ -242,44 +296,48 @@ async fn run_update_cycle(
|
|||||||
let mut ready_v4: Option<(ProxyConfigData, u64)> = None;
|
let mut ready_v4: Option<(ProxyConfigData, u64)> = None;
|
||||||
let cfg_v4 = retry_fetch("https://core.telegram.org/getProxyConfig").await;
|
let cfg_v4 = retry_fetch("https://core.telegram.org/getProxyConfig").await;
|
||||||
if let Some(cfg_v4) = cfg_v4 {
|
if let Some(cfg_v4) = cfg_v4 {
|
||||||
let cfg_v4_hash = hash_proxy_config(&cfg_v4);
|
if snapshot_passes_guards(cfg, &cfg_v4, "getProxyConfig") {
|
||||||
let stable_hits = state.config_v4.observe(cfg_v4_hash);
|
let cfg_v4_hash = hash_proxy_config(&cfg_v4);
|
||||||
if stable_hits < required_cfg_snapshots {
|
let stable_hits = state.config_v4.observe(cfg_v4_hash);
|
||||||
debug!(
|
if stable_hits < required_cfg_snapshots {
|
||||||
stable_hits,
|
debug!(
|
||||||
required_cfg_snapshots,
|
stable_hits,
|
||||||
snapshot = format_args!("0x{cfg_v4_hash:016x}"),
|
required_cfg_snapshots,
|
||||||
"ME config v4 candidate observed"
|
snapshot = format_args!("0x{cfg_v4_hash:016x}"),
|
||||||
);
|
"ME config v4 candidate observed"
|
||||||
} else if state.config_v4.is_applied(cfg_v4_hash) {
|
);
|
||||||
debug!(
|
} else if state.config_v4.is_applied(cfg_v4_hash) {
|
||||||
snapshot = format_args!("0x{cfg_v4_hash:016x}"),
|
debug!(
|
||||||
"ME config v4 stable snapshot already applied"
|
snapshot = format_args!("0x{cfg_v4_hash:016x}"),
|
||||||
);
|
"ME config v4 stable snapshot already applied"
|
||||||
} else {
|
);
|
||||||
ready_v4 = Some((cfg_v4, cfg_v4_hash));
|
} else {
|
||||||
|
ready_v4 = Some((cfg_v4, cfg_v4_hash));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut ready_v6: Option<(ProxyConfigData, u64)> = None;
|
let mut ready_v6: Option<(ProxyConfigData, u64)> = None;
|
||||||
let cfg_v6 = retry_fetch("https://core.telegram.org/getProxyConfigV6").await;
|
let cfg_v6 = retry_fetch("https://core.telegram.org/getProxyConfigV6").await;
|
||||||
if let Some(cfg_v6) = cfg_v6 {
|
if let Some(cfg_v6) = cfg_v6 {
|
||||||
let cfg_v6_hash = hash_proxy_config(&cfg_v6);
|
if snapshot_passes_guards(cfg, &cfg_v6, "getProxyConfigV6") {
|
||||||
let stable_hits = state.config_v6.observe(cfg_v6_hash);
|
let cfg_v6_hash = hash_proxy_config(&cfg_v6);
|
||||||
if stable_hits < required_cfg_snapshots {
|
let stable_hits = state.config_v6.observe(cfg_v6_hash);
|
||||||
debug!(
|
if stable_hits < required_cfg_snapshots {
|
||||||
stable_hits,
|
debug!(
|
||||||
required_cfg_snapshots,
|
stable_hits,
|
||||||
snapshot = format_args!("0x{cfg_v6_hash:016x}"),
|
required_cfg_snapshots,
|
||||||
"ME config v6 candidate observed"
|
snapshot = format_args!("0x{cfg_v6_hash:016x}"),
|
||||||
);
|
"ME config v6 candidate observed"
|
||||||
} else if state.config_v6.is_applied(cfg_v6_hash) {
|
);
|
||||||
debug!(
|
} else if state.config_v6.is_applied(cfg_v6_hash) {
|
||||||
snapshot = format_args!("0x{cfg_v6_hash:016x}"),
|
debug!(
|
||||||
"ME config v6 stable snapshot already applied"
|
snapshot = format_args!("0x{cfg_v6_hash:016x}"),
|
||||||
);
|
"ME config v6 stable snapshot already applied"
|
||||||
} else {
|
);
|
||||||
ready_v6 = Some((cfg_v6, cfg_v6_hash));
|
} else {
|
||||||
|
ready_v6 = Some((cfg_v6, cfg_v6_hash));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -292,28 +350,40 @@ async fn run_update_cycle(
|
|||||||
let update_v6 = ready_v6
|
let update_v6 = ready_v6
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|(snapshot, _)| snapshot.map.clone());
|
.map(|(snapshot, _)| snapshot.map.clone());
|
||||||
|
let update_is_empty =
|
||||||
let changed = pool.update_proxy_maps(update_v4, update_v6).await;
|
update_v4.is_empty() && update_v6.as_ref().is_none_or(|v| v.is_empty());
|
||||||
|
let apply_outcome = if update_is_empty && !cfg.general.me_snapshot_reject_empty_map {
|
||||||
if let Some((snapshot, hash)) = ready_v4 {
|
super::pool_config::SnapshotApplyOutcome::AppliedNoDelta
|
||||||
if let Some(dc) = snapshot.default_dc {
|
|
||||||
pool.default_dc
|
|
||||||
.store(dc, std::sync::atomic::Ordering::Relaxed);
|
|
||||||
}
|
|
||||||
state.config_v4.mark_applied(hash);
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some((_snapshot, hash)) = ready_v6 {
|
|
||||||
state.config_v6.mark_applied(hash);
|
|
||||||
}
|
|
||||||
|
|
||||||
state.last_map_apply_at = Some(tokio::time::Instant::now());
|
|
||||||
|
|
||||||
if changed {
|
|
||||||
maps_changed = true;
|
|
||||||
info!("ME config update applied after stable-gate");
|
|
||||||
} else {
|
} else {
|
||||||
debug!("ME config stable-gate applied with no map delta");
|
pool.update_proxy_maps(update_v4, update_v6).await
|
||||||
|
};
|
||||||
|
|
||||||
|
if matches!(
|
||||||
|
apply_outcome,
|
||||||
|
super::pool_config::SnapshotApplyOutcome::RejectedEmpty
|
||||||
|
) {
|
||||||
|
warn!("ME config stable snapshot rejected (empty endpoint map)");
|
||||||
|
} else {
|
||||||
|
if let Some((snapshot, hash)) = ready_v4 {
|
||||||
|
if let Some(dc) = snapshot.default_dc {
|
||||||
|
pool.default_dc
|
||||||
|
.store(dc, std::sync::atomic::Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
state.config_v4.mark_applied(hash);
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some((_snapshot, hash)) = ready_v6 {
|
||||||
|
state.config_v6.mark_applied(hash);
|
||||||
|
}
|
||||||
|
|
||||||
|
state.last_map_apply_at = Some(tokio::time::Instant::now());
|
||||||
|
|
||||||
|
if apply_outcome.changed() {
|
||||||
|
maps_changed = true;
|
||||||
|
info!("ME config update applied after stable-gate");
|
||||||
|
} else {
|
||||||
|
debug!("ME config stable-gate applied with no map delta");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if let Some(last) = state.last_map_apply_at {
|
} else if let Some(last) = state.last_map_apply_at {
|
||||||
let wait_secs = map_apply_cooldown_remaining_secs(last, apply_cooldown);
|
let wait_secs = map_apply_cooldown_remaining_secs(last, apply_cooldown);
|
||||||
@@ -325,8 +395,7 @@ async fn run_update_cycle(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if maps_changed {
|
if maps_changed {
|
||||||
pool.zero_downtime_reinit_after_map_change(rng.as_ref())
|
enqueue_reinit_trigger(reinit_tx, MeReinitTrigger::MapChanged);
|
||||||
.await;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pool.reset_stun_state();
|
pool.reset_stun_state();
|
||||||
@@ -367,8 +436,8 @@ async fn run_update_cycle(
|
|||||||
|
|
||||||
pub async fn me_config_updater(
|
pub async fn me_config_updater(
|
||||||
pool: Arc<MePool>,
|
pool: Arc<MePool>,
|
||||||
rng: Arc<SecureRandom>,
|
|
||||||
mut config_rx: watch::Receiver<Arc<ProxyConfig>>,
|
mut config_rx: watch::Receiver<Arc<ProxyConfig>>,
|
||||||
|
reinit_tx: mpsc::Sender<MeReinitTrigger>,
|
||||||
) {
|
) {
|
||||||
let mut state = UpdaterState::default();
|
let mut state = UpdaterState::default();
|
||||||
let mut update_every_secs = config_rx
|
let mut update_every_secs = config_rx
|
||||||
@@ -387,7 +456,7 @@ pub async fn me_config_updater(
|
|||||||
tokio::select! {
|
tokio::select! {
|
||||||
_ = &mut sleep => {
|
_ = &mut sleep => {
|
||||||
let cfg = config_rx.borrow().clone();
|
let cfg = config_rx.borrow().clone();
|
||||||
run_update_cycle(&pool, &rng, cfg.as_ref(), &mut state).await;
|
run_update_cycle(&pool, cfg.as_ref(), &mut state, &reinit_tx).await;
|
||||||
let refreshed_secs = cfg.general.effective_update_every_secs().max(1);
|
let refreshed_secs = cfg.general.effective_update_every_secs().max(1);
|
||||||
if refreshed_secs != update_every_secs {
|
if refreshed_secs != update_every_secs {
|
||||||
info!(
|
info!(
|
||||||
@@ -415,6 +484,20 @@ pub async fn me_config_updater(
|
|||||||
cfg.general.me_hardswap_warmup_delay_max_ms,
|
cfg.general.me_hardswap_warmup_delay_max_ms,
|
||||||
cfg.general.me_hardswap_warmup_extra_passes,
|
cfg.general.me_hardswap_warmup_extra_passes,
|
||||||
cfg.general.me_hardswap_warmup_pass_backoff_base_ms,
|
cfg.general.me_hardswap_warmup_pass_backoff_base_ms,
|
||||||
|
cfg.general.me_bind_stale_mode,
|
||||||
|
cfg.general.me_bind_stale_ttl_secs,
|
||||||
|
cfg.general.me_secret_atomic_snapshot,
|
||||||
|
cfg.general.me_deterministic_writer_sort,
|
||||||
|
cfg.general.me_single_endpoint_shadow_writers,
|
||||||
|
cfg.general.me_single_endpoint_outage_mode_enabled,
|
||||||
|
cfg.general.me_single_endpoint_outage_disable_quarantine,
|
||||||
|
cfg.general.me_single_endpoint_outage_backoff_min_ms,
|
||||||
|
cfg.general.me_single_endpoint_outage_backoff_max_ms,
|
||||||
|
cfg.general.me_single_endpoint_shadow_rotate_every_secs,
|
||||||
|
cfg.general.me_floor_mode,
|
||||||
|
cfg.general.me_adaptive_floor_idle_secs,
|
||||||
|
cfg.general.me_adaptive_floor_min_writers_single_endpoint,
|
||||||
|
cfg.general.me_adaptive_floor_recover_grace_secs,
|
||||||
);
|
);
|
||||||
let new_secs = cfg.general.effective_update_every_secs().max(1);
|
let new_secs = cfg.general.effective_update_every_secs().max(1);
|
||||||
if new_secs == update_every_secs {
|
if new_secs == update_every_secs {
|
||||||
@@ -429,7 +512,7 @@ pub async fn me_config_updater(
|
|||||||
);
|
);
|
||||||
update_every_secs = new_secs;
|
update_every_secs = new_secs;
|
||||||
update_every = Duration::from_secs(update_every_secs);
|
update_every = Duration::from_secs(update_every_secs);
|
||||||
run_update_cycle(&pool, &rng, cfg.as_ref(), &mut state).await;
|
run_update_cycle(&pool, cfg.as_ref(), &mut state, &reinit_tx).await;
|
||||||
next_tick = tokio::time::Instant::now() + update_every;
|
next_tick = tokio::time::Instant::now() + update_every;
|
||||||
} else {
|
} else {
|
||||||
info!(
|
info!(
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
use std::net::{IpAddr, SocketAddr};
|
use std::net::{IpAddr, SocketAddr};
|
||||||
|
use std::sync::atomic::Ordering;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
use std::collections::hash_map::DefaultHasher;
|
||||||
|
use std::hash::{Hash, Hasher};
|
||||||
use socket2::{SockRef, TcpKeepalive};
|
use socket2::{SockRef, TcpKeepalive};
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
use libc;
|
use libc;
|
||||||
@@ -14,13 +17,16 @@ use tokio::net::{TcpStream, TcpSocket};
|
|||||||
use tokio::time::timeout;
|
use tokio::time::timeout;
|
||||||
use tracing::{debug, info, warn};
|
use tracing::{debug, info, warn};
|
||||||
|
|
||||||
|
use crate::config::MeSocksKdfPolicy;
|
||||||
use crate::crypto::{SecureRandom, build_middleproxy_prekey, derive_middleproxy_keys, sha256};
|
use crate::crypto::{SecureRandom, build_middleproxy_prekey, derive_middleproxy_keys, sha256};
|
||||||
use crate::error::{ProxyError, Result};
|
use crate::error::{ProxyError, Result};
|
||||||
use crate::network::IpFamily;
|
use crate::network::IpFamily;
|
||||||
|
use crate::network::probe::is_bogon;
|
||||||
use crate::protocol::constants::{
|
use crate::protocol::constants::{
|
||||||
ME_CONNECT_TIMEOUT_SECS, ME_HANDSHAKE_TIMEOUT_SECS, RPC_CRYPTO_AES_U32,
|
ME_CONNECT_TIMEOUT_SECS, ME_HANDSHAKE_TIMEOUT_SECS, RPC_CRYPTO_AES_U32,
|
||||||
RPC_HANDSHAKE_ERROR_U32, rpc_crypto_flags,
|
RPC_HANDSHAKE_ERROR_U32, rpc_crypto_flags,
|
||||||
};
|
};
|
||||||
|
use crate::transport::{UpstreamEgressInfo, UpstreamRouteKind};
|
||||||
|
|
||||||
use super::codec::{
|
use super::codec::{
|
||||||
RpcChecksumMode, build_handshake_payload, build_nonce_payload, build_rpc_frame,
|
RpcChecksumMode, build_handshake_payload, build_nonce_payload, build_rpc_frame,
|
||||||
@@ -30,6 +36,24 @@ use super::codec::{
|
|||||||
use super::wire::{extract_ip_material, IpMaterial};
|
use super::wire::{extract_ip_material, IpMaterial};
|
||||||
use super::MePool;
|
use super::MePool;
|
||||||
|
|
||||||
|
const ME_KDF_DRIFT_STRICT: bool = false;
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||||
|
enum KdfClientPortSource {
|
||||||
|
LocalSocket = 0,
|
||||||
|
SocksBound = 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl KdfClientPortSource {
|
||||||
|
fn from_socks_bound_port(socks_bound_port: Option<u16>) -> Self {
|
||||||
|
if socks_bound_port.is_some() {
|
||||||
|
Self::SocksBound
|
||||||
|
} else {
|
||||||
|
Self::LocalSocket
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Result of a successful ME handshake with timings.
|
/// Result of a successful ME handshake with timings.
|
||||||
pub(crate) struct HandshakeOutput {
|
pub(crate) struct HandshakeOutput {
|
||||||
pub rd: ReadHalf<TcpStream>,
|
pub rd: ReadHalf<TcpStream>,
|
||||||
@@ -43,33 +67,141 @@ pub(crate) struct HandshakeOutput {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl MePool {
|
impl MePool {
|
||||||
/// TCP connect with timeout + return RTT in milliseconds.
|
fn kdf_material_fingerprint(
|
||||||
pub(crate) async fn connect_tcp(&self, addr: SocketAddr) -> Result<(TcpStream, f64)> {
|
local_ip_nat: IpAddr,
|
||||||
let start = Instant::now();
|
peer_addr_nat: SocketAddr,
|
||||||
let connect_fut = async {
|
reflected_ip: Option<IpAddr>,
|
||||||
if addr.is_ipv6()
|
socks_bound_ip: Option<IpAddr>,
|
||||||
&& let Some(v6) = self.detected_ipv6
|
client_port_source: KdfClientPortSource,
|
||||||
{
|
) -> u64 {
|
||||||
match TcpSocket::new_v6() {
|
let mut hasher = DefaultHasher::new();
|
||||||
Ok(sock) => {
|
local_ip_nat.hash(&mut hasher);
|
||||||
if let Err(e) = sock.bind(SocketAddr::new(IpAddr::V6(v6), 0)) {
|
peer_addr_nat.hash(&mut hasher);
|
||||||
debug!(error = %e, bind_ip = %v6, "ME IPv6 bind failed, falling back to default bind");
|
reflected_ip.hash(&mut hasher);
|
||||||
} else {
|
socks_bound_ip.hash(&mut hasher);
|
||||||
match sock.connect(addr).await {
|
client_port_source.hash(&mut hasher);
|
||||||
Ok(stream) => return Ok(stream),
|
hasher.finish()
|
||||||
Err(e) => debug!(error = %e, target = %addr, "ME IPv6 bound connect failed, retrying default connect"),
|
}
|
||||||
}
|
|
||||||
}
|
async fn resolve_dc_idx_for_endpoint(&self, addr: SocketAddr) -> Option<i16> {
|
||||||
|
if addr.is_ipv4() {
|
||||||
|
let map = self.proxy_map_v4.read().await;
|
||||||
|
for (dc, addrs) in map.iter() {
|
||||||
|
if addrs
|
||||||
|
.iter()
|
||||||
|
.any(|(ip, port)| SocketAddr::new(*ip, *port) == addr)
|
||||||
|
{
|
||||||
|
let abs_dc = dc.abs();
|
||||||
|
if abs_dc > 0
|
||||||
|
&& let Ok(dc_idx) = i16::try_from(abs_dc)
|
||||||
|
{
|
||||||
|
return Some(dc_idx);
|
||||||
}
|
}
|
||||||
Err(e) => debug!(error = %e, "ME IPv6 socket creation failed, falling back to default connect"),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TcpStream::connect(addr).await
|
} else {
|
||||||
|
let map = self.proxy_map_v6.read().await;
|
||||||
|
for (dc, addrs) in map.iter() {
|
||||||
|
if addrs
|
||||||
|
.iter()
|
||||||
|
.any(|(ip, port)| SocketAddr::new(*ip, *port) == addr)
|
||||||
|
{
|
||||||
|
let abs_dc = dc.abs();
|
||||||
|
if abs_dc > 0
|
||||||
|
&& let Ok(dc_idx) = i16::try_from(abs_dc)
|
||||||
|
{
|
||||||
|
return Some(dc_idx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn direct_bind_ip_for_stun(
|
||||||
|
family: IpFamily,
|
||||||
|
upstream_egress: Option<UpstreamEgressInfo>,
|
||||||
|
) -> Option<IpAddr> {
|
||||||
|
let info = upstream_egress?;
|
||||||
|
if info.route_kind != UpstreamRouteKind::Direct {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
match (family, info.direct_bind_ip) {
|
||||||
|
(IpFamily::V4, Some(IpAddr::V4(ip))) => Some(IpAddr::V4(ip)),
|
||||||
|
(IpFamily::V6, Some(IpAddr::V6(ip))) => Some(IpAddr::V6(ip)),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn select_socks_bound_addr(
|
||||||
|
family: IpFamily,
|
||||||
|
upstream_egress: Option<UpstreamEgressInfo>,
|
||||||
|
) -> Option<SocketAddr> {
|
||||||
|
let info = upstream_egress?;
|
||||||
|
if !matches!(
|
||||||
|
info.route_kind,
|
||||||
|
UpstreamRouteKind::Socks4 | UpstreamRouteKind::Socks5
|
||||||
|
) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let bound = info.socks_bound_addr?;
|
||||||
|
let family_matches = matches!(
|
||||||
|
(family, bound.ip()),
|
||||||
|
(IpFamily::V4, IpAddr::V4(_)) | (IpFamily::V6, IpAddr::V6(_))
|
||||||
|
);
|
||||||
|
if !family_matches || is_bogon(bound.ip()) || bound.ip().is_unspecified() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
Some(bound)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_socks_route(upstream_egress: Option<UpstreamEgressInfo>) -> bool {
|
||||||
|
matches!(
|
||||||
|
upstream_egress.map(|info| info.route_kind),
|
||||||
|
Some(UpstreamRouteKind::Socks4 | UpstreamRouteKind::Socks5)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// TCP connect with timeout + return RTT in milliseconds.
|
||||||
|
pub(crate) async fn connect_tcp(
|
||||||
|
&self,
|
||||||
|
addr: SocketAddr,
|
||||||
|
) -> Result<(TcpStream, f64, Option<UpstreamEgressInfo>)> {
|
||||||
|
let start = Instant::now();
|
||||||
|
let (stream, upstream_egress) = if let Some(upstream) = &self.upstream {
|
||||||
|
let dc_idx = self.resolve_dc_idx_for_endpoint(addr).await;
|
||||||
|
let (stream, egress) = upstream.connect_with_details(addr, dc_idx, None).await?;
|
||||||
|
(stream, Some(egress))
|
||||||
|
} else {
|
||||||
|
let connect_fut = async {
|
||||||
|
if addr.is_ipv6()
|
||||||
|
&& let Some(v6) = self.detected_ipv6
|
||||||
|
{
|
||||||
|
match TcpSocket::new_v6() {
|
||||||
|
Ok(sock) => {
|
||||||
|
if let Err(e) = sock.bind(SocketAddr::new(IpAddr::V6(v6), 0)) {
|
||||||
|
debug!(error = %e, bind_ip = %v6, "ME IPv6 bind failed, falling back to default bind");
|
||||||
|
} else {
|
||||||
|
match sock.connect(addr).await {
|
||||||
|
Ok(stream) => return Ok(stream),
|
||||||
|
Err(e) => debug!(error = %e, target = %addr, "ME IPv6 bound connect failed, retrying default connect"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => debug!(error = %e, "ME IPv6 socket creation failed, falling back to default connect"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TcpStream::connect(addr).await
|
||||||
|
};
|
||||||
|
|
||||||
|
let stream = timeout(Duration::from_secs(ME_CONNECT_TIMEOUT_SECS), connect_fut)
|
||||||
|
.await
|
||||||
|
.map_err(|_| ProxyError::ConnectionTimeout {
|
||||||
|
addr: addr.to_string(),
|
||||||
|
})??;
|
||||||
|
(stream, None)
|
||||||
};
|
};
|
||||||
|
|
||||||
let stream = timeout(Duration::from_secs(ME_CONNECT_TIMEOUT_SECS), connect_fut)
|
|
||||||
.await
|
|
||||||
.map_err(|_| ProxyError::ConnectionTimeout { addr: addr.to_string() })??;
|
|
||||||
let connect_ms = start.elapsed().as_secs_f64() * 1000.0;
|
let connect_ms = start.elapsed().as_secs_f64() * 1000.0;
|
||||||
stream.set_nodelay(true).ok();
|
stream.set_nodelay(true).ok();
|
||||||
if let Err(e) = Self::configure_keepalive(&stream) {
|
if let Err(e) = Self::configure_keepalive(&stream) {
|
||||||
@@ -79,7 +211,7 @@ impl MePool {
|
|||||||
if let Err(e) = Self::configure_user_timeout(stream.as_raw_fd()) {
|
if let Err(e) = Self::configure_user_timeout(stream.as_raw_fd()) {
|
||||||
warn!(error = %e, "ME TCP_USER_TIMEOUT setup failed");
|
warn!(error = %e, "ME TCP_USER_TIMEOUT setup failed");
|
||||||
}
|
}
|
||||||
Ok((stream, connect_ms))
|
Ok((stream, connect_ms, upstream_egress))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn configure_keepalive(stream: &TcpStream) -> std::io::Result<()> {
|
fn configure_keepalive(stream: &TcpStream) -> std::io::Result<()> {
|
||||||
@@ -117,12 +249,14 @@ impl MePool {
|
|||||||
&self,
|
&self,
|
||||||
stream: TcpStream,
|
stream: TcpStream,
|
||||||
addr: SocketAddr,
|
addr: SocketAddr,
|
||||||
|
upstream_egress: Option<UpstreamEgressInfo>,
|
||||||
rng: &SecureRandom,
|
rng: &SecureRandom,
|
||||||
) -> Result<HandshakeOutput> {
|
) -> Result<HandshakeOutput> {
|
||||||
let hs_start = Instant::now();
|
let hs_start = Instant::now();
|
||||||
|
|
||||||
let local_addr = stream.local_addr().map_err(ProxyError::Io)?;
|
let local_addr = stream.local_addr().map_err(ProxyError::Io)?;
|
||||||
let peer_addr = stream.peer_addr().map_err(ProxyError::Io)?;
|
let transport_peer_addr = stream.peer_addr().map_err(ProxyError::Io)?;
|
||||||
|
let peer_addr = addr;
|
||||||
|
|
||||||
let _ = self.maybe_detect_nat_ip(local_addr.ip()).await;
|
let _ = self.maybe_detect_nat_ip(local_addr.ip()).await;
|
||||||
let family = if local_addr.ip().is_ipv4() {
|
let family = if local_addr.ip().is_ipv4() {
|
||||||
@@ -130,8 +264,32 @@ impl MePool {
|
|||||||
} else {
|
} else {
|
||||||
IpFamily::V6
|
IpFamily::V6
|
||||||
};
|
};
|
||||||
let reflected = if self.nat_probe {
|
let is_socks_route = Self::is_socks_route(upstream_egress);
|
||||||
self.maybe_reflect_public_addr(family).await
|
let socks_bound_addr = Self::select_socks_bound_addr(family, upstream_egress);
|
||||||
|
let reflected = if let Some(bound) = socks_bound_addr {
|
||||||
|
Some(bound)
|
||||||
|
} else if is_socks_route {
|
||||||
|
match self.socks_kdf_policy() {
|
||||||
|
MeSocksKdfPolicy::Strict => {
|
||||||
|
self.stats.increment_me_socks_kdf_strict_reject();
|
||||||
|
return Err(ProxyError::InvalidHandshake(
|
||||||
|
"SOCKS route returned no valid BND.ADDR for ME KDF (strict policy)"
|
||||||
|
.to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
MeSocksKdfPolicy::Compat => {
|
||||||
|
self.stats.increment_me_socks_kdf_compat_fallback();
|
||||||
|
if self.nat_probe {
|
||||||
|
let bind_ip = Self::direct_bind_ip_for_stun(family, upstream_egress);
|
||||||
|
self.maybe_reflect_public_addr(family, bind_ip).await
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if self.nat_probe {
|
||||||
|
let bind_ip = Self::direct_bind_ip_for_stun(family, upstream_egress);
|
||||||
|
self.maybe_reflect_public_addr(family, bind_ip).await
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
@@ -146,7 +304,16 @@ impl MePool {
|
|||||||
.unwrap_or_default()
|
.unwrap_or_default()
|
||||||
.as_secs() as u32;
|
.as_secs() as u32;
|
||||||
|
|
||||||
let ks = self.key_selector().await;
|
let secret_atomic_snapshot = self.secret_atomic_snapshot.load(Ordering::Relaxed);
|
||||||
|
let (ks, secret) = if secret_atomic_snapshot {
|
||||||
|
let snapshot = self.secret_snapshot().await;
|
||||||
|
(snapshot.key_selector, snapshot.secret)
|
||||||
|
} else {
|
||||||
|
// Backward-compatible mode: key selector and secret may come from different updates.
|
||||||
|
let key_selector = self.key_selector().await;
|
||||||
|
let secret = self.secret_snapshot().await.secret;
|
||||||
|
(key_selector, secret)
|
||||||
|
};
|
||||||
let nonce_payload = build_nonce_payload(ks, crypto_ts, &my_nonce);
|
let nonce_payload = build_nonce_payload(ks, crypto_ts, &my_nonce);
|
||||||
let nonce_frame = build_rpc_frame(-2, &nonce_payload, RpcChecksumMode::Crc32);
|
let nonce_frame = build_rpc_frame(-2, &nonce_payload, RpcChecksumMode::Crc32);
|
||||||
let dump = hex_dump(&nonce_frame[..nonce_frame.len().min(44)]);
|
let dump = hex_dump(&nonce_frame[..nonce_frame.len().min(44)]);
|
||||||
@@ -197,7 +364,9 @@ impl MePool {
|
|||||||
%local_addr_nat,
|
%local_addr_nat,
|
||||||
reflected_ip = reflected.map(|r| r.ip()).as_ref().map(ToString::to_string),
|
reflected_ip = reflected.map(|r| r.ip()).as_ref().map(ToString::to_string),
|
||||||
%peer_addr,
|
%peer_addr,
|
||||||
|
%transport_peer_addr,
|
||||||
%peer_addr_nat,
|
%peer_addr_nat,
|
||||||
|
socks_bound_addr = socks_bound_addr.map(|v| v.to_string()),
|
||||||
key_selector = format_args!("0x{ks:08x}"),
|
key_selector = format_args!("0x{ks:08x}"),
|
||||||
crypto_schema = format_args!("0x{schema:08x}"),
|
crypto_schema = format_args!("0x{schema:08x}"),
|
||||||
skew_secs = skew,
|
skew_secs = skew,
|
||||||
@@ -206,7 +375,51 @@ impl MePool {
|
|||||||
|
|
||||||
let ts_bytes = crypto_ts.to_le_bytes();
|
let ts_bytes = crypto_ts.to_le_bytes();
|
||||||
let server_port_bytes = peer_addr_nat.port().to_le_bytes();
|
let server_port_bytes = peer_addr_nat.port().to_le_bytes();
|
||||||
let client_port_bytes = local_addr_nat.port().to_le_bytes();
|
let socks_bound_port = socks_bound_addr
|
||||||
|
.map(|bound| bound.port())
|
||||||
|
.filter(|port| *port != 0);
|
||||||
|
let client_port_for_kdf = socks_bound_port.unwrap_or(local_addr_nat.port());
|
||||||
|
let client_port_source = KdfClientPortSource::from_socks_bound_port(socks_bound_port);
|
||||||
|
let kdf_fingerprint = Self::kdf_material_fingerprint(
|
||||||
|
local_addr_nat.ip(),
|
||||||
|
peer_addr_nat,
|
||||||
|
reflected.map(|value| value.ip()),
|
||||||
|
socks_bound_addr.map(|value| value.ip()),
|
||||||
|
client_port_source,
|
||||||
|
);
|
||||||
|
let mut kdf_fingerprint_guard = self.kdf_material_fingerprint.lock().await;
|
||||||
|
if let Some((prev_fingerprint, prev_client_port)) =
|
||||||
|
kdf_fingerprint_guard.get(&peer_addr_nat).copied()
|
||||||
|
{
|
||||||
|
if prev_fingerprint != kdf_fingerprint {
|
||||||
|
self.stats.increment_me_kdf_drift_total();
|
||||||
|
warn!(
|
||||||
|
%peer_addr_nat,
|
||||||
|
%local_addr_nat,
|
||||||
|
client_port_for_kdf,
|
||||||
|
client_port_source = ?client_port_source,
|
||||||
|
"ME KDF material drift detected for endpoint"
|
||||||
|
);
|
||||||
|
if ME_KDF_DRIFT_STRICT {
|
||||||
|
return Err(ProxyError::InvalidHandshake(
|
||||||
|
"ME KDF material drift detected (strict mode)".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
} else if prev_client_port != client_port_for_kdf {
|
||||||
|
self.stats.increment_me_kdf_port_only_drift_total();
|
||||||
|
debug!(
|
||||||
|
%peer_addr_nat,
|
||||||
|
previous_client_port_for_kdf = prev_client_port,
|
||||||
|
client_port_for_kdf,
|
||||||
|
client_port_source = ?client_port_source,
|
||||||
|
"ME KDF client port changed with stable material"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
kdf_fingerprint_guard.insert(peer_addr_nat, (kdf_fingerprint, client_port_for_kdf));
|
||||||
|
drop(kdf_fingerprint_guard);
|
||||||
|
|
||||||
|
let client_port_bytes = client_port_for_kdf.to_le_bytes();
|
||||||
|
|
||||||
let server_ip = extract_ip_material(peer_addr_nat);
|
let server_ip = extract_ip_material(peer_addr_nat);
|
||||||
let client_ip = extract_ip_material(local_addr_nat);
|
let client_ip = extract_ip_material(local_addr_nat);
|
||||||
@@ -230,8 +443,6 @@ impl MePool {
|
|||||||
|
|
||||||
let diag_level: u8 = std::env::var("ME_DIAG").ok().and_then(|v| v.parse().ok()).unwrap_or(0);
|
let diag_level: u8 = std::env::var("ME_DIAG").ok().and_then(|v| v.parse().ok()).unwrap_or(0);
|
||||||
|
|
||||||
let secret: Vec<u8> = self.proxy_secret.read().await.clone();
|
|
||||||
|
|
||||||
let prekey_client = build_middleproxy_prekey(
|
let prekey_client = build_middleproxy_prekey(
|
||||||
&srv_nonce,
|
&srv_nonce,
|
||||||
&my_nonce,
|
&my_nonce,
|
||||||
@@ -405,6 +616,8 @@ impl MePool {
|
|||||||
} else {
|
} else {
|
||||||
-1
|
-1
|
||||||
};
|
};
|
||||||
|
self.stats.increment_me_handshake_reject_total();
|
||||||
|
self.stats.increment_me_handshake_error_code(err_code);
|
||||||
return Err(ProxyError::InvalidHandshake(format!(
|
return Err(ProxyError::InvalidHandshake(format!(
|
||||||
"ME rejected handshake (error={err_code})"
|
"ME rejected handshake (error={err_code})"
|
||||||
)));
|
)));
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::collections::HashSet;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
use tracing::{debug, info, warn};
|
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
use tracing::{debug, info, warn};
|
||||||
|
|
||||||
|
use crate::config::MeFloorMode;
|
||||||
use crate::crypto::SecureRandom;
|
use crate::crypto::SecureRandom;
|
||||||
use crate::network::IpFamily;
|
use crate::network::IpFamily;
|
||||||
|
|
||||||
@@ -15,13 +17,26 @@ const HEALTH_INTERVAL_SECS: u64 = 1;
|
|||||||
const JITTER_FRAC_NUM: u64 = 2; // jitter up to 50% of backoff
|
const JITTER_FRAC_NUM: u64 = 2; // jitter up to 50% of backoff
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
const MAX_CONCURRENT_PER_DC_DEFAULT: usize = 1;
|
const MAX_CONCURRENT_PER_DC_DEFAULT: usize = 1;
|
||||||
|
const SHADOW_ROTATE_RETRY_SECS: u64 = 30;
|
||||||
|
const IDLE_REFRESH_TRIGGER_BASE_SECS: u64 = 45;
|
||||||
|
const IDLE_REFRESH_TRIGGER_JITTER_SECS: u64 = 5;
|
||||||
|
const IDLE_REFRESH_RETRY_SECS: u64 = 8;
|
||||||
|
const IDLE_REFRESH_SUCCESS_GUARD_SECS: u64 = 5;
|
||||||
|
|
||||||
pub async fn me_health_monitor(pool: Arc<MePool>, rng: Arc<SecureRandom>, _min_connections: usize) {
|
pub async fn me_health_monitor(pool: Arc<MePool>, rng: Arc<SecureRandom>, _min_connections: usize) {
|
||||||
let mut backoff: HashMap<(i32, IpFamily), u64> = HashMap::new();
|
let mut backoff: HashMap<(i32, IpFamily), u64> = HashMap::new();
|
||||||
let mut next_attempt: HashMap<(i32, IpFamily), Instant> = HashMap::new();
|
let mut next_attempt: HashMap<(i32, IpFamily), Instant> = HashMap::new();
|
||||||
let mut inflight: HashMap<(i32, IpFamily), usize> = HashMap::new();
|
let mut inflight: HashMap<(i32, IpFamily), usize> = HashMap::new();
|
||||||
|
let mut outage_backoff: HashMap<(i32, IpFamily), u64> = HashMap::new();
|
||||||
|
let mut outage_next_attempt: HashMap<(i32, IpFamily), Instant> = HashMap::new();
|
||||||
|
let mut single_endpoint_outage: HashSet<(i32, IpFamily)> = HashSet::new();
|
||||||
|
let mut shadow_rotate_deadline: HashMap<(i32, IpFamily), Instant> = HashMap::new();
|
||||||
|
let mut idle_refresh_next_attempt: HashMap<(i32, IpFamily), Instant> = HashMap::new();
|
||||||
|
let mut adaptive_idle_since: HashMap<(i32, IpFamily), Instant> = HashMap::new();
|
||||||
|
let mut adaptive_recover_until: HashMap<(i32, IpFamily), Instant> = HashMap::new();
|
||||||
loop {
|
loop {
|
||||||
tokio::time::sleep(Duration::from_secs(HEALTH_INTERVAL_SECS)).await;
|
tokio::time::sleep(Duration::from_secs(HEALTH_INTERVAL_SECS)).await;
|
||||||
|
pool.prune_closed_writers().await;
|
||||||
check_family(
|
check_family(
|
||||||
IpFamily::V4,
|
IpFamily::V4,
|
||||||
&pool,
|
&pool,
|
||||||
@@ -29,6 +44,13 @@ pub async fn me_health_monitor(pool: Arc<MePool>, rng: Arc<SecureRandom>, _min_c
|
|||||||
&mut backoff,
|
&mut backoff,
|
||||||
&mut next_attempt,
|
&mut next_attempt,
|
||||||
&mut inflight,
|
&mut inflight,
|
||||||
|
&mut outage_backoff,
|
||||||
|
&mut outage_next_attempt,
|
||||||
|
&mut single_endpoint_outage,
|
||||||
|
&mut shadow_rotate_deadline,
|
||||||
|
&mut idle_refresh_next_attempt,
|
||||||
|
&mut adaptive_idle_since,
|
||||||
|
&mut adaptive_recover_until,
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
check_family(
|
check_family(
|
||||||
@@ -38,6 +60,13 @@ pub async fn me_health_monitor(pool: Arc<MePool>, rng: Arc<SecureRandom>, _min_c
|
|||||||
&mut backoff,
|
&mut backoff,
|
||||||
&mut next_attempt,
|
&mut next_attempt,
|
||||||
&mut inflight,
|
&mut inflight,
|
||||||
|
&mut outage_backoff,
|
||||||
|
&mut outage_next_attempt,
|
||||||
|
&mut single_endpoint_outage,
|
||||||
|
&mut shadow_rotate_deadline,
|
||||||
|
&mut idle_refresh_next_attempt,
|
||||||
|
&mut adaptive_idle_since,
|
||||||
|
&mut adaptive_recover_until,
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
@@ -50,6 +79,13 @@ async fn check_family(
|
|||||||
backoff: &mut HashMap<(i32, IpFamily), u64>,
|
backoff: &mut HashMap<(i32, IpFamily), u64>,
|
||||||
next_attempt: &mut HashMap<(i32, IpFamily), Instant>,
|
next_attempt: &mut HashMap<(i32, IpFamily), Instant>,
|
||||||
inflight: &mut HashMap<(i32, IpFamily), usize>,
|
inflight: &mut HashMap<(i32, IpFamily), usize>,
|
||||||
|
outage_backoff: &mut HashMap<(i32, IpFamily), u64>,
|
||||||
|
outage_next_attempt: &mut HashMap<(i32, IpFamily), Instant>,
|
||||||
|
single_endpoint_outage: &mut HashSet<(i32, IpFamily)>,
|
||||||
|
shadow_rotate_deadline: &mut HashMap<(i32, IpFamily), Instant>,
|
||||||
|
idle_refresh_next_attempt: &mut HashMap<(i32, IpFamily), Instant>,
|
||||||
|
adaptive_idle_since: &mut HashMap<(i32, IpFamily), Instant>,
|
||||||
|
adaptive_recover_until: &mut HashMap<(i32, IpFamily), Instant>,
|
||||||
) {
|
) {
|
||||||
let enabled = match family {
|
let enabled = match family {
|
||||||
IpFamily::V4 => pool.decision.ipv4_me,
|
IpFamily::V4 => pool.decision.ipv4_me,
|
||||||
@@ -76,32 +112,119 @@ async fn check_family(
|
|||||||
endpoints.dedup();
|
endpoints.dedup();
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut live_addr_counts = HashMap::<SocketAddr, usize>::new();
|
if pool.floor_mode() == MeFloorMode::Static {
|
||||||
for writer in pool
|
adaptive_idle_since.clear();
|
||||||
.writers
|
adaptive_recover_until.clear();
|
||||||
.read()
|
|
||||||
.await
|
|
||||||
.iter()
|
|
||||||
.filter(|w| !w.draining.load(std::sync::atomic::Ordering::Relaxed))
|
|
||||||
{
|
|
||||||
*live_addr_counts.entry(writer.addr).or_insert(0) += 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut live_addr_counts = HashMap::<SocketAddr, usize>::new();
|
||||||
|
let mut live_writer_ids_by_addr = HashMap::<SocketAddr, Vec<u64>>::new();
|
||||||
|
for writer in pool.writers.read().await.iter().filter(|w| {
|
||||||
|
!w.draining.load(std::sync::atomic::Ordering::Relaxed)
|
||||||
|
}) {
|
||||||
|
*live_addr_counts.entry(writer.addr).or_insert(0) += 1;
|
||||||
|
live_writer_ids_by_addr
|
||||||
|
.entry(writer.addr)
|
||||||
|
.or_default()
|
||||||
|
.push(writer.id);
|
||||||
|
}
|
||||||
|
let writer_idle_since = pool.registry.writer_idle_since_snapshot().await;
|
||||||
|
|
||||||
for (dc, endpoints) in dc_endpoints {
|
for (dc, endpoints) in dc_endpoints {
|
||||||
if endpoints.is_empty() {
|
if endpoints.is_empty() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let required = MePool::required_writers_for_dc(endpoints.len());
|
let key = (dc, family);
|
||||||
|
let reduce_for_idle = should_reduce_floor_for_idle(
|
||||||
|
pool,
|
||||||
|
key,
|
||||||
|
&endpoints,
|
||||||
|
&live_writer_ids_by_addr,
|
||||||
|
adaptive_idle_since,
|
||||||
|
adaptive_recover_until,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
let required = pool.required_writers_for_dc_with_floor_mode(endpoints.len(), reduce_for_idle);
|
||||||
let alive = endpoints
|
let alive = endpoints
|
||||||
.iter()
|
.iter()
|
||||||
.map(|addr| *live_addr_counts.get(addr).unwrap_or(&0))
|
.map(|addr| *live_addr_counts.get(addr).unwrap_or(&0))
|
||||||
.sum::<usize>();
|
.sum::<usize>();
|
||||||
|
|
||||||
|
if endpoints.len() == 1 && pool.single_endpoint_outage_mode_enabled() && alive == 0 {
|
||||||
|
if single_endpoint_outage.insert(key) {
|
||||||
|
pool.stats.increment_me_single_endpoint_outage_enter_total();
|
||||||
|
warn!(
|
||||||
|
dc = %dc,
|
||||||
|
?family,
|
||||||
|
required,
|
||||||
|
endpoint_count = endpoints.len(),
|
||||||
|
"Single-endpoint DC outage detected"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
recover_single_endpoint_outage(
|
||||||
|
pool,
|
||||||
|
rng,
|
||||||
|
key,
|
||||||
|
endpoints[0],
|
||||||
|
required,
|
||||||
|
outage_backoff,
|
||||||
|
outage_next_attempt,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if single_endpoint_outage.remove(&key) {
|
||||||
|
pool.stats.increment_me_single_endpoint_outage_exit_total();
|
||||||
|
outage_backoff.remove(&key);
|
||||||
|
outage_next_attempt.remove(&key);
|
||||||
|
shadow_rotate_deadline.remove(&key);
|
||||||
|
idle_refresh_next_attempt.remove(&key);
|
||||||
|
adaptive_idle_since.remove(&key);
|
||||||
|
adaptive_recover_until.remove(&key);
|
||||||
|
info!(
|
||||||
|
dc = %dc,
|
||||||
|
?family,
|
||||||
|
alive,
|
||||||
|
required,
|
||||||
|
endpoint_count = endpoints.len(),
|
||||||
|
"Single-endpoint DC outage recovered"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if alive >= required {
|
if alive >= required {
|
||||||
|
maybe_refresh_idle_writer_for_dc(
|
||||||
|
pool,
|
||||||
|
rng,
|
||||||
|
key,
|
||||||
|
dc,
|
||||||
|
family,
|
||||||
|
&endpoints,
|
||||||
|
alive,
|
||||||
|
required,
|
||||||
|
&live_writer_ids_by_addr,
|
||||||
|
&writer_idle_since,
|
||||||
|
idle_refresh_next_attempt,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
maybe_rotate_single_endpoint_shadow(
|
||||||
|
pool,
|
||||||
|
rng,
|
||||||
|
key,
|
||||||
|
dc,
|
||||||
|
family,
|
||||||
|
&endpoints,
|
||||||
|
alive,
|
||||||
|
required,
|
||||||
|
&live_writer_ids_by_addr,
|
||||||
|
shadow_rotate_deadline,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let missing = required - alive;
|
let missing = required - alive;
|
||||||
|
|
||||||
let key = (dc, family);
|
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
if let Some(ts) = next_attempt.get(&key)
|
if let Some(ts) = next_attempt.get(&key)
|
||||||
&& now < *ts
|
&& now < *ts
|
||||||
@@ -111,7 +234,18 @@ async fn check_family(
|
|||||||
|
|
||||||
let max_concurrent = pool.me_reconnect_max_concurrent_per_dc.max(1) as usize;
|
let max_concurrent = pool.me_reconnect_max_concurrent_per_dc.max(1) as usize;
|
||||||
if *inflight.get(&key).unwrap_or(&0) >= max_concurrent {
|
if *inflight.get(&key).unwrap_or(&0) >= max_concurrent {
|
||||||
return;
|
continue;
|
||||||
|
}
|
||||||
|
if pool.has_refill_inflight_for_endpoints(&endpoints).await {
|
||||||
|
debug!(
|
||||||
|
dc = %dc,
|
||||||
|
?family,
|
||||||
|
alive,
|
||||||
|
required,
|
||||||
|
endpoint_count = endpoints.len(),
|
||||||
|
"Skipping health reconnect: immediate refill is already in flight for this DC group"
|
||||||
|
);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
*inflight.entry(key).or_insert(0) += 1;
|
*inflight.entry(key).or_insert(0) += 1;
|
||||||
|
|
||||||
@@ -176,3 +310,375 @@ async fn check_family(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn maybe_refresh_idle_writer_for_dc(
|
||||||
|
pool: &Arc<MePool>,
|
||||||
|
rng: &Arc<SecureRandom>,
|
||||||
|
key: (i32, IpFamily),
|
||||||
|
dc: i32,
|
||||||
|
family: IpFamily,
|
||||||
|
endpoints: &[SocketAddr],
|
||||||
|
alive: usize,
|
||||||
|
required: usize,
|
||||||
|
live_writer_ids_by_addr: &HashMap<SocketAddr, Vec<u64>>,
|
||||||
|
writer_idle_since: &HashMap<u64, u64>,
|
||||||
|
idle_refresh_next_attempt: &mut HashMap<(i32, IpFamily), Instant>,
|
||||||
|
) {
|
||||||
|
if alive < required {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let now = Instant::now();
|
||||||
|
if let Some(next) = idle_refresh_next_attempt.get(&key)
|
||||||
|
&& now < *next
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let now_epoch_secs = MePool::now_epoch_secs();
|
||||||
|
let mut candidate: Option<(u64, SocketAddr, u64, u64)> = None;
|
||||||
|
for endpoint in endpoints {
|
||||||
|
let Some(writer_ids) = live_writer_ids_by_addr.get(endpoint) else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
for writer_id in writer_ids {
|
||||||
|
let Some(idle_since_epoch_secs) = writer_idle_since.get(writer_id).copied() else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
let idle_age_secs = now_epoch_secs.saturating_sub(idle_since_epoch_secs);
|
||||||
|
let threshold_secs = IDLE_REFRESH_TRIGGER_BASE_SECS
|
||||||
|
+ (*writer_id % (IDLE_REFRESH_TRIGGER_JITTER_SECS + 1));
|
||||||
|
if idle_age_secs < threshold_secs {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if candidate
|
||||||
|
.as_ref()
|
||||||
|
.map(|(_, _, age, _)| idle_age_secs > *age)
|
||||||
|
.unwrap_or(true)
|
||||||
|
{
|
||||||
|
candidate = Some((*writer_id, *endpoint, idle_age_secs, threshold_secs));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let Some((old_writer_id, endpoint, idle_age_secs, threshold_secs)) = candidate else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
let rotate_ok = match tokio::time::timeout(pool.me_one_timeout, pool.connect_one(endpoint, rng.as_ref())).await {
|
||||||
|
Ok(Ok(())) => true,
|
||||||
|
Ok(Err(error)) => {
|
||||||
|
debug!(
|
||||||
|
dc = %dc,
|
||||||
|
?family,
|
||||||
|
%endpoint,
|
||||||
|
old_writer_id,
|
||||||
|
idle_age_secs,
|
||||||
|
threshold_secs,
|
||||||
|
%error,
|
||||||
|
"Idle writer pre-refresh connect failed"
|
||||||
|
);
|
||||||
|
false
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
debug!(
|
||||||
|
dc = %dc,
|
||||||
|
?family,
|
||||||
|
%endpoint,
|
||||||
|
old_writer_id,
|
||||||
|
idle_age_secs,
|
||||||
|
threshold_secs,
|
||||||
|
"Idle writer pre-refresh connect timed out"
|
||||||
|
);
|
||||||
|
false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if !rotate_ok {
|
||||||
|
idle_refresh_next_attempt.insert(key, now + Duration::from_secs(IDLE_REFRESH_RETRY_SECS));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
pool.mark_writer_draining_with_timeout(old_writer_id, pool.force_close_timeout(), false)
|
||||||
|
.await;
|
||||||
|
idle_refresh_next_attempt.insert(
|
||||||
|
key,
|
||||||
|
now + Duration::from_secs(IDLE_REFRESH_SUCCESS_GUARD_SECS),
|
||||||
|
);
|
||||||
|
info!(
|
||||||
|
dc = %dc,
|
||||||
|
?family,
|
||||||
|
%endpoint,
|
||||||
|
old_writer_id,
|
||||||
|
idle_age_secs,
|
||||||
|
threshold_secs,
|
||||||
|
alive,
|
||||||
|
required,
|
||||||
|
"Idle writer refreshed before upstream idle timeout"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn should_reduce_floor_for_idle(
|
||||||
|
pool: &Arc<MePool>,
|
||||||
|
key: (i32, IpFamily),
|
||||||
|
endpoints: &[SocketAddr],
|
||||||
|
live_writer_ids_by_addr: &HashMap<SocketAddr, Vec<u64>>,
|
||||||
|
adaptive_idle_since: &mut HashMap<(i32, IpFamily), Instant>,
|
||||||
|
adaptive_recover_until: &mut HashMap<(i32, IpFamily), Instant>,
|
||||||
|
) -> bool {
|
||||||
|
if endpoints.len() != 1 || pool.floor_mode() != MeFloorMode::Adaptive {
|
||||||
|
adaptive_idle_since.remove(&key);
|
||||||
|
adaptive_recover_until.remove(&key);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
let now = Instant::now();
|
||||||
|
let endpoint = endpoints[0];
|
||||||
|
let writer_ids = live_writer_ids_by_addr
|
||||||
|
.get(&endpoint)
|
||||||
|
.map(Vec::as_slice)
|
||||||
|
.unwrap_or(&[]);
|
||||||
|
let has_bound_clients = has_bound_clients_on_endpoint(pool, writer_ids).await;
|
||||||
|
if has_bound_clients {
|
||||||
|
adaptive_idle_since.remove(&key);
|
||||||
|
adaptive_recover_until.insert(key, now + pool.adaptive_floor_recover_grace_duration());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(recover_until) = adaptive_recover_until.get(&key)
|
||||||
|
&& now < *recover_until
|
||||||
|
{
|
||||||
|
adaptive_idle_since.remove(&key);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
adaptive_recover_until.remove(&key);
|
||||||
|
|
||||||
|
let idle_since = adaptive_idle_since.entry(key).or_insert(now);
|
||||||
|
now.saturating_duration_since(*idle_since) >= pool.adaptive_floor_idle_duration()
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn has_bound_clients_on_endpoint(pool: &Arc<MePool>, writer_ids: &[u64]) -> bool {
|
||||||
|
for writer_id in writer_ids {
|
||||||
|
if !pool.registry.is_writer_empty(*writer_id).await {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn recover_single_endpoint_outage(
|
||||||
|
pool: &Arc<MePool>,
|
||||||
|
rng: &Arc<SecureRandom>,
|
||||||
|
key: (i32, IpFamily),
|
||||||
|
endpoint: SocketAddr,
|
||||||
|
required: usize,
|
||||||
|
outage_backoff: &mut HashMap<(i32, IpFamily), u64>,
|
||||||
|
outage_next_attempt: &mut HashMap<(i32, IpFamily), Instant>,
|
||||||
|
) {
|
||||||
|
let now = Instant::now();
|
||||||
|
if let Some(ts) = outage_next_attempt.get(&key)
|
||||||
|
&& now < *ts
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let (min_backoff_ms, max_backoff_ms) = pool.single_endpoint_outage_backoff_bounds_ms();
|
||||||
|
pool.stats
|
||||||
|
.increment_me_single_endpoint_outage_reconnect_attempt_total();
|
||||||
|
|
||||||
|
let bypass_quarantine = pool.single_endpoint_outage_disable_quarantine();
|
||||||
|
let attempt_ok = if bypass_quarantine {
|
||||||
|
pool.stats
|
||||||
|
.increment_me_single_endpoint_quarantine_bypass_total();
|
||||||
|
match tokio::time::timeout(pool.me_one_timeout, pool.connect_one(endpoint, rng.as_ref())).await {
|
||||||
|
Ok(Ok(())) => true,
|
||||||
|
Ok(Err(e)) => {
|
||||||
|
debug!(
|
||||||
|
dc = %key.0,
|
||||||
|
family = ?key.1,
|
||||||
|
%endpoint,
|
||||||
|
error = %e,
|
||||||
|
"Single-endpoint outage reconnect failed (quarantine bypass path)"
|
||||||
|
);
|
||||||
|
false
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
debug!(
|
||||||
|
dc = %key.0,
|
||||||
|
family = ?key.1,
|
||||||
|
%endpoint,
|
||||||
|
"Single-endpoint outage reconnect timed out (quarantine bypass path)"
|
||||||
|
);
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let one_endpoint = [endpoint];
|
||||||
|
match tokio::time::timeout(
|
||||||
|
pool.me_one_timeout,
|
||||||
|
pool.connect_endpoints_round_robin(&one_endpoint, rng.as_ref()),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(ok) => ok,
|
||||||
|
Err(_) => {
|
||||||
|
debug!(
|
||||||
|
dc = %key.0,
|
||||||
|
family = ?key.1,
|
||||||
|
%endpoint,
|
||||||
|
"Single-endpoint outage reconnect timed out"
|
||||||
|
);
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if attempt_ok {
|
||||||
|
pool.stats
|
||||||
|
.increment_me_single_endpoint_outage_reconnect_success_total();
|
||||||
|
pool.stats.increment_me_reconnect_success();
|
||||||
|
outage_backoff.insert(key, min_backoff_ms);
|
||||||
|
let jitter = min_backoff_ms / JITTER_FRAC_NUM;
|
||||||
|
let wait = Duration::from_millis(min_backoff_ms)
|
||||||
|
+ Duration::from_millis(rand::rng().random_range(0..=jitter.max(1)));
|
||||||
|
outage_next_attempt.insert(key, now + wait);
|
||||||
|
info!(
|
||||||
|
dc = %key.0,
|
||||||
|
family = ?key.1,
|
||||||
|
%endpoint,
|
||||||
|
required,
|
||||||
|
backoff_ms = min_backoff_ms,
|
||||||
|
"Single-endpoint outage reconnect succeeded"
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
pool.stats.increment_me_reconnect_attempt();
|
||||||
|
let current_ms = *outage_backoff.get(&key).unwrap_or(&min_backoff_ms);
|
||||||
|
let next_ms = current_ms.saturating_mul(2).min(max_backoff_ms);
|
||||||
|
outage_backoff.insert(key, next_ms);
|
||||||
|
let jitter = next_ms / JITTER_FRAC_NUM;
|
||||||
|
let wait = Duration::from_millis(next_ms)
|
||||||
|
+ Duration::from_millis(rand::rng().random_range(0..=jitter.max(1)));
|
||||||
|
outage_next_attempt.insert(key, now + wait);
|
||||||
|
warn!(
|
||||||
|
dc = %key.0,
|
||||||
|
family = ?key.1,
|
||||||
|
%endpoint,
|
||||||
|
required,
|
||||||
|
backoff_ms = next_ms,
|
||||||
|
"Single-endpoint outage reconnect scheduled"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn maybe_rotate_single_endpoint_shadow(
|
||||||
|
pool: &Arc<MePool>,
|
||||||
|
rng: &Arc<SecureRandom>,
|
||||||
|
key: (i32, IpFamily),
|
||||||
|
dc: i32,
|
||||||
|
family: IpFamily,
|
||||||
|
endpoints: &[SocketAddr],
|
||||||
|
alive: usize,
|
||||||
|
required: usize,
|
||||||
|
live_writer_ids_by_addr: &HashMap<SocketAddr, Vec<u64>>,
|
||||||
|
shadow_rotate_deadline: &mut HashMap<(i32, IpFamily), Instant>,
|
||||||
|
) {
|
||||||
|
if endpoints.len() != 1 || alive < required {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let Some(interval) = pool.single_endpoint_shadow_rotate_interval() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
let now = Instant::now();
|
||||||
|
if let Some(deadline) = shadow_rotate_deadline.get(&key)
|
||||||
|
&& now < *deadline
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let endpoint = endpoints[0];
|
||||||
|
if pool.is_endpoint_quarantined(endpoint).await {
|
||||||
|
pool.stats
|
||||||
|
.increment_me_single_endpoint_shadow_rotate_skipped_quarantine_total();
|
||||||
|
shadow_rotate_deadline.insert(key, now + Duration::from_secs(SHADOW_ROTATE_RETRY_SECS));
|
||||||
|
debug!(
|
||||||
|
dc = %dc,
|
||||||
|
?family,
|
||||||
|
%endpoint,
|
||||||
|
"Single-endpoint shadow rotation skipped: endpoint is quarantined"
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let Some(writer_ids) = live_writer_ids_by_addr.get(&endpoint) else {
|
||||||
|
shadow_rotate_deadline.insert(key, now + Duration::from_secs(SHADOW_ROTATE_RETRY_SECS));
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut candidate_writer_id = None;
|
||||||
|
for writer_id in writer_ids {
|
||||||
|
if pool.registry.is_writer_empty(*writer_id).await {
|
||||||
|
candidate_writer_id = Some(*writer_id);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let Some(old_writer_id) = candidate_writer_id else {
|
||||||
|
shadow_rotate_deadline.insert(key, now + Duration::from_secs(SHADOW_ROTATE_RETRY_SECS));
|
||||||
|
debug!(
|
||||||
|
dc = %dc,
|
||||||
|
?family,
|
||||||
|
%endpoint,
|
||||||
|
alive,
|
||||||
|
required,
|
||||||
|
"Single-endpoint shadow rotation skipped: no empty writer candidate"
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
let rotate_ok = match tokio::time::timeout(pool.me_one_timeout, pool.connect_one(endpoint, rng.as_ref())).await {
|
||||||
|
Ok(Ok(())) => true,
|
||||||
|
Ok(Err(e)) => {
|
||||||
|
debug!(
|
||||||
|
dc = %dc,
|
||||||
|
?family,
|
||||||
|
%endpoint,
|
||||||
|
error = %e,
|
||||||
|
"Single-endpoint shadow rotation connect failed"
|
||||||
|
);
|
||||||
|
false
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
debug!(
|
||||||
|
dc = %dc,
|
||||||
|
?family,
|
||||||
|
%endpoint,
|
||||||
|
"Single-endpoint shadow rotation connect timed out"
|
||||||
|
);
|
||||||
|
false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if !rotate_ok {
|
||||||
|
shadow_rotate_deadline.insert(
|
||||||
|
key,
|
||||||
|
now + interval.min(Duration::from_secs(SHADOW_ROTATE_RETRY_SECS)),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
pool.mark_writer_draining_with_timeout(old_writer_id, pool.force_close_timeout(), false)
|
||||||
|
.await;
|
||||||
|
pool.stats.increment_me_single_endpoint_shadow_rotate_total();
|
||||||
|
shadow_rotate_deadline.insert(key, now + interval);
|
||||||
|
info!(
|
||||||
|
dc = %dc,
|
||||||
|
?family,
|
||||||
|
%endpoint,
|
||||||
|
old_writer_id,
|
||||||
|
rotate_every_secs = interval.as_secs(),
|
||||||
|
"Single-endpoint shadow writer rotated"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,31 +1,37 @@
|
|||||||
//! Middle Proxy RPC transport.
|
//! Middle Proxy RPC transport.
|
||||||
|
|
||||||
mod codec;
|
mod codec;
|
||||||
|
mod config_updater;
|
||||||
mod handshake;
|
mod handshake;
|
||||||
mod health;
|
mod health;
|
||||||
mod pool;
|
mod pool;
|
||||||
|
mod pool_config;
|
||||||
|
mod pool_init;
|
||||||
mod pool_nat;
|
mod pool_nat;
|
||||||
|
mod pool_refill;
|
||||||
|
mod pool_reinit;
|
||||||
|
mod pool_writer;
|
||||||
mod ping;
|
mod ping;
|
||||||
mod reader;
|
mod reader;
|
||||||
mod registry;
|
mod registry;
|
||||||
|
mod rotation;
|
||||||
mod send;
|
mod send;
|
||||||
mod secret;
|
mod secret;
|
||||||
mod rotation;
|
|
||||||
mod config_updater;
|
|
||||||
mod wire;
|
mod wire;
|
||||||
|
mod pool_status;
|
||||||
|
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
|
|
||||||
pub use health::me_health_monitor;
|
pub use health::me_health_monitor;
|
||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
pub use ping::{run_me_ping, format_sample_line, MePingReport, MePingSample, MePingFamily};
|
pub use ping::{run_me_ping, format_sample_line, format_me_route, MePingReport, MePingSample, MePingFamily};
|
||||||
pub use pool::MePool;
|
pub use pool::MePool;
|
||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
pub use pool_nat::{stun_probe, detect_public_ip};
|
pub use pool_nat::{stun_probe, detect_public_ip};
|
||||||
pub use registry::ConnRegistry;
|
pub use registry::ConnRegistry;
|
||||||
pub use secret::fetch_proxy_secret;
|
pub use secret::fetch_proxy_secret;
|
||||||
pub use config_updater::{fetch_proxy_config, me_config_updater};
|
pub use config_updater::{fetch_proxy_config, me_config_updater};
|
||||||
pub use rotation::me_rotation_task;
|
pub use rotation::{MeReinitTrigger, me_reinit_scheduler, me_rotation_task};
|
||||||
pub use wire::proto_flags_for_tag;
|
pub use wire::proto_flags_for_tag;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|||||||
@@ -2,8 +2,12 @@ use std::collections::HashMap;
|
|||||||
use std::net::{IpAddr, SocketAddr};
|
use std::net::{IpAddr, SocketAddr};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use tokio::net::UdpSocket;
|
||||||
|
|
||||||
|
use crate::config::{UpstreamConfig, UpstreamType};
|
||||||
use crate::crypto::SecureRandom;
|
use crate::crypto::SecureRandom;
|
||||||
use crate::error::ProxyError;
|
use crate::error::ProxyError;
|
||||||
|
use crate::transport::{UpstreamEgressInfo, UpstreamRouteKind};
|
||||||
|
|
||||||
use super::MePool;
|
use super::MePool;
|
||||||
|
|
||||||
@@ -17,6 +21,7 @@ pub enum MePingFamily {
|
|||||||
pub struct MePingSample {
|
pub struct MePingSample {
|
||||||
pub dc: i32,
|
pub dc: i32,
|
||||||
pub addr: SocketAddr,
|
pub addr: SocketAddr,
|
||||||
|
pub route: Option<String>,
|
||||||
pub connect_ms: Option<f64>,
|
pub connect_ms: Option<f64>,
|
||||||
pub handshake_ms: Option<f64>,
|
pub handshake_ms: Option<f64>,
|
||||||
pub error: Option<String>,
|
pub error: Option<String>,
|
||||||
@@ -50,6 +55,208 @@ pub fn format_sample_line(sample: &MePingSample) -> String {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn format_direct_with_config(
|
||||||
|
interface: &Option<String>,
|
||||||
|
bind_addresses: &Option<Vec<String>>,
|
||||||
|
) -> Option<String> {
|
||||||
|
let mut direct_parts: Vec<String> = Vec::new();
|
||||||
|
if let Some(dev) = interface.as_deref().filter(|v| !v.is_empty()) {
|
||||||
|
direct_parts.push(format!("dev={dev}"));
|
||||||
|
}
|
||||||
|
if let Some(src) = bind_addresses.as_ref().filter(|v| !v.is_empty()) {
|
||||||
|
direct_parts.push(format!("src={}", src.join(",")));
|
||||||
|
}
|
||||||
|
if direct_parts.is_empty() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(format!("direct {}", direct_parts.join(" ")))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pick_target_for_family(reports: &[MePingReport], family: MePingFamily) -> Option<SocketAddr> {
|
||||||
|
reports.iter().find_map(|report| {
|
||||||
|
if report.family != family {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
report
|
||||||
|
.samples
|
||||||
|
.iter()
|
||||||
|
.find(|s| s.error.is_none() && s.handshake_ms.is_some())
|
||||||
|
.map(|s| s.addr)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn route_from_egress(egress: Option<UpstreamEgressInfo>) -> Option<String> {
|
||||||
|
let info = egress?;
|
||||||
|
match info.route_kind {
|
||||||
|
UpstreamRouteKind::Direct => {
|
||||||
|
let src_ip = info
|
||||||
|
.direct_bind_ip
|
||||||
|
.or_else(|| info.local_addr.map(|addr| addr.ip()));
|
||||||
|
let ip = src_ip?;
|
||||||
|
let mut parts = Vec::new();
|
||||||
|
if let Some(dev) = detect_interface_for_ip(ip) {
|
||||||
|
parts.push(format!("dev={dev}"));
|
||||||
|
}
|
||||||
|
parts.push(format!("src={ip}"));
|
||||||
|
Some(format!("direct {}", parts.join(" ")))
|
||||||
|
}
|
||||||
|
UpstreamRouteKind::Socks4 => {
|
||||||
|
let route = info
|
||||||
|
.socks_proxy_addr
|
||||||
|
.map(|addr| format!("socks4://{addr}"))
|
||||||
|
.unwrap_or_else(|| "socks4://unknown".to_string());
|
||||||
|
Some(match info.socks_bound_addr {
|
||||||
|
Some(bound) => format!("{route} bnd={bound}"),
|
||||||
|
None => route,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
UpstreamRouteKind::Socks5 => {
|
||||||
|
let route = info
|
||||||
|
.socks_proxy_addr
|
||||||
|
.map(|addr| format!("socks5://{addr}"))
|
||||||
|
.unwrap_or_else(|| "socks5://unknown".to_string());
|
||||||
|
Some(match info.socks_bound_addr {
|
||||||
|
Some(bound) => format!("{route} bnd={bound}"),
|
||||||
|
None => route,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
|
fn detect_interface_for_ip(ip: IpAddr) -> Option<String> {
|
||||||
|
use nix::ifaddrs::getifaddrs;
|
||||||
|
|
||||||
|
if let Ok(addrs) = getifaddrs() {
|
||||||
|
for iface in addrs {
|
||||||
|
if let Some(address) = iface.address {
|
||||||
|
if let Some(v4) = address.as_sockaddr_in() {
|
||||||
|
if IpAddr::V4(v4.ip()) == ip {
|
||||||
|
return Some(iface.interface_name);
|
||||||
|
}
|
||||||
|
} else if let Some(v6) = address.as_sockaddr_in6() {
|
||||||
|
if IpAddr::V6(v6.ip()) == ip {
|
||||||
|
return Some(iface.interface_name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(unix))]
|
||||||
|
fn detect_interface_for_ip(_ip: IpAddr) -> Option<String> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn detect_direct_route_details(
|
||||||
|
reports: &[MePingReport],
|
||||||
|
prefer_ipv6: bool,
|
||||||
|
v4_ok: bool,
|
||||||
|
v6_ok: bool,
|
||||||
|
) -> Option<String> {
|
||||||
|
let target_addr = if prefer_ipv6 && v6_ok {
|
||||||
|
pick_target_for_family(reports, MePingFamily::V6)
|
||||||
|
.or_else(|| pick_target_for_family(reports, MePingFamily::V4))
|
||||||
|
} else if v4_ok {
|
||||||
|
pick_target_for_family(reports, MePingFamily::V4)
|
||||||
|
.or_else(|| pick_target_for_family(reports, MePingFamily::V6))
|
||||||
|
} else {
|
||||||
|
pick_target_for_family(reports, MePingFamily::V6)
|
||||||
|
.or_else(|| pick_target_for_family(reports, MePingFamily::V4))
|
||||||
|
}?;
|
||||||
|
|
||||||
|
let local_ip = if target_addr.is_ipv4() {
|
||||||
|
let sock = UdpSocket::bind("0.0.0.0:0").await.ok()?;
|
||||||
|
sock.connect(target_addr).await.ok()?;
|
||||||
|
sock.local_addr().ok().map(|a| a.ip())
|
||||||
|
} else {
|
||||||
|
let sock = UdpSocket::bind("[::]:0").await.ok()?;
|
||||||
|
sock.connect(target_addr).await.ok()?;
|
||||||
|
sock.local_addr().ok().map(|a| a.ip())
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut parts = Vec::new();
|
||||||
|
if let Some(ip) = local_ip {
|
||||||
|
if let Some(dev) = detect_interface_for_ip(ip) {
|
||||||
|
parts.push(format!("dev={dev}"));
|
||||||
|
}
|
||||||
|
parts.push(format!("src={ip}"));
|
||||||
|
}
|
||||||
|
|
||||||
|
if parts.is_empty() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(format!("direct {}", parts.join(" ")))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn format_me_route(
|
||||||
|
upstreams: &[UpstreamConfig],
|
||||||
|
reports: &[MePingReport],
|
||||||
|
prefer_ipv6: bool,
|
||||||
|
v4_ok: bool,
|
||||||
|
v6_ok: bool,
|
||||||
|
) -> String {
|
||||||
|
if let Some(route) = reports
|
||||||
|
.iter()
|
||||||
|
.flat_map(|report| report.samples.iter())
|
||||||
|
.find(|sample| sample.error.is_none() && sample.handshake_ms.is_some())
|
||||||
|
.and_then(|sample| sample.route.clone())
|
||||||
|
{
|
||||||
|
return route;
|
||||||
|
}
|
||||||
|
|
||||||
|
let enabled_upstreams: Vec<_> = upstreams.iter().filter(|u| u.enabled).collect();
|
||||||
|
if enabled_upstreams.is_empty() {
|
||||||
|
return detect_direct_route_details(reports, prefer_ipv6, v4_ok, v6_ok)
|
||||||
|
.await
|
||||||
|
.unwrap_or_else(|| "direct".to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
if enabled_upstreams.len() == 1 {
|
||||||
|
return match &enabled_upstreams[0].upstream_type {
|
||||||
|
UpstreamType::Direct {
|
||||||
|
interface,
|
||||||
|
bind_addresses,
|
||||||
|
} => {
|
||||||
|
if let Some(route) = format_direct_with_config(interface, bind_addresses) {
|
||||||
|
route
|
||||||
|
} else {
|
||||||
|
detect_direct_route_details(reports, prefer_ipv6, v4_ok, v6_ok)
|
||||||
|
.await
|
||||||
|
.unwrap_or_else(|| "direct".to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
UpstreamType::Socks4 { address, .. } => format!("socks4://{address}"),
|
||||||
|
UpstreamType::Socks5 { address, .. } => format!("socks5://{address}"),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
let has_direct = enabled_upstreams
|
||||||
|
.iter()
|
||||||
|
.any(|u| matches!(u.upstream_type, UpstreamType::Direct { .. }));
|
||||||
|
let has_socks4 = enabled_upstreams
|
||||||
|
.iter()
|
||||||
|
.any(|u| matches!(u.upstream_type, UpstreamType::Socks4 { .. }));
|
||||||
|
let has_socks5 = enabled_upstreams
|
||||||
|
.iter()
|
||||||
|
.any(|u| matches!(u.upstream_type, UpstreamType::Socks5 { .. }));
|
||||||
|
let mut kinds = Vec::new();
|
||||||
|
if has_direct {
|
||||||
|
kinds.push("direct");
|
||||||
|
}
|
||||||
|
if has_socks4 {
|
||||||
|
kinds.push("socks4");
|
||||||
|
}
|
||||||
|
if has_socks5 {
|
||||||
|
kinds.push("socks5");
|
||||||
|
}
|
||||||
|
format!("mixed upstreams ({})", kinds.join(", "))
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
@@ -64,6 +271,7 @@ mod tests {
|
|||||||
let s = sample(MePingSample {
|
let s = sample(MePingSample {
|
||||||
dc: 4,
|
dc: 4,
|
||||||
addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(1, 2, 3, 4)), 8888),
|
addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(1, 2, 3, 4)), 8888),
|
||||||
|
route: Some("direct src=1.2.3.4".to_string()),
|
||||||
connect_ms: Some(12.3),
|
connect_ms: Some(12.3),
|
||||||
handshake_ms: Some(34.7),
|
handshake_ms: Some(34.7),
|
||||||
error: None,
|
error: None,
|
||||||
@@ -80,6 +288,7 @@ mod tests {
|
|||||||
let s = sample(MePingSample {
|
let s = sample(MePingSample {
|
||||||
dc: -5,
|
dc: -5,
|
||||||
addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(5, 6, 7, 8)), 80),
|
addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(5, 6, 7, 8)), 80),
|
||||||
|
route: Some("socks5".to_string()),
|
||||||
connect_ms: Some(10.0),
|
connect_ms: Some(10.0),
|
||||||
handshake_ms: None,
|
handshake_ms: None,
|
||||||
error: Some("handshake timeout".to_string()),
|
error: Some("handshake timeout".to_string()),
|
||||||
@@ -120,11 +329,13 @@ pub async fn run_me_ping(pool: &Arc<MePool>, rng: &SecureRandom) -> Vec<MePingRe
|
|||||||
let mut connect_ms = None;
|
let mut connect_ms = None;
|
||||||
let mut handshake_ms = None;
|
let mut handshake_ms = None;
|
||||||
let mut error = None;
|
let mut error = None;
|
||||||
|
let mut route = None;
|
||||||
|
|
||||||
match pool.connect_tcp(addr).await {
|
match pool.connect_tcp(addr).await {
|
||||||
Ok((stream, conn_rtt)) => {
|
Ok((stream, conn_rtt, upstream_egress)) => {
|
||||||
connect_ms = Some(conn_rtt);
|
connect_ms = Some(conn_rtt);
|
||||||
match pool.handshake_only(stream, addr, rng).await {
|
route = route_from_egress(upstream_egress);
|
||||||
|
match pool.handshake_only(stream, addr, upstream_egress, rng).await {
|
||||||
Ok(hs) => {
|
Ok(hs) => {
|
||||||
handshake_ms = Some(hs.handshake_ms);
|
handshake_ms = Some(hs.handshake_ms);
|
||||||
// drop halves to close
|
// drop halves to close
|
||||||
@@ -144,6 +355,7 @@ pub async fn run_me_ping(pool: &Arc<MePool>, rng: &SecureRandom) -> Vec<MePingRe
|
|||||||
samples.push(MePingSample {
|
samples.push(MePingSample {
|
||||||
dc,
|
dc,
|
||||||
addr,
|
addr,
|
||||||
|
route,
|
||||||
connect_ms,
|
connect_ms,
|
||||||
handshake_ms,
|
handshake_ms,
|
||||||
error,
|
error,
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
113
src/transport/middle_proxy/pool_config.rs
Normal file
113
src/transport/middle_proxy/pool_config.rs
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
use std::net::IpAddr;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use tracing::warn;
|
||||||
|
|
||||||
|
use super::pool::MePool;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub enum SnapshotApplyOutcome {
|
||||||
|
AppliedChanged,
|
||||||
|
AppliedNoDelta,
|
||||||
|
RejectedEmpty,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SnapshotApplyOutcome {
|
||||||
|
pub fn changed(self) -> bool {
|
||||||
|
matches!(self, SnapshotApplyOutcome::AppliedChanged)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MePool {
|
||||||
|
pub async fn update_proxy_maps(
|
||||||
|
&self,
|
||||||
|
new_v4: HashMap<i32, Vec<(IpAddr, u16)>>,
|
||||||
|
new_v6: Option<HashMap<i32, Vec<(IpAddr, u16)>>>,
|
||||||
|
) -> SnapshotApplyOutcome {
|
||||||
|
if new_v4.is_empty() && new_v6.as_ref().is_none_or(|v| v.is_empty()) {
|
||||||
|
return SnapshotApplyOutcome::RejectedEmpty;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut changed = false;
|
||||||
|
{
|
||||||
|
let mut guard = self.proxy_map_v4.write().await;
|
||||||
|
if !new_v4.is_empty() && *guard != new_v4 {
|
||||||
|
*guard = new_v4;
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let Some(v6) = new_v6 {
|
||||||
|
let mut guard = self.proxy_map_v6.write().await;
|
||||||
|
if !v6.is_empty() && *guard != v6 {
|
||||||
|
*guard = v6;
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Ensure negative DC entries mirror positives when absent (Telegram convention).
|
||||||
|
{
|
||||||
|
let mut guard = self.proxy_map_v4.write().await;
|
||||||
|
let keys: Vec<i32> = guard.keys().cloned().collect();
|
||||||
|
for k in keys.iter().cloned().filter(|k| *k > 0) {
|
||||||
|
if !guard.contains_key(&-k)
|
||||||
|
&& let Some(addrs) = guard.get(&k).cloned()
|
||||||
|
{
|
||||||
|
guard.insert(-k, addrs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
let mut guard = self.proxy_map_v6.write().await;
|
||||||
|
let keys: Vec<i32> = guard.keys().cloned().collect();
|
||||||
|
for k in keys.iter().cloned().filter(|k| *k > 0) {
|
||||||
|
if !guard.contains_key(&-k)
|
||||||
|
&& let Some(addrs) = guard.get(&k).cloned()
|
||||||
|
{
|
||||||
|
guard.insert(-k, addrs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if changed {
|
||||||
|
SnapshotApplyOutcome::AppliedChanged
|
||||||
|
} else {
|
||||||
|
SnapshotApplyOutcome::AppliedNoDelta
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn update_secret(self: &Arc<Self>, new_secret: Vec<u8>) -> bool {
|
||||||
|
if new_secret.len() < 32 {
|
||||||
|
warn!(len = new_secret.len(), "proxy-secret update ignored (too short)");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
let mut guard = self.proxy_secret.write().await;
|
||||||
|
if guard.secret != new_secret {
|
||||||
|
guard.secret = new_secret;
|
||||||
|
guard.key_selector = if guard.secret.len() >= 4 {
|
||||||
|
u32::from_le_bytes([
|
||||||
|
guard.secret[0],
|
||||||
|
guard.secret[1],
|
||||||
|
guard.secret[2],
|
||||||
|
guard.secret[3],
|
||||||
|
])
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
};
|
||||||
|
guard.epoch = guard.epoch.saturating_add(1);
|
||||||
|
drop(guard);
|
||||||
|
self.reconnect_all().await;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn reconnect_all(self: &Arc<Self>) {
|
||||||
|
let ws = self.writers.read().await.clone();
|
||||||
|
for w in ws {
|
||||||
|
if let Ok(()) = self.connect_one(w.addr, self.rng.as_ref()).await {
|
||||||
|
self.mark_writer_draining(w.id).await;
|
||||||
|
tokio::time::sleep(Duration::from_secs(2)).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
201
src/transport/middle_proxy/pool_init.rs
Normal file
201
src/transport/middle_proxy/pool_init.rs
Normal file
@@ -0,0 +1,201 @@
|
|||||||
|
use std::collections::{HashMap, HashSet};
|
||||||
|
use std::net::{IpAddr, SocketAddr};
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use rand::Rng;
|
||||||
|
use rand::seq::SliceRandom;
|
||||||
|
use tracing::{debug, info, warn};
|
||||||
|
|
||||||
|
use crate::crypto::SecureRandom;
|
||||||
|
use crate::error::{ProxyError, Result};
|
||||||
|
|
||||||
|
use super::pool::MePool;
|
||||||
|
|
||||||
|
impl MePool {
|
||||||
|
pub async fn init(self: &Arc<Self>, pool_size: usize, rng: &Arc<SecureRandom>) -> Result<()> {
|
||||||
|
let family_order = self.family_order();
|
||||||
|
let ks = self.key_selector().await;
|
||||||
|
info!(
|
||||||
|
me_servers = self.proxy_map_v4.read().await.len(),
|
||||||
|
pool_size,
|
||||||
|
key_selector = format_args!("0x{ks:08x}"),
|
||||||
|
secret_len = self.proxy_secret.read().await.secret.len(),
|
||||||
|
"Initializing ME pool"
|
||||||
|
);
|
||||||
|
|
||||||
|
for family in family_order {
|
||||||
|
let map = self.proxy_map_for_family(family).await;
|
||||||
|
let mut grouped_dc_addrs: HashMap<i32, Vec<(IpAddr, u16)>> = HashMap::new();
|
||||||
|
for (dc, addrs) in map {
|
||||||
|
if addrs.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
grouped_dc_addrs.entry(dc.abs()).or_default().extend(addrs);
|
||||||
|
}
|
||||||
|
let mut dc_addrs: Vec<(i32, Vec<(IpAddr, u16)>)> = grouped_dc_addrs
|
||||||
|
.into_iter()
|
||||||
|
.map(|(dc, mut addrs)| {
|
||||||
|
addrs.sort_unstable();
|
||||||
|
addrs.dedup();
|
||||||
|
(dc, addrs)
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
dc_addrs.sort_unstable_by_key(|(dc, _)| *dc);
|
||||||
|
|
||||||
|
// Ensure at least one live writer per DC group; run missing DCs in parallel.
|
||||||
|
let mut join = tokio::task::JoinSet::new();
|
||||||
|
for (dc, addrs) in dc_addrs.iter().cloned() {
|
||||||
|
if addrs.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let endpoints: HashSet<SocketAddr> = addrs
|
||||||
|
.iter()
|
||||||
|
.map(|(ip, port)| SocketAddr::new(*ip, *port))
|
||||||
|
.collect();
|
||||||
|
if self.active_writer_count_for_endpoints(&endpoints).await > 0 {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let pool = Arc::clone(self);
|
||||||
|
let rng_clone = Arc::clone(rng);
|
||||||
|
join.spawn(async move { pool.connect_primary_for_dc(dc, addrs, rng_clone).await });
|
||||||
|
}
|
||||||
|
while join.join_next().await.is_some() {}
|
||||||
|
|
||||||
|
let mut missing_dcs = Vec::new();
|
||||||
|
for (dc, addrs) in &dc_addrs {
|
||||||
|
let endpoints: HashSet<SocketAddr> = addrs
|
||||||
|
.iter()
|
||||||
|
.map(|(ip, port)| SocketAddr::new(*ip, *port))
|
||||||
|
.collect();
|
||||||
|
if self.active_writer_count_for_endpoints(&endpoints).await == 0 {
|
||||||
|
missing_dcs.push(*dc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !missing_dcs.is_empty() {
|
||||||
|
return Err(ProxyError::Proxy(format!(
|
||||||
|
"ME init incomplete: no live writers for DC groups {missing_dcs:?}"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Warm reserve writers asynchronously so startup does not block after first working pool is ready.
|
||||||
|
let pool = Arc::clone(self);
|
||||||
|
let rng_clone = Arc::clone(rng);
|
||||||
|
let dc_addrs_bg = dc_addrs.clone();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
if pool.me_warmup_stagger_enabled {
|
||||||
|
for (dc, addrs) in &dc_addrs_bg {
|
||||||
|
for (ip, port) in addrs {
|
||||||
|
if pool.connection_count() >= pool_size {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let addr = SocketAddr::new(*ip, *port);
|
||||||
|
let jitter = rand::rng()
|
||||||
|
.random_range(0..=pool.me_warmup_step_jitter.as_millis() as u64);
|
||||||
|
let delay_ms = pool.me_warmup_step_delay.as_millis() as u64 + jitter;
|
||||||
|
tokio::time::sleep(std::time::Duration::from_millis(delay_ms)).await;
|
||||||
|
if let Err(e) = pool.connect_one(addr, rng_clone.as_ref()).await {
|
||||||
|
debug!(%addr, dc = %dc, error = %e, "Extra ME connect failed (staggered)");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (dc, addrs) in &dc_addrs_bg {
|
||||||
|
for (ip, port) in addrs {
|
||||||
|
if pool.connection_count() >= pool_size {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let addr = SocketAddr::new(*ip, *port);
|
||||||
|
if let Err(e) = pool.connect_one(addr, rng_clone.as_ref()).await {
|
||||||
|
debug!(%addr, dc = %dc, error = %e, "Extra ME connect failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if pool.connection_count() >= pool_size {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
debug!(
|
||||||
|
target_pool_size = pool_size,
|
||||||
|
current_pool_size = pool.connection_count(),
|
||||||
|
"Background ME reserve warmup finished"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
if !self.decision.effective_multipath && self.connection_count() > 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.writers.read().await.is_empty() {
|
||||||
|
return Err(ProxyError::Proxy("No ME connections".into()));
|
||||||
|
}
|
||||||
|
info!(
|
||||||
|
active_writers = self.connection_count(),
|
||||||
|
"ME primary pool ready; reserve warmup continues in background"
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn connect_primary_for_dc(
|
||||||
|
self: Arc<Self>,
|
||||||
|
dc: i32,
|
||||||
|
mut addrs: Vec<(IpAddr, u16)>,
|
||||||
|
rng: Arc<SecureRandom>,
|
||||||
|
) -> bool {
|
||||||
|
if addrs.is_empty() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
addrs.shuffle(&mut rand::rng());
|
||||||
|
if addrs.len() > 1 {
|
||||||
|
let concurrency = 2usize;
|
||||||
|
let mut join = tokio::task::JoinSet::new();
|
||||||
|
let mut next_idx = 0usize;
|
||||||
|
|
||||||
|
while next_idx < addrs.len() || !join.is_empty() {
|
||||||
|
while next_idx < addrs.len() && join.len() < concurrency {
|
||||||
|
let (ip, port) = addrs[next_idx];
|
||||||
|
next_idx += 1;
|
||||||
|
let addr = SocketAddr::new(ip, port);
|
||||||
|
let pool = Arc::clone(&self);
|
||||||
|
let rng_clone = Arc::clone(&rng);
|
||||||
|
join.spawn(async move {
|
||||||
|
(addr, pool.connect_one(addr, rng_clone.as_ref()).await)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let Some(res) = join.join_next().await else {
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
match res {
|
||||||
|
Ok((addr, Ok(()))) => {
|
||||||
|
info!(%addr, dc = %dc, "ME connected");
|
||||||
|
join.abort_all();
|
||||||
|
while join.join_next().await.is_some() {}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
Ok((addr, Err(e))) => {
|
||||||
|
warn!(%addr, dc = %dc, error = %e, "ME connect failed, trying next");
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
warn!(dc = %dc, error = %e, "ME connect task failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
warn!(dc = %dc, "All ME servers for DC failed at init");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (ip, port) in addrs {
|
||||||
|
let addr = SocketAddr::new(ip, port);
|
||||||
|
match self.connect_one(addr, rng.as_ref()).await {
|
||||||
|
Ok(()) => {
|
||||||
|
info!(%addr, dc = %dc, "ME connected");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
Err(e) => warn!(%addr, dc = %dc, error = %e, "ME connect failed, trying next"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
warn!(dc = %dc, "All ME servers for DC failed at init");
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,18 +1,31 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
use std::net::{IpAddr, Ipv4Addr};
|
use std::net::{IpAddr, Ipv4Addr};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use tracing::{info, warn};
|
use tokio::task::JoinSet;
|
||||||
|
use tokio::time::timeout;
|
||||||
|
use tracing::{debug, info, warn};
|
||||||
|
|
||||||
use crate::error::{ProxyError, Result};
|
use crate::error::{ProxyError, Result};
|
||||||
use crate::network::probe::is_bogon;
|
use crate::network::probe::is_bogon;
|
||||||
use crate::network::stun::{stun_probe_dual, IpFamily, StunProbeResult};
|
use crate::network::stun::{stun_probe_dual, stun_probe_family_with_bind, IpFamily};
|
||||||
|
|
||||||
use super::MePool;
|
use super::MePool;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
|
const STUN_BATCH_TIMEOUT: Duration = Duration::from_secs(5);
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub async fn stun_probe(stun_addr: Option<String>) -> Result<crate::network::stun::DualStunResult> {
|
pub async fn stun_probe(stun_addr: Option<String>) -> Result<crate::network::stun::DualStunResult> {
|
||||||
let stun_addr = stun_addr.unwrap_or_else(|| "stun.l.google.com:19302".to_string());
|
let stun_addr = stun_addr.unwrap_or_else(|| {
|
||||||
|
crate::config::defaults::default_stun_servers()
|
||||||
|
.into_iter()
|
||||||
|
.next()
|
||||||
|
.unwrap_or_default()
|
||||||
|
});
|
||||||
|
if stun_addr.is_empty() {
|
||||||
|
return Err(ProxyError::Proxy("STUN server is not configured".to_string()));
|
||||||
|
}
|
||||||
stun_probe_dual(&stun_addr).await
|
stun_probe_dual(&stun_addr).await
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -22,6 +35,101 @@ pub async fn detect_public_ip() -> Option<IpAddr> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl MePool {
|
impl MePool {
|
||||||
|
fn configured_stun_servers(&self) -> Vec<String> {
|
||||||
|
if !self.nat_stun_servers.is_empty() {
|
||||||
|
return self.nat_stun_servers.clone();
|
||||||
|
}
|
||||||
|
if let Some(s) = &self.nat_stun
|
||||||
|
&& !s.trim().is_empty()
|
||||||
|
{
|
||||||
|
return vec![s.clone()];
|
||||||
|
}
|
||||||
|
Vec::new()
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn probe_stun_batch_for_family(
|
||||||
|
&self,
|
||||||
|
servers: &[String],
|
||||||
|
family: IpFamily,
|
||||||
|
attempt: u8,
|
||||||
|
bind_ip: Option<IpAddr>,
|
||||||
|
) -> (Vec<String>, Option<std::net::SocketAddr>) {
|
||||||
|
let mut join_set = JoinSet::new();
|
||||||
|
let mut next_idx = 0usize;
|
||||||
|
let mut live_servers = Vec::new();
|
||||||
|
let mut best_by_ip: HashMap<IpAddr, (usize, std::net::SocketAddr)> = HashMap::new();
|
||||||
|
let concurrency = self.nat_probe_concurrency.max(1);
|
||||||
|
|
||||||
|
while next_idx < servers.len() || !join_set.is_empty() {
|
||||||
|
while next_idx < servers.len() && join_set.len() < concurrency {
|
||||||
|
let stun_addr = servers[next_idx].clone();
|
||||||
|
next_idx += 1;
|
||||||
|
join_set.spawn(async move {
|
||||||
|
let res = timeout(
|
||||||
|
STUN_BATCH_TIMEOUT,
|
||||||
|
stun_probe_family_with_bind(&stun_addr, family, bind_ip),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
(stun_addr, res)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let Some(task) = join_set.join_next().await else {
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
|
||||||
|
match task {
|
||||||
|
Ok((stun_addr, Ok(Ok(picked)))) => {
|
||||||
|
if let Some(result) = picked {
|
||||||
|
live_servers.push(stun_addr.clone());
|
||||||
|
let entry = best_by_ip
|
||||||
|
.entry(result.reflected_addr.ip())
|
||||||
|
.or_insert((0, result.reflected_addr));
|
||||||
|
entry.0 += 1;
|
||||||
|
debug!(
|
||||||
|
local = %result.local_addr,
|
||||||
|
reflected = %result.reflected_addr,
|
||||||
|
family = ?family,
|
||||||
|
stun = %stun_addr,
|
||||||
|
"NAT probe: reflected address"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok((stun_addr, Ok(Err(e)))) => {
|
||||||
|
debug!(
|
||||||
|
error = %e,
|
||||||
|
stun = %stun_addr,
|
||||||
|
attempt = attempt + 1,
|
||||||
|
"NAT probe failed, trying next server"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Ok((stun_addr, Err(_))) => {
|
||||||
|
debug!(
|
||||||
|
stun = %stun_addr,
|
||||||
|
attempt = attempt + 1,
|
||||||
|
"NAT probe timeout, trying next server"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
debug!(
|
||||||
|
error = %e,
|
||||||
|
attempt = attempt + 1,
|
||||||
|
"NAT probe task join failed"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
live_servers.sort_unstable();
|
||||||
|
live_servers.dedup();
|
||||||
|
let best_reflected = best_by_ip
|
||||||
|
.into_values()
|
||||||
|
.max_by_key(|(count, _)| *count)
|
||||||
|
.map(|(_, addr)| addr);
|
||||||
|
|
||||||
|
(live_servers, best_reflected)
|
||||||
|
}
|
||||||
|
|
||||||
pub(super) fn translate_ip_for_nat(&self, ip: IpAddr) -> IpAddr {
|
pub(super) fn translate_ip_for_nat(&self, ip: IpAddr) -> IpAddr {
|
||||||
let nat_ip = self
|
let nat_ip = self
|
||||||
.nat_ip_cfg
|
.nat_ip_cfg
|
||||||
@@ -99,10 +207,21 @@ impl MePool {
|
|||||||
pub(super) async fn maybe_reflect_public_addr(
|
pub(super) async fn maybe_reflect_public_addr(
|
||||||
&self,
|
&self,
|
||||||
family: IpFamily,
|
family: IpFamily,
|
||||||
|
bind_ip: Option<IpAddr>,
|
||||||
) -> Option<std::net::SocketAddr> {
|
) -> Option<std::net::SocketAddr> {
|
||||||
const STUN_CACHE_TTL: Duration = Duration::from_secs(600);
|
const STUN_CACHE_TTL: Duration = Duration::from_secs(600);
|
||||||
|
let use_shared_cache = bind_ip.is_none();
|
||||||
|
if !use_shared_cache {
|
||||||
|
match (family, bind_ip) {
|
||||||
|
(IpFamily::V4, Some(IpAddr::V4(_)))
|
||||||
|
| (IpFamily::V6, Some(IpAddr::V6(_)))
|
||||||
|
| (_, None) => {}
|
||||||
|
_ => return None,
|
||||||
|
}
|
||||||
|
}
|
||||||
// Backoff window
|
// Backoff window
|
||||||
if let Some(until) = *self.stun_backoff_until.read().await
|
if use_shared_cache
|
||||||
|
&& let Some(until) = *self.stun_backoff_until.read().await
|
||||||
&& Instant::now() < until
|
&& Instant::now() < until
|
||||||
{
|
{
|
||||||
if let Ok(cache) = self.nat_reflection_cache.try_lock() {
|
if let Ok(cache) = self.nat_reflection_cache.try_lock() {
|
||||||
@@ -115,7 +234,9 @@ impl MePool {
|
|||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Ok(mut cache) = self.nat_reflection_cache.try_lock() {
|
if use_shared_cache
|
||||||
|
&& let Ok(mut cache) = self.nat_reflection_cache.try_lock()
|
||||||
|
{
|
||||||
let slot = match family {
|
let slot = match family {
|
||||||
IpFamily::V4 => &mut cache.v4,
|
IpFamily::V4 => &mut cache.v4,
|
||||||
IpFamily::V6 => &mut cache.v6,
|
IpFamily::V6 => &mut cache.v6,
|
||||||
@@ -127,42 +248,64 @@ impl MePool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let attempt = self.nat_probe_attempts.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
let attempt = if use_shared_cache {
|
||||||
let servers = if !self.nat_stun_servers.is_empty() {
|
self.nat_probe_attempts.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
|
||||||
self.nat_stun_servers.clone()
|
|
||||||
} else if let Some(s) = &self.nat_stun {
|
|
||||||
vec![s.clone()]
|
|
||||||
} else {
|
} else {
|
||||||
vec!["stun.l.google.com:19302".to_string()]
|
0
|
||||||
|
};
|
||||||
|
let configured_servers = self.configured_stun_servers();
|
||||||
|
let live_snapshot = self.nat_stun_live_servers.read().await.clone();
|
||||||
|
let primary_servers = if live_snapshot.is_empty() {
|
||||||
|
configured_servers.clone()
|
||||||
|
} else {
|
||||||
|
live_snapshot
|
||||||
};
|
};
|
||||||
|
|
||||||
for stun_addr in servers {
|
let (mut live_servers, mut selected_reflected) = self
|
||||||
match stun_probe_dual(&stun_addr).await {
|
.probe_stun_batch_for_family(&primary_servers, family, attempt, bind_ip)
|
||||||
Ok(res) => {
|
.await;
|
||||||
let picked: Option<StunProbeResult> = match family {
|
|
||||||
IpFamily::V4 => res.v4,
|
if selected_reflected.is_none() && !configured_servers.is_empty() && primary_servers != configured_servers {
|
||||||
IpFamily::V6 => res.v6,
|
let (rediscovered_live, rediscovered_reflected) = self
|
||||||
};
|
.probe_stun_batch_for_family(&configured_servers, family, attempt, bind_ip)
|
||||||
if let Some(result) = picked {
|
.await;
|
||||||
info!(local = %result.local_addr, reflected = %result.reflected_addr, family = ?family, stun = %stun_addr, "NAT probe: reflected address");
|
live_servers = rediscovered_live;
|
||||||
self.nat_probe_attempts.store(0, std::sync::atomic::Ordering::Relaxed);
|
selected_reflected = rediscovered_reflected;
|
||||||
if let Ok(mut cache) = self.nat_reflection_cache.try_lock() {
|
}
|
||||||
let slot = match family {
|
|
||||||
IpFamily::V4 => &mut cache.v4,
|
let live_server_count = live_servers.len();
|
||||||
IpFamily::V6 => &mut cache.v6,
|
if !live_servers.is_empty() {
|
||||||
};
|
*self.nat_stun_live_servers.write().await = live_servers;
|
||||||
*slot = Some((Instant::now(), result.reflected_addr));
|
} else {
|
||||||
}
|
self.nat_stun_live_servers.write().await.clear();
|
||||||
return Some(result.reflected_addr);
|
}
|
||||||
}
|
|
||||||
}
|
if let Some(reflected_addr) = selected_reflected {
|
||||||
Err(e) => {
|
if use_shared_cache {
|
||||||
warn!(error = %e, stun = %stun_addr, attempt = attempt + 1, "NAT probe failed, trying next server");
|
self.nat_probe_attempts.store(0, std::sync::atomic::Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
}
|
info!(
|
||||||
|
family = ?family,
|
||||||
|
live_servers = live_server_count,
|
||||||
|
"STUN-Quorum reached, IP: {}",
|
||||||
|
reflected_addr.ip()
|
||||||
|
);
|
||||||
|
if use_shared_cache
|
||||||
|
&& let Ok(mut cache) = self.nat_reflection_cache.try_lock()
|
||||||
|
{
|
||||||
|
let slot = match family {
|
||||||
|
IpFamily::V4 => &mut cache.v4,
|
||||||
|
IpFamily::V6 => &mut cache.v6,
|
||||||
|
};
|
||||||
|
*slot = Some((Instant::now(), reflected_addr));
|
||||||
|
}
|
||||||
|
return Some(reflected_addr);
|
||||||
|
}
|
||||||
|
|
||||||
|
if use_shared_cache {
|
||||||
|
let backoff = Duration::from_secs(60 * 2u64.pow((attempt as u32).min(6)));
|
||||||
|
*self.stun_backoff_until.write().await = Some(Instant::now() + backoff);
|
||||||
}
|
}
|
||||||
let backoff = Duration::from_secs(60 * 2u64.pow((attempt as u32).min(6)));
|
|
||||||
*self.stun_backoff_until.write().await = Some(Instant::now() + backoff);
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
336
src/transport/middle_proxy/pool_refill.rs
Normal file
336
src/transport/middle_proxy/pool_refill.rs
Normal file
@@ -0,0 +1,336 @@
|
|||||||
|
use std::collections::HashSet;
|
||||||
|
use std::net::SocketAddr;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::sync::atomic::Ordering;
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
|
use tracing::{debug, info, warn};
|
||||||
|
|
||||||
|
use crate::crypto::SecureRandom;
|
||||||
|
use crate::network::IpFamily;
|
||||||
|
|
||||||
|
use super::pool::{MePool, RefillDcKey, WriterContour};
|
||||||
|
|
||||||
|
const ME_FLAP_UPTIME_THRESHOLD_SECS: u64 = 20;
|
||||||
|
const ME_FLAP_QUARANTINE_SECS: u64 = 25;
|
||||||
|
|
||||||
|
impl MePool {
|
||||||
|
pub(super) async fn maybe_quarantine_flapping_endpoint(
|
||||||
|
&self,
|
||||||
|
addr: SocketAddr,
|
||||||
|
uptime: Duration,
|
||||||
|
) {
|
||||||
|
if uptime > Duration::from_secs(ME_FLAP_UPTIME_THRESHOLD_SECS) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let until = Instant::now() + Duration::from_secs(ME_FLAP_QUARANTINE_SECS);
|
||||||
|
let mut guard = self.endpoint_quarantine.lock().await;
|
||||||
|
guard.retain(|_, expiry| *expiry > Instant::now());
|
||||||
|
guard.insert(addr, until);
|
||||||
|
self.stats.increment_me_endpoint_quarantine_total();
|
||||||
|
warn!(
|
||||||
|
%addr,
|
||||||
|
uptime_ms = uptime.as_millis(),
|
||||||
|
quarantine_secs = ME_FLAP_QUARANTINE_SECS,
|
||||||
|
"ME endpoint temporarily quarantined due to rapid writer flap"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) async fn is_endpoint_quarantined(&self, addr: SocketAddr) -> bool {
|
||||||
|
let mut guard = self.endpoint_quarantine.lock().await;
|
||||||
|
let now = Instant::now();
|
||||||
|
guard.retain(|_, expiry| *expiry > now);
|
||||||
|
guard.contains_key(&addr)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn connectable_endpoints(&self, endpoints: &[SocketAddr]) -> Vec<SocketAddr> {
|
||||||
|
if endpoints.is_empty() {
|
||||||
|
return Vec::new();
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut guard = self.endpoint_quarantine.lock().await;
|
||||||
|
let now = Instant::now();
|
||||||
|
guard.retain(|_, expiry| *expiry > now);
|
||||||
|
|
||||||
|
let mut ready = Vec::<SocketAddr>::with_capacity(endpoints.len());
|
||||||
|
let mut earliest_quarantine: Option<(SocketAddr, Instant)> = None;
|
||||||
|
for addr in endpoints {
|
||||||
|
if let Some(expiry) = guard.get(addr).copied() {
|
||||||
|
match earliest_quarantine {
|
||||||
|
Some((_, current_expiry)) if current_expiry <= expiry => {}
|
||||||
|
_ => earliest_quarantine = Some((*addr, expiry)),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ready.push(*addr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !ready.is_empty() {
|
||||||
|
return ready;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some((addr, expiry)) = earliest_quarantine {
|
||||||
|
debug!(
|
||||||
|
%addr,
|
||||||
|
wait_ms = expiry.saturating_duration_since(now).as_millis(),
|
||||||
|
"All ME endpoints are quarantined for the DC group; retrying earliest one"
|
||||||
|
);
|
||||||
|
return vec![addr];
|
||||||
|
}
|
||||||
|
|
||||||
|
Vec::new()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) async fn has_refill_inflight_for_endpoints(&self, endpoints: &[SocketAddr]) -> bool {
|
||||||
|
if endpoints.is_empty() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let guard = self.refill_inflight.lock().await;
|
||||||
|
if endpoints.iter().any(|addr| guard.contains(addr)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let dc_keys = self.resolve_refill_dc_keys_for_endpoints(endpoints).await;
|
||||||
|
if dc_keys.is_empty() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
let guard = self.refill_inflight_dc.lock().await;
|
||||||
|
dc_keys.iter().any(|key| guard.contains(key))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn resolve_refill_dc_key_for_addr(&self, addr: SocketAddr) -> Option<RefillDcKey> {
|
||||||
|
let family = if addr.is_ipv4() {
|
||||||
|
IpFamily::V4
|
||||||
|
} else {
|
||||||
|
IpFamily::V6
|
||||||
|
};
|
||||||
|
let map = self.proxy_map_for_family(family).await;
|
||||||
|
for (dc, endpoints) in map {
|
||||||
|
if endpoints
|
||||||
|
.into_iter()
|
||||||
|
.any(|(ip, port)| SocketAddr::new(ip, port) == addr)
|
||||||
|
{
|
||||||
|
return Some(RefillDcKey {
|
||||||
|
dc: dc.abs(),
|
||||||
|
family,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn resolve_refill_dc_keys_for_endpoints(
|
||||||
|
&self,
|
||||||
|
endpoints: &[SocketAddr],
|
||||||
|
) -> HashSet<RefillDcKey> {
|
||||||
|
let mut out = HashSet::<RefillDcKey>::new();
|
||||||
|
for addr in endpoints {
|
||||||
|
if let Some(key) = self.resolve_refill_dc_key_for_addr(*addr).await {
|
||||||
|
out.insert(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) async fn connect_endpoints_round_robin(
|
||||||
|
self: &Arc<Self>,
|
||||||
|
endpoints: &[SocketAddr],
|
||||||
|
rng: &SecureRandom,
|
||||||
|
) -> bool {
|
||||||
|
self.connect_endpoints_round_robin_with_generation_contour(
|
||||||
|
endpoints,
|
||||||
|
rng,
|
||||||
|
self.current_generation(),
|
||||||
|
WriterContour::Active,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) async fn connect_endpoints_round_robin_with_generation_contour(
|
||||||
|
self: &Arc<Self>,
|
||||||
|
endpoints: &[SocketAddr],
|
||||||
|
rng: &SecureRandom,
|
||||||
|
generation: u64,
|
||||||
|
contour: WriterContour,
|
||||||
|
) -> bool {
|
||||||
|
let candidates = self.connectable_endpoints(endpoints).await;
|
||||||
|
if candidates.is_empty() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
let start = (self.rr.fetch_add(1, Ordering::Relaxed) as usize) % candidates.len();
|
||||||
|
for offset in 0..candidates.len() {
|
||||||
|
let idx = (start + offset) % candidates.len();
|
||||||
|
let addr = candidates[idx];
|
||||||
|
match self
|
||||||
|
.connect_one_with_generation_contour(addr, rng, generation, contour)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(()) => return true,
|
||||||
|
Err(e) => debug!(%addr, error = %e, "ME connect failed during round-robin warmup"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn endpoints_for_same_dc(&self, addr: SocketAddr) -> Vec<SocketAddr> {
|
||||||
|
let mut target_dc = HashSet::<i32>::new();
|
||||||
|
let mut endpoints = HashSet::<SocketAddr>::new();
|
||||||
|
|
||||||
|
if self.decision.ipv4_me {
|
||||||
|
let map = self.proxy_map_v4.read().await.clone();
|
||||||
|
for (dc, addrs) in &map {
|
||||||
|
if addrs
|
||||||
|
.iter()
|
||||||
|
.any(|(ip, port)| SocketAddr::new(*ip, *port) == addr)
|
||||||
|
{
|
||||||
|
target_dc.insert(dc.abs());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for dc in &target_dc {
|
||||||
|
for key in [*dc, -*dc] {
|
||||||
|
if let Some(addrs) = map.get(&key) {
|
||||||
|
for (ip, port) in addrs {
|
||||||
|
endpoints.insert(SocketAddr::new(*ip, *port));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.decision.ipv6_me {
|
||||||
|
let map = self.proxy_map_v6.read().await.clone();
|
||||||
|
for (dc, addrs) in &map {
|
||||||
|
if addrs
|
||||||
|
.iter()
|
||||||
|
.any(|(ip, port)| SocketAddr::new(*ip, *port) == addr)
|
||||||
|
{
|
||||||
|
target_dc.insert(dc.abs());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for dc in &target_dc {
|
||||||
|
for key in [*dc, -*dc] {
|
||||||
|
if let Some(addrs) = map.get(&key) {
|
||||||
|
for (ip, port) in addrs {
|
||||||
|
endpoints.insert(SocketAddr::new(*ip, *port));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut sorted: Vec<SocketAddr> = endpoints.into_iter().collect();
|
||||||
|
sorted.sort_unstable();
|
||||||
|
sorted
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn refill_writer_after_loss(self: &Arc<Self>, addr: SocketAddr) -> bool {
|
||||||
|
let fast_retries = self.me_reconnect_fast_retry_count.max(1);
|
||||||
|
let same_endpoint_quarantined = self.is_endpoint_quarantined(addr).await;
|
||||||
|
|
||||||
|
if !same_endpoint_quarantined {
|
||||||
|
for attempt in 0..fast_retries {
|
||||||
|
self.stats.increment_me_reconnect_attempt();
|
||||||
|
match self.connect_one(addr, self.rng.as_ref()).await {
|
||||||
|
Ok(()) => {
|
||||||
|
self.stats.increment_me_reconnect_success();
|
||||||
|
self.stats.increment_me_writer_restored_same_endpoint_total();
|
||||||
|
info!(
|
||||||
|
%addr,
|
||||||
|
attempt = attempt + 1,
|
||||||
|
"ME writer restored on the same endpoint"
|
||||||
|
);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
debug!(
|
||||||
|
%addr,
|
||||||
|
attempt = attempt + 1,
|
||||||
|
error = %e,
|
||||||
|
"ME immediate same-endpoint reconnect failed"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
debug!(
|
||||||
|
%addr,
|
||||||
|
"Skipping immediate same-endpoint reconnect because endpoint is quarantined"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let dc_endpoints = self.endpoints_for_same_dc(addr).await;
|
||||||
|
if dc_endpoints.is_empty() {
|
||||||
|
self.stats.increment_me_refill_failed_total();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for attempt in 0..fast_retries {
|
||||||
|
self.stats.increment_me_reconnect_attempt();
|
||||||
|
if self
|
||||||
|
.connect_endpoints_round_robin(&dc_endpoints, self.rng.as_ref())
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
self.stats.increment_me_reconnect_success();
|
||||||
|
self.stats.increment_me_writer_restored_fallback_total();
|
||||||
|
info!(
|
||||||
|
%addr,
|
||||||
|
attempt = attempt + 1,
|
||||||
|
"ME writer restored via DC fallback endpoint"
|
||||||
|
);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.stats.increment_me_refill_failed_total();
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn trigger_immediate_refill(self: &Arc<Self>, addr: SocketAddr) {
|
||||||
|
let pool = Arc::clone(self);
|
||||||
|
tokio::spawn(async move {
|
||||||
|
let dc_endpoints = pool.endpoints_for_same_dc(addr).await;
|
||||||
|
let dc_keys = pool.resolve_refill_dc_keys_for_endpoints(&dc_endpoints).await;
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut guard = pool.refill_inflight.lock().await;
|
||||||
|
if !guard.insert(addr) {
|
||||||
|
pool.stats.increment_me_refill_skipped_inflight_total();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !dc_keys.is_empty() {
|
||||||
|
let mut dc_guard = pool.refill_inflight_dc.lock().await;
|
||||||
|
if dc_keys.iter().any(|key| dc_guard.contains(key)) {
|
||||||
|
pool.stats.increment_me_refill_skipped_inflight_total();
|
||||||
|
drop(dc_guard);
|
||||||
|
let mut guard = pool.refill_inflight.lock().await;
|
||||||
|
guard.remove(&addr);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
dc_guard.extend(dc_keys.iter().copied());
|
||||||
|
}
|
||||||
|
|
||||||
|
pool.stats.increment_me_refill_triggered_total();
|
||||||
|
|
||||||
|
let restored = pool.refill_writer_after_loss(addr).await;
|
||||||
|
if !restored {
|
||||||
|
warn!(%addr, "ME immediate refill failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut guard = pool.refill_inflight.lock().await;
|
||||||
|
guard.remove(&addr);
|
||||||
|
drop(guard);
|
||||||
|
if !dc_keys.is_empty() {
|
||||||
|
let mut dc_guard = pool.refill_inflight_dc.lock().await;
|
||||||
|
for key in &dc_keys {
|
||||||
|
dc_guard.remove(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
493
src/transport/middle_proxy/pool_reinit.rs
Normal file
493
src/transport/middle_proxy/pool_reinit.rs
Normal file
@@ -0,0 +1,493 @@
|
|||||||
|
use std::collections::{HashMap, HashSet};
|
||||||
|
use std::hash::{Hash, Hasher};
|
||||||
|
use std::net::SocketAddr;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::sync::atomic::Ordering;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use rand::Rng;
|
||||||
|
use rand::seq::SliceRandom;
|
||||||
|
use tracing::{debug, info, warn};
|
||||||
|
use std::collections::hash_map::DefaultHasher;
|
||||||
|
|
||||||
|
use crate::crypto::SecureRandom;
|
||||||
|
|
||||||
|
use super::pool::{MePool, WriterContour};
|
||||||
|
|
||||||
|
const ME_HARDSWAP_PENDING_TTL_SECS: u64 = 1800;
|
||||||
|
|
||||||
|
impl MePool {
|
||||||
|
fn desired_map_hash(desired_by_dc: &HashMap<i32, HashSet<SocketAddr>>) -> u64 {
|
||||||
|
let mut hasher = DefaultHasher::new();
|
||||||
|
let mut dcs: Vec<i32> = desired_by_dc.keys().copied().collect();
|
||||||
|
dcs.sort_unstable();
|
||||||
|
for dc in dcs {
|
||||||
|
dc.hash(&mut hasher);
|
||||||
|
let mut endpoints: Vec<SocketAddr> = desired_by_dc
|
||||||
|
.get(&dc)
|
||||||
|
.map(|set| set.iter().copied().collect())
|
||||||
|
.unwrap_or_default();
|
||||||
|
endpoints.sort_unstable();
|
||||||
|
for endpoint in endpoints {
|
||||||
|
endpoint.hash(&mut hasher);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
hasher.finish()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn clear_pending_hardswap_state(&self) {
|
||||||
|
self.pending_hardswap_generation.store(0, Ordering::Relaxed);
|
||||||
|
self.pending_hardswap_started_at_epoch_secs
|
||||||
|
.store(0, Ordering::Relaxed);
|
||||||
|
self.pending_hardswap_map_hash.store(0, Ordering::Relaxed);
|
||||||
|
self.warm_generation.store(0, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn promote_warm_generation_to_active(&self, generation: u64) {
|
||||||
|
self.active_generation.store(generation, Ordering::Relaxed);
|
||||||
|
self.warm_generation.store(0, Ordering::Relaxed);
|
||||||
|
|
||||||
|
let ws = self.writers.read().await;
|
||||||
|
for writer in ws.iter() {
|
||||||
|
if writer.draining.load(Ordering::Relaxed) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if writer.generation == generation {
|
||||||
|
writer
|
||||||
|
.contour
|
||||||
|
.store(WriterContour::Active.as_u8(), Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn coverage_ratio(
|
||||||
|
desired_by_dc: &HashMap<i32, HashSet<SocketAddr>>,
|
||||||
|
active_writer_addrs: &HashSet<SocketAddr>,
|
||||||
|
) -> (f32, Vec<i32>) {
|
||||||
|
if desired_by_dc.is_empty() {
|
||||||
|
return (1.0, Vec::new());
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut missing_dc = Vec::<i32>::new();
|
||||||
|
let mut covered = 0usize;
|
||||||
|
for (dc, endpoints) in desired_by_dc {
|
||||||
|
if endpoints.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if endpoints
|
||||||
|
.iter()
|
||||||
|
.any(|addr| active_writer_addrs.contains(addr))
|
||||||
|
{
|
||||||
|
covered += 1;
|
||||||
|
} else {
|
||||||
|
missing_dc.push(*dc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
missing_dc.sort_unstable();
|
||||||
|
let total = desired_by_dc.len().max(1);
|
||||||
|
let ratio = (covered as f32) / (total as f32);
|
||||||
|
(ratio, missing_dc)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn reconcile_connections(self: &Arc<Self>, rng: &SecureRandom) {
|
||||||
|
let writers = self.writers.read().await;
|
||||||
|
let current: HashSet<SocketAddr> = writers
|
||||||
|
.iter()
|
||||||
|
.filter(|w| !w.draining.load(Ordering::Relaxed))
|
||||||
|
.map(|w| w.addr)
|
||||||
|
.collect();
|
||||||
|
drop(writers);
|
||||||
|
|
||||||
|
for family in self.family_order() {
|
||||||
|
let map = self.proxy_map_for_family(family).await;
|
||||||
|
for (_dc, addrs) in &map {
|
||||||
|
let dc_addrs: Vec<SocketAddr> = addrs
|
||||||
|
.iter()
|
||||||
|
.map(|(ip, port)| SocketAddr::new(*ip, *port))
|
||||||
|
.collect();
|
||||||
|
if !dc_addrs.iter().any(|a| current.contains(a)) {
|
||||||
|
let mut shuffled = dc_addrs.clone();
|
||||||
|
shuffled.shuffle(&mut rand::rng());
|
||||||
|
for addr in shuffled {
|
||||||
|
if self.connect_one(addr, rng).await.is_ok() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !self.decision.effective_multipath && !current.is_empty() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn desired_dc_endpoints(&self) -> HashMap<i32, HashSet<SocketAddr>> {
|
||||||
|
let mut out: HashMap<i32, HashSet<SocketAddr>> = HashMap::new();
|
||||||
|
|
||||||
|
if self.decision.ipv4_me {
|
||||||
|
let map_v4 = self.proxy_map_v4.read().await.clone();
|
||||||
|
for (dc, addrs) in map_v4 {
|
||||||
|
let entry = out.entry(dc.abs()).or_default();
|
||||||
|
for (ip, port) in addrs {
|
||||||
|
entry.insert(SocketAddr::new(ip, port));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.decision.ipv6_me {
|
||||||
|
let map_v6 = self.proxy_map_v6.read().await.clone();
|
||||||
|
for (dc, addrs) in map_v6 {
|
||||||
|
let entry = out.entry(dc.abs()).or_default();
|
||||||
|
for (ip, port) in addrs {
|
||||||
|
entry.insert(SocketAddr::new(ip, port));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hardswap_warmup_connect_delay_ms(&self) -> u64 {
|
||||||
|
let min_ms = self.me_hardswap_warmup_delay_min_ms.load(Ordering::Relaxed);
|
||||||
|
let max_ms = self.me_hardswap_warmup_delay_max_ms.load(Ordering::Relaxed);
|
||||||
|
let (min_ms, max_ms) = if min_ms <= max_ms {
|
||||||
|
(min_ms, max_ms)
|
||||||
|
} else {
|
||||||
|
(max_ms, min_ms)
|
||||||
|
};
|
||||||
|
if min_ms == max_ms {
|
||||||
|
return min_ms;
|
||||||
|
}
|
||||||
|
rand::rng().random_range(min_ms..=max_ms)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hardswap_warmup_backoff_ms(&self, pass_idx: usize) -> u64 {
|
||||||
|
let base_ms = self
|
||||||
|
.me_hardswap_warmup_pass_backoff_base_ms
|
||||||
|
.load(Ordering::Relaxed);
|
||||||
|
let cap_ms = (self.me_reconnect_backoff_cap.as_millis() as u64).max(base_ms);
|
||||||
|
let shift = (pass_idx as u32).min(20);
|
||||||
|
let scaled = base_ms.saturating_mul(1u64 << shift);
|
||||||
|
let core = scaled.min(cap_ms);
|
||||||
|
let jitter = (core / 2).max(1);
|
||||||
|
core.saturating_add(rand::rng().random_range(0..=jitter))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn fresh_writer_count_for_endpoints(
|
||||||
|
&self,
|
||||||
|
generation: u64,
|
||||||
|
endpoints: &HashSet<SocketAddr>,
|
||||||
|
) -> usize {
|
||||||
|
let ws = self.writers.read().await;
|
||||||
|
ws.iter()
|
||||||
|
.filter(|w| !w.draining.load(Ordering::Relaxed))
|
||||||
|
.filter(|w| w.generation == generation)
|
||||||
|
.filter(|w| endpoints.contains(&w.addr))
|
||||||
|
.count()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) async fn active_writer_count_for_endpoints(
|
||||||
|
&self,
|
||||||
|
endpoints: &HashSet<SocketAddr>,
|
||||||
|
) -> usize {
|
||||||
|
let ws = self.writers.read().await;
|
||||||
|
ws.iter()
|
||||||
|
.filter(|w| !w.draining.load(Ordering::Relaxed))
|
||||||
|
.filter(|w| endpoints.contains(&w.addr))
|
||||||
|
.count()
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn warmup_generation_for_all_dcs(
|
||||||
|
self: &Arc<Self>,
|
||||||
|
rng: &SecureRandom,
|
||||||
|
generation: u64,
|
||||||
|
desired_by_dc: &HashMap<i32, HashSet<SocketAddr>>,
|
||||||
|
) {
|
||||||
|
let extra_passes = self
|
||||||
|
.me_hardswap_warmup_extra_passes
|
||||||
|
.load(Ordering::Relaxed)
|
||||||
|
.min(10) as usize;
|
||||||
|
let total_passes = 1 + extra_passes;
|
||||||
|
|
||||||
|
for (dc, endpoints) in desired_by_dc {
|
||||||
|
if endpoints.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut endpoint_list: Vec<SocketAddr> = endpoints.iter().copied().collect();
|
||||||
|
endpoint_list.sort_unstable();
|
||||||
|
let required = self.required_writers_for_dc(endpoint_list.len());
|
||||||
|
let mut completed = false;
|
||||||
|
let mut last_fresh_count = self
|
||||||
|
.fresh_writer_count_for_endpoints(generation, endpoints)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
for pass_idx in 0..total_passes {
|
||||||
|
if last_fresh_count >= required {
|
||||||
|
completed = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
let missing = required.saturating_sub(last_fresh_count);
|
||||||
|
debug!(
|
||||||
|
dc = *dc,
|
||||||
|
pass = pass_idx + 1,
|
||||||
|
total_passes,
|
||||||
|
fresh_count = last_fresh_count,
|
||||||
|
required,
|
||||||
|
missing,
|
||||||
|
endpoint_count = endpoint_list.len(),
|
||||||
|
"ME hardswap warmup pass started"
|
||||||
|
);
|
||||||
|
|
||||||
|
for attempt_idx in 0..missing {
|
||||||
|
let delay_ms = self.hardswap_warmup_connect_delay_ms();
|
||||||
|
tokio::time::sleep(Duration::from_millis(delay_ms)).await;
|
||||||
|
|
||||||
|
let connected = self
|
||||||
|
.connect_endpoints_round_robin_with_generation_contour(
|
||||||
|
&endpoint_list,
|
||||||
|
rng,
|
||||||
|
generation,
|
||||||
|
WriterContour::Warm,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
debug!(
|
||||||
|
dc = *dc,
|
||||||
|
pass = pass_idx + 1,
|
||||||
|
total_passes,
|
||||||
|
attempt = attempt_idx + 1,
|
||||||
|
delay_ms,
|
||||||
|
connected,
|
||||||
|
"ME hardswap warmup connect attempt finished"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
last_fresh_count = self
|
||||||
|
.fresh_writer_count_for_endpoints(generation, endpoints)
|
||||||
|
.await;
|
||||||
|
if last_fresh_count >= required {
|
||||||
|
completed = true;
|
||||||
|
info!(
|
||||||
|
dc = *dc,
|
||||||
|
pass = pass_idx + 1,
|
||||||
|
total_passes,
|
||||||
|
fresh_count = last_fresh_count,
|
||||||
|
required,
|
||||||
|
"ME hardswap warmup floor reached for DC"
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if pass_idx + 1 < total_passes {
|
||||||
|
let backoff_ms = self.hardswap_warmup_backoff_ms(pass_idx);
|
||||||
|
debug!(
|
||||||
|
dc = *dc,
|
||||||
|
pass = pass_idx + 1,
|
||||||
|
total_passes,
|
||||||
|
fresh_count = last_fresh_count,
|
||||||
|
required,
|
||||||
|
backoff_ms,
|
||||||
|
"ME hardswap warmup pass incomplete, delaying next pass"
|
||||||
|
);
|
||||||
|
tokio::time::sleep(Duration::from_millis(backoff_ms)).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !completed {
|
||||||
|
warn!(
|
||||||
|
dc = *dc,
|
||||||
|
fresh_count = last_fresh_count,
|
||||||
|
required,
|
||||||
|
endpoint_count = endpoint_list.len(),
|
||||||
|
total_passes,
|
||||||
|
"ME warmup stopped: unable to reach required writer floor for DC"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn zero_downtime_reinit_after_map_change(self: &Arc<Self>, rng: &SecureRandom) {
|
||||||
|
let desired_by_dc = self.desired_dc_endpoints().await;
|
||||||
|
if desired_by_dc.is_empty() {
|
||||||
|
warn!("ME endpoint map is empty; skipping stale writer drain");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let desired_map_hash = Self::desired_map_hash(&desired_by_dc);
|
||||||
|
let now_epoch_secs = Self::now_epoch_secs();
|
||||||
|
let previous_generation = self.current_generation();
|
||||||
|
let hardswap = self.hardswap.load(Ordering::Relaxed);
|
||||||
|
let generation = if hardswap {
|
||||||
|
let pending_generation = self.pending_hardswap_generation.load(Ordering::Relaxed);
|
||||||
|
let pending_started_at = self
|
||||||
|
.pending_hardswap_started_at_epoch_secs
|
||||||
|
.load(Ordering::Relaxed);
|
||||||
|
let pending_map_hash = self.pending_hardswap_map_hash.load(Ordering::Relaxed);
|
||||||
|
let pending_age_secs = now_epoch_secs.saturating_sub(pending_started_at);
|
||||||
|
let pending_ttl_expired = pending_started_at > 0 && pending_age_secs > ME_HARDSWAP_PENDING_TTL_SECS;
|
||||||
|
let pending_matches_map = pending_map_hash != 0 && pending_map_hash == desired_map_hash;
|
||||||
|
|
||||||
|
if pending_generation != 0
|
||||||
|
&& pending_generation >= previous_generation
|
||||||
|
&& pending_matches_map
|
||||||
|
&& !pending_ttl_expired
|
||||||
|
{
|
||||||
|
self.stats.increment_me_hardswap_pending_reuse_total();
|
||||||
|
debug!(
|
||||||
|
previous_generation,
|
||||||
|
generation = pending_generation,
|
||||||
|
pending_age_secs,
|
||||||
|
"ME hardswap continues with pending generation"
|
||||||
|
);
|
||||||
|
pending_generation
|
||||||
|
} else {
|
||||||
|
if pending_generation != 0 && pending_ttl_expired {
|
||||||
|
self.stats.increment_me_hardswap_pending_ttl_expired_total();
|
||||||
|
warn!(
|
||||||
|
previous_generation,
|
||||||
|
generation = pending_generation,
|
||||||
|
pending_age_secs,
|
||||||
|
pending_ttl_secs = ME_HARDSWAP_PENDING_TTL_SECS,
|
||||||
|
"ME hardswap pending generation expired by TTL; starting fresh generation"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
let next_generation = self.generation.fetch_add(1, Ordering::Relaxed) + 1;
|
||||||
|
self.pending_hardswap_generation
|
||||||
|
.store(next_generation, Ordering::Relaxed);
|
||||||
|
self.pending_hardswap_started_at_epoch_secs
|
||||||
|
.store(now_epoch_secs, Ordering::Relaxed);
|
||||||
|
self.pending_hardswap_map_hash
|
||||||
|
.store(desired_map_hash, Ordering::Relaxed);
|
||||||
|
self.warm_generation.store(next_generation, Ordering::Relaxed);
|
||||||
|
next_generation
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
self.clear_pending_hardswap_state();
|
||||||
|
self.generation.fetch_add(1, Ordering::Relaxed) + 1
|
||||||
|
};
|
||||||
|
|
||||||
|
if hardswap {
|
||||||
|
self.warm_generation.store(generation, Ordering::Relaxed);
|
||||||
|
self.warmup_generation_for_all_dcs(rng, generation, &desired_by_dc)
|
||||||
|
.await;
|
||||||
|
} else {
|
||||||
|
self.reconcile_connections(rng).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
let writers = self.writers.read().await;
|
||||||
|
let active_writer_addrs: HashSet<SocketAddr> = writers
|
||||||
|
.iter()
|
||||||
|
.filter(|w| !w.draining.load(Ordering::Relaxed))
|
||||||
|
.map(|w| w.addr)
|
||||||
|
.collect();
|
||||||
|
let min_ratio = Self::permille_to_ratio(
|
||||||
|
self.me_pool_min_fresh_ratio_permille
|
||||||
|
.load(Ordering::Relaxed),
|
||||||
|
);
|
||||||
|
let (coverage_ratio, missing_dc) = Self::coverage_ratio(&desired_by_dc, &active_writer_addrs);
|
||||||
|
if !hardswap && coverage_ratio < min_ratio {
|
||||||
|
warn!(
|
||||||
|
previous_generation,
|
||||||
|
generation,
|
||||||
|
coverage_ratio = format_args!("{coverage_ratio:.3}"),
|
||||||
|
min_ratio = format_args!("{min_ratio:.3}"),
|
||||||
|
missing_dc = ?missing_dc,
|
||||||
|
"ME reinit coverage below threshold; keeping stale writers"
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if hardswap {
|
||||||
|
let mut fresh_missing_dc = Vec::<(i32, usize, usize)>::new();
|
||||||
|
for (dc, endpoints) in &desired_by_dc {
|
||||||
|
if endpoints.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let required = self.required_writers_for_dc(endpoints.len());
|
||||||
|
let fresh_count = writers
|
||||||
|
.iter()
|
||||||
|
.filter(|w| !w.draining.load(Ordering::Relaxed))
|
||||||
|
.filter(|w| w.generation == generation)
|
||||||
|
.filter(|w| endpoints.contains(&w.addr))
|
||||||
|
.count();
|
||||||
|
if fresh_count < required {
|
||||||
|
fresh_missing_dc.push((*dc, fresh_count, required));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !fresh_missing_dc.is_empty() {
|
||||||
|
warn!(
|
||||||
|
previous_generation,
|
||||||
|
generation,
|
||||||
|
missing_dc = ?fresh_missing_dc,
|
||||||
|
"ME hardswap pending: fresh generation coverage incomplete"
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else if !missing_dc.is_empty() {
|
||||||
|
warn!(
|
||||||
|
missing_dc = ?missing_dc,
|
||||||
|
// Keep stale writers alive when fresh coverage is incomplete.
|
||||||
|
"ME reinit coverage incomplete; keeping stale writers"
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if hardswap {
|
||||||
|
self.promote_warm_generation_to_active(generation).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
let desired_addrs: HashSet<SocketAddr> = desired_by_dc
|
||||||
|
.values()
|
||||||
|
.flat_map(|set| set.iter().copied())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let stale_writer_ids: Vec<u64> = writers
|
||||||
|
.iter()
|
||||||
|
.filter(|w| !w.draining.load(Ordering::Relaxed))
|
||||||
|
.filter(|w| {
|
||||||
|
if hardswap {
|
||||||
|
w.generation < generation
|
||||||
|
} else {
|
||||||
|
!desired_addrs.contains(&w.addr)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.map(|w| w.id)
|
||||||
|
.collect();
|
||||||
|
drop(writers);
|
||||||
|
|
||||||
|
if stale_writer_ids.is_empty() {
|
||||||
|
if hardswap {
|
||||||
|
self.clear_pending_hardswap_state();
|
||||||
|
}
|
||||||
|
debug!("ME reinit cycle completed with no stale writers");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let drain_timeout = self.force_close_timeout();
|
||||||
|
let drain_timeout_secs = drain_timeout.map(|d| d.as_secs()).unwrap_or(0);
|
||||||
|
info!(
|
||||||
|
stale_writers = stale_writer_ids.len(),
|
||||||
|
previous_generation,
|
||||||
|
generation,
|
||||||
|
hardswap,
|
||||||
|
coverage_ratio = format_args!("{coverage_ratio:.3}"),
|
||||||
|
min_ratio = format_args!("{min_ratio:.3}"),
|
||||||
|
drain_timeout_secs,
|
||||||
|
"ME reinit cycle covered; draining stale writers"
|
||||||
|
);
|
||||||
|
self.stats.increment_pool_swap_total();
|
||||||
|
for writer_id in stale_writer_ids {
|
||||||
|
self.mark_writer_draining_with_timeout(writer_id, drain_timeout, !hardswap)
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
if hardswap {
|
||||||
|
self.clear_pending_hardswap_state();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn zero_downtime_reinit_periodic(self: &Arc<Self>, rng: &SecureRandom) {
|
||||||
|
self.zero_downtime_reinit_after_map_change(rng).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
424
src/transport/middle_proxy/pool_status.rs
Normal file
424
src/transport/middle_proxy/pool_status.rs
Normal file
@@ -0,0 +1,424 @@
|
|||||||
|
use std::collections::{BTreeMap, BTreeSet, HashMap};
|
||||||
|
use std::net::SocketAddr;
|
||||||
|
use std::sync::atomic::Ordering;
|
||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
|
use super::pool::{MePool, WriterContour};
|
||||||
|
use crate::config::{MeBindStaleMode, MeFloorMode, MeSocksKdfPolicy};
|
||||||
|
use crate::transport::upstream::IpPreference;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub(crate) struct MeApiWriterStatusSnapshot {
|
||||||
|
pub writer_id: u64,
|
||||||
|
pub dc: Option<i16>,
|
||||||
|
pub endpoint: SocketAddr,
|
||||||
|
pub generation: u64,
|
||||||
|
pub state: &'static str,
|
||||||
|
pub draining: bool,
|
||||||
|
pub degraded: bool,
|
||||||
|
pub bound_clients: usize,
|
||||||
|
pub idle_for_secs: Option<u64>,
|
||||||
|
pub rtt_ema_ms: Option<f64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub(crate) struct MeApiDcStatusSnapshot {
|
||||||
|
pub dc: i16,
|
||||||
|
pub endpoints: Vec<SocketAddr>,
|
||||||
|
pub available_endpoints: usize,
|
||||||
|
pub available_pct: f64,
|
||||||
|
pub required_writers: usize,
|
||||||
|
pub alive_writers: usize,
|
||||||
|
pub coverage_pct: f64,
|
||||||
|
pub rtt_ms: Option<f64>,
|
||||||
|
pub load: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub(crate) struct MeApiStatusSnapshot {
|
||||||
|
pub generated_at_epoch_secs: u64,
|
||||||
|
pub configured_dc_groups: usize,
|
||||||
|
pub configured_endpoints: usize,
|
||||||
|
pub available_endpoints: usize,
|
||||||
|
pub available_pct: f64,
|
||||||
|
pub required_writers: usize,
|
||||||
|
pub alive_writers: usize,
|
||||||
|
pub coverage_pct: f64,
|
||||||
|
pub writers: Vec<MeApiWriterStatusSnapshot>,
|
||||||
|
pub dcs: Vec<MeApiDcStatusSnapshot>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub(crate) struct MeApiQuarantinedEndpointSnapshot {
|
||||||
|
pub endpoint: SocketAddr,
|
||||||
|
pub remaining_ms: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub(crate) struct MeApiDcPathSnapshot {
|
||||||
|
pub dc: i16,
|
||||||
|
pub ip_preference: Option<&'static str>,
|
||||||
|
pub selected_addr_v4: Option<SocketAddr>,
|
||||||
|
pub selected_addr_v6: Option<SocketAddr>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub(crate) struct MeApiRuntimeSnapshot {
|
||||||
|
pub active_generation: u64,
|
||||||
|
pub warm_generation: u64,
|
||||||
|
pub pending_hardswap_generation: u64,
|
||||||
|
pub pending_hardswap_age_secs: Option<u64>,
|
||||||
|
pub hardswap_enabled: bool,
|
||||||
|
pub floor_mode: &'static str,
|
||||||
|
pub adaptive_floor_idle_secs: u64,
|
||||||
|
pub adaptive_floor_min_writers_single_endpoint: u8,
|
||||||
|
pub adaptive_floor_recover_grace_secs: u64,
|
||||||
|
pub me_keepalive_enabled: bool,
|
||||||
|
pub me_keepalive_interval_secs: u64,
|
||||||
|
pub me_keepalive_jitter_secs: u64,
|
||||||
|
pub me_keepalive_payload_random: bool,
|
||||||
|
pub rpc_proxy_req_every_secs: u64,
|
||||||
|
pub me_reconnect_max_concurrent_per_dc: u32,
|
||||||
|
pub me_reconnect_backoff_base_ms: u64,
|
||||||
|
pub me_reconnect_backoff_cap_ms: u64,
|
||||||
|
pub me_reconnect_fast_retry_count: u32,
|
||||||
|
pub me_pool_drain_ttl_secs: u64,
|
||||||
|
pub me_pool_force_close_secs: u64,
|
||||||
|
pub me_pool_min_fresh_ratio: f32,
|
||||||
|
pub me_bind_stale_mode: &'static str,
|
||||||
|
pub me_bind_stale_ttl_secs: u64,
|
||||||
|
pub me_single_endpoint_shadow_writers: u8,
|
||||||
|
pub me_single_endpoint_outage_mode_enabled: bool,
|
||||||
|
pub me_single_endpoint_outage_disable_quarantine: bool,
|
||||||
|
pub me_single_endpoint_outage_backoff_min_ms: u64,
|
||||||
|
pub me_single_endpoint_outage_backoff_max_ms: u64,
|
||||||
|
pub me_single_endpoint_shadow_rotate_every_secs: u64,
|
||||||
|
pub me_deterministic_writer_sort: bool,
|
||||||
|
pub me_socks_kdf_policy: &'static str,
|
||||||
|
pub quarantined_endpoints: Vec<MeApiQuarantinedEndpointSnapshot>,
|
||||||
|
pub network_path: Vec<MeApiDcPathSnapshot>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MePool {
|
||||||
|
pub(crate) async fn api_status_snapshot(&self) -> MeApiStatusSnapshot {
|
||||||
|
let now_epoch_secs = Self::now_epoch_secs();
|
||||||
|
|
||||||
|
let mut endpoints_by_dc = BTreeMap::<i16, BTreeSet<SocketAddr>>::new();
|
||||||
|
if self.decision.ipv4_me {
|
||||||
|
let map = self.proxy_map_v4.read().await.clone();
|
||||||
|
for (dc, addrs) in map {
|
||||||
|
let abs_dc = dc.abs();
|
||||||
|
if abs_dc == 0 {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let Ok(dc_idx) = i16::try_from(abs_dc) else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
let entry = endpoints_by_dc.entry(dc_idx).or_default();
|
||||||
|
for (ip, port) in addrs {
|
||||||
|
entry.insert(SocketAddr::new(ip, port));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if self.decision.ipv6_me {
|
||||||
|
let map = self.proxy_map_v6.read().await.clone();
|
||||||
|
for (dc, addrs) in map {
|
||||||
|
let abs_dc = dc.abs();
|
||||||
|
if abs_dc == 0 {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let Ok(dc_idx) = i16::try_from(abs_dc) else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
let entry = endpoints_by_dc.entry(dc_idx).or_default();
|
||||||
|
for (ip, port) in addrs {
|
||||||
|
entry.insert(SocketAddr::new(ip, port));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut endpoint_to_dc = HashMap::<SocketAddr, i16>::new();
|
||||||
|
for (dc, endpoints) in &endpoints_by_dc {
|
||||||
|
for endpoint in endpoints {
|
||||||
|
endpoint_to_dc.entry(*endpoint).or_insert(*dc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let configured_dc_groups = endpoints_by_dc.len();
|
||||||
|
let configured_endpoints = endpoints_by_dc.values().map(BTreeSet::len).sum();
|
||||||
|
|
||||||
|
let required_writers = endpoints_by_dc
|
||||||
|
.values()
|
||||||
|
.map(|endpoints| self.required_writers_for_dc_with_floor_mode(endpoints.len(), false))
|
||||||
|
.sum();
|
||||||
|
|
||||||
|
let idle_since = self.registry.writer_idle_since_snapshot().await;
|
||||||
|
let activity = self.registry.writer_activity_snapshot().await;
|
||||||
|
let rtt = self.rtt_stats.lock().await.clone();
|
||||||
|
let writers = self.writers.read().await.clone();
|
||||||
|
|
||||||
|
let mut live_writers_by_endpoint = HashMap::<SocketAddr, usize>::new();
|
||||||
|
let mut live_writers_by_dc = HashMap::<i16, usize>::new();
|
||||||
|
let mut dc_rtt_agg = HashMap::<i16, (f64, u64)>::new();
|
||||||
|
let mut writer_rows = Vec::<MeApiWriterStatusSnapshot>::with_capacity(writers.len());
|
||||||
|
|
||||||
|
for writer in writers {
|
||||||
|
let endpoint = writer.addr;
|
||||||
|
let dc = endpoint_to_dc.get(&endpoint).copied();
|
||||||
|
let draining = writer.draining.load(Ordering::Relaxed);
|
||||||
|
let degraded = writer.degraded.load(Ordering::Relaxed);
|
||||||
|
let bound_clients = activity
|
||||||
|
.bound_clients_by_writer
|
||||||
|
.get(&writer.id)
|
||||||
|
.copied()
|
||||||
|
.unwrap_or(0);
|
||||||
|
let idle_for_secs = idle_since
|
||||||
|
.get(&writer.id)
|
||||||
|
.map(|idle_ts| now_epoch_secs.saturating_sub(*idle_ts));
|
||||||
|
let rtt_ema_ms = rtt.get(&writer.id).map(|(_, ema)| *ema);
|
||||||
|
let state = match WriterContour::from_u8(writer.contour.load(Ordering::Relaxed)) {
|
||||||
|
WriterContour::Warm => "warm",
|
||||||
|
WriterContour::Active => "active",
|
||||||
|
WriterContour::Draining => "draining",
|
||||||
|
};
|
||||||
|
|
||||||
|
if !draining {
|
||||||
|
*live_writers_by_endpoint.entry(endpoint).or_insert(0) += 1;
|
||||||
|
if let Some(dc_idx) = dc {
|
||||||
|
*live_writers_by_dc.entry(dc_idx).or_insert(0) += 1;
|
||||||
|
if let Some(ema_ms) = rtt_ema_ms {
|
||||||
|
let entry = dc_rtt_agg.entry(dc_idx).or_insert((0.0, 0));
|
||||||
|
entry.0 += ema_ms;
|
||||||
|
entry.1 += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
writer_rows.push(MeApiWriterStatusSnapshot {
|
||||||
|
writer_id: writer.id,
|
||||||
|
dc,
|
||||||
|
endpoint,
|
||||||
|
generation: writer.generation,
|
||||||
|
state,
|
||||||
|
draining,
|
||||||
|
degraded,
|
||||||
|
bound_clients,
|
||||||
|
idle_for_secs,
|
||||||
|
rtt_ema_ms,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
writer_rows.sort_by_key(|row| (row.dc.unwrap_or(i16::MAX), row.endpoint, row.writer_id));
|
||||||
|
|
||||||
|
let mut dcs = Vec::<MeApiDcStatusSnapshot>::with_capacity(endpoints_by_dc.len());
|
||||||
|
let mut available_endpoints = 0usize;
|
||||||
|
let mut alive_writers = 0usize;
|
||||||
|
for (dc, endpoints) in endpoints_by_dc {
|
||||||
|
let endpoint_count = endpoints.len();
|
||||||
|
let dc_available_endpoints = endpoints
|
||||||
|
.iter()
|
||||||
|
.filter(|endpoint| live_writers_by_endpoint.contains_key(endpoint))
|
||||||
|
.count();
|
||||||
|
let dc_required_writers =
|
||||||
|
self.required_writers_for_dc_with_floor_mode(endpoint_count, false);
|
||||||
|
let dc_alive_writers = live_writers_by_dc.get(&dc).copied().unwrap_or(0);
|
||||||
|
let dc_load = activity
|
||||||
|
.active_sessions_by_target_dc
|
||||||
|
.get(&dc)
|
||||||
|
.copied()
|
||||||
|
.unwrap_or(0);
|
||||||
|
let dc_rtt_ms = dc_rtt_agg
|
||||||
|
.get(&dc)
|
||||||
|
.and_then(|(sum, count)| (*count > 0).then_some(*sum / (*count as f64)));
|
||||||
|
|
||||||
|
available_endpoints += dc_available_endpoints;
|
||||||
|
alive_writers += dc_alive_writers;
|
||||||
|
|
||||||
|
dcs.push(MeApiDcStatusSnapshot {
|
||||||
|
dc,
|
||||||
|
endpoints: endpoints.into_iter().collect(),
|
||||||
|
available_endpoints: dc_available_endpoints,
|
||||||
|
available_pct: ratio_pct(dc_available_endpoints, endpoint_count),
|
||||||
|
required_writers: dc_required_writers,
|
||||||
|
alive_writers: dc_alive_writers,
|
||||||
|
coverage_pct: ratio_pct(dc_alive_writers, dc_required_writers),
|
||||||
|
rtt_ms: dc_rtt_ms,
|
||||||
|
load: dc_load,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
MeApiStatusSnapshot {
|
||||||
|
generated_at_epoch_secs: now_epoch_secs,
|
||||||
|
configured_dc_groups,
|
||||||
|
configured_endpoints,
|
||||||
|
available_endpoints,
|
||||||
|
available_pct: ratio_pct(available_endpoints, configured_endpoints),
|
||||||
|
required_writers,
|
||||||
|
alive_writers,
|
||||||
|
coverage_pct: ratio_pct(alive_writers, required_writers),
|
||||||
|
writers: writer_rows,
|
||||||
|
dcs,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn api_runtime_snapshot(&self) -> MeApiRuntimeSnapshot {
|
||||||
|
let now = Instant::now();
|
||||||
|
let now_epoch_secs = Self::now_epoch_secs();
|
||||||
|
let pending_started_at = self
|
||||||
|
.pending_hardswap_started_at_epoch_secs
|
||||||
|
.load(Ordering::Relaxed);
|
||||||
|
let pending_hardswap_age_secs = (pending_started_at > 0)
|
||||||
|
.then_some(now_epoch_secs.saturating_sub(pending_started_at));
|
||||||
|
|
||||||
|
let mut quarantined_endpoints = Vec::<MeApiQuarantinedEndpointSnapshot>::new();
|
||||||
|
{
|
||||||
|
let guard = self.endpoint_quarantine.lock().await;
|
||||||
|
for (endpoint, expires_at) in guard.iter() {
|
||||||
|
if *expires_at <= now {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let remaining_ms = expires_at.duration_since(now).as_millis() as u64;
|
||||||
|
quarantined_endpoints.push(MeApiQuarantinedEndpointSnapshot {
|
||||||
|
endpoint: *endpoint,
|
||||||
|
remaining_ms,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
quarantined_endpoints.sort_by_key(|entry| entry.endpoint);
|
||||||
|
|
||||||
|
let mut network_path = Vec::<MeApiDcPathSnapshot>::new();
|
||||||
|
if let Some(upstream) = &self.upstream {
|
||||||
|
for dc in 1..=5 {
|
||||||
|
let dc_idx = dc as i16;
|
||||||
|
let ip_preference = upstream
|
||||||
|
.get_dc_ip_preference(dc_idx)
|
||||||
|
.await
|
||||||
|
.map(ip_preference_label);
|
||||||
|
let selected_addr_v4 = upstream.get_dc_addr(dc_idx, false).await;
|
||||||
|
let selected_addr_v6 = upstream.get_dc_addr(dc_idx, true).await;
|
||||||
|
network_path.push(MeApiDcPathSnapshot {
|
||||||
|
dc: dc_idx,
|
||||||
|
ip_preference,
|
||||||
|
selected_addr_v4,
|
||||||
|
selected_addr_v6,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MeApiRuntimeSnapshot {
|
||||||
|
active_generation: self.active_generation.load(Ordering::Relaxed),
|
||||||
|
warm_generation: self.warm_generation.load(Ordering::Relaxed),
|
||||||
|
pending_hardswap_generation: self.pending_hardswap_generation.load(Ordering::Relaxed),
|
||||||
|
pending_hardswap_age_secs,
|
||||||
|
hardswap_enabled: self.hardswap.load(Ordering::Relaxed),
|
||||||
|
floor_mode: floor_mode_label(self.floor_mode()),
|
||||||
|
adaptive_floor_idle_secs: self.me_adaptive_floor_idle_secs.load(Ordering::Relaxed),
|
||||||
|
adaptive_floor_min_writers_single_endpoint: self
|
||||||
|
.me_adaptive_floor_min_writers_single_endpoint
|
||||||
|
.load(Ordering::Relaxed),
|
||||||
|
adaptive_floor_recover_grace_secs: self
|
||||||
|
.me_adaptive_floor_recover_grace_secs
|
||||||
|
.load(Ordering::Relaxed),
|
||||||
|
me_keepalive_enabled: self.me_keepalive_enabled,
|
||||||
|
me_keepalive_interval_secs: self.me_keepalive_interval.as_secs(),
|
||||||
|
me_keepalive_jitter_secs: self.me_keepalive_jitter.as_secs(),
|
||||||
|
me_keepalive_payload_random: self.me_keepalive_payload_random,
|
||||||
|
rpc_proxy_req_every_secs: self.rpc_proxy_req_every_secs.load(Ordering::Relaxed),
|
||||||
|
me_reconnect_max_concurrent_per_dc: self.me_reconnect_max_concurrent_per_dc,
|
||||||
|
me_reconnect_backoff_base_ms: self.me_reconnect_backoff_base.as_millis() as u64,
|
||||||
|
me_reconnect_backoff_cap_ms: self.me_reconnect_backoff_cap.as_millis() as u64,
|
||||||
|
me_reconnect_fast_retry_count: self.me_reconnect_fast_retry_count,
|
||||||
|
me_pool_drain_ttl_secs: self.me_pool_drain_ttl_secs.load(Ordering::Relaxed),
|
||||||
|
me_pool_force_close_secs: self.me_pool_force_close_secs.load(Ordering::Relaxed),
|
||||||
|
me_pool_min_fresh_ratio: Self::permille_to_ratio(
|
||||||
|
self.me_pool_min_fresh_ratio_permille.load(Ordering::Relaxed),
|
||||||
|
),
|
||||||
|
me_bind_stale_mode: bind_stale_mode_label(self.bind_stale_mode()),
|
||||||
|
me_bind_stale_ttl_secs: self.me_bind_stale_ttl_secs.load(Ordering::Relaxed),
|
||||||
|
me_single_endpoint_shadow_writers: self
|
||||||
|
.me_single_endpoint_shadow_writers
|
||||||
|
.load(Ordering::Relaxed),
|
||||||
|
me_single_endpoint_outage_mode_enabled: self
|
||||||
|
.me_single_endpoint_outage_mode_enabled
|
||||||
|
.load(Ordering::Relaxed),
|
||||||
|
me_single_endpoint_outage_disable_quarantine: self
|
||||||
|
.me_single_endpoint_outage_disable_quarantine
|
||||||
|
.load(Ordering::Relaxed),
|
||||||
|
me_single_endpoint_outage_backoff_min_ms: self
|
||||||
|
.me_single_endpoint_outage_backoff_min_ms
|
||||||
|
.load(Ordering::Relaxed),
|
||||||
|
me_single_endpoint_outage_backoff_max_ms: self
|
||||||
|
.me_single_endpoint_outage_backoff_max_ms
|
||||||
|
.load(Ordering::Relaxed),
|
||||||
|
me_single_endpoint_shadow_rotate_every_secs: self
|
||||||
|
.me_single_endpoint_shadow_rotate_every_secs
|
||||||
|
.load(Ordering::Relaxed),
|
||||||
|
me_deterministic_writer_sort: self
|
||||||
|
.me_deterministic_writer_sort
|
||||||
|
.load(Ordering::Relaxed),
|
||||||
|
me_socks_kdf_policy: socks_kdf_policy_label(self.socks_kdf_policy()),
|
||||||
|
quarantined_endpoints,
|
||||||
|
network_path,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ratio_pct(part: usize, total: usize) -> f64 {
|
||||||
|
if total == 0 {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
let pct = ((part as f64) / (total as f64)) * 100.0;
|
||||||
|
pct.clamp(0.0, 100.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn floor_mode_label(mode: MeFloorMode) -> &'static str {
|
||||||
|
match mode {
|
||||||
|
MeFloorMode::Static => "static",
|
||||||
|
MeFloorMode::Adaptive => "adaptive",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bind_stale_mode_label(mode: MeBindStaleMode) -> &'static str {
|
||||||
|
match mode {
|
||||||
|
MeBindStaleMode::Never => "never",
|
||||||
|
MeBindStaleMode::Ttl => "ttl",
|
||||||
|
MeBindStaleMode::Always => "always",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn socks_kdf_policy_label(policy: MeSocksKdfPolicy) -> &'static str {
|
||||||
|
match policy {
|
||||||
|
MeSocksKdfPolicy::Strict => "strict",
|
||||||
|
MeSocksKdfPolicy::Compat => "compat",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ip_preference_label(preference: IpPreference) -> &'static str {
|
||||||
|
match preference {
|
||||||
|
IpPreference::Unknown => "unknown",
|
||||||
|
IpPreference::PreferV6 => "prefer_v6",
|
||||||
|
IpPreference::PreferV4 => "prefer_v4",
|
||||||
|
IpPreference::BothWork => "both",
|
||||||
|
IpPreference::Unavailable => "unavailable",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::ratio_pct;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn ratio_pct_is_zero_when_denominator_is_zero() {
|
||||||
|
assert_eq!(ratio_pct(1, 0), 0.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn ratio_pct_is_capped_at_100() {
|
||||||
|
assert_eq!(ratio_pct(7, 3), 100.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn ratio_pct_reports_expected_value() {
|
||||||
|
assert_eq!(ratio_pct(1, 4), 25.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
535
src/transport/middle_proxy/pool_writer.rs
Normal file
535
src/transport/middle_proxy/pool_writer.rs
Normal file
@@ -0,0 +1,535 @@
|
|||||||
|
use std::net::SocketAddr;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::sync::atomic::{AtomicBool, AtomicU8, AtomicU64, Ordering};
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
use std::io::ErrorKind;
|
||||||
|
|
||||||
|
use bytes::BytesMut;
|
||||||
|
use rand::Rng;
|
||||||
|
use tokio::sync::mpsc;
|
||||||
|
use tokio_util::sync::CancellationToken;
|
||||||
|
use tracing::{debug, info, warn};
|
||||||
|
|
||||||
|
use crate::config::MeBindStaleMode;
|
||||||
|
use crate::crypto::SecureRandom;
|
||||||
|
use crate::error::{ProxyError, Result};
|
||||||
|
use crate::protocol::constants::{RPC_CLOSE_EXT_U32, RPC_PING_U32};
|
||||||
|
|
||||||
|
use super::codec::{RpcWriter, WriterCommand};
|
||||||
|
use super::pool::{MePool, MeWriter, WriterContour};
|
||||||
|
use super::reader::reader_loop;
|
||||||
|
use super::registry::BoundConn;
|
||||||
|
use super::wire::build_proxy_req_payload;
|
||||||
|
|
||||||
|
const ME_ACTIVE_PING_SECS: u64 = 25;
|
||||||
|
const ME_ACTIVE_PING_JITTER_SECS: i64 = 5;
|
||||||
|
const ME_IDLE_KEEPALIVE_MAX_SECS: u64 = 5;
|
||||||
|
const ME_RPC_PROXY_REQ_RESPONSE_WAIT_MS: u64 = 700;
|
||||||
|
|
||||||
|
fn is_me_peer_closed_error(error: &ProxyError) -> bool {
|
||||||
|
matches!(error, ProxyError::Io(ioe) if ioe.kind() == ErrorKind::UnexpectedEof)
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MePool {
|
||||||
|
pub(crate) async fn prune_closed_writers(self: &Arc<Self>) {
|
||||||
|
let closed_writer_ids: Vec<u64> = {
|
||||||
|
let ws = self.writers.read().await;
|
||||||
|
ws.iter().filter(|w| w.tx.is_closed()).map(|w| w.id).collect()
|
||||||
|
};
|
||||||
|
if closed_writer_ids.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for writer_id in closed_writer_ids {
|
||||||
|
if self.registry.is_writer_empty(writer_id).await {
|
||||||
|
let _ = self.remove_writer_only(writer_id).await;
|
||||||
|
} else {
|
||||||
|
let _ = self.remove_writer_and_close_clients(writer_id).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn connect_one(self: &Arc<Self>, addr: SocketAddr, rng: &SecureRandom) -> Result<()> {
|
||||||
|
self.connect_one_with_generation_contour(
|
||||||
|
addr,
|
||||||
|
rng,
|
||||||
|
self.current_generation(),
|
||||||
|
WriterContour::Active,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) async fn connect_one_with_generation_contour(
|
||||||
|
self: &Arc<Self>,
|
||||||
|
addr: SocketAddr,
|
||||||
|
rng: &SecureRandom,
|
||||||
|
generation: u64,
|
||||||
|
contour: WriterContour,
|
||||||
|
) -> Result<()> {
|
||||||
|
let secret_len = self.proxy_secret.read().await.secret.len();
|
||||||
|
if secret_len < 32 {
|
||||||
|
return Err(ProxyError::Proxy("proxy-secret too short for ME auth".into()));
|
||||||
|
}
|
||||||
|
|
||||||
|
let (stream, _connect_ms, upstream_egress) = self.connect_tcp(addr).await?;
|
||||||
|
let hs = self.handshake_only(stream, addr, upstream_egress, rng).await?;
|
||||||
|
|
||||||
|
let writer_id = self.next_writer_id.fetch_add(1, Ordering::Relaxed);
|
||||||
|
let contour = Arc::new(AtomicU8::new(contour.as_u8()));
|
||||||
|
let cancel = CancellationToken::new();
|
||||||
|
let degraded = Arc::new(AtomicBool::new(false));
|
||||||
|
let draining = Arc::new(AtomicBool::new(false));
|
||||||
|
let draining_started_at_epoch_secs = Arc::new(AtomicU64::new(0));
|
||||||
|
let allow_drain_fallback = Arc::new(AtomicBool::new(false));
|
||||||
|
let (tx, mut rx) = mpsc::channel::<WriterCommand>(4096);
|
||||||
|
let mut rpc_writer = RpcWriter {
|
||||||
|
writer: hs.wr,
|
||||||
|
key: hs.write_key,
|
||||||
|
iv: hs.write_iv,
|
||||||
|
seq_no: 0,
|
||||||
|
crc_mode: hs.crc_mode,
|
||||||
|
};
|
||||||
|
let cancel_wr = cancel.clone();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
loop {
|
||||||
|
tokio::select! {
|
||||||
|
cmd = rx.recv() => {
|
||||||
|
match cmd {
|
||||||
|
Some(WriterCommand::Data(payload)) => {
|
||||||
|
if rpc_writer.send(&payload).await.is_err() { break; }
|
||||||
|
}
|
||||||
|
Some(WriterCommand::DataAndFlush(payload)) => {
|
||||||
|
if rpc_writer.send_and_flush(&payload).await.is_err() { break; }
|
||||||
|
}
|
||||||
|
Some(WriterCommand::Close) | None => break,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ = cancel_wr.cancelled() => break,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let writer = MeWriter {
|
||||||
|
id: writer_id,
|
||||||
|
addr,
|
||||||
|
generation,
|
||||||
|
contour: contour.clone(),
|
||||||
|
created_at: Instant::now(),
|
||||||
|
tx: tx.clone(),
|
||||||
|
cancel: cancel.clone(),
|
||||||
|
degraded: degraded.clone(),
|
||||||
|
draining: draining.clone(),
|
||||||
|
draining_started_at_epoch_secs: draining_started_at_epoch_secs.clone(),
|
||||||
|
allow_drain_fallback: allow_drain_fallback.clone(),
|
||||||
|
};
|
||||||
|
self.writers.write().await.push(writer.clone());
|
||||||
|
self.registry.mark_writer_idle(writer_id).await;
|
||||||
|
self.conn_count.fetch_add(1, Ordering::Relaxed);
|
||||||
|
self.writer_available.notify_one();
|
||||||
|
|
||||||
|
let reg = self.registry.clone();
|
||||||
|
let writers_arc = self.writers_arc();
|
||||||
|
let ping_tracker = self.ping_tracker.clone();
|
||||||
|
let ping_tracker_reader = ping_tracker.clone();
|
||||||
|
let rtt_stats = self.rtt_stats.clone();
|
||||||
|
let stats_reader = self.stats.clone();
|
||||||
|
let stats_reader_close = self.stats.clone();
|
||||||
|
let stats_ping = self.stats.clone();
|
||||||
|
let pool = Arc::downgrade(self);
|
||||||
|
let cancel_ping = cancel.clone();
|
||||||
|
let tx_ping = tx.clone();
|
||||||
|
let ping_tracker_ping = ping_tracker.clone();
|
||||||
|
let cleanup_done = Arc::new(AtomicBool::new(false));
|
||||||
|
let cleanup_for_reader = cleanup_done.clone();
|
||||||
|
let cleanup_for_ping = cleanup_done.clone();
|
||||||
|
let keepalive_enabled = self.me_keepalive_enabled;
|
||||||
|
let keepalive_interval = self.me_keepalive_interval;
|
||||||
|
let keepalive_jitter = self.me_keepalive_jitter;
|
||||||
|
let rpc_proxy_req_every_secs = self.rpc_proxy_req_every_secs.load(Ordering::Relaxed);
|
||||||
|
let tx_signal = tx.clone();
|
||||||
|
let stats_signal = self.stats.clone();
|
||||||
|
let cancel_signal = cancel.clone();
|
||||||
|
let cleanup_for_signal = cleanup_done.clone();
|
||||||
|
let pool_signal = Arc::downgrade(self);
|
||||||
|
let keepalive_jitter_signal = self.me_keepalive_jitter;
|
||||||
|
let cancel_reader_token = cancel.clone();
|
||||||
|
let cancel_ping_token = cancel_ping.clone();
|
||||||
|
|
||||||
|
tokio::spawn(async move {
|
||||||
|
let res = reader_loop(
|
||||||
|
hs.rd,
|
||||||
|
hs.read_key,
|
||||||
|
hs.read_iv,
|
||||||
|
hs.crc_mode,
|
||||||
|
reg.clone(),
|
||||||
|
BytesMut::new(),
|
||||||
|
BytesMut::new(),
|
||||||
|
tx.clone(),
|
||||||
|
ping_tracker_reader,
|
||||||
|
rtt_stats.clone(),
|
||||||
|
stats_reader,
|
||||||
|
writer_id,
|
||||||
|
degraded.clone(),
|
||||||
|
cancel_reader_token.clone(),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
let idle_close_by_peer = if let Err(e) = res.as_ref() {
|
||||||
|
is_me_peer_closed_error(e) && reg.is_writer_empty(writer_id).await
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
};
|
||||||
|
if idle_close_by_peer {
|
||||||
|
stats_reader_close.increment_me_idle_close_by_peer_total();
|
||||||
|
info!(writer_id, "ME socket closed by peer on idle writer");
|
||||||
|
}
|
||||||
|
if let Some(pool) = pool.upgrade()
|
||||||
|
&& cleanup_for_reader
|
||||||
|
.compare_exchange(false, true, Ordering::AcqRel, Ordering::Relaxed)
|
||||||
|
.is_ok()
|
||||||
|
{
|
||||||
|
pool.remove_writer_and_close_clients(writer_id).await;
|
||||||
|
}
|
||||||
|
if let Err(e) = res {
|
||||||
|
if !idle_close_by_peer {
|
||||||
|
warn!(error = %e, "ME reader ended");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let mut ws = writers_arc.write().await;
|
||||||
|
ws.retain(|w| w.id != writer_id);
|
||||||
|
info!(remaining = ws.len(), "Dead ME writer removed from pool");
|
||||||
|
});
|
||||||
|
|
||||||
|
let pool_ping = Arc::downgrade(self);
|
||||||
|
tokio::spawn(async move {
|
||||||
|
let mut ping_id: i64 = rand::random::<i64>();
|
||||||
|
let idle_interval_cap = Duration::from_secs(ME_IDLE_KEEPALIVE_MAX_SECS);
|
||||||
|
// Per-writer jittered start to avoid phase sync.
|
||||||
|
let startup_jitter = if keepalive_enabled {
|
||||||
|
let mut interval = keepalive_interval;
|
||||||
|
if let Some(pool) = pool_ping.upgrade() {
|
||||||
|
if pool.registry.is_writer_empty(writer_id).await {
|
||||||
|
interval = interval.min(idle_interval_cap);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let jitter_cap_ms = interval.as_millis() / 2;
|
||||||
|
let effective_jitter_ms = keepalive_jitter.as_millis().min(jitter_cap_ms).max(1);
|
||||||
|
Duration::from_millis(rand::rng().random_range(0..=effective_jitter_ms as u64))
|
||||||
|
} else {
|
||||||
|
let jitter = rand::rng().random_range(-ME_ACTIVE_PING_JITTER_SECS..=ME_ACTIVE_PING_JITTER_SECS);
|
||||||
|
let wait = (ME_ACTIVE_PING_SECS as i64 + jitter).max(5) as u64;
|
||||||
|
Duration::from_secs(wait)
|
||||||
|
};
|
||||||
|
tokio::select! {
|
||||||
|
_ = cancel_ping_token.cancelled() => return,
|
||||||
|
_ = tokio::time::sleep(startup_jitter) => {}
|
||||||
|
}
|
||||||
|
loop {
|
||||||
|
let wait = if keepalive_enabled {
|
||||||
|
let mut interval = keepalive_interval;
|
||||||
|
if let Some(pool) = pool_ping.upgrade() {
|
||||||
|
if pool.registry.is_writer_empty(writer_id).await {
|
||||||
|
interval = interval.min(idle_interval_cap);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let jitter_cap_ms = interval.as_millis() / 2;
|
||||||
|
let effective_jitter_ms = keepalive_jitter.as_millis().min(jitter_cap_ms).max(1);
|
||||||
|
interval + Duration::from_millis(rand::rng().random_range(0..=effective_jitter_ms as u64))
|
||||||
|
} else {
|
||||||
|
let jitter = rand::rng().random_range(-ME_ACTIVE_PING_JITTER_SECS..=ME_ACTIVE_PING_JITTER_SECS);
|
||||||
|
let secs = (ME_ACTIVE_PING_SECS as i64 + jitter).max(5) as u64;
|
||||||
|
Duration::from_secs(secs)
|
||||||
|
};
|
||||||
|
tokio::select! {
|
||||||
|
_ = cancel_ping_token.cancelled() => {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
_ = tokio::time::sleep(wait) => {}
|
||||||
|
}
|
||||||
|
let sent_id = ping_id;
|
||||||
|
let mut p = Vec::with_capacity(12);
|
||||||
|
p.extend_from_slice(&RPC_PING_U32.to_le_bytes());
|
||||||
|
p.extend_from_slice(&sent_id.to_le_bytes());
|
||||||
|
{
|
||||||
|
let mut tracker = ping_tracker_ping.lock().await;
|
||||||
|
let before = tracker.len();
|
||||||
|
tracker.retain(|_, (ts, _)| ts.elapsed() < Duration::from_secs(120));
|
||||||
|
let expired = before.saturating_sub(tracker.len());
|
||||||
|
if expired > 0 {
|
||||||
|
stats_ping.increment_me_keepalive_timeout_by(expired as u64);
|
||||||
|
}
|
||||||
|
tracker.insert(sent_id, (std::time::Instant::now(), writer_id));
|
||||||
|
}
|
||||||
|
ping_id = ping_id.wrapping_add(1);
|
||||||
|
stats_ping.increment_me_keepalive_sent();
|
||||||
|
if tx_ping.send(WriterCommand::DataAndFlush(p)).await.is_err() {
|
||||||
|
stats_ping.increment_me_keepalive_failed();
|
||||||
|
debug!("ME ping failed, removing dead writer");
|
||||||
|
cancel_ping.cancel();
|
||||||
|
if let Some(pool) = pool_ping.upgrade()
|
||||||
|
&& cleanup_for_ping
|
||||||
|
.compare_exchange(false, true, Ordering::AcqRel, Ordering::Relaxed)
|
||||||
|
.is_ok()
|
||||||
|
{
|
||||||
|
pool.remove_writer_and_close_clients(writer_id).await;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
tokio::spawn(async move {
|
||||||
|
if rpc_proxy_req_every_secs == 0 {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let interval = Duration::from_secs(rpc_proxy_req_every_secs);
|
||||||
|
let startup_jitter_ms = {
|
||||||
|
let jitter_cap_ms = interval.as_millis() / 2;
|
||||||
|
let effective_jitter_ms = keepalive_jitter_signal
|
||||||
|
.as_millis()
|
||||||
|
.min(jitter_cap_ms)
|
||||||
|
.max(1);
|
||||||
|
rand::rng().random_range(0..=effective_jitter_ms as u64)
|
||||||
|
};
|
||||||
|
|
||||||
|
tokio::select! {
|
||||||
|
_ = cancel_signal.cancelled() => return,
|
||||||
|
_ = tokio::time::sleep(Duration::from_millis(startup_jitter_ms)) => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let wait = {
|
||||||
|
let jitter_cap_ms = interval.as_millis() / 2;
|
||||||
|
let effective_jitter_ms = keepalive_jitter_signal
|
||||||
|
.as_millis()
|
||||||
|
.min(jitter_cap_ms)
|
||||||
|
.max(1);
|
||||||
|
interval + Duration::from_millis(rand::rng().random_range(0..=effective_jitter_ms as u64))
|
||||||
|
};
|
||||||
|
|
||||||
|
tokio::select! {
|
||||||
|
_ = cancel_signal.cancelled() => break,
|
||||||
|
_ = tokio::time::sleep(wait) => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
let Some(pool) = pool_signal.upgrade() else {
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
|
||||||
|
let Some(meta) = pool.registry.get_last_writer_meta(writer_id).await else {
|
||||||
|
stats_signal.increment_me_rpc_proxy_req_signal_skipped_no_meta_total();
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
let (conn_id, mut service_rx) = pool.registry.register().await;
|
||||||
|
pool.registry
|
||||||
|
.bind_writer(conn_id, writer_id, tx_signal.clone(), meta.clone())
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let payload = build_proxy_req_payload(
|
||||||
|
conn_id,
|
||||||
|
meta.client_addr,
|
||||||
|
meta.our_addr,
|
||||||
|
&[],
|
||||||
|
pool.proxy_tag.as_deref(),
|
||||||
|
meta.proto_flags,
|
||||||
|
);
|
||||||
|
|
||||||
|
if tx_signal.send(WriterCommand::DataAndFlush(payload)).await.is_err() {
|
||||||
|
stats_signal.increment_me_rpc_proxy_req_signal_failed_total();
|
||||||
|
let _ = pool.registry.unregister(conn_id).await;
|
||||||
|
cancel_signal.cancel();
|
||||||
|
if cleanup_for_signal
|
||||||
|
.compare_exchange(false, true, Ordering::AcqRel, Ordering::Relaxed)
|
||||||
|
.is_ok()
|
||||||
|
{
|
||||||
|
pool.remove_writer_and_close_clients(writer_id).await;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
stats_signal.increment_me_rpc_proxy_req_signal_sent_total();
|
||||||
|
|
||||||
|
if matches!(
|
||||||
|
tokio::time::timeout(
|
||||||
|
Duration::from_millis(ME_RPC_PROXY_REQ_RESPONSE_WAIT_MS),
|
||||||
|
service_rx.recv(),
|
||||||
|
)
|
||||||
|
.await,
|
||||||
|
Ok(Some(_))
|
||||||
|
) {
|
||||||
|
stats_signal.increment_me_rpc_proxy_req_signal_response_total();
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut close_payload = Vec::with_capacity(12);
|
||||||
|
close_payload.extend_from_slice(&RPC_CLOSE_EXT_U32.to_le_bytes());
|
||||||
|
close_payload.extend_from_slice(&conn_id.to_le_bytes());
|
||||||
|
|
||||||
|
if tx_signal
|
||||||
|
.send(WriterCommand::DataAndFlush(close_payload))
|
||||||
|
.await
|
||||||
|
.is_err()
|
||||||
|
{
|
||||||
|
stats_signal.increment_me_rpc_proxy_req_signal_failed_total();
|
||||||
|
let _ = pool.registry.unregister(conn_id).await;
|
||||||
|
cancel_signal.cancel();
|
||||||
|
if cleanup_for_signal
|
||||||
|
.compare_exchange(false, true, Ordering::AcqRel, Ordering::Relaxed)
|
||||||
|
.is_ok()
|
||||||
|
{
|
||||||
|
pool.remove_writer_and_close_clients(writer_id).await;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
stats_signal.increment_me_rpc_proxy_req_signal_close_sent_total();
|
||||||
|
let _ = pool.registry.unregister(conn_id).await;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn remove_writer_and_close_clients(self: &Arc<Self>, writer_id: u64) {
|
||||||
|
let conns = self.remove_writer_only(writer_id).await;
|
||||||
|
for bound in conns {
|
||||||
|
let _ = self.registry.route(bound.conn_id, super::MeResponse::Close).await;
|
||||||
|
let _ = self.registry.unregister(bound.conn_id).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn remove_writer_only(self: &Arc<Self>, writer_id: u64) -> Vec<BoundConn> {
|
||||||
|
let mut close_tx: Option<mpsc::Sender<WriterCommand>> = None;
|
||||||
|
let mut removed_addr: Option<SocketAddr> = None;
|
||||||
|
let mut removed_uptime: Option<Duration> = None;
|
||||||
|
let mut trigger_refill = false;
|
||||||
|
{
|
||||||
|
let mut ws = self.writers.write().await;
|
||||||
|
if let Some(pos) = ws.iter().position(|w| w.id == writer_id) {
|
||||||
|
let w = ws.remove(pos);
|
||||||
|
let was_draining = w.draining.load(Ordering::Relaxed);
|
||||||
|
if was_draining {
|
||||||
|
self.stats.decrement_pool_drain_active();
|
||||||
|
}
|
||||||
|
self.stats.increment_me_writer_removed_total();
|
||||||
|
w.cancel.cancel();
|
||||||
|
removed_addr = Some(w.addr);
|
||||||
|
removed_uptime = Some(w.created_at.elapsed());
|
||||||
|
trigger_refill = !was_draining;
|
||||||
|
if trigger_refill {
|
||||||
|
self.stats.increment_me_writer_removed_unexpected_total();
|
||||||
|
}
|
||||||
|
close_tx = Some(w.tx.clone());
|
||||||
|
self.conn_count.fetch_sub(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let Some(tx) = close_tx {
|
||||||
|
let _ = tx.send(WriterCommand::Close).await;
|
||||||
|
}
|
||||||
|
if trigger_refill
|
||||||
|
&& let Some(addr) = removed_addr
|
||||||
|
{
|
||||||
|
if let Some(uptime) = removed_uptime {
|
||||||
|
self.maybe_quarantine_flapping_endpoint(addr, uptime).await;
|
||||||
|
}
|
||||||
|
self.trigger_immediate_refill(addr);
|
||||||
|
}
|
||||||
|
self.rtt_stats.lock().await.remove(&writer_id);
|
||||||
|
self.registry.writer_lost(writer_id).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn mark_writer_draining_with_timeout(
|
||||||
|
self: &Arc<Self>,
|
||||||
|
writer_id: u64,
|
||||||
|
timeout: Option<Duration>,
|
||||||
|
allow_drain_fallback: bool,
|
||||||
|
) {
|
||||||
|
let timeout = timeout.filter(|d| !d.is_zero());
|
||||||
|
let found = {
|
||||||
|
let mut ws = self.writers.write().await;
|
||||||
|
if let Some(w) = ws.iter_mut().find(|w| w.id == writer_id) {
|
||||||
|
let already_draining = w.draining.swap(true, Ordering::Relaxed);
|
||||||
|
w.allow_drain_fallback
|
||||||
|
.store(allow_drain_fallback, Ordering::Relaxed);
|
||||||
|
w.draining_started_at_epoch_secs
|
||||||
|
.store(Self::now_epoch_secs(), Ordering::Relaxed);
|
||||||
|
if !already_draining {
|
||||||
|
self.stats.increment_pool_drain_active();
|
||||||
|
}
|
||||||
|
w.contour
|
||||||
|
.store(WriterContour::Draining.as_u8(), Ordering::Relaxed);
|
||||||
|
w.draining.store(true, Ordering::Relaxed);
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if !found {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let timeout_secs = timeout.map(|d| d.as_secs()).unwrap_or(0);
|
||||||
|
debug!(
|
||||||
|
writer_id,
|
||||||
|
timeout_secs,
|
||||||
|
allow_drain_fallback,
|
||||||
|
"ME writer marked draining"
|
||||||
|
);
|
||||||
|
|
||||||
|
let pool = Arc::downgrade(self);
|
||||||
|
tokio::spawn(async move {
|
||||||
|
let deadline = timeout.map(|t| Instant::now() + t);
|
||||||
|
while let Some(p) = pool.upgrade() {
|
||||||
|
if let Some(deadline_at) = deadline
|
||||||
|
&& Instant::now() >= deadline_at
|
||||||
|
{
|
||||||
|
warn!(writer_id, "Drain timeout, force-closing");
|
||||||
|
p.stats.increment_pool_force_close_total();
|
||||||
|
let _ = p.remove_writer_and_close_clients(writer_id).await;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if p.registry.is_writer_empty(writer_id).await {
|
||||||
|
let _ = p.remove_writer_only(writer_id).await;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn mark_writer_draining(self: &Arc<Self>, writer_id: u64) {
|
||||||
|
self.mark_writer_draining_with_timeout(writer_id, Some(Duration::from_secs(300)), false)
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) fn writer_accepts_new_binding(&self, writer: &MeWriter) -> bool {
|
||||||
|
if !writer.draining.load(Ordering::Relaxed) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if !writer.allow_drain_fallback.load(Ordering::Relaxed) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
match self.bind_stale_mode() {
|
||||||
|
MeBindStaleMode::Never => false,
|
||||||
|
MeBindStaleMode::Always => true,
|
||||||
|
MeBindStaleMode::Ttl => {
|
||||||
|
let ttl_secs = self.me_bind_stale_ttl_secs.load(Ordering::Relaxed);
|
||||||
|
if ttl_secs == 0 {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
let started = writer.draining_started_at_epoch_secs.load(Ordering::Relaxed);
|
||||||
|
if started == 0 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Self::now_epoch_secs().saturating_sub(started) <= ttl_secs
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::io::ErrorKind;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::sync::atomic::{AtomicBool, Ordering};
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
@@ -45,7 +46,11 @@ pub(crate) async fn reader_loop(
|
|||||||
_ = cancel.cancelled() => return Ok(()),
|
_ = cancel.cancelled() => return Ok(()),
|
||||||
};
|
};
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
return Ok(());
|
stats.increment_me_reader_eof_total();
|
||||||
|
return Err(ProxyError::Io(std::io::Error::new(
|
||||||
|
ErrorKind::UnexpectedEof,
|
||||||
|
"ME socket closed by peer",
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
raw.extend_from_slice(&tmp[..n]);
|
raw.extend_from_slice(&tmp[..n]);
|
||||||
|
|
||||||
@@ -124,7 +129,14 @@ pub(crate) async fn reader_loop(
|
|||||||
match routed {
|
match routed {
|
||||||
RouteResult::NoConn => stats.increment_me_route_drop_no_conn(),
|
RouteResult::NoConn => stats.increment_me_route_drop_no_conn(),
|
||||||
RouteResult::ChannelClosed => stats.increment_me_route_drop_channel_closed(),
|
RouteResult::ChannelClosed => stats.increment_me_route_drop_channel_closed(),
|
||||||
RouteResult::QueueFull => stats.increment_me_route_drop_queue_full(),
|
RouteResult::QueueFullBase => {
|
||||||
|
stats.increment_me_route_drop_queue_full();
|
||||||
|
stats.increment_me_route_drop_queue_full_base();
|
||||||
|
}
|
||||||
|
RouteResult::QueueFullHigh => {
|
||||||
|
stats.increment_me_route_drop_queue_full();
|
||||||
|
stats.increment_me_route_drop_queue_full_high();
|
||||||
|
}
|
||||||
RouteResult::Routed => {}
|
RouteResult::Routed => {}
|
||||||
}
|
}
|
||||||
reg.unregister(cid).await;
|
reg.unregister(cid).await;
|
||||||
@@ -140,7 +152,14 @@ pub(crate) async fn reader_loop(
|
|||||||
match routed {
|
match routed {
|
||||||
RouteResult::NoConn => stats.increment_me_route_drop_no_conn(),
|
RouteResult::NoConn => stats.increment_me_route_drop_no_conn(),
|
||||||
RouteResult::ChannelClosed => stats.increment_me_route_drop_channel_closed(),
|
RouteResult::ChannelClosed => stats.increment_me_route_drop_channel_closed(),
|
||||||
RouteResult::QueueFull => stats.increment_me_route_drop_queue_full(),
|
RouteResult::QueueFullBase => {
|
||||||
|
stats.increment_me_route_drop_queue_full();
|
||||||
|
stats.increment_me_route_drop_queue_full_base();
|
||||||
|
}
|
||||||
|
RouteResult::QueueFullHigh => {
|
||||||
|
stats.increment_me_route_drop_queue_full();
|
||||||
|
stats.increment_me_route_drop_queue_full_high();
|
||||||
|
}
|
||||||
RouteResult::Routed => {}
|
RouteResult::Routed => {}
|
||||||
}
|
}
|
||||||
reg.unregister(cid).await;
|
reg.unregister(cid).await;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::sync::atomic::{AtomicU64, Ordering};
|
use std::sync::atomic::{AtomicU8, AtomicU64, Ordering};
|
||||||
use std::time::Duration;
|
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
use tokio::sync::{mpsc, RwLock};
|
use tokio::sync::{mpsc, RwLock};
|
||||||
use tokio::sync::mpsc::error::TrySendError;
|
use tokio::sync::mpsc::error::TrySendError;
|
||||||
@@ -10,14 +10,17 @@ use super::codec::WriterCommand;
|
|||||||
use super::MeResponse;
|
use super::MeResponse;
|
||||||
|
|
||||||
const ROUTE_CHANNEL_CAPACITY: usize = 4096;
|
const ROUTE_CHANNEL_CAPACITY: usize = 4096;
|
||||||
const ROUTE_BACKPRESSURE_TIMEOUT: Duration = Duration::from_millis(25);
|
const ROUTE_BACKPRESSURE_BASE_TIMEOUT_MS: u64 = 25;
|
||||||
|
const ROUTE_BACKPRESSURE_HIGH_TIMEOUT_MS: u64 = 120;
|
||||||
|
const ROUTE_BACKPRESSURE_HIGH_WATERMARK_PCT: u8 = 80;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub enum RouteResult {
|
pub enum RouteResult {
|
||||||
Routed,
|
Routed,
|
||||||
NoConn,
|
NoConn,
|
||||||
ChannelClosed,
|
ChannelClosed,
|
||||||
QueueFull,
|
QueueFullBase,
|
||||||
|
QueueFullHigh,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@@ -42,12 +45,20 @@ pub struct ConnWriter {
|
|||||||
pub tx: mpsc::Sender<WriterCommand>,
|
pub tx: mpsc::Sender<WriterCommand>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Default)]
|
||||||
|
pub(super) struct WriterActivitySnapshot {
|
||||||
|
pub bound_clients_by_writer: HashMap<u64, usize>,
|
||||||
|
pub active_sessions_by_target_dc: HashMap<i16, usize>,
|
||||||
|
}
|
||||||
|
|
||||||
struct RegistryInner {
|
struct RegistryInner {
|
||||||
map: HashMap<u64, mpsc::Sender<MeResponse>>,
|
map: HashMap<u64, mpsc::Sender<MeResponse>>,
|
||||||
writers: HashMap<u64, mpsc::Sender<WriterCommand>>,
|
writers: HashMap<u64, mpsc::Sender<WriterCommand>>,
|
||||||
writer_for_conn: HashMap<u64, u64>,
|
writer_for_conn: HashMap<u64, u64>,
|
||||||
conns_for_writer: HashMap<u64, HashSet<u64>>,
|
conns_for_writer: HashMap<u64, HashSet<u64>>,
|
||||||
meta: HashMap<u64, ConnMeta>,
|
meta: HashMap<u64, ConnMeta>,
|
||||||
|
last_meta_for_writer: HashMap<u64, ConnMeta>,
|
||||||
|
writer_idle_since_epoch_secs: HashMap<u64, u64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RegistryInner {
|
impl RegistryInner {
|
||||||
@@ -58,6 +69,8 @@ impl RegistryInner {
|
|||||||
writer_for_conn: HashMap::new(),
|
writer_for_conn: HashMap::new(),
|
||||||
conns_for_writer: HashMap::new(),
|
conns_for_writer: HashMap::new(),
|
||||||
meta: HashMap::new(),
|
meta: HashMap::new(),
|
||||||
|
last_meta_for_writer: HashMap::new(),
|
||||||
|
writer_idle_since_epoch_secs: HashMap::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -65,17 +78,53 @@ impl RegistryInner {
|
|||||||
pub struct ConnRegistry {
|
pub struct ConnRegistry {
|
||||||
inner: RwLock<RegistryInner>,
|
inner: RwLock<RegistryInner>,
|
||||||
next_id: AtomicU64,
|
next_id: AtomicU64,
|
||||||
|
route_backpressure_base_timeout_ms: AtomicU64,
|
||||||
|
route_backpressure_high_timeout_ms: AtomicU64,
|
||||||
|
route_backpressure_high_watermark_pct: AtomicU8,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ConnRegistry {
|
impl ConnRegistry {
|
||||||
|
fn now_epoch_secs() -> u64 {
|
||||||
|
SystemTime::now()
|
||||||
|
.duration_since(UNIX_EPOCH)
|
||||||
|
.unwrap_or_default()
|
||||||
|
.as_secs()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let start = rand::random::<u64>() | 1;
|
let start = rand::random::<u64>() | 1;
|
||||||
Self {
|
Self {
|
||||||
inner: RwLock::new(RegistryInner::new()),
|
inner: RwLock::new(RegistryInner::new()),
|
||||||
next_id: AtomicU64::new(start),
|
next_id: AtomicU64::new(start),
|
||||||
|
route_backpressure_base_timeout_ms: AtomicU64::new(
|
||||||
|
ROUTE_BACKPRESSURE_BASE_TIMEOUT_MS,
|
||||||
|
),
|
||||||
|
route_backpressure_high_timeout_ms: AtomicU64::new(
|
||||||
|
ROUTE_BACKPRESSURE_HIGH_TIMEOUT_MS,
|
||||||
|
),
|
||||||
|
route_backpressure_high_watermark_pct: AtomicU8::new(
|
||||||
|
ROUTE_BACKPRESSURE_HIGH_WATERMARK_PCT,
|
||||||
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn update_route_backpressure_policy(
|
||||||
|
&self,
|
||||||
|
base_timeout_ms: u64,
|
||||||
|
high_timeout_ms: u64,
|
||||||
|
high_watermark_pct: u8,
|
||||||
|
) {
|
||||||
|
let base = base_timeout_ms.max(1);
|
||||||
|
let high = high_timeout_ms.max(base);
|
||||||
|
let watermark = high_watermark_pct.clamp(1, 100);
|
||||||
|
self.route_backpressure_base_timeout_ms
|
||||||
|
.store(base, Ordering::Relaxed);
|
||||||
|
self.route_backpressure_high_timeout_ms
|
||||||
|
.store(high, Ordering::Relaxed);
|
||||||
|
self.route_backpressure_high_watermark_pct
|
||||||
|
.store(watermark, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn register(&self) -> (u64, mpsc::Receiver<MeResponse>) {
|
pub async fn register(&self) -> (u64, mpsc::Receiver<MeResponse>) {
|
||||||
let id = self.next_id.fetch_add(1, Ordering::Relaxed);
|
let id = self.next_id.fetch_add(1, Ordering::Relaxed);
|
||||||
let (tx, rx) = mpsc::channel(ROUTE_CHANNEL_CAPACITY);
|
let (tx, rx) = mpsc::channel(ROUTE_CHANNEL_CAPACITY);
|
||||||
@@ -89,8 +138,16 @@ impl ConnRegistry {
|
|||||||
inner.map.remove(&id);
|
inner.map.remove(&id);
|
||||||
inner.meta.remove(&id);
|
inner.meta.remove(&id);
|
||||||
if let Some(writer_id) = inner.writer_for_conn.remove(&id) {
|
if let Some(writer_id) = inner.writer_for_conn.remove(&id) {
|
||||||
if let Some(set) = inner.conns_for_writer.get_mut(&writer_id) {
|
let became_empty = if let Some(set) = inner.conns_for_writer.get_mut(&writer_id) {
|
||||||
set.remove(&id);
|
set.remove(&id);
|
||||||
|
set.is_empty()
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
};
|
||||||
|
if became_empty {
|
||||||
|
inner
|
||||||
|
.writer_idle_since_epoch_secs
|
||||||
|
.insert(writer_id, Self::now_epoch_secs());
|
||||||
}
|
}
|
||||||
return Some(writer_id);
|
return Some(writer_id);
|
||||||
}
|
}
|
||||||
@@ -112,10 +169,40 @@ impl ConnRegistry {
|
|||||||
Err(TrySendError::Closed(_)) => RouteResult::ChannelClosed,
|
Err(TrySendError::Closed(_)) => RouteResult::ChannelClosed,
|
||||||
Err(TrySendError::Full(resp)) => {
|
Err(TrySendError::Full(resp)) => {
|
||||||
// Absorb short bursts without dropping/closing the session immediately.
|
// Absorb short bursts without dropping/closing the session immediately.
|
||||||
match tokio::time::timeout(ROUTE_BACKPRESSURE_TIMEOUT, tx.send(resp)).await {
|
let base_timeout_ms =
|
||||||
|
self.route_backpressure_base_timeout_ms.load(Ordering::Relaxed).max(1);
|
||||||
|
let high_timeout_ms = self
|
||||||
|
.route_backpressure_high_timeout_ms
|
||||||
|
.load(Ordering::Relaxed)
|
||||||
|
.max(base_timeout_ms);
|
||||||
|
let high_watermark_pct = self
|
||||||
|
.route_backpressure_high_watermark_pct
|
||||||
|
.load(Ordering::Relaxed)
|
||||||
|
.clamp(1, 100);
|
||||||
|
let used = ROUTE_CHANNEL_CAPACITY.saturating_sub(tx.capacity());
|
||||||
|
let used_pct = if ROUTE_CHANNEL_CAPACITY == 0 {
|
||||||
|
100
|
||||||
|
} else {
|
||||||
|
(used.saturating_mul(100) / ROUTE_CHANNEL_CAPACITY) as u8
|
||||||
|
};
|
||||||
|
let high_profile = used_pct >= high_watermark_pct;
|
||||||
|
let timeout_ms = if high_profile {
|
||||||
|
high_timeout_ms
|
||||||
|
} else {
|
||||||
|
base_timeout_ms
|
||||||
|
};
|
||||||
|
let timeout_dur = Duration::from_millis(timeout_ms);
|
||||||
|
|
||||||
|
match tokio::time::timeout(timeout_dur, tx.send(resp)).await {
|
||||||
Ok(Ok(())) => RouteResult::Routed,
|
Ok(Ok(())) => RouteResult::Routed,
|
||||||
Ok(Err(_)) => RouteResult::ChannelClosed,
|
Ok(Err(_)) => RouteResult::ChannelClosed,
|
||||||
Err(_) => RouteResult::QueueFull,
|
Err(_) => {
|
||||||
|
if high_profile {
|
||||||
|
RouteResult::QueueFullHigh
|
||||||
|
} else {
|
||||||
|
RouteResult::QueueFullBase
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -129,8 +216,10 @@ impl ConnRegistry {
|
|||||||
meta: ConnMeta,
|
meta: ConnMeta,
|
||||||
) {
|
) {
|
||||||
let mut inner = self.inner.write().await;
|
let mut inner = self.inner.write().await;
|
||||||
inner.meta.entry(conn_id).or_insert(meta);
|
inner.meta.entry(conn_id).or_insert(meta.clone());
|
||||||
inner.writer_for_conn.insert(conn_id, writer_id);
|
inner.writer_for_conn.insert(conn_id, writer_id);
|
||||||
|
inner.last_meta_for_writer.insert(writer_id, meta);
|
||||||
|
inner.writer_idle_since_epoch_secs.remove(&writer_id);
|
||||||
inner.writers.entry(writer_id).or_insert_with(|| tx.clone());
|
inner.writers.entry(writer_id).or_insert_with(|| tx.clone());
|
||||||
inner
|
inner
|
||||||
.conns_for_writer
|
.conns_for_writer
|
||||||
@@ -139,6 +228,49 @@ impl ConnRegistry {
|
|||||||
.insert(conn_id);
|
.insert(conn_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn mark_writer_idle(&self, writer_id: u64) {
|
||||||
|
let mut inner = self.inner.write().await;
|
||||||
|
inner.conns_for_writer.entry(writer_id).or_insert_with(HashSet::new);
|
||||||
|
inner
|
||||||
|
.writer_idle_since_epoch_secs
|
||||||
|
.entry(writer_id)
|
||||||
|
.or_insert(Self::now_epoch_secs());
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_last_writer_meta(&self, writer_id: u64) -> Option<ConnMeta> {
|
||||||
|
let inner = self.inner.read().await;
|
||||||
|
inner.last_meta_for_writer.get(&writer_id).cloned()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn writer_idle_since_snapshot(&self) -> HashMap<u64, u64> {
|
||||||
|
let inner = self.inner.read().await;
|
||||||
|
inner.writer_idle_since_epoch_secs.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) async fn writer_activity_snapshot(&self) -> WriterActivitySnapshot {
|
||||||
|
let inner = self.inner.read().await;
|
||||||
|
let mut bound_clients_by_writer = HashMap::<u64, usize>::new();
|
||||||
|
let mut active_sessions_by_target_dc = HashMap::<i16, usize>::new();
|
||||||
|
|
||||||
|
for (writer_id, conn_ids) in &inner.conns_for_writer {
|
||||||
|
bound_clients_by_writer.insert(*writer_id, conn_ids.len());
|
||||||
|
}
|
||||||
|
for conn_meta in inner.meta.values() {
|
||||||
|
let dc_u16 = conn_meta.target_dc.unsigned_abs();
|
||||||
|
if dc_u16 == 0 {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if let Ok(dc) = i16::try_from(dc_u16) {
|
||||||
|
*active_sessions_by_target_dc.entry(dc).or_insert(0) += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
WriterActivitySnapshot {
|
||||||
|
bound_clients_by_writer,
|
||||||
|
active_sessions_by_target_dc,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn get_writer(&self, conn_id: u64) -> Option<ConnWriter> {
|
pub async fn get_writer(&self, conn_id: u64) -> Option<ConnWriter> {
|
||||||
let inner = self.inner.read().await;
|
let inner = self.inner.read().await;
|
||||||
let writer_id = inner.writer_for_conn.get(&conn_id).cloned()?;
|
let writer_id = inner.writer_for_conn.get(&conn_id).cloned()?;
|
||||||
@@ -149,6 +281,8 @@ impl ConnRegistry {
|
|||||||
pub async fn writer_lost(&self, writer_id: u64) -> Vec<BoundConn> {
|
pub async fn writer_lost(&self, writer_id: u64) -> Vec<BoundConn> {
|
||||||
let mut inner = self.inner.write().await;
|
let mut inner = self.inner.write().await;
|
||||||
inner.writers.remove(&writer_id);
|
inner.writers.remove(&writer_id);
|
||||||
|
inner.last_meta_for_writer.remove(&writer_id);
|
||||||
|
inner.writer_idle_since_epoch_secs.remove(&writer_id);
|
||||||
let conns = inner
|
let conns = inner
|
||||||
.conns_for_writer
|
.conns_for_writer
|
||||||
.remove(&writer_id)
|
.remove(&writer_id)
|
||||||
@@ -184,3 +318,69 @@ impl ConnRegistry {
|
|||||||
.unwrap_or(true)
|
.unwrap_or(true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
||||||
|
|
||||||
|
use super::ConnMeta;
|
||||||
|
use super::ConnRegistry;
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn writer_activity_snapshot_tracks_writer_and_dc_load() {
|
||||||
|
let registry = ConnRegistry::new();
|
||||||
|
|
||||||
|
let (conn_a, _rx_a) = registry.register().await;
|
||||||
|
let (conn_b, _rx_b) = registry.register().await;
|
||||||
|
let (conn_c, _rx_c) = registry.register().await;
|
||||||
|
let (writer_tx_a, _writer_rx_a) = tokio::sync::mpsc::channel(8);
|
||||||
|
let (writer_tx_b, _writer_rx_b) = tokio::sync::mpsc::channel(8);
|
||||||
|
|
||||||
|
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 443);
|
||||||
|
registry
|
||||||
|
.bind_writer(
|
||||||
|
conn_a,
|
||||||
|
10,
|
||||||
|
writer_tx_a.clone(),
|
||||||
|
ConnMeta {
|
||||||
|
target_dc: 2,
|
||||||
|
client_addr: addr,
|
||||||
|
our_addr: addr,
|
||||||
|
proto_flags: 0,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
registry
|
||||||
|
.bind_writer(
|
||||||
|
conn_b,
|
||||||
|
10,
|
||||||
|
writer_tx_a,
|
||||||
|
ConnMeta {
|
||||||
|
target_dc: -2,
|
||||||
|
client_addr: addr,
|
||||||
|
our_addr: addr,
|
||||||
|
proto_flags: 0,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
registry
|
||||||
|
.bind_writer(
|
||||||
|
conn_c,
|
||||||
|
20,
|
||||||
|
writer_tx_b,
|
||||||
|
ConnMeta {
|
||||||
|
target_dc: 4,
|
||||||
|
client_addr: addr,
|
||||||
|
our_addr: addr,
|
||||||
|
proto_flags: 0,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let snapshot = registry.writer_activity_snapshot().await;
|
||||||
|
assert_eq!(snapshot.bound_clients_by_writer.get(&10), Some(&2));
|
||||||
|
assert_eq!(snapshot.bound_clients_by_writer.get(&20), Some(&1));
|
||||||
|
assert_eq!(snapshot.active_sessions_by_target_dc.get(&2), Some(&2));
|
||||||
|
assert_eq!(snapshot.active_sessions_by_target_dc.get(&4), Some(&1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,19 +1,111 @@
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use tokio::sync::watch;
|
use tokio::sync::{mpsc, watch};
|
||||||
use tracing::{info, warn};
|
use tracing::{debug, info, warn};
|
||||||
|
|
||||||
use crate::config::ProxyConfig;
|
use crate::config::ProxyConfig;
|
||||||
use crate::crypto::SecureRandom;
|
use crate::crypto::SecureRandom;
|
||||||
|
|
||||||
use super::MePool;
|
use super::MePool;
|
||||||
|
|
||||||
/// Periodically reinitialize ME generations and swap them after full warmup.
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub async fn me_rotation_task(
|
pub enum MeReinitTrigger {
|
||||||
|
Periodic,
|
||||||
|
MapChanged,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MeReinitTrigger {
|
||||||
|
fn as_str(self) -> &'static str {
|
||||||
|
match self {
|
||||||
|
MeReinitTrigger::Periodic => "periodic",
|
||||||
|
MeReinitTrigger::MapChanged => "map-change",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn enqueue_reinit_trigger(
|
||||||
|
tx: &mpsc::Sender<MeReinitTrigger>,
|
||||||
|
trigger: MeReinitTrigger,
|
||||||
|
) {
|
||||||
|
match tx.try_send(trigger) {
|
||||||
|
Ok(()) => {}
|
||||||
|
Err(tokio::sync::mpsc::error::TrySendError::Full(_)) => {
|
||||||
|
debug!(trigger = trigger.as_str(), "ME reinit trigger dropped (queue full)");
|
||||||
|
}
|
||||||
|
Err(tokio::sync::mpsc::error::TrySendError::Closed(_)) => {
|
||||||
|
warn!(trigger = trigger.as_str(), "ME reinit trigger dropped (scheduler closed)");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn me_reinit_scheduler(
|
||||||
pool: Arc<MePool>,
|
pool: Arc<MePool>,
|
||||||
rng: Arc<SecureRandom>,
|
rng: Arc<SecureRandom>,
|
||||||
|
config_rx: watch::Receiver<Arc<ProxyConfig>>,
|
||||||
|
mut trigger_rx: mpsc::Receiver<MeReinitTrigger>,
|
||||||
|
) {
|
||||||
|
info!("ME reinit scheduler started");
|
||||||
|
loop {
|
||||||
|
let Some(first_trigger) = trigger_rx.recv().await else {
|
||||||
|
warn!("ME reinit scheduler stopped: trigger channel closed");
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut map_change_seen = matches!(first_trigger, MeReinitTrigger::MapChanged);
|
||||||
|
let mut periodic_seen = matches!(first_trigger, MeReinitTrigger::Periodic);
|
||||||
|
let cfg = config_rx.borrow().clone();
|
||||||
|
let coalesce_window = Duration::from_millis(cfg.general.me_reinit_coalesce_window_ms);
|
||||||
|
if !coalesce_window.is_zero() {
|
||||||
|
let deadline = tokio::time::Instant::now() + coalesce_window;
|
||||||
|
loop {
|
||||||
|
let now = tokio::time::Instant::now();
|
||||||
|
if now >= deadline {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
match tokio::time::timeout(deadline - now, trigger_rx.recv()).await {
|
||||||
|
Ok(Some(next)) => {
|
||||||
|
if next == MeReinitTrigger::MapChanged {
|
||||||
|
map_change_seen = true;
|
||||||
|
} else {
|
||||||
|
periodic_seen = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(None) => break,
|
||||||
|
Err(_) => break,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let reason = if map_change_seen && periodic_seen {
|
||||||
|
"map-change+periodic"
|
||||||
|
} else if map_change_seen {
|
||||||
|
"map-change"
|
||||||
|
} else {
|
||||||
|
"periodic"
|
||||||
|
};
|
||||||
|
|
||||||
|
if cfg.general.me_reinit_singleflight {
|
||||||
|
debug!(reason, "ME reinit scheduled (single-flight)");
|
||||||
|
pool.zero_downtime_reinit_periodic(rng.as_ref()).await;
|
||||||
|
} else {
|
||||||
|
debug!(reason, "ME reinit scheduled (concurrent mode)");
|
||||||
|
let pool_clone = pool.clone();
|
||||||
|
let rng_clone = rng.clone();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
pool_clone
|
||||||
|
.zero_downtime_reinit_periodic(rng_clone.as_ref())
|
||||||
|
.await;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Periodically enqueue reinitialization triggers for ME generations.
|
||||||
|
pub async fn me_rotation_task(
|
||||||
mut config_rx: watch::Receiver<Arc<ProxyConfig>>,
|
mut config_rx: watch::Receiver<Arc<ProxyConfig>>,
|
||||||
|
reinit_tx: mpsc::Sender<MeReinitTrigger>,
|
||||||
) {
|
) {
|
||||||
let mut interval_secs = config_rx
|
let mut interval_secs = config_rx
|
||||||
.borrow()
|
.borrow()
|
||||||
@@ -31,7 +123,7 @@ pub async fn me_rotation_task(
|
|||||||
|
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
_ = &mut sleep => {
|
_ = &mut sleep => {
|
||||||
pool.zero_downtime_reinit_periodic(rng.as_ref()).await;
|
enqueue_reinit_trigger(&reinit_tx, MeReinitTrigger::Periodic);
|
||||||
let refreshed_secs = config_rx
|
let refreshed_secs = config_rx
|
||||||
.borrow()
|
.borrow()
|
||||||
.general
|
.general
|
||||||
@@ -70,7 +162,7 @@ pub async fn me_rotation_task(
|
|||||||
);
|
);
|
||||||
interval_secs = new_secs;
|
interval_secs = new_secs;
|
||||||
interval = Duration::from_secs(interval_secs);
|
interval = Duration::from_secs(interval_secs);
|
||||||
pool.zero_downtime_reinit_periodic(rng.as_ref()).await;
|
enqueue_reinit_trigger(&reinit_tx, MeReinitTrigger::Periodic);
|
||||||
next_tick = tokio::time::Instant::now() + interval;
|
next_tick = tokio::time::Instant::now() + interval;
|
||||||
} else {
|
} else {
|
||||||
info!(
|
info!(
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
|
use std::cmp::Reverse;
|
||||||
|
use std::collections::HashMap;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::sync::atomic::Ordering;
|
use std::sync::atomic::Ordering;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use tokio::sync::mpsc::error::TrySendError;
|
||||||
use tracing::{debug, warn};
|
use tracing::{debug, warn};
|
||||||
|
|
||||||
use crate::error::{ProxyError, Result};
|
use crate::error::{ProxyError, Result};
|
||||||
@@ -11,11 +14,16 @@ use crate::protocol::constants::RPC_CLOSE_EXT_U32;
|
|||||||
|
|
||||||
use super::MePool;
|
use super::MePool;
|
||||||
use super::codec::WriterCommand;
|
use super::codec::WriterCommand;
|
||||||
|
use super::pool::WriterContour;
|
||||||
use super::wire::build_proxy_req_payload;
|
use super::wire::build_proxy_req_payload;
|
||||||
use rand::seq::SliceRandom;
|
use rand::seq::SliceRandom;
|
||||||
use super::registry::ConnMeta;
|
use super::registry::ConnMeta;
|
||||||
|
|
||||||
|
const IDLE_WRITER_PENALTY_MID_SECS: u64 = 45;
|
||||||
|
const IDLE_WRITER_PENALTY_HIGH_SECS: u64 = 55;
|
||||||
|
|
||||||
impl MePool {
|
impl MePool {
|
||||||
|
/// Send RPC_PROXY_REQ. `tag_override`: per-user ad_tag (from access.user_ad_tags); if None, uses pool default.
|
||||||
pub async fn send_proxy_req(
|
pub async fn send_proxy_req(
|
||||||
self: &Arc<Self>,
|
self: &Arc<Self>,
|
||||||
conn_id: u64,
|
conn_id: u64,
|
||||||
@@ -24,13 +32,15 @@ impl MePool {
|
|||||||
our_addr: SocketAddr,
|
our_addr: SocketAddr,
|
||||||
data: &[u8],
|
data: &[u8],
|
||||||
proto_flags: u32,
|
proto_flags: u32,
|
||||||
|
tag_override: Option<&[u8]>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
|
let tag = tag_override.or(self.proxy_tag.as_deref());
|
||||||
let payload = build_proxy_req_payload(
|
let payload = build_proxy_req_payload(
|
||||||
conn_id,
|
conn_id,
|
||||||
client_addr,
|
client_addr,
|
||||||
our_addr,
|
our_addr,
|
||||||
data,
|
data,
|
||||||
self.proxy_tag.as_deref(),
|
tag,
|
||||||
proto_flags,
|
proto_flags,
|
||||||
);
|
);
|
||||||
let meta = ConnMeta {
|
let meta = ConnMeta {
|
||||||
@@ -43,15 +53,17 @@ impl MePool {
|
|||||||
|
|
||||||
loop {
|
loop {
|
||||||
if let Some(current) = self.registry.get_writer(conn_id).await {
|
if let Some(current) = self.registry.get_writer(conn_id).await {
|
||||||
let send_res = {
|
match current.tx.try_send(WriterCommand::Data(payload.clone())) {
|
||||||
current
|
|
||||||
.tx
|
|
||||||
.send(WriterCommand::Data(payload.clone()))
|
|
||||||
.await
|
|
||||||
};
|
|
||||||
match send_res {
|
|
||||||
Ok(()) => return Ok(()),
|
Ok(()) => return Ok(()),
|
||||||
Err(_) => {
|
Err(TrySendError::Full(cmd)) => {
|
||||||
|
if current.tx.send(cmd).await.is_ok() {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
warn!(writer_id = current.writer_id, "ME writer channel closed");
|
||||||
|
self.remove_writer_and_close_clients(current.writer_id).await;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Err(TrySendError::Closed(_)) => {
|
||||||
warn!(writer_id = current.writer_id, "ME writer channel closed");
|
warn!(writer_id = current.writer_id, "ME writer channel closed");
|
||||||
self.remove_writer_and_close_clients(current.writer_id).await;
|
self.remove_writer_and_close_clients(current.writer_id).await;
|
||||||
continue;
|
continue;
|
||||||
@@ -94,7 +106,14 @@ impl MePool {
|
|||||||
ws.clone()
|
ws.clone()
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut candidate_indices = self.candidate_indices_for_dc(&writers_snapshot, target_dc).await;
|
let mut candidate_indices = self
|
||||||
|
.candidate_indices_for_dc(&writers_snapshot, target_dc, false)
|
||||||
|
.await;
|
||||||
|
if candidate_indices.is_empty() {
|
||||||
|
candidate_indices = self
|
||||||
|
.candidate_indices_for_dc(&writers_snapshot, target_dc, true)
|
||||||
|
.await;
|
||||||
|
}
|
||||||
if candidate_indices.is_empty() {
|
if candidate_indices.is_empty() {
|
||||||
// Emergency connect-on-demand
|
// Emergency connect-on-demand
|
||||||
if emergency_attempts >= 3 {
|
if emergency_attempts >= 3 {
|
||||||
@@ -120,7 +139,14 @@ impl MePool {
|
|||||||
let ws2 = self.writers.read().await;
|
let ws2 = self.writers.read().await;
|
||||||
writers_snapshot = ws2.clone();
|
writers_snapshot = ws2.clone();
|
||||||
drop(ws2);
|
drop(ws2);
|
||||||
candidate_indices = self.candidate_indices_for_dc(&writers_snapshot, target_dc).await;
|
candidate_indices = self
|
||||||
|
.candidate_indices_for_dc(&writers_snapshot, target_dc, false)
|
||||||
|
.await;
|
||||||
|
if candidate_indices.is_empty() {
|
||||||
|
candidate_indices = self
|
||||||
|
.candidate_indices_for_dc(&writers_snapshot, target_dc, true)
|
||||||
|
.await;
|
||||||
|
}
|
||||||
if !candidate_indices.is_empty() {
|
if !candidate_indices.is_empty() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -130,15 +156,62 @@ impl MePool {
|
|||||||
return Err(ProxyError::Proxy("No ME writers available for target DC".into()));
|
return Err(ProxyError::Proxy("No ME writers available for target DC".into()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
let writer_idle_since = self.registry.writer_idle_since_snapshot().await;
|
||||||
|
let now_epoch_secs = Self::now_epoch_secs();
|
||||||
|
|
||||||
candidate_indices.sort_by_key(|idx| {
|
if self.me_deterministic_writer_sort.load(Ordering::Relaxed) {
|
||||||
let w = &writers_snapshot[*idx];
|
candidate_indices.sort_by(|lhs, rhs| {
|
||||||
let degraded = w.degraded.load(Ordering::Relaxed);
|
let left = &writers_snapshot[*lhs];
|
||||||
let stale = (w.generation < self.current_generation()) as usize;
|
let right = &writers_snapshot[*rhs];
|
||||||
(stale, degraded as usize)
|
let left_key = (
|
||||||
});
|
self.writer_contour_rank_for_selection(left),
|
||||||
|
(left.generation < self.current_generation()) as usize,
|
||||||
|
left.degraded.load(Ordering::Relaxed) as usize,
|
||||||
|
self.writer_idle_rank_for_selection(
|
||||||
|
left,
|
||||||
|
&writer_idle_since,
|
||||||
|
now_epoch_secs,
|
||||||
|
),
|
||||||
|
Reverse(left.tx.capacity()),
|
||||||
|
left.addr,
|
||||||
|
left.id,
|
||||||
|
);
|
||||||
|
let right_key = (
|
||||||
|
self.writer_contour_rank_for_selection(right),
|
||||||
|
(right.generation < self.current_generation()) as usize,
|
||||||
|
right.degraded.load(Ordering::Relaxed) as usize,
|
||||||
|
self.writer_idle_rank_for_selection(
|
||||||
|
right,
|
||||||
|
&writer_idle_since,
|
||||||
|
now_epoch_secs,
|
||||||
|
),
|
||||||
|
Reverse(right.tx.capacity()),
|
||||||
|
right.addr,
|
||||||
|
right.id,
|
||||||
|
);
|
||||||
|
left_key.cmp(&right_key)
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
candidate_indices.sort_by_key(|idx| {
|
||||||
|
let w = &writers_snapshot[*idx];
|
||||||
|
let degraded = w.degraded.load(Ordering::Relaxed);
|
||||||
|
let stale = (w.generation < self.current_generation()) as usize;
|
||||||
|
(
|
||||||
|
self.writer_contour_rank_for_selection(w),
|
||||||
|
stale,
|
||||||
|
degraded as usize,
|
||||||
|
self.writer_idle_rank_for_selection(
|
||||||
|
w,
|
||||||
|
&writer_idle_since,
|
||||||
|
now_epoch_secs,
|
||||||
|
),
|
||||||
|
Reverse(w.tx.capacity()),
|
||||||
|
)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
let start = self.rr.fetch_add(1, Ordering::Relaxed) as usize % candidate_indices.len();
|
let start = self.rr.fetch_add(1, Ordering::Relaxed) as usize % candidate_indices.len();
|
||||||
|
let mut fallback_blocking_idx: Option<usize> = None;
|
||||||
|
|
||||||
for offset in 0..candidate_indices.len() {
|
for offset in 0..candidate_indices.len() {
|
||||||
let idx = candidate_indices[(start + offset) % candidate_indices.len()];
|
let idx = candidate_indices[(start + offset) % candidate_indices.len()];
|
||||||
@@ -146,29 +219,41 @@ impl MePool {
|
|||||||
if !self.writer_accepts_new_binding(w) {
|
if !self.writer_accepts_new_binding(w) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if w.tx.send(WriterCommand::Data(payload.clone())).await.is_ok() {
|
match w.tx.try_send(WriterCommand::Data(payload.clone())) {
|
||||||
self.registry
|
Ok(()) => {
|
||||||
.bind_writer(conn_id, w.id, w.tx.clone(), meta.clone())
|
self.registry
|
||||||
.await;
|
.bind_writer(conn_id, w.id, w.tx.clone(), meta.clone())
|
||||||
if w.generation < self.current_generation() {
|
.await;
|
||||||
self.stats.increment_pool_stale_pick_total();
|
if w.generation < self.current_generation() {
|
||||||
debug!(
|
self.stats.increment_pool_stale_pick_total();
|
||||||
conn_id,
|
debug!(
|
||||||
writer_id = w.id,
|
conn_id,
|
||||||
writer_generation = w.generation,
|
writer_id = w.id,
|
||||||
current_generation = self.current_generation(),
|
writer_generation = w.generation,
|
||||||
"Selected stale ME writer for fallback bind"
|
current_generation = self.current_generation(),
|
||||||
);
|
"Selected stale ME writer for fallback bind"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
Err(TrySendError::Full(_)) => {
|
||||||
|
if fallback_blocking_idx.is_none() {
|
||||||
|
fallback_blocking_idx = Some(idx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(TrySendError::Closed(_)) => {
|
||||||
|
warn!(writer_id = w.id, "ME writer channel closed");
|
||||||
|
self.remove_writer_and_close_clients(w.id).await;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
return Ok(());
|
|
||||||
} else {
|
|
||||||
warn!(writer_id = w.id, "ME writer channel closed");
|
|
||||||
self.remove_writer_and_close_clients(w.id).await;
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let w = writers_snapshot[candidate_indices[start]].clone();
|
let Some(blocking_idx) = fallback_blocking_idx else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
let w = writers_snapshot[blocking_idx].clone();
|
||||||
if !self.writer_accepts_new_binding(&w) {
|
if !self.writer_accepts_new_binding(&w) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -215,6 +300,7 @@ impl MePool {
|
|||||||
&self,
|
&self,
|
||||||
writers: &[super::pool::MeWriter],
|
writers: &[super::pool::MeWriter],
|
||||||
target_dc: i16,
|
target_dc: i16,
|
||||||
|
include_warm: bool,
|
||||||
) -> Vec<usize> {
|
) -> Vec<usize> {
|
||||||
let key = target_dc as i32;
|
let key = target_dc as i32;
|
||||||
let mut preferred = Vec::<SocketAddr>::new();
|
let mut preferred = Vec::<SocketAddr>::new();
|
||||||
@@ -258,13 +344,13 @@ impl MePool {
|
|||||||
|
|
||||||
if preferred.is_empty() {
|
if preferred.is_empty() {
|
||||||
return (0..writers.len())
|
return (0..writers.len())
|
||||||
.filter(|i| self.writer_accepts_new_binding(&writers[*i]))
|
.filter(|i| self.writer_eligible_for_selection(&writers[*i], include_warm))
|
||||||
.collect();
|
.collect();
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut out = Vec::new();
|
let mut out = Vec::new();
|
||||||
for (idx, w) in writers.iter().enumerate() {
|
for (idx, w) in writers.iter().enumerate() {
|
||||||
if !self.writer_accepts_new_binding(w) {
|
if !self.writer_eligible_for_selection(w, include_warm) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if preferred.contains(&w.addr) {
|
if preferred.contains(&w.addr) {
|
||||||
@@ -273,10 +359,52 @@ impl MePool {
|
|||||||
}
|
}
|
||||||
if out.is_empty() {
|
if out.is_empty() {
|
||||||
return (0..writers.len())
|
return (0..writers.len())
|
||||||
.filter(|i| self.writer_accepts_new_binding(&writers[*i]))
|
.filter(|i| self.writer_eligible_for_selection(&writers[*i], include_warm))
|
||||||
.collect();
|
.collect();
|
||||||
}
|
}
|
||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn writer_eligible_for_selection(
|
||||||
|
&self,
|
||||||
|
writer: &super::pool::MeWriter,
|
||||||
|
include_warm: bool,
|
||||||
|
) -> bool {
|
||||||
|
if !self.writer_accepts_new_binding(writer) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
match WriterContour::from_u8(writer.contour.load(Ordering::Relaxed)) {
|
||||||
|
WriterContour::Active => true,
|
||||||
|
WriterContour::Warm => include_warm,
|
||||||
|
WriterContour::Draining => true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn writer_contour_rank_for_selection(&self, writer: &super::pool::MeWriter) -> usize {
|
||||||
|
match WriterContour::from_u8(writer.contour.load(Ordering::Relaxed)) {
|
||||||
|
WriterContour::Active => 0,
|
||||||
|
WriterContour::Warm => 1,
|
||||||
|
WriterContour::Draining => 2,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn writer_idle_rank_for_selection(
|
||||||
|
&self,
|
||||||
|
writer: &super::pool::MeWriter,
|
||||||
|
idle_since_by_writer: &HashMap<u64, u64>,
|
||||||
|
now_epoch_secs: u64,
|
||||||
|
) -> usize {
|
||||||
|
let Some(idle_since) = idle_since_by_writer.get(&writer.id).copied() else {
|
||||||
|
return 0;
|
||||||
|
};
|
||||||
|
let idle_age_secs = now_epoch_secs.saturating_sub(idle_since);
|
||||||
|
if idle_age_secs >= IDLE_WRITER_PENALTY_HIGH_SECS {
|
||||||
|
2
|
||||||
|
} else if idle_age_secs >= IDLE_WRITER_PENALTY_MID_SECS {
|
||||||
|
1
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,5 +14,5 @@ pub use socket::*;
|
|||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
pub use socks::*;
|
pub use socks::*;
|
||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
pub use upstream::{DcPingResult, StartupPingResult, UpstreamManager};
|
pub use upstream::{DcPingResult, StartupPingResult, UpstreamEgressInfo, UpstreamManager, UpstreamRouteKind};
|
||||||
pub mod middle_proxy;
|
pub mod middle_proxy;
|
||||||
|
|||||||
@@ -233,14 +233,12 @@ async fn parse_v2<R: AsyncRead + Unpin>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Builder for PROXY protocol v1 header
|
/// Builder for PROXY protocol v1 header
|
||||||
#[allow(dead_code)]
|
|
||||||
pub struct ProxyProtocolV1Builder {
|
pub struct ProxyProtocolV1Builder {
|
||||||
family: &'static str,
|
family: &'static str,
|
||||||
src_addr: Option<SocketAddr>,
|
src_addr: Option<SocketAddr>,
|
||||||
dst_addr: Option<SocketAddr>,
|
dst_addr: Option<SocketAddr>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
impl ProxyProtocolV1Builder {
|
impl ProxyProtocolV1Builder {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
@@ -288,13 +286,17 @@ impl Default for ProxyProtocolV1Builder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Builder for PROXY protocol v2 header
|
/// Builder for PROXY protocol v2 header
|
||||||
#[allow(dead_code)]
|
|
||||||
pub struct ProxyProtocolV2Builder {
|
pub struct ProxyProtocolV2Builder {
|
||||||
src: Option<SocketAddr>,
|
src: Option<SocketAddr>,
|
||||||
dst: Option<SocketAddr>,
|
dst: Option<SocketAddr>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
impl Default for ProxyProtocolV2Builder {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl ProxyProtocolV2Builder {
|
impl ProxyProtocolV2Builder {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self { src: None, dst: None }
|
Self { src: None, dst: None }
|
||||||
|
|||||||
@@ -5,11 +5,16 @@ use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
|||||||
use tokio::net::TcpStream;
|
use tokio::net::TcpStream;
|
||||||
use crate::error::{ProxyError, Result};
|
use crate::error::{ProxyError, Result};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub struct SocksBoundAddr {
|
||||||
|
pub addr: SocketAddr,
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn connect_socks4(
|
pub async fn connect_socks4(
|
||||||
stream: &mut TcpStream,
|
stream: &mut TcpStream,
|
||||||
target: SocketAddr,
|
target: SocketAddr,
|
||||||
user_id: Option<&str>,
|
user_id: Option<&str>,
|
||||||
) -> Result<()> {
|
) -> Result<SocksBoundAddr> {
|
||||||
let ip = match target.ip() {
|
let ip = match target.ip() {
|
||||||
IpAddr::V4(ip) => ip,
|
IpAddr::V4(ip) => ip,
|
||||||
IpAddr::V6(_) => return Err(ProxyError::Proxy("SOCKS4 does not support IPv6".to_string())),
|
IpAddr::V6(_) => return Err(ProxyError::Proxy("SOCKS4 does not support IPv6".to_string())),
|
||||||
@@ -36,8 +41,13 @@ pub async fn connect_socks4(
|
|||||||
if resp[1] != 90 {
|
if resp[1] != 90 {
|
||||||
return Err(ProxyError::Proxy(format!("SOCKS4 request rejected: code {}", resp[1])));
|
return Err(ProxyError::Proxy(format!("SOCKS4 request rejected: code {}", resp[1])));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
let bound_port = u16::from_be_bytes([resp[2], resp[3]]);
|
||||||
|
let bound_ip = IpAddr::from([resp[4], resp[5], resp[6], resp[7]]);
|
||||||
|
|
||||||
|
Ok(SocksBoundAddr {
|
||||||
|
addr: SocketAddr::new(bound_ip, bound_port),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn connect_socks5(
|
pub async fn connect_socks5(
|
||||||
@@ -45,7 +55,7 @@ pub async fn connect_socks5(
|
|||||||
target: SocketAddr,
|
target: SocketAddr,
|
||||||
username: Option<&str>,
|
username: Option<&str>,
|
||||||
password: Option<&str>,
|
password: Option<&str>,
|
||||||
) -> Result<()> {
|
) -> Result<SocksBoundAddr> {
|
||||||
// 1. Auth negotiation
|
// 1. Auth negotiation
|
||||||
// VER (1) | NMETHODS (1) | METHODS (variable)
|
// VER (1) | NMETHODS (1) | METHODS (variable)
|
||||||
let mut methods = vec![0u8]; // No auth
|
let mut methods = vec![0u8]; // No auth
|
||||||
@@ -122,24 +132,36 @@ pub async fn connect_socks5(
|
|||||||
return Err(ProxyError::Proxy(format!("SOCKS5 request failed: code {}", head[1])));
|
return Err(ProxyError::Proxy(format!("SOCKS5 request failed: code {}", head[1])));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Skip address part of response
|
// Parse bound address from response.
|
||||||
match head[3] {
|
let bound_addr = match head[3] {
|
||||||
1 => { // IPv4
|
1 => { // IPv4
|
||||||
let mut addr = [0u8; 4 + 2];
|
let mut addr = [0u8; 4 + 2];
|
||||||
stream.read_exact(&mut addr).await.map_err(ProxyError::Io)?;
|
stream.read_exact(&mut addr).await.map_err(ProxyError::Io)?;
|
||||||
|
let ip = IpAddr::from([addr[0], addr[1], addr[2], addr[3]]);
|
||||||
|
let port = u16::from_be_bytes([addr[4], addr[5]]);
|
||||||
|
SocketAddr::new(ip, port)
|
||||||
},
|
},
|
||||||
3 => { // Domain
|
3 => { // Domain
|
||||||
let mut len = [0u8; 1];
|
let mut len = [0u8; 1];
|
||||||
stream.read_exact(&mut len).await.map_err(ProxyError::Io)?;
|
stream.read_exact(&mut len).await.map_err(ProxyError::Io)?;
|
||||||
let mut addr = vec![0u8; len[0] as usize + 2];
|
let mut addr = vec![0u8; len[0] as usize + 2];
|
||||||
stream.read_exact(&mut addr).await.map_err(ProxyError::Io)?;
|
stream.read_exact(&mut addr).await.map_err(ProxyError::Io)?;
|
||||||
|
// Domain-bound response is not useful for KDF IP material.
|
||||||
|
let port_pos = addr.len().saturating_sub(2);
|
||||||
|
let port = u16::from_be_bytes([addr[port_pos], addr[port_pos + 1]]);
|
||||||
|
SocketAddr::new(IpAddr::from([0, 0, 0, 0]), port)
|
||||||
},
|
},
|
||||||
4 => { // IPv6
|
4 => { // IPv6
|
||||||
let mut addr = [0u8; 16 + 2];
|
let mut addr = [0u8; 16 + 2];
|
||||||
stream.read_exact(&mut addr).await.map_err(ProxyError::Io)?;
|
stream.read_exact(&mut addr).await.map_err(ProxyError::Io)?;
|
||||||
|
let ip = IpAddr::from(<[u8; 16]>::try_from(&addr[..16]).map_err(|_| {
|
||||||
|
ProxyError::Proxy("Invalid SOCKS5 IPv6 bound address".to_string())
|
||||||
|
})?);
|
||||||
|
let port = u16::from_be_bytes([addr[16], addr[17]]);
|
||||||
|
SocketAddr::new(ip, port)
|
||||||
},
|
},
|
||||||
_ => return Err(ProxyError::Proxy("Invalid address type in SOCKS5 response".to_string())),
|
_ => return Err(ProxyError::Proxy("Invalid address type in SOCKS5 response".to_string())),
|
||||||
}
|
};
|
||||||
|
|
||||||
Ok(())
|
Ok(SocksBoundAddr { addr: bound_addr })
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user