mirror of
https://github.com/telemt/telemt.git
synced 2026-04-15 09:34:10 +03:00
Compare commits
120 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7da062e448 | ||
|
|
1fd78e012d | ||
|
|
7304dacd60 | ||
|
|
3bff0629ca | ||
|
|
a79f0bbaf5 | ||
|
|
aa535bba0a | ||
|
|
eb3245b78f | ||
|
|
da84151e9f | ||
|
|
a303fee65f | ||
|
|
bae811f8f1 | ||
|
|
8892860490 | ||
|
|
0d2958fea7 | ||
|
|
dbd9b53940 | ||
|
|
8f1f051a54 | ||
|
|
471c680def | ||
|
|
be8742a229 | ||
|
|
781947a08a | ||
|
|
b295712dbb | ||
|
|
e8454ea370 | ||
|
|
ea88a40c8f | ||
|
|
2ea4c83d9d | ||
|
|
953fab68c4 | ||
|
|
0f6621d359 | ||
|
|
82bb93e8da | ||
|
|
25b18ab064 | ||
|
|
3e0dc91db6 | ||
|
|
26270bc651 | ||
|
|
be2ec4b9b4 | ||
|
|
766806f5df | ||
|
|
26cf6ff4fa | ||
|
|
b8add81018 | ||
|
|
5be81952f3 | ||
|
|
7ce2e33bae | ||
|
|
9e2f0af5be | ||
|
|
4d72cb1680 | ||
|
|
79eebeb9ef | ||
|
|
1045289539 | ||
|
|
3d0b32edf5 | ||
|
|
41601a40fc | ||
|
|
a2cc503e81 | ||
|
|
5ee4556cea | ||
|
|
487aa8fbce | ||
|
|
32a9405002 | ||
|
|
708bedc95e | ||
|
|
ce64bf1cee | ||
|
|
f4b79f2f79 | ||
|
|
9a907a2470 | ||
|
|
e6839adc17 | ||
|
|
5e98b35fb7 | ||
|
|
af35ad3923 | ||
|
|
8f47fa6dd8 | ||
|
|
453fb477db | ||
|
|
42ae148e78 | ||
|
|
a7e840c19b | ||
|
|
1593fc4e53 | ||
|
|
fc8010a861 | ||
|
|
7293b8eb32 | ||
|
|
6934faaf93 | ||
|
|
66fdc3a34d | ||
|
|
0c4d9301ec | ||
|
|
f7a7fb94d4 | ||
|
|
85fff5e30a | ||
|
|
fc28c1ad88 | ||
|
|
bb87a37686 | ||
|
|
bf2da8f5d8 | ||
|
|
2926b9f5c8 | ||
|
|
820ed8d346 | ||
|
|
e340b716b2 | ||
|
|
9edbbb692e | ||
|
|
356d64371a | ||
|
|
4be4670668 | ||
|
|
0768fee06a | ||
|
|
35ae455e2b | ||
|
|
433e6c9a20 | ||
|
|
34f5289fc3 | ||
|
|
97804d47ff | ||
|
|
b68e9d642e | ||
|
|
f31d9d42fe | ||
|
|
d941873cce | ||
|
|
b11a767741 | ||
|
|
301f829c3c | ||
|
|
76a02610d8 | ||
|
|
76bf5337e8 | ||
|
|
e76b388a05 | ||
|
|
f37e6cbe29 | ||
|
|
e54dce5366 | ||
|
|
c7464d53e1 | ||
|
|
03a6493147 | ||
|
|
36ef2f722d | ||
|
|
b9fda9e2c2 | ||
|
|
c5b590062c | ||
|
|
c0357b2890 | ||
|
|
4f7f7d6880 | ||
|
|
efba10f839 | ||
|
|
6ba12f35d0 | ||
|
|
6a57c23700 | ||
|
|
94b85afbc5 | ||
|
|
cf717032a1 | ||
|
|
d905de2dad | ||
|
|
c7bd1c98e7 | ||
|
|
d3302d77d2 | ||
|
|
df4494c37a | ||
|
|
b84189b21b | ||
|
|
9243661f56 | ||
|
|
bffe97b2b7 | ||
|
|
bee1dd97ee | ||
|
|
16670e36f5 | ||
|
|
5dad663b25 | ||
|
|
8375608aaa | ||
|
|
0057377ac6 | ||
|
|
078ed65a0e | ||
|
|
9872f0ed1b | ||
|
|
fb0cb54776 | ||
|
|
67bae1cf2a | ||
|
|
eb9ac7fae4 | ||
|
|
8046381939 | ||
|
|
650f9fd2a4 | ||
|
|
d4ebc7b5c6 | ||
|
|
3206ce50bb | ||
|
|
bdccb866fe |
135
.github/workflows/release.yml
vendored
Normal file
135
.github/workflows/release.yml
vendored
Normal file
@@ -0,0 +1,135 @@
|
|||||||
|
name: Release
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- '[0-9]+.[0-9]+.[0-9]+' # Matches tags like 3.0.0, 3.1.2, etc.
|
||||||
|
workflow_dispatch: # Manual trigger from GitHub Actions UI
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
|
||||||
|
env:
|
||||||
|
CARGO_TERM_COLOR: always
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
name: Build ${{ matrix.target }}
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
|
||||||
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
matrix:
|
||||||
|
include:
|
||||||
|
- target: x86_64-unknown-linux-gnu
|
||||||
|
artifact_name: telemt
|
||||||
|
asset_name: telemt-x86_64-linux-gnu
|
||||||
|
- target: aarch64-unknown-linux-gnu
|
||||||
|
artifact_name: telemt
|
||||||
|
asset_name: telemt-aarch64-linux-gnu
|
||||||
|
- target: x86_64-unknown-linux-musl
|
||||||
|
artifact_name: telemt
|
||||||
|
asset_name: telemt-x86_64-linux-musl
|
||||||
|
- target: aarch64-unknown-linux-musl
|
||||||
|
artifact_name: telemt
|
||||||
|
asset_name: telemt-aarch64-linux-musl
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout repository
|
||||||
|
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||||
|
|
||||||
|
- name: Install stable Rust toolchain
|
||||||
|
uses: dtolnay/rust-toolchain@888c2e1ea69ab0d4330cbf0af1ecc7b68f368cc1 # v1
|
||||||
|
with:
|
||||||
|
toolchain: stable
|
||||||
|
targets: ${{ matrix.target }}
|
||||||
|
|
||||||
|
- name: Install cross-compilation tools
|
||||||
|
run: |
|
||||||
|
sudo apt-get update
|
||||||
|
sudo apt-get install -y gcc-aarch64-linux-gnu
|
||||||
|
|
||||||
|
- name: Cache cargo registry & build artifacts
|
||||||
|
uses: actions/cache@d4323d4df104b026a6aa633fdb11d772146be0bf # v4.2.2
|
||||||
|
with:
|
||||||
|
path: |
|
||||||
|
~/.cargo/registry
|
||||||
|
~/.cargo/git
|
||||||
|
target
|
||||||
|
key: ${{ runner.os }}-${{ matrix.target }}-cargo-${{ hashFiles('**/Cargo.lock') }}
|
||||||
|
restore-keys: |
|
||||||
|
${{ runner.os }}-${{ matrix.target }}-cargo-
|
||||||
|
|
||||||
|
- name: Install cross
|
||||||
|
run: cargo install cross --git https://github.com/cross-rs/cross
|
||||||
|
|
||||||
|
- name: Build Release
|
||||||
|
env:
|
||||||
|
RUSTFLAGS: ${{ contains(matrix.target, 'musl') && '-C target-feature=+crt-static' || '' }}
|
||||||
|
run: cross build --release --target ${{ matrix.target }}
|
||||||
|
|
||||||
|
- name: Package binary
|
||||||
|
run: |
|
||||||
|
cd target/${{ matrix.target }}/release
|
||||||
|
tar -czvf ${{ matrix.asset_name }}.tar.gz ${{ matrix.artifact_name }}
|
||||||
|
sha256sum ${{ matrix.asset_name }}.tar.gz > ${{ matrix.asset_name }}.sha256
|
||||||
|
|
||||||
|
- name: Upload artifact
|
||||||
|
uses: actions/upload-artifact@65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08 # v4.6.0
|
||||||
|
with:
|
||||||
|
name: ${{ matrix.asset_name }}
|
||||||
|
path: |
|
||||||
|
target/${{ matrix.target }}/release/${{ matrix.asset_name }}.tar.gz
|
||||||
|
target/${{ matrix.target }}/release/${{ matrix.asset_name }}.sha256
|
||||||
|
|
||||||
|
release:
|
||||||
|
name: Create Release
|
||||||
|
needs: build
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout repository
|
||||||
|
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
- name: Download all artifacts
|
||||||
|
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
|
||||||
|
with:
|
||||||
|
path: artifacts
|
||||||
|
|
||||||
|
- name: Update version in Cargo.toml and Cargo.lock
|
||||||
|
run: |
|
||||||
|
# Extract version from tag (remove 'v' prefix if present)
|
||||||
|
VERSION="${GITHUB_REF#refs/tags/}"
|
||||||
|
VERSION="${VERSION#v}"
|
||||||
|
|
||||||
|
# Install cargo-edit for version bumping
|
||||||
|
cargo install cargo-edit
|
||||||
|
|
||||||
|
# Update Cargo.toml version
|
||||||
|
cargo set-version "$VERSION"
|
||||||
|
|
||||||
|
# Configure git
|
||||||
|
git config user.name "github-actions[bot]"
|
||||||
|
git config user.email "github-actions[bot]@users.noreply.github.com"
|
||||||
|
|
||||||
|
# Commit and push changes
|
||||||
|
#git add Cargo.toml Cargo.lock
|
||||||
|
#git commit -m "chore: bump version to $VERSION" || echo "No changes to commit"
|
||||||
|
#git push origin HEAD:main
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
- name: Create Release
|
||||||
|
uses: softprops/action-gh-release@v2
|
||||||
|
with:
|
||||||
|
files: artifacts/**/*
|
||||||
|
generate_release_notes: true
|
||||||
|
draft: false
|
||||||
|
prerelease: ${{ contains(github.ref, '-rc') || contains(github.ref, '-beta') || contains(github.ref, '-alpha') }}
|
||||||
14
CONTRIBUTING.md
Normal file
14
CONTRIBUTING.md
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
# Pull Requests - Rules
|
||||||
|
## General
|
||||||
|
- ONLY signed and verified commits
|
||||||
|
- ONLY from your name
|
||||||
|
- DO NOT commit with `codex` or `claude` as author/commiter
|
||||||
|
- PREFER `flow` branch for development, not `main`
|
||||||
|
|
||||||
|
## AI
|
||||||
|
We are not against modern tools, like AI, where you act as a principal or architect, but we consider it important:
|
||||||
|
|
||||||
|
- you really understand what you're doing
|
||||||
|
- you understand the relationships and dependencies of the components being modified
|
||||||
|
- you understand the architecture of Telegram MTProto, MTProxy, Middle-End KDF at least generically
|
||||||
|
- you DO NOT commit for the sake of commits, but to help the community, core-developers and ordinary users
|
||||||
10
Cargo.toml
10
Cargo.toml
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "telemt"
|
name = "telemt"
|
||||||
version = "3.0.1"
|
version = "3.0.6"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
@@ -24,11 +24,13 @@ zeroize = { version = "1.8", features = ["derive"] }
|
|||||||
|
|
||||||
# Network
|
# Network
|
||||||
socket2 = { version = "0.5", features = ["all"] }
|
socket2 = { version = "0.5", features = ["all"] }
|
||||||
|
nix = { version = "0.28", default-features = false, features = ["net"] }
|
||||||
|
|
||||||
# Serialization
|
# Serialization
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
toml = "0.8"
|
toml = "0.8"
|
||||||
|
x509-parser = "0.15"
|
||||||
|
|
||||||
# Utils
|
# Utils
|
||||||
bytes = "1.9"
|
bytes = "1.9"
|
||||||
@@ -47,13 +49,19 @@ regex = "1.11"
|
|||||||
crossbeam-queue = "0.3"
|
crossbeam-queue = "0.3"
|
||||||
num-bigint = "0.4"
|
num-bigint = "0.4"
|
||||||
num-traits = "0.2"
|
num-traits = "0.2"
|
||||||
|
anyhow = "1.0"
|
||||||
|
|
||||||
# HTTP
|
# HTTP
|
||||||
reqwest = { version = "0.12", features = ["rustls-tls"], default-features = false }
|
reqwest = { version = "0.12", features = ["rustls-tls"], default-features = false }
|
||||||
|
notify = { version = "6", features = ["macos_fsevent"] }
|
||||||
|
ipnetwork = "0.20"
|
||||||
hyper = { version = "1", features = ["server", "http1"] }
|
hyper = { version = "1", features = ["server", "http1"] }
|
||||||
hyper-util = { version = "0.1", features = ["tokio", "server-auto"] }
|
hyper-util = { version = "0.1", features = ["tokio", "server-auto"] }
|
||||||
http-body-util = "0.1"
|
http-body-util = "0.1"
|
||||||
httpdate = "1.0"
|
httpdate = "1.0"
|
||||||
|
tokio-rustls = { version = "0.26", default-features = false, features = ["tls12"] }
|
||||||
|
rustls = { version = "0.23", default-features = false, features = ["std", "tls12", "ring"] }
|
||||||
|
webpki-roots = "0.26"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tokio-test = "0.4"
|
tokio-test = "0.4"
|
||||||
|
|||||||
17
LICENSING.md
Normal file
17
LICENSING.md
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
# LICENSING
|
||||||
|
## Licenses for Versions
|
||||||
|
| Version | License |
|
||||||
|
|---------|---------------|
|
||||||
|
| 1.0 | NO LICNESE |
|
||||||
|
| 1.1 | NO LICENSE |
|
||||||
|
| 1.2 | NO LICENSE |
|
||||||
|
| 2.0 | NO LICENSE |
|
||||||
|
| 3.0 | TELEMT UL 1 |
|
||||||
|
|
||||||
|
### License Types
|
||||||
|
- **NO LICENSE** = ***ALL RIGHT RESERVED***
|
||||||
|
- **TELEMT UL1** - work in progress license for source code of `telemt`, which encourages:
|
||||||
|
- fair use,
|
||||||
|
- contributions,
|
||||||
|
- distribution,
|
||||||
|
- but prohibits NOT mentioning the authors
|
||||||
182
README.md
182
README.md
@@ -10,74 +10,40 @@
|
|||||||
|
|
||||||
### 🇷🇺 RU
|
### 🇷🇺 RU
|
||||||
|
|
||||||
15 февраля мы опубликовали `telemt 3` с поддержкой Middle-End Proxy, а значит:
|
18 февраля мы опубликовали `telemt 3.0.3`, он имеет:
|
||||||
|
|
||||||
- с функциональными медиа, в том числе с CDN/DC=203
|
- улучшенный механизм Middle-End Health Check
|
||||||
- с Ad-tag — показывайте спонсорский канал и собирайте статистику через официального бота
|
- высокоскоростное восстановление инициализации Middle-End
|
||||||
- с новым подходом к безопасности и асинхронности
|
- меньше задержек на hot-path
|
||||||
- с высокоточной диагностикой криптографии через `ME_DIAG`
|
- более корректную работу в Dualstack, а именно - IPv6 Middle-End
|
||||||
|
- аккуратное переподключение клиента без дрифта сессий между Middle-End
|
||||||
|
- автоматическая деградация на Direct-DC при массовой (>2 ME-DC-групп) недоступности Middle-End
|
||||||
|
- автодетект IP за NAT, при возможности - будет выполнен хендшейк с ME, при неудаче - автодеградация
|
||||||
|
- единственный известный специальный DC=203 уже добавлен в код: медиа загружаются с CDN в Direct-DC режиме
|
||||||
|
|
||||||
Для использования нужно:
|
[Здесь вы можете найти релиз](https://github.com/telemt/telemt/releases/tag/3.0.3)
|
||||||
|
|
||||||
1. Версия `telemt` ≥3.0.0
|
Если у вас есть компетенции в асинхронных сетевых приложениях, анализе трафика, реверс-инжиниринге или сетевых расследованиях - мы открыты к идеям и pull requests!
|
||||||
2. Выполнение любого из наборов условий:
|
|
||||||
- публичный IP для исходящих соединений установлен на интерфейса инстанса с `telemt`
|
|
||||||
- ЛИБО
|
|
||||||
- вы используете NAT 1:1 + включили STUN-пробинг
|
|
||||||
3. В конфиге, в секции `[general]` указать:
|
|
||||||
```toml
|
|
||||||
use_middle_proxy = true
|
|
||||||
```
|
|
||||||
|
|
||||||
Если условия из пункта 1 не выполняются:
|
|
||||||
1. Выключите ME-режим:
|
|
||||||
- установите `use_middle_proxy = false`
|
|
||||||
- ЛИБО
|
|
||||||
- Middle-End Proxy будет выключен автоматически по таймауту, но это займёт больше времени при запуске
|
|
||||||
2. В конфиге, добавьте в конец:
|
|
||||||
```toml
|
|
||||||
[dc_overrides]
|
|
||||||
"203" = "91.105.192.100:443"
|
|
||||||
```
|
|
||||||
|
|
||||||
Если у вас есть компетенции в асинхронных сетевых приложениях, анализе трафика, реверс-инжиниринге или сетевых расследованиях — мы открыты к идеям и pull requests.
|
|
||||||
|
|
||||||
</td>
|
</td>
|
||||||
<td width="50%" valign="top">
|
<td width="50%" valign="top">
|
||||||
|
|
||||||
### 🇬🇧 EN
|
### 🇬🇧 EN
|
||||||
|
|
||||||
On February 15, we released `telemt 3` with support for Middle-End Proxy, which means:
|
On February 18, we released `telemt 3.0.3`. This version introduces:
|
||||||
|
|
||||||
- functional media, including CDN/DC=203
|
- improved Middle-End Health Check method
|
||||||
- Ad-tag support – promote a sponsored channel and collect statistics via Telegram bot
|
- high-speed recovery of Middle-End init
|
||||||
- new approach to security and asynchronicity
|
- reduced latency on the hot path
|
||||||
- high-precision cryptography diagnostics via `ME_DIAG`
|
- correct Dualstack support: proper handling of IPv6 Middle-End
|
||||||
|
- *clean* client reconnection without session "drift" between Middle-End
|
||||||
|
- automatic degradation to Direct-DC mode in case of large-scale (>2 ME-DC groups) Middle-End unavailability
|
||||||
|
- automatic public IP detection behind NAT; first - Middle-End handshake is performed, otherwise automatic degradation is applied
|
||||||
|
- known special DC=203 is now handled natively: media is delivered from the CDN via Direct-DC mode
|
||||||
|
|
||||||
To use this feature, the following requirements must be met:
|
[Release is available here](https://github.com/telemt/telemt/releases/tag/3.0.3)
|
||||||
1. `telemt` version ≥ 3.0.0
|
|
||||||
2. One of the following conditions satisfied:
|
|
||||||
- the instance running `telemt` has a public IP address assigned to its network interface for outbound connections
|
|
||||||
- OR
|
|
||||||
- you are using 1:1 NAT and have STUN probing enabled
|
|
||||||
3. In the config file, under the `[general]` section, specify:
|
|
||||||
```toml
|
|
||||||
use_middle_proxy = true
|
|
||||||
````
|
|
||||||
|
|
||||||
If the conditions from step 1 are not satisfied:
|
If you have expertise in asynchronous network applications, traffic analysis, reverse engineering, or network forensics - we welcome ideas and pull requests!
|
||||||
1. Disable Middle-End mode:
|
|
||||||
- set `use_middle_proxy = false`
|
|
||||||
- OR
|
|
||||||
- Middle-End Proxy will be disabled automatically after a timeout, but this will increase startup time
|
|
||||||
|
|
||||||
2. In the config file, add the following at the end:
|
|
||||||
```toml
|
|
||||||
[dc_overrides]
|
|
||||||
"203" = "91.105.192.100:443"
|
|
||||||
```
|
|
||||||
|
|
||||||
If you have expertise in asynchronous network applications, traffic analysis, reverse engineering, or network forensics — we welcome ideas, suggestions, and pull requests.
|
|
||||||
|
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
@@ -86,7 +52,9 @@ If you have expertise in asynchronous network applications, traffic analysis, re
|
|||||||
# Features
|
# Features
|
||||||
💥 The configuration structure has changed since version 1.1.0.0. change it in your environment!
|
💥 The configuration structure has changed since version 1.1.0.0. change it in your environment!
|
||||||
|
|
||||||
⚓ Our implementation of **TLS-fronting** is one of the most deeply debugged, focused, advanced and *almost* **"behaviorally consistent to real"**: we are confident we have it right - [see evidence on our validation and traces](#recognizability-for-dpi-and-crawler)
|
⚓ Our implementation of **TLS-fronting** is one of the most deeply debugged, focused, advanced and *almost* **"behaviorally consistent to real"**: we are confident we have it right - [see evidence on our validation and traces](#recognizability-for-dpi-and-crawler)
|
||||||
|
|
||||||
|
⚓ Our ***Middle-End Pool*** is fastest by design in standard scenarios, compared to other implementations of connecting to the Middle-End Proxy: non dramatically, but usual
|
||||||
|
|
||||||
# GOTO
|
# GOTO
|
||||||
- [Features](#features)
|
- [Features](#features)
|
||||||
@@ -127,7 +95,7 @@ If you have expertise in asynchronous network applications, traffic analysis, re
|
|||||||
**This software is designed for Debian-based OS: in addition to Debian, these are Ubuntu, Mint, Kali, MX and many other Linux**
|
**This software is designed for Debian-based OS: in addition to Debian, these are Ubuntu, Mint, Kali, MX and many other Linux**
|
||||||
1. Download release
|
1. Download release
|
||||||
```bash
|
```bash
|
||||||
wget https://github.com/telemt/telemt/releases/latest/download/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
|
||||||
```
|
```
|
||||||
2. Move to Bin Folder
|
2. Move to Bin Folder
|
||||||
```bash
|
```bash
|
||||||
@@ -210,46 +178,102 @@ then Ctrl+X -> Y -> Enter to save
|
|||||||
```toml
|
```toml
|
||||||
# === General Settings ===
|
# === General Settings ===
|
||||||
[general]
|
[general]
|
||||||
prefer_ipv6 = false
|
|
||||||
fast_mode = true
|
fast_mode = true
|
||||||
use_middle_proxy = false
|
use_middle_proxy = true
|
||||||
# ad_tag = "..."
|
# ad_tag = "00000000000000000000000000000000"
|
||||||
|
# Path to proxy-secret binary (auto-downloaded if missing).
|
||||||
|
proxy_secret_path = "proxy-secret"
|
||||||
|
# disable_colors = false # Disable colored output in logs (useful for files/systemd)
|
||||||
|
|
||||||
|
# === Log Level ===
|
||||||
|
# Log level: debug | verbose | normal | silent
|
||||||
|
# Can be overridden with --silent or --log-level CLI flags
|
||||||
|
# RUST_LOG env var takes absolute priority over all of these
|
||||||
|
log_level = "normal"
|
||||||
|
|
||||||
|
# === Middle Proxy - ME ===
|
||||||
|
# Public IP override for ME KDF when behind NAT; leave unset to auto-detect.
|
||||||
|
# middle_proxy_nat_ip = "203.0.113.10"
|
||||||
|
# Enable STUN probing to discover public IP:port for ME.
|
||||||
|
middle_proxy_nat_probe = true
|
||||||
|
# Primary STUN server (host:port); defaults to Telegram STUN when empty.
|
||||||
|
middle_proxy_nat_stun = "stun.l.google.com:19302"
|
||||||
|
# Optional fallback STUN servers list.
|
||||||
|
middle_proxy_nat_stun_servers = ["stun1.l.google.com:19302", "stun2.l.google.com:19302"]
|
||||||
|
# Desired number of concurrent ME writers in pool.
|
||||||
|
middle_proxy_pool_size = 16
|
||||||
|
# Pre-initialized warm-standby ME connections kept idle.
|
||||||
|
middle_proxy_warm_standby = 8
|
||||||
|
# Ignore STUN/interface mismatch and keep ME enabled even if IP differs.
|
||||||
|
stun_iface_mismatch_ignore = false
|
||||||
|
# Keepalive padding frames - fl==4
|
||||||
|
me_keepalive_enabled = true
|
||||||
|
me_keepalive_interval_secs = 25 # Period between keepalives
|
||||||
|
me_keepalive_jitter_secs = 5 # Jitter added to interval
|
||||||
|
me_keepalive_payload_random = true # Randomize 4-byte payload (vs zeros)
|
||||||
|
# Stagger extra ME connections on warmup to de-phase lifecycles.
|
||||||
|
me_warmup_stagger_enabled = true
|
||||||
|
me_warmup_step_delay_ms = 500 # Base delay between extra connects
|
||||||
|
me_warmup_step_jitter_ms = 300 # Jitter for warmup delay
|
||||||
|
# Reconnect policy knobs.
|
||||||
|
me_reconnect_max_concurrent_per_dc = 1 # Parallel reconnects per DC - EXPERIMENTAL! UNSTABLE!
|
||||||
|
me_reconnect_backoff_base_ms = 500 # Backoff start
|
||||||
|
me_reconnect_backoff_cap_ms = 30000 # Backoff cap
|
||||||
|
me_reconnect_fast_retry_count = 11 # Quick retries before backoff
|
||||||
|
|
||||||
[general.modes]
|
[general.modes]
|
||||||
classic = false
|
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)
|
||||||
|
|
||||||
|
# === Network Parameters ===
|
||||||
|
[network]
|
||||||
|
# Enable/disable families: true/false/auto(None)
|
||||||
|
ipv4 = true
|
||||||
|
ipv6 = false # UNSTABLE WITH ME
|
||||||
|
# prefer = 4 or 6
|
||||||
|
prefer = 4
|
||||||
|
multipath = false # EXPERIMENTAL!
|
||||||
|
|
||||||
# === Server Binding ===
|
# === Server Binding ===
|
||||||
[server]
|
[server]
|
||||||
port = 443
|
port = 443
|
||||||
listen_addr_ipv4 = "0.0.0.0"
|
listen_addr_ipv4 = "0.0.0.0"
|
||||||
listen_addr_ipv6 = "::"
|
listen_addr_ipv6 = "::"
|
||||||
|
# listen_unix_sock = "/var/run/telemt.sock" # Unix socket
|
||||||
|
# listen_unix_sock_perm = "0666" # Socket file permissions
|
||||||
# metrics_port = 9090
|
# metrics_port = 9090
|
||||||
# metrics_whitelist = ["127.0.0.1", "::1"]
|
# metrics_whitelist = [
|
||||||
|
# "192.168.0.0/24",
|
||||||
|
# "172.16.0.0/12",
|
||||||
|
# "127.0.0.1/32",
|
||||||
|
# "::1/128"
|
||||||
|
#]
|
||||||
|
|
||||||
# Listen on multiple interfaces/IPs (overrides listen_addr_*)
|
# Listen on multiple interfaces/IPs - IPv4
|
||||||
[[server.listeners]]
|
[[server.listeners]]
|
||||||
ip = "0.0.0.0"
|
ip = "0.0.0.0"
|
||||||
# announce = "my.hostname.tld" # Optional: hostname for tg:// links
|
|
||||||
# OR
|
|
||||||
# announce = "1.2.3.4" # Optional: Public IP for tg:// links
|
|
||||||
|
|
||||||
|
# Listen on multiple interfaces/IPs - IPv6
|
||||||
[[server.listeners]]
|
[[server.listeners]]
|
||||||
ip = "::"
|
ip = "::"
|
||||||
|
|
||||||
# Users to show in the startup log (tg:// links)
|
|
||||||
[general.links]
|
|
||||||
show = ["hello"] # Users to show in the startup log (tg:// links)
|
|
||||||
# public_host = "proxy.example.com" # Host (IP or domain) for tg:// links
|
|
||||||
# public_port = 443 # Port for tg:// links (default: server.port)
|
|
||||||
|
|
||||||
# === Timeouts (in seconds) ===
|
# === Timeouts (in seconds) ===
|
||||||
[timeouts]
|
[timeouts]
|
||||||
client_handshake = 15
|
client_handshake = 30
|
||||||
tg_connect = 10
|
tg_connect = 10
|
||||||
client_keepalive = 60
|
client_keepalive = 60
|
||||||
client_ack = 300
|
client_ack = 300
|
||||||
|
# Quick ME reconnects for single-address DCs (count and per-attempt timeout, ms).
|
||||||
|
me_one_retry = 12
|
||||||
|
me_one_timeout_ms = 1200
|
||||||
|
|
||||||
# === Anti-Censorship & Masking ===
|
# === Anti-Censorship & Masking ===
|
||||||
[censorship]
|
[censorship]
|
||||||
@@ -261,9 +285,9 @@ mask_port = 443
|
|||||||
fake_cert_len = 2048
|
fake_cert_len = 2048
|
||||||
|
|
||||||
# === Access Control & Users ===
|
# === Access Control & Users ===
|
||||||
# username "hello" is used for example
|
|
||||||
[access]
|
[access]
|
||||||
replay_check_len = 65536
|
replay_check_len = 65536
|
||||||
|
replay_window_secs = 1800
|
||||||
ignore_time_skew = false
|
ignore_time_skew = false
|
||||||
|
|
||||||
[access.users]
|
[access.users]
|
||||||
@@ -273,28 +297,28 @@ hello = "00000000000000000000000000000000"
|
|||||||
# [access.user_max_tcp_conns]
|
# [access.user_max_tcp_conns]
|
||||||
# hello = 50
|
# hello = 50
|
||||||
|
|
||||||
|
# [access.user_max_unique_ips]
|
||||||
|
# hello = 5
|
||||||
|
|
||||||
# [access.user_data_quota]
|
# [access.user_data_quota]
|
||||||
# hello = 1073741824 # 1 GB
|
# hello = 1073741824 # 1 GB
|
||||||
|
|
||||||
# === Upstreams & Routing ===
|
# === Upstreams & Routing ===
|
||||||
# By default, direct connection is used, but you can add SOCKS proxy
|
|
||||||
|
|
||||||
# Direct - Default
|
|
||||||
[[upstreams]]
|
[[upstreams]]
|
||||||
type = "direct"
|
type = "direct"
|
||||||
enabled = true
|
enabled = true
|
||||||
weight = 10
|
weight = 10
|
||||||
|
|
||||||
# SOCKS5
|
|
||||||
# [[upstreams]]
|
# [[upstreams]]
|
||||||
# type = "socks5"
|
# type = "socks5"
|
||||||
# address = "127.0.0.1:9050"
|
# address = "127.0.0.1:1080"
|
||||||
# enabled = false
|
# enabled = false
|
||||||
# weight = 1
|
# weight = 1
|
||||||
|
|
||||||
# === DC Address Overrides ===
|
# === DC Address Overrides ===
|
||||||
# [dc_overrides]
|
# [dc_overrides]
|
||||||
# "203" = "91.105.192.100:443"
|
# "203" = "91.105.192.100:443"
|
||||||
|
|
||||||
```
|
```
|
||||||
### Advanced
|
### Advanced
|
||||||
#### Adtag
|
#### Adtag
|
||||||
|
|||||||
34
ROADMAP.md
Normal file
34
ROADMAP.md
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
### 3.0.0 Anschluss
|
||||||
|
- **Middle Proxy now is stable**, confirmed on canary-deploy over ~20 users
|
||||||
|
- Ad-tag now is working
|
||||||
|
- DC=203/CDN now is working over ME
|
||||||
|
- `getProxyConfig` and `ProxySecret` are automated
|
||||||
|
- Version order is now in format `3.0.0` - without Windows-style "microfixes"
|
||||||
|
|
||||||
|
### 3.0.1 Kabelsammler
|
||||||
|
- Handshake timeouts fixed
|
||||||
|
- Connectivity logging refactored
|
||||||
|
- Docker: tmpfs for ProxyConfig and ProxySecret
|
||||||
|
- Public Host and Port in config
|
||||||
|
- ME Relays Head-of-Line Blocking fixed
|
||||||
|
- ME Ping
|
||||||
|
|
||||||
|
### 3.0.2 Microtrencher
|
||||||
|
- New [network] section
|
||||||
|
- ME Fixes
|
||||||
|
- Small bugs coverage
|
||||||
|
|
||||||
|
### 3.0.3 Ausrutscher
|
||||||
|
- ME as stateful, no conn-id migration
|
||||||
|
- No `flush()` on datapath after RpcWriter
|
||||||
|
- Hightech parser for IPv6 without regexp
|
||||||
|
- `nat_probe = true` by default
|
||||||
|
- Timeout for `recv()` in STUN-client
|
||||||
|
- ConnRegistry review
|
||||||
|
- Dualstack emergency reconnect
|
||||||
|
|
||||||
|
### 3.0.4 Schneeflecken
|
||||||
|
- Only WARN and Links in Normal log
|
||||||
|
- Consistent IP-family detection
|
||||||
|
- Includes for config
|
||||||
|
- `nonce_frame_hex` in log only with `DEBUG`
|
||||||
74
config.toml
74
config.toml
@@ -1,20 +1,69 @@
|
|||||||
# === General Settings ===
|
# === General Settings ===
|
||||||
[general]
|
[general]
|
||||||
prefer_ipv6 = true
|
|
||||||
fast_mode = true
|
fast_mode = true
|
||||||
use_middle_proxy = true
|
use_middle_proxy = true
|
||||||
#ad_tag = "00000000000000000000000000000000"
|
# ad_tag = "00000000000000000000000000000000"
|
||||||
|
# Path to proxy-secret binary (auto-downloaded if missing).
|
||||||
|
proxy_secret_path = "proxy-secret"
|
||||||
|
# disable_colors = false # Disable colored output in logs (useful for files/systemd)
|
||||||
|
|
||||||
|
# === Log Level ===
|
||||||
# Log level: debug | verbose | normal | silent
|
# Log level: debug | verbose | normal | silent
|
||||||
# Can be overridden with --silent or --log-level CLI flags
|
# Can be overridden with --silent or --log-level CLI flags
|
||||||
# RUST_LOG env var takes absolute priority over all of these
|
# RUST_LOG env var takes absolute priority over all of these
|
||||||
log_level = "normal"
|
log_level = "normal"
|
||||||
|
|
||||||
|
# === Middle Proxy - ME ===
|
||||||
|
# Public IP override for ME KDF when behind NAT; leave unset to auto-detect.
|
||||||
|
# middle_proxy_nat_ip = "203.0.113.10"
|
||||||
|
# Enable STUN probing to discover public IP:port for ME.
|
||||||
|
middle_proxy_nat_probe = true
|
||||||
|
# Primary STUN server (host:port); defaults to Telegram STUN when empty.
|
||||||
|
middle_proxy_nat_stun = "stun.l.google.com:19302"
|
||||||
|
# Optional fallback STUN servers list.
|
||||||
|
middle_proxy_nat_stun_servers = ["stun1.l.google.com:19302", "stun2.l.google.com:19302"]
|
||||||
|
# Desired number of concurrent ME writers in pool.
|
||||||
|
middle_proxy_pool_size = 16
|
||||||
|
# Pre-initialized warm-standby ME connections kept idle.
|
||||||
|
middle_proxy_warm_standby = 8
|
||||||
|
# Ignore STUN/interface mismatch and keep ME enabled even if IP differs.
|
||||||
|
stun_iface_mismatch_ignore = false
|
||||||
|
# Keepalive padding frames - fl==4
|
||||||
|
me_keepalive_enabled = true
|
||||||
|
me_keepalive_interval_secs = 25 # Period between keepalives
|
||||||
|
me_keepalive_jitter_secs = 5 # Jitter added to interval
|
||||||
|
me_keepalive_payload_random = true # Randomize 4-byte payload (vs zeros)
|
||||||
|
# Stagger extra ME connections on warmup to de-phase lifecycles.
|
||||||
|
me_warmup_stagger_enabled = true
|
||||||
|
me_warmup_step_delay_ms = 500 # Base delay between extra connects
|
||||||
|
me_warmup_step_jitter_ms = 300 # Jitter for warmup delay
|
||||||
|
# Reconnect policy knobs.
|
||||||
|
me_reconnect_max_concurrent_per_dc = 1 # Parallel reconnects per DC - EXPERIMENTAL! UNSTABLE!
|
||||||
|
me_reconnect_backoff_base_ms = 500 # Backoff start
|
||||||
|
me_reconnect_backoff_cap_ms = 30000 # Backoff cap
|
||||||
|
me_reconnect_fast_retry_count = 11 # Quick retries before backoff
|
||||||
|
|
||||||
[general.modes]
|
[general.modes]
|
||||||
classic = false
|
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)
|
||||||
|
|
||||||
|
# === Network Parameters ===
|
||||||
|
[network]
|
||||||
|
# Enable/disable families: true/false/auto(None)
|
||||||
|
ipv4 = true
|
||||||
|
ipv6 = false # UNSTABLE WITH ME
|
||||||
|
# prefer = 4 or 6
|
||||||
|
prefer = 4
|
||||||
|
multipath = false # EXPERIMENTAL!
|
||||||
|
|
||||||
# === Server Binding ===
|
# === Server Binding ===
|
||||||
[server]
|
[server]
|
||||||
port = 443
|
port = 443
|
||||||
@@ -22,38 +71,39 @@ listen_addr_ipv4 = "0.0.0.0"
|
|||||||
listen_addr_ipv6 = "::"
|
listen_addr_ipv6 = "::"
|
||||||
# listen_unix_sock = "/var/run/telemt.sock" # Unix socket
|
# listen_unix_sock = "/var/run/telemt.sock" # Unix socket
|
||||||
# listen_unix_sock_perm = "0666" # Socket file permissions
|
# listen_unix_sock_perm = "0666" # Socket file permissions
|
||||||
|
# proxy_protocol = false # Enable if behind HAProxy/nginx with PROXY protocol
|
||||||
# metrics_port = 9090
|
# metrics_port = 9090
|
||||||
# metrics_whitelist = ["127.0.0.1", "::1"]
|
# metrics_whitelist = ["127.0.0.1", "::1"]
|
||||||
|
|
||||||
# Listen on multiple interfaces/IPs (overrides listen_addr_*)
|
# Listen on multiple interfaces/IPs - IPv4
|
||||||
[[server.listeners]]
|
[[server.listeners]]
|
||||||
ip = "0.0.0.0"
|
ip = "0.0.0.0"
|
||||||
# announce_ip = "1.2.3.4" # Optional: Public IP for tg:// links
|
|
||||||
|
|
||||||
|
# Listen on multiple interfaces/IPs - IPv6
|
||||||
[[server.listeners]]
|
[[server.listeners]]
|
||||||
ip = "::"
|
ip = "::"
|
||||||
|
|
||||||
# Users to show in the startup log (tg:// links)
|
|
||||||
[general.links]
|
|
||||||
show = ["hello"] # Users to show in the startup log (tg:// links)
|
|
||||||
# public_host = "proxy.example.com" # Host (IP or domain) for tg:// links
|
|
||||||
# public_port = 443 # Port for tg:// links (default: server.port)
|
|
||||||
|
|
||||||
# === Timeouts (in seconds) ===
|
# === Timeouts (in seconds) ===
|
||||||
[timeouts]
|
[timeouts]
|
||||||
client_handshake = 15
|
client_handshake = 30
|
||||||
tg_connect = 10
|
tg_connect = 10
|
||||||
client_keepalive = 60
|
client_keepalive = 60
|
||||||
client_ack = 300
|
client_ack = 300
|
||||||
|
# Quick ME reconnects for single-address DCs (count and per-attempt timeout, ms).
|
||||||
|
me_one_retry = 12
|
||||||
|
me_one_timeout_ms = 1200
|
||||||
|
|
||||||
# === Anti-Censorship & Masking ===
|
# === Anti-Censorship & Masking ===
|
||||||
[censorship]
|
[censorship]
|
||||||
tls_domain = "petrovich.ru"
|
tls_domain = "petrovich.ru"
|
||||||
|
# tls_domains = ["example.com", "cdn.example.net"] # Additional domains for EE links
|
||||||
mask = true
|
mask = true
|
||||||
mask_port = 443
|
mask_port = 443
|
||||||
# mask_host = "petrovich.ru" # Defaults to tls_domain if not set
|
# mask_host = "petrovich.ru" # Defaults to tls_domain if not set
|
||||||
# mask_unix_sock = "/var/run/nginx.sock" # Unix socket (mutually exclusive with mask_host)
|
# mask_unix_sock = "/var/run/nginx.sock" # Unix socket (mutually exclusive with mask_host)
|
||||||
fake_cert_len = 2048
|
fake_cert_len = 2048
|
||||||
|
# tls_emulation = false # Fetch real cert lengths and emulate TLS records
|
||||||
|
# tls_front_dir = "tlsfront" # Cache directory for TLS emulation
|
||||||
|
|
||||||
# === Access Control & Users ===
|
# === Access Control & Users ===
|
||||||
[access]
|
[access]
|
||||||
@@ -79,6 +129,8 @@ hello = "00000000000000000000000000000000"
|
|||||||
type = "direct"
|
type = "direct"
|
||||||
enabled = true
|
enabled = true
|
||||||
weight = 10
|
weight = 10
|
||||||
|
# interface = "192.168.1.100" # Bind outgoing to specific IP or iface name
|
||||||
|
# bind_addresses = ["192.168.1.100"] # List for round-robin binding (family must match target)
|
||||||
|
|
||||||
# [[upstreams]]
|
# [[upstreams]]
|
||||||
# type = "socks5"
|
# type = "socks5"
|
||||||
|
|||||||
@@ -189,11 +189,18 @@ r#"# Telemt MTProxy — auto-generated config
|
|||||||
show_link = ["{username}"]
|
show_link = ["{username}"]
|
||||||
|
|
||||||
[general]
|
[general]
|
||||||
|
# prefer_ipv6 is deprecated; use [network].prefer
|
||||||
prefer_ipv6 = false
|
prefer_ipv6 = false
|
||||||
fast_mode = true
|
fast_mode = true
|
||||||
use_middle_proxy = false
|
use_middle_proxy = false
|
||||||
log_level = "normal"
|
log_level = "normal"
|
||||||
|
|
||||||
|
[network]
|
||||||
|
ipv4 = true
|
||||||
|
ipv6 = true
|
||||||
|
prefer = 4
|
||||||
|
multipath = false
|
||||||
|
|
||||||
[general.modes]
|
[general.modes]
|
||||||
classic = false
|
classic = false
|
||||||
secure = false
|
secure = false
|
||||||
@@ -297,4 +304,4 @@ fn print_links(username: &str, secret: &str, port: u16, domain: &str) {
|
|||||||
println!("The proxy will auto-detect and display the correct link on startup.");
|
println!("The proxy will auto-detect and display the correct link on startup.");
|
||||||
println!("Check: journalctl -u telemt.service | head -30");
|
println!("Check: journalctl -u telemt.service | head -30");
|
||||||
println!("===================");
|
println!("===================");
|
||||||
}
|
}
|
||||||
|
|||||||
141
src/config/defaults.rs
Normal file
141
src/config/defaults.rs
Normal file
@@ -0,0 +1,141 @@
|
|||||||
|
use std::net::IpAddr;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use ipnetwork::IpNetwork;
|
||||||
|
use serde::Deserialize;
|
||||||
|
|
||||||
|
// Helper defaults kept private to the config module.
|
||||||
|
pub(crate) fn default_true() -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_port() -> u16 {
|
||||||
|
443
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_tls_domain() -> String {
|
||||||
|
"www.google.com".to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_mask_port() -> u16 {
|
||||||
|
443
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_fake_cert_len() -> usize {
|
||||||
|
2048
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_tls_front_dir() -> String {
|
||||||
|
"tlsfront".to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_replay_check_len() -> usize {
|
||||||
|
65_536
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_replay_window_secs() -> u64 {
|
||||||
|
1800
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_handshake_timeout() -> u64 {
|
||||||
|
15
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_connect_timeout() -> u64 {
|
||||||
|
10
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_keepalive() -> u64 {
|
||||||
|
60
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_ack_timeout() -> u64 {
|
||||||
|
300
|
||||||
|
}
|
||||||
|
pub(crate) fn default_me_one_retry() -> u8 {
|
||||||
|
3
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_one_timeout() -> u64 {
|
||||||
|
1500
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_listen_addr() -> String {
|
||||||
|
"0.0.0.0".to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_weight() -> u16 {
|
||||||
|
1
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_metrics_whitelist() -> Vec<IpNetwork> {
|
||||||
|
vec![
|
||||||
|
"127.0.0.1/32".parse().unwrap(),
|
||||||
|
"::1/128".parse().unwrap(),
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_prefer_4() -> u8 {
|
||||||
|
4
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_unknown_dc_log_path() -> Option<String> {
|
||||||
|
Some("unknown-dc.txt".to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_pool_size() -> usize {
|
||||||
|
2
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_keepalive_interval() -> u64 {
|
||||||
|
25
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_keepalive_jitter() -> u64 {
|
||||||
|
5
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_warmup_step_delay_ms() -> u64 {
|
||||||
|
500
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_warmup_step_jitter_ms() -> u64 {
|
||||||
|
300
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_reconnect_backoff_base_ms() -> u64 {
|
||||||
|
500
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_reconnect_backoff_cap_ms() -> u64 {
|
||||||
|
30_000
|
||||||
|
}
|
||||||
|
|
||||||
|
// Custom deserializer helpers
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
#[serde(untagged)]
|
||||||
|
pub(crate) enum OneOrMany {
|
||||||
|
One(String),
|
||||||
|
Many(Vec<String>),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn deserialize_dc_overrides<'de, D>(
|
||||||
|
deserializer: D,
|
||||||
|
) -> std::result::Result<HashMap<String, Vec<String>>, D::Error>
|
||||||
|
where
|
||||||
|
D: serde::de::Deserializer<'de>,
|
||||||
|
{
|
||||||
|
let raw: HashMap<String, OneOrMany> = HashMap::deserialize(deserializer)?;
|
||||||
|
let mut out = HashMap::new();
|
||||||
|
for (dc, val) in raw {
|
||||||
|
let mut addrs = match val {
|
||||||
|
OneOrMany::One(s) => vec![s],
|
||||||
|
OneOrMany::Many(v) => v,
|
||||||
|
};
|
||||||
|
addrs.retain(|s| !s.trim().is_empty());
|
||||||
|
if !addrs.is_empty() {
|
||||||
|
out.insert(dc, addrs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(out)
|
||||||
|
}
|
||||||
433
src/config/hot_reload.rs
Normal file
433
src/config/hot_reload.rs
Normal file
@@ -0,0 +1,433 @@
|
|||||||
|
//! Hot-reload: watches the config file via inotify (Linux) / FSEvents (macOS)
|
||||||
|
//! / ReadDirectoryChangesW (Windows) using the `notify` crate.
|
||||||
|
//! SIGHUP is also supported on Unix as an additional manual trigger.
|
||||||
|
//!
|
||||||
|
//! # What can be reloaded without restart
|
||||||
|
//!
|
||||||
|
//! | Section | Field | Effect |
|
||||||
|
//! |-----------|-------------------------------|-----------------------------------|
|
||||||
|
//! | `general` | `log_level` | Filter updated via `log_level_tx` |
|
||||||
|
//! | `general` | `ad_tag` | Passed on next connection |
|
||||||
|
//! | `general` | `middle_proxy_pool_size` | Passed on next connection |
|
||||||
|
//! | `general` | `me_keepalive_*` | Passed on next connection |
|
||||||
|
//! | `access` | All user/quota fields | Effective immediately |
|
||||||
|
//!
|
||||||
|
//! Fields that require re-binding sockets (`server.port`, `censorship.*`,
|
||||||
|
//! `network.*`, `use_middle_proxy`) are **not** applied; a warning is emitted.
|
||||||
|
|
||||||
|
use std::net::IpAddr;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use notify::{EventKind, RecursiveMode, Watcher, recommended_watcher};
|
||||||
|
use tokio::sync::{mpsc, watch};
|
||||||
|
use tracing::{error, info, warn};
|
||||||
|
|
||||||
|
use crate::config::LogLevel;
|
||||||
|
use super::load::ProxyConfig;
|
||||||
|
|
||||||
|
// ── Hot fields ────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
/// Fields that are safe to swap without restarting listeners.
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
pub struct HotFields {
|
||||||
|
pub log_level: LogLevel,
|
||||||
|
pub ad_tag: Option<String>,
|
||||||
|
pub middle_proxy_pool_size: usize,
|
||||||
|
pub me_keepalive_enabled: bool,
|
||||||
|
pub me_keepalive_interval_secs: u64,
|
||||||
|
pub me_keepalive_jitter_secs: u64,
|
||||||
|
pub me_keepalive_payload_random: bool,
|
||||||
|
pub access: crate::config::AccessConfig,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HotFields {
|
||||||
|
pub fn from_config(cfg: &ProxyConfig) -> Self {
|
||||||
|
Self {
|
||||||
|
log_level: cfg.general.log_level.clone(),
|
||||||
|
ad_tag: cfg.general.ad_tag.clone(),
|
||||||
|
middle_proxy_pool_size: cfg.general.middle_proxy_pool_size,
|
||||||
|
me_keepalive_enabled: cfg.general.me_keepalive_enabled,
|
||||||
|
me_keepalive_interval_secs: cfg.general.me_keepalive_interval_secs,
|
||||||
|
me_keepalive_jitter_secs: cfg.general.me_keepalive_jitter_secs,
|
||||||
|
me_keepalive_payload_random: cfg.general.me_keepalive_payload_random,
|
||||||
|
access: cfg.access.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Helpers ───────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
/// Warn if any non-hot fields changed (require restart).
|
||||||
|
fn warn_non_hot_changes(old: &ProxyConfig, new: &ProxyConfig) {
|
||||||
|
if old.server.port != new.server.port {
|
||||||
|
warn!(
|
||||||
|
"config reload: server.port changed ({} → {}); restart required",
|
||||||
|
old.server.port, new.server.port
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if old.censorship.tls_domain != new.censorship.tls_domain {
|
||||||
|
warn!(
|
||||||
|
"config reload: censorship.tls_domain changed ('{}' → '{}'); restart required",
|
||||||
|
old.censorship.tls_domain, new.censorship.tls_domain
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if old.network.ipv4 != new.network.ipv4 || old.network.ipv6 != new.network.ipv6 {
|
||||||
|
warn!("config reload: network.ipv4/ipv6 changed; restart required");
|
||||||
|
}
|
||||||
|
if old.general.use_middle_proxy != new.general.use_middle_proxy {
|
||||||
|
warn!("config reload: use_middle_proxy changed; restart required");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Resolve the public host for link generation — mirrors the logic in main.rs.
|
||||||
|
///
|
||||||
|
/// Priority:
|
||||||
|
/// 1. `[general.links] public_host` — explicit override in config
|
||||||
|
/// 2. `detected_ip_v4` — from STUN/interface probe at startup
|
||||||
|
/// 3. `detected_ip_v6` — fallback
|
||||||
|
/// 4. `"UNKNOWN"` — warn the user to set `public_host`
|
||||||
|
fn resolve_link_host(
|
||||||
|
cfg: &ProxyConfig,
|
||||||
|
detected_ip_v4: Option<IpAddr>,
|
||||||
|
detected_ip_v6: Option<IpAddr>,
|
||||||
|
) -> String {
|
||||||
|
if let Some(ref h) = cfg.general.links.public_host {
|
||||||
|
return h.clone();
|
||||||
|
}
|
||||||
|
detected_ip_v4
|
||||||
|
.or(detected_ip_v6)
|
||||||
|
.map(|ip| ip.to_string())
|
||||||
|
.unwrap_or_else(|| {
|
||||||
|
warn!(
|
||||||
|
"config reload: could not determine public IP for proxy links. \
|
||||||
|
Set [general.links] public_host in config."
|
||||||
|
);
|
||||||
|
"UNKNOWN".to_string()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Print TG proxy links for a single user — mirrors print_proxy_links() in main.rs.
|
||||||
|
fn print_user_links(user: &str, secret: &str, host: &str, port: u16, cfg: &ProxyConfig) {
|
||||||
|
info!(target: "telemt::links", "--- New user: {} ---", user);
|
||||||
|
if cfg.general.modes.classic {
|
||||||
|
info!(
|
||||||
|
target: "telemt::links",
|
||||||
|
" Classic: tg://proxy?server={}&port={}&secret={}",
|
||||||
|
host, port, secret
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if cfg.general.modes.secure {
|
||||||
|
info!(
|
||||||
|
target: "telemt::links",
|
||||||
|
" DD: tg://proxy?server={}&port={}&secret=dd{}",
|
||||||
|
host, port, secret
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if cfg.general.modes.tls {
|
||||||
|
let mut domains = vec![cfg.censorship.tls_domain.clone()];
|
||||||
|
for d in &cfg.censorship.tls_domains {
|
||||||
|
if !domains.contains(d) {
|
||||||
|
domains.push(d.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for domain in &domains {
|
||||||
|
let domain_hex = hex::encode(domain.as_bytes());
|
||||||
|
info!(
|
||||||
|
target: "telemt::links",
|
||||||
|
" EE-TLS: tg://proxy?server={}&port={}&secret=ee{}{}",
|
||||||
|
host, port, secret, domain_hex
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
info!(target: "telemt::links", "--------------------");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Log all detected changes and emit TG links for new users.
|
||||||
|
fn log_changes(
|
||||||
|
old_hot: &HotFields,
|
||||||
|
new_hot: &HotFields,
|
||||||
|
new_cfg: &ProxyConfig,
|
||||||
|
log_tx: &watch::Sender<LogLevel>,
|
||||||
|
detected_ip_v4: Option<IpAddr>,
|
||||||
|
detected_ip_v6: Option<IpAddr>,
|
||||||
|
) {
|
||||||
|
if old_hot.log_level != new_hot.log_level {
|
||||||
|
info!(
|
||||||
|
"config reload: log_level: '{}' → '{}'",
|
||||||
|
old_hot.log_level, new_hot.log_level
|
||||||
|
);
|
||||||
|
log_tx.send(new_hot.log_level.clone()).ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
if old_hot.ad_tag != new_hot.ad_tag {
|
||||||
|
info!(
|
||||||
|
"config reload: ad_tag: {} → {}",
|
||||||
|
old_hot.ad_tag.as_deref().unwrap_or("none"),
|
||||||
|
new_hot.ad_tag.as_deref().unwrap_or("none"),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if old_hot.middle_proxy_pool_size != new_hot.middle_proxy_pool_size {
|
||||||
|
info!(
|
||||||
|
"config reload: middle_proxy_pool_size: {} → {}",
|
||||||
|
old_hot.middle_proxy_pool_size, new_hot.middle_proxy_pool_size,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if old_hot.me_keepalive_enabled != new_hot.me_keepalive_enabled
|
||||||
|
|| old_hot.me_keepalive_interval_secs != new_hot.me_keepalive_interval_secs
|
||||||
|
|| old_hot.me_keepalive_jitter_secs != new_hot.me_keepalive_jitter_secs
|
||||||
|
|| old_hot.me_keepalive_payload_random != new_hot.me_keepalive_payload_random
|
||||||
|
{
|
||||||
|
info!(
|
||||||
|
"config reload: me_keepalive: enabled={} interval={}s jitter={}s random_payload={}",
|
||||||
|
new_hot.me_keepalive_enabled,
|
||||||
|
new_hot.me_keepalive_interval_secs,
|
||||||
|
new_hot.me_keepalive_jitter_secs,
|
||||||
|
new_hot.me_keepalive_payload_random,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if old_hot.access.users != new_hot.access.users {
|
||||||
|
let mut added: Vec<&String> = new_hot.access.users.keys()
|
||||||
|
.filter(|u| !old_hot.access.users.contains_key(*u))
|
||||||
|
.collect();
|
||||||
|
added.sort();
|
||||||
|
|
||||||
|
let mut removed: Vec<&String> = old_hot.access.users.keys()
|
||||||
|
.filter(|u| !new_hot.access.users.contains_key(*u))
|
||||||
|
.collect();
|
||||||
|
removed.sort();
|
||||||
|
|
||||||
|
let mut changed: Vec<&String> = new_hot.access.users.keys()
|
||||||
|
.filter(|u| {
|
||||||
|
old_hot.access.users.get(*u)
|
||||||
|
.map(|s| s != &new_hot.access.users[*u])
|
||||||
|
.unwrap_or(false)
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
changed.sort();
|
||||||
|
|
||||||
|
if !added.is_empty() {
|
||||||
|
info!(
|
||||||
|
"config reload: users added: [{}]",
|
||||||
|
added.iter().map(|s| s.as_str()).collect::<Vec<_>>().join(", ")
|
||||||
|
);
|
||||||
|
let host = resolve_link_host(new_cfg, detected_ip_v4, detected_ip_v6);
|
||||||
|
let port = new_cfg.general.links.public_port.unwrap_or(new_cfg.server.port);
|
||||||
|
for user in &added {
|
||||||
|
if let Some(secret) = new_hot.access.users.get(*user) {
|
||||||
|
print_user_links(user, secret, &host, port, new_cfg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !removed.is_empty() {
|
||||||
|
info!(
|
||||||
|
"config reload: users removed: [{}]",
|
||||||
|
removed.iter().map(|s| s.as_str()).collect::<Vec<_>>().join(", ")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if !changed.is_empty() {
|
||||||
|
info!(
|
||||||
|
"config reload: users secret changed: [{}]",
|
||||||
|
changed.iter().map(|s| s.as_str()).collect::<Vec<_>>().join(", ")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if old_hot.access.user_max_tcp_conns != new_hot.access.user_max_tcp_conns {
|
||||||
|
info!(
|
||||||
|
"config reload: user_max_tcp_conns updated ({} entries)",
|
||||||
|
new_hot.access.user_max_tcp_conns.len()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if old_hot.access.user_expirations != new_hot.access.user_expirations {
|
||||||
|
info!(
|
||||||
|
"config reload: user_expirations updated ({} entries)",
|
||||||
|
new_hot.access.user_expirations.len()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if old_hot.access.user_data_quota != new_hot.access.user_data_quota {
|
||||||
|
info!(
|
||||||
|
"config reload: user_data_quota updated ({} entries)",
|
||||||
|
new_hot.access.user_data_quota.len()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if old_hot.access.user_max_unique_ips != new_hot.access.user_max_unique_ips {
|
||||||
|
info!(
|
||||||
|
"config reload: user_max_unique_ips updated ({} entries)",
|
||||||
|
new_hot.access.user_max_unique_ips.len()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Load config, validate, diff against current, and broadcast if changed.
|
||||||
|
fn reload_config(
|
||||||
|
config_path: &PathBuf,
|
||||||
|
config_tx: &watch::Sender<Arc<ProxyConfig>>,
|
||||||
|
log_tx: &watch::Sender<LogLevel>,
|
||||||
|
detected_ip_v4: Option<IpAddr>,
|
||||||
|
detected_ip_v6: Option<IpAddr>,
|
||||||
|
) {
|
||||||
|
let new_cfg = match ProxyConfig::load(config_path) {
|
||||||
|
Ok(c) => c,
|
||||||
|
Err(e) => {
|
||||||
|
error!("config reload: failed to parse {:?}: {}", config_path, e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Err(e) = new_cfg.validate() {
|
||||||
|
error!("config reload: validation failed: {}; keeping old config", e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let old_cfg = config_tx.borrow().clone();
|
||||||
|
let old_hot = HotFields::from_config(&old_cfg);
|
||||||
|
let new_hot = HotFields::from_config(&new_cfg);
|
||||||
|
|
||||||
|
if old_hot == new_hot {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
warn_non_hot_changes(&old_cfg, &new_cfg);
|
||||||
|
log_changes(&old_hot, &new_hot, &new_cfg, log_tx, detected_ip_v4, detected_ip_v6);
|
||||||
|
config_tx.send(Arc::new(new_cfg)).ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Public API ────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
/// Spawn the hot-reload watcher task.
|
||||||
|
///
|
||||||
|
/// Uses `notify` (inotify on Linux) to detect file changes instantly.
|
||||||
|
/// SIGHUP is also handled on Unix as an additional manual trigger.
|
||||||
|
///
|
||||||
|
/// `detected_ip_v4` / `detected_ip_v6` are the IPs discovered during the
|
||||||
|
/// startup probe — used when generating proxy links for newly added users,
|
||||||
|
/// matching the same logic as the startup output.
|
||||||
|
pub fn spawn_config_watcher(
|
||||||
|
config_path: PathBuf,
|
||||||
|
initial: Arc<ProxyConfig>,
|
||||||
|
detected_ip_v4: Option<IpAddr>,
|
||||||
|
detected_ip_v6: Option<IpAddr>,
|
||||||
|
) -> (watch::Receiver<Arc<ProxyConfig>>, watch::Receiver<LogLevel>) {
|
||||||
|
let initial_level = initial.general.log_level.clone();
|
||||||
|
let (config_tx, config_rx) = watch::channel(initial);
|
||||||
|
let (log_tx, log_rx) = watch::channel(initial_level);
|
||||||
|
|
||||||
|
// Bridge: sync notify callback → async task via mpsc.
|
||||||
|
let (notify_tx, mut notify_rx) = mpsc::channel::<()>(4);
|
||||||
|
|
||||||
|
// Canonicalize the config path so it matches what notify returns in events
|
||||||
|
// (notify always gives absolute paths, but config_path may be relative).
|
||||||
|
let config_path = match config_path.canonicalize() {
|
||||||
|
Ok(p) => p,
|
||||||
|
Err(_) => config_path.to_path_buf(), // file doesn't exist yet, use as-is
|
||||||
|
};
|
||||||
|
|
||||||
|
// Watch the parent directory rather than the file itself, because many
|
||||||
|
// editors (vim, nano, systemd-sysusers) write via rename, which would
|
||||||
|
// cause inotify to lose track of the original inode.
|
||||||
|
let watch_dir = config_path
|
||||||
|
.parent()
|
||||||
|
.unwrap_or_else(|| std::path::Path::new("."))
|
||||||
|
.to_path_buf();
|
||||||
|
|
||||||
|
let config_file = config_path.clone();
|
||||||
|
let tx_clone = notify_tx.clone();
|
||||||
|
|
||||||
|
let watcher_result = recommended_watcher(move |res: notify::Result<notify::Event>| {
|
||||||
|
let Ok(event) = res else { return };
|
||||||
|
|
||||||
|
let is_our_file = event.paths.iter().any(|p| p == &config_file);
|
||||||
|
if !is_our_file {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let relevant = matches!(
|
||||||
|
event.kind,
|
||||||
|
EventKind::Modify(_) | EventKind::Create(_) | EventKind::Remove(_)
|
||||||
|
);
|
||||||
|
if relevant {
|
||||||
|
let _ = tx_clone.try_send(());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
match watcher_result {
|
||||||
|
Ok(mut watcher) => {
|
||||||
|
match watcher.watch(&watch_dir, RecursiveMode::NonRecursive) {
|
||||||
|
Ok(()) => info!("config watcher: watching {:?} via inotify", config_path),
|
||||||
|
Err(e) => warn!(
|
||||||
|
"config watcher: failed to watch {:?}: {}; use SIGHUP to reload",
|
||||||
|
watch_dir, e
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
tokio::spawn(async move {
|
||||||
|
let _watcher = watcher; // keep alive
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
|
let mut sighup = {
|
||||||
|
use tokio::signal::unix::{SignalKind, signal};
|
||||||
|
signal(SignalKind::hangup()).expect("Failed to register SIGHUP handler")
|
||||||
|
};
|
||||||
|
|
||||||
|
loop {
|
||||||
|
#[cfg(unix)]
|
||||||
|
tokio::select! {
|
||||||
|
msg = notify_rx.recv() => {
|
||||||
|
if msg.is_none() { break; }
|
||||||
|
}
|
||||||
|
_ = sighup.recv() => {
|
||||||
|
info!("SIGHUP received — reloading {:?}", config_path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[cfg(not(unix))]
|
||||||
|
if notify_rx.recv().await.is_none() { break; }
|
||||||
|
|
||||||
|
// Debounce: drain extra events fired within 50ms.
|
||||||
|
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
|
||||||
|
while notify_rx.try_recv().is_ok() {}
|
||||||
|
|
||||||
|
reload_config(
|
||||||
|
&config_path,
|
||||||
|
&config_tx,
|
||||||
|
&log_tx,
|
||||||
|
detected_ip_v4,
|
||||||
|
detected_ip_v6,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
warn!(
|
||||||
|
"config watcher: inotify unavailable ({}); only SIGHUP will trigger reload",
|
||||||
|
e
|
||||||
|
);
|
||||||
|
// Fall back to SIGHUP-only.
|
||||||
|
tokio::spawn(async move {
|
||||||
|
#[cfg(unix)]
|
||||||
|
{
|
||||||
|
use tokio::signal::unix::{SignalKind, signal};
|
||||||
|
let mut sighup = signal(SignalKind::hangup())
|
||||||
|
.expect("Failed to register SIGHUP handler");
|
||||||
|
loop {
|
||||||
|
sighup.recv().await;
|
||||||
|
info!("SIGHUP received — reloading {:?}", config_path);
|
||||||
|
reload_config(
|
||||||
|
&config_path,
|
||||||
|
&config_tx,
|
||||||
|
&log_tx,
|
||||||
|
detected_ip_v4,
|
||||||
|
detected_ip_v6,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[cfg(not(unix))]
|
||||||
|
let _ = (config_tx, log_tx, config_path);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
(config_rx, log_rx)
|
||||||
|
}
|
||||||
348
src/config/load.rs
Normal file
348
src/config/load.rs
Normal file
@@ -0,0 +1,348 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
use std::net::IpAddr;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
use rand::Rng;
|
||||||
|
use tracing::warn;
|
||||||
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
|
use crate::error::{ProxyError, Result};
|
||||||
|
|
||||||
|
use super::defaults::*;
|
||||||
|
use super::types::*;
|
||||||
|
|
||||||
|
fn preprocess_includes(content: &str, base_dir: &Path, depth: u8) -> Result<String> {
|
||||||
|
if depth > 10 {
|
||||||
|
return Err(ProxyError::Config("Include depth > 10".into()));
|
||||||
|
}
|
||||||
|
let mut output = String::with_capacity(content.len());
|
||||||
|
for line in content.lines() {
|
||||||
|
let trimmed = line.trim();
|
||||||
|
if let Some(rest) = trimmed.strip_prefix("include") {
|
||||||
|
let rest = rest.trim();
|
||||||
|
if let Some(rest) = rest.strip_prefix('=') {
|
||||||
|
let path_str = rest.trim().trim_matches('"');
|
||||||
|
let resolved = base_dir.join(path_str);
|
||||||
|
let included = std::fs::read_to_string(&resolved)
|
||||||
|
.map_err(|e| ProxyError::Config(e.to_string()))?;
|
||||||
|
let included_dir = resolved.parent().unwrap_or(base_dir);
|
||||||
|
output.push_str(&preprocess_includes(&included, included_dir, depth + 1)?);
|
||||||
|
output.push('\n');
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
output.push_str(line);
|
||||||
|
output.push('\n');
|
||||||
|
}
|
||||||
|
Ok(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn validate_network_cfg(net: &mut NetworkConfig) -> Result<()> {
|
||||||
|
if !net.ipv4 && matches!(net.ipv6, Some(false)) {
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"Both ipv4 and ipv6 are disabled in [network]".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if net.prefer != 4 && net.prefer != 6 {
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"network.prefer must be 4 or 6".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if !net.ipv4 && net.prefer == 4 {
|
||||||
|
warn!("prefer=4 but ipv4=false; forcing prefer=6");
|
||||||
|
net.prefer = 6;
|
||||||
|
}
|
||||||
|
|
||||||
|
if matches!(net.ipv6, Some(false)) && net.prefer == 6 {
|
||||||
|
warn!("prefer=6 but ipv6=false; forcing prefer=4");
|
||||||
|
net.prefer = 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============= Main Config =============
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||||
|
pub struct ProxyConfig {
|
||||||
|
#[serde(default)]
|
||||||
|
pub general: GeneralConfig,
|
||||||
|
|
||||||
|
#[serde(default)]
|
||||||
|
pub network: NetworkConfig,
|
||||||
|
|
||||||
|
#[serde(default)]
|
||||||
|
pub server: ServerConfig,
|
||||||
|
|
||||||
|
#[serde(default)]
|
||||||
|
pub timeouts: TimeoutsConfig,
|
||||||
|
|
||||||
|
#[serde(default)]
|
||||||
|
pub censorship: AntiCensorshipConfig,
|
||||||
|
|
||||||
|
#[serde(default)]
|
||||||
|
pub access: AccessConfig,
|
||||||
|
|
||||||
|
#[serde(default)]
|
||||||
|
pub upstreams: Vec<UpstreamConfig>,
|
||||||
|
|
||||||
|
#[serde(default)]
|
||||||
|
pub show_link: ShowLink,
|
||||||
|
|
||||||
|
/// DC address overrides for non-standard DCs (CDN, media, test, etc.)
|
||||||
|
/// Keys are DC indices as strings, values are one or more "ip:port" addresses.
|
||||||
|
/// Matches the C implementation's `proxy_for <dc_id> <ip>:<port>` config directive.
|
||||||
|
/// Example in config.toml:
|
||||||
|
/// [dc_overrides]
|
||||||
|
/// "203" = ["149.154.175.100:443", "91.105.192.100:443"]
|
||||||
|
#[serde(default, deserialize_with = "deserialize_dc_overrides")]
|
||||||
|
pub dc_overrides: HashMap<String, Vec<String>>,
|
||||||
|
|
||||||
|
/// Default DC index (1-5) for unmapped non-standard DCs.
|
||||||
|
/// Matches the C implementation's `default <dc_id>` config directive.
|
||||||
|
/// If not set, defaults to 2 (matching Telegram's official `default 2;` in proxy-multi.conf).
|
||||||
|
#[serde(default)]
|
||||||
|
pub default_dc: Option<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ProxyConfig {
|
||||||
|
pub fn load<P: AsRef<Path>>(path: P) -> Result<Self> {
|
||||||
|
let content =
|
||||||
|
std::fs::read_to_string(&path).map_err(|e| ProxyError::Config(e.to_string()))?;
|
||||||
|
let base_dir = path.as_ref().parent().unwrap_or(Path::new("."));
|
||||||
|
let processed = preprocess_includes(&content, base_dir, 0)?;
|
||||||
|
|
||||||
|
let mut config: ProxyConfig =
|
||||||
|
toml::from_str(&processed).map_err(|e| ProxyError::Config(e.to_string()))?;
|
||||||
|
|
||||||
|
// Validate secrets.
|
||||||
|
for (user, secret) in &config.access.users {
|
||||||
|
if !secret.chars().all(|c| c.is_ascii_hexdigit()) || secret.len() != 32 {
|
||||||
|
return Err(ProxyError::InvalidSecret {
|
||||||
|
user: user.clone(),
|
||||||
|
reason: "Must be 32 hex characters".to_string(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate tls_domain.
|
||||||
|
if config.censorship.tls_domain.is_empty() {
|
||||||
|
return Err(ProxyError::Config("tls_domain cannot be empty".to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate mask_unix_sock.
|
||||||
|
if let Some(ref sock_path) = config.censorship.mask_unix_sock {
|
||||||
|
if sock_path.is_empty() {
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"mask_unix_sock cannot be empty".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
#[cfg(unix)]
|
||||||
|
if sock_path.len() > 107 {
|
||||||
|
return Err(ProxyError::Config(format!(
|
||||||
|
"mask_unix_sock path too long: {} bytes (max 107)",
|
||||||
|
sock_path.len()
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
#[cfg(not(unix))]
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"mask_unix_sock is only supported on Unix platforms".to_string(),
|
||||||
|
));
|
||||||
|
|
||||||
|
if config.censorship.mask_host.is_some() {
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"mask_unix_sock and mask_host are mutually exclusive".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default mask_host to tls_domain if not set and no unix socket configured.
|
||||||
|
if config.censorship.mask_host.is_none() && config.censorship.mask_unix_sock.is_none() {
|
||||||
|
config.censorship.mask_host = Some(config.censorship.tls_domain.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Merge primary + extra TLS domains, deduplicate (primary always first).
|
||||||
|
if !config.censorship.tls_domains.is_empty() {
|
||||||
|
let mut all = Vec::with_capacity(1 + config.censorship.tls_domains.len());
|
||||||
|
all.push(config.censorship.tls_domain.clone());
|
||||||
|
for d in std::mem::take(&mut config.censorship.tls_domains) {
|
||||||
|
if !d.is_empty() && !all.contains(&d) {
|
||||||
|
all.push(d);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// keep primary as tls_domain; store remaining back to tls_domains
|
||||||
|
if all.len() > 1 {
|
||||||
|
config.censorship.tls_domains = all[1..].to_vec();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Migration: prefer_ipv6 -> network.prefer.
|
||||||
|
if config.general.prefer_ipv6 {
|
||||||
|
if config.network.prefer == 4 {
|
||||||
|
config.network.prefer = 6;
|
||||||
|
}
|
||||||
|
warn!("prefer_ipv6 is deprecated, use [network].prefer = 6");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Auto-enable NAT probe when Middle Proxy is requested.
|
||||||
|
if config.general.use_middle_proxy && !config.general.middle_proxy_nat_probe {
|
||||||
|
config.general.middle_proxy_nat_probe = true;
|
||||||
|
warn!("Auto-enabled middle_proxy_nat_probe for middle proxy mode");
|
||||||
|
}
|
||||||
|
|
||||||
|
validate_network_cfg(&mut config.network)?;
|
||||||
|
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Random fake_cert_len only when default is in use.
|
||||||
|
if !config.censorship.tls_emulation && config.censorship.fake_cert_len == default_fake_cert_len() {
|
||||||
|
config.censorship.fake_cert_len = rand::rng().gen_range(1024..4096);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resolve listen_tcp: explicit value wins, otherwise auto-detect.
|
||||||
|
// If unix socket is set → TCP only when listen_addr_ipv4 or listeners are explicitly provided.
|
||||||
|
// If no unix socket → TCP always (backward compat).
|
||||||
|
let listen_tcp = config.server.listen_tcp.unwrap_or_else(|| {
|
||||||
|
if config.server.listen_unix_sock.is_some() {
|
||||||
|
// Unix socket present: TCP only if user explicitly set addresses or listeners.
|
||||||
|
config.server.listen_addr_ipv4.is_some()
|
||||||
|
|| !config.server.listeners.is_empty()
|
||||||
|
} else {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Migration: Populate listeners if empty (skip when listen_tcp = false).
|
||||||
|
if config.server.listeners.is_empty() && listen_tcp {
|
||||||
|
let ipv4_str = config.server.listen_addr_ipv4
|
||||||
|
.as_deref()
|
||||||
|
.unwrap_or("0.0.0.0");
|
||||||
|
if let Ok(ipv4) = ipv4_str.parse::<IpAddr>() {
|
||||||
|
config.server.listeners.push(ListenerConfig {
|
||||||
|
ip: ipv4,
|
||||||
|
announce: None,
|
||||||
|
announce_ip: None,
|
||||||
|
proxy_protocol: None,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if let Some(ipv6_str) = &config.server.listen_addr_ipv6 {
|
||||||
|
if let Ok(ipv6) = ipv6_str.parse::<IpAddr>() {
|
||||||
|
config.server.listeners.push(ListenerConfig {
|
||||||
|
ip: ipv6,
|
||||||
|
announce: None,
|
||||||
|
announce_ip: None,
|
||||||
|
proxy_protocol: None,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Migration: announce_ip → announce for each listener.
|
||||||
|
for listener in &mut config.server.listeners {
|
||||||
|
if listener.announce.is_none() && listener.announce_ip.is_some() {
|
||||||
|
listener.announce = Some(listener.announce_ip.unwrap().to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Migration: show_link (top-level) → general.links.show.
|
||||||
|
if !config.show_link.is_empty() && config.general.links.show.is_empty() {
|
||||||
|
config.general.links.show = config.show_link.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Migration: Populate upstreams if empty (Default Direct).
|
||||||
|
if config.upstreams.is_empty() {
|
||||||
|
config.upstreams.push(UpstreamConfig {
|
||||||
|
upstream_type: UpstreamType::Direct { interface: None, bind_addresses: None },
|
||||||
|
weight: 1,
|
||||||
|
enabled: true,
|
||||||
|
scopes: String::new(),
|
||||||
|
selected_scope: String::new(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure default DC203 override is present.
|
||||||
|
config
|
||||||
|
.dc_overrides
|
||||||
|
.entry("203".to_string())
|
||||||
|
.or_insert_with(|| vec!["91.105.192.100:443".to_string()]);
|
||||||
|
|
||||||
|
Ok(config)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn validate(&self) -> Result<()> {
|
||||||
|
if self.access.users.is_empty() {
|
||||||
|
return Err(ProxyError::Config("No users configured".to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
if !self.general.modes.classic && !self.general.modes.secure && !self.general.modes.tls {
|
||||||
|
return Err(ProxyError::Config("No modes enabled".to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.censorship.tls_domain.contains(' ') || self.censorship.tls_domain.contains('/') {
|
||||||
|
return Err(ProxyError::Config(format!(
|
||||||
|
"Invalid tls_domain: '{}'. Must be a valid domain name",
|
||||||
|
self.censorship.tls_domain
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(tag) = &self.general.ad_tag {
|
||||||
|
let zeros = "00000000000000000000000000000000";
|
||||||
|
if tag == zeros {
|
||||||
|
warn!("ad_tag is all zeros; register a valid proxy tag via @MTProxybot to enable sponsored channel");
|
||||||
|
}
|
||||||
|
if tag.len() != 32 || tag.chars().any(|c| !c.is_ascii_hexdigit()) {
|
||||||
|
warn!("ad_tag is not a 32-char hex string; ensure you use value issued by @MTProxybot");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn dc_overrides_allow_string_and_array() {
|
||||||
|
let toml = r#"
|
||||||
|
[dc_overrides]
|
||||||
|
"201" = "149.154.175.50:443"
|
||||||
|
"202" = ["149.154.167.51:443", "149.154.175.100:443"]
|
||||||
|
"#;
|
||||||
|
let cfg: ProxyConfig = toml::from_str(toml).unwrap();
|
||||||
|
assert_eq!(cfg.dc_overrides["201"], vec!["149.154.175.50:443"]);
|
||||||
|
assert_eq!(
|
||||||
|
cfg.dc_overrides["202"],
|
||||||
|
vec!["149.154.167.51:443", "149.154.175.100:443"]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn dc_overrides_inject_dc203_default() {
|
||||||
|
let toml = r#"
|
||||||
|
[general]
|
||||||
|
use_middle_proxy = false
|
||||||
|
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "example.com"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
user = "00000000000000000000000000000000"
|
||||||
|
"#;
|
||||||
|
let dir = std::env::temp_dir();
|
||||||
|
let path = dir.join("telemt_dc_override_test.toml");
|
||||||
|
std::fs::write(&path, toml).unwrap();
|
||||||
|
let cfg = ProxyConfig::load(&path).unwrap();
|
||||||
|
assert!(cfg
|
||||||
|
.dc_overrides
|
||||||
|
.get("203")
|
||||||
|
.map(|v| v.contains(&"91.105.192.100:443".to_string()))
|
||||||
|
.unwrap_or(false));
|
||||||
|
let _ = std::fs::remove_file(path);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,798 +1,9 @@
|
|||||||
//! Configuration
|
//! Configuration.
|
||||||
|
|
||||||
use crate::error::{ProxyError, Result};
|
pub(crate) mod defaults;
|
||||||
use chrono::{DateTime, Utc};
|
mod types;
|
||||||
use serde::{Deserialize, Serialize};
|
mod load;
|
||||||
use serde::de::Deserializer;
|
pub mod hot_reload;
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::net::IpAddr;
|
|
||||||
use std::path::Path;
|
|
||||||
use tracing::warn;
|
|
||||||
|
|
||||||
// ============= Helper Defaults =============
|
pub use load::ProxyConfig;
|
||||||
|
pub use types::*;
|
||||||
fn default_true() -> bool {
|
|
||||||
true
|
|
||||||
}
|
|
||||||
fn default_port() -> u16 {
|
|
||||||
443
|
|
||||||
}
|
|
||||||
fn default_tls_domain() -> String {
|
|
||||||
"www.google.com".to_string()
|
|
||||||
}
|
|
||||||
fn default_mask_port() -> u16 {
|
|
||||||
443
|
|
||||||
}
|
|
||||||
fn default_replay_check_len() -> usize {
|
|
||||||
65536
|
|
||||||
}
|
|
||||||
fn default_replay_window_secs() -> u64 {
|
|
||||||
1800
|
|
||||||
}
|
|
||||||
fn default_handshake_timeout() -> u64 {
|
|
||||||
15
|
|
||||||
}
|
|
||||||
fn default_connect_timeout() -> u64 {
|
|
||||||
10
|
|
||||||
}
|
|
||||||
fn default_keepalive() -> u64 {
|
|
||||||
60
|
|
||||||
}
|
|
||||||
fn default_ack_timeout() -> u64 {
|
|
||||||
300
|
|
||||||
}
|
|
||||||
fn default_listen_addr() -> String {
|
|
||||||
"0.0.0.0".to_string()
|
|
||||||
}
|
|
||||||
fn default_fake_cert_len() -> usize {
|
|
||||||
2048
|
|
||||||
}
|
|
||||||
fn default_weight() -> u16 {
|
|
||||||
1
|
|
||||||
}
|
|
||||||
fn default_metrics_whitelist() -> Vec<IpAddr> {
|
|
||||||
vec!["127.0.0.1".parse().unwrap(), "::1".parse().unwrap()]
|
|
||||||
}
|
|
||||||
|
|
||||||
fn default_unknown_dc_log_path() -> Option<String> {
|
|
||||||
Some("unknown-dc.txt".to_string())
|
|
||||||
}
|
|
||||||
|
|
||||||
// ============= Custom Deserializers =============
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
#[serde(untagged)]
|
|
||||||
enum OneOrMany {
|
|
||||||
One(String),
|
|
||||||
Many(Vec<String>),
|
|
||||||
}
|
|
||||||
|
|
||||||
fn deserialize_dc_overrides<'de, D>(
|
|
||||||
deserializer: D,
|
|
||||||
) -> std::result::Result<HashMap<String, Vec<String>>, D::Error>
|
|
||||||
where
|
|
||||||
D: Deserializer<'de>,
|
|
||||||
{
|
|
||||||
let raw: HashMap<String, OneOrMany> = HashMap::deserialize(deserializer)?;
|
|
||||||
let mut out = HashMap::new();
|
|
||||||
for (dc, val) in raw {
|
|
||||||
let mut addrs = match val {
|
|
||||||
OneOrMany::One(s) => vec![s],
|
|
||||||
OneOrMany::Many(v) => v,
|
|
||||||
};
|
|
||||||
addrs.retain(|s| !s.trim().is_empty());
|
|
||||||
if !addrs.is_empty() {
|
|
||||||
out.insert(dc, addrs);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(out)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ============= Log Level =============
|
|
||||||
|
|
||||||
/// Logging verbosity level
|
|
||||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
|
|
||||||
#[serde(rename_all = "lowercase")]
|
|
||||||
pub enum LogLevel {
|
|
||||||
/// All messages including trace (trace + debug + info + warn + error)
|
|
||||||
Debug,
|
|
||||||
/// Detailed operational logs (debug + info + warn + error)
|
|
||||||
Verbose,
|
|
||||||
/// Standard operational logs (info + warn + error)
|
|
||||||
#[default]
|
|
||||||
Normal,
|
|
||||||
/// Minimal output: only warnings and errors (warn + error).
|
|
||||||
/// Startup messages (config, DC connectivity, proxy links) are always shown
|
|
||||||
/// via info! before the filter is applied.
|
|
||||||
Silent,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl LogLevel {
|
|
||||||
/// Convert to tracing EnvFilter directive string
|
|
||||||
pub fn to_filter_str(&self) -> &'static str {
|
|
||||||
match self {
|
|
||||||
LogLevel::Debug => "trace",
|
|
||||||
LogLevel::Verbose => "debug",
|
|
||||||
LogLevel::Normal => "info",
|
|
||||||
LogLevel::Silent => "warn",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Parse from a loose string (CLI argument)
|
|
||||||
pub fn from_str_loose(s: &str) -> Self {
|
|
||||||
match s.to_lowercase().as_str() {
|
|
||||||
"debug" | "trace" => LogLevel::Debug,
|
|
||||||
"verbose" => LogLevel::Verbose,
|
|
||||||
"normal" | "info" => LogLevel::Normal,
|
|
||||||
"silent" | "quiet" | "error" | "warn" => LogLevel::Silent,
|
|
||||||
_ => LogLevel::Normal,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn dc_overrides_allow_string_and_array() {
|
|
||||||
let toml = r#"
|
|
||||||
[dc_overrides]
|
|
||||||
"201" = "149.154.175.50:443"
|
|
||||||
"202" = ["149.154.167.51:443", "149.154.175.100:443"]
|
|
||||||
"#;
|
|
||||||
let cfg: ProxyConfig = toml::from_str(toml).unwrap();
|
|
||||||
assert_eq!(cfg.dc_overrides["201"], vec!["149.154.175.50:443"]);
|
|
||||||
assert_eq!(
|
|
||||||
cfg.dc_overrides["202"],
|
|
||||||
vec!["149.154.167.51:443", "149.154.175.100:443"]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn dc_overrides_inject_dc203_default() {
|
|
||||||
let toml = r#"
|
|
||||||
[general]
|
|
||||||
use_middle_proxy = false
|
|
||||||
|
|
||||||
[censorship]
|
|
||||||
tls_domain = "example.com"
|
|
||||||
|
|
||||||
[access.users]
|
|
||||||
user = "00000000000000000000000000000000"
|
|
||||||
"#;
|
|
||||||
let dir = std::env::temp_dir();
|
|
||||||
let path = dir.join("telemt_dc_override_test.toml");
|
|
||||||
std::fs::write(&path, toml).unwrap();
|
|
||||||
let cfg = ProxyConfig::load(&path).unwrap();
|
|
||||||
assert!(cfg
|
|
||||||
.dc_overrides
|
|
||||||
.get("203")
|
|
||||||
.map(|v| v.contains(&"91.105.192.100:443".to_string()))
|
|
||||||
.unwrap_or(false));
|
|
||||||
let _ = std::fs::remove_file(path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Display for LogLevel {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
match self {
|
|
||||||
LogLevel::Debug => write!(f, "debug"),
|
|
||||||
LogLevel::Verbose => write!(f, "verbose"),
|
|
||||||
LogLevel::Normal => write!(f, "normal"),
|
|
||||||
LogLevel::Silent => write!(f, "silent"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ============= Sub-Configs =============
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
||||||
pub struct ProxyModes {
|
|
||||||
#[serde(default)]
|
|
||||||
pub classic: bool,
|
|
||||||
#[serde(default)]
|
|
||||||
pub secure: bool,
|
|
||||||
#[serde(default = "default_true")]
|
|
||||||
pub tls: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for ProxyModes {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
classic: true,
|
|
||||||
secure: true,
|
|
||||||
tls: true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
||||||
pub struct GeneralConfig {
|
|
||||||
#[serde(default)]
|
|
||||||
pub modes: ProxyModes,
|
|
||||||
|
|
||||||
#[serde(default)]
|
|
||||||
pub prefer_ipv6: bool,
|
|
||||||
|
|
||||||
#[serde(default = "default_true")]
|
|
||||||
pub fast_mode: bool,
|
|
||||||
|
|
||||||
#[serde(default)]
|
|
||||||
pub use_middle_proxy: bool,
|
|
||||||
|
|
||||||
#[serde(default)]
|
|
||||||
pub ad_tag: Option<String>,
|
|
||||||
|
|
||||||
/// Path to proxy-secret binary file (auto-downloaded if absent).
|
|
||||||
/// Infrastructure secret from https://core.telegram.org/getProxySecret
|
|
||||||
#[serde(default)]
|
|
||||||
pub proxy_secret_path: Option<String>,
|
|
||||||
|
|
||||||
/// Public IP override for middle-proxy NAT environments.
|
|
||||||
/// When set, this IP is used in ME key derivation and RPC_PROXY_REQ "our_addr".
|
|
||||||
#[serde(default)]
|
|
||||||
pub middle_proxy_nat_ip: Option<IpAddr>,
|
|
||||||
|
|
||||||
/// Enable STUN-based NAT probing to discover public IP:port for ME KDF.
|
|
||||||
#[serde(default)]
|
|
||||||
pub middle_proxy_nat_probe: bool,
|
|
||||||
|
|
||||||
/// Optional STUN server address (host:port) for NAT probing.
|
|
||||||
#[serde(default)]
|
|
||||||
pub middle_proxy_nat_stun: Option<String>,
|
|
||||||
|
|
||||||
/// Ignore STUN/interface IP mismatch (keep using Middle Proxy even if NAT detected).
|
|
||||||
#[serde(default)]
|
|
||||||
pub stun_iface_mismatch_ignore: bool,
|
|
||||||
|
|
||||||
/// Log unknown (non-standard) DC requests to a file (default: unknown-dc.txt). Set to null to disable.
|
|
||||||
#[serde(default = "default_unknown_dc_log_path")]
|
|
||||||
pub unknown_dc_log_path: Option<String>,
|
|
||||||
|
|
||||||
#[serde(default)]
|
|
||||||
pub log_level: LogLevel,
|
|
||||||
|
|
||||||
/// Disable colored output in logs (useful for files/systemd)
|
|
||||||
#[serde(default)]
|
|
||||||
pub disable_colors: bool,
|
|
||||||
|
|
||||||
/// [general.links] — proxy link generation overrides
|
|
||||||
#[serde(default)]
|
|
||||||
pub links: LinksConfig,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// `[general.links]` — proxy link generation settings.
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
|
||||||
pub struct LinksConfig {
|
|
||||||
/// List of usernames whose tg:// links to display at startup.
|
|
||||||
/// `"*"` = all users, `["alice", "bob"]` = specific users.
|
|
||||||
#[serde(default)]
|
|
||||||
pub show: ShowLink,
|
|
||||||
|
|
||||||
/// Public hostname/IP for tg:// link generation (overrides detected IP).
|
|
||||||
#[serde(default)]
|
|
||||||
pub public_host: Option<String>,
|
|
||||||
|
|
||||||
/// Public port for tg:// link generation (overrides server.port).
|
|
||||||
#[serde(default)]
|
|
||||||
pub public_port: Option<u16>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for GeneralConfig {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
modes: ProxyModes::default(),
|
|
||||||
prefer_ipv6: false,
|
|
||||||
fast_mode: true,
|
|
||||||
use_middle_proxy: false,
|
|
||||||
ad_tag: None,
|
|
||||||
proxy_secret_path: None,
|
|
||||||
middle_proxy_nat_ip: None,
|
|
||||||
middle_proxy_nat_probe: false,
|
|
||||||
middle_proxy_nat_stun: None,
|
|
||||||
stun_iface_mismatch_ignore: false,
|
|
||||||
unknown_dc_log_path: default_unknown_dc_log_path(),
|
|
||||||
log_level: LogLevel::Normal,
|
|
||||||
disable_colors: false,
|
|
||||||
links: LinksConfig::default(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
||||||
pub struct ServerConfig {
|
|
||||||
#[serde(default = "default_port")]
|
|
||||||
pub port: u16,
|
|
||||||
|
|
||||||
#[serde(default)]
|
|
||||||
pub listen_addr_ipv4: Option<String>,
|
|
||||||
|
|
||||||
#[serde(default)]
|
|
||||||
pub listen_addr_ipv6: Option<String>,
|
|
||||||
|
|
||||||
#[serde(default)]
|
|
||||||
pub listen_unix_sock: Option<String>,
|
|
||||||
|
|
||||||
/// Unix socket file permissions (octal, e.g. "0666" or "0777").
|
|
||||||
/// Applied via chmod after bind. Default: no change (inherits umask).
|
|
||||||
#[serde(default)]
|
|
||||||
pub listen_unix_sock_perm: Option<String>,
|
|
||||||
|
|
||||||
/// Enable TCP listening. Default: true when no unix socket, false when
|
|
||||||
/// listen_unix_sock is set. Set explicitly to override auto-detection.
|
|
||||||
#[serde(default)]
|
|
||||||
pub listen_tcp: Option<bool>,
|
|
||||||
|
|
||||||
#[serde(default)]
|
|
||||||
pub metrics_port: Option<u16>,
|
|
||||||
|
|
||||||
#[serde(default = "default_metrics_whitelist")]
|
|
||||||
pub metrics_whitelist: Vec<IpAddr>,
|
|
||||||
|
|
||||||
#[serde(default)]
|
|
||||||
pub listeners: Vec<ListenerConfig>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for ServerConfig {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
port: default_port(),
|
|
||||||
listen_addr_ipv4: Some(default_listen_addr()),
|
|
||||||
listen_addr_ipv6: Some("::".to_string()),
|
|
||||||
listen_unix_sock: None,
|
|
||||||
listen_unix_sock_perm: None,
|
|
||||||
listen_tcp: None,
|
|
||||||
metrics_port: None,
|
|
||||||
metrics_whitelist: default_metrics_whitelist(),
|
|
||||||
listeners: Vec::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
||||||
pub struct TimeoutsConfig {
|
|
||||||
#[serde(default = "default_handshake_timeout")]
|
|
||||||
pub client_handshake: u64,
|
|
||||||
|
|
||||||
#[serde(default = "default_connect_timeout")]
|
|
||||||
pub tg_connect: u64,
|
|
||||||
|
|
||||||
#[serde(default = "default_keepalive")]
|
|
||||||
pub client_keepalive: u64,
|
|
||||||
|
|
||||||
#[serde(default = "default_ack_timeout")]
|
|
||||||
pub client_ack: u64,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for TimeoutsConfig {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
client_handshake: default_handshake_timeout(),
|
|
||||||
tg_connect: default_connect_timeout(),
|
|
||||||
client_keepalive: default_keepalive(),
|
|
||||||
client_ack: default_ack_timeout(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
||||||
pub struct AntiCensorshipConfig {
|
|
||||||
#[serde(default = "default_tls_domain")]
|
|
||||||
pub tls_domain: String,
|
|
||||||
|
|
||||||
#[serde(default = "default_true")]
|
|
||||||
pub mask: bool,
|
|
||||||
|
|
||||||
#[serde(default)]
|
|
||||||
pub mask_host: Option<String>,
|
|
||||||
|
|
||||||
#[serde(default = "default_mask_port")]
|
|
||||||
pub mask_port: u16,
|
|
||||||
|
|
||||||
#[serde(default)]
|
|
||||||
pub mask_unix_sock: Option<String>,
|
|
||||||
|
|
||||||
#[serde(default = "default_fake_cert_len")]
|
|
||||||
pub fake_cert_len: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for AntiCensorshipConfig {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
tls_domain: default_tls_domain(),
|
|
||||||
mask: true,
|
|
||||||
mask_host: None,
|
|
||||||
mask_port: default_mask_port(),
|
|
||||||
mask_unix_sock: None,
|
|
||||||
fake_cert_len: default_fake_cert_len(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
||||||
pub struct AccessConfig {
|
|
||||||
#[serde(default)]
|
|
||||||
pub users: HashMap<String, String>,
|
|
||||||
|
|
||||||
#[serde(default)]
|
|
||||||
pub user_max_tcp_conns: HashMap<String, usize>,
|
|
||||||
|
|
||||||
#[serde(default)]
|
|
||||||
pub user_expirations: HashMap<String, DateTime<Utc>>,
|
|
||||||
|
|
||||||
#[serde(default)]
|
|
||||||
pub user_data_quota: HashMap<String, u64>,
|
|
||||||
|
|
||||||
#[serde(default)]
|
|
||||||
pub user_max_unique_ips: HashMap<String, usize>,
|
|
||||||
|
|
||||||
#[serde(default = "default_replay_check_len")]
|
|
||||||
pub replay_check_len: usize,
|
|
||||||
|
|
||||||
#[serde(default = "default_replay_window_secs")]
|
|
||||||
pub replay_window_secs: u64,
|
|
||||||
|
|
||||||
#[serde(default)]
|
|
||||||
pub ignore_time_skew: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for AccessConfig {
|
|
||||||
fn default() -> Self {
|
|
||||||
let mut users = HashMap::new();
|
|
||||||
users.insert(
|
|
||||||
"default".to_string(),
|
|
||||||
"00000000000000000000000000000000".to_string(),
|
|
||||||
);
|
|
||||||
Self {
|
|
||||||
users,
|
|
||||||
user_max_tcp_conns: HashMap::new(),
|
|
||||||
user_expirations: HashMap::new(),
|
|
||||||
user_data_quota: HashMap::new(),
|
|
||||||
user_max_unique_ips: HashMap::new(),
|
|
||||||
replay_check_len: default_replay_check_len(),
|
|
||||||
replay_window_secs: default_replay_window_secs(),
|
|
||||||
ignore_time_skew: false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ============= Aux Structures =============
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
|
||||||
#[serde(tag = "type", rename_all = "lowercase")]
|
|
||||||
pub enum UpstreamType {
|
|
||||||
Direct {
|
|
||||||
#[serde(default)]
|
|
||||||
interface: Option<String>,
|
|
||||||
},
|
|
||||||
Socks4 {
|
|
||||||
address: String,
|
|
||||||
#[serde(default)]
|
|
||||||
interface: Option<String>,
|
|
||||||
#[serde(default)]
|
|
||||||
user_id: Option<String>,
|
|
||||||
},
|
|
||||||
Socks5 {
|
|
||||||
address: String,
|
|
||||||
#[serde(default)]
|
|
||||||
interface: Option<String>,
|
|
||||||
#[serde(default)]
|
|
||||||
username: Option<String>,
|
|
||||||
#[serde(default)]
|
|
||||||
password: Option<String>,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
||||||
pub struct UpstreamConfig {
|
|
||||||
#[serde(flatten)]
|
|
||||||
pub upstream_type: UpstreamType,
|
|
||||||
#[serde(default = "default_weight")]
|
|
||||||
pub weight: u16,
|
|
||||||
#[serde(default = "default_true")]
|
|
||||||
pub enabled: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
||||||
pub struct ListenerConfig {
|
|
||||||
pub ip: IpAddr,
|
|
||||||
/// IP address or hostname to announce in proxy links.
|
|
||||||
/// Takes precedence over `announce_ip` if both are set.
|
|
||||||
#[serde(default)]
|
|
||||||
pub announce: Option<String>,
|
|
||||||
/// Deprecated: Use `announce` instead. IP address to announce in proxy links.
|
|
||||||
/// Migrated to `announce` automatically if `announce` is not set.
|
|
||||||
#[serde(default)]
|
|
||||||
pub announce_ip: Option<IpAddr>,
|
|
||||||
}
|
|
||||||
|
|
||||||
// ============= ShowLink =============
|
|
||||||
|
|
||||||
/// Controls which users' proxy links are displayed at startup.
|
|
||||||
///
|
|
||||||
/// In TOML, this can be:
|
|
||||||
/// - `show_link = "*"` — show links for all users
|
|
||||||
/// - `show_link = ["a", "b"]` — show links for specific users
|
|
||||||
/// - omitted — show no links (default)
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub enum ShowLink {
|
|
||||||
/// Don't show any links (default when omitted)
|
|
||||||
None,
|
|
||||||
/// Show links for all configured users
|
|
||||||
All,
|
|
||||||
/// Show links for specific users
|
|
||||||
Specific(Vec<String>),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for ShowLink {
|
|
||||||
fn default() -> Self {
|
|
||||||
ShowLink::None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ShowLink {
|
|
||||||
/// Returns true if no links should be shown
|
|
||||||
pub fn is_empty(&self) -> bool {
|
|
||||||
matches!(self, ShowLink::None) || matches!(self, ShowLink::Specific(v) if v.is_empty())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Resolve the list of user names to display, given all configured users
|
|
||||||
pub fn resolve_users<'a>(&'a self, all_users: &'a HashMap<String, String>) -> Vec<&'a String> {
|
|
||||||
match self {
|
|
||||||
ShowLink::None => vec![],
|
|
||||||
ShowLink::All => {
|
|
||||||
let mut names: Vec<&String> = all_users.keys().collect();
|
|
||||||
names.sort();
|
|
||||||
names
|
|
||||||
}
|
|
||||||
ShowLink::Specific(names) => names.iter().collect(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Serialize for ShowLink {
|
|
||||||
fn serialize<S: serde::Serializer>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> {
|
|
||||||
match self {
|
|
||||||
ShowLink::None => Vec::<String>::new().serialize(serializer),
|
|
||||||
ShowLink::All => serializer.serialize_str("*"),
|
|
||||||
ShowLink::Specific(v) => v.serialize(serializer),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'de> Deserialize<'de> for ShowLink {
|
|
||||||
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> std::result::Result<Self, D::Error> {
|
|
||||||
use serde::de;
|
|
||||||
|
|
||||||
struct ShowLinkVisitor;
|
|
||||||
|
|
||||||
impl<'de> de::Visitor<'de> for ShowLinkVisitor {
|
|
||||||
type Value = ShowLink;
|
|
||||||
|
|
||||||
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
||||||
formatter.write_str(r#""*" or an array of user names"#)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_str<E: de::Error>(self, v: &str) -> std::result::Result<ShowLink, E> {
|
|
||||||
if v == "*" {
|
|
||||||
Ok(ShowLink::All)
|
|
||||||
} else {
|
|
||||||
Err(de::Error::invalid_value(
|
|
||||||
de::Unexpected::Str(v),
|
|
||||||
&r#""*""#,
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_seq<A: de::SeqAccess<'de>>(self, mut seq: A) -> std::result::Result<ShowLink, A::Error> {
|
|
||||||
let mut names = Vec::new();
|
|
||||||
while let Some(name) = seq.next_element::<String>()? {
|
|
||||||
names.push(name);
|
|
||||||
}
|
|
||||||
if names.is_empty() {
|
|
||||||
Ok(ShowLink::None)
|
|
||||||
} else {
|
|
||||||
Ok(ShowLink::Specific(names))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
deserializer.deserialize_any(ShowLinkVisitor)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ============= Main Config =============
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
|
||||||
pub struct ProxyConfig {
|
|
||||||
#[serde(default)]
|
|
||||||
pub general: GeneralConfig,
|
|
||||||
|
|
||||||
#[serde(default)]
|
|
||||||
pub server: ServerConfig,
|
|
||||||
|
|
||||||
#[serde(default)]
|
|
||||||
pub timeouts: TimeoutsConfig,
|
|
||||||
|
|
||||||
#[serde(default)]
|
|
||||||
pub censorship: AntiCensorshipConfig,
|
|
||||||
|
|
||||||
#[serde(default)]
|
|
||||||
pub access: AccessConfig,
|
|
||||||
|
|
||||||
#[serde(default)]
|
|
||||||
pub upstreams: Vec<UpstreamConfig>,
|
|
||||||
|
|
||||||
#[serde(default)]
|
|
||||||
pub show_link: ShowLink,
|
|
||||||
|
|
||||||
/// DC address overrides for non-standard DCs (CDN, media, test, etc.)
|
|
||||||
/// Keys are DC indices as strings, values are one or more \"ip:port\" addresses.
|
|
||||||
/// Matches the C implementation's `proxy_for <dc_id> <ip>:<port>` config directive.
|
|
||||||
/// Example in config.toml:
|
|
||||||
/// [dc_overrides]
|
|
||||||
/// \"203\" = [\"149.154.175.100:443\", \"91.105.192.100:443\"]
|
|
||||||
#[serde(default, deserialize_with = "deserialize_dc_overrides")]
|
|
||||||
pub dc_overrides: HashMap<String, Vec<String>>,
|
|
||||||
|
|
||||||
/// Default DC index (1-5) for unmapped non-standard DCs.
|
|
||||||
/// Matches the C implementation's `default <dc_id>` config directive.
|
|
||||||
/// If not set, defaults to 2 (matching Telegram's official `default 2;` in proxy-multi.conf).
|
|
||||||
#[serde(default)]
|
|
||||||
pub default_dc: Option<u8>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ProxyConfig {
|
|
||||||
pub fn load<P: AsRef<Path>>(path: P) -> Result<Self> {
|
|
||||||
let content =
|
|
||||||
std::fs::read_to_string(path).map_err(|e| ProxyError::Config(e.to_string()))?;
|
|
||||||
|
|
||||||
let mut config: ProxyConfig =
|
|
||||||
toml::from_str(&content).map_err(|e| ProxyError::Config(e.to_string()))?;
|
|
||||||
|
|
||||||
// Validate secrets
|
|
||||||
for (user, secret) in &config.access.users {
|
|
||||||
if !secret.chars().all(|c| c.is_ascii_hexdigit()) || secret.len() != 32 {
|
|
||||||
return Err(ProxyError::InvalidSecret {
|
|
||||||
user: user.clone(),
|
|
||||||
reason: "Must be 32 hex characters".to_string(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate tls_domain
|
|
||||||
if config.censorship.tls_domain.is_empty() {
|
|
||||||
return Err(ProxyError::Config("tls_domain cannot be empty".to_string()));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate mask_unix_sock
|
|
||||||
if let Some(ref sock_path) = config.censorship.mask_unix_sock {
|
|
||||||
if sock_path.is_empty() {
|
|
||||||
return Err(ProxyError::Config(
|
|
||||||
"mask_unix_sock cannot be empty".to_string(),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
#[cfg(unix)]
|
|
||||||
if sock_path.len() > 107 {
|
|
||||||
return Err(ProxyError::Config(format!(
|
|
||||||
"mask_unix_sock path too long: {} bytes (max 107)",
|
|
||||||
sock_path.len()
|
|
||||||
)));
|
|
||||||
}
|
|
||||||
#[cfg(not(unix))]
|
|
||||||
return Err(ProxyError::Config(
|
|
||||||
"mask_unix_sock is only supported on Unix platforms".to_string(),
|
|
||||||
));
|
|
||||||
|
|
||||||
if config.censorship.mask_host.is_some() {
|
|
||||||
return Err(ProxyError::Config(
|
|
||||||
"mask_unix_sock and mask_host are mutually exclusive".to_string(),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Default mask_host to tls_domain if not set and no unix socket configured
|
|
||||||
if config.censorship.mask_host.is_none() && config.censorship.mask_unix_sock.is_none() {
|
|
||||||
config.censorship.mask_host = Some(config.censorship.tls_domain.clone());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Random fake_cert_len
|
|
||||||
use rand::Rng;
|
|
||||||
config.censorship.fake_cert_len = rand::rng().gen_range(1024..4096);
|
|
||||||
|
|
||||||
// Resolve listen_tcp: explicit value wins, otherwise auto-detect.
|
|
||||||
// If unix socket is set → TCP only when listen_addr_ipv4 or listeners are explicitly provided.
|
|
||||||
// If no unix socket → TCP always (backward compat).
|
|
||||||
let listen_tcp = config.server.listen_tcp.unwrap_or_else(|| {
|
|
||||||
if config.server.listen_unix_sock.is_some() {
|
|
||||||
// Unix socket present: TCP only if user explicitly set addresses or listeners
|
|
||||||
config.server.listen_addr_ipv4.is_some()
|
|
||||||
|| !config.server.listeners.is_empty()
|
|
||||||
} else {
|
|
||||||
true
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Migration: Populate listeners if empty (skip when listen_tcp = false)
|
|
||||||
if config.server.listeners.is_empty() && listen_tcp {
|
|
||||||
let ipv4_str = config.server.listen_addr_ipv4
|
|
||||||
.as_deref()
|
|
||||||
.unwrap_or("0.0.0.0");
|
|
||||||
if let Ok(ipv4) = ipv4_str.parse::<IpAddr>() {
|
|
||||||
config.server.listeners.push(ListenerConfig {
|
|
||||||
ip: ipv4,
|
|
||||||
announce: None,
|
|
||||||
announce_ip: None,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if let Some(ipv6_str) = &config.server.listen_addr_ipv6 {
|
|
||||||
if let Ok(ipv6) = ipv6_str.parse::<IpAddr>() {
|
|
||||||
config.server.listeners.push(ListenerConfig {
|
|
||||||
ip: ipv6,
|
|
||||||
announce: None,
|
|
||||||
announce_ip: None,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Migration: announce_ip → announce for each listener
|
|
||||||
for listener in &mut config.server.listeners {
|
|
||||||
if listener.announce.is_none() && listener.announce_ip.is_some() {
|
|
||||||
listener.announce = Some(listener.announce_ip.unwrap().to_string());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Migration: show_link (top-level) → general.links.show
|
|
||||||
if !config.show_link.is_empty() && config.general.links.show.is_empty() {
|
|
||||||
config.general.links.show = config.show_link.clone();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Migration: Populate upstreams if empty (Default Direct)
|
|
||||||
if config.upstreams.is_empty() {
|
|
||||||
config.upstreams.push(UpstreamConfig {
|
|
||||||
upstream_type: UpstreamType::Direct { interface: None },
|
|
||||||
weight: 1,
|
|
||||||
enabled: true,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure default DC203 override is present.
|
|
||||||
config
|
|
||||||
.dc_overrides
|
|
||||||
.entry("203".to_string())
|
|
||||||
.or_insert_with(|| vec!["91.105.192.100:443".to_string()]);
|
|
||||||
|
|
||||||
Ok(config)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn validate(&self) -> Result<()> {
|
|
||||||
if self.access.users.is_empty() {
|
|
||||||
return Err(ProxyError::Config("No users configured".to_string()));
|
|
||||||
}
|
|
||||||
|
|
||||||
if !self.general.modes.classic && !self.general.modes.secure && !self.general.modes.tls {
|
|
||||||
return Err(ProxyError::Config("No modes enabled".to_string()));
|
|
||||||
}
|
|
||||||
|
|
||||||
if self.censorship.tls_domain.contains(' ') || self.censorship.tls_domain.contains('/') {
|
|
||||||
return Err(ProxyError::Config(format!(
|
|
||||||
"Invalid tls_domain: '{}'. Must be a valid domain name",
|
|
||||||
self.censorship.tls_domain
|
|
||||||
)));
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(tag) = &self.general.ad_tag {
|
|
||||||
let zeros = "00000000000000000000000000000000";
|
|
||||||
if tag == zeros {
|
|
||||||
warn!("ad_tag is all zeros; register a valid proxy tag via @MTProxybot to enable sponsored channel");
|
|
||||||
}
|
|
||||||
if tag.len() != 32 || tag.chars().any(|c| !c.is_ascii_hexdigit()) {
|
|
||||||
warn!("ad_tag is not a 32-char hex string; ensure you use value issued by @MTProxybot");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
615
src/config/types.rs
Normal file
615
src/config/types.rs
Normal file
@@ -0,0 +1,615 @@
|
|||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
use ipnetwork::IpNetwork;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::net::IpAddr;
|
||||||
|
|
||||||
|
use super::defaults::*;
|
||||||
|
|
||||||
|
// ============= Log Level =============
|
||||||
|
|
||||||
|
/// Logging verbosity level.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
|
||||||
|
#[serde(rename_all = "lowercase")]
|
||||||
|
pub enum LogLevel {
|
||||||
|
/// All messages including trace (trace + debug + info + warn + error).
|
||||||
|
Debug,
|
||||||
|
/// Detailed operational logs (debug + info + warn + error).
|
||||||
|
Verbose,
|
||||||
|
/// Standard operational logs (info + warn + error).
|
||||||
|
#[default]
|
||||||
|
Normal,
|
||||||
|
/// Minimal output: only warnings and errors (warn + error).
|
||||||
|
/// Startup messages (config, DC connectivity, proxy links) are always shown
|
||||||
|
/// via info! before the filter is applied.
|
||||||
|
Silent,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LogLevel {
|
||||||
|
/// Convert to tracing EnvFilter directive string.
|
||||||
|
pub fn to_filter_str(&self) -> &'static str {
|
||||||
|
match self {
|
||||||
|
LogLevel::Debug => "trace",
|
||||||
|
LogLevel::Verbose => "debug",
|
||||||
|
LogLevel::Normal => "info",
|
||||||
|
LogLevel::Silent => "warn",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parse from a loose string (CLI argument).
|
||||||
|
pub fn from_str_loose(s: &str) -> Self {
|
||||||
|
match s.to_lowercase().as_str() {
|
||||||
|
"debug" | "trace" => LogLevel::Debug,
|
||||||
|
"verbose" => LogLevel::Verbose,
|
||||||
|
"normal" | "info" => LogLevel::Normal,
|
||||||
|
"silent" | "quiet" | "error" | "warn" => LogLevel::Silent,
|
||||||
|
_ => LogLevel::Normal,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for LogLevel {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
LogLevel::Debug => write!(f, "debug"),
|
||||||
|
LogLevel::Verbose => write!(f, "verbose"),
|
||||||
|
LogLevel::Normal => write!(f, "normal"),
|
||||||
|
LogLevel::Silent => write!(f, "silent"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============= Sub-Configs =============
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct ProxyModes {
|
||||||
|
#[serde(default)]
|
||||||
|
pub classic: bool,
|
||||||
|
#[serde(default)]
|
||||||
|
pub secure: bool,
|
||||||
|
#[serde(default = "default_true")]
|
||||||
|
pub tls: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for ProxyModes {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
classic: true,
|
||||||
|
secure: true,
|
||||||
|
tls: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct NetworkConfig {
|
||||||
|
#[serde(default = "default_true")]
|
||||||
|
pub ipv4: bool,
|
||||||
|
|
||||||
|
/// None = auto-detect IPv6 availability.
|
||||||
|
#[serde(default)]
|
||||||
|
pub ipv6: Option<bool>,
|
||||||
|
|
||||||
|
/// 4 or 6.
|
||||||
|
#[serde(default = "default_prefer_4")]
|
||||||
|
pub prefer: u8,
|
||||||
|
|
||||||
|
#[serde(default)]
|
||||||
|
pub multipath: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for NetworkConfig {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
ipv4: true,
|
||||||
|
ipv6: None,
|
||||||
|
prefer: 4,
|
||||||
|
multipath: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct GeneralConfig {
|
||||||
|
#[serde(default)]
|
||||||
|
pub modes: ProxyModes,
|
||||||
|
|
||||||
|
#[serde(default)]
|
||||||
|
pub prefer_ipv6: bool,
|
||||||
|
|
||||||
|
#[serde(default = "default_true")]
|
||||||
|
pub fast_mode: bool,
|
||||||
|
|
||||||
|
#[serde(default)]
|
||||||
|
pub use_middle_proxy: bool,
|
||||||
|
|
||||||
|
#[serde(default)]
|
||||||
|
pub ad_tag: Option<String>,
|
||||||
|
|
||||||
|
/// Path to proxy-secret binary file (auto-downloaded if absent).
|
||||||
|
/// Infrastructure secret from https://core.telegram.org/getProxySecret.
|
||||||
|
#[serde(default)]
|
||||||
|
pub proxy_secret_path: Option<String>,
|
||||||
|
|
||||||
|
/// Public IP override for middle-proxy NAT environments.
|
||||||
|
/// When set, this IP is used in ME key derivation and RPC_PROXY_REQ "our_addr".
|
||||||
|
#[serde(default)]
|
||||||
|
pub middle_proxy_nat_ip: Option<IpAddr>,
|
||||||
|
|
||||||
|
/// Enable STUN-based NAT probing to discover public IP:port for ME KDF.
|
||||||
|
#[serde(default)]
|
||||||
|
pub middle_proxy_nat_probe: bool,
|
||||||
|
|
||||||
|
/// Optional STUN server address (host:port) for NAT probing.
|
||||||
|
#[serde(default)]
|
||||||
|
pub middle_proxy_nat_stun: Option<String>,
|
||||||
|
|
||||||
|
/// Optional list of STUN servers for NAT probing fallback.
|
||||||
|
#[serde(default)]
|
||||||
|
pub middle_proxy_nat_stun_servers: Vec<String>,
|
||||||
|
|
||||||
|
/// Desired size of active Middle-Proxy writer pool.
|
||||||
|
#[serde(default = "default_pool_size")]
|
||||||
|
pub middle_proxy_pool_size: usize,
|
||||||
|
|
||||||
|
/// Number of warm standby ME connections kept pre-initialized.
|
||||||
|
#[serde(default)]
|
||||||
|
pub middle_proxy_warm_standby: usize,
|
||||||
|
|
||||||
|
/// Enable ME keepalive padding frames.
|
||||||
|
#[serde(default = "default_true")]
|
||||||
|
pub me_keepalive_enabled: bool,
|
||||||
|
|
||||||
|
/// Keepalive interval in seconds.
|
||||||
|
#[serde(default = "default_keepalive_interval")]
|
||||||
|
pub me_keepalive_interval_secs: u64,
|
||||||
|
|
||||||
|
/// Keepalive jitter in seconds.
|
||||||
|
#[serde(default = "default_keepalive_jitter")]
|
||||||
|
pub me_keepalive_jitter_secs: u64,
|
||||||
|
|
||||||
|
/// Keepalive payload randomized (4 bytes); otherwise zeros.
|
||||||
|
#[serde(default = "default_true")]
|
||||||
|
pub me_keepalive_payload_random: bool,
|
||||||
|
|
||||||
|
/// Enable staggered warmup of extra ME writers.
|
||||||
|
#[serde(default = "default_true")]
|
||||||
|
pub me_warmup_stagger_enabled: bool,
|
||||||
|
|
||||||
|
/// Base delay between warmup connections in ms.
|
||||||
|
#[serde(default = "default_warmup_step_delay_ms")]
|
||||||
|
pub me_warmup_step_delay_ms: u64,
|
||||||
|
|
||||||
|
/// Jitter for warmup delay in ms.
|
||||||
|
#[serde(default = "default_warmup_step_jitter_ms")]
|
||||||
|
pub me_warmup_step_jitter_ms: u64,
|
||||||
|
|
||||||
|
/// Max concurrent reconnect attempts per DC.
|
||||||
|
#[serde(default)]
|
||||||
|
pub me_reconnect_max_concurrent_per_dc: u32,
|
||||||
|
|
||||||
|
/// Base backoff in ms for reconnect.
|
||||||
|
#[serde(default = "default_reconnect_backoff_base_ms")]
|
||||||
|
pub me_reconnect_backoff_base_ms: u64,
|
||||||
|
|
||||||
|
/// Cap backoff in ms for reconnect.
|
||||||
|
#[serde(default = "default_reconnect_backoff_cap_ms")]
|
||||||
|
pub me_reconnect_backoff_cap_ms: u64,
|
||||||
|
|
||||||
|
/// Fast retry attempts before backoff.
|
||||||
|
#[serde(default)]
|
||||||
|
pub me_reconnect_fast_retry_count: u32,
|
||||||
|
|
||||||
|
/// Ignore STUN/interface IP mismatch (keep using Middle Proxy even if NAT detected).
|
||||||
|
#[serde(default)]
|
||||||
|
pub stun_iface_mismatch_ignore: bool,
|
||||||
|
|
||||||
|
/// Log unknown (non-standard) DC requests to a file (default: unknown-dc.txt). Set to null to disable.
|
||||||
|
#[serde(default = "default_unknown_dc_log_path")]
|
||||||
|
pub unknown_dc_log_path: Option<String>,
|
||||||
|
|
||||||
|
#[serde(default)]
|
||||||
|
pub log_level: LogLevel,
|
||||||
|
|
||||||
|
/// Disable colored output in logs (useful for files/systemd).
|
||||||
|
#[serde(default)]
|
||||||
|
pub disable_colors: bool,
|
||||||
|
|
||||||
|
/// [general.links] — proxy link generation overrides.
|
||||||
|
#[serde(default)]
|
||||||
|
pub links: LinksConfig,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for GeneralConfig {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
modes: ProxyModes::default(),
|
||||||
|
prefer_ipv6: false,
|
||||||
|
fast_mode: true,
|
||||||
|
use_middle_proxy: false,
|
||||||
|
ad_tag: None,
|
||||||
|
proxy_secret_path: None,
|
||||||
|
middle_proxy_nat_ip: None,
|
||||||
|
middle_proxy_nat_probe: false,
|
||||||
|
middle_proxy_nat_stun: None,
|
||||||
|
middle_proxy_nat_stun_servers: Vec::new(),
|
||||||
|
middle_proxy_pool_size: default_pool_size(),
|
||||||
|
middle_proxy_warm_standby: 0,
|
||||||
|
me_keepalive_enabled: true,
|
||||||
|
me_keepalive_interval_secs: default_keepalive_interval(),
|
||||||
|
me_keepalive_jitter_secs: default_keepalive_jitter(),
|
||||||
|
me_keepalive_payload_random: true,
|
||||||
|
me_warmup_stagger_enabled: true,
|
||||||
|
me_warmup_step_delay_ms: default_warmup_step_delay_ms(),
|
||||||
|
me_warmup_step_jitter_ms: default_warmup_step_jitter_ms(),
|
||||||
|
me_reconnect_max_concurrent_per_dc: 1,
|
||||||
|
me_reconnect_backoff_base_ms: default_reconnect_backoff_base_ms(),
|
||||||
|
me_reconnect_backoff_cap_ms: default_reconnect_backoff_cap_ms(),
|
||||||
|
me_reconnect_fast_retry_count: 1,
|
||||||
|
stun_iface_mismatch_ignore: false,
|
||||||
|
unknown_dc_log_path: default_unknown_dc_log_path(),
|
||||||
|
log_level: LogLevel::Normal,
|
||||||
|
disable_colors: false,
|
||||||
|
links: LinksConfig::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `[general.links]` — proxy link generation settings.
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||||
|
pub struct LinksConfig {
|
||||||
|
/// List of usernames whose tg:// links to display at startup.
|
||||||
|
/// `"*"` = all users, `["alice", "bob"]` = specific users.
|
||||||
|
#[serde(default)]
|
||||||
|
pub show: ShowLink,
|
||||||
|
|
||||||
|
/// Public hostname/IP for tg:// link generation (overrides detected IP).
|
||||||
|
#[serde(default)]
|
||||||
|
pub public_host: Option<String>,
|
||||||
|
|
||||||
|
/// Public port for tg:// link generation (overrides server.port).
|
||||||
|
#[serde(default)]
|
||||||
|
pub public_port: Option<u16>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct ServerConfig {
|
||||||
|
#[serde(default = "default_port")]
|
||||||
|
pub port: u16,
|
||||||
|
|
||||||
|
#[serde(default)]
|
||||||
|
pub listen_addr_ipv4: Option<String>,
|
||||||
|
|
||||||
|
#[serde(default)]
|
||||||
|
pub listen_addr_ipv6: Option<String>,
|
||||||
|
|
||||||
|
#[serde(default)]
|
||||||
|
pub listen_unix_sock: Option<String>,
|
||||||
|
|
||||||
|
/// Unix socket file permissions (octal, e.g. "0666" or "0777").
|
||||||
|
/// Applied via chmod after bind. Default: no change (inherits umask).
|
||||||
|
#[serde(default)]
|
||||||
|
pub listen_unix_sock_perm: Option<String>,
|
||||||
|
|
||||||
|
/// Enable TCP listening. Default: true when no unix socket, false when
|
||||||
|
/// listen_unix_sock is set. Set explicitly to override auto-detection.
|
||||||
|
#[serde(default)]
|
||||||
|
pub listen_tcp: Option<bool>,
|
||||||
|
|
||||||
|
/// Accept HAProxy PROXY protocol headers on incoming connections.
|
||||||
|
/// When enabled, real client IPs are extracted from PROXY v1/v2 headers.
|
||||||
|
#[serde(default)]
|
||||||
|
pub proxy_protocol: bool,
|
||||||
|
|
||||||
|
#[serde(default)]
|
||||||
|
pub metrics_port: Option<u16>,
|
||||||
|
|
||||||
|
#[serde(default = "default_metrics_whitelist")]
|
||||||
|
pub metrics_whitelist: Vec<IpNetwork>,
|
||||||
|
|
||||||
|
#[serde(default)]
|
||||||
|
pub listeners: Vec<ListenerConfig>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for ServerConfig {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
port: default_port(),
|
||||||
|
listen_addr_ipv4: Some(default_listen_addr()),
|
||||||
|
listen_addr_ipv6: Some("::".to_string()),
|
||||||
|
listen_unix_sock: None,
|
||||||
|
listen_unix_sock_perm: None,
|
||||||
|
listen_tcp: None,
|
||||||
|
proxy_protocol: false,
|
||||||
|
metrics_port: None,
|
||||||
|
metrics_whitelist: default_metrics_whitelist(),
|
||||||
|
listeners: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct TimeoutsConfig {
|
||||||
|
#[serde(default = "default_handshake_timeout")]
|
||||||
|
pub client_handshake: u64,
|
||||||
|
|
||||||
|
#[serde(default = "default_connect_timeout")]
|
||||||
|
pub tg_connect: u64,
|
||||||
|
|
||||||
|
#[serde(default = "default_keepalive")]
|
||||||
|
pub client_keepalive: u64,
|
||||||
|
|
||||||
|
#[serde(default = "default_ack_timeout")]
|
||||||
|
pub client_ack: u64,
|
||||||
|
|
||||||
|
/// Number of quick ME reconnect attempts for single-address DC.
|
||||||
|
#[serde(default = "default_me_one_retry")]
|
||||||
|
pub me_one_retry: u8,
|
||||||
|
|
||||||
|
/// Timeout per quick attempt in milliseconds for single-address DC.
|
||||||
|
#[serde(default = "default_me_one_timeout")]
|
||||||
|
pub me_one_timeout_ms: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for TimeoutsConfig {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
client_handshake: default_handshake_timeout(),
|
||||||
|
tg_connect: default_connect_timeout(),
|
||||||
|
client_keepalive: default_keepalive(),
|
||||||
|
client_ack: default_ack_timeout(),
|
||||||
|
me_one_retry: default_me_one_retry(),
|
||||||
|
me_one_timeout_ms: default_me_one_timeout(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct AntiCensorshipConfig {
|
||||||
|
#[serde(default = "default_tls_domain")]
|
||||||
|
pub tls_domain: String,
|
||||||
|
|
||||||
|
/// Additional TLS domains for generating multiple proxy links.
|
||||||
|
#[serde(default)]
|
||||||
|
pub tls_domains: Vec<String>,
|
||||||
|
|
||||||
|
#[serde(default = "default_true")]
|
||||||
|
pub mask: bool,
|
||||||
|
|
||||||
|
#[serde(default)]
|
||||||
|
pub mask_host: Option<String>,
|
||||||
|
|
||||||
|
#[serde(default = "default_mask_port")]
|
||||||
|
pub mask_port: u16,
|
||||||
|
|
||||||
|
#[serde(default)]
|
||||||
|
pub mask_unix_sock: Option<String>,
|
||||||
|
|
||||||
|
#[serde(default = "default_fake_cert_len")]
|
||||||
|
pub fake_cert_len: usize,
|
||||||
|
|
||||||
|
/// Enable TLS certificate emulation using cached real certificates.
|
||||||
|
#[serde(default)]
|
||||||
|
pub tls_emulation: bool,
|
||||||
|
|
||||||
|
/// Directory to store TLS front cache (on disk).
|
||||||
|
#[serde(default = "default_tls_front_dir")]
|
||||||
|
pub tls_front_dir: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for AntiCensorshipConfig {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
tls_domain: default_tls_domain(),
|
||||||
|
tls_domains: Vec::new(),
|
||||||
|
mask: true,
|
||||||
|
mask_host: None,
|
||||||
|
mask_port: default_mask_port(),
|
||||||
|
mask_unix_sock: None,
|
||||||
|
fake_cert_len: default_fake_cert_len(),
|
||||||
|
tls_emulation: false,
|
||||||
|
tls_front_dir: default_tls_front_dir(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||||
|
pub struct AccessConfig {
|
||||||
|
#[serde(default)]
|
||||||
|
pub users: HashMap<String, String>,
|
||||||
|
|
||||||
|
#[serde(default)]
|
||||||
|
pub user_max_tcp_conns: HashMap<String, usize>,
|
||||||
|
|
||||||
|
#[serde(default)]
|
||||||
|
pub user_expirations: HashMap<String, DateTime<Utc>>,
|
||||||
|
|
||||||
|
#[serde(default)]
|
||||||
|
pub user_data_quota: HashMap<String, u64>,
|
||||||
|
|
||||||
|
#[serde(default)]
|
||||||
|
pub user_max_unique_ips: HashMap<String, usize>,
|
||||||
|
|
||||||
|
#[serde(default = "default_replay_check_len")]
|
||||||
|
pub replay_check_len: usize,
|
||||||
|
|
||||||
|
#[serde(default = "default_replay_window_secs")]
|
||||||
|
pub replay_window_secs: u64,
|
||||||
|
|
||||||
|
#[serde(default)]
|
||||||
|
pub ignore_time_skew: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for AccessConfig {
|
||||||
|
fn default() -> Self {
|
||||||
|
let mut users = HashMap::new();
|
||||||
|
users.insert(
|
||||||
|
"default".to_string(),
|
||||||
|
"00000000000000000000000000000000".to_string(),
|
||||||
|
);
|
||||||
|
Self {
|
||||||
|
users,
|
||||||
|
user_max_tcp_conns: HashMap::new(),
|
||||||
|
user_expirations: HashMap::new(),
|
||||||
|
user_data_quota: HashMap::new(),
|
||||||
|
user_max_unique_ips: HashMap::new(),
|
||||||
|
replay_check_len: default_replay_check_len(),
|
||||||
|
replay_window_secs: default_replay_window_secs(),
|
||||||
|
ignore_time_skew: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============= Aux Structures =============
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||||
|
#[serde(tag = "type", rename_all = "lowercase")]
|
||||||
|
pub enum UpstreamType {
|
||||||
|
Direct {
|
||||||
|
#[serde(default)]
|
||||||
|
interface: Option<String>,
|
||||||
|
#[serde(default)]
|
||||||
|
bind_addresses: Option<Vec<String>>,
|
||||||
|
},
|
||||||
|
Socks4 {
|
||||||
|
address: String,
|
||||||
|
#[serde(default)]
|
||||||
|
interface: Option<String>,
|
||||||
|
#[serde(default)]
|
||||||
|
user_id: Option<String>,
|
||||||
|
},
|
||||||
|
Socks5 {
|
||||||
|
address: String,
|
||||||
|
#[serde(default)]
|
||||||
|
interface: Option<String>,
|
||||||
|
#[serde(default)]
|
||||||
|
username: Option<String>,
|
||||||
|
#[serde(default)]
|
||||||
|
password: Option<String>,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct UpstreamConfig {
|
||||||
|
#[serde(flatten)]
|
||||||
|
pub upstream_type: UpstreamType,
|
||||||
|
#[serde(default = "default_weight")]
|
||||||
|
pub weight: u16,
|
||||||
|
#[serde(default = "default_true")]
|
||||||
|
pub enabled: bool,
|
||||||
|
#[serde(default)]
|
||||||
|
pub scopes: String,
|
||||||
|
#[serde(skip)]
|
||||||
|
pub selected_scope: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct ListenerConfig {
|
||||||
|
pub ip: IpAddr,
|
||||||
|
/// IP address or hostname to announce in proxy links.
|
||||||
|
/// Takes precedence over `announce_ip` if both are set.
|
||||||
|
#[serde(default)]
|
||||||
|
pub announce: Option<String>,
|
||||||
|
/// Deprecated: Use `announce` instead. IP address to announce in proxy links.
|
||||||
|
/// Migrated to `announce` automatically if `announce` is not set.
|
||||||
|
#[serde(default)]
|
||||||
|
pub announce_ip: Option<IpAddr>,
|
||||||
|
/// Per-listener PROXY protocol override. When set, overrides global server.proxy_protocol.
|
||||||
|
#[serde(default)]
|
||||||
|
pub proxy_protocol: Option<bool>,
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============= ShowLink =============
|
||||||
|
|
||||||
|
/// Controls which users' proxy links are displayed at startup.
|
||||||
|
///
|
||||||
|
/// In TOML, this can be:
|
||||||
|
/// - `show_link = "*"` — show links for all users
|
||||||
|
/// - `show_link = ["a", "b"]` — show links for specific users
|
||||||
|
/// - omitted — show no links (default)
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub enum ShowLink {
|
||||||
|
/// Don't show any links (default when omitted).
|
||||||
|
None,
|
||||||
|
/// Show links for all configured users.
|
||||||
|
All,
|
||||||
|
/// Show links for specific users.
|
||||||
|
Specific(Vec<String>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for ShowLink {
|
||||||
|
fn default() -> Self {
|
||||||
|
ShowLink::None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ShowLink {
|
||||||
|
/// Returns true if no links should be shown.
|
||||||
|
pub fn is_empty(&self) -> bool {
|
||||||
|
matches!(self, ShowLink::None) || matches!(self, ShowLink::Specific(v) if v.is_empty())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Resolve the list of user names to display, given all configured users.
|
||||||
|
pub fn resolve_users<'a>(&'a self, all_users: &'a HashMap<String, String>) -> Vec<&'a String> {
|
||||||
|
match self {
|
||||||
|
ShowLink::None => vec![],
|
||||||
|
ShowLink::All => {
|
||||||
|
let mut names: Vec<&String> = all_users.keys().collect();
|
||||||
|
names.sort();
|
||||||
|
names
|
||||||
|
}
|
||||||
|
ShowLink::Specific(names) => names.iter().collect(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Serialize for ShowLink {
|
||||||
|
fn serialize<S: serde::Serializer>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> {
|
||||||
|
match self {
|
||||||
|
ShowLink::None => Vec::<String>::new().serialize(serializer),
|
||||||
|
ShowLink::All => serializer.serialize_str("*"),
|
||||||
|
ShowLink::Specific(v) => v.serialize(serializer),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de> Deserialize<'de> for ShowLink {
|
||||||
|
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> std::result::Result<Self, D::Error> {
|
||||||
|
use serde::de;
|
||||||
|
|
||||||
|
struct ShowLinkVisitor;
|
||||||
|
|
||||||
|
impl<'de> de::Visitor<'de> for ShowLinkVisitor {
|
||||||
|
type Value = ShowLink;
|
||||||
|
|
||||||
|
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
|
formatter.write_str(r#""*" or an array of user names"#)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_str<E: de::Error>(self, v: &str) -> std::result::Result<ShowLink, E> {
|
||||||
|
if v == "*" {
|
||||||
|
Ok(ShowLink::All)
|
||||||
|
} else {
|
||||||
|
Err(de::Error::invalid_value(
|
||||||
|
de::Unexpected::Str(v),
|
||||||
|
&r#""*""#,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_seq<A: de::SeqAccess<'de>>(self, mut seq: A) -> std::result::Result<ShowLink, A::Error> {
|
||||||
|
let mut names = Vec::new();
|
||||||
|
while let Some(name) = seq.next_element::<String>()? {
|
||||||
|
names.push(name);
|
||||||
|
}
|
||||||
|
if names.is_empty() {
|
||||||
|
Ok(ShowLink::None)
|
||||||
|
} else {
|
||||||
|
Ok(ShowLink::Specific(names))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
deserializer.deserialize_any(ShowLinkVisitor)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,6 +11,9 @@ pub struct SecureRandom {
|
|||||||
inner: Mutex<SecureRandomInner>,
|
inner: Mutex<SecureRandomInner>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsafe impl Send for SecureRandom {}
|
||||||
|
unsafe impl Sync for SecureRandom {}
|
||||||
|
|
||||||
struct SecureRandomInner {
|
struct SecureRandomInner {
|
||||||
rng: StdRng,
|
rng: StdRng,
|
||||||
cipher: AesCtr,
|
cipher: AesCtr,
|
||||||
@@ -211,4 +214,4 @@ mod tests {
|
|||||||
|
|
||||||
assert_ne!(shuffled, original);
|
assert_ne!(shuffled, original);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
308
src/main.rs
308
src/main.rs
@@ -3,6 +3,7 @@
|
|||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
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;
|
||||||
@@ -16,27 +17,29 @@ mod config;
|
|||||||
mod crypto;
|
mod crypto;
|
||||||
mod error;
|
mod error;
|
||||||
mod ip_tracker;
|
mod ip_tracker;
|
||||||
|
mod network;
|
||||||
mod metrics;
|
mod metrics;
|
||||||
mod protocol;
|
mod protocol;
|
||||||
mod proxy;
|
mod proxy;
|
||||||
mod stats;
|
mod stats;
|
||||||
mod stream;
|
mod stream;
|
||||||
mod transport;
|
mod transport;
|
||||||
|
mod tls_front;
|
||||||
mod util;
|
mod util;
|
||||||
|
|
||||||
use crate::config::{LogLevel, ProxyConfig};
|
use crate::config::{LogLevel, ProxyConfig};
|
||||||
|
use crate::config::hot_reload::spawn_config_watcher;
|
||||||
use crate::crypto::SecureRandom;
|
use crate::crypto::SecureRandom;
|
||||||
use crate::ip_tracker::UserIpTracker;
|
use crate::ip_tracker::UserIpTracker;
|
||||||
|
use crate::network::probe::{decide_network_capabilities, log_probe_result, run_probe};
|
||||||
use crate::proxy::ClientHandler;
|
use crate::proxy::ClientHandler;
|
||||||
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, format_sample_line,
|
||||||
stun_probe,
|
|
||||||
};
|
};
|
||||||
use crate::transport::{ListenOptions, UpstreamManager, create_listener};
|
use crate::transport::{ListenOptions, UpstreamManager, create_listener};
|
||||||
use crate::util::ip::detect_ip;
|
use crate::tls_front::TlsFrontCache;
|
||||||
use crate::protocol::constants::{TG_MIDDLE_PROXIES_V4, TG_MIDDLE_PROXIES_V6};
|
|
||||||
|
|
||||||
fn parse_cli() -> (String, bool, Option<String>) {
|
fn parse_cli() -> (String, bool, Option<String>) {
|
||||||
let mut config_path = "config.toml".to_string();
|
let mut config_path = "config.toml".to_string();
|
||||||
@@ -93,6 +96,10 @@ fn parse_cli() -> (String, bool, Option<String>) {
|
|||||||
eprintln!(" --no-start Don't start the service after install");
|
eprintln!(" --no-start Don't start the service after install");
|
||||||
std::process::exit(0);
|
std::process::exit(0);
|
||||||
}
|
}
|
||||||
|
"--version" | "-V" => {
|
||||||
|
println!("telemt {}", env!("CARGO_PKG_VERSION"));
|
||||||
|
std::process::exit(0);
|
||||||
|
}
|
||||||
s if !s.starts_with('-') => {
|
s if !s.starts_with('-') => {
|
||||||
config_path = s.to_string();
|
config_path = s.to_string();
|
||||||
}
|
}
|
||||||
@@ -107,41 +114,54 @@ fn parse_cli() -> (String, bool, Option<String>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn print_proxy_links(host: &str, port: u16, config: &ProxyConfig) {
|
fn print_proxy_links(host: &str, port: u16, config: &ProxyConfig) {
|
||||||
info!("--- Proxy Links ({}) ---", host);
|
info!(target: "telemt::links", "--- Proxy Links ({}) ---", host);
|
||||||
for user_name in config.general.links.show.resolve_users(&config.access.users) {
|
for user_name in config.general.links.show.resolve_users(&config.access.users) {
|
||||||
if let Some(secret) = config.access.users.get(user_name) {
|
if let Some(secret) = config.access.users.get(user_name) {
|
||||||
info!("User: {}", user_name);
|
info!(target: "telemt::links", "User: {}", user_name);
|
||||||
if config.general.modes.classic {
|
if config.general.modes.classic {
|
||||||
info!(
|
info!(
|
||||||
|
target: "telemt::links",
|
||||||
" Classic: tg://proxy?server={}&port={}&secret={}",
|
" Classic: tg://proxy?server={}&port={}&secret={}",
|
||||||
host, port, secret
|
host, port, secret
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if config.general.modes.secure {
|
if config.general.modes.secure {
|
||||||
info!(
|
info!(
|
||||||
|
target: "telemt::links",
|
||||||
" DD: tg://proxy?server={}&port={}&secret=dd{}",
|
" DD: tg://proxy?server={}&port={}&secret=dd{}",
|
||||||
host, port, secret
|
host, port, secret
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if config.general.modes.tls {
|
if config.general.modes.tls {
|
||||||
let domain_hex = hex::encode(&config.censorship.tls_domain);
|
let mut domains = Vec::with_capacity(1 + config.censorship.tls_domains.len());
|
||||||
info!(
|
domains.push(config.censorship.tls_domain.clone());
|
||||||
" EE-TLS: tg://proxy?server={}&port={}&secret=ee{}{}",
|
for d in &config.censorship.tls_domains {
|
||||||
host, port, secret, domain_hex
|
if !domains.contains(d) {
|
||||||
);
|
domains.push(d.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for domain in domains {
|
||||||
|
let domain_hex = hex::encode(&domain);
|
||||||
|
info!(
|
||||||
|
target: "telemt::links",
|
||||||
|
" EE-TLS: tg://proxy?server={}&port={}&secret=ee{}{}",
|
||||||
|
host, port, secret, domain_hex
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
warn!("User '{}' in show_link not found", user_name);
|
warn!(target: "telemt::links", "User '{}' in show_link not found", user_name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
info!("------------------------");
|
info!(target: "telemt::links", "------------------------");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
|
async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
|
||||||
let (config_path, cli_silent, cli_log_level) = parse_cli();
|
let (config_path, cli_silent, cli_log_level) = parse_cli();
|
||||||
|
|
||||||
let config = match ProxyConfig::load(&config_path) {
|
let mut config = match ProxyConfig::load(&config_path) {
|
||||||
Ok(c) => c,
|
Ok(c) => c,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
if std::path::Path::new(&config_path).exists() {
|
if std::path::Path::new(&config_path).exists() {
|
||||||
@@ -219,20 +239,20 @@ 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 prefer_ipv6 = config.general.prefer_ipv6;
|
let probe = run_probe(
|
||||||
let mut use_middle_proxy = config.general.use_middle_proxy;
|
&config.network,
|
||||||
let config = Arc::new(config);
|
config.general.middle_proxy_nat_stun.clone(),
|
||||||
|
config.general.middle_proxy_nat_probe,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
let decision = decide_network_capabilities(&config.network, &probe);
|
||||||
|
log_probe_result(&probe, &decision);
|
||||||
|
|
||||||
|
let prefer_ipv6 = decision.prefer_ipv6();
|
||||||
|
let mut use_middle_proxy = config.general.use_middle_proxy && (decision.ipv4_me || decision.ipv6_me);
|
||||||
let stats = Arc::new(Stats::new());
|
let stats = Arc::new(Stats::new());
|
||||||
let rng = Arc::new(SecureRandom::new());
|
let rng = Arc::new(SecureRandom::new());
|
||||||
|
|
||||||
let replay_checker = Arc::new(ReplayChecker::new(
|
|
||||||
config.access.replay_check_len,
|
|
||||||
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));
|
|
||||||
|
|
||||||
// IP Tracker initialization
|
// IP Tracker initialization
|
||||||
let ip_tracker = Arc::new(UserIpTracker::new());
|
let ip_tracker = Arc::new(UserIpTracker::new());
|
||||||
ip_tracker.load_limits(&config.access.user_max_unique_ips).await;
|
ip_tracker.load_limits(&config.access.user_max_unique_ips).await;
|
||||||
@@ -244,39 +264,9 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
|
|||||||
// Connection concurrency limit
|
// Connection concurrency limit
|
||||||
let _max_connections = Arc::new(Semaphore::new(10_000));
|
let _max_connections = Arc::new(Semaphore::new(10_000));
|
||||||
|
|
||||||
// STUN check before choosing transport
|
if use_middle_proxy && !decision.ipv4_me && !decision.ipv6_me {
|
||||||
if use_middle_proxy {
|
warn!("No usable IP family for Middle Proxy detected; falling back to direct DC");
|
||||||
match stun_probe(config.general.middle_proxy_nat_stun.clone()).await {
|
use_middle_proxy = false;
|
||||||
Ok(Some(probe)) => {
|
|
||||||
info!(
|
|
||||||
local_ip = %probe.local_addr.ip(),
|
|
||||||
reflected_ip = %probe.reflected_addr.ip(),
|
|
||||||
"STUN Autodetect:"
|
|
||||||
);
|
|
||||||
if probe.local_addr.ip() != probe.reflected_addr.ip()
|
|
||||||
&& !config.general.stun_iface_mismatch_ignore
|
|
||||||
{
|
|
||||||
match crate::transport::middle_proxy::detect_public_ip().await {
|
|
||||||
Some(ip) => {
|
|
||||||
info!(
|
|
||||||
local_ip = %probe.local_addr.ip(),
|
|
||||||
reflected_ip = %probe.reflected_addr.ip(),
|
|
||||||
public_ip = %ip,
|
|
||||||
"STUN mismatch but public IP auto-detected, continuing with middle proxy"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
warn!(
|
|
||||||
"STUN/IP-on-Interface mismatch and public IP auto-detect failed -> fallback to direct-DC"
|
|
||||||
);
|
|
||||||
use_middle_proxy = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(None) => warn!("STUN probe returned no address; continuing"),
|
|
||||||
Err(e) => warn!(error = %e, "STUN probe failed; continuing"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// =====================================================================
|
// =====================================================================
|
||||||
@@ -348,21 +338,41 @@ match crate::transport::middle_proxy::fetch_proxy_secret(proxy_secret_path).awai
|
|||||||
config.general.middle_proxy_nat_ip,
|
config.general.middle_proxy_nat_ip,
|
||||||
config.general.middle_proxy_nat_probe,
|
config.general.middle_proxy_nat_probe,
|
||||||
config.general.middle_proxy_nat_stun.clone(),
|
config.general.middle_proxy_nat_stun.clone(),
|
||||||
|
config.general.middle_proxy_nat_stun_servers.clone(),
|
||||||
|
probe.detected_ipv6,
|
||||||
|
config.timeouts.me_one_retry,
|
||||||
|
config.timeouts.me_one_timeout_ms,
|
||||||
cfg_v4.map.clone(),
|
cfg_v4.map.clone(),
|
||||||
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(),
|
||||||
|
rng.clone(),
|
||||||
|
stats.clone(),
|
||||||
|
config.general.me_keepalive_enabled,
|
||||||
|
config.general.me_keepalive_interval_secs,
|
||||||
|
config.general.me_keepalive_jitter_secs,
|
||||||
|
config.general.me_keepalive_payload_random,
|
||||||
|
config.general.me_warmup_stagger_enabled,
|
||||||
|
config.general.me_warmup_step_delay_ms,
|
||||||
|
config.general.me_warmup_step_jitter_ms,
|
||||||
|
config.general.me_reconnect_max_concurrent_per_dc,
|
||||||
|
config.general.me_reconnect_backoff_base_ms,
|
||||||
|
config.general.me_reconnect_backoff_cap_ms,
|
||||||
|
config.general.me_reconnect_fast_retry_count,
|
||||||
);
|
);
|
||||||
|
|
||||||
match pool.init(2, &rng).await {
|
let pool_size = config.general.middle_proxy_pool_size.max(1);
|
||||||
|
match pool.init(pool_size, &rng).await {
|
||||||
Ok(()) => {
|
Ok(()) => {
|
||||||
info!("Middle-End pool initialized successfully");
|
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;
|
||||||
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, 2,
|
pool_clone, rng_clone, min_conns,
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
});
|
});
|
||||||
@@ -408,12 +418,93 @@ match crate::transport::middle_proxy::fetch_proxy_secret(proxy_secret_path).awai
|
|||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// If ME failed to initialize, force direct-only mode.
|
||||||
if me_pool.is_some() {
|
if me_pool.is_some() {
|
||||||
info!("Transport: Middle-End Proxy - all DC-over-RPC");
|
info!("Transport: Middle-End Proxy - all DC-over-RPC");
|
||||||
} else {
|
} else {
|
||||||
|
use_middle_proxy = false;
|
||||||
|
// Make runtime config reflect direct-only mode for handlers.
|
||||||
|
config.general.use_middle_proxy = false;
|
||||||
info!("Transport: Direct DC - TCP - standard DC-over-TCP");
|
info!("Transport: Direct DC - TCP - standard DC-over-TCP");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Freeze config after possible fallback decision
|
||||||
|
let config = Arc::new(config);
|
||||||
|
|
||||||
|
let replay_checker = Arc::new(ReplayChecker::new(
|
||||||
|
config.access.replay_check_len,
|
||||||
|
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));
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
// Initial synchronous fetch to warm cache before serving clients.
|
||||||
|
for domain in tls_domains.clone() {
|
||||||
|
match crate::tls_front::fetcher::fetch_real_tls(
|
||||||
|
&domain,
|
||||||
|
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(
|
||||||
|
domain,
|
||||||
|
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;
|
||||||
@@ -482,7 +573,12 @@ match crate::transport::middle_proxy::fetch_proxy_secret(proxy_secret_path).awai
|
|||||||
info!("================= Telegram DC Connectivity =================");
|
info!("================= Telegram DC Connectivity =================");
|
||||||
|
|
||||||
let ping_results = upstream_manager
|
let ping_results = upstream_manager
|
||||||
.ping_all_dcs(prefer_ipv6, &config.dc_overrides)
|
.ping_all_dcs(
|
||||||
|
prefer_ipv6,
|
||||||
|
&config.dc_overrides,
|
||||||
|
decision.ipv4_dc,
|
||||||
|
decision.ipv6_dc,
|
||||||
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
for upstream_result in &ping_results {
|
for upstream_result in &ping_results {
|
||||||
@@ -559,8 +655,15 @@ match crate::transport::middle_proxy::fetch_proxy_secret(proxy_secret_path).awai
|
|||||||
|
|
||||||
// Background tasks
|
// Background tasks
|
||||||
let um_clone = upstream_manager.clone();
|
let um_clone = upstream_manager.clone();
|
||||||
|
let decision_clone = decision.clone();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
um_clone.run_health_checks(prefer_ipv6).await;
|
um_clone
|
||||||
|
.run_health_checks(
|
||||||
|
prefer_ipv6,
|
||||||
|
decision_clone.ipv4_dc,
|
||||||
|
decision_clone.ipv6_dc,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
});
|
});
|
||||||
|
|
||||||
let rc_clone = replay_checker.clone();
|
let rc_clone = replay_checker.clone();
|
||||||
@@ -568,16 +671,44 @@ match crate::transport::middle_proxy::fetch_proxy_secret(proxy_secret_path).awai
|
|||||||
rc_clone.run_periodic_cleanup().await;
|
rc_clone.run_periodic_cleanup().await;
|
||||||
});
|
});
|
||||||
|
|
||||||
let detected_ip = detect_ip().await;
|
let detected_ip_v4: Option<std::net::IpAddr> = probe
|
||||||
|
.reflected_ipv4
|
||||||
|
.map(|s| s.ip())
|
||||||
|
.or_else(|| probe.detected_ipv4.map(std::net::IpAddr::V4));
|
||||||
|
let detected_ip_v6: Option<std::net::IpAddr> = probe
|
||||||
|
.reflected_ipv6
|
||||||
|
.map(|s| s.ip())
|
||||||
|
.or_else(|| probe.detected_ipv6.map(std::net::IpAddr::V6));
|
||||||
debug!(
|
debug!(
|
||||||
"Detected IPs: v4={:?} v6={:?}",
|
"Detected IPs: v4={:?} v6={:?}",
|
||||||
detected_ip.ipv4, detected_ip.ipv6
|
detected_ip_v4, detected_ip_v6
|
||||||
|
);
|
||||||
|
|
||||||
|
// ── Hot-reload watcher ────────────────────────────────────────────────
|
||||||
|
// Uses inotify to detect file changes instantly (SIGHUP also works).
|
||||||
|
// detected_ip_v4/v6 are passed so newly added users get correct TG links.
|
||||||
|
let (config_rx, mut log_level_rx): (
|
||||||
|
tokio::sync::watch::Receiver<Arc<ProxyConfig>>,
|
||||||
|
tokio::sync::watch::Receiver<LogLevel>,
|
||||||
|
) = spawn_config_watcher(
|
||||||
|
std::path::PathBuf::from(&config_path),
|
||||||
|
config.clone(),
|
||||||
|
detected_ip_v4,
|
||||||
|
detected_ip_v6,
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut listeners = Vec::new();
|
let mut listeners = Vec::new();
|
||||||
|
|
||||||
for listener_conf in &config.server.listeners {
|
for listener_conf in &config.server.listeners {
|
||||||
let addr = SocketAddr::new(listener_conf.ip, config.server.port);
|
let addr = SocketAddr::new(listener_conf.ip, config.server.port);
|
||||||
|
if addr.is_ipv4() && !decision.ipv4_dc {
|
||||||
|
warn!(%addr, "Skipping IPv4 listener: IPv4 disabled by [network]");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if addr.is_ipv6() && !decision.ipv6_dc {
|
||||||
|
warn!(%addr, "Skipping IPv6 listener: IPv6 disabled by [network]");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
let options = ListenOptions {
|
let options = ListenOptions {
|
||||||
ipv6_only: listener_conf.ip.is_ipv6(),
|
ipv6_only: listener_conf.ip.is_ipv6(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
@@ -587,6 +718,8 @@ match crate::transport::middle_proxy::fetch_proxy_secret(proxy_secret_path).awai
|
|||||||
Ok(socket) => {
|
Ok(socket) => {
|
||||||
let listener = TcpListener::from_std(socket.into())?;
|
let listener = TcpListener::from_std(socket.into())?;
|
||||||
info!("Listening on {}", addr);
|
info!("Listening on {}", addr);
|
||||||
|
let listener_proxy_protocol =
|
||||||
|
listener_conf.proxy_protocol.unwrap_or(config.server.proxy_protocol);
|
||||||
|
|
||||||
// Resolve the public host for link generation
|
// Resolve the public host for link generation
|
||||||
let public_host = if let Some(ref announce) = listener_conf.announce {
|
let public_host = if let Some(ref announce) = listener_conf.announce {
|
||||||
@@ -594,11 +727,11 @@ match crate::transport::middle_proxy::fetch_proxy_secret(proxy_secret_path).awai
|
|||||||
} else if listener_conf.ip.is_unspecified() {
|
} else if listener_conf.ip.is_unspecified() {
|
||||||
// Auto-detect for unspecified addresses
|
// Auto-detect for unspecified addresses
|
||||||
if listener_conf.ip.is_ipv4() {
|
if listener_conf.ip.is_ipv4() {
|
||||||
detected_ip.ipv4
|
detected_ip_v4
|
||||||
.map(|ip| ip.to_string())
|
.map(|ip| ip.to_string())
|
||||||
.unwrap_or_else(|| listener_conf.ip.to_string())
|
.unwrap_or_else(|| listener_conf.ip.to_string())
|
||||||
} else {
|
} else {
|
||||||
detected_ip.ipv6
|
detected_ip_v6
|
||||||
.map(|ip| ip.to_string())
|
.map(|ip| ip.to_string())
|
||||||
.unwrap_or_else(|| listener_conf.ip.to_string())
|
.unwrap_or_else(|| listener_conf.ip.to_string())
|
||||||
}
|
}
|
||||||
@@ -612,7 +745,7 @@ match crate::transport::middle_proxy::fetch_proxy_secret(proxy_secret_path).awai
|
|||||||
print_proxy_links(&public_host, link_port, &config);
|
print_proxy_links(&public_host, link_port, &config);
|
||||||
}
|
}
|
||||||
|
|
||||||
listeners.push(listener);
|
listeners.push((listener, listener_proxy_protocol));
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("Failed to bind to {}: {}", addr, e);
|
error!("Failed to bind to {}: {}", addr, e);
|
||||||
@@ -626,9 +759,8 @@ match crate::transport::middle_proxy::fetch_proxy_secret(proxy_secret_path).awai
|
|||||||
let (host, port) = if let Some(ref h) = config.general.links.public_host {
|
let (host, port) = if let Some(ref h) = config.general.links.public_host {
|
||||||
(h.clone(), config.general.links.public_port.unwrap_or(config.server.port))
|
(h.clone(), config.general.links.public_port.unwrap_or(config.server.port))
|
||||||
} else {
|
} else {
|
||||||
let ip = detected_ip
|
let ip = detected_ip_v4
|
||||||
.ipv4
|
.or(detected_ip_v6)
|
||||||
.or(detected_ip.ipv6)
|
|
||||||
.map(|ip| ip.to_string());
|
.map(|ip| ip.to_string());
|
||||||
if ip.is_none() {
|
if ip.is_none() {
|
||||||
warn!("show_link is configured but public IP could not be detected. Set public_host in config.");
|
warn!("show_link is configured but public IP could not be detected. Set public_host in config.");
|
||||||
@@ -671,13 +803,14 @@ match crate::transport::middle_proxy::fetch_proxy_secret(proxy_secret_path).awai
|
|||||||
|
|
||||||
has_unix_listener = true;
|
has_unix_listener = true;
|
||||||
|
|
||||||
let config = config.clone();
|
let mut config_rx_unix: tokio::sync::watch::Receiver<Arc<ProxyConfig>> = config_rx.clone();
|
||||||
let stats = stats.clone();
|
let stats = stats.clone();
|
||||||
let upstream_manager = upstream_manager.clone();
|
let upstream_manager = upstream_manager.clone();
|
||||||
let replay_checker = replay_checker.clone();
|
let replay_checker = replay_checker.clone();
|
||||||
let buffer_pool = buffer_pool.clone();
|
let buffer_pool = buffer_pool.clone();
|
||||||
let rng = rng.clone();
|
let rng = rng.clone();
|
||||||
let me_pool = me_pool.clone();
|
let me_pool = me_pool.clone();
|
||||||
|
let tls_cache = tls_cache.clone();
|
||||||
let ip_tracker = ip_tracker.clone();
|
let ip_tracker = ip_tracker.clone();
|
||||||
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
@@ -689,20 +822,22 @@ match crate::transport::middle_proxy::fetch_proxy_secret(proxy_secret_path).awai
|
|||||||
let conn_id = unix_conn_counter.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
let conn_id = unix_conn_counter.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||||
let fake_peer = SocketAddr::from(([127, 0, 0, 1], (conn_id % 65535) as u16));
|
let fake_peer = SocketAddr::from(([127, 0, 0, 1], (conn_id % 65535) as u16));
|
||||||
|
|
||||||
let config = config.clone();
|
let config = config_rx_unix.borrow_and_update().clone();
|
||||||
let stats = stats.clone();
|
let stats = stats.clone();
|
||||||
let upstream_manager = upstream_manager.clone();
|
let upstream_manager = upstream_manager.clone();
|
||||||
let replay_checker = replay_checker.clone();
|
let replay_checker = replay_checker.clone();
|
||||||
let buffer_pool = buffer_pool.clone();
|
let buffer_pool = buffer_pool.clone();
|
||||||
let rng = rng.clone();
|
let rng = rng.clone();
|
||||||
let me_pool = me_pool.clone();
|
let me_pool = me_pool.clone();
|
||||||
|
let tls_cache = tls_cache.clone();
|
||||||
let ip_tracker = ip_tracker.clone();
|
let ip_tracker = ip_tracker.clone();
|
||||||
|
let proxy_protocol_enabled = config.server.proxy_protocol;
|
||||||
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
if let Err(e) = crate::proxy::client::handle_client_stream(
|
if let Err(e) = crate::proxy::client::handle_client_stream(
|
||||||
stream, fake_peer, config, stats,
|
stream, fake_peer, config, stats,
|
||||||
upstream_manager, replay_checker, buffer_pool, rng,
|
upstream_manager, replay_checker, buffer_pool, rng,
|
||||||
me_pool, ip_tracker,
|
me_pool, tls_cache, ip_tracker, proxy_protocol_enabled,
|
||||||
).await {
|
).await {
|
||||||
debug!(error = %e, "Unix socket connection error");
|
debug!(error = %e, "Unix socket connection error");
|
||||||
}
|
}
|
||||||
@@ -725,6 +860,8 @@ match crate::transport::middle_proxy::fetch_proxy_secret(proxy_secret_path).awai
|
|||||||
// Switch to user-configured log level after startup
|
// Switch to user-configured log level after startup
|
||||||
let runtime_filter = if has_rust_log {
|
let runtime_filter = if has_rust_log {
|
||||||
EnvFilter::from_default_env()
|
EnvFilter::from_default_env()
|
||||||
|
} else if matches!(effective_log_level, LogLevel::Silent) {
|
||||||
|
EnvFilter::new("warn,telemt::links=info")
|
||||||
} else {
|
} else {
|
||||||
EnvFilter::new(effective_log_level.to_filter_str())
|
EnvFilter::new(effective_log_level.to_filter_str())
|
||||||
};
|
};
|
||||||
@@ -732,6 +869,20 @@ match crate::transport::middle_proxy::fetch_proxy_secret(proxy_secret_path).awai
|
|||||||
.reload(runtime_filter)
|
.reload(runtime_filter)
|
||||||
.expect("Failed to switch log filter");
|
.expect("Failed to switch log filter");
|
||||||
|
|
||||||
|
// Apply log_level changes from hot-reload to the tracing filter.
|
||||||
|
tokio::spawn(async move {
|
||||||
|
loop {
|
||||||
|
if log_level_rx.changed().await.is_err() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let level = log_level_rx.borrow_and_update().clone();
|
||||||
|
let new_filter = tracing_subscriber::EnvFilter::new(level.to_filter_str());
|
||||||
|
if let Err(e) = filter_handle.reload(new_filter) {
|
||||||
|
tracing::error!("config reload: failed to update log filter: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
if let Some(port) = config.server.metrics_port {
|
if let Some(port) = config.server.metrics_port {
|
||||||
let stats = stats.clone();
|
let stats = stats.clone();
|
||||||
let whitelist = config.server.metrics_whitelist.clone();
|
let whitelist = config.server.metrics_whitelist.clone();
|
||||||
@@ -740,28 +891,31 @@ match crate::transport::middle_proxy::fetch_proxy_secret(proxy_secret_path).awai
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
for listener in listeners {
|
for (listener, listener_proxy_protocol) in listeners {
|
||||||
let config = config.clone();
|
let mut config_rx: tokio::sync::watch::Receiver<Arc<ProxyConfig>> = config_rx.clone();
|
||||||
let stats = stats.clone();
|
let stats = stats.clone();
|
||||||
let upstream_manager = upstream_manager.clone();
|
let upstream_manager = upstream_manager.clone();
|
||||||
let replay_checker = replay_checker.clone();
|
let replay_checker = replay_checker.clone();
|
||||||
let buffer_pool = buffer_pool.clone();
|
let buffer_pool = buffer_pool.clone();
|
||||||
let rng = rng.clone();
|
let rng = rng.clone();
|
||||||
let me_pool = me_pool.clone();
|
let me_pool = me_pool.clone();
|
||||||
|
let tls_cache = tls_cache.clone();
|
||||||
let ip_tracker = ip_tracker.clone();
|
let ip_tracker = ip_tracker.clone();
|
||||||
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
loop {
|
loop {
|
||||||
match listener.accept().await {
|
match listener.accept().await {
|
||||||
Ok((stream, peer_addr)) => {
|
Ok((stream, peer_addr)) => {
|
||||||
let config = config.clone();
|
let config = config_rx.borrow_and_update().clone();
|
||||||
let stats = stats.clone();
|
let stats = stats.clone();
|
||||||
let upstream_manager = upstream_manager.clone();
|
let upstream_manager = upstream_manager.clone();
|
||||||
let replay_checker = replay_checker.clone();
|
let replay_checker = replay_checker.clone();
|
||||||
let buffer_pool = buffer_pool.clone();
|
let buffer_pool = buffer_pool.clone();
|
||||||
let rng = rng.clone();
|
let rng = rng.clone();
|
||||||
let me_pool = me_pool.clone();
|
let me_pool = me_pool.clone();
|
||||||
|
let tls_cache = tls_cache.clone();
|
||||||
let ip_tracker = ip_tracker.clone();
|
let ip_tracker = ip_tracker.clone();
|
||||||
|
let proxy_protocol_enabled = listener_proxy_protocol;
|
||||||
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
if let Err(e) = ClientHandler::new(
|
if let Err(e) = ClientHandler::new(
|
||||||
@@ -774,12 +928,14 @@ match crate::transport::middle_proxy::fetch_proxy_secret(proxy_secret_path).awai
|
|||||||
buffer_pool,
|
buffer_pool,
|
||||||
rng,
|
rng,
|
||||||
me_pool,
|
me_pool,
|
||||||
|
tls_cache,
|
||||||
ip_tracker,
|
ip_tracker,
|
||||||
|
proxy_protocol_enabled,
|
||||||
)
|
)
|
||||||
.run()
|
.run()
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
debug!(peer = %peer_addr, error = %e, "Connection error");
|
warn!(peer = %peer_addr, error = %e, "Connection closed with error");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,19 @@
|
|||||||
use std::convert::Infallible;
|
use std::convert::Infallible;
|
||||||
use std::net::{IpAddr, SocketAddr};
|
use std::net::SocketAddr;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use http_body_util::Full;
|
use http_body_util::{Full, BodyExt};
|
||||||
use hyper::body::Bytes;
|
use hyper::body::Bytes;
|
||||||
use hyper::server::conn::http1;
|
use hyper::server::conn::http1;
|
||||||
use hyper::service::service_fn;
|
use hyper::service::service_fn;
|
||||||
use hyper::{Request, Response, StatusCode};
|
use hyper::{Request, Response, StatusCode};
|
||||||
|
use ipnetwork::IpNetwork;
|
||||||
use tokio::net::TcpListener;
|
use tokio::net::TcpListener;
|
||||||
use tracing::{info, warn, debug};
|
use tracing::{info, warn, debug};
|
||||||
|
|
||||||
use crate::stats::Stats;
|
use crate::stats::Stats;
|
||||||
|
|
||||||
pub async fn serve(port: u16, stats: Arc<Stats>, whitelist: Vec<IpAddr>) {
|
pub async fn serve(port: u16, stats: Arc<Stats>, whitelist: Vec<IpNetwork>) {
|
||||||
let addr = SocketAddr::from(([0, 0, 0, 0], port));
|
let addr = SocketAddr::from(([0, 0, 0, 0], port));
|
||||||
let listener = match TcpListener::bind(addr).await {
|
let listener = match TcpListener::bind(addr).await {
|
||||||
Ok(l) => l,
|
Ok(l) => l,
|
||||||
@@ -32,7 +33,7 @@ pub async fn serve(port: u16, stats: Arc<Stats>, whitelist: Vec<IpAddr>) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if !whitelist.is_empty() && !whitelist.contains(&peer.ip()) {
|
if !whitelist.is_empty() && !whitelist.iter().any(|net| net.contains(peer.ip())) {
|
||||||
debug!(peer = %peer, "Metrics request denied by whitelist");
|
debug!(peer = %peer, "Metrics request denied by whitelist");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -53,7 +54,7 @@ pub async fn serve(port: u16, stats: Arc<Stats>, whitelist: Vec<IpAddr>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle(req: Request<hyper::body::Incoming>, stats: &Stats) -> Result<Response<Full<Bytes>>, Infallible> {
|
fn handle<B>(req: Request<B>, stats: &Stats) -> Result<Response<Full<Bytes>>, Infallible> {
|
||||||
if req.uri().path() != "/metrics" {
|
if req.uri().path() != "/metrics" {
|
||||||
let resp = Response::builder()
|
let resp = Response::builder()
|
||||||
.status(StatusCode::NOT_FOUND)
|
.status(StatusCode::NOT_FOUND)
|
||||||
@@ -91,6 +92,22 @@ fn render_metrics(stats: &Stats) -> String {
|
|||||||
let _ = writeln!(out, "# TYPE telemt_handshake_timeouts_total counter");
|
let _ = writeln!(out, "# TYPE telemt_handshake_timeouts_total counter");
|
||||||
let _ = writeln!(out, "telemt_handshake_timeouts_total {}", stats.get_handshake_timeouts());
|
let _ = writeln!(out, "telemt_handshake_timeouts_total {}", stats.get_handshake_timeouts());
|
||||||
|
|
||||||
|
let _ = writeln!(out, "# HELP telemt_me_keepalive_sent_total ME keepalive frames sent");
|
||||||
|
let _ = writeln!(out, "# TYPE telemt_me_keepalive_sent_total counter");
|
||||||
|
let _ = writeln!(out, "telemt_me_keepalive_sent_total {}", stats.get_me_keepalive_sent());
|
||||||
|
|
||||||
|
let _ = writeln!(out, "# HELP telemt_me_keepalive_failed_total ME keepalive send failures");
|
||||||
|
let _ = writeln!(out, "# TYPE telemt_me_keepalive_failed_total counter");
|
||||||
|
let _ = writeln!(out, "telemt_me_keepalive_failed_total {}", stats.get_me_keepalive_failed());
|
||||||
|
|
||||||
|
let _ = writeln!(out, "# HELP telemt_me_reconnect_attempts_total ME reconnect attempts");
|
||||||
|
let _ = writeln!(out, "# TYPE telemt_me_reconnect_attempts_total counter");
|
||||||
|
let _ = writeln!(out, "telemt_me_reconnect_attempts_total {}", stats.get_me_reconnect_attempts());
|
||||||
|
|
||||||
|
let _ = writeln!(out, "# HELP telemt_me_reconnect_success_total ME reconnect successes");
|
||||||
|
let _ = writeln!(out, "# TYPE telemt_me_reconnect_success_total counter");
|
||||||
|
let _ = writeln!(out, "telemt_me_reconnect_success_total {}", stats.get_me_reconnect_success());
|
||||||
|
|
||||||
let _ = writeln!(out, "# HELP telemt_user_connections_total Per-user total connections");
|
let _ = writeln!(out, "# HELP telemt_user_connections_total Per-user total connections");
|
||||||
let _ = writeln!(out, "# TYPE telemt_user_connections_total counter");
|
let _ = writeln!(out, "# TYPE telemt_user_connections_total counter");
|
||||||
let _ = writeln!(out, "# HELP telemt_user_connections_current Per-user active connections");
|
let _ = writeln!(out, "# HELP telemt_user_connections_current Per-user active connections");
|
||||||
@@ -177,21 +194,20 @@ mod tests {
|
|||||||
stats.increment_connects_all();
|
stats.increment_connects_all();
|
||||||
stats.increment_connects_all();
|
stats.increment_connects_all();
|
||||||
|
|
||||||
let port = 19091u16;
|
let req = Request::builder()
|
||||||
let s = stats.clone();
|
.uri("/metrics")
|
||||||
tokio::spawn(async move {
|
.body(())
|
||||||
serve(port, s, vec![]).await;
|
.unwrap();
|
||||||
});
|
let resp = handle(req, &stats).unwrap();
|
||||||
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
let body = resp.into_body().collect().await.unwrap().to_bytes();
|
||||||
|
assert!(std::str::from_utf8(body.as_ref()).unwrap().contains("telemt_connections_total 3"));
|
||||||
|
|
||||||
let resp = reqwest::get(format!("http://127.0.0.1:{}/metrics", port))
|
let req404 = Request::builder()
|
||||||
.await.unwrap();
|
.uri("/other")
|
||||||
assert_eq!(resp.status(), 200);
|
.body(())
|
||||||
let body = resp.text().await.unwrap();
|
.unwrap();
|
||||||
assert!(body.contains("telemt_connections_total 3"));
|
let resp404 = handle(req404, &stats).unwrap();
|
||||||
|
assert_eq!(resp404.status(), StatusCode::NOT_FOUND);
|
||||||
let resp404 = reqwest::get(format!("http://127.0.0.1:{}/other", port))
|
|
||||||
.await.unwrap();
|
|
||||||
assert_eq!(resp404.status(), 404);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
4
src/network/mod.rs
Normal file
4
src/network/mod.rs
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
pub mod probe;
|
||||||
|
pub mod stun;
|
||||||
|
|
||||||
|
pub use stun::IpFamily;
|
||||||
231
src/network/probe.rs
Normal file
231
src/network/probe.rs
Normal file
@@ -0,0 +1,231 @@
|
|||||||
|
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, UdpSocket};
|
||||||
|
|
||||||
|
use tracing::{info, warn};
|
||||||
|
|
||||||
|
use crate::config::NetworkConfig;
|
||||||
|
use crate::error::Result;
|
||||||
|
use crate::network::stun::{stun_probe_dual, DualStunResult, IpFamily};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Default)]
|
||||||
|
pub struct NetworkProbe {
|
||||||
|
pub detected_ipv4: Option<Ipv4Addr>,
|
||||||
|
pub detected_ipv6: Option<Ipv6Addr>,
|
||||||
|
pub reflected_ipv4: Option<SocketAddr>,
|
||||||
|
pub reflected_ipv6: Option<SocketAddr>,
|
||||||
|
pub ipv4_is_bogon: bool,
|
||||||
|
pub ipv6_is_bogon: bool,
|
||||||
|
pub ipv4_nat_detected: bool,
|
||||||
|
pub ipv6_nat_detected: bool,
|
||||||
|
pub ipv4_usable: bool,
|
||||||
|
pub ipv6_usable: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Default)]
|
||||||
|
pub struct NetworkDecision {
|
||||||
|
pub ipv4_dc: bool,
|
||||||
|
pub ipv6_dc: bool,
|
||||||
|
pub ipv4_me: bool,
|
||||||
|
pub ipv6_me: bool,
|
||||||
|
pub effective_prefer: u8,
|
||||||
|
pub effective_multipath: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NetworkDecision {
|
||||||
|
pub fn prefer_ipv6(&self) -> bool {
|
||||||
|
self.effective_prefer == 6
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn me_families(&self) -> Vec<IpFamily> {
|
||||||
|
let mut res = Vec::new();
|
||||||
|
if self.ipv4_me {
|
||||||
|
res.push(IpFamily::V4);
|
||||||
|
}
|
||||||
|
if self.ipv6_me {
|
||||||
|
res.push(IpFamily::V6);
|
||||||
|
}
|
||||||
|
res
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn run_probe(config: &NetworkConfig, stun_addr: Option<String>, nat_probe: bool) -> Result<NetworkProbe> {
|
||||||
|
let mut probe = NetworkProbe::default();
|
||||||
|
|
||||||
|
probe.detected_ipv4 = detect_local_ip_v4();
|
||||||
|
probe.detected_ipv6 = detect_local_ip_v6();
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
let stun_server = stun_addr.unwrap_or_else(|| "stun.l.google.com:19302".to_string());
|
||||||
|
let stun_res = if nat_probe {
|
||||||
|
match stun_probe_dual(&stun_server).await {
|
||||||
|
Ok(res) => res,
|
||||||
|
Err(e) => {
|
||||||
|
warn!(error = %e, "STUN probe failed, continuing without reflection");
|
||||||
|
DualStunResult::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
DualStunResult::default()
|
||||||
|
};
|
||||||
|
probe.reflected_ipv4 = stun_res.v4.map(|r| r.reflected_addr);
|
||||||
|
probe.reflected_ipv6 = stun_res.v6.map(|r| r.reflected_addr);
|
||||||
|
|
||||||
|
probe.ipv4_nat_detected = match (probe.detected_ipv4, probe.reflected_ipv4) {
|
||||||
|
(Some(det), Some(reflected)) => det != reflected.ip(),
|
||||||
|
_ => false,
|
||||||
|
};
|
||||||
|
probe.ipv6_nat_detected = match (probe.detected_ipv6, probe.reflected_ipv6) {
|
||||||
|
(Some(det), Some(reflected)) => det != reflected.ip(),
|
||||||
|
_ => false,
|
||||||
|
};
|
||||||
|
|
||||||
|
probe.ipv4_usable = config.ipv4
|
||||||
|
&& probe.detected_ipv4.is_some()
|
||||||
|
&& (!probe.ipv4_is_bogon || probe.reflected_ipv4.map(|r| !is_bogon(r.ip())).unwrap_or(false));
|
||||||
|
|
||||||
|
let ipv6_enabled = config.ipv6.unwrap_or(probe.detected_ipv6.is_some());
|
||||||
|
probe.ipv6_usable = ipv6_enabled
|
||||||
|
&& probe.detected_ipv6.is_some()
|
||||||
|
&& (!probe.ipv6_is_bogon || probe.reflected_ipv6.map(|r| !is_bogon(r.ip())).unwrap_or(false));
|
||||||
|
|
||||||
|
Ok(probe)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn decide_network_capabilities(config: &NetworkConfig, probe: &NetworkProbe) -> NetworkDecision {
|
||||||
|
let mut decision = NetworkDecision::default();
|
||||||
|
|
||||||
|
decision.ipv4_dc = config.ipv4 && probe.detected_ipv4.is_some();
|
||||||
|
decision.ipv6_dc = config.ipv6.unwrap_or(probe.detected_ipv6.is_some()) && probe.detected_ipv6.is_some();
|
||||||
|
|
||||||
|
decision.ipv4_me = config.ipv4
|
||||||
|
&& probe.detected_ipv4.is_some()
|
||||||
|
&& (!probe.ipv4_is_bogon || probe.reflected_ipv4.is_some());
|
||||||
|
|
||||||
|
let ipv6_enabled = config.ipv6.unwrap_or(probe.detected_ipv6.is_some());
|
||||||
|
decision.ipv6_me = ipv6_enabled
|
||||||
|
&& probe.detected_ipv6.is_some()
|
||||||
|
&& (!probe.ipv6_is_bogon || probe.reflected_ipv6.is_some());
|
||||||
|
|
||||||
|
decision.effective_prefer = match config.prefer {
|
||||||
|
6 if decision.ipv6_me || decision.ipv6_dc => 6,
|
||||||
|
4 if decision.ipv4_me || decision.ipv4_dc => 4,
|
||||||
|
6 => {
|
||||||
|
warn!("prefer=6 requested but IPv6 unavailable; falling back to IPv4");
|
||||||
|
4
|
||||||
|
}
|
||||||
|
_ => 4,
|
||||||
|
};
|
||||||
|
|
||||||
|
let me_families = decision.ipv4_me as u8 + decision.ipv6_me as u8;
|
||||||
|
decision.effective_multipath = config.multipath && me_families >= 2;
|
||||||
|
|
||||||
|
decision
|
||||||
|
}
|
||||||
|
|
||||||
|
fn detect_local_ip_v4() -> Option<Ipv4Addr> {
|
||||||
|
let socket = UdpSocket::bind("0.0.0.0:0").ok()?;
|
||||||
|
socket.connect("8.8.8.8:80").ok()?;
|
||||||
|
match socket.local_addr().ok()?.ip() {
|
||||||
|
IpAddr::V4(v4) => Some(v4),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn detect_local_ip_v6() -> Option<Ipv6Addr> {
|
||||||
|
let socket = UdpSocket::bind("[::]:0").ok()?;
|
||||||
|
socket.connect("[2001:4860:4860::8888]:80").ok()?;
|
||||||
|
match socket.local_addr().ok()?.ip() {
|
||||||
|
IpAddr::V6(v6) => Some(v6),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_bogon(ip: IpAddr) -> bool {
|
||||||
|
match ip {
|
||||||
|
IpAddr::V4(v4) => is_bogon_v4(v4),
|
||||||
|
IpAddr::V6(v6) => is_bogon_v6(v6),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_bogon_v4(ip: Ipv4Addr) -> bool {
|
||||||
|
let octets = ip.octets();
|
||||||
|
if ip.is_private() || ip.is_loopback() || ip.is_link_local() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if octets[0] == 0 {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if octets[0] == 100 && (octets[1] & 0xC0) == 64 {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if octets[0] == 192 && octets[1] == 0 && octets[2] == 0 {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if octets[0] == 192 && octets[1] == 0 && octets[2] == 2 {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if octets[0] == 198 && (octets[1] & 0xFE) == 18 {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if octets[0] == 198 && octets[1] == 51 && octets[2] == 100 {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if octets[0] == 203 && octets[1] == 0 && octets[2] == 113 {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if ip.is_multicast() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if octets[0] >= 240 {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if ip.is_broadcast() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_bogon_v6(ip: Ipv6Addr) -> bool {
|
||||||
|
if ip.is_unspecified() || ip.is_loopback() || ip.is_unique_local() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
let segs = ip.segments();
|
||||||
|
if (segs[0] & 0xFFC0) == 0xFE80 {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if segs[0..5] == [0, 0, 0, 0, 0] && segs[5] == 0xFFFF {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if segs[0] == 0x0100 && segs[1..4] == [0, 0, 0] {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if segs[0] == 0x2001 && segs[1] == 0x0db8 {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if segs[0] == 0x2002 {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if ip.is_multicast() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn log_probe_result(probe: &NetworkProbe, decision: &NetworkDecision) {
|
||||||
|
info!(
|
||||||
|
ipv4 = probe.detected_ipv4.as_ref().map(|v| v.to_string()).unwrap_or_else(|| "-".into()),
|
||||||
|
ipv6 = probe.detected_ipv6.as_ref().map(|v| v.to_string()).unwrap_or_else(|| "-".into()),
|
||||||
|
reflected_v4 = probe.reflected_ipv4.as_ref().map(|v| v.ip().to_string()).unwrap_or_else(|| "-".into()),
|
||||||
|
reflected_v6 = probe.reflected_ipv6.as_ref().map(|v| v.ip().to_string()).unwrap_or_else(|| "-".into()),
|
||||||
|
ipv4_bogon = probe.ipv4_is_bogon,
|
||||||
|
ipv6_bogon = probe.ipv6_is_bogon,
|
||||||
|
ipv4_me = decision.ipv4_me,
|
||||||
|
ipv6_me = decision.ipv6_me,
|
||||||
|
ipv4_dc = decision.ipv4_dc,
|
||||||
|
ipv6_dc = decision.ipv6_dc,
|
||||||
|
prefer = decision.effective_prefer,
|
||||||
|
multipath = decision.effective_multipath,
|
||||||
|
"Network capabilities resolved"
|
||||||
|
);
|
||||||
|
}
|
||||||
210
src/network/stun.rs
Normal file
210
src/network/stun.rs
Normal file
@@ -0,0 +1,210 @@
|
|||||||
|
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
|
||||||
|
|
||||||
|
use tokio::net::{lookup_host, UdpSocket};
|
||||||
|
use tokio::time::{timeout, Duration, sleep};
|
||||||
|
|
||||||
|
use crate::error::{ProxyError, Result};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub enum IpFamily {
|
||||||
|
V4,
|
||||||
|
V6,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub struct StunProbeResult {
|
||||||
|
pub local_addr: SocketAddr,
|
||||||
|
pub reflected_addr: SocketAddr,
|
||||||
|
pub family: IpFamily,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default, Clone)]
|
||||||
|
pub struct DualStunResult {
|
||||||
|
pub v4: Option<StunProbeResult>,
|
||||||
|
pub v6: Option<StunProbeResult>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn stun_probe_dual(stun_addr: &str) -> Result<DualStunResult> {
|
||||||
|
let (v4, v6) = tokio::join!(
|
||||||
|
stun_probe_family(stun_addr, IpFamily::V4),
|
||||||
|
stun_probe_family(stun_addr, IpFamily::V6),
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(DualStunResult {
|
||||||
|
v4: v4?,
|
||||||
|
v6: v6?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn stun_probe_family(stun_addr: &str, family: IpFamily) -> Result<Option<StunProbeResult>> {
|
||||||
|
use rand::RngCore;
|
||||||
|
|
||||||
|
let bind_addr = match family {
|
||||||
|
IpFamily::V4 => "0.0.0.0:0",
|
||||||
|
IpFamily::V6 => "[::]:0",
|
||||||
|
};
|
||||||
|
|
||||||
|
let socket = UdpSocket::bind(bind_addr)
|
||||||
|
.await
|
||||||
|
.map_err(|e| ProxyError::Proxy(format!("STUN bind failed: {e}")))?;
|
||||||
|
|
||||||
|
let target_addr = resolve_stun_addr(stun_addr, family).await?;
|
||||||
|
if let Some(addr) = target_addr {
|
||||||
|
match socket.connect(addr).await {
|
||||||
|
Ok(()) => {}
|
||||||
|
Err(e) if family == IpFamily::V6 && matches!(
|
||||||
|
e.kind(),
|
||||||
|
std::io::ErrorKind::NetworkUnreachable
|
||||||
|
| std::io::ErrorKind::HostUnreachable
|
||||||
|
| std::io::ErrorKind::Unsupported
|
||||||
|
| std::io::ErrorKind::NetworkDown
|
||||||
|
) => return Ok(None),
|
||||||
|
Err(e) => return Err(ProxyError::Proxy(format!("STUN connect failed: {e}"))),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut req = [0u8; 20];
|
||||||
|
req[0..2].copy_from_slice(&0x0001u16.to_be_bytes()); // Binding Request
|
||||||
|
req[2..4].copy_from_slice(&0u16.to_be_bytes()); // length
|
||||||
|
req[4..8].copy_from_slice(&0x2112A442u32.to_be_bytes()); // magic cookie
|
||||||
|
rand::rng().fill_bytes(&mut req[8..20]); // transaction ID
|
||||||
|
|
||||||
|
let mut buf = [0u8; 256];
|
||||||
|
let mut attempt = 0;
|
||||||
|
let mut backoff = Duration::from_secs(1);
|
||||||
|
loop {
|
||||||
|
socket
|
||||||
|
.send(&req)
|
||||||
|
.await
|
||||||
|
.map_err(|e| ProxyError::Proxy(format!("STUN send failed: {e}")))?;
|
||||||
|
|
||||||
|
let recv_res = timeout(Duration::from_secs(3), socket.recv(&mut buf)).await;
|
||||||
|
let n = match recv_res {
|
||||||
|
Ok(Ok(n)) => n,
|
||||||
|
Ok(Err(e)) => return Err(ProxyError::Proxy(format!("STUN recv failed: {e}"))),
|
||||||
|
Err(_) => {
|
||||||
|
attempt += 1;
|
||||||
|
if attempt >= 3 {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
sleep(backoff).await;
|
||||||
|
backoff *= 2;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if n < 20 {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
|
||||||
|
let magic = 0x2112A442u32.to_be_bytes();
|
||||||
|
let txid = &req[8..20];
|
||||||
|
let mut idx = 20;
|
||||||
|
while idx + 4 <= n {
|
||||||
|
let atype = u16::from_be_bytes(buf[idx..idx + 2].try_into().unwrap());
|
||||||
|
let alen = u16::from_be_bytes(buf[idx + 2..idx + 4].try_into().unwrap()) as usize;
|
||||||
|
idx += 4;
|
||||||
|
if idx + alen > n {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
match atype {
|
||||||
|
0x0020 /* XOR-MAPPED-ADDRESS */ | 0x0001 /* MAPPED-ADDRESS */ => {
|
||||||
|
if alen < 8 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let family_byte = buf[idx + 1];
|
||||||
|
let port_bytes = [buf[idx + 2], buf[idx + 3]];
|
||||||
|
let len_check = match family_byte {
|
||||||
|
0x01 => 4,
|
||||||
|
0x02 => 16,
|
||||||
|
_ => 0,
|
||||||
|
};
|
||||||
|
if len_check == 0 || alen < 4 + len_check {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
let raw_ip = &buf[idx + 4..idx + 4 + len_check];
|
||||||
|
let mut port = u16::from_be_bytes(port_bytes);
|
||||||
|
|
||||||
|
let reflected_ip = if atype == 0x0020 {
|
||||||
|
port ^= ((magic[0] as u16) << 8) | magic[1] as u16;
|
||||||
|
match family_byte {
|
||||||
|
0x01 => {
|
||||||
|
let ip = [
|
||||||
|
raw_ip[0] ^ magic[0],
|
||||||
|
raw_ip[1] ^ magic[1],
|
||||||
|
raw_ip[2] ^ magic[2],
|
||||||
|
raw_ip[3] ^ magic[3],
|
||||||
|
];
|
||||||
|
IpAddr::V4(Ipv4Addr::new(ip[0], ip[1], ip[2], ip[3]))
|
||||||
|
}
|
||||||
|
0x02 => {
|
||||||
|
let mut ip = [0u8; 16];
|
||||||
|
let xor_key = [magic.as_slice(), txid].concat();
|
||||||
|
for (i, b) in raw_ip.iter().enumerate().take(16) {
|
||||||
|
ip[i] = *b ^ xor_key[i];
|
||||||
|
}
|
||||||
|
IpAddr::V6(Ipv6Addr::from(ip))
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
idx += (alen + 3) & !3;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
match family_byte {
|
||||||
|
0x01 => IpAddr::V4(Ipv4Addr::new(raw_ip[0], raw_ip[1], raw_ip[2], raw_ip[3])),
|
||||||
|
0x02 => IpAddr::V6(Ipv6Addr::from(<[u8; 16]>::try_from(raw_ip).unwrap())),
|
||||||
|
_ => {
|
||||||
|
idx += (alen + 3) & !3;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let reflected_addr = SocketAddr::new(reflected_ip, port);
|
||||||
|
let local_addr = socket
|
||||||
|
.local_addr()
|
||||||
|
.map_err(|e| ProxyError::Proxy(format!("STUN local_addr failed: {e}")))?;
|
||||||
|
|
||||||
|
return Ok(Some(StunProbeResult {
|
||||||
|
local_addr,
|
||||||
|
reflected_addr,
|
||||||
|
family,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
idx += (alen + 3) & !3;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn resolve_stun_addr(stun_addr: &str, family: IpFamily) -> Result<Option<SocketAddr>> {
|
||||||
|
if let Ok(addr) = stun_addr.parse::<SocketAddr>() {
|
||||||
|
return Ok(match (addr.is_ipv4(), family) {
|
||||||
|
(true, IpFamily::V4) | (false, IpFamily::V6) => Some(addr),
|
||||||
|
_ => None,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let addrs = lookup_host(stun_addr)
|
||||||
|
.await
|
||||||
|
.map_err(|e| ProxyError::Proxy(format!("STUN resolve failed: {e}")))?;
|
||||||
|
|
||||||
|
let target = addrs
|
||||||
|
.filter(|a| match (a.is_ipv4(), family) {
|
||||||
|
(true, IpFamily::V4) => true,
|
||||||
|
(false, IpFamily::V6) => true,
|
||||||
|
_ => false,
|
||||||
|
})
|
||||||
|
.next();
|
||||||
|
Ok(target)
|
||||||
|
}
|
||||||
@@ -351,6 +351,9 @@ pub fn build_server_hello(
|
|||||||
fake_cert_len: usize,
|
fake_cert_len: usize,
|
||||||
rng: &SecureRandom,
|
rng: &SecureRandom,
|
||||||
) -> Vec<u8> {
|
) -> Vec<u8> {
|
||||||
|
const MIN_APP_DATA: usize = 64;
|
||||||
|
const MAX_APP_DATA: usize = 16640; // RFC 8446 §5.2 upper bound
|
||||||
|
let fake_cert_len = fake_cert_len.max(MIN_APP_DATA).min(MAX_APP_DATA);
|
||||||
let x25519_key = gen_fake_x25519_key(rng);
|
let x25519_key = gen_fake_x25519_key(rng);
|
||||||
|
|
||||||
// Build ServerHello
|
// Build ServerHello
|
||||||
@@ -373,7 +376,13 @@ pub fn build_server_hello(
|
|||||||
app_data_record.push(TLS_RECORD_APPLICATION);
|
app_data_record.push(TLS_RECORD_APPLICATION);
|
||||||
app_data_record.extend_from_slice(&TLS_VERSION);
|
app_data_record.extend_from_slice(&TLS_VERSION);
|
||||||
app_data_record.extend_from_slice(&(fake_cert_len as u16).to_be_bytes());
|
app_data_record.extend_from_slice(&(fake_cert_len as u16).to_be_bytes());
|
||||||
app_data_record.extend_from_slice(&fake_cert);
|
if fake_cert_len > 17 {
|
||||||
|
app_data_record.extend_from_slice(&fake_cert[..fake_cert_len - 17]);
|
||||||
|
app_data_record.push(0x16); // inner content type marker
|
||||||
|
app_data_record.extend_from_slice(&rng.bytes(16)); // AEAD-like tag mimic
|
||||||
|
} else {
|
||||||
|
app_data_record.extend_from_slice(&fake_cert);
|
||||||
|
}
|
||||||
|
|
||||||
// Combine all records
|
// Combine all records
|
||||||
let mut response = Vec::with_capacity(
|
let mut response = Vec::with_capacity(
|
||||||
@@ -397,6 +406,163 @@ pub fn build_server_hello(
|
|||||||
response
|
response
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Extract SNI (server_name) from a TLS ClientHello.
|
||||||
|
pub fn extract_sni_from_client_hello(handshake: &[u8]) -> Option<String> {
|
||||||
|
if handshake.len() < 43 || handshake[0] != TLS_RECORD_HANDSHAKE {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut pos = 5; // after record header
|
||||||
|
if handshake.get(pos).copied()? != 0x01 {
|
||||||
|
return None; // not ClientHello
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handshake length bytes
|
||||||
|
pos += 4; // type + len (3)
|
||||||
|
|
||||||
|
// version (2) + random (32)
|
||||||
|
pos += 2 + 32;
|
||||||
|
if pos + 1 > handshake.len() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let session_id_len = *handshake.get(pos)? as usize;
|
||||||
|
pos += 1 + session_id_len;
|
||||||
|
if pos + 2 > handshake.len() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let cipher_suites_len = u16::from_be_bytes([handshake[pos], handshake[pos + 1]]) as usize;
|
||||||
|
pos += 2 + cipher_suites_len;
|
||||||
|
if pos + 1 > handshake.len() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let comp_len = *handshake.get(pos)? as usize;
|
||||||
|
pos += 1 + comp_len;
|
||||||
|
if pos + 2 > handshake.len() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let ext_len = u16::from_be_bytes([handshake[pos], handshake[pos + 1]]) as usize;
|
||||||
|
pos += 2;
|
||||||
|
let ext_end = pos + ext_len;
|
||||||
|
if ext_end > handshake.len() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
while pos + 4 <= ext_end {
|
||||||
|
let etype = u16::from_be_bytes([handshake[pos], handshake[pos + 1]]);
|
||||||
|
let elen = u16::from_be_bytes([handshake[pos + 2], handshake[pos + 3]]) as usize;
|
||||||
|
pos += 4;
|
||||||
|
if pos + elen > ext_end {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if etype == 0x0000 && elen >= 5 {
|
||||||
|
// server_name extension
|
||||||
|
let list_len = u16::from_be_bytes([handshake[pos], handshake[pos + 1]]) as usize;
|
||||||
|
let mut sn_pos = pos + 2;
|
||||||
|
let sn_end = std::cmp::min(sn_pos + list_len, pos + elen);
|
||||||
|
while sn_pos + 3 <= sn_end {
|
||||||
|
let name_type = handshake[sn_pos];
|
||||||
|
let name_len = u16::from_be_bytes([handshake[sn_pos + 1], handshake[sn_pos + 2]]) as usize;
|
||||||
|
sn_pos += 3;
|
||||||
|
if sn_pos + name_len > sn_end {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if name_type == 0 && name_len > 0 {
|
||||||
|
if let Ok(host) = std::str::from_utf8(&handshake[sn_pos..sn_pos + name_len]) {
|
||||||
|
return Some(host.to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sn_pos += name_len;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pos += elen;
|
||||||
|
}
|
||||||
|
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Extract ALPN protocol list from TLS ClientHello.
|
||||||
|
pub fn extract_alpn_from_client_hello(handshake: &[u8]) -> Option<Vec<String>> {
|
||||||
|
if handshake.len() < 43 || handshake[0] != TLS_RECORD_HANDSHAKE {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut pos = 5; // after record header
|
||||||
|
if handshake.get(pos).copied()? != 0x01 {
|
||||||
|
return None; // not ClientHello
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handshake length bytes
|
||||||
|
pos += 4; // type + len (3)
|
||||||
|
|
||||||
|
// version (2) + random (32)
|
||||||
|
pos += 2 + 32;
|
||||||
|
if pos + 1 > handshake.len() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let session_id_len = *handshake.get(pos)? as usize;
|
||||||
|
pos += 1 + session_id_len;
|
||||||
|
if pos + 2 > handshake.len() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let cipher_suites_len = u16::from_be_bytes([handshake[pos], handshake[pos + 1]]) as usize;
|
||||||
|
pos += 2 + cipher_suites_len;
|
||||||
|
if pos + 1 > handshake.len() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let comp_len = *handshake.get(pos)? as usize;
|
||||||
|
pos += 1 + comp_len;
|
||||||
|
if pos + 2 > handshake.len() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let ext_len = u16::from_be_bytes([handshake[pos], handshake[pos + 1]]) as usize;
|
||||||
|
pos += 2;
|
||||||
|
let ext_end = pos + ext_len;
|
||||||
|
if ext_end > handshake.len() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
while pos + 4 <= ext_end {
|
||||||
|
let etype = u16::from_be_bytes([handshake[pos], handshake[pos + 1]]);
|
||||||
|
let elen = u16::from_be_bytes([handshake[pos + 2], handshake[pos + 3]]) as usize;
|
||||||
|
pos += 4;
|
||||||
|
if pos + elen > ext_end {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if etype == 0x0010 && elen >= 3 {
|
||||||
|
// ALPN
|
||||||
|
let list_len = u16::from_be_bytes([handshake[pos], handshake[pos + 1]]) as usize;
|
||||||
|
let mut alpn_pos = pos + 2;
|
||||||
|
let list_end = std::cmp::min(alpn_pos + list_len, pos + elen);
|
||||||
|
let mut protocols = Vec::new();
|
||||||
|
while alpn_pos < list_end {
|
||||||
|
let proto_len = *handshake.get(alpn_pos)? as usize;
|
||||||
|
alpn_pos += 1;
|
||||||
|
if alpn_pos + proto_len > list_end {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if let Ok(p) = std::str::from_utf8(&handshake[alpn_pos..alpn_pos + proto_len]) {
|
||||||
|
protocols.push(p.to_string());
|
||||||
|
}
|
||||||
|
alpn_pos += proto_len;
|
||||||
|
}
|
||||||
|
return Some(protocols);
|
||||||
|
}
|
||||||
|
|
||||||
|
pos += elen;
|
||||||
|
}
|
||||||
|
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
/// Check if bytes look like a TLS ClientHello
|
/// Check if bytes look like a TLS ClientHello
|
||||||
pub fn is_tls_handshake(first_bytes: &[u8]) -> bool {
|
pub fn is_tls_handshake(first_bytes: &[u8]) -> bool {
|
||||||
if first_bytes.len() < 3 {
|
if first_bytes.len() < 3 {
|
||||||
@@ -668,4 +834,93 @@ mod tests {
|
|||||||
// Should return None (no match) but not panic
|
// Should return None (no match) but not panic
|
||||||
assert!(result.is_none());
|
assert!(result.is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn build_client_hello_with_exts(exts: Vec<(u16, Vec<u8>)>, host: &str) -> Vec<u8> {
|
||||||
|
let mut body = Vec::new();
|
||||||
|
body.extend_from_slice(&TLS_VERSION); // legacy version
|
||||||
|
body.extend_from_slice(&[0u8; 32]); // random
|
||||||
|
body.push(0); // session id len
|
||||||
|
body.extend_from_slice(&2u16.to_be_bytes()); // cipher suites len
|
||||||
|
body.extend_from_slice(&[0x13, 0x01]); // TLS_AES_128_GCM_SHA256
|
||||||
|
body.push(1); // compression len
|
||||||
|
body.push(0); // null compression
|
||||||
|
|
||||||
|
// Build SNI extension
|
||||||
|
let host_bytes = host.as_bytes();
|
||||||
|
let mut sni_ext = Vec::new();
|
||||||
|
sni_ext.extend_from_slice(&(host_bytes.len() as u16 + 3).to_be_bytes());
|
||||||
|
sni_ext.push(0);
|
||||||
|
sni_ext.extend_from_slice(&(host_bytes.len() as u16).to_be_bytes());
|
||||||
|
sni_ext.extend_from_slice(host_bytes);
|
||||||
|
|
||||||
|
let mut ext_blob = Vec::new();
|
||||||
|
for (typ, data) in exts {
|
||||||
|
ext_blob.extend_from_slice(&typ.to_be_bytes());
|
||||||
|
ext_blob.extend_from_slice(&(data.len() as u16).to_be_bytes());
|
||||||
|
ext_blob.extend_from_slice(&data);
|
||||||
|
}
|
||||||
|
// SNI last
|
||||||
|
ext_blob.extend_from_slice(&0x0000u16.to_be_bytes());
|
||||||
|
ext_blob.extend_from_slice(&(sni_ext.len() as u16).to_be_bytes());
|
||||||
|
ext_blob.extend_from_slice(&sni_ext);
|
||||||
|
|
||||||
|
body.extend_from_slice(&(ext_blob.len() as u16).to_be_bytes());
|
||||||
|
body.extend_from_slice(&ext_blob);
|
||||||
|
|
||||||
|
let mut handshake = Vec::new();
|
||||||
|
handshake.push(0x01); // ClientHello
|
||||||
|
let len_bytes = (body.len() as u32).to_be_bytes();
|
||||||
|
handshake.extend_from_slice(&len_bytes[1..4]);
|
||||||
|
handshake.extend_from_slice(&body);
|
||||||
|
|
||||||
|
let mut record = Vec::new();
|
||||||
|
record.push(TLS_RECORD_HANDSHAKE);
|
||||||
|
record.extend_from_slice(&[0x03, 0x01]);
|
||||||
|
record.extend_from_slice(&(handshake.len() as u16).to_be_bytes());
|
||||||
|
record.extend_from_slice(&handshake);
|
||||||
|
record
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_extract_sni_with_grease_extension() {
|
||||||
|
// GREASE type 0x0a0a with zero length before SNI
|
||||||
|
let ch = build_client_hello_with_exts(vec![(0x0a0a, Vec::new())], "example.com");
|
||||||
|
let sni = extract_sni_from_client_hello(&ch);
|
||||||
|
assert_eq!(sni.as_deref(), Some("example.com"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_extract_sni_tolerates_empty_unknown_extension() {
|
||||||
|
let ch = build_client_hello_with_exts(vec![(0x1234, Vec::new())], "test.local");
|
||||||
|
let sni = extract_sni_from_client_hello(&ch);
|
||||||
|
assert_eq!(sni.as_deref(), Some("test.local"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_extract_alpn_single() {
|
||||||
|
let mut alpn_data = Vec::new();
|
||||||
|
// list length = 3 (1 length byte + "h2")
|
||||||
|
alpn_data.extend_from_slice(&3u16.to_be_bytes());
|
||||||
|
alpn_data.push(2);
|
||||||
|
alpn_data.extend_from_slice(b"h2");
|
||||||
|
let ch = build_client_hello_with_exts(vec![(0x0010, alpn_data)], "alpn.test");
|
||||||
|
let alpn = extract_alpn_from_client_hello(&ch).unwrap();
|
||||||
|
assert_eq!(alpn, vec!["h2"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_extract_alpn_multiple() {
|
||||||
|
let mut alpn_data = Vec::new();
|
||||||
|
// list length = 11 (sum of per-proto lengths including length bytes)
|
||||||
|
alpn_data.extend_from_slice(&11u16.to_be_bytes());
|
||||||
|
alpn_data.push(2);
|
||||||
|
alpn_data.extend_from_slice(b"h2");
|
||||||
|
alpn_data.push(4);
|
||||||
|
alpn_data.extend_from_slice(b"spdy");
|
||||||
|
alpn_data.push(2);
|
||||||
|
alpn_data.extend_from_slice(b"h3");
|
||||||
|
let ch = build_client_hello_with_exts(vec![(0x0010, alpn_data)], "alpn.test");
|
||||||
|
let alpn = extract_alpn_from_client_hello(&ch).unwrap();
|
||||||
|
assert_eq!(alpn, vec!["h2", "spdy", "h3"]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,7 +30,9 @@ use crate::protocol::tls;
|
|||||||
use crate::stats::{ReplayChecker, Stats};
|
use crate::stats::{ReplayChecker, Stats};
|
||||||
use crate::stream::{BufferPool, CryptoReader, CryptoWriter};
|
use crate::stream::{BufferPool, CryptoReader, CryptoWriter};
|
||||||
use crate::transport::middle_proxy::MePool;
|
use crate::transport::middle_proxy::MePool;
|
||||||
use crate::transport::{UpstreamManager, configure_client_socket};
|
use crate::transport::{UpstreamManager, configure_client_socket, parse_proxy_protocol};
|
||||||
|
use crate::transport::socket::normalize_ip;
|
||||||
|
use crate::tls_front::TlsFrontCache;
|
||||||
|
|
||||||
use crate::proxy::direct_relay::handle_via_direct;
|
use crate::proxy::direct_relay::handle_via_direct;
|
||||||
use crate::proxy::handshake::{HandshakeSuccess, handle_mtproto_handshake, handle_tls_handshake};
|
use crate::proxy::handshake::{HandshakeSuccess, handle_mtproto_handshake, handle_tls_handshake};
|
||||||
@@ -47,13 +49,36 @@ pub async fn handle_client_stream<S>(
|
|||||||
buffer_pool: Arc<BufferPool>,
|
buffer_pool: Arc<BufferPool>,
|
||||||
rng: Arc<SecureRandom>,
|
rng: Arc<SecureRandom>,
|
||||||
me_pool: Option<Arc<MePool>>,
|
me_pool: Option<Arc<MePool>>,
|
||||||
|
tls_cache: Option<Arc<TlsFrontCache>>,
|
||||||
ip_tracker: Arc<UserIpTracker>,
|
ip_tracker: Arc<UserIpTracker>,
|
||||||
|
proxy_protocol_enabled: bool,
|
||||||
) -> Result<()>
|
) -> Result<()>
|
||||||
where
|
where
|
||||||
S: AsyncRead + AsyncWrite + Unpin + Send + 'static,
|
S: AsyncRead + AsyncWrite + Unpin + Send + 'static,
|
||||||
{
|
{
|
||||||
stats.increment_connects_all();
|
stats.increment_connects_all();
|
||||||
debug!(peer = %peer, "New connection (generic stream)");
|
let mut real_peer = normalize_ip(peer);
|
||||||
|
|
||||||
|
if proxy_protocol_enabled {
|
||||||
|
match parse_proxy_protocol(&mut stream, peer).await {
|
||||||
|
Ok(info) => {
|
||||||
|
debug!(
|
||||||
|
peer = %peer,
|
||||||
|
client = %info.src_addr,
|
||||||
|
version = info.version,
|
||||||
|
"PROXY protocol header parsed"
|
||||||
|
);
|
||||||
|
real_peer = normalize_ip(info.src_addr);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
stats.increment_connects_bad();
|
||||||
|
warn!(peer = %peer, error = %e, "Invalid PROXY protocol header");
|
||||||
|
return Err(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
debug!(peer = %real_peer, "New connection (generic stream)");
|
||||||
|
|
||||||
let handshake_timeout = Duration::from_secs(config.timeouts.client_handshake);
|
let handshake_timeout = Duration::from_secs(config.timeouts.client_handshake);
|
||||||
let stats_for_timeout = stats.clone();
|
let stats_for_timeout = stats.clone();
|
||||||
@@ -69,13 +94,13 @@ where
|
|||||||
stream.read_exact(&mut first_bytes).await?;
|
stream.read_exact(&mut first_bytes).await?;
|
||||||
|
|
||||||
let is_tls = tls::is_tls_handshake(&first_bytes[..3]);
|
let is_tls = tls::is_tls_handshake(&first_bytes[..3]);
|
||||||
debug!(peer = %peer, is_tls = is_tls, "Handshake type detected");
|
debug!(peer = %real_peer, is_tls = is_tls, "Handshake type detected");
|
||||||
|
|
||||||
if is_tls {
|
if is_tls {
|
||||||
let tls_len = u16::from_be_bytes([first_bytes[3], first_bytes[4]]) as usize;
|
let tls_len = u16::from_be_bytes([first_bytes[3], first_bytes[4]]) as usize;
|
||||||
|
|
||||||
if tls_len < 512 {
|
if tls_len < 512 {
|
||||||
debug!(peer = %peer, tls_len = tls_len, "TLS handshake too short");
|
debug!(peer = %real_peer, tls_len = tls_len, "TLS handshake too short");
|
||||||
stats.increment_connects_bad();
|
stats.increment_connects_bad();
|
||||||
let (reader, writer) = tokio::io::split(stream);
|
let (reader, writer) = tokio::io::split(stream);
|
||||||
handle_bad_client(reader, writer, &first_bytes, &config).await;
|
handle_bad_client(reader, writer, &first_bytes, &config).await;
|
||||||
@@ -89,8 +114,8 @@ where
|
|||||||
let (read_half, write_half) = tokio::io::split(stream);
|
let (read_half, write_half) = tokio::io::split(stream);
|
||||||
|
|
||||||
let (mut tls_reader, tls_writer, _tls_user) = match handle_tls_handshake(
|
let (mut tls_reader, tls_writer, _tls_user) = match handle_tls_handshake(
|
||||||
&handshake, read_half, write_half, peer,
|
&handshake, read_half, write_half, real_peer,
|
||||||
&config, &replay_checker, &rng,
|
&config, &replay_checker, &rng, tls_cache.clone(),
|
||||||
).await {
|
).await {
|
||||||
HandshakeResult::Success(result) => result,
|
HandshakeResult::Success(result) => result,
|
||||||
HandshakeResult::BadClient { reader, writer } => {
|
HandshakeResult::BadClient { reader, writer } => {
|
||||||
@@ -107,7 +132,7 @@ where
|
|||||||
.map_err(|_| ProxyError::InvalidHandshake("Short MTProto handshake".into()))?;
|
.map_err(|_| ProxyError::InvalidHandshake("Short MTProto handshake".into()))?;
|
||||||
|
|
||||||
let (crypto_reader, crypto_writer, success) = match handle_mtproto_handshake(
|
let (crypto_reader, crypto_writer, success) = match handle_mtproto_handshake(
|
||||||
&mtproto_handshake, tls_reader, tls_writer, peer,
|
&mtproto_handshake, tls_reader, tls_writer, real_peer,
|
||||||
&config, &replay_checker, true,
|
&config, &replay_checker, true,
|
||||||
).await {
|
).await {
|
||||||
HandshakeResult::Success(result) => result,
|
HandshakeResult::Success(result) => result,
|
||||||
@@ -123,12 +148,12 @@ where
|
|||||||
RunningClientHandler::handle_authenticated_static(
|
RunningClientHandler::handle_authenticated_static(
|
||||||
crypto_reader, crypto_writer, success,
|
crypto_reader, crypto_writer, success,
|
||||||
upstream_manager, stats, config, buffer_pool, rng, me_pool,
|
upstream_manager, stats, config, buffer_pool, rng, me_pool,
|
||||||
local_addr, peer, ip_tracker.clone(),
|
local_addr, real_peer, ip_tracker.clone(),
|
||||||
),
|
),
|
||||||
)))
|
)))
|
||||||
} else {
|
} else {
|
||||||
if !config.general.modes.classic && !config.general.modes.secure {
|
if !config.general.modes.classic && !config.general.modes.secure {
|
||||||
debug!(peer = %peer, "Non-TLS modes disabled");
|
debug!(peer = %real_peer, "Non-TLS modes disabled");
|
||||||
stats.increment_connects_bad();
|
stats.increment_connects_bad();
|
||||||
let (reader, writer) = tokio::io::split(stream);
|
let (reader, writer) = tokio::io::split(stream);
|
||||||
handle_bad_client(reader, writer, &first_bytes, &config).await;
|
handle_bad_client(reader, writer, &first_bytes, &config).await;
|
||||||
@@ -142,7 +167,7 @@ where
|
|||||||
let (read_half, write_half) = tokio::io::split(stream);
|
let (read_half, write_half) = tokio::io::split(stream);
|
||||||
|
|
||||||
let (crypto_reader, crypto_writer, success) = match handle_mtproto_handshake(
|
let (crypto_reader, crypto_writer, success) = match handle_mtproto_handshake(
|
||||||
&handshake, read_half, write_half, peer,
|
&handshake, read_half, write_half, real_peer,
|
||||||
&config, &replay_checker, false,
|
&config, &replay_checker, false,
|
||||||
).await {
|
).await {
|
||||||
HandshakeResult::Success(result) => result,
|
HandshakeResult::Success(result) => result,
|
||||||
@@ -166,7 +191,7 @@ where
|
|||||||
rng,
|
rng,
|
||||||
me_pool,
|
me_pool,
|
||||||
local_addr,
|
local_addr,
|
||||||
peer,
|
real_peer,
|
||||||
ip_tracker.clone(),
|
ip_tracker.clone(),
|
||||||
)
|
)
|
||||||
)))
|
)))
|
||||||
@@ -203,7 +228,9 @@ pub struct RunningClientHandler {
|
|||||||
buffer_pool: Arc<BufferPool>,
|
buffer_pool: Arc<BufferPool>,
|
||||||
rng: Arc<SecureRandom>,
|
rng: Arc<SecureRandom>,
|
||||||
me_pool: Option<Arc<MePool>>,
|
me_pool: Option<Arc<MePool>>,
|
||||||
|
tls_cache: Option<Arc<TlsFrontCache>>,
|
||||||
ip_tracker: Arc<UserIpTracker>,
|
ip_tracker: Arc<UserIpTracker>,
|
||||||
|
proxy_protocol_enabled: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ClientHandler {
|
impl ClientHandler {
|
||||||
@@ -217,7 +244,9 @@ impl ClientHandler {
|
|||||||
buffer_pool: Arc<BufferPool>,
|
buffer_pool: Arc<BufferPool>,
|
||||||
rng: Arc<SecureRandom>,
|
rng: Arc<SecureRandom>,
|
||||||
me_pool: Option<Arc<MePool>>,
|
me_pool: Option<Arc<MePool>>,
|
||||||
|
tls_cache: Option<Arc<TlsFrontCache>>,
|
||||||
ip_tracker: Arc<UserIpTracker>,
|
ip_tracker: Arc<UserIpTracker>,
|
||||||
|
proxy_protocol_enabled: bool,
|
||||||
) -> RunningClientHandler {
|
) -> RunningClientHandler {
|
||||||
RunningClientHandler {
|
RunningClientHandler {
|
||||||
stream,
|
stream,
|
||||||
@@ -229,7 +258,9 @@ impl ClientHandler {
|
|||||||
buffer_pool,
|
buffer_pool,
|
||||||
rng,
|
rng,
|
||||||
me_pool,
|
me_pool,
|
||||||
|
tls_cache,
|
||||||
ip_tracker,
|
ip_tracker,
|
||||||
|
proxy_protocol_enabled,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -238,6 +269,7 @@ impl RunningClientHandler {
|
|||||||
pub async fn run(mut self) -> Result<()> {
|
pub async fn run(mut self) -> Result<()> {
|
||||||
self.stats.increment_connects_all();
|
self.stats.increment_connects_all();
|
||||||
|
|
||||||
|
self.peer = normalize_ip(self.peer);
|
||||||
let peer = self.peer;
|
let peer = self.peer;
|
||||||
let ip_tracker = self.ip_tracker.clone();
|
let ip_tracker = self.ip_tracker.clone();
|
||||||
debug!(peer = %peer, "New connection");
|
debug!(peer = %peer, "New connection");
|
||||||
@@ -275,6 +307,25 @@ impl RunningClientHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn do_handshake(mut self) -> Result<HandshakeOutcome> {
|
async fn do_handshake(mut self) -> Result<HandshakeOutcome> {
|
||||||
|
if self.proxy_protocol_enabled {
|
||||||
|
match parse_proxy_protocol(&mut self.stream, self.peer).await {
|
||||||
|
Ok(info) => {
|
||||||
|
debug!(
|
||||||
|
peer = %self.peer,
|
||||||
|
client = %info.src_addr,
|
||||||
|
version = info.version,
|
||||||
|
"PROXY protocol header parsed"
|
||||||
|
);
|
||||||
|
self.peer = normalize_ip(info.src_addr);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
self.stats.increment_connects_bad();
|
||||||
|
warn!(peer = %self.peer, error = %e, "Invalid PROXY protocol header");
|
||||||
|
return Err(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let mut first_bytes = [0u8; 5];
|
let mut first_bytes = [0u8; 5];
|
||||||
self.stream.read_exact(&mut first_bytes).await?;
|
self.stream.read_exact(&mut first_bytes).await?;
|
||||||
|
|
||||||
@@ -327,6 +378,7 @@ impl RunningClientHandler {
|
|||||||
&config,
|
&config,
|
||||||
&replay_checker,
|
&replay_checker,
|
||||||
&self.rng,
|
&self.rng,
|
||||||
|
self.tls_cache.clone(),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ where
|
|||||||
);
|
);
|
||||||
|
|
||||||
let tg_stream = upstream_manager
|
let tg_stream = upstream_manager
|
||||||
.connect(dc_addr, Some(success.dc_idx))
|
.connect(dc_addr, Some(success.dc_idx), user.strip_prefix("scope_").filter(|s| !s.is_empty()))
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
debug!(peer = %success.peer, dc_addr = %dc_addr, "Connected, performing TG handshake");
|
debug!(peer = %success.peer, dc_addr = %dc_addr, "Connected, performing TG handshake");
|
||||||
@@ -80,7 +80,8 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn get_dc_addr_static(dc_idx: i16, config: &ProxyConfig) -> Result<SocketAddr> {
|
fn get_dc_addr_static(dc_idx: i16, config: &ProxyConfig) -> Result<SocketAddr> {
|
||||||
let datacenters = if config.general.prefer_ipv6 {
|
let prefer_v6 = config.network.prefer == 6 && config.network.ipv6.unwrap_or(true);
|
||||||
|
let datacenters = if prefer_v6 {
|
||||||
&*TG_DATACENTERS_V6
|
&*TG_DATACENTERS_V6
|
||||||
} else {
|
} else {
|
||||||
&*TG_DATACENTERS_V4
|
&*TG_DATACENTERS_V4
|
||||||
@@ -90,7 +91,6 @@ fn get_dc_addr_static(dc_idx: i16, config: &ProxyConfig) -> Result<SocketAddr> {
|
|||||||
|
|
||||||
let dc_key = dc_idx.to_string();
|
let dc_key = dc_idx.to_string();
|
||||||
if let Some(addrs) = config.dc_overrides.get(&dc_key) {
|
if let Some(addrs) = config.dc_overrides.get(&dc_key) {
|
||||||
let prefer_v6 = config.general.prefer_ipv6;
|
|
||||||
let mut parsed = Vec::new();
|
let mut parsed = Vec::new();
|
||||||
for addr_str in addrs {
|
for addr_str in addrs {
|
||||||
match addr_str.parse::<SocketAddr>() {
|
match addr_str.parse::<SocketAddr>() {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
//! MTProto Handshake
|
//! MTProto Handshake
|
||||||
|
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
|
use std::sync::Arc;
|
||||||
use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt};
|
use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt};
|
||||||
use tracing::{debug, warn, trace, info};
|
use tracing::{debug, warn, trace, info};
|
||||||
use zeroize::Zeroize;
|
use zeroize::Zeroize;
|
||||||
@@ -12,6 +13,7 @@ use crate::stream::{FakeTlsReader, FakeTlsWriter, CryptoReader, CryptoWriter};
|
|||||||
use crate::error::{ProxyError, HandshakeResult};
|
use crate::error::{ProxyError, HandshakeResult};
|
||||||
use crate::stats::ReplayChecker;
|
use crate::stats::ReplayChecker;
|
||||||
use crate::config::ProxyConfig;
|
use crate::config::ProxyConfig;
|
||||||
|
use crate::tls_front::{TlsFrontCache, emulator};
|
||||||
|
|
||||||
/// Result of successful handshake
|
/// Result of successful handshake
|
||||||
///
|
///
|
||||||
@@ -55,6 +57,7 @@ pub async fn handle_tls_handshake<R, W>(
|
|||||||
config: &ProxyConfig,
|
config: &ProxyConfig,
|
||||||
replay_checker: &ReplayChecker,
|
replay_checker: &ReplayChecker,
|
||||||
rng: &SecureRandom,
|
rng: &SecureRandom,
|
||||||
|
tls_cache: Option<Arc<TlsFrontCache>>,
|
||||||
) -> HandshakeResult<(FakeTlsReader<R>, FakeTlsWriter<W>, String), R, W>
|
) -> HandshakeResult<(FakeTlsReader<R>, FakeTlsWriter<W>, String), R, W>
|
||||||
where
|
where
|
||||||
R: AsyncRead + Unpin,
|
R: AsyncRead + Unpin,
|
||||||
@@ -102,13 +105,37 @@ where
|
|||||||
None => return HandshakeResult::BadClient { reader, writer },
|
None => return HandshakeResult::BadClient { reader, writer },
|
||||||
};
|
};
|
||||||
|
|
||||||
let response = tls::build_server_hello(
|
let cached = if config.censorship.tls_emulation {
|
||||||
secret,
|
if let Some(cache) = tls_cache.as_ref() {
|
||||||
&validation.digest,
|
if let Some(sni) = tls::extract_sni_from_client_hello(handshake) {
|
||||||
&validation.session_id,
|
Some(cache.get(&sni).await)
|
||||||
config.censorship.fake_cert_len,
|
} else {
|
||||||
rng,
|
Some(cache.get(&config.censorship.tls_domain).await)
|
||||||
);
|
}
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
let response = if let Some(cached_entry) = cached {
|
||||||
|
emulator::build_emulated_server_hello(
|
||||||
|
secret,
|
||||||
|
&validation.digest,
|
||||||
|
&validation.session_id,
|
||||||
|
&cached_entry,
|
||||||
|
rng,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
tls::build_server_hello(
|
||||||
|
secret,
|
||||||
|
&validation.digest,
|
||||||
|
&validation.session_id,
|
||||||
|
config.censorship.fake_cert_len,
|
||||||
|
rng,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
debug!(peer = %peer, response_len = response.len(), "Sending TLS ServerHello");
|
debug!(peer = %peer, response_len = response.len(), "Sending TLS ServerHello");
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//! Masking - forward unrecognized traffic to mask host
|
//! Masking - forward unrecognized traffic to mask host
|
||||||
|
|
||||||
use std::time::Duration;
|
|
||||||
use std::str;
|
use std::str;
|
||||||
|
use std::time::Duration;
|
||||||
use tokio::net::TcpStream;
|
use tokio::net::TcpStream;
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
use tokio::net::UnixStream;
|
use tokio::net::UnixStream;
|
||||||
@@ -11,9 +11,9 @@ use tracing::debug;
|
|||||||
use crate::config::ProxyConfig;
|
use crate::config::ProxyConfig;
|
||||||
|
|
||||||
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.
|
||||||
/// Limits resource consumption from slow-loris attacks and port scanners.
|
/// Limits resource consumption from slow-loris attacks and port scanners.
|
||||||
const MASK_RELAY_TIMEOUT: Duration = Duration::from_secs(60);
|
const MASK_RELAY_TIMEOUT: Duration = Duration::from_secs(60);
|
||||||
const MASK_BUFFER_SIZE: usize = 8192;
|
const MASK_BUFFER_SIZE: usize = 8192;
|
||||||
|
|
||||||
/// Detect client type based on initial data
|
/// Detect client type based on initial data
|
||||||
@@ -78,7 +78,9 @@ where
|
|||||||
match connect_result {
|
match connect_result {
|
||||||
Ok(Ok(stream)) => {
|
Ok(Ok(stream)) => {
|
||||||
let (mask_read, mask_write) = stream.into_split();
|
let (mask_read, mask_write) = stream.into_split();
|
||||||
relay_to_mask(reader, writer, mask_read, mask_write, initial_data).await;
|
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)");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Ok(Err(e)) => {
|
Ok(Err(e)) => {
|
||||||
debug!(error = %e, "Failed to connect to mask unix socket");
|
debug!(error = %e, "Failed to connect to mask unix socket");
|
||||||
@@ -110,7 +112,9 @@ where
|
|||||||
match connect_result {
|
match connect_result {
|
||||||
Ok(Ok(stream)) => {
|
Ok(Ok(stream)) => {
|
||||||
let (mask_read, mask_write) = stream.into_split();
|
let (mask_read, mask_write) = stream.into_split();
|
||||||
relay_to_mask(reader, writer, mask_read, mask_write, initial_data).await;
|
if timeout(MASK_RELAY_TIMEOUT, relay_to_mask(reader, writer, mask_read, mask_write, initial_data)).await.is_err() {
|
||||||
|
debug!("Mask relay timed out");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Ok(Err(e)) => {
|
Ok(Err(e)) => {
|
||||||
debug!(error = %e, "Failed to connect to mask host");
|
debug!(error = %e, "Failed to connect to mask host");
|
||||||
|
|||||||
@@ -19,6 +19,10 @@ pub struct Stats {
|
|||||||
connects_all: AtomicU64,
|
connects_all: AtomicU64,
|
||||||
connects_bad: AtomicU64,
|
connects_bad: AtomicU64,
|
||||||
handshake_timeouts: AtomicU64,
|
handshake_timeouts: AtomicU64,
|
||||||
|
me_keepalive_sent: AtomicU64,
|
||||||
|
me_keepalive_failed: AtomicU64,
|
||||||
|
me_reconnect_attempts: AtomicU64,
|
||||||
|
me_reconnect_success: AtomicU64,
|
||||||
user_stats: DashMap<String, UserStats>,
|
user_stats: DashMap<String, UserStats>,
|
||||||
start_time: parking_lot::RwLock<Option<Instant>>,
|
start_time: parking_lot::RwLock<Option<Instant>>,
|
||||||
}
|
}
|
||||||
@@ -43,8 +47,16 @@ impl Stats {
|
|||||||
pub fn increment_connects_all(&self) { self.connects_all.fetch_add(1, Ordering::Relaxed); }
|
pub fn increment_connects_all(&self) { self.connects_all.fetch_add(1, Ordering::Relaxed); }
|
||||||
pub fn increment_connects_bad(&self) { self.connects_bad.fetch_add(1, Ordering::Relaxed); }
|
pub fn increment_connects_bad(&self) { self.connects_bad.fetch_add(1, Ordering::Relaxed); }
|
||||||
pub fn increment_handshake_timeouts(&self) { self.handshake_timeouts.fetch_add(1, 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); }
|
||||||
|
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 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) }
|
||||||
|
pub fn get_me_keepalive_sent(&self) -> u64 { self.me_keepalive_sent.load(Ordering::Relaxed) }
|
||||||
|
pub fn get_me_keepalive_failed(&self) -> u64 { self.me_keepalive_failed.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 increment_user_connects(&self, user: &str) {
|
pub fn increment_user_connects(&self, user: &str) {
|
||||||
self.user_stats.entry(user.to_string()).or_default()
|
self.user_stats.entry(user.to_string()).or_default()
|
||||||
|
|||||||
143
src/tls_front/cache.rs
Normal file
143
src/tls_front/cache.rs
Normal file
@@ -0,0 +1,143 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::time::{SystemTime, Duration};
|
||||||
|
|
||||||
|
use tokio::sync::RwLock;
|
||||||
|
use tokio::time::sleep;
|
||||||
|
use tracing::{debug, warn, info};
|
||||||
|
|
||||||
|
use crate::tls_front::types::{CachedTlsData, ParsedServerHello, TlsFetchResult};
|
||||||
|
|
||||||
|
/// Lightweight in-memory + optional on-disk cache for TLS fronting data.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct TlsFrontCache {
|
||||||
|
memory: RwLock<HashMap<String, Arc<CachedTlsData>>>,
|
||||||
|
default: Arc<CachedTlsData>,
|
||||||
|
disk_path: PathBuf,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TlsFrontCache {
|
||||||
|
pub fn new(domains: &[String], default_len: usize, disk_path: impl AsRef<Path>) -> Self {
|
||||||
|
let default_template = ParsedServerHello {
|
||||||
|
version: [0x03, 0x03],
|
||||||
|
random: [0u8; 32],
|
||||||
|
session_id: Vec::new(),
|
||||||
|
cipher_suite: [0x13, 0x01],
|
||||||
|
compression: 0,
|
||||||
|
extensions: Vec::new(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let default = Arc::new(CachedTlsData {
|
||||||
|
server_hello_template: default_template,
|
||||||
|
cert_info: None,
|
||||||
|
app_data_records_sizes: vec![default_len],
|
||||||
|
total_app_data_len: default_len,
|
||||||
|
fetched_at: SystemTime::now(),
|
||||||
|
domain: "default".to_string(),
|
||||||
|
});
|
||||||
|
|
||||||
|
let mut map = HashMap::new();
|
||||||
|
for d in domains {
|
||||||
|
map.insert(d.clone(), default.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
Self {
|
||||||
|
memory: RwLock::new(map),
|
||||||
|
default,
|
||||||
|
disk_path: disk_path.as_ref().to_path_buf(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get(&self, sni: &str) -> Arc<CachedTlsData> {
|
||||||
|
let guard = self.memory.read().await;
|
||||||
|
guard.get(sni).cloned().unwrap_or_else(|| self.default.clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn set(&self, domain: &str, data: CachedTlsData) {
|
||||||
|
let mut guard = self.memory.write().await;
|
||||||
|
guard.insert(domain.to_string(), Arc::new(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn load_from_disk(&self) {
|
||||||
|
let path = self.disk_path.clone();
|
||||||
|
if tokio::fs::create_dir_all(&path).await.is_err() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let mut loaded = 0usize;
|
||||||
|
if let Ok(mut dir) = tokio::fs::read_dir(&path).await {
|
||||||
|
while let Ok(Some(entry)) = dir.next_entry().await {
|
||||||
|
if let Ok(name) = entry.file_name().into_string() {
|
||||||
|
if !name.ends_with(".json") {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if let Ok(data) = tokio::fs::read(entry.path()).await {
|
||||||
|
if let Ok(cached) = serde_json::from_slice::<CachedTlsData>(&data) {
|
||||||
|
let domain = cached.domain.clone();
|
||||||
|
self.set(&domain, cached).await;
|
||||||
|
loaded += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if loaded > 0 {
|
||||||
|
info!(count = loaded, "Loaded TLS cache entries from disk");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn persist(&self, domain: &str, data: &CachedTlsData) {
|
||||||
|
if tokio::fs::create_dir_all(&self.disk_path).await.is_err() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let fname = format!("{}.json", domain.replace(['/', '\\'], "_"));
|
||||||
|
let path = self.disk_path.join(fname);
|
||||||
|
if let Ok(json) = serde_json::to_vec_pretty(data) {
|
||||||
|
// best-effort write
|
||||||
|
let _ = tokio::fs::write(path, json).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Spawn background updater that periodically refreshes cached domains using provided fetcher.
|
||||||
|
pub fn spawn_updater<F>(
|
||||||
|
self: Arc<Self>,
|
||||||
|
domains: Vec<String>,
|
||||||
|
interval: Duration,
|
||||||
|
fetcher: F,
|
||||||
|
) where
|
||||||
|
F: Fn(String) -> tokio::task::JoinHandle<()> + Send + Sync + 'static,
|
||||||
|
{
|
||||||
|
tokio::spawn(async move {
|
||||||
|
loop {
|
||||||
|
for domain in &domains {
|
||||||
|
fetcher(domain.clone()).await;
|
||||||
|
}
|
||||||
|
sleep(interval).await;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Replace cached entry from a fetch result.
|
||||||
|
pub async fn update_from_fetch(&self, domain: &str, fetched: TlsFetchResult) {
|
||||||
|
let data = CachedTlsData {
|
||||||
|
server_hello_template: fetched.server_hello_parsed,
|
||||||
|
cert_info: fetched.cert_info,
|
||||||
|
app_data_records_sizes: fetched.app_data_records_sizes.clone(),
|
||||||
|
total_app_data_len: fetched.total_app_data_len,
|
||||||
|
fetched_at: SystemTime::now(),
|
||||||
|
domain: domain.to_string(),
|
||||||
|
};
|
||||||
|
|
||||||
|
self.set(domain, data.clone()).await;
|
||||||
|
self.persist(domain, &data).await;
|
||||||
|
debug!(domain = %domain, len = fetched.total_app_data_len, "TLS cache updated");
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn default_entry(&self) -> Arc<CachedTlsData> {
|
||||||
|
self.default.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn disk_path(&self) -> &Path {
|
||||||
|
&self.disk_path
|
||||||
|
}
|
||||||
|
}
|
||||||
134
src/tls_front/emulator.rs
Normal file
134
src/tls_front/emulator.rs
Normal file
@@ -0,0 +1,134 @@
|
|||||||
|
use crate::crypto::{sha256_hmac, SecureRandom};
|
||||||
|
use crate::protocol::constants::{
|
||||||
|
TLS_RECORD_APPLICATION, TLS_RECORD_CHANGE_CIPHER, TLS_RECORD_HANDSHAKE, TLS_VERSION,
|
||||||
|
};
|
||||||
|
use crate::protocol::tls::{TLS_DIGEST_LEN, TLS_DIGEST_POS, gen_fake_x25519_key};
|
||||||
|
use crate::tls_front::types::CachedTlsData;
|
||||||
|
|
||||||
|
const MIN_APP_DATA: usize = 64;
|
||||||
|
const MAX_APP_DATA: usize = 16640; // RFC 8446 §5.2 allows up to 2^14 + 256
|
||||||
|
|
||||||
|
fn jitter_and_clamp_sizes(sizes: &[usize], rng: &SecureRandom) -> Vec<usize> {
|
||||||
|
sizes
|
||||||
|
.iter()
|
||||||
|
.map(|&size| {
|
||||||
|
let base = size.max(MIN_APP_DATA).min(MAX_APP_DATA);
|
||||||
|
let jitter_range = ((base as f64) * 0.03).round() as i64;
|
||||||
|
if jitter_range == 0 {
|
||||||
|
return base;
|
||||||
|
}
|
||||||
|
let mut rand_bytes = [0u8; 2];
|
||||||
|
rand_bytes.copy_from_slice(&rng.bytes(2));
|
||||||
|
let span = 2 * jitter_range + 1;
|
||||||
|
let delta = (u16::from_le_bytes(rand_bytes) as i64 % span) - jitter_range;
|
||||||
|
let adjusted = (base as i64 + delta).clamp(MIN_APP_DATA as i64, MAX_APP_DATA as i64);
|
||||||
|
adjusted as usize
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Build a ServerHello + CCS + ApplicationData sequence using cached TLS metadata.
|
||||||
|
pub fn build_emulated_server_hello(
|
||||||
|
secret: &[u8],
|
||||||
|
client_digest: &[u8; TLS_DIGEST_LEN],
|
||||||
|
session_id: &[u8],
|
||||||
|
cached: &CachedTlsData,
|
||||||
|
rng: &SecureRandom,
|
||||||
|
) -> Vec<u8> {
|
||||||
|
// --- ServerHello ---
|
||||||
|
let mut extensions = Vec::new();
|
||||||
|
// KeyShare (x25519)
|
||||||
|
let key = gen_fake_x25519_key(rng);
|
||||||
|
extensions.extend_from_slice(&0x0033u16.to_be_bytes()); // key_share
|
||||||
|
extensions.extend_from_slice(&(2 + 2 + 32u16).to_be_bytes()); // len
|
||||||
|
extensions.extend_from_slice(&0x001du16.to_be_bytes()); // X25519
|
||||||
|
extensions.extend_from_slice(&(32u16).to_be_bytes());
|
||||||
|
extensions.extend_from_slice(&key);
|
||||||
|
// supported_versions (TLS1.3)
|
||||||
|
extensions.extend_from_slice(&0x002bu16.to_be_bytes());
|
||||||
|
extensions.extend_from_slice(&(2u16).to_be_bytes());
|
||||||
|
extensions.extend_from_slice(&0x0304u16.to_be_bytes());
|
||||||
|
|
||||||
|
let extensions_len = extensions.len() as u16;
|
||||||
|
|
||||||
|
let body_len = 2 + // version
|
||||||
|
32 + // random
|
||||||
|
1 + session_id.len() + // session id
|
||||||
|
2 + // cipher
|
||||||
|
1 + // compression
|
||||||
|
2 + extensions.len(); // extensions
|
||||||
|
|
||||||
|
let mut message = Vec::with_capacity(4 + body_len);
|
||||||
|
message.push(0x02); // ServerHello
|
||||||
|
let len_bytes = (body_len as u32).to_be_bytes();
|
||||||
|
message.extend_from_slice(&len_bytes[1..4]);
|
||||||
|
message.extend_from_slice(&cached.server_hello_template.version); // 0x0303
|
||||||
|
message.extend_from_slice(&[0u8; 32]); // random placeholder
|
||||||
|
message.push(session_id.len() as u8);
|
||||||
|
message.extend_from_slice(session_id);
|
||||||
|
let cipher = if cached.server_hello_template.cipher_suite == [0, 0] {
|
||||||
|
[0x13, 0x01]
|
||||||
|
} else {
|
||||||
|
cached.server_hello_template.cipher_suite
|
||||||
|
};
|
||||||
|
message.extend_from_slice(&cipher);
|
||||||
|
message.push(cached.server_hello_template.compression);
|
||||||
|
message.extend_from_slice(&extensions_len.to_be_bytes());
|
||||||
|
message.extend_from_slice(&extensions);
|
||||||
|
|
||||||
|
let mut server_hello = Vec::with_capacity(5 + message.len());
|
||||||
|
server_hello.push(TLS_RECORD_HANDSHAKE);
|
||||||
|
server_hello.extend_from_slice(&TLS_VERSION);
|
||||||
|
server_hello.extend_from_slice(&(message.len() as u16).to_be_bytes());
|
||||||
|
server_hello.extend_from_slice(&message);
|
||||||
|
|
||||||
|
// --- ChangeCipherSpec ---
|
||||||
|
let change_cipher_spec = [
|
||||||
|
TLS_RECORD_CHANGE_CIPHER,
|
||||||
|
TLS_VERSION[0],
|
||||||
|
TLS_VERSION[1],
|
||||||
|
0x00,
|
||||||
|
0x01,
|
||||||
|
0x01,
|
||||||
|
];
|
||||||
|
|
||||||
|
// --- ApplicationData (fake encrypted records) ---
|
||||||
|
// Use the same number and sizes of ApplicationData records as the cached server.
|
||||||
|
let mut sizes = cached.app_data_records_sizes.clone();
|
||||||
|
if sizes.is_empty() {
|
||||||
|
sizes.push(cached.total_app_data_len.max(1024));
|
||||||
|
}
|
||||||
|
let sizes = jitter_and_clamp_sizes(&sizes, rng);
|
||||||
|
|
||||||
|
let mut app_data = Vec::new();
|
||||||
|
for size in sizes {
|
||||||
|
let mut rec = Vec::with_capacity(5 + size);
|
||||||
|
rec.push(TLS_RECORD_APPLICATION);
|
||||||
|
rec.extend_from_slice(&TLS_VERSION);
|
||||||
|
rec.extend_from_slice(&(size as u16).to_be_bytes());
|
||||||
|
if size > 17 {
|
||||||
|
let body_len = size - 17;
|
||||||
|
rec.extend_from_slice(&rng.bytes(body_len));
|
||||||
|
rec.push(0x16); // inner content type marker (handshake)
|
||||||
|
rec.extend_from_slice(&rng.bytes(16)); // AEAD-like tag
|
||||||
|
} else {
|
||||||
|
rec.extend_from_slice(&rng.bytes(size));
|
||||||
|
}
|
||||||
|
app_data.extend_from_slice(&rec);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Combine ---
|
||||||
|
let mut response = Vec::with_capacity(server_hello.len() + change_cipher_spec.len() + app_data.len());
|
||||||
|
response.extend_from_slice(&server_hello);
|
||||||
|
response.extend_from_slice(&change_cipher_spec);
|
||||||
|
response.extend_from_slice(&app_data);
|
||||||
|
|
||||||
|
// --- HMAC ---
|
||||||
|
let mut hmac_input = Vec::with_capacity(TLS_DIGEST_LEN + response.len());
|
||||||
|
hmac_input.extend_from_slice(client_digest);
|
||||||
|
hmac_input.extend_from_slice(&response);
|
||||||
|
let digest = sha256_hmac(secret, &hmac_input);
|
||||||
|
response[TLS_DIGEST_POS..TLS_DIGEST_POS + TLS_DIGEST_LEN].copy_from_slice(&digest);
|
||||||
|
|
||||||
|
response
|
||||||
|
}
|
||||||
465
src/tls_front/fetcher.rs
Normal file
465
src/tls_front/fetcher.rs
Normal file
@@ -0,0 +1,465 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use anyhow::{Context, Result, anyhow};
|
||||||
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||||
|
use tokio::net::TcpStream;
|
||||||
|
use tokio::time::timeout;
|
||||||
|
use tokio_rustls::client::TlsStream;
|
||||||
|
use tokio_rustls::TlsConnector;
|
||||||
|
use tracing::{debug, warn};
|
||||||
|
|
||||||
|
use rustls::client::danger::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier};
|
||||||
|
use rustls::client::ClientConfig;
|
||||||
|
use rustls::pki_types::{CertificateDer, ServerName, UnixTime};
|
||||||
|
use rustls::{DigitallySignedStruct, Error as RustlsError};
|
||||||
|
|
||||||
|
use x509_parser::prelude::FromDer;
|
||||||
|
use x509_parser::certificate::X509Certificate;
|
||||||
|
|
||||||
|
use crate::crypto::SecureRandom;
|
||||||
|
use crate::protocol::constants::{TLS_RECORD_APPLICATION, TLS_RECORD_HANDSHAKE};
|
||||||
|
use crate::tls_front::types::{ParsedServerHello, TlsExtension, TlsFetchResult, ParsedCertificateInfo};
|
||||||
|
|
||||||
|
/// No-op verifier: accept any certificate (we only need lengths and metadata).
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct NoVerify;
|
||||||
|
|
||||||
|
impl ServerCertVerifier for NoVerify {
|
||||||
|
fn verify_server_cert(
|
||||||
|
&self,
|
||||||
|
_end_entity: &CertificateDer<'_>,
|
||||||
|
_intermediates: &[CertificateDer<'_>],
|
||||||
|
_server_name: &ServerName<'_>,
|
||||||
|
_ocsp: &[u8],
|
||||||
|
_now: UnixTime,
|
||||||
|
) -> Result<ServerCertVerified, RustlsError> {
|
||||||
|
Ok(ServerCertVerified::assertion())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn verify_tls12_signature(
|
||||||
|
&self,
|
||||||
|
_message: &[u8],
|
||||||
|
_cert: &CertificateDer<'_>,
|
||||||
|
_dss: &DigitallySignedStruct,
|
||||||
|
) -> Result<HandshakeSignatureValid, RustlsError> {
|
||||||
|
Ok(HandshakeSignatureValid::assertion())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn verify_tls13_signature(
|
||||||
|
&self,
|
||||||
|
_message: &[u8],
|
||||||
|
_cert: &CertificateDer<'_>,
|
||||||
|
_dss: &DigitallySignedStruct,
|
||||||
|
) -> Result<HandshakeSignatureValid, RustlsError> {
|
||||||
|
Ok(HandshakeSignatureValid::assertion())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn supported_verify_schemes(&self) -> Vec<rustls::SignatureScheme> {
|
||||||
|
use rustls::SignatureScheme::*;
|
||||||
|
vec![
|
||||||
|
RSA_PKCS1_SHA256,
|
||||||
|
RSA_PSS_SHA256,
|
||||||
|
ECDSA_NISTP256_SHA256,
|
||||||
|
ECDSA_NISTP384_SHA384,
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_client_config() -> Arc<ClientConfig> {
|
||||||
|
let root = rustls::RootCertStore::empty();
|
||||||
|
|
||||||
|
let provider = rustls::crypto::ring::default_provider();
|
||||||
|
let mut config = ClientConfig::builder_with_provider(Arc::new(provider))
|
||||||
|
.with_protocol_versions(&[&rustls::version::TLS13, &rustls::version::TLS12])
|
||||||
|
.expect("protocol versions")
|
||||||
|
.with_root_certificates(root)
|
||||||
|
.with_no_client_auth();
|
||||||
|
|
||||||
|
config
|
||||||
|
.dangerous()
|
||||||
|
.set_certificate_verifier(Arc::new(NoVerify));
|
||||||
|
|
||||||
|
Arc::new(config)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_client_hello(sni: &str, rng: &SecureRandom) -> Vec<u8> {
|
||||||
|
// === ClientHello body ===
|
||||||
|
let mut body = Vec::new();
|
||||||
|
|
||||||
|
// Legacy version (TLS 1.0) as in real ClientHello headers
|
||||||
|
body.extend_from_slice(&[0x03, 0x03]);
|
||||||
|
|
||||||
|
// Random
|
||||||
|
body.extend_from_slice(&rng.bytes(32));
|
||||||
|
|
||||||
|
// Session ID: empty
|
||||||
|
body.push(0);
|
||||||
|
|
||||||
|
// Cipher suites (common minimal set, TLS1.3 + a few 1.2 fallbacks)
|
||||||
|
let cipher_suites: [u8; 10] = [
|
||||||
|
0x13, 0x01, // TLS_AES_128_GCM_SHA256
|
||||||
|
0x13, 0x02, // TLS_AES_256_GCM_SHA384
|
||||||
|
0x13, 0x03, // TLS_CHACHA20_POLY1305_SHA256
|
||||||
|
0x00, 0x2f, // TLS_RSA_WITH_AES_128_CBC_SHA (legacy)
|
||||||
|
0x00, 0xff, // RENEGOTIATION_INFO_SCSV
|
||||||
|
];
|
||||||
|
body.extend_from_slice(&(cipher_suites.len() as u16).to_be_bytes());
|
||||||
|
body.extend_from_slice(&cipher_suites);
|
||||||
|
|
||||||
|
// Compression methods: null only
|
||||||
|
body.push(1);
|
||||||
|
body.push(0);
|
||||||
|
|
||||||
|
// === Extensions ===
|
||||||
|
let mut exts = Vec::new();
|
||||||
|
|
||||||
|
// server_name (SNI)
|
||||||
|
let sni_bytes = sni.as_bytes();
|
||||||
|
let mut sni_ext = Vec::with_capacity(5 + sni_bytes.len());
|
||||||
|
sni_ext.extend_from_slice(&(sni_bytes.len() as u16 + 3).to_be_bytes());
|
||||||
|
sni_ext.push(0); // host_name
|
||||||
|
sni_ext.extend_from_slice(&(sni_bytes.len() as u16).to_be_bytes());
|
||||||
|
sni_ext.extend_from_slice(sni_bytes);
|
||||||
|
exts.extend_from_slice(&0x0000u16.to_be_bytes());
|
||||||
|
exts.extend_from_slice(&(sni_ext.len() as u16).to_be_bytes());
|
||||||
|
exts.extend_from_slice(&sni_ext);
|
||||||
|
|
||||||
|
// supported_groups
|
||||||
|
let groups: [u16; 2] = [0x001d, 0x0017]; // x25519, secp256r1
|
||||||
|
exts.extend_from_slice(&0x000au16.to_be_bytes());
|
||||||
|
exts.extend_from_slice(&((2 + groups.len() * 2) as u16).to_be_bytes());
|
||||||
|
exts.extend_from_slice(&(groups.len() as u16 * 2).to_be_bytes());
|
||||||
|
for g in groups { exts.extend_from_slice(&g.to_be_bytes()); }
|
||||||
|
|
||||||
|
// signature_algorithms
|
||||||
|
let sig_algs: [u16; 4] = [0x0804, 0x0805, 0x0403, 0x0503]; // rsa_pss_rsae_sha256/384, ecdsa_secp256r1_sha256, rsa_pkcs1_sha256
|
||||||
|
exts.extend_from_slice(&0x000du16.to_be_bytes());
|
||||||
|
exts.extend_from_slice(&((2 + sig_algs.len() * 2) as u16).to_be_bytes());
|
||||||
|
exts.extend_from_slice(&(sig_algs.len() as u16 * 2).to_be_bytes());
|
||||||
|
for a in sig_algs { exts.extend_from_slice(&a.to_be_bytes()); }
|
||||||
|
|
||||||
|
// supported_versions (TLS1.3 + TLS1.2)
|
||||||
|
let versions: [u16; 2] = [0x0304, 0x0303];
|
||||||
|
exts.extend_from_slice(&0x002bu16.to_be_bytes());
|
||||||
|
exts.extend_from_slice(&((1 + versions.len() * 2) as u16).to_be_bytes());
|
||||||
|
exts.push((versions.len() * 2) as u8);
|
||||||
|
for v in versions { exts.extend_from_slice(&v.to_be_bytes()); }
|
||||||
|
|
||||||
|
// key_share (x25519)
|
||||||
|
let key = gen_key_share(rng);
|
||||||
|
let mut keyshare = Vec::with_capacity(4 + key.len());
|
||||||
|
keyshare.extend_from_slice(&0x001du16.to_be_bytes()); // group
|
||||||
|
keyshare.extend_from_slice(&(key.len() as u16).to_be_bytes());
|
||||||
|
keyshare.extend_from_slice(&key);
|
||||||
|
exts.extend_from_slice(&0x0033u16.to_be_bytes());
|
||||||
|
exts.extend_from_slice(&((2 + keyshare.len()) as u16).to_be_bytes());
|
||||||
|
exts.extend_from_slice(&(keyshare.len() as u16).to_be_bytes());
|
||||||
|
exts.extend_from_slice(&keyshare);
|
||||||
|
|
||||||
|
// ALPN (http/1.1)
|
||||||
|
let alpn_proto = b"http/1.1";
|
||||||
|
exts.extend_from_slice(&0x0010u16.to_be_bytes());
|
||||||
|
exts.extend_from_slice(&((2 + 1 + alpn_proto.len()) as u16).to_be_bytes());
|
||||||
|
exts.extend_from_slice(&((1 + alpn_proto.len()) as u16).to_be_bytes());
|
||||||
|
exts.push(alpn_proto.len() as u8);
|
||||||
|
exts.extend_from_slice(alpn_proto);
|
||||||
|
|
||||||
|
// padding to reduce recognizability and keep length ~500 bytes
|
||||||
|
const TARGET_EXT_LEN: usize = 180;
|
||||||
|
if exts.len() < TARGET_EXT_LEN {
|
||||||
|
let remaining = TARGET_EXT_LEN - exts.len();
|
||||||
|
if remaining > 4 {
|
||||||
|
let pad_len = remaining - 4; // minus type+len
|
||||||
|
exts.extend_from_slice(&0x0015u16.to_be_bytes()); // padding extension
|
||||||
|
exts.extend_from_slice(&(pad_len as u16).to_be_bytes());
|
||||||
|
exts.resize(exts.len() + pad_len, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extensions length prefix
|
||||||
|
body.extend_from_slice(&(exts.len() as u16).to_be_bytes());
|
||||||
|
body.extend_from_slice(&exts);
|
||||||
|
|
||||||
|
// === Handshake wrapper ===
|
||||||
|
let mut handshake = Vec::new();
|
||||||
|
handshake.push(0x01); // ClientHello
|
||||||
|
let len_bytes = (body.len() as u32).to_be_bytes();
|
||||||
|
handshake.extend_from_slice(&len_bytes[1..4]);
|
||||||
|
handshake.extend_from_slice(&body);
|
||||||
|
|
||||||
|
// === Record ===
|
||||||
|
let mut record = Vec::new();
|
||||||
|
record.push(TLS_RECORD_HANDSHAKE);
|
||||||
|
record.extend_from_slice(&[0x03, 0x01]); // legacy record version
|
||||||
|
record.extend_from_slice(&(handshake.len() as u16).to_be_bytes());
|
||||||
|
record.extend_from_slice(&handshake);
|
||||||
|
|
||||||
|
record
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gen_key_share(rng: &SecureRandom) -> [u8; 32] {
|
||||||
|
let mut key = [0u8; 32];
|
||||||
|
key.copy_from_slice(&rng.bytes(32));
|
||||||
|
key
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn read_tls_record(stream: &mut TcpStream) -> Result<(u8, Vec<u8>)> {
|
||||||
|
let mut header = [0u8; 5];
|
||||||
|
stream.read_exact(&mut header).await?;
|
||||||
|
let len = u16::from_be_bytes([header[3], header[4]]) as usize;
|
||||||
|
let mut body = vec![0u8; len];
|
||||||
|
stream.read_exact(&mut body).await?;
|
||||||
|
Ok((header[0], body))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_server_hello(body: &[u8]) -> Option<ParsedServerHello> {
|
||||||
|
if body.len() < 4 || body[0] != 0x02 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let msg_len = u32::from_be_bytes([0, body[1], body[2], body[3]]) as usize;
|
||||||
|
if msg_len + 4 > body.len() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut pos = 4;
|
||||||
|
let version = [*body.get(pos)?, *body.get(pos + 1)?];
|
||||||
|
pos += 2;
|
||||||
|
|
||||||
|
let mut random = [0u8; 32];
|
||||||
|
random.copy_from_slice(body.get(pos..pos + 32)?);
|
||||||
|
pos += 32;
|
||||||
|
|
||||||
|
let session_len = *body.get(pos)? as usize;
|
||||||
|
pos += 1;
|
||||||
|
let session_id = body.get(pos..pos + session_len)?.to_vec();
|
||||||
|
pos += session_len;
|
||||||
|
|
||||||
|
let cipher_suite = [*body.get(pos)?, *body.get(pos + 1)?];
|
||||||
|
pos += 2;
|
||||||
|
|
||||||
|
let compression = *body.get(pos)?;
|
||||||
|
pos += 1;
|
||||||
|
|
||||||
|
let ext_len = u16::from_be_bytes([*body.get(pos)?, *body.get(pos + 1)?]) as usize;
|
||||||
|
pos += 2;
|
||||||
|
let ext_end = pos.checked_add(ext_len)?;
|
||||||
|
if ext_end > body.len() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut extensions = Vec::new();
|
||||||
|
while pos + 4 <= ext_end {
|
||||||
|
let etype = u16::from_be_bytes([body[pos], body[pos + 1]]);
|
||||||
|
let elen = u16::from_be_bytes([body[pos + 2], body[pos + 3]]) as usize;
|
||||||
|
pos += 4;
|
||||||
|
let data = body.get(pos..pos + elen)?.to_vec();
|
||||||
|
pos += elen;
|
||||||
|
extensions.push(TlsExtension { ext_type: etype, data });
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(ParsedServerHello {
|
||||||
|
version,
|
||||||
|
random,
|
||||||
|
session_id,
|
||||||
|
cipher_suite,
|
||||||
|
compression,
|
||||||
|
extensions,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_cert_info(certs: &[CertificateDer<'static>]) -> Option<ParsedCertificateInfo> {
|
||||||
|
let first = certs.first()?;
|
||||||
|
let (_rem, cert) = X509Certificate::from_der(first.as_ref()).ok()?;
|
||||||
|
|
||||||
|
let not_before = Some(cert.validity().not_before.to_datetime().unix_timestamp());
|
||||||
|
let not_after = Some(cert.validity().not_after.to_datetime().unix_timestamp());
|
||||||
|
|
||||||
|
let issuer_cn = cert
|
||||||
|
.issuer()
|
||||||
|
.iter_common_name()
|
||||||
|
.next()
|
||||||
|
.and_then(|cn| cn.as_str().ok())
|
||||||
|
.map(|s| s.to_string());
|
||||||
|
|
||||||
|
let subject_cn = cert
|
||||||
|
.subject()
|
||||||
|
.iter_common_name()
|
||||||
|
.next()
|
||||||
|
.and_then(|cn| cn.as_str().ok())
|
||||||
|
.map(|s| s.to_string());
|
||||||
|
|
||||||
|
let san_names = cert
|
||||||
|
.subject_alternative_name()
|
||||||
|
.ok()
|
||||||
|
.flatten()
|
||||||
|
.map(|san| {
|
||||||
|
san.value
|
||||||
|
.general_names
|
||||||
|
.iter()
|
||||||
|
.filter_map(|gn| match gn {
|
||||||
|
x509_parser::extensions::GeneralName::DNSName(n) => Some(n.to_string()),
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
})
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
Some(ParsedCertificateInfo {
|
||||||
|
not_after_unix: not_after,
|
||||||
|
not_before_unix: not_before,
|
||||||
|
issuer_cn,
|
||||||
|
subject_cn,
|
||||||
|
san_names,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn fetch_via_raw_tls(
|
||||||
|
host: &str,
|
||||||
|
port: u16,
|
||||||
|
sni: &str,
|
||||||
|
connect_timeout: Duration,
|
||||||
|
) -> Result<TlsFetchResult> {
|
||||||
|
let addr = format!("{host}:{port}");
|
||||||
|
let mut stream = timeout(connect_timeout, TcpStream::connect(addr)).await??;
|
||||||
|
|
||||||
|
let rng = SecureRandom::new();
|
||||||
|
let client_hello = build_client_hello(sni, &rng);
|
||||||
|
timeout(connect_timeout, async {
|
||||||
|
stream.write_all(&client_hello).await?;
|
||||||
|
stream.flush().await?;
|
||||||
|
Ok::<(), std::io::Error>(())
|
||||||
|
})
|
||||||
|
.await??;
|
||||||
|
|
||||||
|
let mut records = Vec::new();
|
||||||
|
// Read up to 4 records: ServerHello, CCS, and up to two ApplicationData.
|
||||||
|
for _ in 0..4 {
|
||||||
|
match timeout(connect_timeout, read_tls_record(&mut stream)).await {
|
||||||
|
Ok(Ok(rec)) => records.push(rec),
|
||||||
|
Ok(Err(e)) => return Err(e.into()),
|
||||||
|
Err(_) => break,
|
||||||
|
}
|
||||||
|
if records.len() >= 3 && records.iter().any(|(t, _)| *t == TLS_RECORD_APPLICATION) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut app_sizes = Vec::new();
|
||||||
|
let mut server_hello = None;
|
||||||
|
for (t, body) in &records {
|
||||||
|
if *t == TLS_RECORD_HANDSHAKE && server_hello.is_none() {
|
||||||
|
server_hello = parse_server_hello(body);
|
||||||
|
} else if *t == TLS_RECORD_APPLICATION {
|
||||||
|
app_sizes.push(body.len());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let parsed = server_hello.ok_or_else(|| anyhow!("ServerHello not received"))?;
|
||||||
|
let total_app_data_len = app_sizes.iter().sum::<usize>().max(1024);
|
||||||
|
|
||||||
|
Ok(TlsFetchResult {
|
||||||
|
server_hello_parsed: parsed,
|
||||||
|
app_data_records_sizes: if app_sizes.is_empty() {
|
||||||
|
vec![total_app_data_len]
|
||||||
|
} else {
|
||||||
|
app_sizes
|
||||||
|
},
|
||||||
|
total_app_data_len,
|
||||||
|
cert_info: None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Fetch real TLS metadata for the given SNI: negotiated cipher and cert lengths.
|
||||||
|
pub async fn fetch_real_tls(
|
||||||
|
host: &str,
|
||||||
|
port: u16,
|
||||||
|
sni: &str,
|
||||||
|
connect_timeout: Duration,
|
||||||
|
upstream: Option<std::sync::Arc<crate::transport::UpstreamManager>>,
|
||||||
|
) -> Result<TlsFetchResult> {
|
||||||
|
// Preferred path: raw TLS probe for accurate record sizing
|
||||||
|
match fetch_via_raw_tls(host, port, sni, connect_timeout).await {
|
||||||
|
Ok(res) => return Ok(res),
|
||||||
|
Err(e) => {
|
||||||
|
warn!(sni = %sni, error = %e, "Raw TLS fetch failed, falling back to rustls");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback: rustls handshake to at least get certificate sizes
|
||||||
|
let stream = if let Some(manager) = upstream {
|
||||||
|
// Resolve host to SocketAddr
|
||||||
|
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(s) => s,
|
||||||
|
Err(e) => {
|
||||||
|
warn!(sni = %sni, error = %e, "Upstream connect failed, using direct connect");
|
||||||
|
timeout(connect_timeout, TcpStream::connect((host, port))).await??
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
timeout(connect_timeout, TcpStream::connect((host, port))).await??
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
timeout(connect_timeout, TcpStream::connect((host, port))).await??
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
timeout(connect_timeout, TcpStream::connect((host, port))).await??
|
||||||
|
};
|
||||||
|
|
||||||
|
let config = build_client_config();
|
||||||
|
let connector = TlsConnector::from(config);
|
||||||
|
|
||||||
|
let server_name = ServerName::try_from(sni.to_owned())
|
||||||
|
.or_else(|_| ServerName::try_from(host.to_owned()))
|
||||||
|
.map_err(|_| RustlsError::General("invalid SNI".into()))?;
|
||||||
|
|
||||||
|
let tls_stream: TlsStream<TcpStream> = connector.connect(server_name, stream).await?;
|
||||||
|
|
||||||
|
// Extract negotiated parameters and certificates
|
||||||
|
let (_io, session) = tls_stream.get_ref();
|
||||||
|
let cipher_suite = session
|
||||||
|
.negotiated_cipher_suite()
|
||||||
|
.map(|s| u16::from(s.suite()).to_be_bytes())
|
||||||
|
.unwrap_or([0x13, 0x01]);
|
||||||
|
|
||||||
|
let certs: Vec<CertificateDer<'static>> = session
|
||||||
|
.peer_certificates()
|
||||||
|
.map(|slice| slice.to_vec())
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
let total_cert_len: usize = certs.iter().map(|c| c.len()).sum::<usize>().max(1024);
|
||||||
|
let cert_info = parse_cert_info(&certs);
|
||||||
|
|
||||||
|
// Heuristic: split across two records if large to mimic real servers a bit.
|
||||||
|
let app_data_records_sizes = if total_cert_len > 3000 {
|
||||||
|
vec![total_cert_len / 2, total_cert_len - total_cert_len / 2]
|
||||||
|
} else {
|
||||||
|
vec![total_cert_len]
|
||||||
|
};
|
||||||
|
|
||||||
|
let parsed = ParsedServerHello {
|
||||||
|
version: [0x03, 0x03],
|
||||||
|
random: [0u8; 32],
|
||||||
|
session_id: Vec::new(),
|
||||||
|
cipher_suite,
|
||||||
|
compression: 0,
|
||||||
|
extensions: Vec::new(),
|
||||||
|
};
|
||||||
|
|
||||||
|
debug!(
|
||||||
|
sni = %sni,
|
||||||
|
len = total_cert_len,
|
||||||
|
cipher = format!("0x{:04x}", u16::from_be_bytes(cipher_suite)),
|
||||||
|
"Fetched TLS metadata via rustls"
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(TlsFetchResult {
|
||||||
|
server_hello_parsed: parsed,
|
||||||
|
app_data_records_sizes: app_data_records_sizes.clone(),
|
||||||
|
total_app_data_len: app_data_records_sizes.iter().sum(),
|
||||||
|
cert_info,
|
||||||
|
})
|
||||||
|
}
|
||||||
7
src/tls_front/mod.rs
Normal file
7
src/tls_front/mod.rs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
pub mod types;
|
||||||
|
pub mod cache;
|
||||||
|
pub mod fetcher;
|
||||||
|
pub mod emulator;
|
||||||
|
|
||||||
|
pub use cache::TlsFrontCache;
|
||||||
|
pub use types::{CachedTlsData, TlsFetchResult};
|
||||||
55
src/tls_front/types.rs
Normal file
55
src/tls_front/types.rs
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
use std::time::SystemTime;
|
||||||
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
|
/// Parsed representation of an unencrypted TLS ServerHello.
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct ParsedServerHello {
|
||||||
|
pub version: [u8; 2],
|
||||||
|
pub random: [u8; 32],
|
||||||
|
pub session_id: Vec<u8>,
|
||||||
|
pub cipher_suite: [u8; 2],
|
||||||
|
pub compression: u8,
|
||||||
|
pub extensions: Vec<TlsExtension>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generic TLS extension container.
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct TlsExtension {
|
||||||
|
pub ext_type: u16,
|
||||||
|
pub data: Vec<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Basic certificate metadata (optional, informative).
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct ParsedCertificateInfo {
|
||||||
|
pub not_after_unix: Option<i64>,
|
||||||
|
pub not_before_unix: Option<i64>,
|
||||||
|
pub issuer_cn: Option<String>,
|
||||||
|
pub subject_cn: Option<String>,
|
||||||
|
pub san_names: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Cached data per SNI used by the emulator.
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct CachedTlsData {
|
||||||
|
pub server_hello_template: ParsedServerHello,
|
||||||
|
pub cert_info: Option<ParsedCertificateInfo>,
|
||||||
|
pub app_data_records_sizes: Vec<usize>,
|
||||||
|
pub total_app_data_len: usize,
|
||||||
|
#[serde(default = "now_system_time", skip_serializing, skip_deserializing)]
|
||||||
|
pub fetched_at: SystemTime,
|
||||||
|
pub domain: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn now_system_time() -> SystemTime {
|
||||||
|
SystemTime::now()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Result of attempting to fetch real TLS artifacts.
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct TlsFetchResult {
|
||||||
|
pub server_hello_parsed: ParsedServerHello,
|
||||||
|
pub app_data_records_sizes: Vec<usize>,
|
||||||
|
pub total_app_data_len: usize,
|
||||||
|
pub cert_info: Option<ParsedCertificateInfo>,
|
||||||
|
}
|
||||||
@@ -4,6 +4,14 @@ use crate::crypto::{AesCbc, crc32};
|
|||||||
use crate::error::{ProxyError, Result};
|
use crate::error::{ProxyError, Result};
|
||||||
use crate::protocol::constants::*;
|
use crate::protocol::constants::*;
|
||||||
|
|
||||||
|
/// Commands sent to dedicated writer tasks to avoid mutex contention on TCP writes.
|
||||||
|
pub(crate) enum WriterCommand {
|
||||||
|
Data(Vec<u8>),
|
||||||
|
DataAndFlush(Vec<u8>),
|
||||||
|
Keepalive,
|
||||||
|
Close,
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn build_rpc_frame(seq_no: i32, payload: &[u8]) -> Vec<u8> {
|
pub(crate) fn build_rpc_frame(seq_no: i32, payload: &[u8]) -> Vec<u8> {
|
||||||
let total_len = (4 + 4 + payload.len() + 4) as u32;
|
let total_len = (4 + 4 + payload.len() + 4) as u32;
|
||||||
let mut frame = Vec::with_capacity(total_len as usize);
|
let mut frame = Vec::with_capacity(total_len as usize);
|
||||||
@@ -174,7 +182,19 @@ impl RpcWriter {
|
|||||||
if buf.len() >= 16 {
|
if buf.len() >= 16 {
|
||||||
self.iv.copy_from_slice(&buf[buf.len() - 16..]);
|
self.iv.copy_from_slice(&buf[buf.len() - 16..]);
|
||||||
}
|
}
|
||||||
self.writer.write_all(&buf).await.map_err(ProxyError::Io)?;
|
self.writer.write_all(&buf).await.map_err(ProxyError::Io)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn send_and_flush(&mut self, payload: &[u8]) -> Result<()> {
|
||||||
|
self.send(payload).await?;
|
||||||
self.writer.flush().await.map_err(ProxyError::Io)
|
self.writer.flush().await.map_err(ProxyError::Io)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn send_keepalive(&mut self, payload: [u8; 4]) -> Result<()> {
|
||||||
|
// Keepalive is a frame with fl == 4 and 4 bytes payload.
|
||||||
|
let mut frame = Vec::with_capacity(8);
|
||||||
|
frame.extend_from_slice(&4u32.to_le_bytes());
|
||||||
|
frame.extend_from_slice(&payload);
|
||||||
|
self.send(&frame).await
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ use std::net::IpAddr;
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use regex::Regex;
|
|
||||||
use httpdate;
|
use httpdate;
|
||||||
use tracing::{debug, info, warn};
|
use tracing::{debug, info, warn};
|
||||||
|
|
||||||
@@ -14,12 +13,69 @@ use super::secret::download_proxy_secret;
|
|||||||
use crate::crypto::SecureRandom;
|
use crate::crypto::SecureRandom;
|
||||||
use std::time::SystemTime;
|
use std::time::SystemTime;
|
||||||
|
|
||||||
|
async fn retry_fetch(url: &str) -> Option<ProxyConfigData> {
|
||||||
|
let delays = [1u64, 5, 15];
|
||||||
|
for (i, d) in delays.iter().enumerate() {
|
||||||
|
match fetch_proxy_config(url).await {
|
||||||
|
Ok(cfg) => return Some(cfg),
|
||||||
|
Err(e) => {
|
||||||
|
if i == delays.len() - 1 {
|
||||||
|
warn!(error = %e, url, "fetch_proxy_config failed");
|
||||||
|
} else {
|
||||||
|
debug!(error = %e, url, "fetch_proxy_config retrying");
|
||||||
|
tokio::time::sleep(Duration::from_secs(*d)).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default)]
|
#[derive(Debug, Clone, Default)]
|
||||||
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>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn parse_host_port(s: &str) -> Option<(IpAddr, u16)> {
|
||||||
|
if let Some(bracket_end) = s.rfind(']') {
|
||||||
|
if s.starts_with('[') && bracket_end + 1 < s.len() && s.as_bytes().get(bracket_end + 1) == Some(&b':') {
|
||||||
|
let host = &s[1..bracket_end];
|
||||||
|
let port_str = &s[bracket_end + 2..];
|
||||||
|
let ip = host.parse::<IpAddr>().ok()?;
|
||||||
|
let port = port_str.parse::<u16>().ok()?;
|
||||||
|
return Some((ip, port));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let idx = s.rfind(':')?;
|
||||||
|
let host = &s[..idx];
|
||||||
|
let port_str = &s[idx + 1..];
|
||||||
|
let ip = host.parse::<IpAddr>().ok()?;
|
||||||
|
let port = port_str.parse::<u16>().ok()?;
|
||||||
|
Some((ip, port))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_proxy_line(line: &str) -> Option<(i32, IpAddr, u16)> {
|
||||||
|
// Accepts lines like:
|
||||||
|
// proxy_for 4 91.108.4.195:8888;
|
||||||
|
// proxy_for 2 [2001:67c:04e8:f002::d]:80;
|
||||||
|
// proxy_for 2 2001:67c:04e8:f002::d:80;
|
||||||
|
let trimmed = line.trim();
|
||||||
|
if !trimmed.starts_with("proxy_for") {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
// Capture everything between dc and trailing ';'
|
||||||
|
let without_prefix = trimmed.trim_start_matches("proxy_for").trim();
|
||||||
|
let mut parts = without_prefix.split_whitespace();
|
||||||
|
let dc_str = parts.next()?;
|
||||||
|
let rest = parts.next()?;
|
||||||
|
let host_port = rest.trim_end_matches(';');
|
||||||
|
let dc = dc_str.parse::<i32>().ok()?;
|
||||||
|
let (ip, port) = parse_host_port(host_port)?;
|
||||||
|
Some((dc, ip, port))
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn fetch_proxy_config(url: &str) -> Result<ProxyConfigData> {
|
pub async fn fetch_proxy_config(url: &str) -> Result<ProxyConfigData> {
|
||||||
let resp = reqwest::get(url)
|
let resp = reqwest::get(url)
|
||||||
.await
|
.await
|
||||||
@@ -48,26 +104,26 @@ pub async fn fetch_proxy_config(url: &str) -> Result<ProxyConfigData> {
|
|||||||
.await
|
.await
|
||||||
.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 re_proxy = Regex::new(r"proxy_for\s+(-?\d+)\s+([^\s:]+):(\d+)\s*;").unwrap();
|
|
||||||
let re_default = Regex::new(r"default\s+(-?\d+)\s*;").unwrap();
|
|
||||||
|
|
||||||
let mut map: HashMap<i32, Vec<(IpAddr, u16)>> = HashMap::new();
|
let mut map: HashMap<i32, Vec<(IpAddr, u16)>> = HashMap::new();
|
||||||
for cap in re_proxy.captures_iter(&text) {
|
for line in text.lines() {
|
||||||
if let (Some(dc), Some(host), Some(port)) = (cap.get(1), cap.get(2), cap.get(3)) {
|
if let Some((dc, ip, port)) = parse_proxy_line(line) {
|
||||||
if let Ok(dc_idx) = dc.as_str().parse::<i32>() {
|
map.entry(dc).or_default().push((ip, port));
|
||||||
if let Ok(ip) = host.as_str().parse::<IpAddr>() {
|
|
||||||
if let Ok(port_num) = port.as_str().parse::<u16>() {
|
|
||||||
map.entry(dc_idx).or_default().push((ip, port_num));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let default_dc = re_default
|
let default_dc = text
|
||||||
.captures(&text)
|
.lines()
|
||||||
.and_then(|c| c.get(1))
|
.find_map(|l| {
|
||||||
.and_then(|m| m.as_str().parse::<i32>().ok());
|
let t = l.trim();
|
||||||
|
if let Some(rest) = t.strip_prefix("default") {
|
||||||
|
return rest
|
||||||
|
.trim()
|
||||||
|
.trim_end_matches(';')
|
||||||
|
.parse::<i32>()
|
||||||
|
.ok();
|
||||||
|
}
|
||||||
|
None
|
||||||
|
});
|
||||||
|
|
||||||
Ok(ProxyConfigData { map, default_dc })
|
Ok(ProxyConfigData { map, default_dc })
|
||||||
}
|
}
|
||||||
@@ -80,7 +136,8 @@ pub async fn me_config_updater(pool: Arc<MePool>, rng: Arc<SecureRandom>, interv
|
|||||||
tick.tick().await;
|
tick.tick().await;
|
||||||
|
|
||||||
// Update proxy config v4
|
// Update proxy config v4
|
||||||
if let Ok(cfg) = fetch_proxy_config("https://core.telegram.org/getProxyConfig").await {
|
let cfg_v4 = retry_fetch("https://core.telegram.org/getProxyConfig").await;
|
||||||
|
if let Some(cfg) = cfg_v4 {
|
||||||
let changed = pool.update_proxy_maps(cfg.map.clone(), None).await;
|
let changed = pool.update_proxy_maps(cfg.map.clone(), None).await;
|
||||||
if let Some(dc) = cfg.default_dc {
|
if let Some(dc) = cfg.default_dc {
|
||||||
pool.default_dc.store(dc, std::sync::atomic::Ordering::Relaxed);
|
pool.default_dc.store(dc, std::sync::atomic::Ordering::Relaxed);
|
||||||
@@ -91,14 +148,20 @@ pub async fn me_config_updater(pool: Arc<MePool>, rng: Arc<SecureRandom>, interv
|
|||||||
} else {
|
} else {
|
||||||
debug!("ME config v4 unchanged");
|
debug!("ME config v4 unchanged");
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
warn!("getProxyConfig update failed");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update proxy config v6 (optional)
|
// Update proxy config v6 (optional)
|
||||||
if let Ok(cfg_v6) = fetch_proxy_config("https://core.telegram.org/getProxyConfigV6").await {
|
let cfg_v6 = retry_fetch("https://core.telegram.org/getProxyConfigV6").await;
|
||||||
let _ = pool.update_proxy_maps(HashMap::new(), Some(cfg_v6.map)).await;
|
if let Some(cfg_v6) = cfg_v6 {
|
||||||
|
let changed = pool.update_proxy_maps(HashMap::new(), Some(cfg_v6.map)).await;
|
||||||
|
if changed {
|
||||||
|
info!("ME config updated (v6), reconciling connections");
|
||||||
|
pool.reconcile_connections(&rng).await;
|
||||||
|
} else {
|
||||||
|
debug!("ME config v6 unchanged");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
pool.reset_stun_state();
|
||||||
|
|
||||||
// Update proxy-secret
|
// Update proxy-secret
|
||||||
match download_proxy_secret().await {
|
match download_proxy_secret().await {
|
||||||
@@ -111,3 +174,35 @@ pub async fn me_config_updater(pool: Arc<MePool>, rng: Arc<SecureRandom>, interv
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_ipv6_bracketed() {
|
||||||
|
let line = "proxy_for 2 [2001:67c:04e8:f002::d]:80;";
|
||||||
|
let res = parse_proxy_line(line).unwrap();
|
||||||
|
assert_eq!(res.0, 2);
|
||||||
|
assert_eq!(res.1, "2001:67c:04e8:f002::d".parse::<IpAddr>().unwrap());
|
||||||
|
assert_eq!(res.2, 80);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_ipv6_plain() {
|
||||||
|
let line = "proxy_for 2 2001:67c:04e8:f002::d:80;";
|
||||||
|
let res = parse_proxy_line(line).unwrap();
|
||||||
|
assert_eq!(res.0, 2);
|
||||||
|
assert_eq!(res.1, "2001:67c:04e8:f002::d".parse::<IpAddr>().unwrap());
|
||||||
|
assert_eq!(res.2, 80);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_ipv4() {
|
||||||
|
let line = "proxy_for 4 91.108.4.195:8888;";
|
||||||
|
let res = parse_proxy_line(line).unwrap();
|
||||||
|
assert_eq!(res.0, 4);
|
||||||
|
assert_eq!(res.1, "91.108.4.195".parse::<IpAddr>().unwrap());
|
||||||
|
assert_eq!(res.2, 8888);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,12 +10,13 @@ use std::os::raw::c_int;
|
|||||||
|
|
||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
use tokio::io::{AsyncReadExt, AsyncWriteExt, ReadHalf, WriteHalf};
|
use tokio::io::{AsyncReadExt, AsyncWriteExt, ReadHalf, WriteHalf};
|
||||||
use tokio::net::TcpStream;
|
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::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::protocol::constants::{
|
use crate::protocol::constants::{
|
||||||
ME_CONNECT_TIMEOUT_SECS, ME_HANDSHAKE_TIMEOUT_SECS, RPC_CRYPTO_AES_U32, RPC_HANDSHAKE_ERROR_U32,
|
ME_CONNECT_TIMEOUT_SECS, ME_HANDSHAKE_TIMEOUT_SECS, RPC_CRYPTO_AES_U32, RPC_HANDSHAKE_ERROR_U32,
|
||||||
RPC_HANDSHAKE_U32, RPC_PING_U32, RPC_PONG_U32, RPC_NONCE_U32,
|
RPC_HANDSHAKE_U32, RPC_PING_U32, RPC_PONG_U32, RPC_NONCE_U32,
|
||||||
@@ -43,7 +44,28 @@ impl MePool {
|
|||||||
/// TCP connect with timeout + return RTT in milliseconds.
|
/// TCP connect with timeout + return RTT in milliseconds.
|
||||||
pub(crate) async fn connect_tcp(&self, addr: SocketAddr) -> Result<(TcpStream, f64)> {
|
pub(crate) async fn connect_tcp(&self, addr: SocketAddr) -> Result<(TcpStream, f64)> {
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let stream = timeout(Duration::from_secs(ME_CONNECT_TIMEOUT_SECS), TcpStream::connect(addr))
|
let connect_fut = async {
|
||||||
|
if addr.is_ipv6() {
|
||||||
|
if 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
|
.await
|
||||||
.map_err(|_| ProxyError::ConnectionTimeout { addr: addr.to_string() })??;
|
.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;
|
||||||
@@ -101,8 +123,13 @@ impl MePool {
|
|||||||
let peer_addr = stream.peer_addr().map_err(ProxyError::Io)?;
|
let peer_addr = stream.peer_addr().map_err(ProxyError::Io)?;
|
||||||
|
|
||||||
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() {
|
||||||
|
IpFamily::V4
|
||||||
|
} else {
|
||||||
|
IpFamily::V6
|
||||||
|
};
|
||||||
let reflected = if self.nat_probe {
|
let reflected = if self.nat_probe {
|
||||||
self.maybe_reflect_public_addr().await
|
self.maybe_reflect_public_addr(family).await
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
@@ -121,7 +148,7 @@ impl MePool {
|
|||||||
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);
|
let nonce_frame = build_rpc_frame(-2, &nonce_payload);
|
||||||
let dump = hex_dump(&nonce_frame[..nonce_frame.len().min(44)]);
|
let dump = hex_dump(&nonce_frame[..nonce_frame.len().min(44)]);
|
||||||
info!(
|
debug!(
|
||||||
key_selector = format_args!("0x{ks:08x}"),
|
key_selector = format_args!("0x{ks:08x}"),
|
||||||
crypto_ts,
|
crypto_ts,
|
||||||
frame_len = nonce_frame.len(),
|
frame_len = nonce_frame.len(),
|
||||||
|
|||||||
@@ -1,113 +1,144 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::{HashMap, 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 tracing::{debug, info, warn};
|
||||||
use rand::seq::SliceRandom;
|
use rand::seq::SliceRandom;
|
||||||
|
use rand::Rng;
|
||||||
|
|
||||||
use crate::crypto::SecureRandom;
|
use crate::crypto::SecureRandom;
|
||||||
|
use crate::network::IpFamily;
|
||||||
|
|
||||||
use super::MePool;
|
use super::MePool;
|
||||||
|
|
||||||
pub async fn me_health_monitor(pool: Arc<MePool>, rng: Arc<SecureRandom>, _min_connections: usize) {
|
const HEALTH_INTERVAL_SECS: u64 = 1;
|
||||||
let mut backoff: HashMap<i32, u64> = HashMap::new();
|
const JITTER_FRAC_NUM: u64 = 2; // jitter up to 50% of backoff
|
||||||
let mut last_attempt: HashMap<i32, Instant> = HashMap::new();
|
const MAX_CONCURRENT_PER_DC_DEFAULT: usize = 1;
|
||||||
loop {
|
|
||||||
tokio::time::sleep(Duration::from_secs(30)).await;
|
|
||||||
// Per-DC coverage check
|
|
||||||
let map = pool.proxy_map_v4.read().await.clone();
|
|
||||||
let writer_addrs: std::collections::HashSet<SocketAddr> = pool
|
|
||||||
.writers
|
|
||||||
.read()
|
|
||||||
.await
|
|
||||||
.iter()
|
|
||||||
.map(|w| w.addr)
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
for (dc, addrs) in map.iter() {
|
pub async fn me_health_monitor(pool: Arc<MePool>, rng: Arc<SecureRandom>, _min_connections: usize) {
|
||||||
let dc_addrs: Vec<SocketAddr> = addrs
|
let mut backoff: HashMap<(i32, IpFamily), u64> = HashMap::new();
|
||||||
|
let mut next_attempt: HashMap<(i32, IpFamily), Instant> = HashMap::new();
|
||||||
|
let mut inflight: HashMap<(i32, IpFamily), usize> = HashMap::new();
|
||||||
|
loop {
|
||||||
|
tokio::time::sleep(Duration::from_secs(HEALTH_INTERVAL_SECS)).await;
|
||||||
|
check_family(
|
||||||
|
IpFamily::V4,
|
||||||
|
&pool,
|
||||||
|
&rng,
|
||||||
|
&mut backoff,
|
||||||
|
&mut next_attempt,
|
||||||
|
&mut inflight,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
check_family(
|
||||||
|
IpFamily::V6,
|
||||||
|
&pool,
|
||||||
|
&rng,
|
||||||
|
&mut backoff,
|
||||||
|
&mut next_attempt,
|
||||||
|
&mut inflight,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn check_family(
|
||||||
|
family: IpFamily,
|
||||||
|
pool: &Arc<MePool>,
|
||||||
|
rng: &Arc<SecureRandom>,
|
||||||
|
backoff: &mut HashMap<(i32, IpFamily), u64>,
|
||||||
|
next_attempt: &mut HashMap<(i32, IpFamily), Instant>,
|
||||||
|
inflight: &mut HashMap<(i32, IpFamily), usize>,
|
||||||
|
) {
|
||||||
|
let enabled = match family {
|
||||||
|
IpFamily::V4 => pool.decision.ipv4_me,
|
||||||
|
IpFamily::V6 => pool.decision.ipv6_me,
|
||||||
|
};
|
||||||
|
if !enabled {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let map = match family {
|
||||||
|
IpFamily::V4 => pool.proxy_map_v4.read().await.clone(),
|
||||||
|
IpFamily::V6 => pool.proxy_map_v6.read().await.clone(),
|
||||||
|
};
|
||||||
|
let writer_addrs: HashSet<SocketAddr> = pool
|
||||||
|
.writers
|
||||||
|
.read()
|
||||||
|
.await
|
||||||
|
.iter()
|
||||||
|
.map(|w| w.addr)
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let entries: Vec<(i32, Vec<SocketAddr>)> = map
|
||||||
|
.iter()
|
||||||
|
.map(|(dc, addrs)| {
|
||||||
|
let list = addrs
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(ip, port)| SocketAddr::new(*ip, *port))
|
.map(|(ip, port)| SocketAddr::new(*ip, *port))
|
||||||
.collect();
|
.collect::<Vec<_>>();
|
||||||
let has_coverage = dc_addrs.iter().any(|a| writer_addrs.contains(a));
|
(*dc, list)
|
||||||
if !has_coverage {
|
})
|
||||||
let delay = *backoff.get(dc).unwrap_or(&30);
|
.collect();
|
||||||
let now = Instant::now();
|
|
||||||
if let Some(last) = last_attempt.get(dc) {
|
for (dc, dc_addrs) in entries {
|
||||||
if now.duration_since(*last).as_secs() < delay {
|
let has_coverage = dc_addrs.iter().any(|a| writer_addrs.contains(a));
|
||||||
continue;
|
if has_coverage {
|
||||||
}
|
continue;
|
||||||
}
|
}
|
||||||
warn!(dc = %dc, delay, "DC has no ME coverage, reconnecting...");
|
|
||||||
let mut shuffled = dc_addrs.clone();
|
let key = (dc, family);
|
||||||
shuffled.shuffle(&mut rand::rng());
|
let now = Instant::now();
|
||||||
let mut reconnected = false;
|
if let Some(ts) = next_attempt.get(&key) {
|
||||||
for addr in shuffled {
|
if now < *ts {
|
||||||
match pool.connect_one(addr, &rng).await {
|
continue;
|
||||||
Ok(()) => {
|
|
||||||
info!(%addr, dc = %dc, "ME reconnected for DC coverage");
|
|
||||||
backoff.insert(*dc, 30);
|
|
||||||
last_attempt.insert(*dc, now);
|
|
||||||
reconnected = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
Err(e) => debug!(%addr, dc = %dc, error = %e, "ME reconnect failed"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !reconnected {
|
|
||||||
let next = (*backoff.get(dc).unwrap_or(&30)).saturating_mul(2).min(300);
|
|
||||||
backoff.insert(*dc, next);
|
|
||||||
last_attempt.insert(*dc, now);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// IPv6 coverage check (if available)
|
let max_concurrent = pool.me_reconnect_max_concurrent_per_dc.max(1) as usize;
|
||||||
let map_v6 = pool.proxy_map_v6.read().await.clone();
|
if *inflight.get(&key).unwrap_or(&0) >= max_concurrent {
|
||||||
let writer_addrs_v6: std::collections::HashSet<SocketAddr> = pool
|
return;
|
||||||
.writers
|
}
|
||||||
.read()
|
*inflight.entry(key).or_insert(0) += 1;
|
||||||
.await
|
|
||||||
.iter()
|
let mut shuffled = dc_addrs.clone();
|
||||||
.map(|w| w.addr)
|
shuffled.shuffle(&mut rand::rng());
|
||||||
.collect();
|
let mut success = false;
|
||||||
for (dc, addrs) in map_v6.iter() {
|
for addr in shuffled {
|
||||||
let dc_addrs: Vec<SocketAddr> = addrs
|
let res = tokio::time::timeout(pool.me_one_timeout, pool.connect_one(addr, rng.as_ref())).await;
|
||||||
.iter()
|
match res {
|
||||||
.map(|(ip, port)| SocketAddr::new(*ip, *port))
|
Ok(Ok(())) => {
|
||||||
.collect();
|
info!(%addr, dc = %dc, ?family, "ME reconnected for DC coverage");
|
||||||
let has_coverage = dc_addrs.iter().any(|a| writer_addrs_v6.contains(a));
|
pool.stats.increment_me_reconnect_success();
|
||||||
if !has_coverage {
|
backoff.insert(key, pool.me_reconnect_backoff_base.as_millis() as u64);
|
||||||
let delay = *backoff.get(dc).unwrap_or(&30);
|
let jitter = pool.me_reconnect_backoff_base.as_millis() as u64 / JITTER_FRAC_NUM;
|
||||||
let now = Instant::now();
|
let wait = pool.me_reconnect_backoff_base
|
||||||
if let Some(last) = last_attempt.get(dc) {
|
+ Duration::from_millis(rand::rng().random_range(0..=jitter.max(1)));
|
||||||
if now.duration_since(*last).as_secs() < delay {
|
next_attempt.insert(key, now + wait);
|
||||||
continue;
|
success = true;
|
||||||
}
|
break;
|
||||||
}
|
}
|
||||||
warn!(dc = %dc, delay, "IPv6 DC has no ME coverage, reconnecting...");
|
Ok(Err(e)) => {
|
||||||
let mut shuffled = dc_addrs.clone();
|
pool.stats.increment_me_reconnect_attempt();
|
||||||
shuffled.shuffle(&mut rand::rng());
|
debug!(%addr, dc = %dc, error = %e, ?family, "ME reconnect failed")
|
||||||
let mut reconnected = false;
|
|
||||||
for addr in shuffled {
|
|
||||||
match pool.connect_one(addr, &rng).await {
|
|
||||||
Ok(()) => {
|
|
||||||
info!(%addr, dc = %dc, "ME reconnected for IPv6 DC coverage");
|
|
||||||
backoff.insert(*dc, 30);
|
|
||||||
last_attempt.insert(*dc, now);
|
|
||||||
reconnected = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
Err(e) => debug!(%addr, dc = %dc, error = %e, "ME reconnect failed (IPv6)"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !reconnected {
|
|
||||||
let next = (*backoff.get(dc).unwrap_or(&30)).saturating_mul(2).min(300);
|
|
||||||
backoff.insert(*dc, next);
|
|
||||||
last_attempt.insert(*dc, now);
|
|
||||||
}
|
}
|
||||||
|
Err(_) => debug!(%addr, dc = %dc, ?family, "ME reconnect timed out"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if !success {
|
||||||
|
pool.stats.increment_me_reconnect_attempt();
|
||||||
|
let curr = *backoff.get(&key).unwrap_or(&(pool.me_reconnect_backoff_base.as_millis() as u64));
|
||||||
|
let next_ms = (curr.saturating_mul(2)).min(pool.me_reconnect_backoff_cap.as_millis() as u64);
|
||||||
|
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)));
|
||||||
|
next_attempt.insert(key, now + wait);
|
||||||
|
warn!(dc = %dc, backoff_ms = next_ms, ?family, "DC has no ME coverage, scheduled reconnect");
|
||||||
|
}
|
||||||
|
if let Some(v) = inflight.get_mut(&key) {
|
||||||
|
*v = v.saturating_sub(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ use bytes::Bytes;
|
|||||||
pub use health::me_health_monitor;
|
pub use health::me_health_monitor;
|
||||||
pub use ping::{run_me_ping, format_sample_line, MePingReport, MePingSample, MePingFamily};
|
pub use ping::{run_me_ping, format_sample_line, MePingReport, MePingSample, MePingFamily};
|
||||||
pub use pool::MePool;
|
pub use pool::MePool;
|
||||||
pub use pool_nat::{stun_probe, detect_public_ip, StunProbeResult};
|
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};
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
use std::net::{IpAddr, SocketAddr};
|
use std::net::{IpAddr, SocketAddr};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
@@ -92,8 +93,16 @@ mod tests {
|
|||||||
pub async fn run_me_ping(pool: &Arc<MePool>, rng: &SecureRandom) -> Vec<MePingReport> {
|
pub async fn run_me_ping(pool: &Arc<MePool>, rng: &SecureRandom) -> Vec<MePingReport> {
|
||||||
let mut reports = Vec::new();
|
let mut reports = Vec::new();
|
||||||
|
|
||||||
let v4_map = pool.proxy_map_v4.read().await.clone();
|
let v4_map = if pool.decision.ipv4_me {
|
||||||
let v6_map = pool.proxy_map_v6.read().await.clone();
|
pool.proxy_map_v4.read().await.clone()
|
||||||
|
} else {
|
||||||
|
HashMap::new()
|
||||||
|
};
|
||||||
|
let v6_map = if pool.decision.ipv6_me {
|
||||||
|
pool.proxy_map_v6.read().await.clone()
|
||||||
|
} else {
|
||||||
|
HashMap::new()
|
||||||
|
};
|
||||||
|
|
||||||
let mut grouped: Vec<(MePingFamily, i32, Vec<(IpAddr, u16)>)> = Vec::new();
|
let mut grouped: Vec<(MePingFamily, i32, Vec<(IpAddr, u16)>)> = Vec::new();
|
||||||
for (dc, addrs) in v4_map {
|
for (dc, addrs) in v4_map {
|
||||||
|
|||||||
@@ -1,57 +1,89 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::net::{IpAddr, SocketAddr};
|
use std::net::{IpAddr, Ipv6Addr, SocketAddr};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::sync::atomic::{AtomicBool, AtomicI32, AtomicU64, Ordering};
|
use std::sync::atomic::{AtomicBool, AtomicI32, AtomicU64, AtomicUsize, Ordering};
|
||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use rand::seq::SliceRandom;
|
use rand::seq::SliceRandom;
|
||||||
use tokio::sync::{Mutex, RwLock};
|
use tokio::sync::{Mutex, RwLock, mpsc, Notify};
|
||||||
use tokio_util::sync::CancellationToken;
|
use tokio_util::sync::CancellationToken;
|
||||||
use tracing::{debug, info, warn};
|
use tracing::{debug, info, warn};
|
||||||
use std::time::Duration;
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
use crate::crypto::SecureRandom;
|
use crate::crypto::SecureRandom;
|
||||||
use crate::error::{ProxyError, Result};
|
use crate::error::{ProxyError, Result};
|
||||||
|
use crate::network::probe::NetworkDecision;
|
||||||
|
use crate::network::IpFamily;
|
||||||
use crate::protocol::constants::*;
|
use crate::protocol::constants::*;
|
||||||
|
|
||||||
use super::ConnRegistry;
|
use super::ConnRegistry;
|
||||||
use super::registry::{BoundConn, ConnMeta};
|
use super::registry::{BoundConn, ConnMeta};
|
||||||
use super::codec::RpcWriter;
|
use super::codec::{RpcWriter, WriterCommand};
|
||||||
use super::reader::reader_loop;
|
use super::reader::reader_loop;
|
||||||
use super::MeResponse;
|
use super::MeResponse;
|
||||||
|
|
||||||
const ME_ACTIVE_PING_SECS: u64 = 25;
|
const ME_ACTIVE_PING_SECS: u64 = 25;
|
||||||
const ME_ACTIVE_PING_JITTER_SECS: i64 = 5;
|
const ME_ACTIVE_PING_JITTER_SECS: i64 = 5;
|
||||||
|
const ME_KEEPALIVE_PAYLOAD_LEN: usize = 4;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct MeWriter {
|
pub struct MeWriter {
|
||||||
pub id: u64,
|
pub id: u64,
|
||||||
pub addr: SocketAddr,
|
pub addr: SocketAddr,
|
||||||
pub writer: Arc<Mutex<RpcWriter>>,
|
pub tx: mpsc::Sender<WriterCommand>,
|
||||||
pub cancel: CancellationToken,
|
pub cancel: CancellationToken,
|
||||||
pub degraded: Arc<AtomicBool>,
|
pub degraded: Arc<AtomicBool>,
|
||||||
|
pub draining: Arc<AtomicBool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct MePool {
|
pub struct MePool {
|
||||||
pub(super) registry: Arc<ConnRegistry>,
|
pub(super) registry: Arc<ConnRegistry>,
|
||||||
pub(super) writers: Arc<RwLock<Vec<MeWriter>>>,
|
pub(super) writers: Arc<RwLock<Vec<MeWriter>>>,
|
||||||
pub(super) rr: AtomicU64,
|
pub(super) rr: AtomicU64,
|
||||||
|
pub(super) decision: NetworkDecision,
|
||||||
|
pub(super) rng: Arc<SecureRandom>,
|
||||||
pub(super) proxy_tag: Option<Vec<u8>>,
|
pub(super) proxy_tag: Option<Vec<u8>>,
|
||||||
pub(super) proxy_secret: Arc<RwLock<Vec<u8>>>,
|
pub(super) proxy_secret: Arc<RwLock<Vec<u8>>>,
|
||||||
pub(super) nat_ip_cfg: Option<IpAddr>,
|
pub(super) nat_ip_cfg: Option<IpAddr>,
|
||||||
pub(super) nat_ip_detected: Arc<RwLock<Option<IpAddr>>>,
|
pub(super) nat_ip_detected: Arc<RwLock<Option<IpAddr>>>,
|
||||||
pub(super) nat_probe: bool,
|
pub(super) nat_probe: bool,
|
||||||
pub(super) nat_stun: Option<String>,
|
pub(super) nat_stun: Option<String>,
|
||||||
|
pub(super) nat_stun_servers: Vec<String>,
|
||||||
|
pub(super) detected_ipv6: Option<Ipv6Addr>,
|
||||||
|
pub(super) nat_probe_attempts: std::sync::atomic::AtomicU8,
|
||||||
|
pub(super) nat_probe_disabled: std::sync::atomic::AtomicBool,
|
||||||
|
pub(super) stun_backoff_until: Arc<RwLock<Option<Instant>>>,
|
||||||
|
pub(super) me_one_retry: u8,
|
||||||
|
pub(super) me_one_timeout: Duration,
|
||||||
|
pub(super) me_keepalive_enabled: bool,
|
||||||
|
pub(super) me_keepalive_interval: Duration,
|
||||||
|
pub(super) me_keepalive_jitter: Duration,
|
||||||
|
pub(super) me_keepalive_payload_random: bool,
|
||||||
|
pub(super) me_warmup_stagger_enabled: bool,
|
||||||
|
pub(super) me_warmup_step_delay: Duration,
|
||||||
|
pub(super) me_warmup_step_jitter: Duration,
|
||||||
|
pub(super) me_reconnect_max_concurrent_per_dc: u32,
|
||||||
|
pub(super) me_reconnect_backoff_base: Duration,
|
||||||
|
pub(super) me_reconnect_backoff_cap: Duration,
|
||||||
|
pub(super) me_reconnect_fast_retry_count: u32,
|
||||||
pub(super) proxy_map_v4: Arc<RwLock<HashMap<i32, Vec<(IpAddr, u16)>>>>,
|
pub(super) proxy_map_v4: Arc<RwLock<HashMap<i32, Vec<(IpAddr, u16)>>>>,
|
||||||
pub(super) proxy_map_v6: Arc<RwLock<HashMap<i32, Vec<(IpAddr, u16)>>>>,
|
pub(super) proxy_map_v6: Arc<RwLock<HashMap<i32, Vec<(IpAddr, u16)>>>>,
|
||||||
pub(super) default_dc: AtomicI32,
|
pub(super) default_dc: AtomicI32,
|
||||||
pub(super) next_writer_id: AtomicU64,
|
pub(super) next_writer_id: AtomicU64,
|
||||||
pub(super) ping_tracker: Arc<Mutex<HashMap<i64, (std::time::Instant, u64)>>>,
|
pub(super) ping_tracker: Arc<Mutex<HashMap<i64, (std::time::Instant, u64)>>>,
|
||||||
pub(super) rtt_stats: Arc<Mutex<HashMap<u64, (f64, f64)>>>,
|
pub(super) rtt_stats: Arc<Mutex<HashMap<u64, (f64, f64)>>>,
|
||||||
pub(super) nat_reflection_cache: Arc<Mutex<Option<(std::time::Instant, std::net::SocketAddr)>>>,
|
pub(super) nat_reflection_cache: Arc<Mutex<NatReflectionCache>>,
|
||||||
|
pub(super) writer_available: Arc<Notify>,
|
||||||
|
pub(super) conn_count: AtomicUsize,
|
||||||
|
pub(super) stats: Arc<crate::stats::Stats>,
|
||||||
pool_size: usize,
|
pool_size: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
pub struct NatReflectionCache {
|
||||||
|
pub v4: Option<(std::time::Instant, std::net::SocketAddr)>,
|
||||||
|
pub v6: Option<(std::time::Instant, std::net::SocketAddr)>,
|
||||||
|
}
|
||||||
|
|
||||||
impl MePool {
|
impl MePool {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
proxy_tag: Option<Vec<u8>>,
|
proxy_tag: Option<Vec<u8>>,
|
||||||
@@ -59,20 +91,59 @@ impl MePool {
|
|||||||
nat_ip: Option<IpAddr>,
|
nat_ip: Option<IpAddr>,
|
||||||
nat_probe: bool,
|
nat_probe: bool,
|
||||||
nat_stun: Option<String>,
|
nat_stun: Option<String>,
|
||||||
|
nat_stun_servers: Vec<String>,
|
||||||
|
detected_ipv6: Option<Ipv6Addr>,
|
||||||
|
me_one_retry: u8,
|
||||||
|
me_one_timeout_ms: u64,
|
||||||
proxy_map_v4: HashMap<i32, Vec<(IpAddr, u16)>>,
|
proxy_map_v4: HashMap<i32, Vec<(IpAddr, u16)>>,
|
||||||
proxy_map_v6: HashMap<i32, Vec<(IpAddr, u16)>>,
|
proxy_map_v6: HashMap<i32, Vec<(IpAddr, u16)>>,
|
||||||
default_dc: Option<i32>,
|
default_dc: Option<i32>,
|
||||||
|
decision: NetworkDecision,
|
||||||
|
rng: Arc<SecureRandom>,
|
||||||
|
stats: Arc<crate::stats::Stats>,
|
||||||
|
me_keepalive_enabled: bool,
|
||||||
|
me_keepalive_interval_secs: u64,
|
||||||
|
me_keepalive_jitter_secs: u64,
|
||||||
|
me_keepalive_payload_random: bool,
|
||||||
|
me_warmup_stagger_enabled: bool,
|
||||||
|
me_warmup_step_delay_ms: u64,
|
||||||
|
me_warmup_step_jitter_ms: u64,
|
||||||
|
me_reconnect_max_concurrent_per_dc: u32,
|
||||||
|
me_reconnect_backoff_base_ms: u64,
|
||||||
|
me_reconnect_backoff_cap_ms: u64,
|
||||||
|
me_reconnect_fast_retry_count: u32,
|
||||||
) -> Arc<Self> {
|
) -> Arc<Self> {
|
||||||
Arc::new(Self {
|
Arc::new(Self {
|
||||||
registry: Arc::new(ConnRegistry::new()),
|
registry: Arc::new(ConnRegistry::new()),
|
||||||
writers: Arc::new(RwLock::new(Vec::new())),
|
writers: Arc::new(RwLock::new(Vec::new())),
|
||||||
rr: AtomicU64::new(0),
|
rr: AtomicU64::new(0),
|
||||||
|
decision,
|
||||||
|
rng,
|
||||||
proxy_tag,
|
proxy_tag,
|
||||||
proxy_secret: Arc::new(RwLock::new(proxy_secret)),
|
proxy_secret: Arc::new(RwLock::new(proxy_secret)),
|
||||||
nat_ip_cfg: nat_ip,
|
nat_ip_cfg: nat_ip,
|
||||||
nat_ip_detected: Arc::new(RwLock::new(None)),
|
nat_ip_detected: Arc::new(RwLock::new(None)),
|
||||||
nat_probe,
|
nat_probe,
|
||||||
nat_stun,
|
nat_stun,
|
||||||
|
nat_stun_servers,
|
||||||
|
detected_ipv6,
|
||||||
|
nat_probe_attempts: std::sync::atomic::AtomicU8::new(0),
|
||||||
|
nat_probe_disabled: std::sync::atomic::AtomicBool::new(false),
|
||||||
|
stun_backoff_until: Arc::new(RwLock::new(None)),
|
||||||
|
me_one_retry,
|
||||||
|
me_one_timeout: Duration::from_millis(me_one_timeout_ms),
|
||||||
|
stats,
|
||||||
|
me_keepalive_enabled,
|
||||||
|
me_keepalive_interval: Duration::from_secs(me_keepalive_interval_secs),
|
||||||
|
me_keepalive_jitter: Duration::from_secs(me_keepalive_jitter_secs),
|
||||||
|
me_keepalive_payload_random,
|
||||||
|
me_warmup_stagger_enabled,
|
||||||
|
me_warmup_step_delay: Duration::from_millis(me_warmup_step_delay_ms),
|
||||||
|
me_warmup_step_jitter: Duration::from_millis(me_warmup_step_jitter_ms),
|
||||||
|
me_reconnect_max_concurrent_per_dc,
|
||||||
|
me_reconnect_backoff_base: Duration::from_millis(me_reconnect_backoff_base_ms),
|
||||||
|
me_reconnect_backoff_cap: Duration::from_millis(me_reconnect_backoff_cap_ms),
|
||||||
|
me_reconnect_fast_retry_count,
|
||||||
pool_size: 2,
|
pool_size: 2,
|
||||||
proxy_map_v4: Arc::new(RwLock::new(proxy_map_v4)),
|
proxy_map_v4: Arc::new(RwLock::new(proxy_map_v4)),
|
||||||
proxy_map_v6: Arc::new(RwLock::new(proxy_map_v6)),
|
proxy_map_v6: Arc::new(RwLock::new(proxy_map_v6)),
|
||||||
@@ -80,7 +151,9 @@ impl MePool {
|
|||||||
next_writer_id: AtomicU64::new(1),
|
next_writer_id: AtomicU64::new(1),
|
||||||
ping_tracker: Arc::new(Mutex::new(HashMap::new())),
|
ping_tracker: Arc::new(Mutex::new(HashMap::new())),
|
||||||
rtt_stats: Arc::new(Mutex::new(HashMap::new())),
|
rtt_stats: Arc::new(Mutex::new(HashMap::new())),
|
||||||
nat_reflection_cache: Arc::new(Mutex::new(None)),
|
nat_reflection_cache: Arc::new(Mutex::new(NatReflectionCache::default())),
|
||||||
|
writer_available: Arc::new(Notify::new()),
|
||||||
|
conn_count: AtomicUsize::new(0),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,6 +161,11 @@ impl MePool {
|
|||||||
self.proxy_tag.is_some()
|
self.proxy_tag.is_some()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn reset_stun_state(&self) {
|
||||||
|
self.nat_probe_attempts.store(0, Ordering::Relaxed);
|
||||||
|
self.nat_probe_disabled.store(false, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn translate_our_addr(&self, addr: SocketAddr) -> SocketAddr {
|
pub fn translate_our_addr(&self, addr: SocketAddr) -> SocketAddr {
|
||||||
let ip = self.translate_ip_for_nat(addr.ip());
|
let ip = self.translate_ip_for_nat(addr.ip());
|
||||||
SocketAddr::new(ip, addr.port())
|
SocketAddr::new(ip, addr.port())
|
||||||
@@ -103,29 +181,34 @@ impl MePool {
|
|||||||
|
|
||||||
pub async fn reconcile_connections(self: &Arc<Self>, rng: &SecureRandom) {
|
pub async fn reconcile_connections(self: &Arc<Self>, rng: &SecureRandom) {
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
let map = self.proxy_map_v4.read().await.clone();
|
|
||||||
let dc_addrs: Vec<(i32, Vec<(IpAddr, u16)>)> = map
|
|
||||||
.iter()
|
|
||||||
.map(|(dc, addrs)| (*dc, addrs.clone()))
|
|
||||||
.collect();
|
|
||||||
let writers = self.writers.read().await;
|
let writers = self.writers.read().await;
|
||||||
let current: HashSet<SocketAddr> = writers.iter().map(|w| w.addr).collect();
|
let current: HashSet<SocketAddr> = writers
|
||||||
|
.iter()
|
||||||
|
.filter(|w| !w.draining.load(Ordering::Relaxed))
|
||||||
|
.map(|w| w.addr)
|
||||||
|
.collect();
|
||||||
drop(writers);
|
drop(writers);
|
||||||
|
|
||||||
for (_dc, addrs) in map.iter() {
|
for family in self.family_order() {
|
||||||
let dc_addrs: Vec<SocketAddr> = addrs
|
let map = self.proxy_map_for_family(family).await;
|
||||||
.iter()
|
for (_dc, addrs) in map.iter() {
|
||||||
.map(|(ip, port)| SocketAddr::new(*ip, *port))
|
let dc_addrs: Vec<SocketAddr> = addrs
|
||||||
.collect();
|
.iter()
|
||||||
if !dc_addrs.iter().any(|a| current.contains(a)) {
|
.map(|(ip, port)| SocketAddr::new(*ip, *port))
|
||||||
let mut shuffled = dc_addrs.clone();
|
.collect();
|
||||||
shuffled.shuffle(&mut rand::rng());
|
if !dc_addrs.iter().any(|a| current.contains(a)) {
|
||||||
for addr in shuffled {
|
let mut shuffled = dc_addrs.clone();
|
||||||
if self.connect_one(addr, rng).await.is_ok() {
|
shuffled.shuffle(&mut rand::rng());
|
||||||
break;
|
for addr in shuffled {
|
||||||
|
if self.connect_one(addr, rng).await.is_ok() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if !self.decision.effective_multipath && !current.is_empty() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,12 +229,36 @@ impl MePool {
|
|||||||
let mut guard = self.proxy_map_v6.write().await;
|
let mut guard = self.proxy_map_v6.write().await;
|
||||||
if !v6.is_empty() && *guard != v6 {
|
if !v6.is_empty() && *guard != v6 {
|
||||||
*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) {
|
||||||
|
if 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) {
|
||||||
|
if let Some(addrs) = guard.get(&k).cloned() {
|
||||||
|
guard.insert(-k, addrs);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
changed
|
changed
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn update_secret(&self, new_secret: Vec<u8>) -> bool {
|
pub async fn update_secret(self: &Arc<Self>, new_secret: Vec<u8>) -> bool {
|
||||||
if new_secret.len() < 32 {
|
if new_secret.len() < 32 {
|
||||||
warn!(len = new_secret.len(), "proxy-secret update ignored (too short)");
|
warn!(len = new_secret.len(), "proxy-secret update ignored (too short)");
|
||||||
return false;
|
return false;
|
||||||
@@ -166,10 +273,14 @@ impl MePool {
|
|||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn reconnect_all(&self) {
|
pub async fn reconnect_all(self: &Arc<Self>) {
|
||||||
// Graceful: do not drop all at once. New connections will use updated secret.
|
let ws = self.writers.read().await.clone();
|
||||||
// Existing writers remain until health monitor replaces them.
|
for w in ws {
|
||||||
// No-op here to avoid total outage.
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) async fn key_selector(&self) -> u32 {
|
pub(super) async fn key_selector(&self) -> u32 {
|
||||||
@@ -181,47 +292,108 @@ impl MePool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(super) fn family_order(&self) -> Vec<IpFamily> {
|
||||||
|
let mut order = Vec::new();
|
||||||
|
if self.decision.prefer_ipv6() {
|
||||||
|
if self.decision.ipv6_me {
|
||||||
|
order.push(IpFamily::V6);
|
||||||
|
}
|
||||||
|
if self.decision.ipv4_me {
|
||||||
|
order.push(IpFamily::V4);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if self.decision.ipv4_me {
|
||||||
|
order.push(IpFamily::V4);
|
||||||
|
}
|
||||||
|
if self.decision.ipv6_me {
|
||||||
|
order.push(IpFamily::V6);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
order
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn proxy_map_for_family(&self, family: IpFamily) -> HashMap<i32, Vec<(IpAddr, u16)>> {
|
||||||
|
match family {
|
||||||
|
IpFamily::V4 => self.proxy_map_v4.read().await.clone(),
|
||||||
|
IpFamily::V6 => self.proxy_map_v6.read().await.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn init(self: &Arc<Self>, pool_size: usize, rng: &Arc<SecureRandom>) -> Result<()> {
|
pub async fn init(self: &Arc<Self>, pool_size: usize, rng: &Arc<SecureRandom>) -> Result<()> {
|
||||||
let map = self.proxy_map_v4.read().await.clone();
|
let family_order = self.family_order();
|
||||||
let dc_addrs: Vec<(i32, Vec<(IpAddr, u16)>)> = map
|
|
||||||
.iter()
|
|
||||||
.map(|(dc, addrs)| (*dc, addrs.clone()))
|
|
||||||
.collect();
|
|
||||||
let ks = self.key_selector().await;
|
let ks = self.key_selector().await;
|
||||||
info!(
|
info!(
|
||||||
me_servers = map.len(),
|
me_servers = self.proxy_map_v4.read().await.len(),
|
||||||
pool_size,
|
pool_size,
|
||||||
key_selector = format_args!("0x{ks:08x}"),
|
key_selector = format_args!("0x{ks:08x}"),
|
||||||
secret_len = self.proxy_secret.read().await.len(),
|
secret_len = self.proxy_secret.read().await.len(),
|
||||||
"Initializing ME pool"
|
"Initializing ME pool"
|
||||||
);
|
);
|
||||||
|
|
||||||
// Ensure at least one connection per DC; run DCs in parallel.
|
for family in family_order {
|
||||||
let mut join = tokio::task::JoinSet::new();
|
let map = self.proxy_map_for_family(family).await;
|
||||||
for (dc, addrs) in dc_addrs.iter().cloned() {
|
let dc_addrs: Vec<(i32, Vec<(IpAddr, u16)>)> = map
|
||||||
if addrs.is_empty() {
|
.iter()
|
||||||
continue;
|
.map(|(dc, addrs)| (*dc, addrs.clone()))
|
||||||
}
|
.collect();
|
||||||
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 let Some(_res) = join.join_next().await {}
|
|
||||||
|
|
||||||
// Additional connections up to pool_size total (round-robin across DCs)
|
// Ensure at least one connection per DC; run DCs in parallel.
|
||||||
for (dc, addrs) in dc_addrs.iter() {
|
let mut join = tokio::task::JoinSet::new();
|
||||||
for (ip, port) in addrs {
|
let mut dc_failures = 0usize;
|
||||||
|
for (dc, addrs) in dc_addrs.iter().cloned() {
|
||||||
|
if addrs.is_empty() {
|
||||||
|
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 let Some(res) = join.join_next().await {
|
||||||
|
if let Ok(false) = res {
|
||||||
|
dc_failures += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if dc_failures > 2 {
|
||||||
|
return Err(ProxyError::Proxy("Too many ME DC init failures, falling back to direct".into()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Additional connections up to pool_size total (round-robin across DCs), staggered to de-phase lifecycles.
|
||||||
|
if self.me_warmup_stagger_enabled {
|
||||||
|
let mut delay_ms = 0u64;
|
||||||
|
for (dc, addrs) in dc_addrs.iter() {
|
||||||
|
for (ip, port) in addrs {
|
||||||
|
if self.connection_count() >= pool_size {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let addr = SocketAddr::new(*ip, *port);
|
||||||
|
let jitter = rand::rng().random_range(0..=self.me_warmup_step_jitter.as_millis() as u64);
|
||||||
|
delay_ms = delay_ms.saturating_add(self.me_warmup_step_delay.as_millis() as u64 + jitter);
|
||||||
|
tokio::time::sleep(Duration::from_millis(delay_ms)).await;
|
||||||
|
if let Err(e) = self.connect_one(addr, rng.as_ref()).await {
|
||||||
|
debug!(%addr, dc = %dc, error = %e, "Extra ME connect failed (staggered)");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (dc, addrs) in dc_addrs.iter() {
|
||||||
|
for (ip, port) in addrs {
|
||||||
|
if self.connection_count() >= pool_size {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let addr = SocketAddr::new(*ip, *port);
|
||||||
|
if let Err(e) = self.connect_one(addr, rng.as_ref()).await {
|
||||||
|
debug!(%addr, dc = %dc, error = %e, "Extra ME connect failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
if self.connection_count() >= pool_size {
|
if self.connection_count() >= pool_size {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
let addr = SocketAddr::new(*ip, *port);
|
|
||||||
if let Err(e) = self.connect_one(addr, rng.as_ref()).await {
|
|
||||||
debug!(%addr, dc = %dc, error = %e, "Extra ME connect failed");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if self.connection_count() >= pool_size {
|
}
|
||||||
|
|
||||||
|
if !self.decision.effective_multipath && self.connection_count() > 0 {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -244,20 +416,62 @@ impl MePool {
|
|||||||
let writer_id = self.next_writer_id.fetch_add(1, Ordering::Relaxed);
|
let writer_id = self.next_writer_id.fetch_add(1, Ordering::Relaxed);
|
||||||
let cancel = CancellationToken::new();
|
let cancel = CancellationToken::new();
|
||||||
let degraded = Arc::new(AtomicBool::new(false));
|
let degraded = Arc::new(AtomicBool::new(false));
|
||||||
let rpc_w = Arc::new(Mutex::new(RpcWriter {
|
let draining = Arc::new(AtomicBool::new(false));
|
||||||
|
let (tx, mut rx) = mpsc::channel::<WriterCommand>(4096);
|
||||||
|
let tx_for_keepalive = tx.clone();
|
||||||
|
let keepalive_random = self.me_keepalive_payload_random;
|
||||||
|
let stats = self.stats.clone();
|
||||||
|
let mut rpc_writer = RpcWriter {
|
||||||
writer: hs.wr,
|
writer: hs.wr,
|
||||||
key: hs.write_key,
|
key: hs.write_key,
|
||||||
iv: hs.write_iv,
|
iv: hs.write_iv,
|
||||||
seq_no: 0,
|
seq_no: 0,
|
||||||
}));
|
};
|
||||||
|
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::Keepalive) => {
|
||||||
|
let mut payload = [0u8; ME_KEEPALIVE_PAYLOAD_LEN];
|
||||||
|
if keepalive_random {
|
||||||
|
rand::rng().fill(&mut payload);
|
||||||
|
}
|
||||||
|
match rpc_writer.send_keepalive(payload).await {
|
||||||
|
Ok(()) => {
|
||||||
|
stats.increment_me_keepalive_sent();
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
stats.increment_me_keepalive_failed();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(WriterCommand::Close) | None => break,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ = cancel_wr.cancelled() => break,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
let writer = MeWriter {
|
let writer = MeWriter {
|
||||||
id: writer_id,
|
id: writer_id,
|
||||||
addr,
|
addr,
|
||||||
writer: rpc_w.clone(),
|
tx: tx.clone(),
|
||||||
cancel: cancel.clone(),
|
cancel: cancel.clone(),
|
||||||
degraded: degraded.clone(),
|
degraded: degraded.clone(),
|
||||||
|
draining: draining.clone(),
|
||||||
};
|
};
|
||||||
self.writers.write().await.push(writer.clone());
|
self.writers.write().await.push(writer.clone());
|
||||||
|
self.conn_count.fetch_add(1, Ordering::Relaxed);
|
||||||
|
self.writer_available.notify_waiters();
|
||||||
|
|
||||||
let reg = self.registry.clone();
|
let reg = self.registry.clone();
|
||||||
let writers_arc = self.writers_arc();
|
let writers_arc = self.writers_arc();
|
||||||
@@ -265,11 +479,19 @@ impl MePool {
|
|||||||
let rtt_stats = self.rtt_stats.clone();
|
let rtt_stats = self.rtt_stats.clone();
|
||||||
let pool = Arc::downgrade(self);
|
let pool = Arc::downgrade(self);
|
||||||
let cancel_ping = cancel.clone();
|
let cancel_ping = cancel.clone();
|
||||||
let rpc_w_ping = rpc_w.clone();
|
let tx_ping = tx.clone();
|
||||||
let ping_tracker_ping = ping_tracker.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 cancel_reader_token = cancel.clone();
|
||||||
|
let cancel_ping_token = cancel_ping.clone();
|
||||||
|
let cancel_keepalive_token = cancel.clone();
|
||||||
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
let cancel_reader = cancel.clone();
|
|
||||||
let res = reader_loop(
|
let res = reader_loop(
|
||||||
hs.rd,
|
hs.rd,
|
||||||
hs.read_key,
|
hs.read_key,
|
||||||
@@ -277,16 +499,21 @@ impl MePool {
|
|||||||
reg.clone(),
|
reg.clone(),
|
||||||
BytesMut::new(),
|
BytesMut::new(),
|
||||||
BytesMut::new(),
|
BytesMut::new(),
|
||||||
rpc_w.clone(),
|
tx.clone(),
|
||||||
ping_tracker.clone(),
|
ping_tracker.clone(),
|
||||||
rtt_stats.clone(),
|
rtt_stats.clone(),
|
||||||
writer_id,
|
writer_id,
|
||||||
degraded.clone(),
|
degraded.clone(),
|
||||||
cancel_reader.clone(),
|
cancel_reader_token.clone(),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
if let Some(pool) = pool.upgrade() {
|
if let Some(pool) = pool.upgrade() {
|
||||||
pool.remove_writer_and_reroute(writer_id).await;
|
if 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 let Err(e) = res {
|
||||||
warn!(error = %e, "ME reader ended");
|
warn!(error = %e, "ME reader ended");
|
||||||
@@ -304,30 +531,56 @@ impl MePool {
|
|||||||
.random_range(-ME_ACTIVE_PING_JITTER_SECS..=ME_ACTIVE_PING_JITTER_SECS);
|
.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;
|
let wait = (ME_ACTIVE_PING_SECS as i64 + jitter).max(5) as u64;
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
_ = cancel_ping.cancelled() => {
|
_ = cancel_ping_token.cancelled() => {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
_ = tokio::time::sleep(Duration::from_secs(wait)) => {}
|
_ = tokio::time::sleep(Duration::from_secs(wait)) => {}
|
||||||
}
|
}
|
||||||
|
let sent_id = ping_id;
|
||||||
let mut p = Vec::with_capacity(12);
|
let mut p = Vec::with_capacity(12);
|
||||||
p.extend_from_slice(&RPC_PING_U32.to_le_bytes());
|
p.extend_from_slice(&RPC_PING_U32.to_le_bytes());
|
||||||
p.extend_from_slice(&ping_id.to_le_bytes());
|
p.extend_from_slice(&sent_id.to_le_bytes());
|
||||||
ping_id = ping_id.wrapping_add(1);
|
|
||||||
{
|
{
|
||||||
let mut tracker = ping_tracker_ping.lock().await;
|
let mut tracker = ping_tracker_ping.lock().await;
|
||||||
tracker.insert(ping_id, (std::time::Instant::now(), writer_id));
|
tracker.retain(|_, (ts, _)| ts.elapsed() < Duration::from_secs(120));
|
||||||
|
tracker.insert(sent_id, (std::time::Instant::now(), writer_id));
|
||||||
}
|
}
|
||||||
if let Err(e) = rpc_w_ping.lock().await.send(&p).await {
|
ping_id = ping_id.wrapping_add(1);
|
||||||
debug!(error = %e, "Active ME ping failed, removing dead writer");
|
if tx_ping.send(WriterCommand::DataAndFlush(p)).await.is_err() {
|
||||||
|
debug!("Active ME ping failed, removing dead writer");
|
||||||
cancel_ping.cancel();
|
cancel_ping.cancel();
|
||||||
if let Some(pool) = pool_ping.upgrade() {
|
if let Some(pool) = pool_ping.upgrade() {
|
||||||
pool.remove_writer_and_reroute(writer_id).await;
|
if cleanup_for_ping
|
||||||
|
.compare_exchange(false, true, Ordering::AcqRel, Ordering::Relaxed)
|
||||||
|
.is_ok()
|
||||||
|
{
|
||||||
|
pool.remove_writer_and_close_clients(writer_id).await;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if keepalive_enabled {
|
||||||
|
let tx_keepalive = tx_for_keepalive;
|
||||||
|
let cancel_keepalive = cancel_keepalive_token;
|
||||||
|
tokio::spawn(async move {
|
||||||
|
// Per-writer jittered start to avoid phase sync.
|
||||||
|
let initial_jitter_ms = rand::rng().random_range(0..=keepalive_jitter.as_millis().max(1) as u64);
|
||||||
|
tokio::time::sleep(Duration::from_millis(initial_jitter_ms)).await;
|
||||||
|
loop {
|
||||||
|
tokio::select! {
|
||||||
|
_ = cancel_keepalive.cancelled() => break,
|
||||||
|
_ = tokio::time::sleep(keepalive_interval + Duration::from_millis(rand::rng().random_range(0..=keepalive_jitter.as_millis() as u64))) => {}
|
||||||
|
}
|
||||||
|
if tx_keepalive.send(WriterCommand::Keepalive).await.is_err() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -336,9 +589,9 @@ impl MePool {
|
|||||||
dc: i32,
|
dc: i32,
|
||||||
mut addrs: Vec<(IpAddr, u16)>,
|
mut addrs: Vec<(IpAddr, u16)>,
|
||||||
rng: Arc<SecureRandom>,
|
rng: Arc<SecureRandom>,
|
||||||
) {
|
) -> bool {
|
||||||
if addrs.is_empty() {
|
if addrs.is_empty() {
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
addrs.shuffle(&mut rand::rng());
|
addrs.shuffle(&mut rand::rng());
|
||||||
for (ip, port) in addrs {
|
for (ip, port) in addrs {
|
||||||
@@ -346,20 +599,20 @@ impl MePool {
|
|||||||
match self.connect_one(addr, rng.as_ref()).await {
|
match self.connect_one(addr, rng.as_ref()).await {
|
||||||
Ok(()) => {
|
Ok(()) => {
|
||||||
info!(%addr, dc = %dc, "ME connected");
|
info!(%addr, dc = %dc, "ME connected");
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
Err(e) => warn!(%addr, dc = %dc, error = %e, "ME connect failed, trying next"),
|
Err(e) => warn!(%addr, dc = %dc, error = %e, "ME connect failed, trying next"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
warn!(dc = %dc, "All ME servers for DC failed at init");
|
warn!(dc = %dc, "All ME servers for DC failed at init");
|
||||||
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn remove_writer_and_reroute(&self, writer_id: u64) {
|
pub(crate) async fn remove_writer_and_close_clients(self: &Arc<Self>, writer_id: u64) {
|
||||||
let mut queue = self.remove_writer_only(writer_id).await;
|
let conns = self.remove_writer_only(writer_id).await;
|
||||||
while let Some(bound) = queue.pop() {
|
for bound in conns {
|
||||||
if !self.reroute_conn(&bound, &mut queue).await {
|
let _ = self.registry.route(bound.conn_id, super::MeResponse::Close).await;
|
||||||
let _ = self.registry.route(bound.conn_id, super::MeResponse::Close).await;
|
let _ = self.registry.unregister(bound.conn_id).await;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -369,84 +622,42 @@ impl MePool {
|
|||||||
if let Some(pos) = ws.iter().position(|w| w.id == writer_id) {
|
if let Some(pos) = ws.iter().position(|w| w.id == writer_id) {
|
||||||
let w = ws.remove(pos);
|
let w = ws.remove(pos);
|
||||||
w.cancel.cancel();
|
w.cancel.cancel();
|
||||||
|
let _ = w.tx.send(WriterCommand::Close).await;
|
||||||
|
self.conn_count.fetch_sub(1, Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
self.rtt_stats.lock().await.remove(&writer_id);
|
||||||
self.registry.writer_lost(writer_id).await
|
self.registry.writer_lost(writer_id).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn reroute_conn(&self, bound: &BoundConn, backlog: &mut Vec<BoundConn>) -> bool {
|
pub(crate) async fn mark_writer_draining(self: &Arc<Self>, writer_id: u64) {
|
||||||
let payload = super::wire::build_proxy_req_payload(
|
{
|
||||||
bound.conn_id,
|
let mut ws = self.writers.write().await;
|
||||||
bound.meta.client_addr,
|
if let Some(w) = ws.iter_mut().find(|w| w.id == writer_id) {
|
||||||
bound.meta.our_addr,
|
w.draining.store(true, Ordering::Relaxed);
|
||||||
&[],
|
|
||||||
self.proxy_tag.as_deref(),
|
|
||||||
bound.meta.proto_flags,
|
|
||||||
);
|
|
||||||
|
|
||||||
let mut attempts = 0;
|
|
||||||
loop {
|
|
||||||
let writers_snapshot = {
|
|
||||||
let ws = self.writers.read().await;
|
|
||||||
if ws.is_empty() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
ws.clone()
|
|
||||||
};
|
|
||||||
let mut candidates = self.candidate_indices_for_dc(&writers_snapshot, bound.meta.target_dc).await;
|
|
||||||
if candidates.is_empty() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
candidates.sort_by_key(|idx| {
|
|
||||||
writers_snapshot[*idx]
|
|
||||||
.degraded
|
|
||||||
.load(Ordering::Relaxed)
|
|
||||||
.then_some(1usize)
|
|
||||||
.unwrap_or(0)
|
|
||||||
});
|
|
||||||
let start = self.rr.fetch_add(1, Ordering::Relaxed) as usize % candidates.len();
|
|
||||||
|
|
||||||
for offset in 0..candidates.len() {
|
|
||||||
let idx = candidates[(start + offset) % candidates.len()];
|
|
||||||
let w = &writers_snapshot[idx];
|
|
||||||
if let Ok(mut guard) = w.writer.try_lock() {
|
|
||||||
let send_res = guard.send(&payload).await;
|
|
||||||
drop(guard);
|
|
||||||
match send_res {
|
|
||||||
Ok(()) => {
|
|
||||||
self.registry
|
|
||||||
.bind_writer(bound.conn_id, w.id, w.writer.clone(), bound.meta.clone())
|
|
||||||
.await;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
warn!(error = %e, writer_id = w.id, "ME reroute send failed");
|
|
||||||
backlog.extend(self.remove_writer_only(w.id).await);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let w = writers_snapshot[candidates[start]].clone();
|
|
||||||
match w.writer.lock().await.send(&payload).await {
|
|
||||||
Ok(()) => {
|
|
||||||
self.registry
|
|
||||||
.bind_writer(bound.conn_id, w.id, w.writer.clone(), bound.meta.clone())
|
|
||||||
.await;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
warn!(error = %e, writer_id = w.id, "ME reroute send failed (blocking)");
|
|
||||||
backlog.extend(self.remove_writer_only(w.id).await);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
attempts += 1;
|
|
||||||
if attempts > 3 {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let pool = Arc::downgrade(self);
|
||||||
|
tokio::spawn(async move {
|
||||||
|
let deadline = Instant::now() + Duration::from_secs(300);
|
||||||
|
loop {
|
||||||
|
if let Some(p) = pool.upgrade() {
|
||||||
|
if Instant::now() >= deadline {
|
||||||
|
warn!(writer_id, "Drain timeout, force-closing");
|
||||||
|
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;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +1,17 @@
|
|||||||
use std::net::{IpAddr, Ipv4Addr};
|
use std::net::{IpAddr, Ipv4Addr};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use tracing::{info, warn};
|
use tracing::{info, warn, debug};
|
||||||
|
|
||||||
use crate::error::{ProxyError, Result};
|
use crate::error::{ProxyError, Result};
|
||||||
|
use crate::network::probe::is_bogon;
|
||||||
|
use crate::network::stun::{stun_probe_dual, IpFamily, StunProbeResult};
|
||||||
|
|
||||||
use super::MePool;
|
use super::MePool;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
pub async fn stun_probe(stun_addr: Option<String>) -> Result<crate::network::stun::DualStunResult> {
|
||||||
#[derive(Debug, Clone, Copy)]
|
|
||||||
pub struct StunProbeResult {
|
|
||||||
pub local_addr: std::net::SocketAddr,
|
|
||||||
pub reflected_addr: std::net::SocketAddr,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn stun_probe(stun_addr: Option<String>) -> Result<Option<StunProbeResult>> {
|
|
||||||
let stun_addr = stun_addr.unwrap_or_else(|| "stun.l.google.com:19302".to_string());
|
let stun_addr = stun_addr.unwrap_or_else(|| "stun.l.google.com:19302".to_string());
|
||||||
fetch_stun_binding(&stun_addr).await
|
stun_probe_dual(&stun_addr).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn detect_public_ip() -> Option<IpAddr> {
|
pub async fn detect_public_ip() -> Option<IpAddr> {
|
||||||
@@ -35,7 +30,7 @@ impl MePool {
|
|||||||
|
|
||||||
match (ip, nat_ip) {
|
match (ip, nat_ip) {
|
||||||
(IpAddr::V4(src), IpAddr::V4(dst))
|
(IpAddr::V4(src), IpAddr::V4(dst))
|
||||||
if is_privateish(IpAddr::V4(src))
|
if is_bogon(IpAddr::V4(src))
|
||||||
|| src.is_loopback()
|
|| src.is_loopback()
|
||||||
|| src.is_unspecified() =>
|
|| src.is_unspecified() =>
|
||||||
{
|
{
|
||||||
@@ -55,7 +50,7 @@ impl MePool {
|
|||||||
) -> std::net::SocketAddr {
|
) -> std::net::SocketAddr {
|
||||||
let ip = if let Some(r) = reflected {
|
let ip = if let Some(r) = reflected {
|
||||||
// Use reflected IP (not port) only when local address is non-public.
|
// Use reflected IP (not port) only when local address is non-public.
|
||||||
if is_privateish(addr.ip()) || addr.ip().is_loopback() || addr.ip().is_unspecified() {
|
if is_bogon(addr.ip()) || addr.ip().is_loopback() || addr.ip().is_unspecified() {
|
||||||
r.ip()
|
r.ip()
|
||||||
} else {
|
} else {
|
||||||
self.translate_ip_for_nat(addr.ip())
|
self.translate_ip_for_nat(addr.ip())
|
||||||
@@ -73,7 +68,7 @@ impl MePool {
|
|||||||
return self.nat_ip_cfg;
|
return self.nat_ip_cfg;
|
||||||
}
|
}
|
||||||
|
|
||||||
if !(is_privateish(local_ip) || local_ip.is_loopback() || local_ip.is_unspecified()) {
|
if !(is_bogon(local_ip) || local_ip.is_loopback() || local_ip.is_unspecified()) {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,37 +93,74 @@ impl MePool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) async fn maybe_reflect_public_addr(&self) -> Option<std::net::SocketAddr> {
|
pub(super) async fn maybe_reflect_public_addr(
|
||||||
|
&self,
|
||||||
|
family: IpFamily,
|
||||||
|
) -> Option<std::net::SocketAddr> {
|
||||||
const STUN_CACHE_TTL: Duration = Duration::from_secs(600);
|
const STUN_CACHE_TTL: Duration = Duration::from_secs(600);
|
||||||
|
// Backoff window
|
||||||
|
if let Some(until) = *self.stun_backoff_until.read().await {
|
||||||
|
if Instant::now() < until {
|
||||||
|
if let Ok(cache) = self.nat_reflection_cache.try_lock() {
|
||||||
|
let slot = match family {
|
||||||
|
IpFamily::V4 => cache.v4,
|
||||||
|
IpFamily::V6 => cache.v6,
|
||||||
|
};
|
||||||
|
return slot.map(|(_, addr)| addr);
|
||||||
|
}
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if let Ok(mut cache) = self.nat_reflection_cache.try_lock() {
|
if let Ok(mut cache) = self.nat_reflection_cache.try_lock() {
|
||||||
if let Some((ts, addr)) = *cache {
|
let slot = match family {
|
||||||
|
IpFamily::V4 => &mut cache.v4,
|
||||||
|
IpFamily::V6 => &mut cache.v6,
|
||||||
|
};
|
||||||
|
if let Some((ts, addr)) = slot {
|
||||||
if ts.elapsed() < STUN_CACHE_TTL {
|
if ts.elapsed() < STUN_CACHE_TTL {
|
||||||
return Some(addr);
|
return Some(*addr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let stun_addr = self
|
let attempt = self.nat_probe_attempts.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||||
.nat_stun
|
let servers = if !self.nat_stun_servers.is_empty() {
|
||||||
.clone()
|
self.nat_stun_servers.clone()
|
||||||
.unwrap_or_else(|| "stun.l.google.com:19302".to_string());
|
} else if let Some(s) = &self.nat_stun {
|
||||||
match fetch_stun_binding(&stun_addr).await {
|
vec![s.clone()]
|
||||||
Ok(sa) => {
|
} else {
|
||||||
if let Some(result) = sa {
|
vec!["stun.l.google.com:19302".to_string()]
|
||||||
info!(local = %result.local_addr, reflected = %result.reflected_addr, "NAT probe: reflected address");
|
};
|
||||||
if let Ok(mut cache) = self.nat_reflection_cache.try_lock() {
|
|
||||||
*cache = Some((Instant::now(), result.reflected_addr));
|
for stun_addr in servers {
|
||||||
|
match stun_probe_dual(&stun_addr).await {
|
||||||
|
Ok(res) => {
|
||||||
|
let picked: Option<StunProbeResult> = match family {
|
||||||
|
IpFamily::V4 => res.v4,
|
||||||
|
IpFamily::V6 => res.v6,
|
||||||
|
};
|
||||||
|
if let Some(result) = picked {
|
||||||
|
info!(local = %result.local_addr, reflected = %result.reflected_addr, family = ?family, stun = %stun_addr, "NAT probe: reflected address");
|
||||||
|
self.nat_probe_attempts.store(0, std::sync::atomic::Ordering::Relaxed);
|
||||||
|
if 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(), result.reflected_addr));
|
||||||
|
}
|
||||||
|
return Some(result.reflected_addr);
|
||||||
}
|
}
|
||||||
Some(result.reflected_addr)
|
}
|
||||||
} else {
|
Err(e) => {
|
||||||
None
|
warn!(error = %e, stun = %stun_addr, attempt = attempt + 1, "NAT probe failed, trying next server");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
|
||||||
warn!(error = %e, "NAT probe failed");
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
let backoff = Duration::from_secs(60 * 2u64.pow((attempt as u32).min(6)));
|
||||||
|
*self.stun_backoff_until.write().await = Some(Instant::now() + backoff);
|
||||||
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,98 +190,3 @@ async fn fetch_public_ipv4_once(url: &str) -> Result<Option<Ipv4Addr>> {
|
|||||||
let ip = text.trim().parse().ok();
|
let ip = text.trim().parse().ok();
|
||||||
Ok(ip)
|
Ok(ip)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn fetch_stun_binding(stun_addr: &str) -> Result<Option<StunProbeResult>> {
|
|
||||||
use rand::RngCore;
|
|
||||||
use tokio::net::UdpSocket;
|
|
||||||
|
|
||||||
let socket = UdpSocket::bind("0.0.0.0:0")
|
|
||||||
.await
|
|
||||||
.map_err(|e| ProxyError::Proxy(format!("STUN bind failed: {e}")))?;
|
|
||||||
socket
|
|
||||||
.connect(stun_addr)
|
|
||||||
.await
|
|
||||||
.map_err(|e| ProxyError::Proxy(format!("STUN connect failed: {e}")))?;
|
|
||||||
|
|
||||||
// Build minimal Binding Request.
|
|
||||||
let mut req = vec![0u8; 20];
|
|
||||||
req[0..2].copy_from_slice(&0x0001u16.to_be_bytes()); // Binding Request
|
|
||||||
req[2..4].copy_from_slice(&0u16.to_be_bytes()); // length
|
|
||||||
req[4..8].copy_from_slice(&0x2112A442u32.to_be_bytes()); // magic cookie
|
|
||||||
rand::rng().fill_bytes(&mut req[8..20]);
|
|
||||||
|
|
||||||
socket
|
|
||||||
.send(&req)
|
|
||||||
.await
|
|
||||||
.map_err(|e| ProxyError::Proxy(format!("STUN send failed: {e}")))?;
|
|
||||||
|
|
||||||
let mut buf = [0u8; 128];
|
|
||||||
let n = socket
|
|
||||||
.recv(&mut buf)
|
|
||||||
.await
|
|
||||||
.map_err(|e| ProxyError::Proxy(format!("STUN recv failed: {e}")))?;
|
|
||||||
if n < 20 {
|
|
||||||
return Ok(None);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse attributes.
|
|
||||||
let mut idx = 20;
|
|
||||||
while idx + 4 <= n {
|
|
||||||
let atype = u16::from_be_bytes(buf[idx..idx + 2].try_into().unwrap());
|
|
||||||
let alen = u16::from_be_bytes(buf[idx + 2..idx + 4].try_into().unwrap()) as usize;
|
|
||||||
idx += 4;
|
|
||||||
if idx + alen > n {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
match atype {
|
|
||||||
0x0020 /* XOR-MAPPED-ADDRESS */ | 0x0001 /* MAPPED-ADDRESS */ => {
|
|
||||||
if alen < 8 {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
let family = buf[idx + 1];
|
|
||||||
if family != 0x01 {
|
|
||||||
// only IPv4 supported here
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
let port_bytes = [buf[idx + 2], buf[idx + 3]];
|
|
||||||
let ip_bytes = [buf[idx + 4], buf[idx + 5], buf[idx + 6], buf[idx + 7]];
|
|
||||||
|
|
||||||
let (port, ip) = if atype == 0x0020 {
|
|
||||||
let magic = 0x2112A442u32.to_be_bytes();
|
|
||||||
let port = u16::from_be_bytes(port_bytes) ^ ((magic[0] as u16) << 8 | magic[1] as u16);
|
|
||||||
let ip = [
|
|
||||||
ip_bytes[0] ^ magic[0],
|
|
||||||
ip_bytes[1] ^ magic[1],
|
|
||||||
ip_bytes[2] ^ magic[2],
|
|
||||||
ip_bytes[3] ^ magic[3],
|
|
||||||
];
|
|
||||||
(port, ip)
|
|
||||||
} else {
|
|
||||||
(u16::from_be_bytes(port_bytes), ip_bytes)
|
|
||||||
};
|
|
||||||
let reflected = std::net::SocketAddr::new(
|
|
||||||
IpAddr::V4(Ipv4Addr::new(ip[0], ip[1], ip[2], ip[3])),
|
|
||||||
port,
|
|
||||||
);
|
|
||||||
let local_addr = socket.local_addr().map_err(|e| {
|
|
||||||
ProxyError::Proxy(format!("STUN local_addr failed: {e}"))
|
|
||||||
})?;
|
|
||||||
return Ok(Some(StunProbeResult {
|
|
||||||
local_addr,
|
|
||||||
reflected_addr: reflected,
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
idx += (alen + 3) & !3; // 4-byte alignment
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_privateish(ip: IpAddr) -> bool {
|
|
||||||
match ip {
|
|
||||||
IpAddr::V4(v4) => v4.is_private() || v4.is_link_local(),
|
|
||||||
IpAddr::V6(v6) => v6.is_unique_local(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ use std::time::Instant;
|
|||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use tokio::io::AsyncReadExt;
|
use tokio::io::AsyncReadExt;
|
||||||
use tokio::net::TcpStream;
|
use tokio::net::TcpStream;
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::{Mutex, mpsc};
|
||||||
use tokio_util::sync::CancellationToken;
|
use tokio_util::sync::CancellationToken;
|
||||||
use tracing::{debug, trace, warn};
|
use tracing::{debug, trace, warn};
|
||||||
|
|
||||||
@@ -14,7 +14,7 @@ use crate::crypto::{AesCbc, crc32};
|
|||||||
use crate::error::{ProxyError, Result};
|
use crate::error::{ProxyError, Result};
|
||||||
use crate::protocol::constants::*;
|
use crate::protocol::constants::*;
|
||||||
|
|
||||||
use super::codec::RpcWriter;
|
use super::codec::WriterCommand;
|
||||||
use super::{ConnRegistry, MeResponse};
|
use super::{ConnRegistry, MeResponse};
|
||||||
|
|
||||||
pub(crate) async fn reader_loop(
|
pub(crate) async fn reader_loop(
|
||||||
@@ -24,7 +24,7 @@ pub(crate) async fn reader_loop(
|
|||||||
reg: Arc<ConnRegistry>,
|
reg: Arc<ConnRegistry>,
|
||||||
enc_leftover: BytesMut,
|
enc_leftover: BytesMut,
|
||||||
mut dec: BytesMut,
|
mut dec: BytesMut,
|
||||||
writer: Arc<Mutex<RpcWriter>>,
|
tx: mpsc::Sender<WriterCommand>,
|
||||||
ping_tracker: Arc<Mutex<HashMap<i64, (Instant, u64)>>>,
|
ping_tracker: Arc<Mutex<HashMap<i64, (Instant, u64)>>>,
|
||||||
rtt_stats: Arc<Mutex<HashMap<u64, (f64, f64)>>>,
|
rtt_stats: Arc<Mutex<HashMap<u64, (f64, f64)>>>,
|
||||||
_writer_id: u64,
|
_writer_id: u64,
|
||||||
@@ -33,6 +33,8 @@ pub(crate) async fn reader_loop(
|
|||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let mut raw = enc_leftover;
|
let mut raw = enc_leftover;
|
||||||
let mut expected_seq: i32 = 0;
|
let mut expected_seq: i32 = 0;
|
||||||
|
let mut crc_errors = 0u32;
|
||||||
|
let mut seq_mismatch = 0u32;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let mut tmp = [0u8; 16_384];
|
let mut tmp = [0u8; 16_384];
|
||||||
@@ -80,12 +82,20 @@ pub(crate) async fn reader_loop(
|
|||||||
let ec = u32::from_le_bytes(frame[pe..pe + 4].try_into().unwrap());
|
let ec = u32::from_le_bytes(frame[pe..pe + 4].try_into().unwrap());
|
||||||
if crc32(&frame[..pe]) != ec {
|
if crc32(&frame[..pe]) != ec {
|
||||||
warn!("CRC mismatch in data frame");
|
warn!("CRC mismatch in data frame");
|
||||||
|
crc_errors += 1;
|
||||||
|
if crc_errors > 3 {
|
||||||
|
return Err(ProxyError::Proxy("Too many CRC mismatches".into()));
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let seq_no = i32::from_le_bytes(frame[4..8].try_into().unwrap());
|
let seq_no = i32::from_le_bytes(frame[4..8].try_into().unwrap());
|
||||||
if seq_no != expected_seq {
|
if seq_no != expected_seq {
|
||||||
warn!(seq_no, expected = expected_seq, "ME RPC seq mismatch");
|
warn!(seq_no, expected = expected_seq, "ME RPC seq mismatch");
|
||||||
|
seq_mismatch += 1;
|
||||||
|
if seq_mismatch > 10 {
|
||||||
|
return Err(ProxyError::Proxy("Too many seq mismatches".into()));
|
||||||
|
}
|
||||||
expected_seq = seq_no.wrapping_add(1);
|
expected_seq = seq_no.wrapping_add(1);
|
||||||
} else {
|
} else {
|
||||||
expected_seq = expected_seq.wrapping_add(1);
|
expected_seq = expected_seq.wrapping_add(1);
|
||||||
@@ -108,7 +118,7 @@ pub(crate) async fn reader_loop(
|
|||||||
let routed = reg.route(cid, MeResponse::Data { flags, data }).await;
|
let routed = reg.route(cid, MeResponse::Data { flags, data }).await;
|
||||||
if !routed {
|
if !routed {
|
||||||
reg.unregister(cid).await;
|
reg.unregister(cid).await;
|
||||||
send_close_conn(&writer, cid).await;
|
send_close_conn(&tx, cid).await;
|
||||||
}
|
}
|
||||||
} else if pt == RPC_SIMPLE_ACK_U32 && body.len() >= 12 {
|
} else if pt == RPC_SIMPLE_ACK_U32 && body.len() >= 12 {
|
||||||
let cid = u64::from_le_bytes(body[0..8].try_into().unwrap());
|
let cid = u64::from_le_bytes(body[0..8].try_into().unwrap());
|
||||||
@@ -118,7 +128,7 @@ pub(crate) async fn reader_loop(
|
|||||||
let routed = reg.route(cid, MeResponse::Ack(cfm)).await;
|
let routed = reg.route(cid, MeResponse::Ack(cfm)).await;
|
||||||
if !routed {
|
if !routed {
|
||||||
reg.unregister(cid).await;
|
reg.unregister(cid).await;
|
||||||
send_close_conn(&writer, cid).await;
|
send_close_conn(&tx, cid).await;
|
||||||
}
|
}
|
||||||
} else if pt == RPC_CLOSE_EXT_U32 && body.len() >= 8 {
|
} else if pt == RPC_CLOSE_EXT_U32 && body.len() >= 8 {
|
||||||
let cid = u64::from_le_bytes(body[0..8].try_into().unwrap());
|
let cid = u64::from_le_bytes(body[0..8].try_into().unwrap());
|
||||||
@@ -136,8 +146,8 @@ pub(crate) async fn reader_loop(
|
|||||||
let mut pong = Vec::with_capacity(12);
|
let mut pong = Vec::with_capacity(12);
|
||||||
pong.extend_from_slice(&RPC_PONG_U32.to_le_bytes());
|
pong.extend_from_slice(&RPC_PONG_U32.to_le_bytes());
|
||||||
pong.extend_from_slice(&ping_id.to_le_bytes());
|
pong.extend_from_slice(&ping_id.to_le_bytes());
|
||||||
if let Err(e) = writer.lock().await.send(&pong).await {
|
if tx.send(WriterCommand::DataAndFlush(pong)).await.is_err() {
|
||||||
warn!(error = %e, "PONG send failed");
|
warn!("PONG send failed");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else if pt == RPC_PONG_U32 && body.len() >= 8 {
|
} else if pt == RPC_PONG_U32 && body.len() >= 8 {
|
||||||
@@ -152,6 +162,9 @@ pub(crate) async fn reader_loop(
|
|||||||
entry.1 = entry.1 * 0.8 + rtt * 0.2;
|
entry.1 = entry.1 * 0.8 + rtt * 0.2;
|
||||||
if rtt < entry.0 {
|
if rtt < entry.0 {
|
||||||
entry.0 = rtt;
|
entry.0 = rtt;
|
||||||
|
} else {
|
||||||
|
// allow slow baseline drift upward to avoid stale minimum
|
||||||
|
entry.0 = entry.0 * 0.99 + rtt * 0.01;
|
||||||
}
|
}
|
||||||
let degraded_now = entry.1 > entry.0 * 2.0;
|
let degraded_now = entry.1 > entry.0 * 2.0;
|
||||||
degraded.store(degraded_now, Ordering::Relaxed);
|
degraded.store(degraded_now, Ordering::Relaxed);
|
||||||
@@ -168,12 +181,10 @@ pub(crate) async fn reader_loop(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn send_close_conn(writer: &Arc<Mutex<RpcWriter>>, conn_id: u64) {
|
async fn send_close_conn(tx: &mpsc::Sender<WriterCommand>, conn_id: u64) {
|
||||||
let mut p = Vec::with_capacity(12);
|
let mut p = Vec::with_capacity(12);
|
||||||
p.extend_from_slice(&RPC_CLOSE_CONN_U32.to_le_bytes());
|
p.extend_from_slice(&RPC_CLOSE_CONN_U32.to_le_bytes());
|
||||||
p.extend_from_slice(&conn_id.to_le_bytes());
|
p.extend_from_slice(&conn_id.to_le_bytes());
|
||||||
|
|
||||||
if let Err(e) = writer.lock().await.send(&p).await {
|
let _ = tx.send(WriterCommand::DataAndFlush(p)).await;
|
||||||
debug!(conn_id, error = %e, "Failed to send RPC_CLOSE_CONN");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::{HashMap, HashSet};
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::sync::atomic::{AtomicU64, Ordering};
|
use std::sync::atomic::{AtomicU64, Ordering};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use tokio::sync::{mpsc, Mutex, RwLock};
|
use tokio::sync::{mpsc, Mutex, RwLock};
|
||||||
|
|
||||||
use super::codec::RpcWriter;
|
use super::codec::WriterCommand;
|
||||||
use super::MeResponse;
|
use super::MeResponse;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@@ -25,15 +25,31 @@ pub struct BoundConn {
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct ConnWriter {
|
pub struct ConnWriter {
|
||||||
pub writer_id: u64,
|
pub writer_id: u64,
|
||||||
pub writer: Arc<Mutex<RpcWriter>>,
|
pub tx: mpsc::Sender<WriterCommand>,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct RegistryInner {
|
||||||
|
map: HashMap<u64, mpsc::Sender<MeResponse>>,
|
||||||
|
writers: HashMap<u64, mpsc::Sender<WriterCommand>>,
|
||||||
|
writer_for_conn: HashMap<u64, u64>,
|
||||||
|
conns_for_writer: HashMap<u64, HashSet<u64>>,
|
||||||
|
meta: HashMap<u64, ConnMeta>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RegistryInner {
|
||||||
|
fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
map: HashMap::new(),
|
||||||
|
writers: HashMap::new(),
|
||||||
|
writer_for_conn: HashMap::new(),
|
||||||
|
conns_for_writer: HashMap::new(),
|
||||||
|
meta: HashMap::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ConnRegistry {
|
pub struct ConnRegistry {
|
||||||
map: RwLock<HashMap<u64, mpsc::Sender<MeResponse>>>,
|
inner: RwLock<RegistryInner>,
|
||||||
writers: RwLock<HashMap<u64, Arc<Mutex<RpcWriter>>>>,
|
|
||||||
writer_for_conn: RwLock<HashMap<u64, u64>>,
|
|
||||||
conns_for_writer: RwLock<HashMap<u64, Vec<u64>>>,
|
|
||||||
meta: RwLock<HashMap<u64, ConnMeta>>,
|
|
||||||
next_id: AtomicU64,
|
next_id: AtomicU64,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,11 +57,7 @@ impl ConnRegistry {
|
|||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let start = rand::random::<u64>() | 1;
|
let start = rand::random::<u64>() | 1;
|
||||||
Self {
|
Self {
|
||||||
map: RwLock::new(HashMap::new()),
|
inner: RwLock::new(RegistryInner::new()),
|
||||||
writers: RwLock::new(HashMap::new()),
|
|
||||||
writer_for_conn: RwLock::new(HashMap::new()),
|
|
||||||
conns_for_writer: RwLock::new(HashMap::new()),
|
|
||||||
meta: RwLock::new(HashMap::new()),
|
|
||||||
next_id: AtomicU64::new(start),
|
next_id: AtomicU64::new(start),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -53,23 +65,27 @@ impl ConnRegistry {
|
|||||||
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(1024);
|
let (tx, rx) = mpsc::channel(1024);
|
||||||
self.map.write().await.insert(id, tx);
|
self.inner.write().await.map.insert(id, tx);
|
||||||
(id, rx)
|
(id, rx)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn unregister(&self, id: u64) {
|
/// Unregister connection, returning associated writer_id if any.
|
||||||
self.map.write().await.remove(&id);
|
pub async fn unregister(&self, id: u64) -> Option<u64> {
|
||||||
self.meta.write().await.remove(&id);
|
let mut inner = self.inner.write().await;
|
||||||
if let Some(writer_id) = self.writer_for_conn.write().await.remove(&id) {
|
inner.map.remove(&id);
|
||||||
if let Some(list) = self.conns_for_writer.write().await.get_mut(&writer_id) {
|
inner.meta.remove(&id);
|
||||||
list.retain(|c| *c != id);
|
if let Some(writer_id) = inner.writer_for_conn.remove(&id) {
|
||||||
|
if let Some(set) = inner.conns_for_writer.get_mut(&writer_id) {
|
||||||
|
set.remove(&id);
|
||||||
}
|
}
|
||||||
|
return Some(writer_id);
|
||||||
}
|
}
|
||||||
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn route(&self, id: u64, resp: MeResponse) -> bool {
|
pub async fn route(&self, id: u64, resp: MeResponse) -> bool {
|
||||||
let m = self.map.read().await;
|
let inner = self.inner.read().await;
|
||||||
if let Some(tx) = m.get(&id) {
|
if let Some(tx) = inner.map.get(&id) {
|
||||||
tx.try_send(resp).is_ok()
|
tx.try_send(resp).is_ok()
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
@@ -80,43 +96,41 @@ impl ConnRegistry {
|
|||||||
&self,
|
&self,
|
||||||
conn_id: u64,
|
conn_id: u64,
|
||||||
writer_id: u64,
|
writer_id: u64,
|
||||||
writer: Arc<Mutex<RpcWriter>>,
|
tx: mpsc::Sender<WriterCommand>,
|
||||||
meta: ConnMeta,
|
meta: ConnMeta,
|
||||||
) {
|
) {
|
||||||
self.meta.write().await.entry(conn_id).or_insert(meta);
|
let mut inner = self.inner.write().await;
|
||||||
self.writer_for_conn.write().await.insert(conn_id, writer_id);
|
inner.meta.entry(conn_id).or_insert(meta);
|
||||||
self.writers.write().await.entry(writer_id).or_insert_with(|| writer.clone());
|
inner.writer_for_conn.insert(conn_id, writer_id);
|
||||||
self.conns_for_writer
|
inner.writers.entry(writer_id).or_insert_with(|| tx.clone());
|
||||||
.write()
|
inner
|
||||||
.await
|
.conns_for_writer
|
||||||
.entry(writer_id)
|
.entry(writer_id)
|
||||||
.or_insert_with(Vec::new)
|
.or_insert_with(HashSet::new)
|
||||||
.push(conn_id);
|
.insert(conn_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_writer(&self, conn_id: u64) -> Option<ConnWriter> {
|
pub async fn get_writer(&self, conn_id: u64) -> Option<ConnWriter> {
|
||||||
let writer_id = {
|
let inner = self.inner.read().await;
|
||||||
let guard = self.writer_for_conn.read().await;
|
let writer_id = inner.writer_for_conn.get(&conn_id).cloned()?;
|
||||||
guard.get(&conn_id).cloned()
|
let writer = inner.writers.get(&writer_id).cloned()?;
|
||||||
}?;
|
Some(ConnWriter { writer_id, tx: writer })
|
||||||
let writer = {
|
|
||||||
let guard = self.writers.read().await;
|
|
||||||
guard.get(&writer_id).cloned()
|
|
||||||
}?;
|
|
||||||
Some(ConnWriter { writer_id, writer })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn writer_lost(&self, writer_id: u64) -> Vec<BoundConn> {
|
pub async fn writer_lost(&self, writer_id: u64) -> Vec<BoundConn> {
|
||||||
self.writers.write().await.remove(&writer_id);
|
let mut inner = self.inner.write().await;
|
||||||
let conns = self.conns_for_writer.write().await.remove(&writer_id).unwrap_or_default();
|
inner.writers.remove(&writer_id);
|
||||||
|
let conns = inner
|
||||||
|
.conns_for_writer
|
||||||
|
.remove(&writer_id)
|
||||||
|
.unwrap_or_default()
|
||||||
|
.into_iter()
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
let mut out = Vec::new();
|
let mut out = Vec::new();
|
||||||
let mut writer_for_conn = self.writer_for_conn.write().await;
|
|
||||||
let meta = self.meta.read().await;
|
|
||||||
|
|
||||||
for conn_id in conns {
|
for conn_id in conns {
|
||||||
writer_for_conn.remove(&conn_id);
|
inner.writer_for_conn.remove(&conn_id);
|
||||||
if let Some(m) = meta.get(&conn_id) {
|
if let Some(m) = inner.meta.get(&conn_id) {
|
||||||
out.push(BoundConn {
|
out.push(BoundConn {
|
||||||
conn_id,
|
conn_id,
|
||||||
meta: m.clone(),
|
meta: m.clone(),
|
||||||
@@ -127,7 +141,16 @@ impl ConnRegistry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_meta(&self, conn_id: u64) -> Option<ConnMeta> {
|
pub async fn get_meta(&self, conn_id: u64) -> Option<ConnMeta> {
|
||||||
let guard = self.meta.read().await;
|
let inner = self.inner.read().await;
|
||||||
guard.get(&conn_id).cloned()
|
inner.meta.get(&conn_id).cloned()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn is_writer_empty(&self, writer_id: u64) -> bool {
|
||||||
|
let inner = self.inner.read().await;
|
||||||
|
inner
|
||||||
|
.conns_for_writer
|
||||||
|
.get(&writer_id)
|
||||||
|
.map(|s| s.is_empty())
|
||||||
|
.unwrap_or(true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use std::sync::atomic::Ordering;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use tracing::{info, warn};
|
use tracing::{info, warn};
|
||||||
@@ -15,7 +16,12 @@ pub async fn me_rotation_task(pool: Arc<MePool>, rng: Arc<SecureRandom>, interva
|
|||||||
|
|
||||||
let candidate = {
|
let candidate = {
|
||||||
let ws = pool.writers.read().await;
|
let ws = pool.writers.read().await;
|
||||||
ws.get(0).cloned()
|
if ws.is_empty() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
let idx = (pool.rr.load(std::sync::atomic::Ordering::Relaxed) as usize) % ws.len();
|
||||||
|
ws.get(idx).cloned()
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let Some(w) = candidate else {
|
let Some(w) = candidate else {
|
||||||
@@ -25,8 +31,17 @@ pub async fn me_rotation_task(pool: Arc<MePool>, rng: Arc<SecureRandom>, interva
|
|||||||
info!(addr = %w.addr, writer_id = w.id, "Rotating ME connection");
|
info!(addr = %w.addr, writer_id = w.id, "Rotating ME connection");
|
||||||
match pool.connect_one(w.addr, rng.as_ref()).await {
|
match pool.connect_one(w.addr, rng.as_ref()).await {
|
||||||
Ok(()) => {
|
Ok(()) => {
|
||||||
// Remove old writer after new one is up.
|
tokio::time::sleep(Duration::from_secs(2)).await;
|
||||||
pool.remove_writer_and_reroute(w.id).await;
|
let ws = pool.writers.read().await;
|
||||||
|
let new_alive = ws.iter().any(|nw|
|
||||||
|
nw.id != w.id && nw.addr == w.addr && !nw.degraded.load(Ordering::Relaxed) && !nw.draining.load(Ordering::Relaxed)
|
||||||
|
);
|
||||||
|
drop(ws);
|
||||||
|
if new_alive {
|
||||||
|
pool.mark_writer_draining(w.id).await;
|
||||||
|
} else {
|
||||||
|
warn!(addr = %w.addr, writer_id = w.id, "New writer died, keeping old");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
warn!(addr = %w.addr, writer_id = w.id, error = %e, "ME rotation connect failed");
|
warn!(addr = %w.addr, writer_id = w.id, error = %e, "ME rotation connect failed");
|
||||||
@@ -34,4 +49,3 @@ pub async fn me_rotation_task(pool: Arc<MePool>, rng: Arc<SecureRandom>, interva
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,15 +3,15 @@ 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::Mutex;
|
|
||||||
use tracing::{debug, warn};
|
use tracing::{debug, warn};
|
||||||
|
|
||||||
use crate::error::{ProxyError, Result};
|
use crate::error::{ProxyError, Result};
|
||||||
|
use crate::network::IpFamily;
|
||||||
use crate::protocol::constants::RPC_CLOSE_EXT_U32;
|
use crate::protocol::constants::RPC_CLOSE_EXT_U32;
|
||||||
|
|
||||||
use super::MePool;
|
use super::MePool;
|
||||||
|
use super::codec::WriterCommand;
|
||||||
use super::wire::build_proxy_req_payload;
|
use super::wire::build_proxy_req_payload;
|
||||||
use crate::crypto::SecureRandom;
|
|
||||||
use rand::seq::SliceRandom;
|
use rand::seq::SliceRandom;
|
||||||
use super::registry::ConnMeta;
|
use super::registry::ConnMeta;
|
||||||
|
|
||||||
@@ -44,19 +44,16 @@ 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 = {
|
let send_res = {
|
||||||
if let Ok(mut guard) = current.writer.try_lock() {
|
current
|
||||||
let r = guard.send(&payload).await;
|
.tx
|
||||||
drop(guard);
|
.send(WriterCommand::Data(payload.clone()))
|
||||||
r
|
.await
|
||||||
} else {
|
|
||||||
current.writer.lock().await.send(&payload).await
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
match send_res {
|
match send_res {
|
||||||
Ok(()) => return Ok(()),
|
Ok(()) => return Ok(()),
|
||||||
Err(e) => {
|
Err(_) => {
|
||||||
warn!(error = %e, writer_id = current.writer_id, "ME write failed");
|
warn!(writer_id = current.writer_id, "ME writer channel closed");
|
||||||
self.remove_writer_and_reroute(current.writer_id).await;
|
self.remove_writer_and_close_clients(current.writer_id).await;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -65,7 +62,26 @@ impl MePool {
|
|||||||
let mut writers_snapshot = {
|
let mut writers_snapshot = {
|
||||||
let ws = self.writers.read().await;
|
let ws = self.writers.read().await;
|
||||||
if ws.is_empty() {
|
if ws.is_empty() {
|
||||||
return Err(ProxyError::Proxy("All ME connections dead".into()));
|
drop(ws);
|
||||||
|
for family in self.family_order() {
|
||||||
|
let map = match family {
|
||||||
|
IpFamily::V4 => self.proxy_map_v4.read().await.clone(),
|
||||||
|
IpFamily::V6 => self.proxy_map_v6.read().await.clone(),
|
||||||
|
};
|
||||||
|
for (_dc, addrs) in map.iter() {
|
||||||
|
for (ip, port) in addrs {
|
||||||
|
let addr = SocketAddr::new(*ip, *port);
|
||||||
|
if self.connect_one(addr, self.rng.as_ref()).await.is_ok() {
|
||||||
|
self.writer_available.notify_waiters();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if tokio::time::timeout(Duration::from_secs(3), self.writer_available.notified()).await.is_err() {
|
||||||
|
return Err(ProxyError::Proxy("All ME connections dead (waited 3s)".into()));
|
||||||
|
}
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
ws.clone()
|
ws.clone()
|
||||||
};
|
};
|
||||||
@@ -77,22 +93,30 @@ 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()));
|
||||||
}
|
}
|
||||||
emergency_attempts += 1;
|
emergency_attempts += 1;
|
||||||
let map = self.proxy_map_v4.read().await;
|
for family in self.family_order() {
|
||||||
if let Some(addrs) = map.get(&(target_dc as i32)) {
|
let map_guard = match family {
|
||||||
let mut shuffled = addrs.clone();
|
IpFamily::V4 => self.proxy_map_v4.read().await,
|
||||||
shuffled.shuffle(&mut rand::rng());
|
IpFamily::V6 => self.proxy_map_v6.read().await,
|
||||||
drop(map);
|
};
|
||||||
for (ip, port) in shuffled {
|
if let Some(addrs) = map_guard.get(&(target_dc as i32)) {
|
||||||
let addr = SocketAddr::new(ip, port);
|
let mut shuffled = addrs.clone();
|
||||||
if self.connect_one(addr, &SecureRandom::new()).await.is_ok() {
|
shuffled.shuffle(&mut rand::rng());
|
||||||
|
drop(map_guard);
|
||||||
|
for (ip, port) in shuffled {
|
||||||
|
let addr = SocketAddr::new(ip, port);
|
||||||
|
if self.connect_one(addr, self.rng.as_ref()).await.is_ok() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tokio::time::sleep(Duration::from_millis(100 * emergency_attempts)).await;
|
||||||
|
let ws2 = self.writers.read().await;
|
||||||
|
writers_snapshot = ws2.clone();
|
||||||
|
drop(ws2);
|
||||||
|
candidate_indices = self.candidate_indices_for_dc(&writers_snapshot, target_dc).await;
|
||||||
|
if !candidate_indices.is_empty() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tokio::time::sleep(Duration::from_millis(100 * emergency_attempts)).await;
|
|
||||||
let ws2 = self.writers.read().await;
|
|
||||||
writers_snapshot = ws2.clone();
|
|
||||||
drop(ws2);
|
|
||||||
candidate_indices = self.candidate_indices_for_dc(&writers_snapshot, target_dc).await;
|
|
||||||
}
|
}
|
||||||
if candidate_indices.is_empty() {
|
if candidate_indices.is_empty() {
|
||||||
return Err(ProxyError::Proxy("No ME writers available for target DC".into()));
|
return Err(ProxyError::Proxy("No ME writers available for target DC".into()));
|
||||||
@@ -100,11 +124,10 @@ impl MePool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
candidate_indices.sort_by_key(|idx| {
|
candidate_indices.sort_by_key(|idx| {
|
||||||
writers_snapshot[*idx]
|
let w = &writers_snapshot[*idx];
|
||||||
.degraded
|
let degraded = w.degraded.load(Ordering::Relaxed);
|
||||||
.load(Ordering::Relaxed)
|
let draining = w.draining.load(Ordering::Relaxed);
|
||||||
.then_some(1usize)
|
(draining as usize, degraded as usize)
|
||||||
.unwrap_or(0)
|
|
||||||
});
|
});
|
||||||
|
|
||||||
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();
|
||||||
@@ -112,36 +135,35 @@ impl MePool {
|
|||||||
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()];
|
||||||
let w = &writers_snapshot[idx];
|
let w = &writers_snapshot[idx];
|
||||||
if let Ok(mut guard) = w.writer.try_lock() {
|
if w.draining.load(Ordering::Relaxed) {
|
||||||
let send_res = guard.send(&payload).await;
|
continue;
|
||||||
drop(guard);
|
}
|
||||||
match send_res {
|
if w.tx.send(WriterCommand::Data(payload.clone())).await.is_ok() {
|
||||||
Ok(()) => {
|
self.registry
|
||||||
self.registry
|
.bind_writer(conn_id, w.id, w.tx.clone(), meta.clone())
|
||||||
.bind_writer(conn_id, w.id, w.writer.clone(), meta.clone())
|
.await;
|
||||||
.await;
|
return Ok(());
|
||||||
return Ok(());
|
} else {
|
||||||
}
|
warn!(writer_id = w.id, "ME writer channel closed");
|
||||||
Err(e) => {
|
self.remove_writer_and_close_clients(w.id).await;
|
||||||
warn!(error = %e, writer_id = w.id, "ME write failed");
|
continue;
|
||||||
self.remove_writer_and_reroute(w.id).await;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let w = writers_snapshot[candidate_indices[start]].clone();
|
let w = writers_snapshot[candidate_indices[start]].clone();
|
||||||
match w.writer.lock().await.send(&payload).await {
|
if w.draining.load(Ordering::Relaxed) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
match w.tx.send(WriterCommand::Data(payload.clone())).await {
|
||||||
Ok(()) => {
|
Ok(()) => {
|
||||||
self.registry
|
self.registry
|
||||||
.bind_writer(conn_id, w.id, w.writer.clone(), meta.clone())
|
.bind_writer(conn_id, w.id, w.tx.clone(), meta.clone())
|
||||||
.await;
|
.await;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(_) => {
|
||||||
warn!(error = %e, writer_id = w.id, "ME write failed (blocking)");
|
warn!(writer_id = w.id, "ME writer channel closed (blocking)");
|
||||||
self.remove_writer_and_reroute(w.id).await;
|
self.remove_writer_and_close_clients(w.id).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -152,9 +174,9 @@ impl MePool {
|
|||||||
let mut p = Vec::with_capacity(12);
|
let mut p = Vec::with_capacity(12);
|
||||||
p.extend_from_slice(&RPC_CLOSE_EXT_U32.to_le_bytes());
|
p.extend_from_slice(&RPC_CLOSE_EXT_U32.to_le_bytes());
|
||||||
p.extend_from_slice(&conn_id.to_le_bytes());
|
p.extend_from_slice(&conn_id.to_le_bytes());
|
||||||
if let Err(e) = w.writer.lock().await.send(&p).await {
|
if w.tx.send(WriterCommand::DataAndFlush(p)).await.is_err() {
|
||||||
debug!(error = %e, "ME close write failed");
|
debug!("ME close write failed");
|
||||||
self.remove_writer_and_reroute(w.writer_id).await;
|
self.remove_writer_and_close_clients(w.writer_id).await;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
debug!(conn_id, "ME close skipped (writer missing)");
|
debug!(conn_id, "ME close skipped (writer missing)");
|
||||||
@@ -165,7 +187,7 @@ impl MePool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn connection_count(&self) -> usize {
|
pub fn connection_count(&self) -> usize {
|
||||||
self.writers.try_read().map(|w| w.len()).unwrap_or(0)
|
self.conn_count.load(Ordering::Relaxed)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) async fn candidate_indices_for_dc(
|
pub(super) async fn candidate_indices_for_dc(
|
||||||
@@ -173,46 +195,65 @@ impl MePool {
|
|||||||
writers: &[super::pool::MeWriter],
|
writers: &[super::pool::MeWriter],
|
||||||
target_dc: i16,
|
target_dc: i16,
|
||||||
) -> Vec<usize> {
|
) -> Vec<usize> {
|
||||||
let mut preferred = Vec::<SocketAddr>::new();
|
|
||||||
let key = target_dc as i32;
|
let key = target_dc as i32;
|
||||||
let map = self.proxy_map_v4.read().await;
|
let mut preferred = Vec::<SocketAddr>::new();
|
||||||
|
|
||||||
if let Some(v) = map.get(&key) {
|
for family in self.family_order() {
|
||||||
preferred.extend(v.iter().map(|(ip, port)| SocketAddr::new(*ip, *port)));
|
let map_guard = match family {
|
||||||
}
|
IpFamily::V4 => self.proxy_map_v4.read().await,
|
||||||
if preferred.is_empty() {
|
IpFamily::V6 => self.proxy_map_v6.read().await,
|
||||||
let abs = key.abs();
|
};
|
||||||
if let Some(v) = map.get(&abs) {
|
|
||||||
|
if let Some(v) = map_guard.get(&key) {
|
||||||
preferred.extend(v.iter().map(|(ip, port)| SocketAddr::new(*ip, *port)));
|
preferred.extend(v.iter().map(|(ip, port)| SocketAddr::new(*ip, *port)));
|
||||||
}
|
}
|
||||||
}
|
if preferred.is_empty() {
|
||||||
if preferred.is_empty() {
|
let abs = key.abs();
|
||||||
let abs = key.abs();
|
if let Some(v) = map_guard.get(&abs) {
|
||||||
if let Some(v) = map.get(&-abs) {
|
|
||||||
preferred.extend(v.iter().map(|(ip, port)| SocketAddr::new(*ip, *port)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if preferred.is_empty() {
|
|
||||||
let def = self.default_dc.load(Ordering::Relaxed);
|
|
||||||
if def != 0 {
|
|
||||||
if let Some(v) = map.get(&def) {
|
|
||||||
preferred.extend(v.iter().map(|(ip, port)| SocketAddr::new(*ip, *port)));
|
preferred.extend(v.iter().map(|(ip, port)| SocketAddr::new(*ip, *port)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if preferred.is_empty() {
|
||||||
|
let abs = key.abs();
|
||||||
|
if let Some(v) = map_guard.get(&-abs) {
|
||||||
|
preferred.extend(v.iter().map(|(ip, port)| SocketAddr::new(*ip, *port)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if preferred.is_empty() {
|
||||||
|
let def = self.default_dc.load(Ordering::Relaxed);
|
||||||
|
if def != 0 {
|
||||||
|
if let Some(v) = map_guard.get(&def) {
|
||||||
|
preferred.extend(v.iter().map(|(ip, port)| SocketAddr::new(*ip, *port)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
drop(map_guard);
|
||||||
|
|
||||||
|
if !preferred.is_empty() && !self.decision.effective_multipath {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if preferred.is_empty() {
|
if preferred.is_empty() {
|
||||||
return (0..writers.len()).collect();
|
return (0..writers.len())
|
||||||
|
.filter(|i| !writers[*i].draining.load(Ordering::Relaxed))
|
||||||
|
.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 w.draining.load(Ordering::Relaxed) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if preferred.iter().any(|p| *p == w.addr) {
|
if preferred.iter().any(|p| *p == w.addr) {
|
||||||
out.push(idx);
|
out.push(idx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if out.is_empty() {
|
if out.is_empty() {
|
||||||
return (0..writers.len()).collect();
|
return (0..writers.len())
|
||||||
|
.filter(|i| !writers[*i].draining.load(Ordering::Relaxed))
|
||||||
|
.collect();
|
||||||
}
|
}
|
||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -283,6 +283,58 @@ impl Default for ProxyProtocolV1Builder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Builder for PROXY protocol v2 header
|
||||||
|
pub struct ProxyProtocolV2Builder {
|
||||||
|
src: Option<SocketAddr>,
|
||||||
|
dst: Option<SocketAddr>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ProxyProtocolV2Builder {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self { src: None, dst: None }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_addrs(mut self, src: SocketAddr, dst: SocketAddr) -> Self {
|
||||||
|
self.src = Some(src);
|
||||||
|
self.dst = Some(dst);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn build(&self) -> Vec<u8> {
|
||||||
|
let mut header = Vec::new();
|
||||||
|
header.extend_from_slice(PROXY_V2_SIGNATURE);
|
||||||
|
// version 2, PROXY command
|
||||||
|
header.push(0x21);
|
||||||
|
|
||||||
|
match (self.src, self.dst) {
|
||||||
|
(Some(SocketAddr::V4(src)), Some(SocketAddr::V4(dst))) => {
|
||||||
|
header.push(0x11); // INET + STREAM
|
||||||
|
header.extend_from_slice(&(12u16).to_be_bytes());
|
||||||
|
header.extend_from_slice(&src.ip().octets());
|
||||||
|
header.extend_from_slice(&dst.ip().octets());
|
||||||
|
header.extend_from_slice(&src.port().to_be_bytes());
|
||||||
|
header.extend_from_slice(&dst.port().to_be_bytes());
|
||||||
|
}
|
||||||
|
(Some(SocketAddr::V6(src)), Some(SocketAddr::V6(dst))) => {
|
||||||
|
header.push(0x21); // INET6 + STREAM
|
||||||
|
header.extend_from_slice(&(36u16).to_be_bytes());
|
||||||
|
header.extend_from_slice(&src.ip().octets());
|
||||||
|
header.extend_from_slice(&dst.ip().octets());
|
||||||
|
header.extend_from_slice(&src.port().to_be_bytes());
|
||||||
|
header.extend_from_slice(&dst.port().to_be_bytes());
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
// LOCAL/UNSPEC: no address information
|
||||||
|
header[12] = 0x20; // version 2, LOCAL command
|
||||||
|
header.push(0x00);
|
||||||
|
header.extend_from_slice(&0u16.to_be_bytes());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
header
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
@@ -378,4 +430,4 @@ mod tests {
|
|||||||
let header = ProxyProtocolV1Builder::new().build();
|
let header = ProxyProtocolV1Builder::new().build();
|
||||||
assert_eq!(header, b"PROXY UNKNOWN\r\n");
|
assert_eq!(header, b"PROXY UNKNOWN\r\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -122,6 +122,38 @@ pub fn get_local_addr(stream: &TcpStream) -> Option<SocketAddr> {
|
|||||||
stream.local_addr().ok()
|
stream.local_addr().ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Resolve primary IP address of a network interface by name.
|
||||||
|
/// Returns the first address matching the requested family (IPv4/IPv6).
|
||||||
|
#[cfg(unix)]
|
||||||
|
pub fn resolve_interface_ip(name: &str, want_ipv6: bool) -> Option<IpAddr> {
|
||||||
|
use nix::ifaddrs::getifaddrs;
|
||||||
|
|
||||||
|
if let Ok(addrs) = getifaddrs() {
|
||||||
|
for iface in addrs {
|
||||||
|
if iface.interface_name == name {
|
||||||
|
if let Some(address) = iface.address {
|
||||||
|
if let Some(v4) = address.as_sockaddr_in() {
|
||||||
|
if !want_ipv6 {
|
||||||
|
return Some(IpAddr::V4(v4.ip()));
|
||||||
|
}
|
||||||
|
} else if let Some(v6) = address.as_sockaddr_in6() {
|
||||||
|
if want_ipv6 {
|
||||||
|
return Some(IpAddr::V6(v6.ip().clone()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Stub for non-Unix platforms: interface name resolution unsupported.
|
||||||
|
#[cfg(not(unix))]
|
||||||
|
pub fn resolve_interface_ip(_name: &str, _want_ipv6: bool) -> Option<IpAddr> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
/// Get peer address of a socket
|
/// Get peer address of a socket
|
||||||
pub fn get_peer_addr(stream: &TcpStream) -> Option<SocketAddr> {
|
pub fn get_peer_addr(stream: &TcpStream) -> Option<SocketAddr> {
|
||||||
stream.peer_addr().ok()
|
stream.peer_addr().ok()
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::net::{SocketAddr, IpAddr};
|
use std::net::{SocketAddr, IpAddr};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use tokio::net::TcpStream;
|
use tokio::net::TcpStream;
|
||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
@@ -15,7 +16,7 @@ use tracing::{debug, warn, info, trace};
|
|||||||
use crate::config::{UpstreamConfig, UpstreamType};
|
use crate::config::{UpstreamConfig, UpstreamType};
|
||||||
use crate::error::{Result, ProxyError};
|
use crate::error::{Result, ProxyError};
|
||||||
use crate::protocol::constants::{TG_DATACENTERS_V4, TG_DATACENTERS_V6, TG_DATACENTER_PORT};
|
use crate::protocol::constants::{TG_DATACENTERS_V4, TG_DATACENTERS_V6, TG_DATACENTER_PORT};
|
||||||
use crate::transport::socket::create_outgoing_socket_bound;
|
use crate::transport::socket::{create_outgoing_socket_bound, resolve_interface_ip};
|
||||||
use crate::transport::socks::{connect_socks4, connect_socks5};
|
use crate::transport::socks::{connect_socks4, connect_socks5};
|
||||||
|
|
||||||
/// Number of Telegram datacenters
|
/// Number of Telegram datacenters
|
||||||
@@ -84,6 +85,8 @@ struct UpstreamState {
|
|||||||
dc_latency: [LatencyEma; NUM_DCS],
|
dc_latency: [LatencyEma; NUM_DCS],
|
||||||
/// Per-DC IP version preference (learned from connectivity tests)
|
/// Per-DC IP version preference (learned from connectivity tests)
|
||||||
dc_ip_pref: [IpPreference; NUM_DCS],
|
dc_ip_pref: [IpPreference; NUM_DCS],
|
||||||
|
/// Round-robin counter for bind_addresses selection
|
||||||
|
bind_rr: Arc<AtomicUsize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UpstreamState {
|
impl UpstreamState {
|
||||||
@@ -95,6 +98,7 @@ impl UpstreamState {
|
|||||||
last_check: std::time::Instant::now(),
|
last_check: std::time::Instant::now(),
|
||||||
dc_latency: [LatencyEma::new(0.3); NUM_DCS],
|
dc_latency: [LatencyEma::new(0.3); NUM_DCS],
|
||||||
dc_ip_pref: [IpPreference::Unknown; NUM_DCS],
|
dc_ip_pref: [IpPreference::Unknown; NUM_DCS],
|
||||||
|
bind_rr: Arc::new(AtomicUsize::new(0)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -166,21 +170,85 @@ impl UpstreamManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn resolve_bind_address(
|
||||||
|
interface: &Option<String>,
|
||||||
|
bind_addresses: &Option<Vec<String>>,
|
||||||
|
target: SocketAddr,
|
||||||
|
rr: Option<&AtomicUsize>,
|
||||||
|
) -> Option<IpAddr> {
|
||||||
|
let want_ipv6 = target.is_ipv6();
|
||||||
|
|
||||||
|
if let Some(addrs) = bind_addresses {
|
||||||
|
let candidates: Vec<IpAddr> = addrs
|
||||||
|
.iter()
|
||||||
|
.filter_map(|s| s.parse::<IpAddr>().ok())
|
||||||
|
.filter(|ip| ip.is_ipv6() == want_ipv6)
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
if !candidates.is_empty() {
|
||||||
|
if let Some(counter) = rr {
|
||||||
|
let idx = counter.fetch_add(1, Ordering::Relaxed) % candidates.len();
|
||||||
|
return Some(candidates[idx]);
|
||||||
|
}
|
||||||
|
return candidates.first().copied();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(iface) = interface {
|
||||||
|
if let Ok(ip) = iface.parse::<IpAddr>() {
|
||||||
|
if ip.is_ipv6() == want_ipv6 {
|
||||||
|
return Some(ip);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
#[cfg(unix)]
|
||||||
|
if let Some(ip) = resolve_interface_ip(iface, want_ipv6) {
|
||||||
|
return Some(ip);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
/// Select upstream using latency-weighted random selection.
|
/// Select upstream using latency-weighted random selection.
|
||||||
async fn select_upstream(&self, dc_idx: Option<i16>) -> Option<usize> {
|
async fn select_upstream(&self, dc_idx: Option<i16>, scope: Option<&str>) -> Option<usize> {
|
||||||
let upstreams = self.upstreams.read().await;
|
let upstreams = self.upstreams.read().await;
|
||||||
if upstreams.is_empty() {
|
if upstreams.is_empty() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
// Scope filter:
|
||||||
let healthy: Vec<usize> = upstreams.iter()
|
// If scope is set: only scoped and matched items
|
||||||
|
// If scope is not set: only unscoped items
|
||||||
|
let filtered_upstreams : Vec<usize> = upstreams.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.filter(|(_, u)| u.healthy)
|
.filter(|(_, u)| {
|
||||||
|
scope.map_or(
|
||||||
|
u.config.scopes.is_empty(),
|
||||||
|
|req_scope| {
|
||||||
|
u.config.scopes
|
||||||
|
.split(',')
|
||||||
|
.map(str::trim)
|
||||||
|
.any(|s| s == req_scope)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
.map(|(i, _)| i)
|
.map(|(i, _)| i)
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
// Healthy filter
|
||||||
|
let healthy: Vec<usize> = filtered_upstreams.iter()
|
||||||
|
.filter(|&&i| upstreams[i].healthy)
|
||||||
|
.copied()
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
if filtered_upstreams.is_empty() {
|
||||||
|
warn!(scope = scope, "No upstreams available! Using first (direct?)");
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
if healthy.is_empty() {
|
if healthy.is_empty() {
|
||||||
return Some(rand::rng().gen_range(0..upstreams.len()));
|
warn!(scope = scope, "No healthy upstreams available! Using random.");
|
||||||
|
return Some(filtered_upstreams[rand::rng().gen_range(0..filtered_upstreams.len())]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if healthy.len() == 1 {
|
if healthy.len() == 1 {
|
||||||
@@ -222,18 +290,28 @@ impl UpstreamManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Connect to target through a selected upstream.
|
/// Connect to target through a selected upstream.
|
||||||
pub async fn connect(&self, target: SocketAddr, dc_idx: Option<i16>) -> Result<TcpStream> {
|
pub async fn connect(&self, target: SocketAddr, dc_idx: Option<i16>, scope: Option<&str>) -> Result<TcpStream> {
|
||||||
let idx = self.select_upstream(dc_idx).await
|
let idx = self.select_upstream(dc_idx, scope).await
|
||||||
.ok_or_else(|| ProxyError::Config("No upstreams available".to_string()))?;
|
.ok_or_else(|| ProxyError::Config("No upstreams available".to_string()))?;
|
||||||
|
|
||||||
let upstream = {
|
let mut upstream = {
|
||||||
let guard = self.upstreams.read().await;
|
let guard = self.upstreams.read().await;
|
||||||
guard[idx].config.clone()
|
guard[idx].config.clone()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Set scope for configuration copy
|
||||||
|
if let Some(s) = scope {
|
||||||
|
upstream.selected_scope = s.to_string();
|
||||||
|
}
|
||||||
|
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
|
|
||||||
match self.connect_via_upstream(&upstream, target).await {
|
let bind_rr = {
|
||||||
|
let guard = self.upstreams.read().await;
|
||||||
|
guard.get(idx).map(|u| u.bind_rr.clone())
|
||||||
|
};
|
||||||
|
|
||||||
|
match self.connect_via_upstream(&upstream, target, bind_rr).await {
|
||||||
Ok(stream) => {
|
Ok(stream) => {
|
||||||
let rtt_ms = start.elapsed().as_secs_f64() * 1000.0;
|
let rtt_ms = start.elapsed().as_secs_f64() * 1000.0;
|
||||||
let mut guard = self.upstreams.write().await;
|
let mut guard = self.upstreams.write().await;
|
||||||
@@ -265,13 +343,27 @@ impl UpstreamManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn connect_via_upstream(&self, config: &UpstreamConfig, target: SocketAddr) -> Result<TcpStream> {
|
async fn connect_via_upstream(
|
||||||
|
&self,
|
||||||
|
config: &UpstreamConfig,
|
||||||
|
target: SocketAddr,
|
||||||
|
bind_rr: Option<Arc<AtomicUsize>>,
|
||||||
|
) -> Result<TcpStream> {
|
||||||
match &config.upstream_type {
|
match &config.upstream_type {
|
||||||
UpstreamType::Direct { interface } => {
|
UpstreamType::Direct { interface, bind_addresses } => {
|
||||||
let bind_ip = interface.as_ref()
|
let bind_ip = Self::resolve_bind_address(
|
||||||
.and_then(|s| s.parse::<IpAddr>().ok());
|
interface,
|
||||||
|
bind_addresses,
|
||||||
|
target,
|
||||||
|
bind_rr.as_deref(),
|
||||||
|
);
|
||||||
|
|
||||||
let socket = create_outgoing_socket_bound(target, bind_ip)?;
|
let socket = create_outgoing_socket_bound(target, bind_ip)?;
|
||||||
|
if let Some(ip) = bind_ip {
|
||||||
|
debug!(bind = %ip, target = %target, "Bound outgoing socket");
|
||||||
|
} else if interface.is_some() || bind_addresses.is_some() {
|
||||||
|
debug!(target = %target, "No matching bind address for target family");
|
||||||
|
}
|
||||||
|
|
||||||
socket.set_nonblocking(true)?;
|
socket.set_nonblocking(true)?;
|
||||||
match socket.connect(&target.into()) {
|
match socket.connect(&target.into()) {
|
||||||
@@ -294,8 +386,12 @@ impl UpstreamManager {
|
|||||||
let proxy_addr: SocketAddr = address.parse()
|
let proxy_addr: SocketAddr = address.parse()
|
||||||
.map_err(|_| ProxyError::Config("Invalid SOCKS4 address".to_string()))?;
|
.map_err(|_| ProxyError::Config("Invalid SOCKS4 address".to_string()))?;
|
||||||
|
|
||||||
let bind_ip = interface.as_ref()
|
let bind_ip = Self::resolve_bind_address(
|
||||||
.and_then(|s| s.parse::<IpAddr>().ok());
|
interface,
|
||||||
|
&None,
|
||||||
|
proxy_addr,
|
||||||
|
bind_rr.as_deref(),
|
||||||
|
);
|
||||||
|
|
||||||
let socket = create_outgoing_socket_bound(proxy_addr, bind_ip)?;
|
let socket = create_outgoing_socket_bound(proxy_addr, bind_ip)?;
|
||||||
|
|
||||||
@@ -313,16 +409,24 @@ impl UpstreamManager {
|
|||||||
if let Some(e) = stream.take_error()? {
|
if let Some(e) = stream.take_error()? {
|
||||||
return Err(ProxyError::Io(e));
|
return Err(ProxyError::Io(e));
|
||||||
}
|
}
|
||||||
|
// replace socks user_id with config.selected_scope, if set
|
||||||
|
let scope: Option<&str> = Some(config.selected_scope.as_str())
|
||||||
|
.filter(|s| !s.is_empty());
|
||||||
|
let _user_id: Option<&str> = scope.or(user_id.as_deref());
|
||||||
|
|
||||||
connect_socks4(&mut stream, target, user_id.as_deref()).await?;
|
connect_socks4(&mut stream, target, _user_id).await?;
|
||||||
Ok(stream)
|
Ok(stream)
|
||||||
},
|
},
|
||||||
UpstreamType::Socks5 { address, interface, username, password } => {
|
UpstreamType::Socks5 { address, interface, username, password } => {
|
||||||
let proxy_addr: SocketAddr = address.parse()
|
let proxy_addr: SocketAddr = address.parse()
|
||||||
.map_err(|_| ProxyError::Config("Invalid SOCKS5 address".to_string()))?;
|
.map_err(|_| ProxyError::Config("Invalid SOCKS5 address".to_string()))?;
|
||||||
|
|
||||||
let bind_ip = interface.as_ref()
|
let bind_ip = Self::resolve_bind_address(
|
||||||
.and_then(|s| s.parse::<IpAddr>().ok());
|
interface,
|
||||||
|
&None,
|
||||||
|
proxy_addr,
|
||||||
|
bind_rr.as_deref(),
|
||||||
|
);
|
||||||
|
|
||||||
let socket = create_outgoing_socket_bound(proxy_addr, bind_ip)?;
|
let socket = create_outgoing_socket_bound(proxy_addr, bind_ip)?;
|
||||||
|
|
||||||
@@ -341,7 +445,14 @@ impl UpstreamManager {
|
|||||||
return Err(ProxyError::Io(e));
|
return Err(ProxyError::Io(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
connect_socks5(&mut stream, target, username.as_deref(), password.as_deref()).await?;
|
debug!(config = ?config, "Socks5 connection");
|
||||||
|
// replace socks user:pass with config.selected_scope, if set
|
||||||
|
let scope: Option<&str> = Some(config.selected_scope.as_str())
|
||||||
|
.filter(|s| !s.is_empty());
|
||||||
|
let _username: Option<&str> = scope.or(username.as_deref());
|
||||||
|
let _password: Option<&str> = scope.or(password.as_deref());
|
||||||
|
|
||||||
|
connect_socks5(&mut stream, target, _username, _password).await?;
|
||||||
Ok(stream)
|
Ok(stream)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -355,104 +466,127 @@ impl UpstreamManager {
|
|||||||
&self,
|
&self,
|
||||||
prefer_ipv6: bool,
|
prefer_ipv6: bool,
|
||||||
dc_overrides: &HashMap<String, Vec<String>>,
|
dc_overrides: &HashMap<String, Vec<String>>,
|
||||||
|
ipv4_enabled: bool,
|
||||||
|
ipv6_enabled: bool,
|
||||||
) -> Vec<StartupPingResult> {
|
) -> Vec<StartupPingResult> {
|
||||||
let upstreams: Vec<(usize, UpstreamConfig)> = {
|
let upstreams: Vec<(usize, UpstreamConfig, Arc<AtomicUsize>)> = {
|
||||||
let guard = self.upstreams.read().await;
|
let guard = self.upstreams.read().await;
|
||||||
guard.iter().enumerate()
|
guard.iter().enumerate()
|
||||||
.map(|(i, u)| (i, u.config.clone()))
|
.map(|(i, u)| (i, u.config.clone(), u.bind_rr.clone()))
|
||||||
.collect()
|
.collect()
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut all_results = Vec::new();
|
let mut all_results = Vec::new();
|
||||||
|
|
||||||
for (upstream_idx, upstream_config) in &upstreams {
|
for (upstream_idx, upstream_config, bind_rr) in &upstreams {
|
||||||
let upstream_name = match &upstream_config.upstream_type {
|
let upstream_name = match &upstream_config.upstream_type {
|
||||||
UpstreamType::Direct { interface } => {
|
UpstreamType::Direct { interface, .. } => {
|
||||||
format!("direct{}", interface.as_ref().map(|i| format!(" ({})", i)).unwrap_or_default())
|
format!("direct{}", interface.as_ref().map(|i| format!(" ({})", i)).unwrap_or_default())
|
||||||
}
|
}
|
||||||
UpstreamType::Socks4 { address, .. } => format!("socks4://{}", address),
|
UpstreamType::Socks4 { address, .. } => format!("socks4://{}", address),
|
||||||
UpstreamType::Socks5 { address, .. } => format!("socks5://{}", address),
|
UpstreamType::Socks5 { address, .. } => format!("socks5://{}", address),
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut v6_results = Vec::new();
|
let mut v6_results = Vec::with_capacity(NUM_DCS);
|
||||||
let mut v4_results = Vec::new();
|
if ipv6_enabled {
|
||||||
|
for dc_zero_idx in 0..NUM_DCS {
|
||||||
|
let dc_v6 = TG_DATACENTERS_V6[dc_zero_idx];
|
||||||
|
let addr_v6 = SocketAddr::new(dc_v6, TG_DATACENTER_PORT);
|
||||||
|
|
||||||
// === Ping IPv6 first ===
|
let result = tokio::time::timeout(
|
||||||
for dc_zero_idx in 0..NUM_DCS {
|
Duration::from_secs(DC_PING_TIMEOUT_SECS),
|
||||||
let dc_v6 = TG_DATACENTERS_V6[dc_zero_idx];
|
self.ping_single_dc(&upstream_config, Some(bind_rr.clone()), addr_v6)
|
||||||
let addr_v6 = SocketAddr::new(dc_v6, TG_DATACENTER_PORT);
|
).await;
|
||||||
|
|
||||||
let result = tokio::time::timeout(
|
let ping_result = match result {
|
||||||
Duration::from_secs(DC_PING_TIMEOUT_SECS),
|
Ok(Ok(rtt_ms)) => {
|
||||||
self.ping_single_dc(&upstream_config, addr_v6)
|
let mut guard = self.upstreams.write().await;
|
||||||
).await;
|
if let Some(u) = guard.get_mut(*upstream_idx) {
|
||||||
|
u.dc_latency[dc_zero_idx].update(rtt_ms);
|
||||||
let ping_result = match result {
|
}
|
||||||
Ok(Ok(rtt_ms)) => {
|
DcPingResult {
|
||||||
let mut guard = self.upstreams.write().await;
|
dc_idx: dc_zero_idx + 1,
|
||||||
if let Some(u) = guard.get_mut(*upstream_idx) {
|
dc_addr: addr_v6,
|
||||||
u.dc_latency[dc_zero_idx].update(rtt_ms);
|
rtt_ms: Some(rtt_ms),
|
||||||
|
error: None,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
DcPingResult {
|
Ok(Err(e)) => DcPingResult {
|
||||||
dc_idx: dc_zero_idx + 1,
|
dc_idx: dc_zero_idx + 1,
|
||||||
dc_addr: addr_v6,
|
dc_addr: addr_v6,
|
||||||
rtt_ms: Some(rtt_ms),
|
rtt_ms: None,
|
||||||
error: None,
|
error: Some(e.to_string()),
|
||||||
}
|
},
|
||||||
}
|
Err(_) => DcPingResult {
|
||||||
Ok(Err(e)) => DcPingResult {
|
dc_idx: dc_zero_idx + 1,
|
||||||
|
dc_addr: addr_v6,
|
||||||
|
rtt_ms: None,
|
||||||
|
error: Some("timeout".to_string()),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
v6_results.push(ping_result);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for dc_zero_idx in 0..NUM_DCS {
|
||||||
|
let dc_v6 = TG_DATACENTERS_V6[dc_zero_idx];
|
||||||
|
v6_results.push(DcPingResult {
|
||||||
dc_idx: dc_zero_idx + 1,
|
dc_idx: dc_zero_idx + 1,
|
||||||
dc_addr: addr_v6,
|
dc_addr: SocketAddr::new(dc_v6, TG_DATACENTER_PORT),
|
||||||
rtt_ms: None,
|
rtt_ms: None,
|
||||||
error: Some(e.to_string()),
|
error: Some("ipv6 disabled".to_string()),
|
||||||
},
|
});
|
||||||
Err(_) => DcPingResult {
|
}
|
||||||
dc_idx: dc_zero_idx + 1,
|
|
||||||
dc_addr: addr_v6,
|
|
||||||
rtt_ms: None,
|
|
||||||
error: Some("timeout".to_string()),
|
|
||||||
},
|
|
||||||
};
|
|
||||||
v6_results.push(ping_result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// === Then ping IPv4 ===
|
let mut v4_results = Vec::with_capacity(NUM_DCS);
|
||||||
for dc_zero_idx in 0..NUM_DCS {
|
if ipv4_enabled {
|
||||||
let dc_v4 = TG_DATACENTERS_V4[dc_zero_idx];
|
for dc_zero_idx in 0..NUM_DCS {
|
||||||
let addr_v4 = SocketAddr::new(dc_v4, TG_DATACENTER_PORT);
|
let dc_v4 = TG_DATACENTERS_V4[dc_zero_idx];
|
||||||
|
let addr_v4 = SocketAddr::new(dc_v4, TG_DATACENTER_PORT);
|
||||||
|
|
||||||
let result = tokio::time::timeout(
|
let result = tokio::time::timeout(
|
||||||
Duration::from_secs(DC_PING_TIMEOUT_SECS),
|
Duration::from_secs(DC_PING_TIMEOUT_SECS),
|
||||||
self.ping_single_dc(&upstream_config, addr_v4)
|
self.ping_single_dc(&upstream_config, Some(bind_rr.clone()), addr_v4)
|
||||||
).await;
|
).await;
|
||||||
|
|
||||||
let ping_result = match result {
|
let ping_result = match result {
|
||||||
Ok(Ok(rtt_ms)) => {
|
Ok(Ok(rtt_ms)) => {
|
||||||
let mut guard = self.upstreams.write().await;
|
let mut guard = self.upstreams.write().await;
|
||||||
if let Some(u) = guard.get_mut(*upstream_idx) {
|
if let Some(u) = guard.get_mut(*upstream_idx) {
|
||||||
u.dc_latency[dc_zero_idx].update(rtt_ms);
|
u.dc_latency[dc_zero_idx].update(rtt_ms);
|
||||||
|
}
|
||||||
|
DcPingResult {
|
||||||
|
dc_idx: dc_zero_idx + 1,
|
||||||
|
dc_addr: addr_v4,
|
||||||
|
rtt_ms: Some(rtt_ms),
|
||||||
|
error: None,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
DcPingResult {
|
Ok(Err(e)) => DcPingResult {
|
||||||
dc_idx: dc_zero_idx + 1,
|
dc_idx: dc_zero_idx + 1,
|
||||||
dc_addr: addr_v4,
|
dc_addr: addr_v4,
|
||||||
rtt_ms: Some(rtt_ms),
|
rtt_ms: None,
|
||||||
error: None,
|
error: Some(e.to_string()),
|
||||||
}
|
},
|
||||||
}
|
Err(_) => DcPingResult {
|
||||||
Ok(Err(e)) => DcPingResult {
|
dc_idx: dc_zero_idx + 1,
|
||||||
|
dc_addr: addr_v4,
|
||||||
|
rtt_ms: None,
|
||||||
|
error: Some("timeout".to_string()),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
v4_results.push(ping_result);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for dc_zero_idx in 0..NUM_DCS {
|
||||||
|
let dc_v4 = TG_DATACENTERS_V4[dc_zero_idx];
|
||||||
|
v4_results.push(DcPingResult {
|
||||||
dc_idx: dc_zero_idx + 1,
|
dc_idx: dc_zero_idx + 1,
|
||||||
dc_addr: addr_v4,
|
dc_addr: SocketAddr::new(dc_v4, TG_DATACENTER_PORT),
|
||||||
rtt_ms: None,
|
rtt_ms: None,
|
||||||
error: Some(e.to_string()),
|
error: Some("ipv4 disabled".to_string()),
|
||||||
},
|
});
|
||||||
Err(_) => DcPingResult {
|
}
|
||||||
dc_idx: dc_zero_idx + 1,
|
|
||||||
dc_addr: addr_v4,
|
|
||||||
rtt_ms: None,
|
|
||||||
error: Some("timeout".to_string()),
|
|
||||||
},
|
|
||||||
};
|
|
||||||
v4_results.push(ping_result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// === Ping DC overrides (v4/v6) ===
|
// === Ping DC overrides (v4/v6) ===
|
||||||
@@ -470,9 +604,12 @@ impl UpstreamManager {
|
|||||||
match addr_str.parse::<SocketAddr>() {
|
match addr_str.parse::<SocketAddr>() {
|
||||||
Ok(addr) => {
|
Ok(addr) => {
|
||||||
let is_v6 = addr.is_ipv6();
|
let is_v6 = addr.is_ipv6();
|
||||||
|
if (is_v6 && !ipv6_enabled) || (!is_v6 && !ipv4_enabled) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
let result = tokio::time::timeout(
|
let result = tokio::time::timeout(
|
||||||
Duration::from_secs(DC_PING_TIMEOUT_SECS),
|
Duration::from_secs(DC_PING_TIMEOUT_SECS),
|
||||||
self.ping_single_dc(&upstream_config, addr)
|
self.ping_single_dc(&upstream_config, Some(bind_rr.clone()), addr)
|
||||||
).await;
|
).await;
|
||||||
|
|
||||||
let ping_result = match result {
|
let ping_result = match result {
|
||||||
@@ -541,9 +678,14 @@ impl UpstreamManager {
|
|||||||
all_results
|
all_results
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn ping_single_dc(&self, config: &UpstreamConfig, target: SocketAddr) -> Result<f64> {
|
async fn ping_single_dc(
|
||||||
|
&self,
|
||||||
|
config: &UpstreamConfig,
|
||||||
|
bind_rr: Option<Arc<AtomicUsize>>,
|
||||||
|
target: SocketAddr,
|
||||||
|
) -> Result<f64> {
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let _stream = self.connect_via_upstream(config, target).await?;
|
let _stream = self.connect_via_upstream(config, target, bind_rr).await?;
|
||||||
Ok(start.elapsed().as_secs_f64() * 1000.0)
|
Ok(start.elapsed().as_secs_f64() * 1000.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -551,7 +693,7 @@ impl UpstreamManager {
|
|||||||
|
|
||||||
/// Background health check: rotates through DCs, 30s interval.
|
/// Background health check: rotates through DCs, 30s interval.
|
||||||
/// Uses preferred IP version based on config.
|
/// Uses preferred IP version based on config.
|
||||||
pub async fn run_health_checks(&self, prefer_ipv6: bool) {
|
pub async fn run_health_checks(&self, prefer_ipv6: bool, ipv4_enabled: bool, ipv6_enabled: bool) {
|
||||||
let mut dc_rotation = 0usize;
|
let mut dc_rotation = 0usize;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
@@ -560,30 +702,39 @@ impl UpstreamManager {
|
|||||||
let dc_zero_idx = dc_rotation % NUM_DCS;
|
let dc_zero_idx = dc_rotation % NUM_DCS;
|
||||||
dc_rotation += 1;
|
dc_rotation += 1;
|
||||||
|
|
||||||
let dc_addr = if prefer_ipv6 {
|
let primary_v6 = SocketAddr::new(TG_DATACENTERS_V6[dc_zero_idx], TG_DATACENTER_PORT);
|
||||||
SocketAddr::new(TG_DATACENTERS_V6[dc_zero_idx], TG_DATACENTER_PORT)
|
let primary_v4 = SocketAddr::new(TG_DATACENTERS_V4[dc_zero_idx], TG_DATACENTER_PORT);
|
||||||
|
let dc_addr = if prefer_ipv6 && ipv6_enabled {
|
||||||
|
primary_v6
|
||||||
|
} else if ipv4_enabled {
|
||||||
|
primary_v4
|
||||||
|
} else if ipv6_enabled {
|
||||||
|
primary_v6
|
||||||
} else {
|
} else {
|
||||||
SocketAddr::new(TG_DATACENTERS_V4[dc_zero_idx], TG_DATACENTER_PORT)
|
continue;
|
||||||
};
|
};
|
||||||
|
|
||||||
let fallback_addr = if prefer_ipv6 {
|
let fallback_addr = if dc_addr.is_ipv6() && ipv4_enabled {
|
||||||
SocketAddr::new(TG_DATACENTERS_V4[dc_zero_idx], TG_DATACENTER_PORT)
|
Some(primary_v4)
|
||||||
|
} else if dc_addr.is_ipv4() && ipv6_enabled {
|
||||||
|
Some(primary_v6)
|
||||||
} else {
|
} else {
|
||||||
SocketAddr::new(TG_DATACENTERS_V6[dc_zero_idx], TG_DATACENTER_PORT)
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
let count = self.upstreams.read().await.len();
|
let count = self.upstreams.read().await.len();
|
||||||
|
|
||||||
for i in 0..count {
|
for i in 0..count {
|
||||||
let config = {
|
let (config, bind_rr) = {
|
||||||
let guard = self.upstreams.read().await;
|
let guard = self.upstreams.read().await;
|
||||||
guard[i].config.clone()
|
let u = &guard[i];
|
||||||
|
(u.config.clone(), u.bind_rr.clone())
|
||||||
};
|
};
|
||||||
|
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let result = tokio::time::timeout(
|
let result = tokio::time::timeout(
|
||||||
Duration::from_secs(10),
|
Duration::from_secs(10),
|
||||||
self.connect_via_upstream(&config, dc_addr)
|
self.connect_via_upstream(&config, dc_addr, Some(bind_rr.clone()))
|
||||||
).await;
|
).await;
|
||||||
|
|
||||||
match result {
|
match result {
|
||||||
@@ -608,48 +759,60 @@ impl UpstreamManager {
|
|||||||
// Try fallback
|
// Try fallback
|
||||||
debug!(dc = dc_zero_idx + 1, "Health check failed, trying fallback");
|
debug!(dc = dc_zero_idx + 1, "Health check failed, trying fallback");
|
||||||
|
|
||||||
let start2 = Instant::now();
|
if let Some(fallback_addr) = fallback_addr {
|
||||||
let result2 = tokio::time::timeout(
|
let start2 = Instant::now();
|
||||||
Duration::from_secs(10),
|
let result2 = tokio::time::timeout(
|
||||||
self.connect_via_upstream(&config, fallback_addr)
|
Duration::from_secs(10),
|
||||||
).await;
|
self.connect_via_upstream(&config, fallback_addr, Some(bind_rr.clone()))
|
||||||
|
).await;
|
||||||
|
|
||||||
|
let mut guard = self.upstreams.write().await;
|
||||||
|
let u = &mut guard[i];
|
||||||
|
|
||||||
|
match result2 {
|
||||||
|
Ok(Ok(_stream)) => {
|
||||||
|
let rtt_ms = start2.elapsed().as_secs_f64() * 1000.0;
|
||||||
|
u.dc_latency[dc_zero_idx].update(rtt_ms);
|
||||||
|
|
||||||
|
if !u.healthy {
|
||||||
|
info!(
|
||||||
|
rtt = format!("{:.0} ms", rtt_ms),
|
||||||
|
dc = dc_zero_idx + 1,
|
||||||
|
"Upstream recovered (fallback)"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
u.healthy = true;
|
||||||
|
u.fails = 0;
|
||||||
|
}
|
||||||
|
Ok(Err(e)) => {
|
||||||
|
u.fails += 1;
|
||||||
|
debug!(dc = dc_zero_idx + 1, fails = u.fails,
|
||||||
|
"Health check failed (both): {}", e);
|
||||||
|
if u.fails > 3 {
|
||||||
|
u.healthy = false;
|
||||||
|
warn!("Upstream unhealthy (fails)");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
u.fails += 1;
|
||||||
|
debug!(dc = dc_zero_idx + 1, fails = u.fails,
|
||||||
|
"Health check timeout (both)");
|
||||||
|
if u.fails > 3 {
|
||||||
|
u.healthy = false;
|
||||||
|
warn!("Upstream unhealthy (timeout)");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
u.last_check = std::time::Instant::now();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
let mut guard = self.upstreams.write().await;
|
let mut guard = self.upstreams.write().await;
|
||||||
let u = &mut guard[i];
|
let u = &mut guard[i];
|
||||||
|
u.fails += 1;
|
||||||
match result2 {
|
if u.fails > 3 {
|
||||||
Ok(Ok(_stream)) => {
|
u.healthy = false;
|
||||||
let rtt_ms = start2.elapsed().as_secs_f64() * 1000.0;
|
warn!("Upstream unhealthy (no fallback family)");
|
||||||
u.dc_latency[dc_zero_idx].update(rtt_ms);
|
|
||||||
|
|
||||||
if !u.healthy {
|
|
||||||
info!(
|
|
||||||
rtt = format!("{:.0} ms", rtt_ms),
|
|
||||||
dc = dc_zero_idx + 1,
|
|
||||||
"Upstream recovered (fallback)"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
u.healthy = true;
|
|
||||||
u.fails = 0;
|
|
||||||
}
|
|
||||||
Ok(Err(e)) => {
|
|
||||||
u.fails += 1;
|
|
||||||
debug!(dc = dc_zero_idx + 1, fails = u.fails,
|
|
||||||
"Health check failed (both): {}", e);
|
|
||||||
if u.fails > 3 {
|
|
||||||
u.healthy = false;
|
|
||||||
warn!("Upstream unhealthy (fails)");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(_) => {
|
|
||||||
u.fails += 1;
|
|
||||||
debug!(dc = dc_zero_idx + 1, fails = u.fails,
|
|
||||||
"Health check timeout (both)");
|
|
||||||
if u.fails > 3 {
|
|
||||||
u.healthy = false;
|
|
||||||
warn!("Upstream unhealthy (timeout)");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
u.last_check = std::time::Instant::now();
|
u.last_check = std::time::Instant::now();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ Type=simple
|
|||||||
WorkingDirectory=/bin
|
WorkingDirectory=/bin
|
||||||
ExecStart=/bin/telemt /etc/telemt.toml
|
ExecStart=/bin/telemt /etc/telemt.toml
|
||||||
Restart=on-failure
|
Restart=on-failure
|
||||||
|
LimitNOFILE=65536
|
||||||
|
|
||||||
[Install]
|
[Install]
|
||||||
WantedBy=multi-user.target
|
WantedBy=multi-user.target
|
||||||
|
|||||||
804
tools/grafana-dashboard.json
Normal file
804
tools/grafana-dashboard.json
Normal file
@@ -0,0 +1,804 @@
|
|||||||
|
{
|
||||||
|
"apiVersion": "dashboard.grafana.app/v1beta1",
|
||||||
|
"kind": "Dashboard",
|
||||||
|
"metadata": {
|
||||||
|
"annotations": {
|
||||||
|
"grafana.app/folder": "afd9kjusw2jnkb",
|
||||||
|
"grafana.app/saved-from-ui": "Grafana v12.4.0-21693836646 (f059795f04)"
|
||||||
|
},
|
||||||
|
"labels": {},
|
||||||
|
"name": "pi9trh5",
|
||||||
|
"namespace": "default"
|
||||||
|
},
|
||||||
|
"spec": {
|
||||||
|
"annotations": {
|
||||||
|
"list": [
|
||||||
|
{
|
||||||
|
"builtIn": 1,
|
||||||
|
"datasource": {
|
||||||
|
"type": "prometheus",
|
||||||
|
"uid": "${datasource}"
|
||||||
|
},
|
||||||
|
"enable": true,
|
||||||
|
"hide": true,
|
||||||
|
"iconColor": "rgba(0, 211, 255, 1)",
|
||||||
|
"name": "Annotations & Alerts",
|
||||||
|
"type": "dashboard"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"editable": true,
|
||||||
|
"fiscalYearStartMonth": 0,
|
||||||
|
"graphTooltip": 0,
|
||||||
|
"links": [],
|
||||||
|
"panels": [
|
||||||
|
{
|
||||||
|
"collapsed": false,
|
||||||
|
"gridPos": {
|
||||||
|
"h": 1,
|
||||||
|
"w": 24,
|
||||||
|
"x": 0,
|
||||||
|
"y": 0
|
||||||
|
},
|
||||||
|
"id": 5,
|
||||||
|
"panels": [],
|
||||||
|
"title": "Common",
|
||||||
|
"type": "row"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"datasource": {
|
||||||
|
"type": "prometheus",
|
||||||
|
"uid": "${datasource}"
|
||||||
|
},
|
||||||
|
"fieldConfig": {
|
||||||
|
"defaults": {
|
||||||
|
"color": {
|
||||||
|
"mode": "thresholds"
|
||||||
|
},
|
||||||
|
"mappings": [],
|
||||||
|
"thresholds": {
|
||||||
|
"mode": "absolute",
|
||||||
|
"steps": [
|
||||||
|
{
|
||||||
|
"color": "red",
|
||||||
|
"value": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"color": "green",
|
||||||
|
"value": 300
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"unit": "s"
|
||||||
|
},
|
||||||
|
"overrides": []
|
||||||
|
},
|
||||||
|
"gridPos": {
|
||||||
|
"h": 8,
|
||||||
|
"w": 6,
|
||||||
|
"x": 0,
|
||||||
|
"y": 1
|
||||||
|
},
|
||||||
|
"id": 1,
|
||||||
|
"options": {
|
||||||
|
"colorMode": "value",
|
||||||
|
"graphMode": "area",
|
||||||
|
"justifyMode": "auto",
|
||||||
|
"orientation": "auto",
|
||||||
|
"percentChangeColorMode": "standard",
|
||||||
|
"reduceOptions": {
|
||||||
|
"calcs": [
|
||||||
|
"lastNotNull"
|
||||||
|
],
|
||||||
|
"fields": "",
|
||||||
|
"values": false
|
||||||
|
},
|
||||||
|
"showPercentChange": false,
|
||||||
|
"textMode": "auto",
|
||||||
|
"wideLayout": true
|
||||||
|
},
|
||||||
|
"pluginVersion": "12.4.0-21693836646",
|
||||||
|
"targets": [
|
||||||
|
{
|
||||||
|
"datasource": {
|
||||||
|
"type": "prometheus",
|
||||||
|
"uid": "${datasource}"
|
||||||
|
},
|
||||||
|
"editorMode": "code",
|
||||||
|
"expr": "max(telemt_uptime_seconds) by (service)",
|
||||||
|
"format": "time_series",
|
||||||
|
"legendFormat": "__auto",
|
||||||
|
"range": true,
|
||||||
|
"refId": "A"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"title": "uptime",
|
||||||
|
"type": "stat"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"datasource": {
|
||||||
|
"type": "prometheus",
|
||||||
|
"uid": "${datasource}"
|
||||||
|
},
|
||||||
|
"fieldConfig": {
|
||||||
|
"defaults": {
|
||||||
|
"color": {
|
||||||
|
"mode": "thresholds"
|
||||||
|
},
|
||||||
|
"mappings": [],
|
||||||
|
"thresholds": {
|
||||||
|
"mode": "absolute",
|
||||||
|
"steps": [
|
||||||
|
{
|
||||||
|
"color": "green",
|
||||||
|
"value": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"color": "red",
|
||||||
|
"value": 80
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"unit": "none"
|
||||||
|
},
|
||||||
|
"overrides": []
|
||||||
|
},
|
||||||
|
"gridPos": {
|
||||||
|
"h": 8,
|
||||||
|
"w": 6,
|
||||||
|
"x": 6,
|
||||||
|
"y": 1
|
||||||
|
},
|
||||||
|
"id": 2,
|
||||||
|
"options": {
|
||||||
|
"colorMode": "value",
|
||||||
|
"graphMode": "area",
|
||||||
|
"justifyMode": "auto",
|
||||||
|
"orientation": "auto",
|
||||||
|
"percentChangeColorMode": "standard",
|
||||||
|
"reduceOptions": {
|
||||||
|
"calcs": [
|
||||||
|
"lastNotNull"
|
||||||
|
],
|
||||||
|
"fields": "",
|
||||||
|
"values": false
|
||||||
|
},
|
||||||
|
"showPercentChange": false,
|
||||||
|
"textMode": "auto",
|
||||||
|
"wideLayout": true
|
||||||
|
},
|
||||||
|
"pluginVersion": "12.4.0-21693836646",
|
||||||
|
"targets": [
|
||||||
|
{
|
||||||
|
"datasource": {
|
||||||
|
"type": "prometheus",
|
||||||
|
"uid": "${datasource}"
|
||||||
|
},
|
||||||
|
"editorMode": "code",
|
||||||
|
"expr": "max(telemt_connections_total) by (service)",
|
||||||
|
"format": "time_series",
|
||||||
|
"legendFormat": "__auto",
|
||||||
|
"range": true,
|
||||||
|
"refId": "A"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"title": "connections_total",
|
||||||
|
"type": "stat"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"datasource": {
|
||||||
|
"type": "prometheus",
|
||||||
|
"uid": "${datasource}"
|
||||||
|
},
|
||||||
|
"fieldConfig": {
|
||||||
|
"defaults": {
|
||||||
|
"color": {
|
||||||
|
"mode": "thresholds"
|
||||||
|
},
|
||||||
|
"mappings": [],
|
||||||
|
"thresholds": {
|
||||||
|
"mode": "absolute",
|
||||||
|
"steps": [
|
||||||
|
{
|
||||||
|
"color": "green",
|
||||||
|
"value": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"color": "red",
|
||||||
|
"value": 80
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"unit": "none"
|
||||||
|
},
|
||||||
|
"overrides": []
|
||||||
|
},
|
||||||
|
"gridPos": {
|
||||||
|
"h": 8,
|
||||||
|
"w": 6,
|
||||||
|
"x": 12,
|
||||||
|
"y": 1
|
||||||
|
},
|
||||||
|
"id": 3,
|
||||||
|
"options": {
|
||||||
|
"colorMode": "value",
|
||||||
|
"graphMode": "area",
|
||||||
|
"justifyMode": "auto",
|
||||||
|
"orientation": "auto",
|
||||||
|
"percentChangeColorMode": "standard",
|
||||||
|
"reduceOptions": {
|
||||||
|
"calcs": [
|
||||||
|
"lastNotNull"
|
||||||
|
],
|
||||||
|
"fields": "",
|
||||||
|
"values": false
|
||||||
|
},
|
||||||
|
"showPercentChange": false,
|
||||||
|
"textMode": "auto",
|
||||||
|
"wideLayout": true
|
||||||
|
},
|
||||||
|
"pluginVersion": "12.4.0-21693836646",
|
||||||
|
"targets": [
|
||||||
|
{
|
||||||
|
"datasource": {
|
||||||
|
"type": "prometheus",
|
||||||
|
"uid": "${datasource}"
|
||||||
|
},
|
||||||
|
"editorMode": "code",
|
||||||
|
"expr": "max(telemt_connections_bad_total) by (service)",
|
||||||
|
"format": "time_series",
|
||||||
|
"legendFormat": "__auto",
|
||||||
|
"range": true,
|
||||||
|
"refId": "A"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"title": "connections_bad",
|
||||||
|
"type": "stat"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"datasource": {
|
||||||
|
"type": "prometheus",
|
||||||
|
"uid": "${datasource}"
|
||||||
|
},
|
||||||
|
"fieldConfig": {
|
||||||
|
"defaults": {
|
||||||
|
"color": {
|
||||||
|
"mode": "thresholds"
|
||||||
|
},
|
||||||
|
"mappings": [],
|
||||||
|
"thresholds": {
|
||||||
|
"mode": "absolute",
|
||||||
|
"steps": [
|
||||||
|
{
|
||||||
|
"color": "green",
|
||||||
|
"value": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"color": "red",
|
||||||
|
"value": 80
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"unit": "none"
|
||||||
|
},
|
||||||
|
"overrides": []
|
||||||
|
},
|
||||||
|
"gridPos": {
|
||||||
|
"h": 8,
|
||||||
|
"w": 6,
|
||||||
|
"x": 18,
|
||||||
|
"y": 1
|
||||||
|
},
|
||||||
|
"id": 4,
|
||||||
|
"options": {
|
||||||
|
"colorMode": "value",
|
||||||
|
"graphMode": "area",
|
||||||
|
"justifyMode": "auto",
|
||||||
|
"orientation": "auto",
|
||||||
|
"percentChangeColorMode": "standard",
|
||||||
|
"reduceOptions": {
|
||||||
|
"calcs": [
|
||||||
|
"lastNotNull"
|
||||||
|
],
|
||||||
|
"fields": "",
|
||||||
|
"values": false
|
||||||
|
},
|
||||||
|
"showPercentChange": false,
|
||||||
|
"textMode": "auto",
|
||||||
|
"wideLayout": true
|
||||||
|
},
|
||||||
|
"pluginVersion": "12.4.0-21693836646",
|
||||||
|
"targets": [
|
||||||
|
{
|
||||||
|
"datasource": {
|
||||||
|
"type": "prometheus",
|
||||||
|
"uid": "${datasource}"
|
||||||
|
},
|
||||||
|
"editorMode": "code",
|
||||||
|
"expr": "max(telemt_handshake_timeouts_total) by (service)",
|
||||||
|
"format": "time_series",
|
||||||
|
"legendFormat": "__auto",
|
||||||
|
"range": true,
|
||||||
|
"refId": "A"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"title": "handshake_timeouts",
|
||||||
|
"type": "stat"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"collapsed": false,
|
||||||
|
"gridPos": {
|
||||||
|
"h": 1,
|
||||||
|
"w": 24,
|
||||||
|
"x": 0,
|
||||||
|
"y": 9
|
||||||
|
},
|
||||||
|
"id": 6,
|
||||||
|
"panels": [],
|
||||||
|
"repeat": "user",
|
||||||
|
"title": "$user",
|
||||||
|
"type": "row"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"datasource": {
|
||||||
|
"type": "prometheus",
|
||||||
|
"uid": "${datasource}"
|
||||||
|
},
|
||||||
|
"fieldConfig": {
|
||||||
|
"defaults": {
|
||||||
|
"color": {
|
||||||
|
"mode": "palette-classic"
|
||||||
|
},
|
||||||
|
"custom": {
|
||||||
|
"axisBorderShow": false,
|
||||||
|
"axisCenteredZero": false,
|
||||||
|
"axisColorMode": "text",
|
||||||
|
"axisLabel": "",
|
||||||
|
"axisPlacement": "auto",
|
||||||
|
"barAlignment": 0,
|
||||||
|
"barWidthFactor": 0.6,
|
||||||
|
"drawStyle": "line",
|
||||||
|
"fillOpacity": 0,
|
||||||
|
"gradientMode": "none",
|
||||||
|
"hideFrom": {
|
||||||
|
"legend": false,
|
||||||
|
"tooltip": false,
|
||||||
|
"viz": false
|
||||||
|
},
|
||||||
|
"insertNulls": false,
|
||||||
|
"lineInterpolation": "linear",
|
||||||
|
"lineWidth": 1,
|
||||||
|
"pointSize": 5,
|
||||||
|
"scaleDistribution": {
|
||||||
|
"type": "linear"
|
||||||
|
},
|
||||||
|
"showPoints": "auto",
|
||||||
|
"showValues": false,
|
||||||
|
"spanNulls": false,
|
||||||
|
"stacking": {
|
||||||
|
"group": "A",
|
||||||
|
"mode": "none"
|
||||||
|
},
|
||||||
|
"thresholdsStyle": {
|
||||||
|
"mode": "off"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"mappings": [],
|
||||||
|
"thresholds": {
|
||||||
|
"mode": "absolute",
|
||||||
|
"steps": [
|
||||||
|
{
|
||||||
|
"color": "green",
|
||||||
|
"value": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"color": "red",
|
||||||
|
"value": 80
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"unit": "none"
|
||||||
|
},
|
||||||
|
"overrides": []
|
||||||
|
},
|
||||||
|
"gridPos": {
|
||||||
|
"h": 8,
|
||||||
|
"w": 12,
|
||||||
|
"x": 0,
|
||||||
|
"y": 10
|
||||||
|
},
|
||||||
|
"id": 7,
|
||||||
|
"options": {
|
||||||
|
"legend": {
|
||||||
|
"calcs": [],
|
||||||
|
"displayMode": "list",
|
||||||
|
"placement": "bottom",
|
||||||
|
"showLegend": false
|
||||||
|
},
|
||||||
|
"tooltip": {
|
||||||
|
"hideZeros": false,
|
||||||
|
"mode": "single",
|
||||||
|
"sort": "none"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"pluginVersion": "12.4.0-21693836646",
|
||||||
|
"targets": [
|
||||||
|
{
|
||||||
|
"editorMode": "code",
|
||||||
|
"expr": "sum(telemt_user_connections_total{user=\"$user\"}) by (user)",
|
||||||
|
"format": "time_series",
|
||||||
|
"legendFormat": "{{ user }}",
|
||||||
|
"range": true,
|
||||||
|
"refId": "A"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"title": "user_connections",
|
||||||
|
"type": "timeseries"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"datasource": {
|
||||||
|
"type": "prometheus",
|
||||||
|
"uid": "${datasource}"
|
||||||
|
},
|
||||||
|
"fieldConfig": {
|
||||||
|
"defaults": {
|
||||||
|
"color": {
|
||||||
|
"mode": "palette-classic"
|
||||||
|
},
|
||||||
|
"custom": {
|
||||||
|
"axisBorderShow": false,
|
||||||
|
"axisCenteredZero": false,
|
||||||
|
"axisColorMode": "text",
|
||||||
|
"axisLabel": "",
|
||||||
|
"axisPlacement": "auto",
|
||||||
|
"barAlignment": 0,
|
||||||
|
"barWidthFactor": 0.6,
|
||||||
|
"drawStyle": "line",
|
||||||
|
"fillOpacity": 0,
|
||||||
|
"gradientMode": "none",
|
||||||
|
"hideFrom": {
|
||||||
|
"legend": false,
|
||||||
|
"tooltip": false,
|
||||||
|
"viz": false
|
||||||
|
},
|
||||||
|
"insertNulls": false,
|
||||||
|
"lineInterpolation": "linear",
|
||||||
|
"lineWidth": 1,
|
||||||
|
"pointSize": 5,
|
||||||
|
"scaleDistribution": {
|
||||||
|
"type": "linear"
|
||||||
|
},
|
||||||
|
"showPoints": "auto",
|
||||||
|
"showValues": false,
|
||||||
|
"spanNulls": false,
|
||||||
|
"stacking": {
|
||||||
|
"group": "A",
|
||||||
|
"mode": "none"
|
||||||
|
},
|
||||||
|
"thresholdsStyle": {
|
||||||
|
"mode": "off"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"mappings": [],
|
||||||
|
"thresholds": {
|
||||||
|
"mode": "absolute",
|
||||||
|
"steps": [
|
||||||
|
{
|
||||||
|
"color": "green",
|
||||||
|
"value": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"color": "red",
|
||||||
|
"value": 80
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"unit": "none"
|
||||||
|
},
|
||||||
|
"overrides": []
|
||||||
|
},
|
||||||
|
"gridPos": {
|
||||||
|
"h": 8,
|
||||||
|
"w": 12,
|
||||||
|
"x": 12,
|
||||||
|
"y": 10
|
||||||
|
},
|
||||||
|
"id": 8,
|
||||||
|
"options": {
|
||||||
|
"legend": {
|
||||||
|
"calcs": [],
|
||||||
|
"displayMode": "list",
|
||||||
|
"placement": "bottom",
|
||||||
|
"showLegend": false
|
||||||
|
},
|
||||||
|
"tooltip": {
|
||||||
|
"hideZeros": false,
|
||||||
|
"mode": "single",
|
||||||
|
"sort": "none"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"pluginVersion": "12.4.0-21693836646",
|
||||||
|
"targets": [
|
||||||
|
{
|
||||||
|
"editorMode": "code",
|
||||||
|
"expr": "sum(telemt_user_connections_current{user=\"$user\"}) by (user)",
|
||||||
|
"format": "time_series",
|
||||||
|
"legendFormat": "{{ user }}",
|
||||||
|
"range": true,
|
||||||
|
"refId": "A"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"title": "user_connections_current",
|
||||||
|
"type": "timeseries"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"datasource": {
|
||||||
|
"type": "prometheus",
|
||||||
|
"uid": "${datasource}"
|
||||||
|
},
|
||||||
|
"fieldConfig": {
|
||||||
|
"defaults": {
|
||||||
|
"color": {
|
||||||
|
"mode": "palette-classic"
|
||||||
|
},
|
||||||
|
"custom": {
|
||||||
|
"axisBorderShow": false,
|
||||||
|
"axisCenteredZero": false,
|
||||||
|
"axisColorMode": "text",
|
||||||
|
"axisLabel": "",
|
||||||
|
"axisPlacement": "auto",
|
||||||
|
"barAlignment": 0,
|
||||||
|
"barWidthFactor": 0.6,
|
||||||
|
"drawStyle": "line",
|
||||||
|
"fillOpacity": 0,
|
||||||
|
"gradientMode": "none",
|
||||||
|
"hideFrom": {
|
||||||
|
"legend": false,
|
||||||
|
"tooltip": false,
|
||||||
|
"viz": false
|
||||||
|
},
|
||||||
|
"insertNulls": false,
|
||||||
|
"lineInterpolation": "linear",
|
||||||
|
"lineWidth": 1,
|
||||||
|
"pointSize": 5,
|
||||||
|
"scaleDistribution": {
|
||||||
|
"type": "linear"
|
||||||
|
},
|
||||||
|
"showPoints": "auto",
|
||||||
|
"showValues": false,
|
||||||
|
"spanNulls": false,
|
||||||
|
"stacking": {
|
||||||
|
"group": "A",
|
||||||
|
"mode": "none"
|
||||||
|
},
|
||||||
|
"thresholdsStyle": {
|
||||||
|
"mode": "off"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"mappings": [],
|
||||||
|
"thresholds": {
|
||||||
|
"mode": "absolute",
|
||||||
|
"steps": [
|
||||||
|
{
|
||||||
|
"color": "green",
|
||||||
|
"value": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"color": "red",
|
||||||
|
"value": 80
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"unit": "binBps"
|
||||||
|
},
|
||||||
|
"overrides": []
|
||||||
|
},
|
||||||
|
"gridPos": {
|
||||||
|
"h": 8,
|
||||||
|
"w": 12,
|
||||||
|
"x": 0,
|
||||||
|
"y": 18
|
||||||
|
},
|
||||||
|
"id": 9,
|
||||||
|
"options": {
|
||||||
|
"legend": {
|
||||||
|
"calcs": [],
|
||||||
|
"displayMode": "list",
|
||||||
|
"placement": "bottom",
|
||||||
|
"showLegend": false
|
||||||
|
},
|
||||||
|
"tooltip": {
|
||||||
|
"hideZeros": false,
|
||||||
|
"mode": "single",
|
||||||
|
"sort": "none"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"pluginVersion": "12.4.0-21693836646",
|
||||||
|
"targets": [
|
||||||
|
{
|
||||||
|
"editorMode": "code",
|
||||||
|
"expr": "- sum(rate(telemt_user_octets_from_client{user=\"$user\"}[$__rate_interval])) by (user)",
|
||||||
|
"format": "time_series",
|
||||||
|
"legendFormat": "{{ user }} TX",
|
||||||
|
"range": true,
|
||||||
|
"refId": "A"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"datasource": {
|
||||||
|
"type": "prometheus",
|
||||||
|
"uid": "${datasource}"
|
||||||
|
},
|
||||||
|
"editorMode": "code",
|
||||||
|
"expr": "sum(rate(telemt_user_octets_to_client{user=\"$user\"}[$__rate_interval])) by (user)",
|
||||||
|
"format": "time_series",
|
||||||
|
"legendFormat": "{{ user }} RX",
|
||||||
|
"range": true,
|
||||||
|
"refId": "B"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"title": "user_octets",
|
||||||
|
"type": "timeseries"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"datasource": {
|
||||||
|
"type": "prometheus",
|
||||||
|
"uid": "${datasource}"
|
||||||
|
},
|
||||||
|
"fieldConfig": {
|
||||||
|
"defaults": {
|
||||||
|
"color": {
|
||||||
|
"mode": "palette-classic"
|
||||||
|
},
|
||||||
|
"custom": {
|
||||||
|
"axisBorderShow": false,
|
||||||
|
"axisCenteredZero": false,
|
||||||
|
"axisColorMode": "text",
|
||||||
|
"axisLabel": "",
|
||||||
|
"axisPlacement": "auto",
|
||||||
|
"barAlignment": 0,
|
||||||
|
"barWidthFactor": 0.6,
|
||||||
|
"drawStyle": "line",
|
||||||
|
"fillOpacity": 0,
|
||||||
|
"gradientMode": "none",
|
||||||
|
"hideFrom": {
|
||||||
|
"legend": false,
|
||||||
|
"tooltip": false,
|
||||||
|
"viz": false
|
||||||
|
},
|
||||||
|
"insertNulls": false,
|
||||||
|
"lineInterpolation": "linear",
|
||||||
|
"lineWidth": 1,
|
||||||
|
"pointSize": 5,
|
||||||
|
"scaleDistribution": {
|
||||||
|
"type": "linear"
|
||||||
|
},
|
||||||
|
"showPoints": "auto",
|
||||||
|
"showValues": false,
|
||||||
|
"spanNulls": false,
|
||||||
|
"stacking": {
|
||||||
|
"group": "A",
|
||||||
|
"mode": "none"
|
||||||
|
},
|
||||||
|
"thresholdsStyle": {
|
||||||
|
"mode": "off"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"mappings": [],
|
||||||
|
"thresholds": {
|
||||||
|
"mode": "absolute",
|
||||||
|
"steps": [
|
||||||
|
{
|
||||||
|
"color": "green",
|
||||||
|
"value": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"color": "red",
|
||||||
|
"value": 80
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"unit": "pps"
|
||||||
|
},
|
||||||
|
"overrides": []
|
||||||
|
},
|
||||||
|
"gridPos": {
|
||||||
|
"h": 8,
|
||||||
|
"w": 12,
|
||||||
|
"x": 12,
|
||||||
|
"y": 18
|
||||||
|
},
|
||||||
|
"id": 10,
|
||||||
|
"options": {
|
||||||
|
"legend": {
|
||||||
|
"calcs": [],
|
||||||
|
"displayMode": "list",
|
||||||
|
"placement": "bottom",
|
||||||
|
"showLegend": false
|
||||||
|
},
|
||||||
|
"tooltip": {
|
||||||
|
"hideZeros": false,
|
||||||
|
"mode": "single",
|
||||||
|
"sort": "none"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"pluginVersion": "12.4.0-21693836646",
|
||||||
|
"targets": [
|
||||||
|
{
|
||||||
|
"editorMode": "code",
|
||||||
|
"expr": "- sum(rate(telemt_user_msgs_from_client{user=\"$user\"}[$__rate_interval])) by (user)",
|
||||||
|
"format": "time_series",
|
||||||
|
"legendFormat": "{{ user }} TX",
|
||||||
|
"range": true,
|
||||||
|
"refId": "A"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"datasource": {
|
||||||
|
"type": "prometheus",
|
||||||
|
"uid": "${datasource}"
|
||||||
|
},
|
||||||
|
"editorMode": "code",
|
||||||
|
"expr": "sum(rate(telemt_user_msgs_to_client{user=\"$user\"}[$__rate_interval])) by (user)",
|
||||||
|
"format": "time_series",
|
||||||
|
"legendFormat": "{{ user }} RX",
|
||||||
|
"range": true,
|
||||||
|
"refId": "B"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"title": "user_msgs",
|
||||||
|
"type": "timeseries"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"preload": false,
|
||||||
|
"schemaVersion": 42,
|
||||||
|
"tags": [],
|
||||||
|
"templating": {
|
||||||
|
"list": [
|
||||||
|
{
|
||||||
|
"current": {
|
||||||
|
"text": "docker",
|
||||||
|
"value": "docker"
|
||||||
|
},
|
||||||
|
"datasource": {
|
||||||
|
"type": "prometheus",
|
||||||
|
"uid": "${datasource}"
|
||||||
|
},
|
||||||
|
"definition": "label_values(telemt_user_connections_total,user)",
|
||||||
|
"hide": 2,
|
||||||
|
"multi": true,
|
||||||
|
"name": "user",
|
||||||
|
"options": [],
|
||||||
|
"query": {
|
||||||
|
"qryType": 1,
|
||||||
|
"query": "label_values(telemt_user_connections_total,user)",
|
||||||
|
"refId": "VariableQueryEditor-VariableQuery"
|
||||||
|
},
|
||||||
|
"refresh": 1,
|
||||||
|
"regex": "",
|
||||||
|
"regexApplyTo": "value",
|
||||||
|
"sort": 1,
|
||||||
|
"type": "query"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"current": {
|
||||||
|
"text": "VM long-term",
|
||||||
|
"value": "P7D3016A027385E71"
|
||||||
|
},
|
||||||
|
"name": "datasource",
|
||||||
|
"options": [],
|
||||||
|
"query": "prometheus",
|
||||||
|
"refresh": 1,
|
||||||
|
"regex": "",
|
||||||
|
"type": "datasource"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"time": {
|
||||||
|
"from": "now-6h",
|
||||||
|
"to": "now"
|
||||||
|
},
|
||||||
|
"timepicker": {},
|
||||||
|
"timezone": "browser",
|
||||||
|
"title": "Telemt MtProto proxy",
|
||||||
|
"weekStart": ""
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user