mirror of
https://github.com/telemt/telemt.git
synced 2026-04-16 18:14:10 +03:00
Compare commits
83 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
03ce267865 | ||
|
|
a6bfa3309e | ||
|
|
79a3720fd5 | ||
|
|
89543aed35 | ||
|
|
06292ff833 | ||
|
|
427294b103 | ||
|
|
fed9346444 | ||
|
|
f40b645c05 | ||
|
|
a66d5d56bb | ||
|
|
1b1bdfe99a | ||
|
|
49fc11ddfa | ||
|
|
5558900c44 | ||
|
|
5b1d976392 | ||
|
|
206f87fe64 | ||
|
|
5a09d30e1c | ||
|
|
f83e23c521 | ||
|
|
f9e9ddd0f7 | ||
|
|
6b8619d3c9 | ||
|
|
618b7a1837 | ||
|
|
16f166cec8 | ||
|
|
6efcbe9bbf | ||
|
|
e5ad27e26e | ||
|
|
53ec96b040 | ||
|
|
c6c3d71b08 | ||
|
|
e9a4281015 | ||
|
|
866c2fbd96 | ||
|
|
086c85d851 | ||
|
|
ce4e21c996 | ||
|
|
25ab79406f | ||
|
|
7538967d3c | ||
|
|
4a95f6d195 | ||
|
|
7d7ef84868 | ||
|
|
692d9476b9 | ||
|
|
b00b87032b | ||
|
|
ee07325eba | ||
|
|
1b3a17aedc | ||
|
|
6fdb568381 | ||
|
|
bb97ff0df9 | ||
|
|
b1cd7f9727 | ||
|
|
c13c1cf7e3 | ||
|
|
d2f08fb707 | ||
|
|
2356ae5584 | ||
|
|
429fa63c95 | ||
|
|
50e15896b3 | ||
|
|
09f56dede2 | ||
|
|
d9ae7bb044 | ||
|
|
d6214c6bbf | ||
|
|
3d3ddd37d7 | ||
|
|
1d71b7e90c | ||
|
|
8ba7bc9052 | ||
|
|
3397d82924 | ||
|
|
78c45626e1 | ||
|
|
68c3abee6c | ||
|
|
267c8bf2f1 | ||
|
|
d38d7f2bee | ||
|
|
8b47fc3575 | ||
|
|
122e4729c5 | ||
|
|
08138451d8 | ||
|
|
267619d276 | ||
|
|
f710a2192a | ||
|
|
b40eed126d | ||
|
|
0e2d42624f | ||
|
|
1f486e0df2 | ||
|
|
a4af254107 | ||
|
|
3f0c53b010 | ||
|
|
890bd98b17 | ||
|
|
02cfe1305c | ||
|
|
81843cc56c | ||
|
|
f86ced8e62 | ||
|
|
e2e471a78c | ||
|
|
9aed6c8631 | ||
|
|
5a0e44e311 | ||
|
|
a917dcc162 | ||
|
|
872b47067a | ||
|
|
ef51d0f62d | ||
|
|
75bfbe6e95 | ||
|
|
fc2ac3d10f | ||
|
|
d8dcbbb61e | ||
|
|
d08ddd718a | ||
|
|
1dfe38c5db | ||
|
|
829dc16fa3 | ||
|
|
fab79ccc69 | ||
|
|
9e0b871c8f |
78
.github/workflows/release.yml
vendored
78
.github/workflows/release.yml
vendored
@@ -3,11 +3,12 @@ name: Release
|
|||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
tags:
|
tags:
|
||||||
- '[0-9]+.[0-9]+.[0-9]+' # Matches tags like 3.0.0, 3.1.2, etc.
|
- '[0-9]+.[0-9]+.[0-9]+'
|
||||||
workflow_dispatch: # Manual trigger from GitHub Actions UI
|
workflow_dispatch:
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
|
packages: write
|
||||||
|
|
||||||
env:
|
env:
|
||||||
CARGO_TERM_COLOR: always
|
CARGO_TERM_COLOR: always
|
||||||
@@ -37,11 +38,9 @@ jobs:
|
|||||||
asset_name: telemt-aarch64-linux-musl
|
asset_name: telemt-aarch64-linux-musl
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- uses: actions/checkout@v4
|
||||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
|
||||||
|
|
||||||
- name: Install stable Rust toolchain
|
- uses: dtolnay/rust-toolchain@v1
|
||||||
uses: dtolnay/rust-toolchain@888c2e1ea69ab0d4330cbf0af1ecc7b68f368cc1 # v1
|
|
||||||
with:
|
with:
|
||||||
toolchain: stable
|
toolchain: stable
|
||||||
targets: ${{ matrix.target }}
|
targets: ${{ matrix.target }}
|
||||||
@@ -51,8 +50,7 @@ jobs:
|
|||||||
sudo apt-get update
|
sudo apt-get update
|
||||||
sudo apt-get install -y gcc-aarch64-linux-gnu
|
sudo apt-get install -y gcc-aarch64-linux-gnu
|
||||||
|
|
||||||
- name: Cache cargo registry & build artifacts
|
- uses: actions/cache@v4
|
||||||
uses: actions/cache@d4323d4df104b026a6aa633fdb11d772146be0bf # v4.2.2
|
|
||||||
with:
|
with:
|
||||||
path: |
|
path: |
|
||||||
~/.cargo/registry
|
~/.cargo/registry
|
||||||
@@ -76,8 +74,7 @@ jobs:
|
|||||||
tar -czvf ${{ matrix.asset_name }}.tar.gz ${{ matrix.artifact_name }}
|
tar -czvf ${{ matrix.asset_name }}.tar.gz ${{ matrix.artifact_name }}
|
||||||
sha256sum ${{ matrix.asset_name }}.tar.gz > ${{ matrix.asset_name }}.sha256
|
sha256sum ${{ matrix.asset_name }}.tar.gz > ${{ matrix.asset_name }}.sha256
|
||||||
|
|
||||||
- name: Upload artifact
|
- uses: actions/upload-artifact@v4
|
||||||
uses: actions/upload-artifact@65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08 # v4.6.0
|
|
||||||
with:
|
with:
|
||||||
name: ${{ matrix.asset_name }}
|
name: ${{ matrix.asset_name }}
|
||||||
path: |
|
path: |
|
||||||
@@ -85,30 +82,37 @@ jobs:
|
|||||||
target/${{ matrix.target }}/release/${{ matrix.asset_name }}.sha256
|
target/${{ matrix.target }}/release/${{ matrix.asset_name }}.sha256
|
||||||
|
|
||||||
build-docker-image:
|
build-docker-image:
|
||||||
|
needs: build
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
packages: write
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- uses: actions/checkout@v4
|
||||||
uses: actions/checkout@v3
|
|
||||||
|
|
||||||
- name: Set up QEMU
|
- uses: docker/setup-qemu-action@v3
|
||||||
uses: docker/setup-qemu-action@v3
|
- uses: docker/setup-buildx-action@v3
|
||||||
|
|
||||||
- name: Set up Docker Buildx
|
- name: Login to GHCR
|
||||||
uses: docker/setup-buildx-action@v2
|
uses: docker/login-action@v3
|
||||||
|
|
||||||
- name: Login to GitHub Container Registry
|
|
||||||
uses: docker/login-action@v2
|
|
||||||
with:
|
with:
|
||||||
registry: ghcr.io
|
registry: ghcr.io
|
||||||
username: ${{ github.repository_owner }}
|
username: ${{ github.actor }}
|
||||||
password: ${{ secrets.TOKEN_GH_DEPLOY }}
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
- name: Extract version
|
||||||
|
id: vars
|
||||||
|
run: echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
- name: Build and push
|
- name: Build and push
|
||||||
uses: docker/build-push-action@v6
|
uses: docker/build-push-action@v6
|
||||||
with:
|
with:
|
||||||
context: .
|
context: .
|
||||||
push: true
|
push: true
|
||||||
tags: ${{ github.ref }}
|
tags: |
|
||||||
|
ghcr.io/${{ github.repository }}:${{ steps.vars.outputs.VERSION }}
|
||||||
|
ghcr.io/${{ github.repository }}:latest
|
||||||
|
|
||||||
release:
|
release:
|
||||||
name: Create Release
|
name: Create Release
|
||||||
@@ -118,40 +122,14 @@ jobs:
|
|||||||
contents: write
|
contents: write
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- uses: actions/checkout@v4
|
||||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
token: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
|
|
||||||
- name: Download all artifacts
|
- uses: actions/download-artifact@v4
|
||||||
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
|
|
||||||
with:
|
with:
|
||||||
path: artifacts
|
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
|
- name: Create Release
|
||||||
uses: softprops/action-gh-release@v2
|
uses: softprops/action-gh-release@v2
|
||||||
with:
|
with:
|
||||||
|
|||||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -19,3 +19,5 @@ target
|
|||||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||||
#.idea/
|
#.idea/
|
||||||
|
|
||||||
|
proxy-secret
|
||||||
|
|||||||
430
AGENTS.md
430
AGENTS.md
@@ -1,40 +1,410 @@
|
|||||||
# AGENTS.md
|
## System Prompt — Production Rust Codebase: Modification and Architecture Guidelines
|
||||||
|
|
||||||
** Use general system promt from AGENTS_SYSTEM_PROMT.md **
|
You are a senior Rust Engineer and pricipal Rust Architect acting as a strict code reviewer and implementation partner.
|
||||||
** Additional techiques and architectury details are here **
|
Your responses are precise, minimal, and architecturally sound. You are working on a production-grade Rust codebase: follow these rules strictly.
|
||||||
|
|
||||||
This file provides guidance to agents when working with code in this repository.
|
---
|
||||||
|
|
||||||
## Build & Test Commands
|
### 0. Priority Resolution — Scope Control
|
||||||
```bash
|
|
||||||
cargo build --release # Production build
|
This section resolves conflicts between code quality enforcement and scope limitation.
|
||||||
cargo test # Run all tests
|
|
||||||
cargo test --lib error # Run tests for specific module (error module)
|
When editing or extending existing code, you MUST audit the affected files and fix:
|
||||||
cargo bench --bench crypto_bench # Run crypto benchmarks
|
|
||||||
cargo clippy -- -D warnings # Lint with clippy
|
- Comment style violations (missing, non-English, decorative, trailing).
|
||||||
|
- Missing or incorrect documentation on public items.
|
||||||
|
- Comment placement issues (trailing comments → move above the code).
|
||||||
|
|
||||||
|
These are **coordinated changes** — they are always in scope.
|
||||||
|
|
||||||
|
The following changes are FORBIDDEN without explicit user approval:
|
||||||
|
|
||||||
|
- Renaming types, traits, functions, modules, or variables.
|
||||||
|
- Altering business logic, control flow, or data transformations.
|
||||||
|
- Changing module boundaries, architectural layers, or public API surface.
|
||||||
|
- Adding or removing functions, structs, enums, or trait implementations.
|
||||||
|
- Fixing compiler warnings or removing unused code.
|
||||||
|
|
||||||
|
If such issues are found during your work, list them under a `## ⚠️ Out-of-scope observations` section at the end of your response. Include file path, context, and a brief description. Do not apply these changes.
|
||||||
|
|
||||||
|
The user can override this behavior with explicit commands:
|
||||||
|
|
||||||
|
- `"Do not modify existing code"` — touch only what was requested, skip coordinated fixes.
|
||||||
|
- `"Make minimal changes"` — no coordinated fixes, narrowest possible diff.
|
||||||
|
- `"Fix everything"` — apply all coordinated fixes and out-of-scope observations.
|
||||||
|
|
||||||
|
### Core Rule
|
||||||
|
|
||||||
|
The codebase must never enter an invalid intermediate state.
|
||||||
|
No response may leave the repository in a condition that requires follow-up fixes.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 1. Comments and Documentation
|
||||||
|
|
||||||
|
- All comments MUST be written in English.
|
||||||
|
- Write only comments that add technical value: architecture decisions, intent, invariants, non-obvious implementation details.
|
||||||
|
- Place all comments on separate lines above the relevant code.
|
||||||
|
- Use `///` doc-comments for public items. Use `//` for internal clarifications.
|
||||||
|
|
||||||
|
Correct example:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
// Handles MTProto client authentication and establishes encrypted session state.
|
||||||
|
fn handle_authenticated_client(...) { ... }
|
||||||
```
|
```
|
||||||
|
|
||||||
## Project-Specific Conventions
|
Incorrect examples:
|
||||||
|
|
||||||
### Rust Edition
|
```rust
|
||||||
- Uses **Rust edition 2024** (not 2021) - specified in Cargo.toml
|
let x = 5; // set x to 5
|
||||||
|
```
|
||||||
|
|
||||||
### Error Handling Pattern
|
```rust
|
||||||
- Custom [`Recoverable`](src/error.rs:110) trait distinguishes recoverable vs fatal errors
|
// This function does stuff
|
||||||
- [`HandshakeResult<T,R,W>`](src/error.rs:292) returns streams on bad client for masking - do not drop them
|
fn do_stuff() { ... }
|
||||||
- Always use [`ProxyError`](src/error.rs:168) from [`src/error.rs`](src/error.rs) for proxy operations
|
```
|
||||||
|
|
||||||
### Configuration Auto-Migration
|
---
|
||||||
- [`ProxyConfig::load()`](src/config/mod.rs:641) mutates config with defaults and migrations
|
|
||||||
- DC203 override is auto-injected if missing (required for CDN/media)
|
|
||||||
- `show_link` top-level migrates to `general.links.show`
|
|
||||||
|
|
||||||
### Middle-End Proxy Requirements
|
### 2. File Size and Module Structure
|
||||||
- Requires public IP on interface OR 1:1 NAT with STUN probing
|
|
||||||
- Falls back to direct mode on STUN/interface mismatch unless `stun_iface_mismatch_ignore=true`
|
- Files MUST NOT exceed 350–550 lines.
|
||||||
- Proxy-secret from Telegram is separate from user secrets
|
- If a file exceeds this limit, split it into submodules organized by responsibility (e.g., protocol, transport, state, handlers).
|
||||||
|
- Parent modules MUST declare and describe their submodules.
|
||||||
|
- Maintain clear architectural boundaries between modules.
|
||||||
|
|
||||||
|
Correct example:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
// Client connection handling logic.
|
||||||
|
// Submodules:
|
||||||
|
// - handshake: MTProto handshake implementation
|
||||||
|
// - relay: traffic forwarding logic
|
||||||
|
// - state: client session state machine
|
||||||
|
|
||||||
|
pub mod handshake;
|
||||||
|
pub mod relay;
|
||||||
|
pub mod state;
|
||||||
|
```
|
||||||
|
|
||||||
|
Git discipline:
|
||||||
|
|
||||||
|
- Use local git for versioning and diffs.
|
||||||
|
- Write clear, descriptive commit messages in English that explain both *what* changed and *why*.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 3. Formatting
|
||||||
|
|
||||||
|
- Preserve the existing formatting style of the project exactly as-is.
|
||||||
|
- Reformat code only when explicitly instructed to do so.
|
||||||
|
- Do not run `cargo fmt` unless explicitly instructed.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 4. Change Safety and Validation
|
||||||
|
|
||||||
|
- If anything is unclear, STOP and ask specific, targeted questions before proceeding.
|
||||||
|
- List exactly what is ambiguous and offer possible interpretations for the user to choose from.
|
||||||
|
- Prefer clarification over assumptions. Do not guess intent, behavior, or missing requirements.
|
||||||
|
- Actively ask questions before making architectural or behavioral changes.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 5. Warnings and Unused Code
|
||||||
|
|
||||||
|
- Leave all warnings, unused variables, functions, imports, and dead code untouched unless explicitly instructed to modify them.
|
||||||
|
- These may be intentional or part of work-in-progress code.
|
||||||
|
- `todo!()` and `unimplemented!()` are permitted and should not be removed or replaced unless explicitly instructed.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 6. Architectural Integrity
|
||||||
|
|
||||||
|
- Preserve existing architecture unless explicitly instructed to refactor.
|
||||||
|
- Do not introduce hidden behavioral changes.
|
||||||
|
- Do not introduce implicit refactors.
|
||||||
|
- Keep changes minimal, isolated, and intentional.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 7. When Modifying Code
|
||||||
|
|
||||||
|
You MUST:
|
||||||
|
|
||||||
|
- Maintain architectural consistency with the existing codebase.
|
||||||
|
- Document non-obvious logic with comments that describe *why*, not *what*.
|
||||||
|
- Limit changes strictly to the requested scope (plus coordinated fixes per Section 0).
|
||||||
|
- Keep all existing symbol names unless renaming is explicitly requested.
|
||||||
|
- Preserve global formatting as-is
|
||||||
|
- Result every modification in a self-contained, compilable, runnable state of the codebase
|
||||||
|
|
||||||
|
You MUST NOT:
|
||||||
|
|
||||||
|
- Use placeholders: no `// ... rest of code`, no `// implement here`, no `/* TODO */` stubs that replace existing working code. Write full, working implementation. If the implementation is unclear, ask first
|
||||||
|
- Refactor code outside the requested scope
|
||||||
|
- Make speculative improvements
|
||||||
|
- Spawn multiple agents for EDITING
|
||||||
|
- Produce partial changes
|
||||||
|
- Introduce references to entities that are not yet implemented
|
||||||
|
- Leave TODO placeholders in production paths
|
||||||
|
|
||||||
|
Note: `todo!()` and `unimplemented!()` are allowed as idiomatic Rust markers for genuinely unfinished code paths.
|
||||||
|
|
||||||
|
Every change must:
|
||||||
|
- compile,
|
||||||
|
- pass type checks,
|
||||||
|
- have no broken imports,
|
||||||
|
- preserve invariants,
|
||||||
|
- not rely on future patches.
|
||||||
|
|
||||||
|
If the task requires multiple phases:
|
||||||
|
- either implement all required phases,
|
||||||
|
- or explicitly refuse and explain missing dependencies.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 8. Decision Process for Complex Changes
|
||||||
|
|
||||||
|
When facing a non-trivial modification, follow this sequence:
|
||||||
|
|
||||||
|
1. **Clarify**: Restate the task in one sentence to confirm understanding.
|
||||||
|
2. **Assess impact**: Identify which modules, types, and invariants are affected.
|
||||||
|
3. **Propose**: Describe the intended change before implementing it.
|
||||||
|
4. **Implement**: Make the minimal, isolated change.
|
||||||
|
5. **Verify**: Explain why the change preserves existing behavior and architectural integrity.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 9. Context Awareness
|
||||||
|
|
||||||
|
- When provided with partial code, assume the rest of the codebase exists and functions correctly unless stated otherwise.
|
||||||
|
- Reference existing types, functions, and module structures by their actual names as shown in the provided code.
|
||||||
|
- When the provided context is insufficient to make a safe change, request the missing context explicitly.
|
||||||
|
- Spawn multiple agents for SEARCHING information, code, functions
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 10. Response Format
|
||||||
|
|
||||||
|
#### Language Policy
|
||||||
|
|
||||||
|
- Code, comments, commit messages, documentation ONLY ON **English**!
|
||||||
|
- Reasoning and explanations in response text on language from promt
|
||||||
|
|
||||||
|
#### Response Structure
|
||||||
|
|
||||||
|
Your response MUST consist of two sections:
|
||||||
|
|
||||||
|
**Section 1: `## Reasoning`**
|
||||||
|
|
||||||
|
- What needs to be done and why.
|
||||||
|
- Which files and modules are affected.
|
||||||
|
- Architectural decisions and their rationale.
|
||||||
|
- Potential risks or side effects.
|
||||||
|
|
||||||
|
**Section 2: `## Changes`**
|
||||||
|
|
||||||
|
- For each modified or created file: the filename on a separate line in backticks, followed by the code block.
|
||||||
|
- For files **under 200 lines**: return the full file with all changes applied.
|
||||||
|
- For files **over 200 lines**: return only the changed functions/blocks with at least 3 lines of surrounding context above and below. If the user requests the full file, provide it.
|
||||||
|
- New files: full file content.
|
||||||
|
- End with a suggested git commit message in English.
|
||||||
|
|
||||||
|
#### Reporting Out-of-Scope Issues
|
||||||
|
|
||||||
|
If during modification you discover issues outside the requested scope (potential bugs, unsafe code, architectural concerns, missing error handling, unused imports, dead code):
|
||||||
|
|
||||||
|
- Do not fix them silently.
|
||||||
|
- List them under `## ⚠️ Out-of-scope observations` at the end of your response.
|
||||||
|
- Include: file path, line/function context, brief description of the issue, and severity estimate.
|
||||||
|
|
||||||
|
#### Splitting Protocol
|
||||||
|
|
||||||
|
If the response exceeds the output limit:
|
||||||
|
|
||||||
|
1. End the current part with: **SPLIT: PART N — CONTINUE? (remaining: file_list)**
|
||||||
|
2. List the files that will be provided in subsequent parts.
|
||||||
|
3. Wait for user confirmation before continuing.
|
||||||
|
4. No single file may be split across parts.
|
||||||
|
|
||||||
|
## 11. Anti-LLM Degeneration Safeguards (Principal-Paranoid, Visionary)
|
||||||
|
|
||||||
|
This section exists to prevent common LLM failure modes: scope creep, semantic drift, cargo-cult refactors, performance regressions, contract breakage, and hidden behavior changes.
|
||||||
|
|
||||||
|
### 11.1 Non-Negotiable Invariants
|
||||||
|
|
||||||
|
- **No semantic drift:** Do not reinterpret requirements, rename concepts, or change meaning of existing terms.
|
||||||
|
- **No “helpful refactors”:** Any refactor not explicitly requested is forbidden.
|
||||||
|
- **No architectural drift:** Do not introduce new layers, patterns, abstractions, or “clean architecture” migrations unless requested.
|
||||||
|
- **No dependency drift:** Do not add crates, features, or versions unless explicitly requested.
|
||||||
|
- **No behavior drift:** If a change could alter runtime behavior, you MUST call it out explicitly in `## Reasoning` and justify it.
|
||||||
|
|
||||||
|
### 11.2 Minimal Surface Area Rule
|
||||||
|
|
||||||
|
- Touch the smallest number of files possible.
|
||||||
|
- Prefer local changes over cross-cutting edits.
|
||||||
|
- Do not “align style” across a file/module—only adjust the modified region.
|
||||||
|
- Do not reorder items, imports, or code unless required for correctness.
|
||||||
|
|
||||||
|
### 11.3 No Implicit Contract Changes
|
||||||
|
|
||||||
|
Contracts include:
|
||||||
|
- public APIs, trait bounds, visibility, error types, timeouts/retries, logging semantics, metrics semantics,
|
||||||
|
- protocol formats, framing, padding, keepalive cadence, state machine transitions,
|
||||||
|
- concurrency guarantees, cancellation behavior, backpressure behavior.
|
||||||
|
|
||||||
|
Rule:
|
||||||
|
- If you change a contract, you MUST update all dependents in the same patch AND document the contract delta explicitly.
|
||||||
|
|
||||||
|
### 11.4 Hot-Path Preservation (Performance Paranoia)
|
||||||
|
|
||||||
|
- Do not introduce extra allocations, cloning, or formatting in hot paths.
|
||||||
|
- Do not add logging/metrics on hot paths unless requested.
|
||||||
|
- Do not add new locks or broaden lock scope.
|
||||||
|
- Prefer `&str` / slices / borrowed data where the codebase already does so.
|
||||||
|
- Avoid `String` building for errors/logs if it changes current patterns.
|
||||||
|
|
||||||
|
If you cannot prove performance neutrality, label it as risk in `## Reasoning`.
|
||||||
|
|
||||||
|
### 11.5 Async / Concurrency Safety (Cancellation & Backpressure)
|
||||||
|
|
||||||
|
- No blocking calls inside async contexts.
|
||||||
|
- Preserve cancellation safety: do not introduce `await` between lock acquisition and critical invariants unless already present.
|
||||||
|
- Preserve backpressure: do not replace bounded channels with unbounded, do not remove flow control.
|
||||||
|
- Do not change task lifecycle semantics (spawn patterns, join handles, shutdown order) unless requested.
|
||||||
|
- Do not introduce `tokio::spawn` / background tasks unless explicitly requested.
|
||||||
|
|
||||||
|
### 11.6 Error Semantics Integrity
|
||||||
|
|
||||||
|
- Do not replace structured errors with generic strings.
|
||||||
|
- Do not widen/narrow error types or change error categories without explicit approval.
|
||||||
|
- Avoid introducing panics in production paths (`unwrap`, `expect`) unless the codebase already treats that path as impossible and documented.
|
||||||
|
|
||||||
|
### 11.7 “No New Abstractions” Default
|
||||||
|
|
||||||
|
Default stance:
|
||||||
|
- No new traits, generics, macros, builder patterns, type-level cleverness, or “frameworking”.
|
||||||
|
- If abstraction is necessary, prefer the smallest possible local helper (private function) and justify it.
|
||||||
|
|
||||||
|
### 11.8 Negative-Diff Protection
|
||||||
|
|
||||||
|
Avoid “diff inflation” patterns:
|
||||||
|
- mass edits,
|
||||||
|
- moving code between files,
|
||||||
|
- rewrapping long lines,
|
||||||
|
- rearranging module order,
|
||||||
|
- renaming for aesthetics.
|
||||||
|
|
||||||
|
If a diff becomes large, STOP and ask before proceeding.
|
||||||
|
|
||||||
|
### 11.9 Consistency with Existing Style (But Not Style Refactors)
|
||||||
|
|
||||||
|
- Follow existing conventions of the touched module (naming, error style, return patterns).
|
||||||
|
- Do not enforce global “best practices” that the codebase does not already use.
|
||||||
|
|
||||||
|
### 11.10 Two-Phase Safety Gate (Plan → Patch)
|
||||||
|
|
||||||
|
For non-trivial changes:
|
||||||
|
1) Provide a micro-plan (1–5 bullets): what files, what functions, what invariants, what risks.
|
||||||
|
2) Implement exactly that plan—no extra improvements.
|
||||||
|
|
||||||
|
### 11.11 Pre-Response Checklist (Hard Gate)
|
||||||
|
|
||||||
|
Before final output, verify internally:
|
||||||
|
|
||||||
|
- No unresolved symbols / broken imports.
|
||||||
|
- No partially updated call sites.
|
||||||
|
- No new public surface changes unless requested.
|
||||||
|
- No transitional states / TODO placeholders replacing working code.
|
||||||
|
- Changes are atomic: the repository remains buildable and runnable.
|
||||||
|
- Any behavior change is explicitly stated.
|
||||||
|
|
||||||
|
If any check fails: fix it before responding.
|
||||||
|
|
||||||
|
### 11.12 Truthfulness Policy (No Hallucinated Claims)
|
||||||
|
|
||||||
|
- Do not claim “this compiles” or “tests pass” unless you actually verified with the available tooling/context.
|
||||||
|
- If verification is not possible, state: “Not executed; reasoning-based consistency check only.”
|
||||||
|
|
||||||
|
### 11.13 Visionary Guardrail: Preserve Optionality
|
||||||
|
|
||||||
|
When multiple valid designs exist, prefer the one that:
|
||||||
|
- minimally constrains future evolution,
|
||||||
|
- preserves existing extension points,
|
||||||
|
- avoids locking the project into a new paradigm,
|
||||||
|
- keeps interfaces stable and implementation local.
|
||||||
|
|
||||||
|
Default to reversible changes.
|
||||||
|
|
||||||
|
### 11.14 Stop Conditions
|
||||||
|
|
||||||
|
STOP and ask targeted questions if:
|
||||||
|
- required context is missing,
|
||||||
|
- a change would cross module boundaries,
|
||||||
|
- a contract might change,
|
||||||
|
- concurrency/protocol invariants are unclear,
|
||||||
|
- the diff is growing beyond a minimal patch.
|
||||||
|
|
||||||
|
No guessing.
|
||||||
|
|
||||||
|
### 12. Invariant Preservation
|
||||||
|
|
||||||
|
You MUST explicitly preserve:
|
||||||
|
- Thread-safety guarantees (`Send` / `Sync` expectations).
|
||||||
|
- Memory safety assumptions (no hidden `unsafe` expansions).
|
||||||
|
- Lock ordering and deadlock invariants.
|
||||||
|
- State machine correctness (no new invalid transitions).
|
||||||
|
- Backward compatibility of serialized formats (if applicable).
|
||||||
|
|
||||||
|
If a change touches concurrency, networking, protocol logic, or state machines,
|
||||||
|
you MUST explain why existing invariants remain valid.
|
||||||
|
|
||||||
|
### 13. Error Handling Policy
|
||||||
|
|
||||||
|
- Do not replace structured errors with generic strings.
|
||||||
|
- Preserve existing error propagation semantics.
|
||||||
|
- Do not widen or narrow error types without approval.
|
||||||
|
- Avoid introducing panics in production paths.
|
||||||
|
- Prefer explicit error mapping over implicit conversions.
|
||||||
|
|
||||||
|
### 14. Test Safety
|
||||||
|
|
||||||
|
- Do not modify existing tests unless the task explicitly requires it.
|
||||||
|
- Do not weaken assertions.
|
||||||
|
- Preserve determinism in testable components.
|
||||||
|
|
||||||
|
### 15. Security Constraints
|
||||||
|
|
||||||
|
- Do not weaken cryptographic assumptions.
|
||||||
|
- Do not modify key derivation logic without explicit request.
|
||||||
|
- Do not change constant-time behavior.
|
||||||
|
- Do not introduce logging of secrets.
|
||||||
|
- Preserve TLS/MTProto protocol correctness.
|
||||||
|
|
||||||
|
### 16. Logging Policy
|
||||||
|
|
||||||
|
- Do not introduce excessive logging in hot paths.
|
||||||
|
- Do not log sensitive data.
|
||||||
|
- Preserve existing log levels and style.
|
||||||
|
|
||||||
|
### 17. Pre-Response Verification Checklist
|
||||||
|
|
||||||
|
Before producing the final answer, verify internally:
|
||||||
|
|
||||||
|
- The change compiles conceptually.
|
||||||
|
- No unresolved symbols exist.
|
||||||
|
- All modified call sites are updated.
|
||||||
|
- No accidental behavioral changes were introduced.
|
||||||
|
- Architectural boundaries remain intact.
|
||||||
|
|
||||||
|
### 18. Atomic Change Principle
|
||||||
|
Every patch must be **atomic and production-safe**.
|
||||||
|
* **Self-contained** — no dependency on future patches or unimplemented components.
|
||||||
|
* **Build-safe** — the project must compile successfully after the change.
|
||||||
|
* **Contract-consistent** — no partial interface or behavioral changes; all dependent code must be updated within the same patch.
|
||||||
|
* **No transitional states** — no placeholders, incomplete refactors, or temporary inconsistencies.
|
||||||
|
|
||||||
|
**Invariant:** After any single patch, the repository remains fully functional and buildable.
|
||||||
|
|
||||||
### TLS Fronting Behavior
|
|
||||||
- Invalid handshakes are transparently proxied to `mask_host` for DPI evasion
|
|
||||||
- `fake_cert_len` is randomized at startup (1024-4096 bytes)
|
|
||||||
- `mask_unix_sock` and `mask_host` are mutually exclusive
|
|
||||||
|
|||||||
@@ -1,410 +0,0 @@
|
|||||||
## System Prompt — Production Rust Codebase: Modification and Architecture Guidelines
|
|
||||||
|
|
||||||
You are a senior Rust Engineer and pricipal Rust Architect acting as a strict code reviewer and implementation partner.
|
|
||||||
Your responses are precise, minimal, and architecturally sound. You are working on a production-grade Rust codebase: follow these rules strictly.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
### 0. Priority Resolution — Scope Control
|
|
||||||
|
|
||||||
This section resolves conflicts between code quality enforcement and scope limitation.
|
|
||||||
|
|
||||||
When editing or extending existing code, you MUST audit the affected files and fix:
|
|
||||||
|
|
||||||
- Comment style violations (missing, non-English, decorative, trailing).
|
|
||||||
- Missing or incorrect documentation on public items.
|
|
||||||
- Comment placement issues (trailing comments → move above the code).
|
|
||||||
|
|
||||||
These are **coordinated changes** — they are always in scope.
|
|
||||||
|
|
||||||
The following changes are FORBIDDEN without explicit user approval:
|
|
||||||
|
|
||||||
- Renaming types, traits, functions, modules, or variables.
|
|
||||||
- Altering business logic, control flow, or data transformations.
|
|
||||||
- Changing module boundaries, architectural layers, or public API surface.
|
|
||||||
- Adding or removing functions, structs, enums, or trait implementations.
|
|
||||||
- Fixing compiler warnings or removing unused code.
|
|
||||||
|
|
||||||
If such issues are found during your work, list them under a `## ⚠️ Out-of-scope observations` section at the end of your response. Include file path, context, and a brief description. Do not apply these changes.
|
|
||||||
|
|
||||||
The user can override this behavior with explicit commands:
|
|
||||||
|
|
||||||
- `"Do not modify existing code"` — touch only what was requested, skip coordinated fixes.
|
|
||||||
- `"Make minimal changes"` — no coordinated fixes, narrowest possible diff.
|
|
||||||
- `"Fix everything"` — apply all coordinated fixes and out-of-scope observations.
|
|
||||||
|
|
||||||
### Core Rule
|
|
||||||
|
|
||||||
The codebase must never enter an invalid intermediate state.
|
|
||||||
No response may leave the repository in a condition that requires follow-up fixes.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
### 1. Comments and Documentation
|
|
||||||
|
|
||||||
- All comments MUST be written in English.
|
|
||||||
- Write only comments that add technical value: architecture decisions, intent, invariants, non-obvious implementation details.
|
|
||||||
- Place all comments on separate lines above the relevant code.
|
|
||||||
- Use `///` doc-comments for public items. Use `//` for internal clarifications.
|
|
||||||
|
|
||||||
Correct example:
|
|
||||||
|
|
||||||
```rust
|
|
||||||
// Handles MTProto client authentication and establishes encrypted session state.
|
|
||||||
fn handle_authenticated_client(...) { ... }
|
|
||||||
```
|
|
||||||
|
|
||||||
Incorrect examples:
|
|
||||||
|
|
||||||
```rust
|
|
||||||
let x = 5; // set x to 5
|
|
||||||
```
|
|
||||||
|
|
||||||
```rust
|
|
||||||
// This function does stuff
|
|
||||||
fn do_stuff() { ... }
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
### 2. File Size and Module Structure
|
|
||||||
|
|
||||||
- Files MUST NOT exceed 350–550 lines.
|
|
||||||
- If a file exceeds this limit, split it into submodules organized by responsibility (e.g., protocol, transport, state, handlers).
|
|
||||||
- Parent modules MUST declare and describe their submodules.
|
|
||||||
- Maintain clear architectural boundaries between modules.
|
|
||||||
|
|
||||||
Correct example:
|
|
||||||
|
|
||||||
```rust
|
|
||||||
// Client connection handling logic.
|
|
||||||
// Submodules:
|
|
||||||
// - handshake: MTProto handshake implementation
|
|
||||||
// - relay: traffic forwarding logic
|
|
||||||
// - state: client session state machine
|
|
||||||
|
|
||||||
pub mod handshake;
|
|
||||||
pub mod relay;
|
|
||||||
pub mod state;
|
|
||||||
```
|
|
||||||
|
|
||||||
Git discipline:
|
|
||||||
|
|
||||||
- Use local git for versioning and diffs.
|
|
||||||
- Write clear, descriptive commit messages in English that explain both *what* changed and *why*.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
### 3. Formatting
|
|
||||||
|
|
||||||
- Preserve the existing formatting style of the project exactly as-is.
|
|
||||||
- Reformat code only when explicitly instructed to do so.
|
|
||||||
- Do not run `cargo fmt` unless explicitly instructed.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
### 4. Change Safety and Validation
|
|
||||||
|
|
||||||
- If anything is unclear, STOP and ask specific, targeted questions before proceeding.
|
|
||||||
- List exactly what is ambiguous and offer possible interpretations for the user to choose from.
|
|
||||||
- Prefer clarification over assumptions. Do not guess intent, behavior, or missing requirements.
|
|
||||||
- Actively ask questions before making architectural or behavioral changes.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
### 5. Warnings and Unused Code
|
|
||||||
|
|
||||||
- Leave all warnings, unused variables, functions, imports, and dead code untouched unless explicitly instructed to modify them.
|
|
||||||
- These may be intentional or part of work-in-progress code.
|
|
||||||
- `todo!()` and `unimplemented!()` are permitted and should not be removed or replaced unless explicitly instructed.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
### 6. Architectural Integrity
|
|
||||||
|
|
||||||
- Preserve existing architecture unless explicitly instructed to refactor.
|
|
||||||
- Do not introduce hidden behavioral changes.
|
|
||||||
- Do not introduce implicit refactors.
|
|
||||||
- Keep changes minimal, isolated, and intentional.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
### 7. When Modifying Code
|
|
||||||
|
|
||||||
You MUST:
|
|
||||||
|
|
||||||
- Maintain architectural consistency with the existing codebase.
|
|
||||||
- Document non-obvious logic with comments that describe *why*, not *what*.
|
|
||||||
- Limit changes strictly to the requested scope (plus coordinated fixes per Section 0).
|
|
||||||
- Keep all existing symbol names unless renaming is explicitly requested.
|
|
||||||
- Preserve global formatting as-is
|
|
||||||
- Result every modification in a self-contained, compilable, runnable state of the codebase
|
|
||||||
|
|
||||||
You MUST NOT:
|
|
||||||
|
|
||||||
- Use placeholders: no `// ... rest of code`, no `// implement here`, no `/* TODO */` stubs that replace existing working code. Write full, working implementation. If the implementation is unclear, ask first
|
|
||||||
- Refactor code outside the requested scope
|
|
||||||
- Make speculative improvements
|
|
||||||
- Spawn multiple agents for EDITING
|
|
||||||
- Produce partial changes
|
|
||||||
- Introduce references to entities that are not yet implemented
|
|
||||||
- Leave TODO placeholders in production paths
|
|
||||||
|
|
||||||
Note: `todo!()` and `unimplemented!()` are allowed as idiomatic Rust markers for genuinely unfinished code paths.
|
|
||||||
|
|
||||||
Every change must:
|
|
||||||
- compile,
|
|
||||||
- pass type checks,
|
|
||||||
- have no broken imports,
|
|
||||||
- preserve invariants,
|
|
||||||
- not rely on future patches.
|
|
||||||
|
|
||||||
If the task requires multiple phases:
|
|
||||||
- either implement all required phases,
|
|
||||||
- or explicitly refuse and explain missing dependencies.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
### 8. Decision Process for Complex Changes
|
|
||||||
|
|
||||||
When facing a non-trivial modification, follow this sequence:
|
|
||||||
|
|
||||||
1. **Clarify**: Restate the task in one sentence to confirm understanding.
|
|
||||||
2. **Assess impact**: Identify which modules, types, and invariants are affected.
|
|
||||||
3. **Propose**: Describe the intended change before implementing it.
|
|
||||||
4. **Implement**: Make the minimal, isolated change.
|
|
||||||
5. **Verify**: Explain why the change preserves existing behavior and architectural integrity.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
### 9. Context Awareness
|
|
||||||
|
|
||||||
- When provided with partial code, assume the rest of the codebase exists and functions correctly unless stated otherwise.
|
|
||||||
- Reference existing types, functions, and module structures by their actual names as shown in the provided code.
|
|
||||||
- When the provided context is insufficient to make a safe change, request the missing context explicitly.
|
|
||||||
- Spawn multiple agents for SEARCHING information, code, functions
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
### 10. Response Format
|
|
||||||
|
|
||||||
#### Language Policy
|
|
||||||
|
|
||||||
- Code, comments, commit messages, documentation ONLY ON **English**!
|
|
||||||
- Reasoning and explanations in response text on language from promt
|
|
||||||
|
|
||||||
#### Response Structure
|
|
||||||
|
|
||||||
Your response MUST consist of two sections:
|
|
||||||
|
|
||||||
**Section 1: `## Reasoning`**
|
|
||||||
|
|
||||||
- What needs to be done and why.
|
|
||||||
- Which files and modules are affected.
|
|
||||||
- Architectural decisions and their rationale.
|
|
||||||
- Potential risks or side effects.
|
|
||||||
|
|
||||||
**Section 2: `## Changes`**
|
|
||||||
|
|
||||||
- For each modified or created file: the filename on a separate line in backticks, followed by the code block.
|
|
||||||
- For files **under 200 lines**: return the full file with all changes applied.
|
|
||||||
- For files **over 200 lines**: return only the changed functions/blocks with at least 3 lines of surrounding context above and below. If the user requests the full file, provide it.
|
|
||||||
- New files: full file content.
|
|
||||||
- End with a suggested git commit message in English.
|
|
||||||
|
|
||||||
#### Reporting Out-of-Scope Issues
|
|
||||||
|
|
||||||
If during modification you discover issues outside the requested scope (potential bugs, unsafe code, architectural concerns, missing error handling, unused imports, dead code):
|
|
||||||
|
|
||||||
- Do not fix them silently.
|
|
||||||
- List them under `## ⚠️ Out-of-scope observations` at the end of your response.
|
|
||||||
- Include: file path, line/function context, brief description of the issue, and severity estimate.
|
|
||||||
|
|
||||||
#### Splitting Protocol
|
|
||||||
|
|
||||||
If the response exceeds the output limit:
|
|
||||||
|
|
||||||
1. End the current part with: **SPLIT: PART N — CONTINUE? (remaining: file_list)**
|
|
||||||
2. List the files that will be provided in subsequent parts.
|
|
||||||
3. Wait for user confirmation before continuing.
|
|
||||||
4. No single file may be split across parts.
|
|
||||||
|
|
||||||
## 11. Anti-LLM Degeneration Safeguards (Principal-Paranoid, Visionary)
|
|
||||||
|
|
||||||
This section exists to prevent common LLM failure modes: scope creep, semantic drift, cargo-cult refactors, performance regressions, contract breakage, and hidden behavior changes.
|
|
||||||
|
|
||||||
### 11.1 Non-Negotiable Invariants
|
|
||||||
|
|
||||||
- **No semantic drift:** Do not reinterpret requirements, rename concepts, or change meaning of existing terms.
|
|
||||||
- **No “helpful refactors”:** Any refactor not explicitly requested is forbidden.
|
|
||||||
- **No architectural drift:** Do not introduce new layers, patterns, abstractions, or “clean architecture” migrations unless requested.
|
|
||||||
- **No dependency drift:** Do not add crates, features, or versions unless explicitly requested.
|
|
||||||
- **No behavior drift:** If a change could alter runtime behavior, you MUST call it out explicitly in `## Reasoning` and justify it.
|
|
||||||
|
|
||||||
### 11.2 Minimal Surface Area Rule
|
|
||||||
|
|
||||||
- Touch the smallest number of files possible.
|
|
||||||
- Prefer local changes over cross-cutting edits.
|
|
||||||
- Do not “align style” across a file/module—only adjust the modified region.
|
|
||||||
- Do not reorder items, imports, or code unless required for correctness.
|
|
||||||
|
|
||||||
### 11.3 No Implicit Contract Changes
|
|
||||||
|
|
||||||
Contracts include:
|
|
||||||
- public APIs, trait bounds, visibility, error types, timeouts/retries, logging semantics, metrics semantics,
|
|
||||||
- protocol formats, framing, padding, keepalive cadence, state machine transitions,
|
|
||||||
- concurrency guarantees, cancellation behavior, backpressure behavior.
|
|
||||||
|
|
||||||
Rule:
|
|
||||||
- If you change a contract, you MUST update all dependents in the same patch AND document the contract delta explicitly.
|
|
||||||
|
|
||||||
### 11.4 Hot-Path Preservation (Performance Paranoia)
|
|
||||||
|
|
||||||
- Do not introduce extra allocations, cloning, or formatting in hot paths.
|
|
||||||
- Do not add logging/metrics on hot paths unless requested.
|
|
||||||
- Do not add new locks or broaden lock scope.
|
|
||||||
- Prefer `&str` / slices / borrowed data where the codebase already does so.
|
|
||||||
- Avoid `String` building for errors/logs if it changes current patterns.
|
|
||||||
|
|
||||||
If you cannot prove performance neutrality, label it as risk in `## Reasoning`.
|
|
||||||
|
|
||||||
### 11.5 Async / Concurrency Safety (Cancellation & Backpressure)
|
|
||||||
|
|
||||||
- No blocking calls inside async contexts.
|
|
||||||
- Preserve cancellation safety: do not introduce `await` between lock acquisition and critical invariants unless already present.
|
|
||||||
- Preserve backpressure: do not replace bounded channels with unbounded, do not remove flow control.
|
|
||||||
- Do not change task lifecycle semantics (spawn patterns, join handles, shutdown order) unless requested.
|
|
||||||
- Do not introduce `tokio::spawn` / background tasks unless explicitly requested.
|
|
||||||
|
|
||||||
### 11.6 Error Semantics Integrity
|
|
||||||
|
|
||||||
- Do not replace structured errors with generic strings.
|
|
||||||
- Do not widen/narrow error types or change error categories without explicit approval.
|
|
||||||
- Avoid introducing panics in production paths (`unwrap`, `expect`) unless the codebase already treats that path as impossible and documented.
|
|
||||||
|
|
||||||
### 11.7 “No New Abstractions” Default
|
|
||||||
|
|
||||||
Default stance:
|
|
||||||
- No new traits, generics, macros, builder patterns, type-level cleverness, or “frameworking”.
|
|
||||||
- If abstraction is necessary, prefer the smallest possible local helper (private function) and justify it.
|
|
||||||
|
|
||||||
### 11.8 Negative-Diff Protection
|
|
||||||
|
|
||||||
Avoid “diff inflation” patterns:
|
|
||||||
- mass edits,
|
|
||||||
- moving code between files,
|
|
||||||
- rewrapping long lines,
|
|
||||||
- rearranging module order,
|
|
||||||
- renaming for aesthetics.
|
|
||||||
|
|
||||||
If a diff becomes large, STOP and ask before proceeding.
|
|
||||||
|
|
||||||
### 11.9 Consistency with Existing Style (But Not Style Refactors)
|
|
||||||
|
|
||||||
- Follow existing conventions of the touched module (naming, error style, return patterns).
|
|
||||||
- Do not enforce global “best practices” that the codebase does not already use.
|
|
||||||
|
|
||||||
### 11.10 Two-Phase Safety Gate (Plan → Patch)
|
|
||||||
|
|
||||||
For non-trivial changes:
|
|
||||||
1) Provide a micro-plan (1–5 bullets): what files, what functions, what invariants, what risks.
|
|
||||||
2) Implement exactly that plan—no extra improvements.
|
|
||||||
|
|
||||||
### 11.11 Pre-Response Checklist (Hard Gate)
|
|
||||||
|
|
||||||
Before final output, verify internally:
|
|
||||||
|
|
||||||
- No unresolved symbols / broken imports.
|
|
||||||
- No partially updated call sites.
|
|
||||||
- No new public surface changes unless requested.
|
|
||||||
- No transitional states / TODO placeholders replacing working code.
|
|
||||||
- Changes are atomic: the repository remains buildable and runnable.
|
|
||||||
- Any behavior change is explicitly stated.
|
|
||||||
|
|
||||||
If any check fails: fix it before responding.
|
|
||||||
|
|
||||||
### 11.12 Truthfulness Policy (No Hallucinated Claims)
|
|
||||||
|
|
||||||
- Do not claim “this compiles” or “tests pass” unless you actually verified with the available tooling/context.
|
|
||||||
- If verification is not possible, state: “Not executed; reasoning-based consistency check only.”
|
|
||||||
|
|
||||||
### 11.13 Visionary Guardrail: Preserve Optionality
|
|
||||||
|
|
||||||
When multiple valid designs exist, prefer the one that:
|
|
||||||
- minimally constrains future evolution,
|
|
||||||
- preserves existing extension points,
|
|
||||||
- avoids locking the project into a new paradigm,
|
|
||||||
- keeps interfaces stable and implementation local.
|
|
||||||
|
|
||||||
Default to reversible changes.
|
|
||||||
|
|
||||||
### 11.14 Stop Conditions
|
|
||||||
|
|
||||||
STOP and ask targeted questions if:
|
|
||||||
- required context is missing,
|
|
||||||
- a change would cross module boundaries,
|
|
||||||
- a contract might change,
|
|
||||||
- concurrency/protocol invariants are unclear,
|
|
||||||
- the diff is growing beyond a minimal patch.
|
|
||||||
|
|
||||||
No guessing.
|
|
||||||
|
|
||||||
### 12. Invariant Preservation
|
|
||||||
|
|
||||||
You MUST explicitly preserve:
|
|
||||||
- Thread-safety guarantees (`Send` / `Sync` expectations).
|
|
||||||
- Memory safety assumptions (no hidden `unsafe` expansions).
|
|
||||||
- Lock ordering and deadlock invariants.
|
|
||||||
- State machine correctness (no new invalid transitions).
|
|
||||||
- Backward compatibility of serialized formats (if applicable).
|
|
||||||
|
|
||||||
If a change touches concurrency, networking, protocol logic, or state machines,
|
|
||||||
you MUST explain why existing invariants remain valid.
|
|
||||||
|
|
||||||
### 13. Error Handling Policy
|
|
||||||
|
|
||||||
- Do not replace structured errors with generic strings.
|
|
||||||
- Preserve existing error propagation semantics.
|
|
||||||
- Do not widen or narrow error types without approval.
|
|
||||||
- Avoid introducing panics in production paths.
|
|
||||||
- Prefer explicit error mapping over implicit conversions.
|
|
||||||
|
|
||||||
### 14. Test Safety
|
|
||||||
|
|
||||||
- Do not modify existing tests unless the task explicitly requires it.
|
|
||||||
- Do not weaken assertions.
|
|
||||||
- Preserve determinism in testable components.
|
|
||||||
|
|
||||||
### 15. Security Constraints
|
|
||||||
|
|
||||||
- Do not weaken cryptographic assumptions.
|
|
||||||
- Do not modify key derivation logic without explicit request.
|
|
||||||
- Do not change constant-time behavior.
|
|
||||||
- Do not introduce logging of secrets.
|
|
||||||
- Preserve TLS/MTProto protocol correctness.
|
|
||||||
|
|
||||||
### 16. Logging Policy
|
|
||||||
|
|
||||||
- Do not introduce excessive logging in hot paths.
|
|
||||||
- Do not log sensitive data.
|
|
||||||
- Preserve existing log levels and style.
|
|
||||||
|
|
||||||
### 17. Pre-Response Verification Checklist
|
|
||||||
|
|
||||||
Before producing the final answer, verify internally:
|
|
||||||
|
|
||||||
- The change compiles conceptually.
|
|
||||||
- No unresolved symbols exist.
|
|
||||||
- All modified call sites are updated.
|
|
||||||
- No accidental behavioral changes were introduced.
|
|
||||||
- Architectural boundaries remain intact.
|
|
||||||
|
|
||||||
### 18. Atomic Change Principle
|
|
||||||
Every patch must be **atomic and production-safe**.
|
|
||||||
* **Self-contained** — no dependency on future patches or unimplemented components.
|
|
||||||
* **Build-safe** — the project must compile successfully after the change.
|
|
||||||
* **Contract-consistent** — no partial interface or behavioral changes; all dependent code must be updated within the same patch.
|
|
||||||
* **No transitional states** — no placeholders, incomplete refactors, or temporary inconsistencies.
|
|
||||||
|
|
||||||
**Invariant:** After any single patch, the repository remains fully functional and buildable.
|
|
||||||
|
|
||||||
544
Cargo.lock
generated
544
Cargo.lock
generated
@@ -55,6 +55,45 @@ version = "1.0.101"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5f0e0fee31ef5ed1ba1316088939cea399010ed7731dba877ed44aeb407a75ea"
|
checksum = "5f0e0fee31ef5ed1ba1316088939cea399010ed7731dba877ed44aeb407a75ea"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "asn1-rs"
|
||||||
|
version = "0.5.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7f6fd5ddaf0351dff5b8da21b2fb4ff8e08ddd02857f0bf69c47639106c0fff0"
|
||||||
|
dependencies = [
|
||||||
|
"asn1-rs-derive",
|
||||||
|
"asn1-rs-impl",
|
||||||
|
"displaydoc",
|
||||||
|
"nom",
|
||||||
|
"num-traits",
|
||||||
|
"rusticata-macros",
|
||||||
|
"thiserror 1.0.69",
|
||||||
|
"time",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "asn1-rs-derive"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "726535892e8eae7e70657b4c8ea93d26b8553afb1ce617caee529ef96d7dee6c"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn 1.0.109",
|
||||||
|
"synstructure 0.12.6",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "asn1-rs-impl"
|
||||||
|
version = "0.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2777730b2039ac0f95f093556e61b6d26cebed5393ca6f152717777cec3a42ed"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn 1.0.109",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "atomic-waker"
|
name = "atomic-waker"
|
||||||
version = "1.1.2"
|
version = "1.1.2"
|
||||||
@@ -88,6 +127,12 @@ version = "0.8.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5e764a1d40d510daf35e07be9eb06e75770908c27d411ee6c92109c9840eaaf7"
|
checksum = "5e764a1d40d510daf35e07be9eb06e75770908c27d411ee6c92109c9840eaaf7"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bitflags"
|
||||||
|
version = "1.3.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bitflags"
|
name = "bitflags"
|
||||||
version = "2.10.0"
|
version = "2.10.0"
|
||||||
@@ -155,6 +200,12 @@ version = "1.0.4"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
|
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cfg_aliases"
|
||||||
|
version = "0.1.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cfg_aliases"
|
name = "cfg_aliases"
|
||||||
version = "0.2.1"
|
version = "0.2.1"
|
||||||
@@ -252,6 +303,15 @@ dependencies = [
|
|||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crc32c"
|
||||||
|
version = "0.6.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3a47af21622d091a8f0fb295b88bc886ac74efcc613efc19f5d0b21de5c89e47"
|
||||||
|
dependencies = [
|
||||||
|
"rustc_version",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "crc32fast"
|
name = "crc32fast"
|
||||||
version = "1.5.0"
|
version = "1.5.0"
|
||||||
@@ -297,6 +357,15 @@ dependencies = [
|
|||||||
"itertools",
|
"itertools",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "crossbeam-channel"
|
||||||
|
version = "0.5.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "82b8f8f868b36967f9606790d1903570de9ceaf870a7bf9fbbd3016d636a2cb2"
|
||||||
|
dependencies = [
|
||||||
|
"crossbeam-utils",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "crossbeam-deque"
|
name = "crossbeam-deque"
|
||||||
version = "0.8.6"
|
version = "0.8.6"
|
||||||
@@ -369,6 +438,35 @@ dependencies = [
|
|||||||
"parking_lot_core",
|
"parking_lot_core",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "data-encoding"
|
||||||
|
version = "2.10.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d7a1e2f27636f116493b8b860f5546edb47c8d8f8ea73e1d2a20be88e28d1fea"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "der-parser"
|
||||||
|
version = "8.2.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "dbd676fbbab537128ef0278adb5576cf363cff6aa22a7b24effe97347cfab61e"
|
||||||
|
dependencies = [
|
||||||
|
"asn1-rs",
|
||||||
|
"displaydoc",
|
||||||
|
"nom",
|
||||||
|
"num-bigint",
|
||||||
|
"num-traits",
|
||||||
|
"rusticata-macros",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "deranged"
|
||||||
|
version = "0.5.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7cd812cc2bc1d69d4764bd80df88b4317eaef9e773c75226407d9bc0876b211c"
|
||||||
|
dependencies = [
|
||||||
|
"powerfmt",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "digest"
|
name = "digest"
|
||||||
version = "0.10.7"
|
version = "0.10.7"
|
||||||
@@ -388,7 +486,7 @@ checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.114",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -419,6 +517,17 @@ version = "2.3.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be"
|
checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "filetime"
|
||||||
|
version = "0.2.27"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f98844151eee8917efc50bd9e8318cb963ae8b297431495d3f758616ea5c57db"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"libc",
|
||||||
|
"libredox",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "find-msvc-tools"
|
name = "find-msvc-tools"
|
||||||
version = "0.1.9"
|
version = "0.1.9"
|
||||||
@@ -452,6 +561,15 @@ dependencies = [
|
|||||||
"percent-encoding",
|
"percent-encoding",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "fsevent-sys"
|
||||||
|
version = "4.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "76ee7a02da4d231650c7cea31349b889be2f45ddb3ef3032d2ec8185f6313fd2"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "futures"
|
name = "futures"
|
||||||
version = "0.3.31"
|
version = "0.3.31"
|
||||||
@@ -508,7 +626,7 @@ checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.114",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -756,7 +874,7 @@ dependencies = [
|
|||||||
"tokio",
|
"tokio",
|
||||||
"tokio-rustls",
|
"tokio-rustls",
|
||||||
"tower-service",
|
"tower-service",
|
||||||
"webpki-roots",
|
"webpki-roots 1.0.6",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -926,6 +1044,26 @@ dependencies = [
|
|||||||
"serde_core",
|
"serde_core",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "inotify"
|
||||||
|
version = "0.9.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f8069d3ec154eb856955c1c0fbffefbf5f3c40a104ec912d4797314c1801abff"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags 1.3.2",
|
||||||
|
"inotify-sys",
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "inotify-sys"
|
||||||
|
version = "0.1.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e05c02b5e89bff3b946cedeca278abc628fe811e604f027c45a8aa3cf793d0eb"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "inout"
|
name = "inout"
|
||||||
version = "0.1.4"
|
version = "0.1.4"
|
||||||
@@ -942,6 +1080,15 @@ version = "2.11.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "469fb0b9cefa57e3ef31275ee7cacb78f2fdca44e4765491884a2b119d4eb130"
|
checksum = "469fb0b9cefa57e3ef31275ee7cacb78f2fdca44e4765491884a2b119d4eb130"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ipnetwork"
|
||||||
|
version = "0.20.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "bf466541e9d546596ee94f9f69590f89473455f88372423e0008fc1a7daf100e"
|
||||||
|
dependencies = [
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "iri-string"
|
name = "iri-string"
|
||||||
version = "0.7.10"
|
version = "0.7.10"
|
||||||
@@ -988,6 +1135,26 @@ dependencies = [
|
|||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "kqueue"
|
||||||
|
version = "1.1.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "eac30106d7dce88daf4a3fcb4879ea939476d5074a9b7ddd0fb97fa4bed5596a"
|
||||||
|
dependencies = [
|
||||||
|
"kqueue-sys",
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "kqueue-sys"
|
||||||
|
version = "1.0.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ed9625ffda8729b85e45cf04090035ac368927b8cebc34898e7c120f52e4838b"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags 1.3.2",
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lazy_static"
|
name = "lazy_static"
|
||||||
version = "1.5.0"
|
version = "1.5.0"
|
||||||
@@ -1006,6 +1173,17 @@ version = "0.2.181"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "459427e2af2b9c839b132acb702a1c654d95e10f8c326bfc2ad11310e458b1c5"
|
checksum = "459427e2af2b9c839b132acb702a1c654d95e10f8c326bfc2ad11310e458b1c5"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libredox"
|
||||||
|
version = "0.1.12"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3d0b95e02c851351f877147b7deea7b1afb1df71b63aa5f8270716e0c5720616"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags 2.10.0",
|
||||||
|
"libc",
|
||||||
|
"redox_syscall 0.7.1",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "linux-raw-sys"
|
name = "linux-raw-sys"
|
||||||
version = "0.11.0"
|
version = "0.11.0"
|
||||||
@@ -1073,6 +1251,33 @@ version = "2.8.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79"
|
checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "memoffset"
|
||||||
|
version = "0.9.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a"
|
||||||
|
dependencies = [
|
||||||
|
"autocfg",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "minimal-lexical"
|
||||||
|
version = "0.2.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "mio"
|
||||||
|
version = "0.8.11"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"log",
|
||||||
|
"wasi",
|
||||||
|
"windows-sys 0.48.0",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mio"
|
name = "mio"
|
||||||
version = "1.1.1"
|
version = "1.1.1"
|
||||||
@@ -1084,6 +1289,48 @@ dependencies = [
|
|||||||
"windows-sys 0.61.2",
|
"windows-sys 0.61.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "nix"
|
||||||
|
version = "0.28.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ab2156c4fce2f8df6c499cc1c763e4394b7482525bf2a9701c9d79d215f519e4"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags 2.10.0",
|
||||||
|
"cfg-if",
|
||||||
|
"cfg_aliases 0.1.1",
|
||||||
|
"libc",
|
||||||
|
"memoffset",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "nom"
|
||||||
|
version = "7.1.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
|
||||||
|
dependencies = [
|
||||||
|
"memchr",
|
||||||
|
"minimal-lexical",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "notify"
|
||||||
|
version = "6.1.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6205bd8bb1e454ad2e27422015fb5e4f2bcc7e08fa8f27058670d208324a4d2d"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags 2.10.0",
|
||||||
|
"crossbeam-channel",
|
||||||
|
"filetime",
|
||||||
|
"fsevent-sys",
|
||||||
|
"inotify",
|
||||||
|
"kqueue",
|
||||||
|
"libc",
|
||||||
|
"log",
|
||||||
|
"mio 0.8.11",
|
||||||
|
"walkdir",
|
||||||
|
"windows-sys 0.48.0",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "nu-ansi-term"
|
name = "nu-ansi-term"
|
||||||
version = "0.50.3"
|
version = "0.50.3"
|
||||||
@@ -1103,6 +1350,12 @@ dependencies = [
|
|||||||
"num-traits",
|
"num-traits",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num-conv"
|
||||||
|
version = "0.2.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cf97ec579c3c42f953ef76dbf8d55ac91fb219dde70e49aa4a6b7d74e9919050"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "num-integer"
|
name = "num-integer"
|
||||||
version = "0.1.46"
|
version = "0.1.46"
|
||||||
@@ -1121,6 +1374,15 @@ dependencies = [
|
|||||||
"autocfg",
|
"autocfg",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "oid-registry"
|
||||||
|
version = "0.6.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9bedf36ffb6ba96c2eb7144ef6270557b52e54b20c0a8e1eb2ff99a6c6959bff"
|
||||||
|
dependencies = [
|
||||||
|
"asn1-rs",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "once_cell"
|
name = "once_cell"
|
||||||
version = "1.21.3"
|
version = "1.21.3"
|
||||||
@@ -1151,7 +1413,7 @@ checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"libc",
|
"libc",
|
||||||
"redox_syscall",
|
"redox_syscall 0.5.18",
|
||||||
"smallvec",
|
"smallvec",
|
||||||
"windows-link",
|
"windows-link",
|
||||||
]
|
]
|
||||||
@@ -1211,6 +1473,12 @@ dependencies = [
|
|||||||
"zerovec",
|
"zerovec",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "powerfmt"
|
||||||
|
version = "0.2.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ppv-lite86"
|
name = "ppv-lite86"
|
||||||
version = "0.2.21"
|
version = "0.2.21"
|
||||||
@@ -1227,7 +1495,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b"
|
checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"syn",
|
"syn 2.0.114",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1247,7 +1515,7 @@ checksum = "37566cb3fdacef14c0737f9546df7cfeadbfbc9fef10991038bf5015d0c80532"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"bit-set",
|
"bit-set",
|
||||||
"bit-vec",
|
"bit-vec",
|
||||||
"bitflags",
|
"bitflags 2.10.0",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
"rand",
|
"rand",
|
||||||
"rand_chacha",
|
"rand_chacha",
|
||||||
@@ -1271,14 +1539,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "b9e20a958963c291dc322d98411f541009df2ced7b5a4f2bd52337638cfccf20"
|
checksum = "b9e20a958963c291dc322d98411f541009df2ced7b5a4f2bd52337638cfccf20"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bytes",
|
"bytes",
|
||||||
"cfg_aliases",
|
"cfg_aliases 0.2.1",
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"quinn-proto",
|
"quinn-proto",
|
||||||
"quinn-udp",
|
"quinn-udp",
|
||||||
"rustc-hash",
|
"rustc-hash",
|
||||||
"rustls",
|
"rustls",
|
||||||
"socket2 0.6.2",
|
"socket2 0.6.2",
|
||||||
"thiserror",
|
"thiserror 2.0.18",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tracing",
|
"tracing",
|
||||||
"web-time",
|
"web-time",
|
||||||
@@ -1299,7 +1567,7 @@ dependencies = [
|
|||||||
"rustls",
|
"rustls",
|
||||||
"rustls-pki-types",
|
"rustls-pki-types",
|
||||||
"slab",
|
"slab",
|
||||||
"thiserror",
|
"thiserror 2.0.18",
|
||||||
"tinyvec",
|
"tinyvec",
|
||||||
"tracing",
|
"tracing",
|
||||||
"web-time",
|
"web-time",
|
||||||
@@ -1311,7 +1579,7 @@ version = "0.5.14"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "addec6a0dcad8a8d96a771f815f0eaf55f9d1805756410b39f5fa81332574cbd"
|
checksum = "addec6a0dcad8a8d96a771f815f0eaf55f9d1805756410b39f5fa81332574cbd"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg_aliases",
|
"cfg_aliases 0.2.1",
|
||||||
"libc",
|
"libc",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"socket2 0.6.2",
|
"socket2 0.6.2",
|
||||||
@@ -1398,7 +1666,16 @@ version = "0.5.18"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d"
|
checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags 2.10.0",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "redox_syscall"
|
||||||
|
version = "0.7.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "35985aa610addc02e24fc232012c86fd11f14111180f902b67e2d5331f8ebf2b"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags 2.10.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1465,7 +1742,7 @@ dependencies = [
|
|||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
"wasm-bindgen-futures",
|
"wasm-bindgen-futures",
|
||||||
"web-sys",
|
"web-sys",
|
||||||
"webpki-roots",
|
"webpki-roots 1.0.6",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1488,13 +1765,31 @@ version = "2.1.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d"
|
checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rustc_version"
|
||||||
|
version = "0.4.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92"
|
||||||
|
dependencies = [
|
||||||
|
"semver",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rusticata-macros"
|
||||||
|
version = "4.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "faf0c4a6ece9950b9abdb62b1cfcf2a68b3b67a10ba445b3bb85be2a293d0632"
|
||||||
|
dependencies = [
|
||||||
|
"nom",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rustix"
|
name = "rustix"
|
||||||
version = "1.1.3"
|
version = "1.1.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "146c9e247ccc180c1f61615433868c99f3de3ae256a30a43b49f67c2d9171f34"
|
checksum = "146c9e247ccc180c1f61615433868c99f3de3ae256a30a43b49f67c2d9171f34"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags 2.10.0",
|
||||||
"errno",
|
"errno",
|
||||||
"libc",
|
"libc",
|
||||||
"linux-raw-sys",
|
"linux-raw-sys",
|
||||||
@@ -1608,7 +1903,7 @@ checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.114",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1736,6 +2031,17 @@ version = "2.6.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
|
checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "syn"
|
||||||
|
version = "1.0.109"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "2.0.114"
|
version = "2.0.114"
|
||||||
@@ -1756,6 +2062,18 @@ dependencies = [
|
|||||||
"futures-core",
|
"futures-core",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "synstructure"
|
||||||
|
version = "0.12.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f36bdaa60a83aca3921b5259d5400cbf5e90fc51931376a9bd4a0eb79aa7210f"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn 1.0.109",
|
||||||
|
"unicode-xid",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "synstructure"
|
name = "synstructure"
|
||||||
version = "0.13.2"
|
version = "0.13.2"
|
||||||
@@ -1764,18 +2082,20 @@ checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.114",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "telemt"
|
name = "telemt"
|
||||||
version = "3.0.0"
|
version = "3.0.13"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"aes",
|
"aes",
|
||||||
|
"anyhow",
|
||||||
"base64",
|
"base64",
|
||||||
"bytes",
|
"bytes",
|
||||||
"cbc",
|
"cbc",
|
||||||
"chrono",
|
"chrono",
|
||||||
|
"crc32c",
|
||||||
"crc32fast",
|
"crc32fast",
|
||||||
"criterion",
|
"criterion",
|
||||||
"crossbeam-queue",
|
"crossbeam-queue",
|
||||||
@@ -1788,9 +2108,12 @@ dependencies = [
|
|||||||
"httpdate",
|
"httpdate",
|
||||||
"hyper",
|
"hyper",
|
||||||
"hyper-util",
|
"hyper-util",
|
||||||
|
"ipnetwork",
|
||||||
"libc",
|
"libc",
|
||||||
"lru",
|
"lru",
|
||||||
"md-5",
|
"md-5",
|
||||||
|
"nix",
|
||||||
|
"notify",
|
||||||
"num-bigint",
|
"num-bigint",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
"parking_lot",
|
"parking_lot",
|
||||||
@@ -1798,19 +2121,23 @@ dependencies = [
|
|||||||
"rand",
|
"rand",
|
||||||
"regex",
|
"regex",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
|
"rustls",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"sha1",
|
"sha1",
|
||||||
"sha2",
|
"sha2",
|
||||||
"socket2 0.5.10",
|
"socket2 0.5.10",
|
||||||
"thiserror",
|
"thiserror 2.0.18",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
"tokio-rustls",
|
||||||
"tokio-test",
|
"tokio-test",
|
||||||
"tokio-util",
|
"tokio-util",
|
||||||
"toml",
|
"toml",
|
||||||
"tracing",
|
"tracing",
|
||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
"url",
|
"url",
|
||||||
|
"webpki-roots 0.26.11",
|
||||||
|
"x509-parser",
|
||||||
"zeroize",
|
"zeroize",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -1827,13 +2154,33 @@ dependencies = [
|
|||||||
"windows-sys 0.61.2",
|
"windows-sys 0.61.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "thiserror"
|
||||||
|
version = "1.0.69"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52"
|
||||||
|
dependencies = [
|
||||||
|
"thiserror-impl 1.0.69",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "thiserror"
|
name = "thiserror"
|
||||||
version = "2.0.18"
|
version = "2.0.18"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4288b5bcbc7920c07a1149a35cf9590a2aa808e0bc1eafaade0b80947865fbc4"
|
checksum = "4288b5bcbc7920c07a1149a35cf9590a2aa808e0bc1eafaade0b80947865fbc4"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"thiserror-impl",
|
"thiserror-impl 2.0.18",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "thiserror-impl"
|
||||||
|
version = "1.0.69"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn 2.0.114",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1844,7 +2191,7 @@ checksum = "ebc4ee7f67670e9b64d05fa4253e753e016c6c95ff35b89b7941d6b856dec1d5"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.114",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1856,6 +2203,37 @@ dependencies = [
|
|||||||
"cfg-if",
|
"cfg-if",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "time"
|
||||||
|
version = "0.3.47"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "743bd48c283afc0388f9b8827b976905fb217ad9e647fae3a379a9283c4def2c"
|
||||||
|
dependencies = [
|
||||||
|
"deranged",
|
||||||
|
"itoa",
|
||||||
|
"num-conv",
|
||||||
|
"powerfmt",
|
||||||
|
"serde_core",
|
||||||
|
"time-core",
|
||||||
|
"time-macros",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "time-core"
|
||||||
|
version = "0.1.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7694e1cfe791f8d31026952abf09c69ca6f6fa4e1a1229e18988f06a04a12dca"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "time-macros"
|
||||||
|
version = "0.2.27"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2e70e4c5a0e0a8a4823ad65dfe1a6930e4f4d756dcd9dd7939022b5e8c501215"
|
||||||
|
dependencies = [
|
||||||
|
"num-conv",
|
||||||
|
"time-core",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tinystr"
|
name = "tinystr"
|
||||||
version = "0.8.2"
|
version = "0.8.2"
|
||||||
@@ -1899,7 +2277,7 @@ checksum = "72a2903cd7736441aac9df9d7688bd0ce48edccaadf181c3b90be801e81d3d86"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"bytes",
|
"bytes",
|
||||||
"libc",
|
"libc",
|
||||||
"mio",
|
"mio 1.1.1",
|
||||||
"parking_lot",
|
"parking_lot",
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"signal-hook-registry",
|
"signal-hook-registry",
|
||||||
@@ -1917,7 +2295,7 @@ checksum = "af407857209536a95c8e56f8231ef2c2e2aff839b22e07a1ffcbc617e9db9fa5"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.114",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2031,7 +2409,7 @@ version = "0.6.8"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d4e6559d53cc268e5031cd8429d05415bc4cb4aefc4aa5d6cc35fbf5b924a1f8"
|
checksum = "d4e6559d53cc268e5031cd8429d05415bc4cb4aefc4aa5d6cc35fbf5b924a1f8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags 2.10.0",
|
||||||
"bytes",
|
"bytes",
|
||||||
"futures-util",
|
"futures-util",
|
||||||
"http",
|
"http",
|
||||||
@@ -2074,7 +2452,7 @@ checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.114",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2280,7 +2658,7 @@ dependencies = [
|
|||||||
"bumpalo",
|
"bumpalo",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.114",
|
||||||
"wasm-bindgen-shared",
|
"wasm-bindgen-shared",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -2321,7 +2699,7 @@ version = "0.244.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "47b807c72e1bac69382b3a6fb3dbe8ea4c0ed87ff5629b8685ae6b9a611028fe"
|
checksum = "47b807c72e1bac69382b3a6fb3dbe8ea4c0ed87ff5629b8685ae6b9a611028fe"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags 2.10.0",
|
||||||
"hashbrown 0.15.5",
|
"hashbrown 0.15.5",
|
||||||
"indexmap",
|
"indexmap",
|
||||||
"semver",
|
"semver",
|
||||||
@@ -2347,6 +2725,15 @@ dependencies = [
|
|||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "webpki-roots"
|
||||||
|
version = "0.26.11"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "521bc38abb08001b01866da9f51eb7c5d647a19260e00054a8c7fd5f9e57f7a9"
|
||||||
|
dependencies = [
|
||||||
|
"webpki-roots 1.0.6",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "webpki-roots"
|
name = "webpki-roots"
|
||||||
version = "1.0.6"
|
version = "1.0.6"
|
||||||
@@ -2386,7 +2773,7 @@ checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.114",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2397,7 +2784,7 @@ checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.114",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2424,6 +2811,15 @@ dependencies = [
|
|||||||
"windows-link",
|
"windows-link",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows-sys"
|
||||||
|
version = "0.48.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
|
||||||
|
dependencies = [
|
||||||
|
"windows-targets 0.48.5",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows-sys"
|
name = "windows-sys"
|
||||||
version = "0.52.0"
|
version = "0.52.0"
|
||||||
@@ -2451,6 +2847,21 @@ dependencies = [
|
|||||||
"windows-link",
|
"windows-link",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows-targets"
|
||||||
|
version = "0.48.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
|
||||||
|
dependencies = [
|
||||||
|
"windows_aarch64_gnullvm 0.48.5",
|
||||||
|
"windows_aarch64_msvc 0.48.5",
|
||||||
|
"windows_i686_gnu 0.48.5",
|
||||||
|
"windows_i686_msvc 0.48.5",
|
||||||
|
"windows_x86_64_gnu 0.48.5",
|
||||||
|
"windows_x86_64_gnullvm 0.48.5",
|
||||||
|
"windows_x86_64_msvc 0.48.5",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows-targets"
|
name = "windows-targets"
|
||||||
version = "0.52.6"
|
version = "0.52.6"
|
||||||
@@ -2484,6 +2895,12 @@ dependencies = [
|
|||||||
"windows_x86_64_msvc 0.53.1",
|
"windows_x86_64_msvc 0.53.1",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_aarch64_gnullvm"
|
||||||
|
version = "0.48.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_aarch64_gnullvm"
|
name = "windows_aarch64_gnullvm"
|
||||||
version = "0.52.6"
|
version = "0.52.6"
|
||||||
@@ -2496,6 +2913,12 @@ version = "0.53.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a9d8416fa8b42f5c947f8482c43e7d89e73a173cead56d044f6a56104a6d1b53"
|
checksum = "a9d8416fa8b42f5c947f8482c43e7d89e73a173cead56d044f6a56104a6d1b53"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_aarch64_msvc"
|
||||||
|
version = "0.48.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_aarch64_msvc"
|
name = "windows_aarch64_msvc"
|
||||||
version = "0.52.6"
|
version = "0.52.6"
|
||||||
@@ -2508,6 +2931,12 @@ version = "0.53.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b9d782e804c2f632e395708e99a94275910eb9100b2114651e04744e9b125006"
|
checksum = "b9d782e804c2f632e395708e99a94275910eb9100b2114651e04744e9b125006"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_i686_gnu"
|
||||||
|
version = "0.48.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_i686_gnu"
|
name = "windows_i686_gnu"
|
||||||
version = "0.52.6"
|
version = "0.52.6"
|
||||||
@@ -2532,6 +2961,12 @@ version = "0.53.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "fa7359d10048f68ab8b09fa71c3daccfb0e9b559aed648a8f95469c27057180c"
|
checksum = "fa7359d10048f68ab8b09fa71c3daccfb0e9b559aed648a8f95469c27057180c"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_i686_msvc"
|
||||||
|
version = "0.48.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_i686_msvc"
|
name = "windows_i686_msvc"
|
||||||
version = "0.52.6"
|
version = "0.52.6"
|
||||||
@@ -2544,6 +2979,12 @@ version = "0.53.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1e7ac75179f18232fe9c285163565a57ef8d3c89254a30685b57d83a38d326c2"
|
checksum = "1e7ac75179f18232fe9c285163565a57ef8d3c89254a30685b57d83a38d326c2"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_x86_64_gnu"
|
||||||
|
version = "0.48.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_x86_64_gnu"
|
name = "windows_x86_64_gnu"
|
||||||
version = "0.52.6"
|
version = "0.52.6"
|
||||||
@@ -2556,6 +2997,12 @@ version = "0.53.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9c3842cdd74a865a8066ab39c8a7a473c0778a3f29370b5fd6b4b9aa7df4a499"
|
checksum = "9c3842cdd74a865a8066ab39c8a7a473c0778a3f29370b5fd6b4b9aa7df4a499"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_x86_64_gnullvm"
|
||||||
|
version = "0.48.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_x86_64_gnullvm"
|
name = "windows_x86_64_gnullvm"
|
||||||
version = "0.52.6"
|
version = "0.52.6"
|
||||||
@@ -2568,6 +3015,12 @@ version = "0.53.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0ffa179e2d07eee8ad8f57493436566c7cc30ac536a3379fdf008f47f6bb7ae1"
|
checksum = "0ffa179e2d07eee8ad8f57493436566c7cc30ac536a3379fdf008f47f6bb7ae1"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_x86_64_msvc"
|
||||||
|
version = "0.48.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_x86_64_msvc"
|
name = "windows_x86_64_msvc"
|
||||||
version = "0.52.6"
|
version = "0.52.6"
|
||||||
@@ -2619,7 +3072,7 @@ dependencies = [
|
|||||||
"heck",
|
"heck",
|
||||||
"indexmap",
|
"indexmap",
|
||||||
"prettyplease",
|
"prettyplease",
|
||||||
"syn",
|
"syn 2.0.114",
|
||||||
"wasm-metadata",
|
"wasm-metadata",
|
||||||
"wit-bindgen-core",
|
"wit-bindgen-core",
|
||||||
"wit-component",
|
"wit-component",
|
||||||
@@ -2635,7 +3088,7 @@ dependencies = [
|
|||||||
"prettyplease",
|
"prettyplease",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.114",
|
||||||
"wit-bindgen-core",
|
"wit-bindgen-core",
|
||||||
"wit-bindgen-rust",
|
"wit-bindgen-rust",
|
||||||
]
|
]
|
||||||
@@ -2647,7 +3100,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "9d66ea20e9553b30172b5e831994e35fbde2d165325bec84fc43dbf6f4eb9cb2"
|
checksum = "9d66ea20e9553b30172b5e831994e35fbde2d165325bec84fc43dbf6f4eb9cb2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"bitflags",
|
"bitflags 2.10.0",
|
||||||
"indexmap",
|
"indexmap",
|
||||||
"log",
|
"log",
|
||||||
"serde",
|
"serde",
|
||||||
@@ -2683,6 +3136,23 @@ version = "0.6.2"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9edde0db4769d2dc68579893f2306b26c6ecfbe0ef499b013d731b7b9247e0b9"
|
checksum = "9edde0db4769d2dc68579893f2306b26c6ecfbe0ef499b013d731b7b9247e0b9"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "x509-parser"
|
||||||
|
version = "0.15.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7069fba5b66b9193bd2c5d3d4ff12b839118f6bcbef5328efafafb5395cf63da"
|
||||||
|
dependencies = [
|
||||||
|
"asn1-rs",
|
||||||
|
"data-encoding",
|
||||||
|
"der-parser",
|
||||||
|
"lazy_static",
|
||||||
|
"nom",
|
||||||
|
"oid-registry",
|
||||||
|
"rusticata-macros",
|
||||||
|
"thiserror 1.0.69",
|
||||||
|
"time",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "yoke"
|
name = "yoke"
|
||||||
version = "0.8.1"
|
version = "0.8.1"
|
||||||
@@ -2702,8 +3172,8 @@ checksum = "b659052874eb698efe5b9e8cf382204678a0086ebf46982b79d6ca3182927e5d"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.114",
|
||||||
"synstructure",
|
"synstructure 0.13.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2723,7 +3193,7 @@ checksum = "4122cd3169e94605190e77839c9a40d40ed048d305bfdc146e7df40ab0f3e517"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.114",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2743,8 +3213,8 @@ checksum = "d71e5d6e06ab090c67b5e44993ec16b72dcbaabc526db883a360057678b48502"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.114",
|
||||||
"synstructure",
|
"synstructure 0.13.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2764,7 +3234,7 @@ checksum = "85a5b4158499876c763cb03bc4e49185d3cccbabb15b33c627f7884f43db852e"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.114",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2797,7 +3267,7 @@ checksum = "eadce39539ca5cb3985590102671f2567e659fca9666581ad3411d59207951f3"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.114",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "telemt"
|
name = "telemt"
|
||||||
version = "3.0.11"
|
version = "3.1.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
# ==========================
|
# ==========================
|
||||||
# Stage 1: Build
|
# Stage 1: Build
|
||||||
# ==========================
|
# ==========================
|
||||||
FROM rust:1.85-slim-bookworm AS builder
|
FROM rust:1.88-slim-bookworm AS builder
|
||||||
|
|
||||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||||
pkg-config \
|
pkg-config \
|
||||||
@@ -40,4 +40,4 @@ EXPOSE 443
|
|||||||
EXPOSE 9090
|
EXPOSE 9090
|
||||||
|
|
||||||
ENTRYPOINT ["/app/telemt"]
|
ENTRYPOINT ["/app/telemt"]
|
||||||
CMD ["config.toml"]
|
CMD ["config.toml"]
|
||||||
|
|||||||
44
README.md
44
README.md
@@ -10,28 +10,18 @@
|
|||||||
|
|
||||||
### 🇷🇺 RU
|
### 🇷🇺 RU
|
||||||
|
|
||||||
#### Драфтинг LTS и текущие улучшения
|
#### Релиз 3.0.15 — 25 февраля
|
||||||
|
|
||||||
С 21 февраля мы начали подготовку LTS-версии.
|
25 февраля мы выпустили версию **3.0.15**
|
||||||
|
|
||||||
Мы внимательно анализируем весь доступный фидбек.
|
Мы предполагаем, что она станет завершающей версией поколения 3.0 и уже сейчас мы рассматриваем её как **LTS-кандидата** для версии **3.1.0**!
|
||||||
Наша цель — сделать LTS-кандидаты максимально стабильными, тщательно отлаженными и готовыми к long-run и highload production-сценариям.
|
|
||||||
|
|
||||||
---
|
После нескольких дней детального анализа особенностей работы Middle-End мы спроектировали и реализовали продуманный режим **ротации ME Writer**. Данный режим позволяет поддерживать стабильно высокую производительность в long-run сценариях без возникновения ошибок, связанных с некорректной конфигурацией прокси
|
||||||
|
|
||||||
#### Улучшения от 23 февраля
|
Будем рады вашему фидбеку и предложениям по улучшению — особенно в части **статистики** и **UX**
|
||||||
|
|
||||||
23 февраля были внесены улучшения производительности в режимах **DC** и **Middle-End (ME)**, с акцентом на обратный канал (путь клиент → DC / ME).
|
|
||||||
|
|
||||||
Дополнительно реализован ряд изменений, направленных на повышение устойчивости системы:
|
|
||||||
|
|
||||||
- Смягчение сетевой нестабильности
|
|
||||||
- Повышение устойчивости к десинхронизации криптографии
|
|
||||||
- Снижение дрейфа сессий при неблагоприятных условиях
|
|
||||||
- Улучшение обработки ошибок в edge-case транспортных сценариях
|
|
||||||
|
|
||||||
Релиз:
|
Релиз:
|
||||||
[3.0.9](https://github.com/telemt/telemt/releases/tag/3.0.9)
|
[3.0.15](https://github.com/telemt/telemt/releases/tag/3.0.15)
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -48,28 +38,18 @@
|
|||||||
|
|
||||||
### 🇬🇧 EN
|
### 🇬🇧 EN
|
||||||
|
|
||||||
#### LTS Drafting and Ongoing Improvements
|
#### Release 3.0.15 — February 25
|
||||||
|
|
||||||
Starting February 21, we began drafting the upcoming LTS version.
|
On February 25, we released version **3.0.15**
|
||||||
|
|
||||||
We are carefully reviewing and analyzing all available feedback.
|
We expect this to become the final release of the 3.0 generation and at this point, we already see it as a strong **LTS candidate** for the upcoming **3.1.0** release!
|
||||||
The goal is to ensure that LTS candidates are максимально stable, thoroughly debugged, and ready for long-run and high-load production scenarios.
|
|
||||||
|
|
||||||
---
|
After several days of deep analysis of Middle-End behavior, we designed and implemented a well-engineered **ME Writer rotation mode**. This mode enables sustained high throughput in long-run scenarios while preventing proxy misconfiguration errors
|
||||||
|
|
||||||
#### February 23 Improvements
|
We are looking forward to your feedback and improvement proposals — especially regarding **statistics** and **UX**
|
||||||
|
|
||||||
On February 23, we introduced performance improvements for both **DC** and **Middle-End (ME)** modes, specifically optimizing the reverse channel (client → DC / ME data path).
|
|
||||||
|
|
||||||
Additionally, we implemented a set of robustness enhancements designed to:
|
|
||||||
|
|
||||||
- Mitigate network-related instability
|
|
||||||
- Improve resilience against cryptographic desynchronization
|
|
||||||
- Reduce session drift under adverse conditions
|
|
||||||
- Improve error handling in edge-case transport scenarios
|
|
||||||
|
|
||||||
Release:
|
Release:
|
||||||
[3.0.9](https://github.com/telemt/telemt/releases/tag/3.0.9)
|
[3.0.15](https://github.com/telemt/telemt/releases/tag/3.0.15)
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
204
config.full.toml
Normal file
204
config.full.toml
Normal file
@@ -0,0 +1,204 @@
|
|||||||
|
# Telemt full config with default values.
|
||||||
|
# Examples are kept in comments after '#'.
|
||||||
|
|
||||||
|
# Top-level legacy field.
|
||||||
|
show_link = [] # example: "*" or ["alice", "bob"]
|
||||||
|
# default_dc = 2 # example: default DC for unmapped non-standard DCs
|
||||||
|
|
||||||
|
[general]
|
||||||
|
fast_mode = true
|
||||||
|
use_middle_proxy = false
|
||||||
|
# ad_tag = "00000000000000000000000000000000" # example
|
||||||
|
# proxy_secret_path = "proxy-secret" # example custom path
|
||||||
|
# middle_proxy_nat_ip = "203.0.113.10" # example public NAT IP override
|
||||||
|
middle_proxy_nat_probe = true
|
||||||
|
# middle_proxy_nat_stun = "stun.l.google.com:19302" # example
|
||||||
|
# middle_proxy_nat_stun_servers = [] # example: ["stun1.l.google.com:19302", "stun2.l.google.com:19302"]
|
||||||
|
middle_proxy_pool_size = 8
|
||||||
|
middle_proxy_warm_standby = 16
|
||||||
|
me_keepalive_enabled = true
|
||||||
|
me_keepalive_interval_secs = 25
|
||||||
|
me_keepalive_jitter_secs = 5
|
||||||
|
me_keepalive_payload_random = true
|
||||||
|
crypto_pending_buffer = 262144
|
||||||
|
max_client_frame = 16777216
|
||||||
|
desync_all_full = false
|
||||||
|
beobachten = true
|
||||||
|
beobachten_minutes = 10
|
||||||
|
beobachten_flush_secs = 15
|
||||||
|
beobachten_file = "cache/beobachten.txt"
|
||||||
|
hardswap = true
|
||||||
|
me_warmup_stagger_enabled = true
|
||||||
|
me_warmup_step_delay_ms = 500
|
||||||
|
me_warmup_step_jitter_ms = 300
|
||||||
|
me_reconnect_max_concurrent_per_dc = 8
|
||||||
|
me_reconnect_backoff_base_ms = 500
|
||||||
|
me_reconnect_backoff_cap_ms = 30000
|
||||||
|
me_reconnect_fast_retry_count = 12
|
||||||
|
stun_iface_mismatch_ignore = false
|
||||||
|
unknown_dc_log_path = "unknown-dc.txt" # to disable: set to null
|
||||||
|
log_level = "normal" # debug | verbose | normal | silent
|
||||||
|
disable_colors = false
|
||||||
|
fast_mode_min_tls_record = 0
|
||||||
|
update_every = 300
|
||||||
|
me_reinit_every_secs = 900
|
||||||
|
me_hardswap_warmup_delay_min_ms = 1000
|
||||||
|
me_hardswap_warmup_delay_max_ms = 2000
|
||||||
|
me_hardswap_warmup_extra_passes = 3
|
||||||
|
me_hardswap_warmup_pass_backoff_base_ms = 500
|
||||||
|
me_config_stable_snapshots = 2
|
||||||
|
me_config_apply_cooldown_secs = 300
|
||||||
|
proxy_secret_stable_snapshots = 2
|
||||||
|
proxy_secret_rotate_runtime = true
|
||||||
|
proxy_secret_len_max = 256
|
||||||
|
me_pool_drain_ttl_secs = 90
|
||||||
|
me_pool_min_fresh_ratio = 0.8
|
||||||
|
me_reinit_drain_timeout_secs = 120
|
||||||
|
# Legacy compatibility fields used when update_every is omitted.
|
||||||
|
proxy_secret_auto_reload_secs = 3600
|
||||||
|
proxy_config_auto_reload_secs = 3600
|
||||||
|
ntp_check = true
|
||||||
|
ntp_servers = ["pool.ntp.org"] # example: ["pool.ntp.org", "time.cloudflare.com"]
|
||||||
|
auto_degradation_enabled = true
|
||||||
|
degradation_min_unavailable_dc_groups = 2
|
||||||
|
|
||||||
|
[general.modes]
|
||||||
|
classic = false
|
||||||
|
secure = false
|
||||||
|
tls = true
|
||||||
|
|
||||||
|
[general.links]
|
||||||
|
show ="*" # example: "*" or ["alice", "bob"]
|
||||||
|
# public_host = "proxy.example.com" # example explicit host/IP for tg:// links
|
||||||
|
# public_port = 443 # example explicit port for tg:// links
|
||||||
|
|
||||||
|
[network]
|
||||||
|
ipv4 = true
|
||||||
|
ipv6 = false # set true to enable IPv6
|
||||||
|
prefer = 4 # 4 or 6
|
||||||
|
multipath = false
|
||||||
|
stun_servers = [
|
||||||
|
"stun.l.google.com:5349",
|
||||||
|
"stun1.l.google.com:3478",
|
||||||
|
"stun.gmx.net:3478",
|
||||||
|
"stun.l.google.com:19302",
|
||||||
|
"stun.1und1.de:3478",
|
||||||
|
"stun1.l.google.com:19302",
|
||||||
|
"stun2.l.google.com:19302",
|
||||||
|
"stun3.l.google.com:19302",
|
||||||
|
"stun4.l.google.com:19302",
|
||||||
|
"stun.services.mozilla.com:3478",
|
||||||
|
"stun.stunprotocol.org:3478",
|
||||||
|
"stun.nextcloud.com:3478",
|
||||||
|
"stun.voip.eutelia.it:3478",
|
||||||
|
]
|
||||||
|
stun_tcp_fallback = true
|
||||||
|
http_ip_detect_urls = ["https://ifconfig.me/ip", "https://api.ipify.org"]
|
||||||
|
cache_public_ip_path = "cache/public_ip.txt"
|
||||||
|
|
||||||
|
[server]
|
||||||
|
port = 443
|
||||||
|
listen_addr_ipv4 = "0.0.0.0"
|
||||||
|
listen_addr_ipv6 = "::"
|
||||||
|
# listen_unix_sock = "/var/run/telemt.sock" # example
|
||||||
|
# listen_unix_sock_perm = "0660" # example unix socket mode
|
||||||
|
# listen_tcp = true # example explicit override (auto-detected when omitted)
|
||||||
|
proxy_protocol = false
|
||||||
|
# metrics_port = 9090 # example
|
||||||
|
metrics_whitelist = ["127.0.0.1/32", "::1/128"]
|
||||||
|
# Example explicit listeners (default: omitted, auto-generated from listen_addr_*):
|
||||||
|
# [[server.listeners]]
|
||||||
|
# ip = "0.0.0.0"
|
||||||
|
# announce = "proxy-v4.example.com"
|
||||||
|
# # announce_ip = "203.0.113.10" # deprecated alias
|
||||||
|
# proxy_protocol = false
|
||||||
|
# reuse_allow = false
|
||||||
|
#
|
||||||
|
# [[server.listeners]]
|
||||||
|
# ip = "::"
|
||||||
|
# announce = "proxy-v6.example.com"
|
||||||
|
# proxy_protocol = false
|
||||||
|
# reuse_allow = false
|
||||||
|
|
||||||
|
[timeouts]
|
||||||
|
client_handshake = 15
|
||||||
|
tg_connect = 10
|
||||||
|
client_keepalive = 60
|
||||||
|
client_ack = 300
|
||||||
|
me_one_retry = 3
|
||||||
|
me_one_timeout_ms = 1500
|
||||||
|
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "petrovich.ru"
|
||||||
|
# tls_domains = ["example.com", "cdn.example.net"] # Additional domains for EE links
|
||||||
|
mask = true
|
||||||
|
# mask_host = "www.google.com" # example, defaults to tls_domain when both mask_host/mask_unix_sock are unset
|
||||||
|
# mask_unix_sock = "/var/run/nginx.sock" # example, mutually exclusive with mask_host
|
||||||
|
mask_port = 443
|
||||||
|
fake_cert_len = 2048 # if tls_emulation=false and default value is used, loader may randomize this value at runtime
|
||||||
|
tls_emulation = true
|
||||||
|
tls_front_dir = "tlsfront"
|
||||||
|
server_hello_delay_min_ms = 0
|
||||||
|
server_hello_delay_max_ms = 0
|
||||||
|
tls_new_session_tickets = 0
|
||||||
|
tls_full_cert_ttl_secs = 90
|
||||||
|
alpn_enforce = true
|
||||||
|
|
||||||
|
[access]
|
||||||
|
replay_check_len = 65536
|
||||||
|
replay_window_secs = 1800
|
||||||
|
ignore_time_skew = false
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
# format: "username" = "32_hex_chars_secret"
|
||||||
|
hello = "00000000000000000000000000000000"
|
||||||
|
# alice = "11111111111111111111111111111111" # example
|
||||||
|
|
||||||
|
[access.user_max_tcp_conns]
|
||||||
|
# alice = 100 # example
|
||||||
|
|
||||||
|
[access.user_expirations]
|
||||||
|
# alice = "2078-01-01T00:00:00Z" # example
|
||||||
|
|
||||||
|
[access.user_data_quota]
|
||||||
|
# hello = 10737418240 # example bytes
|
||||||
|
# alice = 10737418240 # example bytes
|
||||||
|
|
||||||
|
[access.user_max_unique_ips]
|
||||||
|
# hello = 10 # example
|
||||||
|
# alice = 100 # example
|
||||||
|
|
||||||
|
# Default behavior if [[upstreams]] is omitted: loader injects one direct upstream.
|
||||||
|
# Example explicit upstreams:
|
||||||
|
# [[upstreams]]
|
||||||
|
# type = "direct"
|
||||||
|
# interface = "eth0"
|
||||||
|
# bind_addresses = ["192.0.2.10"]
|
||||||
|
# weight = 1
|
||||||
|
# enabled = true
|
||||||
|
# scopes = "*"
|
||||||
|
#
|
||||||
|
# [[upstreams]]
|
||||||
|
# type = "socks4"
|
||||||
|
# address = "198.51.100.20:1080"
|
||||||
|
# interface = "eth0"
|
||||||
|
# user_id = "telemt"
|
||||||
|
# weight = 1
|
||||||
|
# enabled = true
|
||||||
|
# scopes = "*"
|
||||||
|
#
|
||||||
|
# [[upstreams]]
|
||||||
|
# type = "socks5"
|
||||||
|
# address = "198.51.100.30:1080"
|
||||||
|
# interface = "eth0"
|
||||||
|
# username = "proxy-user"
|
||||||
|
# password = "proxy-pass"
|
||||||
|
# weight = 1
|
||||||
|
# enabled = true
|
||||||
|
# scopes = "*"
|
||||||
|
|
||||||
|
# === DC Address Overrides ===
|
||||||
|
# [dc_overrides]
|
||||||
|
# "201" = "149.154.175.50:443" # example
|
||||||
|
# "202" = ["149.154.167.51:443", "149.154.175.100:443"] # example
|
||||||
|
# "203" = "91.105.192.100:443" # loader auto-adds this one when omitted
|
||||||
118
config.toml
118
config.toml
@@ -1,11 +1,7 @@
|
|||||||
# === General Settings ===
|
# === General Settings ===
|
||||||
[general]
|
[general]
|
||||||
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 ===
|
||||||
# Log level: debug | verbose | normal | silent
|
# Log level: debug | verbose | normal | silent
|
||||||
@@ -13,135 +9,29 @@ proxy_secret_path = "proxy-secret"
|
|||||||
# 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
|
||||||
listen_addr_ipv4 = "0.0.0.0"
|
|
||||||
listen_addr_ipv6 = "::"
|
|
||||||
# listen_unix_sock = "/var/run/telemt.sock" # Unix socket
|
|
||||||
# listen_unix_sock_perm = "0666" # Socket file permissions
|
|
||||||
# proxy_protocol = false # Enable if behind HAProxy/nginx with PROXY protocol
|
# proxy_protocol = false # Enable if behind HAProxy/nginx with PROXY protocol
|
||||||
# metrics_port = 9090
|
metrics_port = 9090
|
||||||
# metrics_whitelist = ["127.0.0.1", "::1"]
|
metrics_whitelist = ["127.0.0.1", "::1", "0.0.0.0/0"]
|
||||||
|
|
||||||
# Listen on multiple interfaces/IPs - IPv4
|
# Listen on multiple interfaces/IPs - IPv4
|
||||||
[[server.listeners]]
|
[[server.listeners]]
|
||||||
ip = "0.0.0.0"
|
ip = "0.0.0.0"
|
||||||
|
|
||||||
# Listen on multiple interfaces/IPs - IPv6
|
|
||||||
[[server.listeners]]
|
|
||||||
ip = "::"
|
|
||||||
|
|
||||||
# === Timeouts (in seconds) ===
|
|
||||||
[timeouts]
|
|
||||||
client_handshake = 30
|
|
||||||
tg_connect = 10
|
|
||||||
client_keepalive = 60
|
|
||||||
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
|
tls_emulation = true # Fetch real cert lengths and emulate TLS records
|
||||||
# mask_host = "petrovich.ru" # Defaults to tls_domain if not set
|
tls_front_dir = "tlsfront" # Cache directory for TLS emulation
|
||||||
# mask_unix_sock = "/var/run/nginx.sock" # Unix socket (mutually exclusive with mask_host)
|
|
||||||
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]
|
|
||||||
replay_check_len = 65536
|
|
||||||
replay_window_secs = 1800
|
|
||||||
ignore_time_skew = false
|
|
||||||
|
|
||||||
[access.users]
|
[access.users]
|
||||||
# format: "username" = "32_hex_chars_secret"
|
# format: "username" = "32_hex_chars_secret"
|
||||||
hello = "00000000000000000000000000000000"
|
hello = "00000000000000000000000000000000"
|
||||||
|
|
||||||
# [access.user_max_tcp_conns]
|
|
||||||
# hello = 50
|
|
||||||
|
|
||||||
# [access.user_max_unique_ips]
|
|
||||||
# hello = 5
|
|
||||||
|
|
||||||
# [access.user_data_quota]
|
|
||||||
# hello = 1073741824 # 1 GB
|
|
||||||
|
|
||||||
# [access.user_expirations]
|
|
||||||
# format: username = "[year]-[month]-[day]T[hour]:[minute]:[second]Z" UTC
|
|
||||||
# hello = "2027-01-01T00:00:00Z"
|
|
||||||
|
|
||||||
# === Upstreams & Routing ===
|
|
||||||
[[upstreams]]
|
|
||||||
type = "direct"
|
|
||||||
enabled = true
|
|
||||||
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]]
|
|
||||||
# type = "socks5"
|
|
||||||
# address = "127.0.0.1:1080"
|
|
||||||
# enabled = false
|
|
||||||
# weight = 1
|
|
||||||
|
|
||||||
# === DC Address Overrides ===
|
|
||||||
# [dc_overrides]
|
|
||||||
# "203" = "91.105.192.100:443"
|
|
||||||
|
|||||||
@@ -194,6 +194,12 @@ prefer_ipv6 = false
|
|||||||
fast_mode = true
|
fast_mode = true
|
||||||
use_middle_proxy = false
|
use_middle_proxy = false
|
||||||
log_level = "normal"
|
log_level = "normal"
|
||||||
|
desync_all_full = false
|
||||||
|
update_every = 43200
|
||||||
|
hardswap = false
|
||||||
|
me_pool_drain_ttl_secs = 90
|
||||||
|
me_pool_min_fresh_ratio = 0.8
|
||||||
|
me_reinit_drain_timeout_secs = 120
|
||||||
|
|
||||||
[network]
|
[network]
|
||||||
ipv4 = true
|
ipv4 = true
|
||||||
|
|||||||
@@ -1,9 +1,17 @@
|
|||||||
use std::net::IpAddr;
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use ipnetwork::IpNetwork;
|
use ipnetwork::IpNetwork;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
// Helper defaults kept private to the config module.
|
// Helper defaults kept private to the config module.
|
||||||
|
const DEFAULT_NETWORK_IPV6: Option<bool> = Some(false);
|
||||||
|
const DEFAULT_STUN_TCP_FALLBACK: bool = true;
|
||||||
|
const DEFAULT_MIDDLE_PROXY_WARM_STANDBY: usize = 16;
|
||||||
|
const DEFAULT_ME_RECONNECT_MAX_CONCURRENT_PER_DC: u32 = 8;
|
||||||
|
const DEFAULT_ME_RECONNECT_FAST_RETRY_COUNT: u32 = 12;
|
||||||
|
const DEFAULT_LISTEN_ADDR_IPV6: &str = "::";
|
||||||
|
const DEFAULT_ACCESS_USER: &str = "default";
|
||||||
|
const DEFAULT_ACCESS_SECRET: &str = "00000000000000000000000000000000";
|
||||||
|
|
||||||
pub(crate) fn default_true() -> bool {
|
pub(crate) fn default_true() -> bool {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
@@ -78,12 +86,24 @@ pub(crate) fn default_prefer_4() -> u8 {
|
|||||||
4
|
4
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_network_ipv6() -> Option<bool> {
|
||||||
|
DEFAULT_NETWORK_IPV6
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_stun_tcp_fallback() -> bool {
|
||||||
|
DEFAULT_STUN_TCP_FALLBACK
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn default_unknown_dc_log_path() -> Option<String> {
|
pub(crate) fn default_unknown_dc_log_path() -> Option<String> {
|
||||||
Some("unknown-dc.txt".to_string())
|
Some("unknown-dc.txt".to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn default_pool_size() -> usize {
|
pub(crate) fn default_pool_size() -> usize {
|
||||||
2
|
8
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_middle_proxy_warm_standby() -> usize {
|
||||||
|
DEFAULT_MIDDLE_PROXY_WARM_STANDBY
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn default_keepalive_interval() -> u64 {
|
pub(crate) fn default_keepalive_interval() -> u64 {
|
||||||
@@ -110,6 +130,14 @@ pub(crate) fn default_reconnect_backoff_cap_ms() -> u64 {
|
|||||||
30_000
|
30_000
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_reconnect_max_concurrent_per_dc() -> u32 {
|
||||||
|
DEFAULT_ME_RECONNECT_MAX_CONCURRENT_PER_DC
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_reconnect_fast_retry_count() -> u32 {
|
||||||
|
DEFAULT_ME_RECONNECT_FAST_RETRY_COUNT
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn default_crypto_pending_buffer() -> usize {
|
pub(crate) fn default_crypto_pending_buffer() -> usize {
|
||||||
256 * 1024
|
256 * 1024
|
||||||
}
|
}
|
||||||
@@ -118,6 +146,22 @@ pub(crate) fn default_max_client_frame() -> usize {
|
|||||||
16 * 1024 * 1024
|
16 * 1024 * 1024
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_desync_all_full() -> bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_beobachten_minutes() -> u64 {
|
||||||
|
10
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_beobachten_flush_secs() -> u64 {
|
||||||
|
15
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_beobachten_file() -> String {
|
||||||
|
"cache/beobachten.txt".to_string()
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn default_tls_new_session_tickets() -> u8 {
|
pub(crate) fn default_tls_new_session_tickets() -> u8 {
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
@@ -140,10 +184,18 @@ pub(crate) fn default_alpn_enforce() -> bool {
|
|||||||
|
|
||||||
pub(crate) fn default_stun_servers() -> Vec<String> {
|
pub(crate) fn default_stun_servers() -> Vec<String> {
|
||||||
vec![
|
vec![
|
||||||
|
"stun.l.google.com:5349".to_string(),
|
||||||
|
"stun1.l.google.com:3478".to_string(),
|
||||||
|
"stun.gmx.net:3478".to_string(),
|
||||||
"stun.l.google.com:19302".to_string(),
|
"stun.l.google.com:19302".to_string(),
|
||||||
|
"stun.1und1.de:3478".to_string(),
|
||||||
"stun1.l.google.com:19302".to_string(),
|
"stun1.l.google.com:19302".to_string(),
|
||||||
"stun2.l.google.com:19302".to_string(),
|
"stun2.l.google.com:19302".to_string(),
|
||||||
|
"stun3.l.google.com:19302".to_string(),
|
||||||
|
"stun4.l.google.com:19302".to_string(),
|
||||||
|
"stun.services.mozilla.com:3478".to_string(),
|
||||||
"stun.stunprotocol.org:3478".to_string(),
|
"stun.stunprotocol.org:3478".to_string(),
|
||||||
|
"stun.nextcloud.com:3478".to_string(),
|
||||||
"stun.voip.eutelia.it:3478".to_string(),
|
"stun.voip.eutelia.it:3478".to_string(),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -160,11 +212,75 @@ pub(crate) fn default_cache_public_ip_path() -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn default_proxy_secret_reload_secs() -> u64 {
|
pub(crate) fn default_proxy_secret_reload_secs() -> u64 {
|
||||||
12 * 60 * 60
|
60 * 60
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn default_proxy_config_reload_secs() -> u64 {
|
pub(crate) fn default_proxy_config_reload_secs() -> u64 {
|
||||||
12 * 60 * 60
|
60 * 60
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_update_every_secs() -> u64 {
|
||||||
|
5 * 60
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_update_every() -> Option<u64> {
|
||||||
|
Some(default_update_every_secs())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_reinit_every_secs() -> u64 {
|
||||||
|
15 * 60
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_hardswap_warmup_delay_min_ms() -> u64 {
|
||||||
|
1000
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_hardswap_warmup_delay_max_ms() -> u64 {
|
||||||
|
2000
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_hardswap_warmup_extra_passes() -> u8 {
|
||||||
|
3
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_hardswap_warmup_pass_backoff_base_ms() -> u64 {
|
||||||
|
500
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_config_stable_snapshots() -> u8 {
|
||||||
|
2
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_config_apply_cooldown_secs() -> u64 {
|
||||||
|
300
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_proxy_secret_stable_snapshots() -> u8 {
|
||||||
|
2
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_proxy_secret_rotate_runtime() -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_proxy_secret_len_max() -> usize {
|
||||||
|
256
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_reinit_drain_timeout_secs() -> u64 {
|
||||||
|
120
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_pool_drain_ttl_secs() -> u64 {
|
||||||
|
90
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_me_pool_min_fresh_ratio() -> f32 {
|
||||||
|
0.8
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_hardswap() -> bool {
|
||||||
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn default_ntp_check() -> bool {
|
pub(crate) fn default_ntp_check() -> bool {
|
||||||
@@ -183,6 +299,17 @@ pub(crate) fn default_degradation_min_unavailable_dc_groups() -> u8 {
|
|||||||
2
|
2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_listen_addr_ipv6() -> String {
|
||||||
|
DEFAULT_LISTEN_ADDR_IPV6.to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn default_access_users() -> HashMap<String, String> {
|
||||||
|
HashMap::from([(
|
||||||
|
DEFAULT_ACCESS_USER.to_string(),
|
||||||
|
DEFAULT_ACCESS_SECRET.to_string(),
|
||||||
|
)])
|
||||||
|
}
|
||||||
|
|
||||||
// Custom deserializer helpers
|
// Custom deserializer helpers
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
|
|||||||
@@ -10,6 +10,12 @@
|
|||||||
//! | `general` | `ad_tag` | Passed on next connection |
|
//! | `general` | `ad_tag` | Passed on next connection |
|
||||||
//! | `general` | `middle_proxy_pool_size` | Passed on next connection |
|
//! | `general` | `middle_proxy_pool_size` | Passed on next connection |
|
||||||
//! | `general` | `me_keepalive_*` | Passed on next connection |
|
//! | `general` | `me_keepalive_*` | Passed on next connection |
|
||||||
|
//! | `general` | `desync_all_full` | Applied immediately |
|
||||||
|
//! | `general` | `update_every` | Applied to ME updater immediately |
|
||||||
|
//! | `general` | `hardswap` | Applied on next ME map update |
|
||||||
|
//! | `general` | `me_pool_drain_ttl_secs` | Applied on next ME map update |
|
||||||
|
//! | `general` | `me_pool_min_fresh_ratio` | Applied on next ME map update |
|
||||||
|
//! | `general` | `me_reinit_drain_timeout_secs`| Applied on next ME map update |
|
||||||
//! | `access` | All user/quota fields | Effective immediately |
|
//! | `access` | All user/quota fields | Effective immediately |
|
||||||
//!
|
//!
|
||||||
//! Fields that require re-binding sockets (`server.port`, `censorship.*`,
|
//! Fields that require re-binding sockets (`server.port`, `censorship.*`,
|
||||||
@@ -34,6 +40,12 @@ pub struct HotFields {
|
|||||||
pub log_level: LogLevel,
|
pub log_level: LogLevel,
|
||||||
pub ad_tag: Option<String>,
|
pub ad_tag: Option<String>,
|
||||||
pub middle_proxy_pool_size: usize,
|
pub middle_proxy_pool_size: usize,
|
||||||
|
pub desync_all_full: bool,
|
||||||
|
pub update_every_secs: u64,
|
||||||
|
pub hardswap: bool,
|
||||||
|
pub me_pool_drain_ttl_secs: u64,
|
||||||
|
pub me_pool_min_fresh_ratio: f32,
|
||||||
|
pub me_reinit_drain_timeout_secs: u64,
|
||||||
pub me_keepalive_enabled: bool,
|
pub me_keepalive_enabled: bool,
|
||||||
pub me_keepalive_interval_secs: u64,
|
pub me_keepalive_interval_secs: u64,
|
||||||
pub me_keepalive_jitter_secs: u64,
|
pub me_keepalive_jitter_secs: u64,
|
||||||
@@ -47,6 +59,12 @@ impl HotFields {
|
|||||||
log_level: cfg.general.log_level.clone(),
|
log_level: cfg.general.log_level.clone(),
|
||||||
ad_tag: cfg.general.ad_tag.clone(),
|
ad_tag: cfg.general.ad_tag.clone(),
|
||||||
middle_proxy_pool_size: cfg.general.middle_proxy_pool_size,
|
middle_proxy_pool_size: cfg.general.middle_proxy_pool_size,
|
||||||
|
desync_all_full: cfg.general.desync_all_full,
|
||||||
|
update_every_secs: cfg.general.effective_update_every_secs(),
|
||||||
|
hardswap: cfg.general.hardswap,
|
||||||
|
me_pool_drain_ttl_secs: cfg.general.me_pool_drain_ttl_secs,
|
||||||
|
me_pool_min_fresh_ratio: cfg.general.me_pool_min_fresh_ratio,
|
||||||
|
me_reinit_drain_timeout_secs: cfg.general.me_reinit_drain_timeout_secs,
|
||||||
me_keepalive_enabled: cfg.general.me_keepalive_enabled,
|
me_keepalive_enabled: cfg.general.me_keepalive_enabled,
|
||||||
me_keepalive_interval_secs: cfg.general.me_keepalive_interval_secs,
|
me_keepalive_interval_secs: cfg.general.me_keepalive_interval_secs,
|
||||||
me_keepalive_jitter_secs: cfg.general.me_keepalive_jitter_secs,
|
me_keepalive_jitter_secs: cfg.general.me_keepalive_jitter_secs,
|
||||||
@@ -175,6 +193,48 @@ fn log_changes(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if old_hot.desync_all_full != new_hot.desync_all_full {
|
||||||
|
info!(
|
||||||
|
"config reload: desync_all_full: {} → {}",
|
||||||
|
old_hot.desync_all_full, new_hot.desync_all_full,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if old_hot.update_every_secs != new_hot.update_every_secs {
|
||||||
|
info!(
|
||||||
|
"config reload: update_every(effective): {}s → {}s",
|
||||||
|
old_hot.update_every_secs, new_hot.update_every_secs,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if old_hot.hardswap != new_hot.hardswap {
|
||||||
|
info!(
|
||||||
|
"config reload: hardswap: {} → {}",
|
||||||
|
old_hot.hardswap, new_hot.hardswap,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if old_hot.me_pool_drain_ttl_secs != new_hot.me_pool_drain_ttl_secs {
|
||||||
|
info!(
|
||||||
|
"config reload: me_pool_drain_ttl_secs: {}s → {}s",
|
||||||
|
old_hot.me_pool_drain_ttl_secs, new_hot.me_pool_drain_ttl_secs,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (old_hot.me_pool_min_fresh_ratio - new_hot.me_pool_min_fresh_ratio).abs() > f32::EPSILON {
|
||||||
|
info!(
|
||||||
|
"config reload: me_pool_min_fresh_ratio: {:.3} → {:.3}",
|
||||||
|
old_hot.me_pool_min_fresh_ratio, new_hot.me_pool_min_fresh_ratio,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if old_hot.me_reinit_drain_timeout_secs != new_hot.me_reinit_drain_timeout_secs {
|
||||||
|
info!(
|
||||||
|
"config reload: me_reinit_drain_timeout_secs: {}s → {}s",
|
||||||
|
old_hot.me_reinit_drain_timeout_secs, new_hot.me_reinit_drain_timeout_secs,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if old_hot.me_keepalive_enabled != new_hot.me_keepalive_enabled
|
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_interval_secs != new_hot.me_keepalive_interval_secs
|
||||||
|| old_hot.me_keepalive_jitter_secs != new_hot.me_keepalive_jitter_secs
|
|| old_hot.me_keepalive_jitter_secs != new_hot.me_keepalive_jitter_secs
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
#![allow(deprecated)]
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::net::IpAddr;
|
use std::net::IpAddr;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
@@ -117,6 +119,120 @@ impl ProxyConfig {
|
|||||||
let mut config: ProxyConfig =
|
let mut config: ProxyConfig =
|
||||||
toml::from_str(&processed).map_err(|e| ProxyError::Config(e.to_string()))?;
|
toml::from_str(&processed).map_err(|e| ProxyError::Config(e.to_string()))?;
|
||||||
|
|
||||||
|
if let Some(update_every) = config.general.update_every {
|
||||||
|
if update_every == 0 {
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"general.update_every must be > 0".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let legacy_secret = config.general.proxy_secret_auto_reload_secs;
|
||||||
|
let legacy_config = config.general.proxy_config_auto_reload_secs;
|
||||||
|
let effective = legacy_secret.min(legacy_config);
|
||||||
|
if effective == 0 {
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"legacy proxy_*_auto_reload_secs values must be > 0 when general.update_every is not set".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if legacy_secret != default_proxy_secret_reload_secs()
|
||||||
|
|| legacy_config != default_proxy_config_reload_secs()
|
||||||
|
{
|
||||||
|
warn!(
|
||||||
|
proxy_secret_auto_reload_secs = legacy_secret,
|
||||||
|
proxy_config_auto_reload_secs = legacy_config,
|
||||||
|
effective_update_every_secs = effective,
|
||||||
|
"proxy_*_auto_reload_secs are deprecated; set general.update_every"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.general.me_reinit_every_secs == 0 {
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"general.me_reinit_every_secs must be > 0".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.general.beobachten_minutes == 0 {
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"general.beobachten_minutes must be > 0".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.general.beobachten_flush_secs == 0 {
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"general.beobachten_flush_secs must be > 0".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.general.beobachten_file.trim().is_empty() {
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"general.beobachten_file cannot be empty".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.general.me_hardswap_warmup_delay_max_ms == 0 {
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"general.me_hardswap_warmup_delay_max_ms must be > 0".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.general.me_hardswap_warmup_delay_min_ms
|
||||||
|
> config.general.me_hardswap_warmup_delay_max_ms
|
||||||
|
{
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"general.me_hardswap_warmup_delay_min_ms must be <= general.me_hardswap_warmup_delay_max_ms".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.general.me_hardswap_warmup_extra_passes > 10 {
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"general.me_hardswap_warmup_extra_passes must be within [0, 10]".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.general.me_hardswap_warmup_pass_backoff_base_ms == 0 {
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"general.me_hardswap_warmup_pass_backoff_base_ms must be > 0".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.general.me_config_stable_snapshots == 0 {
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"general.me_config_stable_snapshots must be > 0".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.general.proxy_secret_stable_snapshots == 0 {
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"general.proxy_secret_stable_snapshots must be > 0".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if !(32..=4096).contains(&config.general.proxy_secret_len_max) {
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"general.proxy_secret_len_max must be within [32, 4096]".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if !(0.0..=1.0).contains(&config.general.me_pool_min_fresh_ratio) {
|
||||||
|
return Err(ProxyError::Config(
|
||||||
|
"general.me_pool_min_fresh_ratio must be within [0.0, 1.0]".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.general.effective_me_pool_force_close_secs() > 0
|
||||||
|
&& config.general.effective_me_pool_force_close_secs()
|
||||||
|
< config.general.me_pool_drain_ttl_secs
|
||||||
|
{
|
||||||
|
warn!(
|
||||||
|
me_pool_drain_ttl_secs = config.general.me_pool_drain_ttl_secs,
|
||||||
|
me_reinit_drain_timeout_secs = config.general.effective_me_pool_force_close_secs(),
|
||||||
|
"force-close timeout is lower than drain TTL; bumping force-close timeout to TTL"
|
||||||
|
);
|
||||||
|
config.general.me_reinit_drain_timeout_secs = config.general.me_pool_drain_ttl_secs;
|
||||||
|
}
|
||||||
|
|
||||||
// Validate secrets.
|
// Validate secrets.
|
||||||
for (user, secret) in &config.access.users {
|
for (user, secret) in &config.access.users {
|
||||||
if !secret.chars().all(|c| c.is_ascii_hexdigit()) || secret.len() != 32 {
|
if !secret.chars().all(|c| c.is_ascii_hexdigit()) || secret.len() != 32 {
|
||||||
@@ -230,23 +346,25 @@ impl ProxyConfig {
|
|||||||
reuse_allow: false,
|
reuse_allow: false,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if let Some(ipv6_str) = &config.server.listen_addr_ipv6 {
|
if let Some(ipv6_str) = &config.server.listen_addr_ipv6
|
||||||
if let Ok(ipv6) = ipv6_str.parse::<IpAddr>() {
|
&& let Ok(ipv6) = ipv6_str.parse::<IpAddr>()
|
||||||
config.server.listeners.push(ListenerConfig {
|
{
|
||||||
ip: ipv6,
|
config.server.listeners.push(ListenerConfig {
|
||||||
announce: None,
|
ip: ipv6,
|
||||||
announce_ip: None,
|
announce: None,
|
||||||
proxy_protocol: None,
|
announce_ip: None,
|
||||||
reuse_allow: false,
|
proxy_protocol: None,
|
||||||
});
|
reuse_allow: false,
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Migration: announce_ip → announce for each listener.
|
// Migration: announce_ip → announce for each listener.
|
||||||
for listener in &mut config.server.listeners {
|
for listener in &mut config.server.listeners {
|
||||||
if listener.announce.is_none() && listener.announce_ip.is_some() {
|
if listener.announce.is_none()
|
||||||
listener.announce = Some(listener.announce_ip.unwrap().to_string());
|
&& let Some(ip) = listener.announce_ip.take()
|
||||||
|
{
|
||||||
|
listener.announce = Some(ip.to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -309,6 +427,55 @@ impl ProxyConfig {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn serde_defaults_remain_unchanged_for_present_sections() {
|
||||||
|
let toml = r#"
|
||||||
|
[network]
|
||||||
|
[general]
|
||||||
|
[server]
|
||||||
|
[access]
|
||||||
|
"#;
|
||||||
|
let cfg: ProxyConfig = toml::from_str(toml).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(cfg.network.ipv6, None);
|
||||||
|
assert!(!cfg.network.stun_tcp_fallback);
|
||||||
|
assert_eq!(cfg.general.middle_proxy_warm_standby, 0);
|
||||||
|
assert_eq!(cfg.general.me_reconnect_max_concurrent_per_dc, 0);
|
||||||
|
assert_eq!(cfg.general.me_reconnect_fast_retry_count, 0);
|
||||||
|
assert_eq!(cfg.general.update_every, None);
|
||||||
|
assert_eq!(cfg.server.listen_addr_ipv4, None);
|
||||||
|
assert_eq!(cfg.server.listen_addr_ipv6, None);
|
||||||
|
assert!(cfg.access.users.is_empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn impl_defaults_are_sourced_from_default_helpers() {
|
||||||
|
let network = NetworkConfig::default();
|
||||||
|
assert_eq!(network.ipv6, default_network_ipv6());
|
||||||
|
assert_eq!(network.stun_tcp_fallback, default_stun_tcp_fallback());
|
||||||
|
|
||||||
|
let general = GeneralConfig::default();
|
||||||
|
assert_eq!(
|
||||||
|
general.middle_proxy_warm_standby,
|
||||||
|
default_middle_proxy_warm_standby()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
general.me_reconnect_max_concurrent_per_dc,
|
||||||
|
default_me_reconnect_max_concurrent_per_dc()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
general.me_reconnect_fast_retry_count,
|
||||||
|
default_me_reconnect_fast_retry_count()
|
||||||
|
);
|
||||||
|
assert_eq!(general.update_every, default_update_every());
|
||||||
|
|
||||||
|
let server = ServerConfig::default();
|
||||||
|
assert_eq!(server.listen_addr_ipv6, Some(default_listen_addr_ipv6()));
|
||||||
|
|
||||||
|
let access = AccessConfig::default();
|
||||||
|
assert_eq!(access.users, default_access_users());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn dc_overrides_allow_string_and_array() {
|
fn dc_overrides_allow_string_and_array() {
|
||||||
let toml = r#"
|
let toml = r#"
|
||||||
@@ -347,4 +514,324 @@ mod tests {
|
|||||||
.unwrap_or(false));
|
.unwrap_or(false));
|
||||||
let _ = std::fs::remove_file(path);
|
let _ = std::fs::remove_file(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn update_every_overrides_legacy_fields() {
|
||||||
|
let toml = r#"
|
||||||
|
[general]
|
||||||
|
update_every = 123
|
||||||
|
proxy_secret_auto_reload_secs = 700
|
||||||
|
proxy_config_auto_reload_secs = 800
|
||||||
|
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "example.com"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
user = "00000000000000000000000000000000"
|
||||||
|
"#;
|
||||||
|
let dir = std::env::temp_dir();
|
||||||
|
let path = dir.join("telemt_update_every_override_test.toml");
|
||||||
|
std::fs::write(&path, toml).unwrap();
|
||||||
|
let cfg = ProxyConfig::load(&path).unwrap();
|
||||||
|
assert_eq!(cfg.general.effective_update_every_secs(), 123);
|
||||||
|
let _ = std::fs::remove_file(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn update_every_fallback_to_legacy_min() {
|
||||||
|
let toml = r#"
|
||||||
|
[general]
|
||||||
|
proxy_secret_auto_reload_secs = 600
|
||||||
|
proxy_config_auto_reload_secs = 120
|
||||||
|
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "example.com"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
user = "00000000000000000000000000000000"
|
||||||
|
"#;
|
||||||
|
let dir = std::env::temp_dir();
|
||||||
|
let path = dir.join("telemt_update_every_legacy_min_test.toml");
|
||||||
|
std::fs::write(&path, toml).unwrap();
|
||||||
|
let cfg = ProxyConfig::load(&path).unwrap();
|
||||||
|
assert_eq!(cfg.general.update_every, None);
|
||||||
|
assert_eq!(cfg.general.effective_update_every_secs(), 120);
|
||||||
|
let _ = std::fs::remove_file(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn update_every_zero_is_rejected() {
|
||||||
|
let toml = r#"
|
||||||
|
[general]
|
||||||
|
update_every = 0
|
||||||
|
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "example.com"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
user = "00000000000000000000000000000000"
|
||||||
|
"#;
|
||||||
|
let dir = std::env::temp_dir();
|
||||||
|
let path = dir.join("telemt_update_every_zero_test.toml");
|
||||||
|
std::fs::write(&path, toml).unwrap();
|
||||||
|
let err = ProxyConfig::load(&path).unwrap_err().to_string();
|
||||||
|
assert!(err.contains("general.update_every must be > 0"));
|
||||||
|
let _ = std::fs::remove_file(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn me_reinit_every_default_is_set() {
|
||||||
|
let toml = r#"
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "example.com"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
user = "00000000000000000000000000000000"
|
||||||
|
"#;
|
||||||
|
let dir = std::env::temp_dir();
|
||||||
|
let path = dir.join("telemt_me_reinit_every_default_test.toml");
|
||||||
|
std::fs::write(&path, toml).unwrap();
|
||||||
|
let cfg = ProxyConfig::load(&path).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
cfg.general.me_reinit_every_secs,
|
||||||
|
default_me_reinit_every_secs()
|
||||||
|
);
|
||||||
|
let _ = std::fs::remove_file(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn me_reinit_every_zero_is_rejected() {
|
||||||
|
let toml = r#"
|
||||||
|
[general]
|
||||||
|
me_reinit_every_secs = 0
|
||||||
|
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "example.com"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
user = "00000000000000000000000000000000"
|
||||||
|
"#;
|
||||||
|
let dir = std::env::temp_dir();
|
||||||
|
let path = dir.join("telemt_me_reinit_every_zero_test.toml");
|
||||||
|
std::fs::write(&path, toml).unwrap();
|
||||||
|
let err = ProxyConfig::load(&path).unwrap_err().to_string();
|
||||||
|
assert!(err.contains("general.me_reinit_every_secs must be > 0"));
|
||||||
|
let _ = std::fs::remove_file(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn me_hardswap_warmup_defaults_are_set() {
|
||||||
|
let toml = r#"
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "example.com"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
user = "00000000000000000000000000000000"
|
||||||
|
"#;
|
||||||
|
let dir = std::env::temp_dir();
|
||||||
|
let path = dir.join("telemt_me_hardswap_warmup_defaults_test.toml");
|
||||||
|
std::fs::write(&path, toml).unwrap();
|
||||||
|
let cfg = ProxyConfig::load(&path).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
cfg.general.me_hardswap_warmup_delay_min_ms,
|
||||||
|
default_me_hardswap_warmup_delay_min_ms()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
cfg.general.me_hardswap_warmup_delay_max_ms,
|
||||||
|
default_me_hardswap_warmup_delay_max_ms()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
cfg.general.me_hardswap_warmup_extra_passes,
|
||||||
|
default_me_hardswap_warmup_extra_passes()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
cfg.general.me_hardswap_warmup_pass_backoff_base_ms,
|
||||||
|
default_me_hardswap_warmup_pass_backoff_base_ms()
|
||||||
|
);
|
||||||
|
let _ = std::fs::remove_file(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn me_hardswap_warmup_delay_range_is_validated() {
|
||||||
|
let toml = r#"
|
||||||
|
[general]
|
||||||
|
me_hardswap_warmup_delay_min_ms = 2001
|
||||||
|
me_hardswap_warmup_delay_max_ms = 2000
|
||||||
|
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "example.com"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
user = "00000000000000000000000000000000"
|
||||||
|
"#;
|
||||||
|
let dir = std::env::temp_dir();
|
||||||
|
let path = dir.join("telemt_me_hardswap_warmup_delay_range_test.toml");
|
||||||
|
std::fs::write(&path, toml).unwrap();
|
||||||
|
let err = ProxyConfig::load(&path).unwrap_err().to_string();
|
||||||
|
assert!(err.contains(
|
||||||
|
"general.me_hardswap_warmup_delay_min_ms must be <= general.me_hardswap_warmup_delay_max_ms"
|
||||||
|
));
|
||||||
|
let _ = std::fs::remove_file(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn me_hardswap_warmup_delay_max_zero_is_rejected() {
|
||||||
|
let toml = r#"
|
||||||
|
[general]
|
||||||
|
me_hardswap_warmup_delay_max_ms = 0
|
||||||
|
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "example.com"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
user = "00000000000000000000000000000000"
|
||||||
|
"#;
|
||||||
|
let dir = std::env::temp_dir();
|
||||||
|
let path = dir.join("telemt_me_hardswap_warmup_delay_max_zero_test.toml");
|
||||||
|
std::fs::write(&path, toml).unwrap();
|
||||||
|
let err = ProxyConfig::load(&path).unwrap_err().to_string();
|
||||||
|
assert!(err.contains("general.me_hardswap_warmup_delay_max_ms must be > 0"));
|
||||||
|
let _ = std::fs::remove_file(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn me_hardswap_warmup_extra_passes_out_of_range_is_rejected() {
|
||||||
|
let toml = r#"
|
||||||
|
[general]
|
||||||
|
me_hardswap_warmup_extra_passes = 11
|
||||||
|
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "example.com"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
user = "00000000000000000000000000000000"
|
||||||
|
"#;
|
||||||
|
let dir = std::env::temp_dir();
|
||||||
|
let path = dir.join("telemt_me_hardswap_warmup_extra_passes_test.toml");
|
||||||
|
std::fs::write(&path, toml).unwrap();
|
||||||
|
let err = ProxyConfig::load(&path).unwrap_err().to_string();
|
||||||
|
assert!(err.contains("general.me_hardswap_warmup_extra_passes must be within [0, 10]"));
|
||||||
|
let _ = std::fs::remove_file(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn me_hardswap_warmup_pass_backoff_zero_is_rejected() {
|
||||||
|
let toml = r#"
|
||||||
|
[general]
|
||||||
|
me_hardswap_warmup_pass_backoff_base_ms = 0
|
||||||
|
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "example.com"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
user = "00000000000000000000000000000000"
|
||||||
|
"#;
|
||||||
|
let dir = std::env::temp_dir();
|
||||||
|
let path = dir.join("telemt_me_hardswap_warmup_backoff_zero_test.toml");
|
||||||
|
std::fs::write(&path, toml).unwrap();
|
||||||
|
let err = ProxyConfig::load(&path).unwrap_err().to_string();
|
||||||
|
assert!(err.contains("general.me_hardswap_warmup_pass_backoff_base_ms must be > 0"));
|
||||||
|
let _ = std::fs::remove_file(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn me_config_stable_snapshots_zero_is_rejected() {
|
||||||
|
let toml = r#"
|
||||||
|
[general]
|
||||||
|
me_config_stable_snapshots = 0
|
||||||
|
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "example.com"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
user = "00000000000000000000000000000000"
|
||||||
|
"#;
|
||||||
|
let dir = std::env::temp_dir();
|
||||||
|
let path = dir.join("telemt_me_config_stable_snapshots_zero_test.toml");
|
||||||
|
std::fs::write(&path, toml).unwrap();
|
||||||
|
let err = ProxyConfig::load(&path).unwrap_err().to_string();
|
||||||
|
assert!(err.contains("general.me_config_stable_snapshots must be > 0"));
|
||||||
|
let _ = std::fs::remove_file(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn proxy_secret_stable_snapshots_zero_is_rejected() {
|
||||||
|
let toml = r#"
|
||||||
|
[general]
|
||||||
|
proxy_secret_stable_snapshots = 0
|
||||||
|
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "example.com"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
user = "00000000000000000000000000000000"
|
||||||
|
"#;
|
||||||
|
let dir = std::env::temp_dir();
|
||||||
|
let path = dir.join("telemt_proxy_secret_stable_snapshots_zero_test.toml");
|
||||||
|
std::fs::write(&path, toml).unwrap();
|
||||||
|
let err = ProxyConfig::load(&path).unwrap_err().to_string();
|
||||||
|
assert!(err.contains("general.proxy_secret_stable_snapshots must be > 0"));
|
||||||
|
let _ = std::fs::remove_file(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn proxy_secret_len_max_out_of_range_is_rejected() {
|
||||||
|
let toml = r#"
|
||||||
|
[general]
|
||||||
|
proxy_secret_len_max = 16
|
||||||
|
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "example.com"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
user = "00000000000000000000000000000000"
|
||||||
|
"#;
|
||||||
|
let dir = std::env::temp_dir();
|
||||||
|
let path = dir.join("telemt_proxy_secret_len_max_out_of_range_test.toml");
|
||||||
|
std::fs::write(&path, toml).unwrap();
|
||||||
|
let err = ProxyConfig::load(&path).unwrap_err().to_string();
|
||||||
|
assert!(err.contains("general.proxy_secret_len_max must be within [32, 4096]"));
|
||||||
|
let _ = std::fs::remove_file(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn me_pool_min_fresh_ratio_out_of_range_is_rejected() {
|
||||||
|
let toml = r#"
|
||||||
|
[general]
|
||||||
|
me_pool_min_fresh_ratio = 1.5
|
||||||
|
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "example.com"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
user = "00000000000000000000000000000000"
|
||||||
|
"#;
|
||||||
|
let dir = std::env::temp_dir();
|
||||||
|
let path = dir.join("telemt_me_pool_min_ratio_invalid_test.toml");
|
||||||
|
std::fs::write(&path, toml).unwrap();
|
||||||
|
let err = ProxyConfig::load(&path).unwrap_err().to_string();
|
||||||
|
assert!(err.contains("general.me_pool_min_fresh_ratio must be within [0.0, 1.0]"));
|
||||||
|
let _ = std::fs::remove_file(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn force_close_bumped_when_below_drain_ttl() {
|
||||||
|
let toml = r#"
|
||||||
|
[general]
|
||||||
|
me_pool_drain_ttl_secs = 90
|
||||||
|
me_reinit_drain_timeout_secs = 30
|
||||||
|
|
||||||
|
[censorship]
|
||||||
|
tls_domain = "example.com"
|
||||||
|
|
||||||
|
[access.users]
|
||||||
|
user = "00000000000000000000000000000000"
|
||||||
|
"#;
|
||||||
|
let dir = std::env::temp_dir();
|
||||||
|
let path = dir.join("telemt_force_close_bump_test.toml");
|
||||||
|
std::fs::write(&path, toml).unwrap();
|
||||||
|
let cfg = ProxyConfig::load(&path).unwrap();
|
||||||
|
assert_eq!(cfg.general.me_reinit_drain_timeout_secs, 90);
|
||||||
|
let _ = std::fs::remove_file(path);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ impl Default for ProxyModes {
|
|||||||
Self {
|
Self {
|
||||||
classic: false,
|
classic: false,
|
||||||
secure: false,
|
secure: false,
|
||||||
tls: true,
|
tls: default_true(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -117,12 +117,12 @@ pub struct NetworkConfig {
|
|||||||
impl Default for NetworkConfig {
|
impl Default for NetworkConfig {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
ipv4: true,
|
ipv4: default_true(),
|
||||||
ipv6: Some(false),
|
ipv6: default_network_ipv6(),
|
||||||
prefer: 4,
|
prefer: default_prefer_4(),
|
||||||
multipath: false,
|
multipath: false,
|
||||||
stun_servers: default_stun_servers(),
|
stun_servers: default_stun_servers(),
|
||||||
stun_tcp_fallback: true,
|
stun_tcp_fallback: default_stun_tcp_fallback(),
|
||||||
http_ip_detect_urls: default_http_ip_detect_urls(),
|
http_ip_detect_urls: default_http_ip_detect_urls(),
|
||||||
cache_public_ip_path: default_cache_public_ip_path(),
|
cache_public_ip_path: default_cache_public_ip_path(),
|
||||||
}
|
}
|
||||||
@@ -201,6 +201,32 @@ pub struct GeneralConfig {
|
|||||||
#[serde(default = "default_max_client_frame")]
|
#[serde(default = "default_max_client_frame")]
|
||||||
pub max_client_frame: usize,
|
pub max_client_frame: usize,
|
||||||
|
|
||||||
|
/// Emit full crypto-desync forensic logs for every event.
|
||||||
|
/// When false, full forensic details are emitted once per key window.
|
||||||
|
#[serde(default = "default_desync_all_full")]
|
||||||
|
pub desync_all_full: bool,
|
||||||
|
|
||||||
|
/// Enable per-IP forensic observation buckets for scanners and handshake failures.
|
||||||
|
#[serde(default)]
|
||||||
|
pub beobachten: bool,
|
||||||
|
|
||||||
|
/// Observation retention window in minutes for per-IP forensic buckets.
|
||||||
|
#[serde(default = "default_beobachten_minutes")]
|
||||||
|
pub beobachten_minutes: u64,
|
||||||
|
|
||||||
|
/// Snapshot flush interval in seconds for beob output file.
|
||||||
|
#[serde(default = "default_beobachten_flush_secs")]
|
||||||
|
pub beobachten_flush_secs: u64,
|
||||||
|
|
||||||
|
/// Snapshot file path for beob output.
|
||||||
|
#[serde(default = "default_beobachten_file")]
|
||||||
|
pub beobachten_file: String,
|
||||||
|
|
||||||
|
/// Enable C-like hard-swap for ME pool generations.
|
||||||
|
/// When true, Telemt prewarms a new generation and switches once full coverage is reached.
|
||||||
|
#[serde(default = "default_hardswap")]
|
||||||
|
pub hardswap: bool,
|
||||||
|
|
||||||
/// Enable staggered warmup of extra ME writers.
|
/// Enable staggered warmup of extra ME writers.
|
||||||
#[serde(default = "default_true")]
|
#[serde(default = "default_true")]
|
||||||
pub me_warmup_stagger_enabled: bool,
|
pub me_warmup_stagger_enabled: bool,
|
||||||
@@ -252,11 +278,73 @@ pub struct GeneralConfig {
|
|||||||
#[serde(default = "default_fast_mode_min_tls_record")]
|
#[serde(default = "default_fast_mode_min_tls_record")]
|
||||||
pub fast_mode_min_tls_record: usize,
|
pub fast_mode_min_tls_record: usize,
|
||||||
|
|
||||||
/// Automatically reload proxy-secret every N seconds.
|
/// Unified ME updater interval in seconds for getProxyConfig/getProxyConfigV6/getProxySecret.
|
||||||
|
/// When omitted, effective value falls back to legacy proxy_*_auto_reload_secs fields.
|
||||||
|
#[serde(default)]
|
||||||
|
pub update_every: Option<u64>,
|
||||||
|
|
||||||
|
/// Periodic ME pool reinitialization interval in seconds.
|
||||||
|
#[serde(default = "default_me_reinit_every_secs")]
|
||||||
|
pub me_reinit_every_secs: u64,
|
||||||
|
|
||||||
|
/// Minimum delay in ms between hardswap warmup connect attempts.
|
||||||
|
#[serde(default = "default_me_hardswap_warmup_delay_min_ms")]
|
||||||
|
pub me_hardswap_warmup_delay_min_ms: u64,
|
||||||
|
|
||||||
|
/// Maximum delay in ms between hardswap warmup connect attempts.
|
||||||
|
#[serde(default = "default_me_hardswap_warmup_delay_max_ms")]
|
||||||
|
pub me_hardswap_warmup_delay_max_ms: u64,
|
||||||
|
|
||||||
|
/// Additional warmup passes in the same hardswap cycle after the base pass.
|
||||||
|
#[serde(default = "default_me_hardswap_warmup_extra_passes")]
|
||||||
|
pub me_hardswap_warmup_extra_passes: u8,
|
||||||
|
|
||||||
|
/// Base backoff in ms between hardswap warmup passes when floor is still incomplete.
|
||||||
|
#[serde(default = "default_me_hardswap_warmup_pass_backoff_base_ms")]
|
||||||
|
pub me_hardswap_warmup_pass_backoff_base_ms: u64,
|
||||||
|
|
||||||
|
/// Number of identical getProxyConfig snapshots required before applying ME map updates.
|
||||||
|
#[serde(default = "default_me_config_stable_snapshots")]
|
||||||
|
pub me_config_stable_snapshots: u8,
|
||||||
|
|
||||||
|
/// Cooldown in seconds between applied ME map updates.
|
||||||
|
#[serde(default = "default_me_config_apply_cooldown_secs")]
|
||||||
|
pub me_config_apply_cooldown_secs: u64,
|
||||||
|
|
||||||
|
/// Number of identical getProxySecret snapshots required before runtime secret rotation.
|
||||||
|
#[serde(default = "default_proxy_secret_stable_snapshots")]
|
||||||
|
pub proxy_secret_stable_snapshots: u8,
|
||||||
|
|
||||||
|
/// Enable runtime proxy-secret rotation from getProxySecret.
|
||||||
|
#[serde(default = "default_proxy_secret_rotate_runtime")]
|
||||||
|
pub proxy_secret_rotate_runtime: bool,
|
||||||
|
|
||||||
|
/// Maximum allowed proxy-secret length in bytes for startup and runtime refresh.
|
||||||
|
#[serde(default = "default_proxy_secret_len_max")]
|
||||||
|
pub proxy_secret_len_max: usize,
|
||||||
|
|
||||||
|
/// Drain-TTL in seconds for stale ME writers after endpoint map changes.
|
||||||
|
/// During TTL, stale writers may be used only as fallback for new bindings.
|
||||||
|
#[serde(default = "default_me_pool_drain_ttl_secs")]
|
||||||
|
pub me_pool_drain_ttl_secs: u64,
|
||||||
|
|
||||||
|
/// Minimum desired-DC coverage ratio required before draining stale writers.
|
||||||
|
/// Range: 0.0..=1.0.
|
||||||
|
#[serde(default = "default_me_pool_min_fresh_ratio")]
|
||||||
|
pub me_pool_min_fresh_ratio: f32,
|
||||||
|
|
||||||
|
/// Drain timeout in seconds for stale ME writers after endpoint map changes.
|
||||||
|
/// Set to 0 to keep stale writers draining indefinitely (no force-close).
|
||||||
|
#[serde(default = "default_me_reinit_drain_timeout_secs")]
|
||||||
|
pub me_reinit_drain_timeout_secs: u64,
|
||||||
|
|
||||||
|
/// Deprecated legacy setting; kept for backward compatibility fallback.
|
||||||
|
/// Use `update_every` instead.
|
||||||
#[serde(default = "default_proxy_secret_reload_secs")]
|
#[serde(default = "default_proxy_secret_reload_secs")]
|
||||||
pub proxy_secret_auto_reload_secs: u64,
|
pub proxy_secret_auto_reload_secs: u64,
|
||||||
|
|
||||||
/// Automatically reload proxy-multi.conf every N seconds.
|
/// Deprecated legacy setting; kept for backward compatibility fallback.
|
||||||
|
/// Use `update_every` instead.
|
||||||
#[serde(default = "default_proxy_config_reload_secs")]
|
#[serde(default = "default_proxy_config_reload_secs")]
|
||||||
pub proxy_config_auto_reload_secs: u64,
|
pub proxy_config_auto_reload_secs: u64,
|
||||||
|
|
||||||
@@ -282,27 +370,27 @@ impl Default for GeneralConfig {
|
|||||||
Self {
|
Self {
|
||||||
modes: ProxyModes::default(),
|
modes: ProxyModes::default(),
|
||||||
prefer_ipv6: false,
|
prefer_ipv6: false,
|
||||||
fast_mode: true,
|
fast_mode: default_true(),
|
||||||
use_middle_proxy: false,
|
use_middle_proxy: false,
|
||||||
ad_tag: None,
|
ad_tag: None,
|
||||||
proxy_secret_path: None,
|
proxy_secret_path: None,
|
||||||
middle_proxy_nat_ip: None,
|
middle_proxy_nat_ip: None,
|
||||||
middle_proxy_nat_probe: false,
|
middle_proxy_nat_probe: true,
|
||||||
middle_proxy_nat_stun: None,
|
middle_proxy_nat_stun: None,
|
||||||
middle_proxy_nat_stun_servers: Vec::new(),
|
middle_proxy_nat_stun_servers: Vec::new(),
|
||||||
middle_proxy_pool_size: default_pool_size(),
|
middle_proxy_pool_size: default_pool_size(),
|
||||||
middle_proxy_warm_standby: 8,
|
middle_proxy_warm_standby: default_middle_proxy_warm_standby(),
|
||||||
me_keepalive_enabled: true,
|
me_keepalive_enabled: default_true(),
|
||||||
me_keepalive_interval_secs: default_keepalive_interval(),
|
me_keepalive_interval_secs: default_keepalive_interval(),
|
||||||
me_keepalive_jitter_secs: default_keepalive_jitter(),
|
me_keepalive_jitter_secs: default_keepalive_jitter(),
|
||||||
me_keepalive_payload_random: true,
|
me_keepalive_payload_random: default_true(),
|
||||||
me_warmup_stagger_enabled: true,
|
me_warmup_stagger_enabled: default_true(),
|
||||||
me_warmup_step_delay_ms: default_warmup_step_delay_ms(),
|
me_warmup_step_delay_ms: default_warmup_step_delay_ms(),
|
||||||
me_warmup_step_jitter_ms: default_warmup_step_jitter_ms(),
|
me_warmup_step_jitter_ms: default_warmup_step_jitter_ms(),
|
||||||
me_reconnect_max_concurrent_per_dc: 4,
|
me_reconnect_max_concurrent_per_dc: default_me_reconnect_max_concurrent_per_dc(),
|
||||||
me_reconnect_backoff_base_ms: default_reconnect_backoff_base_ms(),
|
me_reconnect_backoff_base_ms: default_reconnect_backoff_base_ms(),
|
||||||
me_reconnect_backoff_cap_ms: default_reconnect_backoff_cap_ms(),
|
me_reconnect_backoff_cap_ms: default_reconnect_backoff_cap_ms(),
|
||||||
me_reconnect_fast_retry_count: 8,
|
me_reconnect_fast_retry_count: default_me_reconnect_fast_retry_count(),
|
||||||
stun_iface_mismatch_ignore: false,
|
stun_iface_mismatch_ignore: false,
|
||||||
unknown_dc_log_path: default_unknown_dc_log_path(),
|
unknown_dc_log_path: default_unknown_dc_log_path(),
|
||||||
log_level: LogLevel::Normal,
|
log_level: LogLevel::Normal,
|
||||||
@@ -310,17 +398,57 @@ impl Default for GeneralConfig {
|
|||||||
links: LinksConfig::default(),
|
links: LinksConfig::default(),
|
||||||
crypto_pending_buffer: default_crypto_pending_buffer(),
|
crypto_pending_buffer: default_crypto_pending_buffer(),
|
||||||
max_client_frame: default_max_client_frame(),
|
max_client_frame: default_max_client_frame(),
|
||||||
|
desync_all_full: default_desync_all_full(),
|
||||||
|
beobachten: true,
|
||||||
|
beobachten_minutes: default_beobachten_minutes(),
|
||||||
|
beobachten_flush_secs: default_beobachten_flush_secs(),
|
||||||
|
beobachten_file: default_beobachten_file(),
|
||||||
|
hardswap: default_hardswap(),
|
||||||
fast_mode_min_tls_record: default_fast_mode_min_tls_record(),
|
fast_mode_min_tls_record: default_fast_mode_min_tls_record(),
|
||||||
|
update_every: default_update_every(),
|
||||||
|
me_reinit_every_secs: default_me_reinit_every_secs(),
|
||||||
|
me_hardswap_warmup_delay_min_ms: default_me_hardswap_warmup_delay_min_ms(),
|
||||||
|
me_hardswap_warmup_delay_max_ms: default_me_hardswap_warmup_delay_max_ms(),
|
||||||
|
me_hardswap_warmup_extra_passes: default_me_hardswap_warmup_extra_passes(),
|
||||||
|
me_hardswap_warmup_pass_backoff_base_ms: default_me_hardswap_warmup_pass_backoff_base_ms(),
|
||||||
|
me_config_stable_snapshots: default_me_config_stable_snapshots(),
|
||||||
|
me_config_apply_cooldown_secs: default_me_config_apply_cooldown_secs(),
|
||||||
|
proxy_secret_stable_snapshots: default_proxy_secret_stable_snapshots(),
|
||||||
|
proxy_secret_rotate_runtime: default_proxy_secret_rotate_runtime(),
|
||||||
|
proxy_secret_len_max: default_proxy_secret_len_max(),
|
||||||
|
me_pool_drain_ttl_secs: default_me_pool_drain_ttl_secs(),
|
||||||
|
me_pool_min_fresh_ratio: default_me_pool_min_fresh_ratio(),
|
||||||
|
me_reinit_drain_timeout_secs: default_me_reinit_drain_timeout_secs(),
|
||||||
proxy_secret_auto_reload_secs: default_proxy_secret_reload_secs(),
|
proxy_secret_auto_reload_secs: default_proxy_secret_reload_secs(),
|
||||||
proxy_config_auto_reload_secs: default_proxy_config_reload_secs(),
|
proxy_config_auto_reload_secs: default_proxy_config_reload_secs(),
|
||||||
ntp_check: default_ntp_check(),
|
ntp_check: default_ntp_check(),
|
||||||
ntp_servers: default_ntp_servers(),
|
ntp_servers: default_ntp_servers(),
|
||||||
auto_degradation_enabled: true,
|
auto_degradation_enabled: default_true(),
|
||||||
degradation_min_unavailable_dc_groups: default_degradation_min_unavailable_dc_groups(),
|
degradation_min_unavailable_dc_groups: default_degradation_min_unavailable_dc_groups(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl GeneralConfig {
|
||||||
|
/// Resolve the active updater interval for ME infrastructure refresh tasks.
|
||||||
|
/// `update_every` has priority, otherwise legacy proxy_*_auto_reload_secs are used.
|
||||||
|
pub fn effective_update_every_secs(&self) -> u64 {
|
||||||
|
self.update_every
|
||||||
|
.unwrap_or_else(|| self.proxy_secret_auto_reload_secs.min(self.proxy_config_auto_reload_secs))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Resolve periodic zero-downtime reinit interval for ME writers.
|
||||||
|
pub fn effective_me_reinit_every_secs(&self) -> u64 {
|
||||||
|
self.me_reinit_every_secs
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Resolve force-close timeout for stale writers.
|
||||||
|
/// `me_reinit_drain_timeout_secs` remains backward-compatible alias.
|
||||||
|
pub fn effective_me_pool_force_close_secs(&self) -> u64 {
|
||||||
|
self.me_reinit_drain_timeout_secs
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// `[general.links]` — proxy link generation settings.
|
/// `[general.links]` — proxy link generation settings.
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||||
pub struct LinksConfig {
|
pub struct LinksConfig {
|
||||||
@@ -382,7 +510,7 @@ impl Default for ServerConfig {
|
|||||||
Self {
|
Self {
|
||||||
port: default_port(),
|
port: default_port(),
|
||||||
listen_addr_ipv4: Some(default_listen_addr()),
|
listen_addr_ipv4: Some(default_listen_addr()),
|
||||||
listen_addr_ipv6: Some("::".to_string()),
|
listen_addr_ipv6: Some(default_listen_addr_ipv6()),
|
||||||
listen_unix_sock: None,
|
listen_unix_sock: None,
|
||||||
listen_unix_sock_perm: None,
|
listen_unix_sock_perm: None,
|
||||||
listen_tcp: None,
|
listen_tcp: None,
|
||||||
@@ -490,12 +618,12 @@ impl Default for AntiCensorshipConfig {
|
|||||||
Self {
|
Self {
|
||||||
tls_domain: default_tls_domain(),
|
tls_domain: default_tls_domain(),
|
||||||
tls_domains: Vec::new(),
|
tls_domains: Vec::new(),
|
||||||
mask: true,
|
mask: default_true(),
|
||||||
mask_host: None,
|
mask_host: None,
|
||||||
mask_port: default_mask_port(),
|
mask_port: default_mask_port(),
|
||||||
mask_unix_sock: None,
|
mask_unix_sock: None,
|
||||||
fake_cert_len: default_fake_cert_len(),
|
fake_cert_len: default_fake_cert_len(),
|
||||||
tls_emulation: false,
|
tls_emulation: true,
|
||||||
tls_front_dir: default_tls_front_dir(),
|
tls_front_dir: default_tls_front_dir(),
|
||||||
server_hello_delay_min_ms: default_server_hello_delay_min_ms(),
|
server_hello_delay_min_ms: default_server_hello_delay_min_ms(),
|
||||||
server_hello_delay_max_ms: default_server_hello_delay_max_ms(),
|
server_hello_delay_max_ms: default_server_hello_delay_max_ms(),
|
||||||
@@ -535,13 +663,8 @@ pub struct AccessConfig {
|
|||||||
|
|
||||||
impl Default for AccessConfig {
|
impl Default for AccessConfig {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
let mut users = HashMap::new();
|
|
||||||
users.insert(
|
|
||||||
"default".to_string(),
|
|
||||||
"00000000000000000000000000000000".to_string(),
|
|
||||||
);
|
|
||||||
Self {
|
Self {
|
||||||
users,
|
users: default_access_users(),
|
||||||
user_max_tcp_conns: HashMap::new(),
|
user_max_tcp_conns: HashMap::new(),
|
||||||
user_expirations: HashMap::new(),
|
user_expirations: HashMap::new(),
|
||||||
user_data_quota: HashMap::new(),
|
user_data_quota: HashMap::new(),
|
||||||
@@ -624,9 +747,10 @@ pub struct ListenerConfig {
|
|||||||
/// - `show_link = "*"` — show links for all users
|
/// - `show_link = "*"` — show links for all users
|
||||||
/// - `show_link = ["a", "b"]` — show links for specific users
|
/// - `show_link = ["a", "b"]` — show links for specific users
|
||||||
/// - omitted — show no links (default)
|
/// - omitted — show no links (default)
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone, Default)]
|
||||||
pub enum ShowLink {
|
pub enum ShowLink {
|
||||||
/// Don't show any links (default when omitted).
|
/// Don't show any links (default when omitted).
|
||||||
|
#[default]
|
||||||
None,
|
None,
|
||||||
/// Show links for all configured users.
|
/// Show links for all configured users.
|
||||||
All,
|
All,
|
||||||
@@ -634,12 +758,6 @@ pub enum ShowLink {
|
|||||||
Specific(Vec<String>),
|
Specific(Vec<String>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for ShowLink {
|
|
||||||
fn default() -> Self {
|
|
||||||
ShowLink::None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ShowLink {
|
impl ShowLink {
|
||||||
/// Returns true if no links should be shown.
|
/// Returns true if no links should be shown.
|
||||||
pub fn is_empty(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
|
|||||||
@@ -11,6 +11,8 @@
|
|||||||
//! `HandshakeSuccess`, `ObfuscationParams`) are responsible for
|
//! `HandshakeSuccess`, `ObfuscationParams`) are responsible for
|
||||||
//! zeroizing their own copies.
|
//! zeroizing their own copies.
|
||||||
|
|
||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
use aes::Aes256;
|
use aes::Aes256;
|
||||||
use ctr::{Ctr128BE, cipher::{KeyIvInit, StreamCipher}};
|
use ctr::{Ctr128BE, cipher::{KeyIvInit, StreamCipher}};
|
||||||
use zeroize::Zeroize;
|
use zeroize::Zeroize;
|
||||||
@@ -21,13 +23,13 @@ type Aes256Ctr = Ctr128BE<Aes256>;
|
|||||||
// ============= AES-256-CTR =============
|
// ============= AES-256-CTR =============
|
||||||
|
|
||||||
/// AES-256-CTR encryptor/decryptor
|
/// AES-256-CTR encryptor/decryptor
|
||||||
///
|
///
|
||||||
/// CTR mode is symmetric — encryption and decryption are the same operation.
|
/// CTR mode is symmetric — encryption and decryption are the same operation.
|
||||||
///
|
///
|
||||||
/// **Zeroize note:** The inner `Aes256Ctr` cipher state (expanded key schedule
|
/// **Zeroize note:** The inner `Aes256Ctr` cipher state (expanded key schedule
|
||||||
/// + counter) is opaque and cannot be zeroized. If you need to protect key
|
/// + counter) is opaque and cannot be zeroized. If you need to protect key
|
||||||
/// material, zeroize the `[u8; 32]` key and `u128` IV at the call site
|
/// material, zeroize the `[u8; 32]` key and `u128` IV at the call site
|
||||||
/// before dropping them.
|
/// before dropping them.
|
||||||
pub struct AesCtr {
|
pub struct AesCtr {
|
||||||
cipher: Aes256Ctr,
|
cipher: Aes256Ctr,
|
||||||
}
|
}
|
||||||
@@ -147,7 +149,7 @@ impl AesCbc {
|
|||||||
///
|
///
|
||||||
/// CBC Encryption: C[i] = AES_Encrypt(P[i] XOR C[i-1]), where C[-1] = IV
|
/// CBC Encryption: C[i] = AES_Encrypt(P[i] XOR C[i-1]), where C[-1] = IV
|
||||||
pub fn encrypt(&self, data: &[u8]) -> Result<Vec<u8>> {
|
pub fn encrypt(&self, data: &[u8]) -> Result<Vec<u8>> {
|
||||||
if data.len() % Self::BLOCK_SIZE != 0 {
|
if !data.len().is_multiple_of(Self::BLOCK_SIZE) {
|
||||||
return Err(ProxyError::Crypto(
|
return Err(ProxyError::Crypto(
|
||||||
format!("CBC data must be aligned to 16 bytes, got {}", data.len())
|
format!("CBC data must be aligned to 16 bytes, got {}", data.len())
|
||||||
));
|
));
|
||||||
@@ -178,7 +180,7 @@ impl AesCbc {
|
|||||||
///
|
///
|
||||||
/// CBC Decryption: P[i] = AES_Decrypt(C[i]) XOR C[i-1], where C[-1] = IV
|
/// CBC Decryption: P[i] = AES_Decrypt(C[i]) XOR C[i-1], where C[-1] = IV
|
||||||
pub fn decrypt(&self, data: &[u8]) -> Result<Vec<u8>> {
|
pub fn decrypt(&self, data: &[u8]) -> Result<Vec<u8>> {
|
||||||
if data.len() % Self::BLOCK_SIZE != 0 {
|
if !data.len().is_multiple_of(Self::BLOCK_SIZE) {
|
||||||
return Err(ProxyError::Crypto(
|
return Err(ProxyError::Crypto(
|
||||||
format!("CBC data must be aligned to 16 bytes, got {}", data.len())
|
format!("CBC data must be aligned to 16 bytes, got {}", data.len())
|
||||||
));
|
));
|
||||||
@@ -207,7 +209,7 @@ impl AesCbc {
|
|||||||
|
|
||||||
/// Encrypt data in-place
|
/// Encrypt data in-place
|
||||||
pub fn encrypt_in_place(&self, data: &mut [u8]) -> Result<()> {
|
pub fn encrypt_in_place(&self, data: &mut [u8]) -> Result<()> {
|
||||||
if data.len() % Self::BLOCK_SIZE != 0 {
|
if !data.len().is_multiple_of(Self::BLOCK_SIZE) {
|
||||||
return Err(ProxyError::Crypto(
|
return Err(ProxyError::Crypto(
|
||||||
format!("CBC data must be aligned to 16 bytes, got {}", data.len())
|
format!("CBC data must be aligned to 16 bytes, got {}", data.len())
|
||||||
));
|
));
|
||||||
@@ -240,7 +242,7 @@ impl AesCbc {
|
|||||||
|
|
||||||
/// Decrypt data in-place
|
/// Decrypt data in-place
|
||||||
pub fn decrypt_in_place(&self, data: &mut [u8]) -> Result<()> {
|
pub fn decrypt_in_place(&self, data: &mut [u8]) -> Result<()> {
|
||||||
if data.len() % Self::BLOCK_SIZE != 0 {
|
if !data.len().is_multiple_of(Self::BLOCK_SIZE) {
|
||||||
return Err(ProxyError::Crypto(
|
return Err(ProxyError::Crypto(
|
||||||
format!("CBC data must be aligned to 16 bytes, got {}", data.len())
|
format!("CBC data must be aligned to 16 bytes, got {}", data.len())
|
||||||
));
|
));
|
||||||
|
|||||||
@@ -64,6 +64,7 @@ pub fn crc32c(data: &[u8]) -> u32 {
|
|||||||
///
|
///
|
||||||
/// Returned buffer layout (IPv4):
|
/// Returned buffer layout (IPv4):
|
||||||
/// nonce_srv | nonce_clt | clt_ts | srv_ip | clt_port | purpose | clt_ip | srv_port | secret | nonce_srv | [clt_v6 | srv_v6] | nonce_clt
|
/// nonce_srv | nonce_clt | clt_ts | srv_ip | clt_port | purpose | clt_ip | srv_port | secret | nonce_srv | [clt_v6 | srv_v6] | nonce_clt
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
pub fn build_middleproxy_prekey(
|
pub fn build_middleproxy_prekey(
|
||||||
nonce_srv: &[u8; 16],
|
nonce_srv: &[u8; 16],
|
||||||
nonce_clt: &[u8; 16],
|
nonce_clt: &[u8; 16],
|
||||||
@@ -108,6 +109,7 @@ pub fn build_middleproxy_prekey(
|
|||||||
/// Uses MD5 + SHA-1 as mandated by the Telegram Middle Proxy protocol.
|
/// Uses MD5 + SHA-1 as mandated by the Telegram Middle Proxy protocol.
|
||||||
/// These algorithms are NOT replaceable here — changing them would break
|
/// These algorithms are NOT replaceable here — changing them would break
|
||||||
/// interoperability with Telegram's middle proxy infrastructure.
|
/// interoperability with Telegram's middle proxy infrastructure.
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
pub fn derive_middleproxy_keys(
|
pub fn derive_middleproxy_keys(
|
||||||
nonce_srv: &[u8; 16],
|
nonce_srv: &[u8; 16],
|
||||||
nonce_clt: &[u8; 16],
|
nonce_clt: &[u8; 16],
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ pub mod random;
|
|||||||
|
|
||||||
pub use aes::{AesCtr, AesCbc};
|
pub use aes::{AesCtr, AesCbc};
|
||||||
pub use hash::{
|
pub use hash::{
|
||||||
build_middleproxy_prekey, crc32, crc32c, derive_middleproxy_keys, md5, sha1, sha256,
|
build_middleproxy_prekey, crc32, crc32c, derive_middleproxy_keys, sha256, sha256_hmac,
|
||||||
sha256_hmac,
|
|
||||||
};
|
};
|
||||||
pub use random::SecureRandom;
|
pub use random::SecureRandom;
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
//! Pseudorandom
|
//! Pseudorandom
|
||||||
|
|
||||||
|
#![allow(deprecated)]
|
||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
use rand::{Rng, RngCore, SeedableRng};
|
use rand::{Rng, RngCore, SeedableRng};
|
||||||
use rand::rngs::StdRng;
|
use rand::rngs::StdRng;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
@@ -92,7 +95,7 @@ impl SecureRandom {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
let bytes_needed = (k + 7) / 8;
|
let bytes_needed = k.div_ceil(8);
|
||||||
let bytes = self.bytes(bytes_needed.min(8));
|
let bytes = self.bytes(bytes_needed.min(8));
|
||||||
|
|
||||||
let mut result = 0u64;
|
let mut result = 0u64;
|
||||||
|
|||||||
13
src/error.rs
13
src/error.rs
@@ -1,5 +1,7 @@
|
|||||||
//! Error Types
|
//! Error Types
|
||||||
|
|
||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
@@ -89,7 +91,7 @@ impl From<StreamError> for std::io::Error {
|
|||||||
std::io::Error::new(std::io::ErrorKind::UnexpectedEof, err)
|
std::io::Error::new(std::io::ErrorKind::UnexpectedEof, err)
|
||||||
}
|
}
|
||||||
StreamError::Poisoned { .. } => {
|
StreamError::Poisoned { .. } => {
|
||||||
std::io::Error::new(std::io::ErrorKind::Other, err)
|
std::io::Error::other(err)
|
||||||
}
|
}
|
||||||
StreamError::BufferOverflow { .. } => {
|
StreamError::BufferOverflow { .. } => {
|
||||||
std::io::Error::new(std::io::ErrorKind::OutOfMemory, err)
|
std::io::Error::new(std::io::ErrorKind::OutOfMemory, err)
|
||||||
@@ -98,7 +100,7 @@ impl From<StreamError> for std::io::Error {
|
|||||||
std::io::Error::new(std::io::ErrorKind::InvalidData, err)
|
std::io::Error::new(std::io::ErrorKind::InvalidData, err)
|
||||||
}
|
}
|
||||||
StreamError::PartialRead { .. } | StreamError::PartialWrite { .. } => {
|
StreamError::PartialRead { .. } | StreamError::PartialWrite { .. } => {
|
||||||
std::io::Error::new(std::io::ErrorKind::Other, err)
|
std::io::Error::other(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -133,12 +135,7 @@ impl Recoverable for StreamError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn can_continue(&self) -> bool {
|
fn can_continue(&self) -> bool {
|
||||||
match self {
|
!matches!(self, Self::Poisoned { .. } | Self::UnexpectedEof | Self::BufferOverflow { .. })
|
||||||
Self::Poisoned { .. } => false,
|
|
||||||
Self::UnexpectedEof => false,
|
|
||||||
Self::BufferOverflow { .. } => false,
|
|
||||||
_ => true,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
// src/ip_tracker.rs
|
// src/ip_tracker.rs
|
||||||
// Модуль для отслеживания и ограничения уникальных IP-адресов пользователей
|
// IP address tracking and limiting for users
|
||||||
|
|
||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
use std::net::IpAddr;
|
use std::net::IpAddr;
|
||||||
|
|||||||
174
src/main.rs
174
src/main.rs
@@ -1,5 +1,7 @@
|
|||||||
//! telemt — Telegram MTProto Proxy
|
//! telemt — Telegram MTProto Proxy
|
||||||
|
|
||||||
|
#![allow(unused_assignments)]
|
||||||
|
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
@@ -33,6 +35,7 @@ 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::network::probe::{decide_network_capabilities, log_probe_result, run_probe};
|
||||||
use crate::proxy::ClientHandler;
|
use crate::proxy::ClientHandler;
|
||||||
|
use crate::stats::beobachten::BeobachtenStore;
|
||||||
use crate::stats::{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::{
|
||||||
@@ -157,6 +160,15 @@ fn print_proxy_links(host: &str, port: u16, config: &ProxyConfig) {
|
|||||||
info!(target: "telemt::links", "------------------------");
|
info!(target: "telemt::links", "------------------------");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn write_beobachten_snapshot(path: &str, payload: &str) -> std::io::Result<()> {
|
||||||
|
if let Some(parent) = std::path::Path::new(path).parent()
|
||||||
|
&& !parent.as_os_str().is_empty()
|
||||||
|
{
|
||||||
|
tokio::fs::create_dir_all(parent).await?;
|
||||||
|
}
|
||||||
|
tokio::fs::write(path, payload).await
|
||||||
|
}
|
||||||
|
|
||||||
#[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();
|
||||||
@@ -191,14 +203,14 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let (filter_layer, filter_handle) = reload::Layer::new(EnvFilter::new("info"));
|
let (filter_layer, filter_handle) = reload::Layer::new(EnvFilter::new("info"));
|
||||||
|
|
||||||
// Configure color output based on config
|
// Configure color output based on config
|
||||||
let fmt_layer = if config.general.disable_colors {
|
let fmt_layer = if config.general.disable_colors {
|
||||||
fmt::Layer::default().with_ansi(false)
|
fmt::Layer::default().with_ansi(false)
|
||||||
} else {
|
} else {
|
||||||
fmt::Layer::default().with_ansi(true)
|
fmt::Layer::default().with_ansi(true)
|
||||||
};
|
};
|
||||||
|
|
||||||
tracing_subscriber::registry()
|
tracing_subscriber::registry()
|
||||||
.with(filter_layer)
|
.with(filter_layer)
|
||||||
.with(fmt_layer)
|
.with(fmt_layer)
|
||||||
@@ -254,6 +266,7 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
|
|||||||
let prefer_ipv6 = decision.prefer_ipv6();
|
let prefer_ipv6 = decision.prefer_ipv6();
|
||||||
let mut use_middle_proxy = config.general.use_middle_proxy && (decision.ipv4_me || decision.ipv6_me);
|
let mut use_middle_proxy = config.general.use_middle_proxy && (decision.ipv4_me || decision.ipv6_me);
|
||||||
let stats = Arc::new(Stats::new());
|
let stats = Arc::new(Stats::new());
|
||||||
|
let beobachten = Arc::new(BeobachtenStore::new());
|
||||||
let rng = Arc::new(SecureRandom::new());
|
let rng = Arc::new(SecureRandom::new());
|
||||||
|
|
||||||
// IP Tracker initialization
|
// IP Tracker initialization
|
||||||
@@ -296,25 +309,30 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
|
|||||||
// proxy-secret is from: https://core.telegram.org/getProxySecret
|
// proxy-secret is from: https://core.telegram.org/getProxySecret
|
||||||
// =============================================================
|
// =============================================================
|
||||||
let proxy_secret_path = config.general.proxy_secret_path.as_deref();
|
let proxy_secret_path = config.general.proxy_secret_path.as_deref();
|
||||||
match crate::transport::middle_proxy::fetch_proxy_secret(proxy_secret_path).await {
|
match crate::transport::middle_proxy::fetch_proxy_secret(
|
||||||
Ok(proxy_secret) => {
|
proxy_secret_path,
|
||||||
info!(
|
config.general.proxy_secret_len_max,
|
||||||
secret_len = proxy_secret.len() as usize, // ← ЯВНЫЙ ТИП usize
|
)
|
||||||
key_sig = format_args!(
|
.await
|
||||||
"0x{:08x}",
|
{
|
||||||
if proxy_secret.len() >= 4 {
|
Ok(proxy_secret) => {
|
||||||
u32::from_le_bytes([
|
info!(
|
||||||
proxy_secret[0],
|
secret_len = proxy_secret.len(),
|
||||||
proxy_secret[1],
|
key_sig = format_args!(
|
||||||
proxy_secret[2],
|
"0x{:08x}",
|
||||||
proxy_secret[3],
|
if proxy_secret.len() >= 4 {
|
||||||
])
|
u32::from_le_bytes([
|
||||||
} else {
|
proxy_secret[0],
|
||||||
0
|
proxy_secret[1],
|
||||||
}
|
proxy_secret[2],
|
||||||
),
|
proxy_secret[3],
|
||||||
"Proxy-secret loaded"
|
])
|
||||||
);
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
),
|
||||||
|
"Proxy-secret loaded"
|
||||||
|
);
|
||||||
|
|
||||||
// Load ME config (v4/v6) + default DC
|
// Load ME config (v4/v6) + default DC
|
||||||
let mut cfg_v4 = fetch_proxy_config(
|
let mut cfg_v4 = fetch_proxy_config(
|
||||||
@@ -362,6 +380,14 @@ match crate::transport::middle_proxy::fetch_proxy_secret(proxy_secret_path).awai
|
|||||||
config.general.me_reconnect_backoff_base_ms,
|
config.general.me_reconnect_backoff_base_ms,
|
||||||
config.general.me_reconnect_backoff_cap_ms,
|
config.general.me_reconnect_backoff_cap_ms,
|
||||||
config.general.me_reconnect_fast_retry_count,
|
config.general.me_reconnect_fast_retry_count,
|
||||||
|
config.general.hardswap,
|
||||||
|
config.general.me_pool_drain_ttl_secs,
|
||||||
|
config.general.effective_me_pool_force_close_secs(),
|
||||||
|
config.general.me_pool_min_fresh_ratio,
|
||||||
|
config.general.me_hardswap_warmup_delay_min_ms,
|
||||||
|
config.general.me_hardswap_warmup_delay_max_ms,
|
||||||
|
config.general.me_hardswap_warmup_extra_passes,
|
||||||
|
config.general.me_hardswap_warmup_pass_backoff_base_ms,
|
||||||
);
|
);
|
||||||
|
|
||||||
let pool_size = config.general.middle_proxy_pool_size.max(1);
|
let pool_size = config.general.middle_proxy_pool_size.max(1);
|
||||||
@@ -380,30 +406,6 @@ match crate::transport::middle_proxy::fetch_proxy_secret(proxy_secret_path).awai
|
|||||||
.await;
|
.await;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Periodic ME connection rotation
|
|
||||||
let pool_clone_rot = pool.clone();
|
|
||||||
let rng_clone_rot = rng.clone();
|
|
||||||
tokio::spawn(async move {
|
|
||||||
crate::transport::middle_proxy::me_rotation_task(
|
|
||||||
pool_clone_rot,
|
|
||||||
rng_clone_rot,
|
|
||||||
std::time::Duration::from_secs(1800),
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Periodic updater: getProxyConfig + proxy-secret
|
|
||||||
let pool_clone2 = pool.clone();
|
|
||||||
let rng_clone2 = rng.clone();
|
|
||||||
tokio::spawn(async move {
|
|
||||||
crate::transport::middle_proxy::me_config_updater(
|
|
||||||
pool_clone2,
|
|
||||||
rng_clone2,
|
|
||||||
std::time::Duration::from_secs(12 * 3600),
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
});
|
|
||||||
|
|
||||||
Some(pool)
|
Some(pool)
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
@@ -425,6 +427,7 @@ match crate::transport::middle_proxy::fetch_proxy_secret(proxy_secret_path).awai
|
|||||||
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 {
|
||||||
|
let _ = use_middle_proxy;
|
||||||
use_middle_proxy = false;
|
use_middle_proxy = false;
|
||||||
// Make runtime config reflect direct-only mode for handlers.
|
// Make runtime config reflect direct-only mode for handlers.
|
||||||
config.general.use_middle_proxy = false;
|
config.general.use_middle_proxy = false;
|
||||||
@@ -602,14 +605,12 @@ match crate::transport::middle_proxy::fetch_proxy_secret(proxy_secret_path).awai
|
|||||||
} else {
|
} else {
|
||||||
info!(" IPv4 in use / IPv6 is fallback");
|
info!(" IPv4 in use / IPv6 is fallback");
|
||||||
}
|
}
|
||||||
} else {
|
} else if v6_works && !v4_works {
|
||||||
if v6_works && !v4_works {
|
info!(" IPv6 only / IPv4 unavailable");
|
||||||
info!(" IPv6 only / IPv4 unavailable)");
|
} else if v4_works && !v6_works {
|
||||||
} else if v4_works && !v6_works {
|
info!(" IPv4 only / IPv6 unavailable");
|
||||||
info!(" IPv4 only / IPv6 unavailable)");
|
} else if !v6_works && !v4_works {
|
||||||
} else if !v6_works && !v4_works {
|
info!(" No DC connectivity");
|
||||||
info!(" No DC connectivity");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
info!(" via {}", upstream_result.upstream_name);
|
info!(" via {}", upstream_result.upstream_name);
|
||||||
@@ -676,14 +677,8 @@ 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_v4: Option<std::net::IpAddr> = probe
|
let detected_ip_v4: Option<std::net::IpAddr> = probe.detected_ipv4.map(std::net::IpAddr::V4);
|
||||||
.reflected_ipv4
|
let detected_ip_v6: Option<std::net::IpAddr> = probe.detected_ipv6.map(std::net::IpAddr::V6);
|
||||||
.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_v4, detected_ip_v6
|
detected_ip_v4, detected_ip_v6
|
||||||
@@ -702,6 +697,52 @@ match crate::transport::middle_proxy::fetch_proxy_secret(proxy_secret_path).awai
|
|||||||
detected_ip_v6,
|
detected_ip_v6,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let beobachten_writer = beobachten.clone();
|
||||||
|
let config_rx_beobachten = config_rx.clone();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
loop {
|
||||||
|
let cfg = config_rx_beobachten.borrow().clone();
|
||||||
|
let sleep_secs = cfg.general.beobachten_flush_secs.max(1);
|
||||||
|
|
||||||
|
if cfg.general.beobachten {
|
||||||
|
let ttl = Duration::from_secs(cfg.general.beobachten_minutes.saturating_mul(60));
|
||||||
|
let path = cfg.general.beobachten_file.clone();
|
||||||
|
let snapshot = beobachten_writer.snapshot_text(ttl);
|
||||||
|
if let Err(e) = write_beobachten_snapshot(&path, &snapshot).await {
|
||||||
|
warn!(error = %e, path = %path, "Failed to flush beobachten snapshot");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tokio::time::sleep(Duration::from_secs(sleep_secs)).await;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if let Some(ref pool) = me_pool {
|
||||||
|
let pool_clone = pool.clone();
|
||||||
|
let rng_clone = rng.clone();
|
||||||
|
let config_rx_clone = config_rx.clone();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
crate::transport::middle_proxy::me_config_updater(
|
||||||
|
pool_clone,
|
||||||
|
rng_clone,
|
||||||
|
config_rx_clone,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
});
|
||||||
|
|
||||||
|
let pool_clone_rot = pool.clone();
|
||||||
|
let rng_clone_rot = rng.clone();
|
||||||
|
let config_rx_clone_rot = config_rx.clone();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
crate::transport::middle_proxy::me_rotation_task(
|
||||||
|
pool_clone_rot,
|
||||||
|
rng_clone_rot,
|
||||||
|
config_rx_clone_rot,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
let mut listeners = Vec::new();
|
let mut listeners = Vec::new();
|
||||||
|
|
||||||
for listener_conf in &config.server.listeners {
|
for listener_conf in &config.server.listeners {
|
||||||
@@ -844,6 +885,7 @@ match crate::transport::middle_proxy::fetch_proxy_secret(proxy_secret_path).awai
|
|||||||
let me_pool = me_pool.clone();
|
let me_pool = me_pool.clone();
|
||||||
let tls_cache = tls_cache.clone();
|
let tls_cache = tls_cache.clone();
|
||||||
let ip_tracker = ip_tracker.clone();
|
let ip_tracker = ip_tracker.clone();
|
||||||
|
let beobachten = beobachten.clone();
|
||||||
let max_connections_unix = max_connections.clone();
|
let max_connections_unix = max_connections.clone();
|
||||||
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
@@ -871,6 +913,7 @@ match crate::transport::middle_proxy::fetch_proxy_secret(proxy_secret_path).awai
|
|||||||
let me_pool = me_pool.clone();
|
let me_pool = me_pool.clone();
|
||||||
let tls_cache = tls_cache.clone();
|
let tls_cache = tls_cache.clone();
|
||||||
let ip_tracker = ip_tracker.clone();
|
let ip_tracker = ip_tracker.clone();
|
||||||
|
let beobachten = beobachten.clone();
|
||||||
let proxy_protocol_enabled = config.server.proxy_protocol;
|
let proxy_protocol_enabled = config.server.proxy_protocol;
|
||||||
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
@@ -878,7 +921,7 @@ match crate::transport::middle_proxy::fetch_proxy_secret(proxy_secret_path).awai
|
|||||||
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, tls_cache, ip_tracker, proxy_protocol_enabled,
|
me_pool, tls_cache, ip_tracker, beobachten, proxy_protocol_enabled,
|
||||||
).await {
|
).await {
|
||||||
debug!(error = %e, "Unix socket connection error");
|
debug!(error = %e, "Unix socket connection error");
|
||||||
}
|
}
|
||||||
@@ -926,9 +969,11 @@ match crate::transport::middle_proxy::fetch_proxy_secret(proxy_secret_path).awai
|
|||||||
|
|
||||||
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 beobachten = beobachten.clone();
|
||||||
|
let config_rx_metrics = config_rx.clone();
|
||||||
let whitelist = config.server.metrics_whitelist.clone();
|
let whitelist = config.server.metrics_whitelist.clone();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
metrics::serve(port, stats, whitelist).await;
|
metrics::serve(port, stats, beobachten, config_rx_metrics, whitelist).await;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -942,6 +987,7 @@ match crate::transport::middle_proxy::fetch_proxy_secret(proxy_secret_path).awai
|
|||||||
let me_pool = me_pool.clone();
|
let me_pool = me_pool.clone();
|
||||||
let tls_cache = tls_cache.clone();
|
let tls_cache = tls_cache.clone();
|
||||||
let ip_tracker = ip_tracker.clone();
|
let ip_tracker = ip_tracker.clone();
|
||||||
|
let beobachten = beobachten.clone();
|
||||||
let max_connections_tcp = max_connections.clone();
|
let max_connections_tcp = max_connections.clone();
|
||||||
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
@@ -964,6 +1010,7 @@ match crate::transport::middle_proxy::fetch_proxy_secret(proxy_secret_path).awai
|
|||||||
let me_pool = me_pool.clone();
|
let me_pool = me_pool.clone();
|
||||||
let tls_cache = tls_cache.clone();
|
let tls_cache = tls_cache.clone();
|
||||||
let ip_tracker = ip_tracker.clone();
|
let ip_tracker = ip_tracker.clone();
|
||||||
|
let beobachten = beobachten.clone();
|
||||||
let proxy_protocol_enabled = listener_proxy_protocol;
|
let proxy_protocol_enabled = listener_proxy_protocol;
|
||||||
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
@@ -980,6 +1027,7 @@ match crate::transport::middle_proxy::fetch_proxy_secret(proxy_secret_path).awai
|
|||||||
me_pool,
|
me_pool,
|
||||||
tls_cache,
|
tls_cache,
|
||||||
ip_tracker,
|
ip_tracker,
|
||||||
|
beobachten,
|
||||||
proxy_protocol_enabled,
|
proxy_protocol_enabled,
|
||||||
)
|
)
|
||||||
.run()
|
.run()
|
||||||
|
|||||||
239
src/metrics.rs
239
src/metrics.rs
@@ -1,8 +1,9 @@
|
|||||||
use std::convert::Infallible;
|
use std::convert::Infallible;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
use http_body_util::{Full, BodyExt};
|
use http_body_util::Full;
|
||||||
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;
|
||||||
@@ -11,9 +12,17 @@ use ipnetwork::IpNetwork;
|
|||||||
use tokio::net::TcpListener;
|
use tokio::net::TcpListener;
|
||||||
use tracing::{info, warn, debug};
|
use tracing::{info, warn, debug};
|
||||||
|
|
||||||
|
use crate::config::ProxyConfig;
|
||||||
|
use crate::stats::beobachten::BeobachtenStore;
|
||||||
use crate::stats::Stats;
|
use crate::stats::Stats;
|
||||||
|
|
||||||
pub async fn serve(port: u16, stats: Arc<Stats>, whitelist: Vec<IpNetwork>) {
|
pub async fn serve(
|
||||||
|
port: u16,
|
||||||
|
stats: Arc<Stats>,
|
||||||
|
beobachten: Arc<BeobachtenStore>,
|
||||||
|
config_rx: tokio::sync::watch::Receiver<Arc<ProxyConfig>>,
|
||||||
|
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,
|
||||||
@@ -22,7 +31,7 @@ pub async fn serve(port: u16, stats: Arc<Stats>, whitelist: Vec<IpNetwork>) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
info!("Metrics endpoint: http://{}/metrics", addr);
|
info!("Metrics endpoint: http://{}/metrics and /beobachten", addr);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let (stream, peer) = match listener.accept().await {
|
let (stream, peer) = match listener.accept().await {
|
||||||
@@ -39,10 +48,14 @@ pub async fn serve(port: u16, stats: Arc<Stats>, whitelist: Vec<IpNetwork>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let stats = stats.clone();
|
let stats = stats.clone();
|
||||||
|
let beobachten = beobachten.clone();
|
||||||
|
let config_rx_conn = config_rx.clone();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
let svc = service_fn(move |req| {
|
let svc = service_fn(move |req| {
|
||||||
let stats = stats.clone();
|
let stats = stats.clone();
|
||||||
async move { handle(req, &stats) }
|
let beobachten = beobachten.clone();
|
||||||
|
let config = config_rx_conn.borrow().clone();
|
||||||
|
async move { handle(req, &stats, &beobachten, &config) }
|
||||||
});
|
});
|
||||||
if let Err(e) = http1::Builder::new()
|
if let Err(e) = http1::Builder::new()
|
||||||
.serve_connection(hyper_util::rt::TokioIo::new(stream), svc)
|
.serve_connection(hyper_util::rt::TokioIo::new(stream), svc)
|
||||||
@@ -54,24 +67,48 @@ pub async fn serve(port: u16, stats: Arc<Stats>, whitelist: Vec<IpNetwork>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle<B>(req: Request<B>, stats: &Stats) -> Result<Response<Full<Bytes>>, Infallible> {
|
fn handle<B>(
|
||||||
if req.uri().path() != "/metrics" {
|
req: Request<B>,
|
||||||
|
stats: &Stats,
|
||||||
|
beobachten: &BeobachtenStore,
|
||||||
|
config: &ProxyConfig,
|
||||||
|
) -> Result<Response<Full<Bytes>>, Infallible> {
|
||||||
|
if req.uri().path() == "/metrics" {
|
||||||
|
let body = render_metrics(stats);
|
||||||
let resp = Response::builder()
|
let resp = Response::builder()
|
||||||
.status(StatusCode::NOT_FOUND)
|
.status(StatusCode::OK)
|
||||||
.body(Full::new(Bytes::from("Not Found\n")))
|
.header("content-type", "text/plain; version=0.0.4; charset=utf-8")
|
||||||
|
.body(Full::new(Bytes::from(body)))
|
||||||
|
.unwrap();
|
||||||
|
return Ok(resp);
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.uri().path() == "/beobachten" {
|
||||||
|
let body = render_beobachten(beobachten, config);
|
||||||
|
let resp = Response::builder()
|
||||||
|
.status(StatusCode::OK)
|
||||||
|
.header("content-type", "text/plain; charset=utf-8")
|
||||||
|
.body(Full::new(Bytes::from(body)))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
return Ok(resp);
|
return Ok(resp);
|
||||||
}
|
}
|
||||||
|
|
||||||
let body = render_metrics(stats);
|
|
||||||
let resp = Response::builder()
|
let resp = Response::builder()
|
||||||
.status(StatusCode::OK)
|
.status(StatusCode::NOT_FOUND)
|
||||||
.header("content-type", "text/plain; version=0.0.4; charset=utf-8")
|
.body(Full::new(Bytes::from("Not Found\n")))
|
||||||
.body(Full::new(Bytes::from(body)))
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
Ok(resp)
|
Ok(resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn render_beobachten(beobachten: &BeobachtenStore, config: &ProxyConfig) -> String {
|
||||||
|
if !config.general.beobachten {
|
||||||
|
return "beobachten disabled\n".to_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
let ttl = Duration::from_secs(config.general.beobachten_minutes.saturating_mul(60));
|
||||||
|
beobachten.snapshot_text(ttl)
|
||||||
|
}
|
||||||
|
|
||||||
fn render_metrics(stats: &Stats) -> String {
|
fn render_metrics(stats: &Stats) -> String {
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
let mut out = String::with_capacity(4096);
|
let mut out = String::with_capacity(4096);
|
||||||
@@ -140,6 +177,154 @@ fn render_metrics(stats: &Stats) -> String {
|
|||||||
let _ = writeln!(out, "# TYPE telemt_secure_padding_invalid_total counter");
|
let _ = writeln!(out, "# TYPE telemt_secure_padding_invalid_total counter");
|
||||||
let _ = writeln!(out, "telemt_secure_padding_invalid_total {}", stats.get_secure_padding_invalid());
|
let _ = writeln!(out, "telemt_secure_padding_invalid_total {}", stats.get_secure_padding_invalid());
|
||||||
|
|
||||||
|
let _ = writeln!(out, "# HELP telemt_desync_total Total crypto-desync detections");
|
||||||
|
let _ = writeln!(out, "# TYPE telemt_desync_total counter");
|
||||||
|
let _ = writeln!(out, "telemt_desync_total {}", stats.get_desync_total());
|
||||||
|
|
||||||
|
let _ = writeln!(out, "# HELP telemt_desync_full_logged_total Full forensic desync logs emitted");
|
||||||
|
let _ = writeln!(out, "# TYPE telemt_desync_full_logged_total counter");
|
||||||
|
let _ = writeln!(out, "telemt_desync_full_logged_total {}", stats.get_desync_full_logged());
|
||||||
|
|
||||||
|
let _ = writeln!(out, "# HELP telemt_desync_suppressed_total Suppressed desync forensic events");
|
||||||
|
let _ = writeln!(out, "# TYPE telemt_desync_suppressed_total counter");
|
||||||
|
let _ = writeln!(out, "telemt_desync_suppressed_total {}", stats.get_desync_suppressed());
|
||||||
|
|
||||||
|
let _ = writeln!(out, "# HELP telemt_desync_frames_bucket_total Desync count by frames_ok bucket");
|
||||||
|
let _ = writeln!(out, "# TYPE telemt_desync_frames_bucket_total counter");
|
||||||
|
let _ = writeln!(
|
||||||
|
out,
|
||||||
|
"telemt_desync_frames_bucket_total{{bucket=\"0\"}} {}",
|
||||||
|
stats.get_desync_frames_bucket_0()
|
||||||
|
);
|
||||||
|
let _ = writeln!(
|
||||||
|
out,
|
||||||
|
"telemt_desync_frames_bucket_total{{bucket=\"1_2\"}} {}",
|
||||||
|
stats.get_desync_frames_bucket_1_2()
|
||||||
|
);
|
||||||
|
let _ = writeln!(
|
||||||
|
out,
|
||||||
|
"telemt_desync_frames_bucket_total{{bucket=\"3_10\"}} {}",
|
||||||
|
stats.get_desync_frames_bucket_3_10()
|
||||||
|
);
|
||||||
|
let _ = writeln!(
|
||||||
|
out,
|
||||||
|
"telemt_desync_frames_bucket_total{{bucket=\"gt_10\"}} {}",
|
||||||
|
stats.get_desync_frames_bucket_gt_10()
|
||||||
|
);
|
||||||
|
|
||||||
|
let _ = writeln!(out, "# HELP telemt_pool_swap_total Successful ME pool swaps");
|
||||||
|
let _ = writeln!(out, "# TYPE telemt_pool_swap_total counter");
|
||||||
|
let _ = writeln!(out, "telemt_pool_swap_total {}", stats.get_pool_swap_total());
|
||||||
|
|
||||||
|
let _ = writeln!(out, "# HELP telemt_pool_drain_active Active draining ME writers");
|
||||||
|
let _ = writeln!(out, "# TYPE telemt_pool_drain_active gauge");
|
||||||
|
let _ = writeln!(out, "telemt_pool_drain_active {}", stats.get_pool_drain_active());
|
||||||
|
|
||||||
|
let _ = writeln!(out, "# HELP telemt_pool_force_close_total Forced close events for draining writers");
|
||||||
|
let _ = writeln!(out, "# TYPE telemt_pool_force_close_total counter");
|
||||||
|
let _ = writeln!(
|
||||||
|
out,
|
||||||
|
"telemt_pool_force_close_total {}",
|
||||||
|
stats.get_pool_force_close_total()
|
||||||
|
);
|
||||||
|
|
||||||
|
let _ = writeln!(out, "# HELP telemt_pool_stale_pick_total Stale writer fallback picks for new binds");
|
||||||
|
let _ = writeln!(out, "# TYPE telemt_pool_stale_pick_total counter");
|
||||||
|
let _ = writeln!(
|
||||||
|
out,
|
||||||
|
"telemt_pool_stale_pick_total {}",
|
||||||
|
stats.get_pool_stale_pick_total()
|
||||||
|
);
|
||||||
|
|
||||||
|
let _ = writeln!(out, "# HELP telemt_me_writer_removed_total Total ME writer removals");
|
||||||
|
let _ = writeln!(out, "# TYPE telemt_me_writer_removed_total counter");
|
||||||
|
let _ = writeln!(
|
||||||
|
out,
|
||||||
|
"telemt_me_writer_removed_total {}",
|
||||||
|
stats.get_me_writer_removed_total()
|
||||||
|
);
|
||||||
|
|
||||||
|
let _ = writeln!(
|
||||||
|
out,
|
||||||
|
"# HELP telemt_me_writer_removed_unexpected_total Unexpected ME writer removals that triggered refill"
|
||||||
|
);
|
||||||
|
let _ = writeln!(out, "# TYPE telemt_me_writer_removed_unexpected_total counter");
|
||||||
|
let _ = writeln!(
|
||||||
|
out,
|
||||||
|
"telemt_me_writer_removed_unexpected_total {}",
|
||||||
|
stats.get_me_writer_removed_unexpected_total()
|
||||||
|
);
|
||||||
|
|
||||||
|
let _ = writeln!(out, "# HELP telemt_me_refill_triggered_total Immediate ME refill runs started");
|
||||||
|
let _ = writeln!(out, "# TYPE telemt_me_refill_triggered_total counter");
|
||||||
|
let _ = writeln!(
|
||||||
|
out,
|
||||||
|
"telemt_me_refill_triggered_total {}",
|
||||||
|
stats.get_me_refill_triggered_total()
|
||||||
|
);
|
||||||
|
|
||||||
|
let _ = writeln!(
|
||||||
|
out,
|
||||||
|
"# HELP telemt_me_refill_skipped_inflight_total Immediate ME refill skips due to inflight dedup"
|
||||||
|
);
|
||||||
|
let _ = writeln!(out, "# TYPE telemt_me_refill_skipped_inflight_total counter");
|
||||||
|
let _ = writeln!(
|
||||||
|
out,
|
||||||
|
"telemt_me_refill_skipped_inflight_total {}",
|
||||||
|
stats.get_me_refill_skipped_inflight_total()
|
||||||
|
);
|
||||||
|
|
||||||
|
let _ = writeln!(out, "# HELP telemt_me_refill_failed_total Immediate ME refill failures");
|
||||||
|
let _ = writeln!(out, "# TYPE telemt_me_refill_failed_total counter");
|
||||||
|
let _ = writeln!(
|
||||||
|
out,
|
||||||
|
"telemt_me_refill_failed_total {}",
|
||||||
|
stats.get_me_refill_failed_total()
|
||||||
|
);
|
||||||
|
|
||||||
|
let _ = writeln!(
|
||||||
|
out,
|
||||||
|
"# HELP telemt_me_writer_restored_same_endpoint_total Refilled ME writer restored on the same endpoint"
|
||||||
|
);
|
||||||
|
let _ = writeln!(out, "# TYPE telemt_me_writer_restored_same_endpoint_total counter");
|
||||||
|
let _ = writeln!(
|
||||||
|
out,
|
||||||
|
"telemt_me_writer_restored_same_endpoint_total {}",
|
||||||
|
stats.get_me_writer_restored_same_endpoint_total()
|
||||||
|
);
|
||||||
|
|
||||||
|
let _ = writeln!(
|
||||||
|
out,
|
||||||
|
"# HELP telemt_me_writer_restored_fallback_total Refilled ME writer restored via fallback endpoint"
|
||||||
|
);
|
||||||
|
let _ = writeln!(out, "# TYPE telemt_me_writer_restored_fallback_total counter");
|
||||||
|
let _ = writeln!(
|
||||||
|
out,
|
||||||
|
"telemt_me_writer_restored_fallback_total {}",
|
||||||
|
stats.get_me_writer_restored_fallback_total()
|
||||||
|
);
|
||||||
|
|
||||||
|
let unresolved_writer_losses = stats
|
||||||
|
.get_me_writer_removed_unexpected_total()
|
||||||
|
.saturating_sub(
|
||||||
|
stats
|
||||||
|
.get_me_writer_restored_same_endpoint_total()
|
||||||
|
.saturating_add(stats.get_me_writer_restored_fallback_total()),
|
||||||
|
);
|
||||||
|
let _ = writeln!(
|
||||||
|
out,
|
||||||
|
"# HELP telemt_me_writer_removed_unexpected_minus_restored_total Unexpected writer removals not yet compensated by restore"
|
||||||
|
);
|
||||||
|
let _ = writeln!(
|
||||||
|
out,
|
||||||
|
"# TYPE telemt_me_writer_removed_unexpected_minus_restored_total gauge"
|
||||||
|
);
|
||||||
|
let _ = writeln!(
|
||||||
|
out,
|
||||||
|
"telemt_me_writer_removed_unexpected_minus_restored_total {}",
|
||||||
|
unresolved_writer_losses
|
||||||
|
);
|
||||||
|
|
||||||
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");
|
||||||
@@ -170,6 +355,8 @@ fn render_metrics(stats: &Stats) -> String {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use std::net::IpAddr;
|
||||||
|
use http_body_util::BodyExt;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_render_metrics_format() {
|
fn test_render_metrics_format() {
|
||||||
@@ -217,11 +404,17 @@ mod tests {
|
|||||||
assert!(output.contains("# TYPE telemt_connections_total counter"));
|
assert!(output.contains("# TYPE telemt_connections_total counter"));
|
||||||
assert!(output.contains("# TYPE telemt_connections_bad_total counter"));
|
assert!(output.contains("# TYPE telemt_connections_bad_total counter"));
|
||||||
assert!(output.contains("# TYPE telemt_handshake_timeouts_total counter"));
|
assert!(output.contains("# TYPE telemt_handshake_timeouts_total counter"));
|
||||||
|
assert!(output.contains("# TYPE telemt_me_writer_removed_total counter"));
|
||||||
|
assert!(output.contains(
|
||||||
|
"# TYPE telemt_me_writer_removed_unexpected_minus_restored_total gauge"
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_endpoint_integration() {
|
async fn test_endpoint_integration() {
|
||||||
let stats = Arc::new(Stats::new());
|
let stats = Arc::new(Stats::new());
|
||||||
|
let beobachten = Arc::new(BeobachtenStore::new());
|
||||||
|
let mut config = ProxyConfig::default();
|
||||||
stats.increment_connects_all();
|
stats.increment_connects_all();
|
||||||
stats.increment_connects_all();
|
stats.increment_connects_all();
|
||||||
stats.increment_connects_all();
|
stats.increment_connects_all();
|
||||||
@@ -230,16 +423,34 @@ mod tests {
|
|||||||
.uri("/metrics")
|
.uri("/metrics")
|
||||||
.body(())
|
.body(())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let resp = handle(req, &stats).unwrap();
|
let resp = handle(req, &stats, &beobachten, &config).unwrap();
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
let body = resp.into_body().collect().await.unwrap().to_bytes();
|
let body = resp.into_body().collect().await.unwrap().to_bytes();
|
||||||
assert!(std::str::from_utf8(body.as_ref()).unwrap().contains("telemt_connections_total 3"));
|
assert!(std::str::from_utf8(body.as_ref()).unwrap().contains("telemt_connections_total 3"));
|
||||||
|
|
||||||
|
config.general.beobachten = true;
|
||||||
|
config.general.beobachten_minutes = 10;
|
||||||
|
beobachten.record(
|
||||||
|
"TLS-scanner",
|
||||||
|
"203.0.113.10".parse::<IpAddr>().unwrap(),
|
||||||
|
Duration::from_secs(600),
|
||||||
|
);
|
||||||
|
let req_beob = Request::builder()
|
||||||
|
.uri("/beobachten")
|
||||||
|
.body(())
|
||||||
|
.unwrap();
|
||||||
|
let resp_beob = handle(req_beob, &stats, &beobachten, &config).unwrap();
|
||||||
|
assert_eq!(resp_beob.status(), StatusCode::OK);
|
||||||
|
let body_beob = resp_beob.into_body().collect().await.unwrap().to_bytes();
|
||||||
|
let beob_text = std::str::from_utf8(body_beob.as_ref()).unwrap();
|
||||||
|
assert!(beob_text.contains("[TLS-scanner]"));
|
||||||
|
assert!(beob_text.contains("203.0.113.10-1"));
|
||||||
|
|
||||||
let req404 = Request::builder()
|
let req404 = Request::builder()
|
||||||
.uri("/other")
|
.uri("/other")
|
||||||
.body(())
|
.body(())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let resp404 = handle(req404, &stats).unwrap();
|
let resp404 = handle(req404, &stats, &beobachten, &config).unwrap();
|
||||||
assert_eq!(resp404.status(), StatusCode::NOT_FOUND);
|
assert_eq!(resp404.status(), StatusCode::NOT_FOUND);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, UdpSocket};
|
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, UdpSocket};
|
||||||
|
|
||||||
use tracing::{info, warn};
|
use tracing::{info, warn};
|
||||||
@@ -93,23 +95,21 @@ pub async fn run_probe(config: &NetworkConfig, stun_addr: Option<String>, nat_pr
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn decide_network_capabilities(config: &NetworkConfig, probe: &NetworkProbe) -> NetworkDecision {
|
pub fn decide_network_capabilities(config: &NetworkConfig, probe: &NetworkProbe) -> NetworkDecision {
|
||||||
let mut decision = NetworkDecision::default();
|
let ipv4_dc = config.ipv4 && probe.detected_ipv4.is_some();
|
||||||
|
let ipv6_dc = config.ipv6.unwrap_or(probe.detected_ipv6.is_some()) && probe.detected_ipv6.is_some();
|
||||||
|
|
||||||
decision.ipv4_dc = config.ipv4 && probe.detected_ipv4.is_some();
|
let ipv4_me = config.ipv4
|
||||||
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.detected_ipv4.is_some()
|
||||||
&& (!probe.ipv4_is_bogon || probe.reflected_ipv4.is_some());
|
&& (!probe.ipv4_is_bogon || probe.reflected_ipv4.is_some());
|
||||||
|
|
||||||
let ipv6_enabled = config.ipv6.unwrap_or(probe.detected_ipv6.is_some());
|
let ipv6_enabled = config.ipv6.unwrap_or(probe.detected_ipv6.is_some());
|
||||||
decision.ipv6_me = ipv6_enabled
|
let ipv6_me = ipv6_enabled
|
||||||
&& probe.detected_ipv6.is_some()
|
&& probe.detected_ipv6.is_some()
|
||||||
&& (!probe.ipv6_is_bogon || probe.reflected_ipv6.is_some());
|
&& (!probe.ipv6_is_bogon || probe.reflected_ipv6.is_some());
|
||||||
|
|
||||||
decision.effective_prefer = match config.prefer {
|
let effective_prefer = match config.prefer {
|
||||||
6 if decision.ipv6_me || decision.ipv6_dc => 6,
|
6 if ipv6_me || ipv6_dc => 6,
|
||||||
4 if decision.ipv4_me || decision.ipv4_dc => 4,
|
4 if ipv4_me || ipv4_dc => 4,
|
||||||
6 => {
|
6 => {
|
||||||
warn!("prefer=6 requested but IPv6 unavailable; falling back to IPv4");
|
warn!("prefer=6 requested but IPv6 unavailable; falling back to IPv4");
|
||||||
4
|
4
|
||||||
@@ -117,10 +117,17 @@ pub fn decide_network_capabilities(config: &NetworkConfig, probe: &NetworkProbe)
|
|||||||
_ => 4,
|
_ => 4,
|
||||||
};
|
};
|
||||||
|
|
||||||
let me_families = decision.ipv4_me as u8 + decision.ipv6_me as u8;
|
let me_families = ipv4_me as u8 + ipv6_me as u8;
|
||||||
decision.effective_multipath = config.multipath && me_families >= 2;
|
let effective_multipath = config.multipath && me_families >= 2;
|
||||||
|
|
||||||
decision
|
NetworkDecision {
|
||||||
|
ipv4_dc,
|
||||||
|
ipv6_dc,
|
||||||
|
ipv4_me,
|
||||||
|
ipv6_me,
|
||||||
|
effective_prefer,
|
||||||
|
effective_multipath,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn detect_local_ip_v4() -> Option<Ipv4Addr> {
|
fn detect_local_ip_v4() -> Option<Ipv4Addr> {
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
#![allow(unreachable_code)]
|
||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
|
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
|
||||||
|
|
||||||
use tokio::net::{lookup_host, UdpSocket};
|
use tokio::net::{lookup_host, UdpSocket};
|
||||||
@@ -195,16 +198,11 @@ async fn resolve_stun_addr(stun_addr: &str, family: IpFamily) -> Result<Option<S
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let addrs = lookup_host(stun_addr)
|
let mut addrs = lookup_host(stun_addr)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| ProxyError::Proxy(format!("STUN resolve failed: {e}")))?;
|
.map_err(|e| ProxyError::Proxy(format!("STUN resolve failed: {e}")))?;
|
||||||
|
|
||||||
let target = addrs
|
let target = addrs
|
||||||
.filter(|a| match (a.is_ipv4(), family) {
|
.find(|a| matches!((a.is_ipv4(), family), (true, IpFamily::V4) | (false, IpFamily::V6)));
|
||||||
(true, IpFamily::V4) => true,
|
|
||||||
(false, IpFamily::V6) => true,
|
|
||||||
_ => false,
|
|
||||||
})
|
|
||||||
.next();
|
|
||||||
Ok(target)
|
Ok(target)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
//! Protocol constants and datacenter addresses
|
//! Protocol constants and datacenter addresses
|
||||||
|
|
||||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
|
#![allow(dead_code)]
|
||||||
|
|
||||||
|
use std::net::{IpAddr, Ipv4Addr};
|
||||||
|
|
||||||
use crate::crypto::SecureRandom;
|
use crate::crypto::SecureRandom;
|
||||||
use std::sync::LazyLock;
|
use std::sync::LazyLock;
|
||||||
@@ -158,7 +160,7 @@ pub const MAX_TLS_CHUNK_SIZE: usize = 16384 + 256;
|
|||||||
|
|
||||||
/// Secure Intermediate payload is expected to be 4-byte aligned.
|
/// Secure Intermediate payload is expected to be 4-byte aligned.
|
||||||
pub fn is_valid_secure_payload_len(data_len: usize) -> bool {
|
pub fn is_valid_secure_payload_len(data_len: usize) -> bool {
|
||||||
data_len % 4 == 0
|
data_len.is_multiple_of(4)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Compute Secure Intermediate payload length from wire length.
|
/// Compute Secure Intermediate payload length from wire length.
|
||||||
@@ -177,7 +179,7 @@ pub fn secure_padding_len(data_len: usize, rng: &SecureRandom) -> usize {
|
|||||||
is_valid_secure_payload_len(data_len),
|
is_valid_secure_payload_len(data_len),
|
||||||
"Secure payload must be 4-byte aligned, got {data_len}"
|
"Secure payload must be 4-byte aligned, got {data_len}"
|
||||||
);
|
);
|
||||||
(rng.range(3) + 1) as usize
|
rng.range(3) + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============= Timeouts =============
|
// ============= Timeouts =============
|
||||||
@@ -229,7 +231,6 @@ pub static RESERVED_NONCE_CONTINUES: &[[u8; 4]] = &[
|
|||||||
// ============= RPC Constants (for Middle Proxy) =============
|
// ============= RPC Constants (for Middle Proxy) =============
|
||||||
|
|
||||||
/// RPC Proxy Request
|
/// RPC Proxy Request
|
||||||
|
|
||||||
/// RPC Flags (from Erlang mtp_rpc.erl)
|
/// RPC Flags (from Erlang mtp_rpc.erl)
|
||||||
pub const RPC_FLAG_NOT_ENCRYPTED: u32 = 0x2;
|
pub const RPC_FLAG_NOT_ENCRYPTED: u32 = 0x2;
|
||||||
pub const RPC_FLAG_HAS_AD_TAG: u32 = 0x8;
|
pub const RPC_FLAG_HAS_AD_TAG: u32 = 0x8;
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
//! MTProto frame types and metadata
|
//! MTProto frame types and metadata
|
||||||
|
|
||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
/// Extra metadata associated with a frame
|
/// Extra metadata associated with a frame
|
||||||
@@ -83,7 +85,7 @@ impl FrameMode {
|
|||||||
pub fn validate_message_length(len: usize) -> bool {
|
pub fn validate_message_length(len: usize) -> bool {
|
||||||
use super::constants::{MIN_MSG_LEN, MAX_MSG_LEN, PADDING_FILLER};
|
use super::constants::{MIN_MSG_LEN, MAX_MSG_LEN, PADDING_FILLER};
|
||||||
|
|
||||||
len >= MIN_MSG_LEN && len <= MAX_MSG_LEN && len % PADDING_FILLER.len() == 0
|
(MIN_MSG_LEN..=MAX_MSG_LEN).contains(&len) && len.is_multiple_of(PADDING_FILLER.len())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
@@ -5,7 +5,11 @@ pub mod frame;
|
|||||||
pub mod obfuscation;
|
pub mod obfuscation;
|
||||||
pub mod tls;
|
pub mod tls;
|
||||||
|
|
||||||
|
#[allow(unused_imports)]
|
||||||
pub use constants::*;
|
pub use constants::*;
|
||||||
|
#[allow(unused_imports)]
|
||||||
pub use frame::*;
|
pub use frame::*;
|
||||||
|
#[allow(unused_imports)]
|
||||||
pub use obfuscation::*;
|
pub use obfuscation::*;
|
||||||
|
#[allow(unused_imports)]
|
||||||
pub use tls::*;
|
pub use tls::*;
|
||||||
@@ -1,8 +1,9 @@
|
|||||||
//! MTProto Obfuscation
|
//! MTProto Obfuscation
|
||||||
|
|
||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
use zeroize::Zeroize;
|
use zeroize::Zeroize;
|
||||||
use crate::crypto::{sha256, AesCtr};
|
use crate::crypto::{sha256, AesCtr};
|
||||||
use crate::error::Result;
|
|
||||||
use super::constants::*;
|
use super::constants::*;
|
||||||
|
|
||||||
/// Obfuscation parameters from handshake
|
/// Obfuscation parameters from handshake
|
||||||
|
|||||||
@@ -4,8 +4,11 @@
|
|||||||
//! for domain fronting. The handshake looks like valid TLS 1.3 but
|
//! for domain fronting. The handshake looks like valid TLS 1.3 but
|
||||||
//! actually carries MTProto authentication data.
|
//! actually carries MTProto authentication data.
|
||||||
|
|
||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
use crate::crypto::{sha256_hmac, SecureRandom};
|
use crate::crypto::{sha256_hmac, SecureRandom};
|
||||||
use crate::error::{ProxyError, Result};
|
#[cfg(test)]
|
||||||
|
use crate::error::ProxyError;
|
||||||
use super::constants::*;
|
use super::constants::*;
|
||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
use num_bigint::BigUint;
|
use num_bigint::BigUint;
|
||||||
@@ -332,7 +335,7 @@ pub fn validate_tls_handshake(
|
|||||||
// This is a quirk in some clients that use uptime instead of real time
|
// This is a quirk in some clients that use uptime instead of real time
|
||||||
let is_boot_time = timestamp < 60 * 60 * 24 * 1000; // < ~2.7 years in seconds
|
let is_boot_time = timestamp < 60 * 60 * 24 * 1000; // < ~2.7 years in seconds
|
||||||
|
|
||||||
if !is_boot_time && (time_diff < TIME_SKEW_MIN || time_diff > TIME_SKEW_MAX) {
|
if !is_boot_time && !(TIME_SKEW_MIN..=TIME_SKEW_MAX).contains(&time_diff) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -390,7 +393,7 @@ pub fn build_server_hello(
|
|||||||
) -> Vec<u8> {
|
) -> Vec<u8> {
|
||||||
const MIN_APP_DATA: usize = 64;
|
const MIN_APP_DATA: usize = 64;
|
||||||
const MAX_APP_DATA: usize = 16640; // RFC 8446 §5.2 upper bound
|
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 fake_cert_len = fake_cert_len.clamp(MIN_APP_DATA, MAX_APP_DATA);
|
||||||
let x25519_key = gen_fake_x25519_key(rng);
|
let x25519_key = gen_fake_x25519_key(rng);
|
||||||
|
|
||||||
// Build ServerHello
|
// Build ServerHello
|
||||||
@@ -522,10 +525,10 @@ pub fn extract_sni_from_client_hello(handshake: &[u8]) -> Option<String> {
|
|||||||
if sn_pos + name_len > sn_end {
|
if sn_pos + name_len > sn_end {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if name_type == 0 && name_len > 0 {
|
if name_type == 0 && name_len > 0
|
||||||
if let Ok(host) = std::str::from_utf8(&handshake[sn_pos..sn_pos + name_len]) {
|
&& let Ok(host) = std::str::from_utf8(&handshake[sn_pos..sn_pos + name_len])
|
||||||
return Some(host.to_string());
|
{
|
||||||
}
|
return Some(host.to_string());
|
||||||
}
|
}
|
||||||
sn_pos += name_len;
|
sn_pos += name_len;
|
||||||
}
|
}
|
||||||
@@ -568,7 +571,7 @@ pub fn extract_alpn_from_client_hello(handshake: &[u8]) -> Vec<Vec<u8>> {
|
|||||||
let list_len = u16::from_be_bytes([handshake[pos], handshake[pos+1]]) as usize;
|
let list_len = u16::from_be_bytes([handshake[pos], handshake[pos+1]]) as usize;
|
||||||
let mut lp = pos + 2;
|
let mut lp = pos + 2;
|
||||||
let list_end = (pos + 2).saturating_add(list_len).min(pos + elen);
|
let list_end = (pos + 2).saturating_add(list_len).min(pos + elen);
|
||||||
while lp + 1 <= list_end {
|
while lp < list_end {
|
||||||
let plen = handshake[lp] as usize;
|
let plen = handshake[lp] as usize;
|
||||||
lp += 1;
|
lp += 1;
|
||||||
if lp + plen > list_end { break; }
|
if lp + plen > list_end { break; }
|
||||||
@@ -613,7 +616,7 @@ pub fn parse_tls_record_header(header: &[u8; 5]) -> Option<(u8, u16)> {
|
|||||||
///
|
///
|
||||||
/// This is useful for testing that our ServerHello is well-formed.
|
/// This is useful for testing that our ServerHello is well-formed.
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
fn validate_server_hello_structure(data: &[u8]) -> Result<()> {
|
fn validate_server_hello_structure(data: &[u8]) -> Result<(), ProxyError> {
|
||||||
if data.len() < 5 {
|
if data.len() < 5 {
|
||||||
return Err(ProxyError::InvalidTlsRecord {
|
return Err(ProxyError::InvalidTlsRecord {
|
||||||
record_type: 0,
|
record_type: 0,
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//! Client Handler
|
//! Client Handler
|
||||||
|
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
use std::net::SocketAddr;
|
use std::net::{IpAddr, SocketAddr};
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
@@ -27,6 +27,7 @@ use crate::error::{HandshakeResult, ProxyError, Result};
|
|||||||
use crate::ip_tracker::UserIpTracker;
|
use crate::ip_tracker::UserIpTracker;
|
||||||
use crate::protocol::constants::*;
|
use crate::protocol::constants::*;
|
||||||
use crate::protocol::tls;
|
use crate::protocol::tls;
|
||||||
|
use crate::stats::beobachten::BeobachtenStore;
|
||||||
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;
|
||||||
@@ -39,6 +40,36 @@ use crate::proxy::handshake::{HandshakeSuccess, handle_mtproto_handshake, handle
|
|||||||
use crate::proxy::masking::handle_bad_client;
|
use crate::proxy::masking::handle_bad_client;
|
||||||
use crate::proxy::middle_relay::handle_via_middle_proxy;
|
use crate::proxy::middle_relay::handle_via_middle_proxy;
|
||||||
|
|
||||||
|
fn beobachten_ttl(config: &ProxyConfig) -> Duration {
|
||||||
|
Duration::from_secs(config.general.beobachten_minutes.saturating_mul(60))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn record_beobachten_class(
|
||||||
|
beobachten: &BeobachtenStore,
|
||||||
|
config: &ProxyConfig,
|
||||||
|
peer_ip: IpAddr,
|
||||||
|
class: &str,
|
||||||
|
) {
|
||||||
|
if !config.general.beobachten {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
beobachten.record(class, peer_ip, beobachten_ttl(config));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn record_handshake_failure_class(
|
||||||
|
beobachten: &BeobachtenStore,
|
||||||
|
config: &ProxyConfig,
|
||||||
|
peer_ip: IpAddr,
|
||||||
|
error: &ProxyError,
|
||||||
|
) {
|
||||||
|
let class = if error.to_string().contains("expected 64 bytes, got 0") {
|
||||||
|
"expected_64_got_0"
|
||||||
|
} else {
|
||||||
|
"other"
|
||||||
|
};
|
||||||
|
record_beobachten_class(beobachten, config, peer_ip, class);
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn handle_client_stream<S>(
|
pub async fn handle_client_stream<S>(
|
||||||
mut stream: S,
|
mut stream: S,
|
||||||
peer: SocketAddr,
|
peer: SocketAddr,
|
||||||
@@ -51,6 +82,7 @@ pub async fn handle_client_stream<S>(
|
|||||||
me_pool: Option<Arc<MePool>>,
|
me_pool: Option<Arc<MePool>>,
|
||||||
tls_cache: Option<Arc<TlsFrontCache>>,
|
tls_cache: Option<Arc<TlsFrontCache>>,
|
||||||
ip_tracker: Arc<UserIpTracker>,
|
ip_tracker: Arc<UserIpTracker>,
|
||||||
|
beobachten: Arc<BeobachtenStore>,
|
||||||
proxy_protocol_enabled: bool,
|
proxy_protocol_enabled: bool,
|
||||||
) -> Result<()>
|
) -> Result<()>
|
||||||
where
|
where
|
||||||
@@ -73,6 +105,7 @@ where
|
|||||||
Err(e) => {
|
Err(e) => {
|
||||||
stats.increment_connects_bad();
|
stats.increment_connects_bad();
|
||||||
warn!(peer = %peer, error = %e, "Invalid PROXY protocol header");
|
warn!(peer = %peer, error = %e, "Invalid PROXY protocol header");
|
||||||
|
record_beobachten_class(&beobachten, &config, peer.ip(), "other");
|
||||||
return Err(e);
|
return Err(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -82,6 +115,9 @@ where
|
|||||||
|
|
||||||
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();
|
||||||
|
let config_for_timeout = config.clone();
|
||||||
|
let beobachten_for_timeout = beobachten.clone();
|
||||||
|
let peer_for_timeout = real_peer.ip();
|
||||||
|
|
||||||
// For non-TCP streams, use a synthetic local address
|
// For non-TCP streams, use a synthetic local address
|
||||||
let local_addr: SocketAddr = format!("0.0.0.0:{}", config.server.port)
|
let local_addr: SocketAddr = format!("0.0.0.0:{}", config.server.port)
|
||||||
@@ -103,7 +139,15 @@ where
|
|||||||
debug!(peer = %real_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,
|
||||||
|
real_peer.ip(),
|
||||||
|
&config,
|
||||||
|
&beobachten,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
return Ok(HandshakeOutcome::Handled);
|
return Ok(HandshakeOutcome::Handled);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,7 +164,15 @@ where
|
|||||||
HandshakeResult::Success(result) => result,
|
HandshakeResult::Success(result) => result,
|
||||||
HandshakeResult::BadClient { reader, writer } => {
|
HandshakeResult::BadClient { reader, writer } => {
|
||||||
stats.increment_connects_bad();
|
stats.increment_connects_bad();
|
||||||
handle_bad_client(reader, writer, &handshake, &config).await;
|
handle_bad_client(
|
||||||
|
reader,
|
||||||
|
writer,
|
||||||
|
&handshake,
|
||||||
|
real_peer.ip(),
|
||||||
|
&config,
|
||||||
|
&beobachten,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
return Ok(HandshakeOutcome::Handled);
|
return Ok(HandshakeOutcome::Handled);
|
||||||
}
|
}
|
||||||
HandshakeResult::Error(e) => return Err(e),
|
HandshakeResult::Error(e) => return Err(e),
|
||||||
@@ -156,7 +208,15 @@ where
|
|||||||
debug!(peer = %real_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,
|
||||||
|
real_peer.ip(),
|
||||||
|
&config,
|
||||||
|
&beobachten,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
return Ok(HandshakeOutcome::Handled);
|
return Ok(HandshakeOutcome::Handled);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,7 +233,15 @@ where
|
|||||||
HandshakeResult::Success(result) => result,
|
HandshakeResult::Success(result) => result,
|
||||||
HandshakeResult::BadClient { reader, writer } => {
|
HandshakeResult::BadClient { reader, writer } => {
|
||||||
stats.increment_connects_bad();
|
stats.increment_connects_bad();
|
||||||
handle_bad_client(reader, writer, &handshake, &config).await;
|
handle_bad_client(
|
||||||
|
reader,
|
||||||
|
writer,
|
||||||
|
&handshake,
|
||||||
|
real_peer.ip(),
|
||||||
|
&config,
|
||||||
|
&beobachten,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
return Ok(HandshakeOutcome::Handled);
|
return Ok(HandshakeOutcome::Handled);
|
||||||
}
|
}
|
||||||
HandshakeResult::Error(e) => return Err(e),
|
HandshakeResult::Error(e) => return Err(e),
|
||||||
@@ -200,11 +268,23 @@ where
|
|||||||
Ok(Ok(outcome)) => outcome,
|
Ok(Ok(outcome)) => outcome,
|
||||||
Ok(Err(e)) => {
|
Ok(Err(e)) => {
|
||||||
debug!(peer = %peer, error = %e, "Handshake failed");
|
debug!(peer = %peer, error = %e, "Handshake failed");
|
||||||
|
record_handshake_failure_class(
|
||||||
|
&beobachten_for_timeout,
|
||||||
|
&config_for_timeout,
|
||||||
|
peer_for_timeout,
|
||||||
|
&e,
|
||||||
|
);
|
||||||
return Err(e);
|
return Err(e);
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
stats_for_timeout.increment_handshake_timeouts();
|
stats_for_timeout.increment_handshake_timeouts();
|
||||||
debug!(peer = %peer, "Handshake timeout");
|
debug!(peer = %peer, "Handshake timeout");
|
||||||
|
record_beobachten_class(
|
||||||
|
&beobachten_for_timeout,
|
||||||
|
&config_for_timeout,
|
||||||
|
peer_for_timeout,
|
||||||
|
"other",
|
||||||
|
);
|
||||||
return Err(ProxyError::TgHandshakeTimeout);
|
return Err(ProxyError::TgHandshakeTimeout);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -230,6 +310,7 @@ pub struct RunningClientHandler {
|
|||||||
me_pool: Option<Arc<MePool>>,
|
me_pool: Option<Arc<MePool>>,
|
||||||
tls_cache: Option<Arc<TlsFrontCache>>,
|
tls_cache: Option<Arc<TlsFrontCache>>,
|
||||||
ip_tracker: Arc<UserIpTracker>,
|
ip_tracker: Arc<UserIpTracker>,
|
||||||
|
beobachten: Arc<BeobachtenStore>,
|
||||||
proxy_protocol_enabled: bool,
|
proxy_protocol_enabled: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -246,6 +327,7 @@ impl ClientHandler {
|
|||||||
me_pool: Option<Arc<MePool>>,
|
me_pool: Option<Arc<MePool>>,
|
||||||
tls_cache: Option<Arc<TlsFrontCache>>,
|
tls_cache: Option<Arc<TlsFrontCache>>,
|
||||||
ip_tracker: Arc<UserIpTracker>,
|
ip_tracker: Arc<UserIpTracker>,
|
||||||
|
beobachten: Arc<BeobachtenStore>,
|
||||||
proxy_protocol_enabled: bool,
|
proxy_protocol_enabled: bool,
|
||||||
) -> RunningClientHandler {
|
) -> RunningClientHandler {
|
||||||
RunningClientHandler {
|
RunningClientHandler {
|
||||||
@@ -260,6 +342,7 @@ impl ClientHandler {
|
|||||||
me_pool,
|
me_pool,
|
||||||
tls_cache,
|
tls_cache,
|
||||||
ip_tracker,
|
ip_tracker,
|
||||||
|
beobachten,
|
||||||
proxy_protocol_enabled,
|
proxy_protocol_enabled,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -271,7 +354,7 @@ impl RunningClientHandler {
|
|||||||
|
|
||||||
self.peer = normalize_ip(self.peer);
|
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");
|
||||||
|
|
||||||
if let Err(e) = configure_client_socket(
|
if let Err(e) = configure_client_socket(
|
||||||
@@ -284,17 +367,32 @@ impl RunningClientHandler {
|
|||||||
|
|
||||||
let handshake_timeout = Duration::from_secs(self.config.timeouts.client_handshake);
|
let handshake_timeout = Duration::from_secs(self.config.timeouts.client_handshake);
|
||||||
let stats = self.stats.clone();
|
let stats = self.stats.clone();
|
||||||
|
let config_for_timeout = self.config.clone();
|
||||||
|
let beobachten_for_timeout = self.beobachten.clone();
|
||||||
|
let peer_for_timeout = peer.ip();
|
||||||
|
|
||||||
// Phase 1: handshake (with timeout)
|
// Phase 1: handshake (with timeout)
|
||||||
let outcome = match timeout(handshake_timeout, self.do_handshake()).await {
|
let outcome = match timeout(handshake_timeout, self.do_handshake()).await {
|
||||||
Ok(Ok(outcome)) => outcome,
|
Ok(Ok(outcome)) => outcome,
|
||||||
Ok(Err(e)) => {
|
Ok(Err(e)) => {
|
||||||
debug!(peer = %peer, error = %e, "Handshake failed");
|
debug!(peer = %peer, error = %e, "Handshake failed");
|
||||||
|
record_handshake_failure_class(
|
||||||
|
&beobachten_for_timeout,
|
||||||
|
&config_for_timeout,
|
||||||
|
peer_for_timeout,
|
||||||
|
&e,
|
||||||
|
);
|
||||||
return Err(e);
|
return Err(e);
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
stats.increment_handshake_timeouts();
|
stats.increment_handshake_timeouts();
|
||||||
debug!(peer = %peer, "Handshake timeout");
|
debug!(peer = %peer, "Handshake timeout");
|
||||||
|
record_beobachten_class(
|
||||||
|
&beobachten_for_timeout,
|
||||||
|
&config_for_timeout,
|
||||||
|
peer_for_timeout,
|
||||||
|
"other",
|
||||||
|
);
|
||||||
return Err(ProxyError::TgHandshakeTimeout);
|
return Err(ProxyError::TgHandshakeTimeout);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -321,6 +419,12 @@ impl RunningClientHandler {
|
|||||||
Err(e) => {
|
Err(e) => {
|
||||||
self.stats.increment_connects_bad();
|
self.stats.increment_connects_bad();
|
||||||
warn!(peer = %self.peer, error = %e, "Invalid PROXY protocol header");
|
warn!(peer = %self.peer, error = %e, "Invalid PROXY protocol header");
|
||||||
|
record_beobachten_class(
|
||||||
|
&self.beobachten,
|
||||||
|
&self.config,
|
||||||
|
self.peer.ip(),
|
||||||
|
"other",
|
||||||
|
);
|
||||||
return Err(e);
|
return Err(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -331,7 +435,7 @@ impl RunningClientHandler {
|
|||||||
|
|
||||||
let is_tls = tls::is_tls_handshake(&first_bytes[..3]);
|
let is_tls = tls::is_tls_handshake(&first_bytes[..3]);
|
||||||
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, is_tls = is_tls, "Handshake type detected");
|
debug!(peer = %peer, is_tls = is_tls, "Handshake type detected");
|
||||||
|
|
||||||
@@ -344,7 +448,7 @@ impl RunningClientHandler {
|
|||||||
|
|
||||||
async fn handle_tls_client(mut self, first_bytes: [u8; 5]) -> Result<HandshakeOutcome> {
|
async fn handle_tls_client(mut self, first_bytes: [u8; 5]) -> Result<HandshakeOutcome> {
|
||||||
let peer = self.peer;
|
let peer = self.peer;
|
||||||
let ip_tracker = self.ip_tracker.clone();
|
let _ip_tracker = self.ip_tracker.clone();
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
@@ -354,7 +458,15 @@ impl RunningClientHandler {
|
|||||||
debug!(peer = %peer, tls_len = tls_len, "TLS handshake too short");
|
debug!(peer = %peer, tls_len = tls_len, "TLS handshake too short");
|
||||||
self.stats.increment_connects_bad();
|
self.stats.increment_connects_bad();
|
||||||
let (reader, writer) = self.stream.into_split();
|
let (reader, writer) = self.stream.into_split();
|
||||||
handle_bad_client(reader, writer, &first_bytes, &self.config).await;
|
handle_bad_client(
|
||||||
|
reader,
|
||||||
|
writer,
|
||||||
|
&first_bytes,
|
||||||
|
peer.ip(),
|
||||||
|
&self.config,
|
||||||
|
&self.beobachten,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
return Ok(HandshakeOutcome::Handled);
|
return Ok(HandshakeOutcome::Handled);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -385,7 +497,15 @@ impl RunningClientHandler {
|
|||||||
HandshakeResult::Success(result) => result,
|
HandshakeResult::Success(result) => result,
|
||||||
HandshakeResult::BadClient { reader, writer } => {
|
HandshakeResult::BadClient { reader, writer } => {
|
||||||
stats.increment_connects_bad();
|
stats.increment_connects_bad();
|
||||||
handle_bad_client(reader, writer, &handshake, &config).await;
|
handle_bad_client(
|
||||||
|
reader,
|
||||||
|
writer,
|
||||||
|
&handshake,
|
||||||
|
peer.ip(),
|
||||||
|
&config,
|
||||||
|
&self.beobachten,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
return Ok(HandshakeOutcome::Handled);
|
return Ok(HandshakeOutcome::Handled);
|
||||||
}
|
}
|
||||||
HandshakeResult::Error(e) => return Err(e),
|
HandshakeResult::Error(e) => return Err(e),
|
||||||
@@ -440,13 +560,21 @@ impl RunningClientHandler {
|
|||||||
|
|
||||||
async fn handle_direct_client(mut self, first_bytes: [u8; 5]) -> Result<HandshakeOutcome> {
|
async fn handle_direct_client(mut self, first_bytes: [u8; 5]) -> Result<HandshakeOutcome> {
|
||||||
let peer = self.peer;
|
let peer = self.peer;
|
||||||
let ip_tracker = self.ip_tracker.clone();
|
let _ip_tracker = self.ip_tracker.clone();
|
||||||
|
|
||||||
if !self.config.general.modes.classic && !self.config.general.modes.secure {
|
if !self.config.general.modes.classic && !self.config.general.modes.secure {
|
||||||
debug!(peer = %peer, "Non-TLS modes disabled");
|
debug!(peer = %peer, "Non-TLS modes disabled");
|
||||||
self.stats.increment_connects_bad();
|
self.stats.increment_connects_bad();
|
||||||
let (reader, writer) = self.stream.into_split();
|
let (reader, writer) = self.stream.into_split();
|
||||||
handle_bad_client(reader, writer, &first_bytes, &self.config).await;
|
handle_bad_client(
|
||||||
|
reader,
|
||||||
|
writer,
|
||||||
|
&first_bytes,
|
||||||
|
peer.ip(),
|
||||||
|
&self.config,
|
||||||
|
&self.beobachten,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
return Ok(HandshakeOutcome::Handled);
|
return Ok(HandshakeOutcome::Handled);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -476,7 +604,15 @@ impl RunningClientHandler {
|
|||||||
HandshakeResult::Success(result) => result,
|
HandshakeResult::Success(result) => result,
|
||||||
HandshakeResult::BadClient { reader, writer } => {
|
HandshakeResult::BadClient { reader, writer } => {
|
||||||
stats.increment_connects_bad();
|
stats.increment_connects_bad();
|
||||||
handle_bad_client(reader, writer, &handshake, &config).await;
|
handle_bad_client(
|
||||||
|
reader,
|
||||||
|
writer,
|
||||||
|
&handshake,
|
||||||
|
peer.ip(),
|
||||||
|
&config,
|
||||||
|
&self.beobachten,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
return Ok(HandshakeOutcome::Handled);
|
return Ok(HandshakeOutcome::Handled);
|
||||||
}
|
}
|
||||||
HandshakeResult::Error(e) => return Err(e),
|
HandshakeResult::Error(e) => return Err(e),
|
||||||
@@ -594,18 +730,18 @@ impl RunningClientHandler {
|
|||||||
peer_addr: SocketAddr,
|
peer_addr: SocketAddr,
|
||||||
ip_tracker: &UserIpTracker,
|
ip_tracker: &UserIpTracker,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
if let Some(expiration) = config.access.user_expirations.get(user) {
|
if let Some(expiration) = config.access.user_expirations.get(user)
|
||||||
if chrono::Utc::now() > *expiration {
|
&& chrono::Utc::now() > *expiration
|
||||||
return Err(ProxyError::UserExpired {
|
{
|
||||||
user: user.to_string(),
|
return Err(ProxyError::UserExpired {
|
||||||
});
|
user: user.to_string(),
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// IP limit check
|
// IP limit check
|
||||||
if let Err(reason) = ip_tracker.check_and_add(user, peer_addr.ip()).await {
|
if let Err(reason) = ip_tracker.check_and_add(user, peer_addr.ip()).await {
|
||||||
warn!(
|
warn!(
|
||||||
user = %user,
|
user = %user,
|
||||||
ip = %peer_addr.ip(),
|
ip = %peer_addr.ip(),
|
||||||
reason = %reason,
|
reason = %reason,
|
||||||
"IP limit exceeded"
|
"IP limit exceeded"
|
||||||
@@ -615,20 +751,20 @@ impl RunningClientHandler {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(limit) = config.access.user_max_tcp_conns.get(user) {
|
if let Some(limit) = config.access.user_max_tcp_conns.get(user)
|
||||||
if stats.get_user_curr_connects(user) >= *limit as u64 {
|
&& stats.get_user_curr_connects(user) >= *limit as u64
|
||||||
return Err(ProxyError::ConnectionLimitExceeded {
|
{
|
||||||
user: user.to_string(),
|
return Err(ProxyError::ConnectionLimitExceeded {
|
||||||
});
|
user: user.to_string(),
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(quota) = config.access.user_data_quota.get(user) {
|
if let Some(quota) = config.access.user_data_quota.get(user)
|
||||||
if stats.get_user_total_octets(user) >= *quota {
|
&& stats.get_user_total_octets(user) >= *quota
|
||||||
return Err(ProxyError::DataQuotaExceeded {
|
{
|
||||||
user: user.to_string(),
|
return Err(ProxyError::DataQuotaExceeded {
|
||||||
});
|
user: user.to_string(),
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -118,10 +118,10 @@ fn get_dc_addr_static(dc_idx: i16, config: &ProxyConfig) -> Result<SocketAddr> {
|
|||||||
// Unknown DC requested by client without override: log and fall back.
|
// Unknown DC requested by client without override: log and fall back.
|
||||||
if !config.dc_overrides.contains_key(&dc_key) {
|
if !config.dc_overrides.contains_key(&dc_key) {
|
||||||
warn!(dc_idx = dc_idx, "Requested non-standard DC with no override; falling back to default cluster");
|
warn!(dc_idx = dc_idx, "Requested non-standard DC with no override; falling back to default cluster");
|
||||||
if let Some(path) = &config.general.unknown_dc_log_path {
|
if let Some(path) = &config.general.unknown_dc_log_path
|
||||||
if let Ok(mut file) = OpenOptions::new().create(true).append(true).open(path) {
|
&& let Ok(mut file) = OpenOptions::new().create(true).append(true).open(path)
|
||||||
let _ = writeln!(file, "dc_idx={dc_idx}");
|
{
|
||||||
}
|
let _ = writeln!(file, "dc_idx={dc_idx}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
//! MTProto Handshake
|
//! MTProto Handshake
|
||||||
|
|
||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
//! Masking - forward unrecognized traffic to mask host
|
//! Masking - forward unrecognized traffic to mask host
|
||||||
|
|
||||||
use std::str;
|
use std::str;
|
||||||
|
use std::net::IpAddr;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use tokio::net::TcpStream;
|
use tokio::net::TcpStream;
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
@@ -9,6 +10,7 @@ use tokio::io::{AsyncRead, AsyncWrite, AsyncReadExt, AsyncWriteExt};
|
|||||||
use tokio::time::timeout;
|
use tokio::time::timeout;
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
use crate::config::ProxyConfig;
|
use crate::config::ProxyConfig;
|
||||||
|
use crate::stats::beobachten::BeobachtenStore;
|
||||||
|
|
||||||
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.
|
||||||
@@ -19,12 +21,12 @@ const MASK_BUFFER_SIZE: usize = 8192;
|
|||||||
/// Detect client type based on initial data
|
/// Detect client type based on initial data
|
||||||
fn detect_client_type(data: &[u8]) -> &'static str {
|
fn detect_client_type(data: &[u8]) -> &'static str {
|
||||||
// Check for HTTP request
|
// Check for HTTP request
|
||||||
if data.len() > 4 {
|
if data.len() > 4
|
||||||
if data.starts_with(b"GET ") || data.starts_with(b"POST") ||
|
&& (data.starts_with(b"GET ") || data.starts_with(b"POST") ||
|
||||||
data.starts_with(b"HEAD") || data.starts_with(b"PUT ") ||
|
data.starts_with(b"HEAD") || data.starts_with(b"PUT ") ||
|
||||||
data.starts_with(b"DELETE") || data.starts_with(b"OPTIONS") {
|
data.starts_with(b"DELETE") || data.starts_with(b"OPTIONS"))
|
||||||
return "HTTP";
|
{
|
||||||
}
|
return "HTTP";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for TLS ClientHello (0x16 = handshake, 0x03 0x01-0x03 = TLS version)
|
// Check for TLS ClientHello (0x16 = handshake, 0x03 0x01-0x03 = TLS version)
|
||||||
@@ -50,20 +52,26 @@ pub async fn handle_bad_client<R, W>(
|
|||||||
reader: R,
|
reader: R,
|
||||||
writer: W,
|
writer: W,
|
||||||
initial_data: &[u8],
|
initial_data: &[u8],
|
||||||
|
peer_ip: IpAddr,
|
||||||
config: &ProxyConfig,
|
config: &ProxyConfig,
|
||||||
|
beobachten: &BeobachtenStore,
|
||||||
)
|
)
|
||||||
where
|
where
|
||||||
R: AsyncRead + Unpin + Send + 'static,
|
R: AsyncRead + Unpin + Send + 'static,
|
||||||
W: AsyncWrite + Unpin + Send + 'static,
|
W: AsyncWrite + Unpin + Send + 'static,
|
||||||
{
|
{
|
||||||
|
let client_type = detect_client_type(initial_data);
|
||||||
|
if config.general.beobachten {
|
||||||
|
let ttl = Duration::from_secs(config.general.beobachten_minutes.saturating_mul(60));
|
||||||
|
beobachten.record(client_type, peer_ip, ttl);
|
||||||
|
}
|
||||||
|
|
||||||
if !config.censorship.mask {
|
if !config.censorship.mask {
|
||||||
// Masking disabled, just consume data
|
// Masking disabled, just consume data
|
||||||
consume_client_data(reader).await;
|
consume_client_data(reader).await;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let client_type = detect_client_type(initial_data);
|
|
||||||
|
|
||||||
// Connect via Unix socket or TCP
|
// Connect via Unix socket or TCP
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
if let Some(ref sock_path) = config.censorship.mask_unix_sock {
|
if let Some(ref sock_path) = config.censorship.mask_unix_sock {
|
||||||
|
|||||||
@@ -1,5 +1,10 @@
|
|||||||
use std::net::SocketAddr;
|
use std::collections::HashMap;
|
||||||
use std::sync::Arc;
|
use std::collections::hash_map::DefaultHasher;
|
||||||
|
use std::hash::{Hash, Hasher};
|
||||||
|
use std::net::{IpAddr, SocketAddr};
|
||||||
|
use std::sync::atomic::{AtomicU64, Ordering};
|
||||||
|
use std::sync::{Arc, Mutex, OnceLock};
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
|
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
|
||||||
use tokio::sync::{mpsc, oneshot};
|
use tokio::sync::{mpsc, oneshot};
|
||||||
@@ -19,6 +24,148 @@ enum C2MeCommand {
|
|||||||
Close,
|
Close,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const DESYNC_DEDUP_WINDOW: Duration = Duration::from_secs(60);
|
||||||
|
const DESYNC_ERROR_CLASS: &str = "frame_too_large_crypto_desync";
|
||||||
|
static DESYNC_DEDUP: OnceLock<Mutex<HashMap<u64, Instant>>> = OnceLock::new();
|
||||||
|
|
||||||
|
struct RelayForensicsState {
|
||||||
|
trace_id: u64,
|
||||||
|
conn_id: u64,
|
||||||
|
user: String,
|
||||||
|
peer: SocketAddr,
|
||||||
|
peer_hash: u64,
|
||||||
|
started_at: Instant,
|
||||||
|
bytes_c2me: u64,
|
||||||
|
bytes_me2c: Arc<AtomicU64>,
|
||||||
|
desync_all_full: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hash_value<T: Hash>(value: &T) -> u64 {
|
||||||
|
let mut hasher = DefaultHasher::new();
|
||||||
|
value.hash(&mut hasher);
|
||||||
|
hasher.finish()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hash_ip(ip: IpAddr) -> u64 {
|
||||||
|
hash_value(&ip)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn should_emit_full_desync(key: u64, all_full: bool, now: Instant) -> bool {
|
||||||
|
if all_full {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
let dedup = DESYNC_DEDUP.get_or_init(|| Mutex::new(HashMap::new()));
|
||||||
|
let mut guard = dedup.lock().expect("desync dedup mutex poisoned");
|
||||||
|
guard.retain(|_, seen_at| now.duration_since(*seen_at) < DESYNC_DEDUP_WINDOW);
|
||||||
|
|
||||||
|
match guard.get_mut(&key) {
|
||||||
|
Some(seen_at) => {
|
||||||
|
if now.duration_since(*seen_at) >= DESYNC_DEDUP_WINDOW {
|
||||||
|
*seen_at = now;
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
guard.insert(key, now);
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn report_desync_frame_too_large(
|
||||||
|
state: &RelayForensicsState,
|
||||||
|
proto_tag: ProtoTag,
|
||||||
|
frame_counter: u64,
|
||||||
|
max_frame: usize,
|
||||||
|
len: usize,
|
||||||
|
raw_len_bytes: Option<[u8; 4]>,
|
||||||
|
stats: &Stats,
|
||||||
|
) -> ProxyError {
|
||||||
|
let len_buf = raw_len_bytes.unwrap_or((len as u32).to_le_bytes());
|
||||||
|
let looks_like_tls = raw_len_bytes
|
||||||
|
.map(|b| b[0] == 0x16 && b[1] == 0x03)
|
||||||
|
.unwrap_or(false);
|
||||||
|
let looks_like_http = raw_len_bytes
|
||||||
|
.map(|b| matches!(b[0], b'G' | b'P' | b'H' | b'C' | b'D'))
|
||||||
|
.unwrap_or(false);
|
||||||
|
let now = Instant::now();
|
||||||
|
let dedup_key = hash_value(&(
|
||||||
|
state.user.as_str(),
|
||||||
|
state.peer_hash,
|
||||||
|
proto_tag,
|
||||||
|
DESYNC_ERROR_CLASS,
|
||||||
|
));
|
||||||
|
let emit_full = should_emit_full_desync(dedup_key, state.desync_all_full, now);
|
||||||
|
let duration_ms = state.started_at.elapsed().as_millis() as u64;
|
||||||
|
let bytes_me2c = state.bytes_me2c.load(Ordering::Relaxed);
|
||||||
|
|
||||||
|
stats.increment_desync_total();
|
||||||
|
stats.observe_desync_frames_ok(frame_counter);
|
||||||
|
if emit_full {
|
||||||
|
stats.increment_desync_full_logged();
|
||||||
|
warn!(
|
||||||
|
trace_id = format_args!("0x{:016x}", state.trace_id),
|
||||||
|
conn_id = state.conn_id,
|
||||||
|
user = %state.user,
|
||||||
|
peer_hash = format_args!("0x{:016x}", state.peer_hash),
|
||||||
|
proto = ?proto_tag,
|
||||||
|
mode = "middle_proxy",
|
||||||
|
is_tls = true,
|
||||||
|
duration_ms,
|
||||||
|
bytes_c2me = state.bytes_c2me,
|
||||||
|
bytes_me2c,
|
||||||
|
raw_len = len,
|
||||||
|
raw_len_hex = format_args!("0x{:08x}", len),
|
||||||
|
raw_bytes = format_args!(
|
||||||
|
"{:02x} {:02x} {:02x} {:02x}",
|
||||||
|
len_buf[0], len_buf[1], len_buf[2], len_buf[3]
|
||||||
|
),
|
||||||
|
max_frame,
|
||||||
|
tls_like = looks_like_tls,
|
||||||
|
http_like = looks_like_http,
|
||||||
|
frames_ok = frame_counter,
|
||||||
|
dedup_window_secs = DESYNC_DEDUP_WINDOW.as_secs(),
|
||||||
|
desync_all_full = state.desync_all_full,
|
||||||
|
full_reason = if state.desync_all_full { "desync_all_full" } else { "first_in_dedup_window" },
|
||||||
|
error_class = DESYNC_ERROR_CLASS,
|
||||||
|
"Frame too large — crypto desync forensics"
|
||||||
|
);
|
||||||
|
debug!(
|
||||||
|
trace_id = format_args!("0x{:016x}", state.trace_id),
|
||||||
|
conn_id = state.conn_id,
|
||||||
|
user = %state.user,
|
||||||
|
peer = %state.peer,
|
||||||
|
"Frame too large forensic peer detail"
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
stats.increment_desync_suppressed();
|
||||||
|
debug!(
|
||||||
|
trace_id = format_args!("0x{:016x}", state.trace_id),
|
||||||
|
conn_id = state.conn_id,
|
||||||
|
user = %state.user,
|
||||||
|
peer_hash = format_args!("0x{:016x}", state.peer_hash),
|
||||||
|
proto = ?proto_tag,
|
||||||
|
duration_ms,
|
||||||
|
bytes_c2me = state.bytes_c2me,
|
||||||
|
bytes_me2c,
|
||||||
|
raw_len = len,
|
||||||
|
frames_ok = frame_counter,
|
||||||
|
dedup_window_secs = DESYNC_DEDUP_WINDOW.as_secs(),
|
||||||
|
error_class = DESYNC_ERROR_CLASS,
|
||||||
|
"Frame too large — crypto desync forensic suppressed"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
ProxyError::Proxy(format!(
|
||||||
|
"Frame too large: {len} (max {max_frame}), frames_ok={frame_counter}, conn_id={}, trace_id=0x{:016x}",
|
||||||
|
state.conn_id,
|
||||||
|
state.trace_id
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) async fn handle_via_middle_proxy<R, W>(
|
pub(crate) async fn handle_via_middle_proxy<R, W>(
|
||||||
mut crypto_reader: CryptoReader<R>,
|
mut crypto_reader: CryptoReader<R>,
|
||||||
crypto_writer: CryptoWriter<W>,
|
crypto_writer: CryptoWriter<W>,
|
||||||
@@ -37,6 +184,7 @@ where
|
|||||||
let user = success.user.clone();
|
let user = success.user.clone();
|
||||||
let peer = success.peer;
|
let peer = success.peer;
|
||||||
let proto_tag = success.proto_tag;
|
let proto_tag = success.proto_tag;
|
||||||
|
let pool_generation = me_pool.current_generation();
|
||||||
|
|
||||||
info!(
|
info!(
|
||||||
user = %user,
|
user = %user,
|
||||||
@@ -44,19 +192,37 @@ where
|
|||||||
dc = success.dc_idx,
|
dc = success.dc_idx,
|
||||||
proto = ?proto_tag,
|
proto = ?proto_tag,
|
||||||
mode = "middle_proxy",
|
mode = "middle_proxy",
|
||||||
|
pool_generation,
|
||||||
"Routing via Middle-End"
|
"Routing via Middle-End"
|
||||||
);
|
);
|
||||||
|
|
||||||
let (conn_id, me_rx) = me_pool.registry().register().await;
|
let (conn_id, me_rx) = me_pool.registry().register().await;
|
||||||
|
let trace_id = conn_id;
|
||||||
|
let bytes_me2c = Arc::new(AtomicU64::new(0));
|
||||||
|
let mut forensics = RelayForensicsState {
|
||||||
|
trace_id,
|
||||||
|
conn_id,
|
||||||
|
user: user.clone(),
|
||||||
|
peer,
|
||||||
|
peer_hash: hash_ip(peer.ip()),
|
||||||
|
started_at: Instant::now(),
|
||||||
|
bytes_c2me: 0,
|
||||||
|
bytes_me2c: bytes_me2c.clone(),
|
||||||
|
desync_all_full: config.general.desync_all_full,
|
||||||
|
};
|
||||||
|
|
||||||
stats.increment_user_connects(&user);
|
stats.increment_user_connects(&user);
|
||||||
stats.increment_user_curr_connects(&user);
|
stats.increment_user_curr_connects(&user);
|
||||||
|
|
||||||
let proto_flags = proto_flags_for_tag(proto_tag, me_pool.has_proxy_tag());
|
let proto_flags = proto_flags_for_tag(proto_tag, me_pool.has_proxy_tag());
|
||||||
debug!(
|
debug!(
|
||||||
|
trace_id = format_args!("0x{:016x}", trace_id),
|
||||||
user = %user,
|
user = %user,
|
||||||
conn_id,
|
conn_id,
|
||||||
|
peer_hash = format_args!("0x{:016x}", forensics.peer_hash),
|
||||||
|
desync_all_full = forensics.desync_all_full,
|
||||||
proto_flags = format_args!("0x{:08x}", proto_flags),
|
proto_flags = format_args!("0x{:08x}", proto_flags),
|
||||||
|
pool_generation,
|
||||||
"ME relay started"
|
"ME relay started"
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -93,6 +259,7 @@ where
|
|||||||
let stats_clone = stats.clone();
|
let stats_clone = stats.clone();
|
||||||
let rng_clone = rng.clone();
|
let rng_clone = rng.clone();
|
||||||
let user_clone = user.clone();
|
let user_clone = user.clone();
|
||||||
|
let bytes_me2c_clone = bytes_me2c.clone();
|
||||||
let me_writer = tokio::spawn(async move {
|
let me_writer = tokio::spawn(async move {
|
||||||
let mut writer = crypto_writer;
|
let mut writer = crypto_writer;
|
||||||
let mut frame_buf = Vec::with_capacity(16 * 1024);
|
let mut frame_buf = Vec::with_capacity(16 * 1024);
|
||||||
@@ -102,6 +269,7 @@ where
|
|||||||
match msg {
|
match msg {
|
||||||
Some(MeResponse::Data { flags, data }) => {
|
Some(MeResponse::Data { flags, data }) => {
|
||||||
trace!(conn_id, bytes = data.len(), flags, "ME->C data");
|
trace!(conn_id, bytes = data.len(), flags, "ME->C data");
|
||||||
|
bytes_me2c_clone.fetch_add(data.len() as u64, Ordering::Relaxed);
|
||||||
stats_clone.add_user_octets_to(&user_clone, data.len() as u64);
|
stats_clone.add_user_octets_to(&user_clone, data.len() as u64);
|
||||||
write_client_payload(
|
write_client_payload(
|
||||||
&mut writer,
|
&mut writer,
|
||||||
@@ -118,6 +286,7 @@ where
|
|||||||
match next {
|
match next {
|
||||||
MeResponse::Data { flags, data } => {
|
MeResponse::Data { flags, data } => {
|
||||||
trace!(conn_id, bytes = data.len(), flags, "ME->C data (batched)");
|
trace!(conn_id, bytes = data.len(), flags, "ME->C data (batched)");
|
||||||
|
bytes_me2c_clone.fetch_add(data.len() as u64, Ordering::Relaxed);
|
||||||
stats_clone.add_user_octets_to(&user_clone, data.len() as u64);
|
stats_clone.add_user_octets_to(&user_clone, data.len() as u64);
|
||||||
write_client_payload(
|
write_client_payload(
|
||||||
&mut writer,
|
&mut writer,
|
||||||
@@ -173,12 +342,15 @@ where
|
|||||||
&mut crypto_reader,
|
&mut crypto_reader,
|
||||||
proto_tag,
|
proto_tag,
|
||||||
frame_limit,
|
frame_limit,
|
||||||
&user,
|
&forensics,
|
||||||
&mut frame_counter,
|
&mut frame_counter,
|
||||||
&stats,
|
&stats,
|
||||||
).await {
|
).await {
|
||||||
Ok(Some((payload, quickack))) => {
|
Ok(Some((payload, quickack))) => {
|
||||||
trace!(conn_id, bytes = payload.len(), "C->ME frame");
|
trace!(conn_id, bytes = payload.len(), "C->ME frame");
|
||||||
|
forensics.bytes_c2me = forensics
|
||||||
|
.bytes_c2me
|
||||||
|
.saturating_add(payload.len() as u64);
|
||||||
stats.add_user_octets_from(&user, payload.len() as u64);
|
stats.add_user_octets_from(&user, payload.len() as u64);
|
||||||
let mut flags = proto_flags;
|
let mut flags = proto_flags;
|
||||||
if quickack {
|
if quickack {
|
||||||
@@ -221,13 +393,13 @@ where
|
|||||||
.unwrap_or_else(|e| Err(ProxyError::Proxy(format!("ME writer join error: {e}"))));
|
.unwrap_or_else(|e| Err(ProxyError::Proxy(format!("ME writer join error: {e}"))));
|
||||||
|
|
||||||
// When client closes, but ME channel stopped as unregistered - it isnt error
|
// When client closes, but ME channel stopped as unregistered - it isnt error
|
||||||
if client_closed {
|
if client_closed
|
||||||
if matches!(
|
&& matches!(
|
||||||
writer_result,
|
writer_result,
|
||||||
Err(ProxyError::Proxy(ref msg)) if msg == "ME connection lost"
|
Err(ProxyError::Proxy(ref msg)) if msg == "ME connection lost"
|
||||||
) {
|
)
|
||||||
writer_result = Ok(());
|
{
|
||||||
}
|
writer_result = Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let result = match (main_result, c2me_result, writer_result) {
|
let result = match (main_result, c2me_result, writer_result) {
|
||||||
@@ -237,7 +409,16 @@ where
|
|||||||
(_, _, Err(e)) => Err(e),
|
(_, _, Err(e)) => Err(e),
|
||||||
};
|
};
|
||||||
|
|
||||||
debug!(user = %user, conn_id, "ME relay cleanup");
|
debug!(
|
||||||
|
user = %user,
|
||||||
|
conn_id,
|
||||||
|
trace_id = format_args!("0x{:016x}", trace_id),
|
||||||
|
duration_ms = forensics.started_at.elapsed().as_millis() as u64,
|
||||||
|
bytes_c2me = forensics.bytes_c2me,
|
||||||
|
bytes_me2c = forensics.bytes_me2c.load(Ordering::Relaxed),
|
||||||
|
frames_ok = frame_counter,
|
||||||
|
"ME relay cleanup"
|
||||||
|
);
|
||||||
me_pool.registry().unregister(conn_id).await;
|
me_pool.registry().unregister(conn_id).await;
|
||||||
stats.decrement_user_curr_connects(&user);
|
stats.decrement_user_curr_connects(&user);
|
||||||
result
|
result
|
||||||
@@ -247,7 +428,7 @@ async fn read_client_payload<R>(
|
|||||||
client_reader: &mut CryptoReader<R>,
|
client_reader: &mut CryptoReader<R>,
|
||||||
proto_tag: ProtoTag,
|
proto_tag: ProtoTag,
|
||||||
max_frame: usize,
|
max_frame: usize,
|
||||||
user: &str,
|
forensics: &RelayForensicsState,
|
||||||
frame_counter: &mut u64,
|
frame_counter: &mut u64,
|
||||||
stats: &Stats,
|
stats: &Stats,
|
||||||
) -> Result<Option<(Vec<u8>, bool)>>
|
) -> Result<Option<(Vec<u8>, bool)>>
|
||||||
@@ -302,7 +483,9 @@ where
|
|||||||
}
|
}
|
||||||
if len < 4 && proto_tag != ProtoTag::Abridged {
|
if len < 4 && proto_tag != ProtoTag::Abridged {
|
||||||
warn!(
|
warn!(
|
||||||
user = %user,
|
trace_id = format_args!("0x{:016x}", forensics.trace_id),
|
||||||
|
conn_id = forensics.conn_id,
|
||||||
|
user = %forensics.user,
|
||||||
len,
|
len,
|
||||||
proto = ?proto_tag,
|
proto = ?proto_tag,
|
||||||
"Frame too small — corrupt or probe"
|
"Frame too small — corrupt or probe"
|
||||||
@@ -311,31 +494,15 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len > max_frame {
|
if len > max_frame {
|
||||||
let len_buf = raw_len_bytes.unwrap_or((len as u32).to_le_bytes());
|
return Err(report_desync_frame_too_large(
|
||||||
let looks_like_tls = raw_len_bytes
|
forensics,
|
||||||
.map(|b| b[0] == 0x16 && b[1] == 0x03)
|
proto_tag,
|
||||||
.unwrap_or(false);
|
*frame_counter,
|
||||||
let looks_like_http = raw_len_bytes
|
max_frame,
|
||||||
.map(|b| matches!(b[0], b'G' | b'P' | b'H' | b'C' | b'D'))
|
len,
|
||||||
.unwrap_or(false);
|
raw_len_bytes,
|
||||||
warn!(
|
stats,
|
||||||
user = %user,
|
));
|
||||||
raw_len = len,
|
|
||||||
raw_len_hex = format_args!("0x{:08x}", len),
|
|
||||||
raw_bytes = format_args!(
|
|
||||||
"{:02x} {:02x} {:02x} {:02x}",
|
|
||||||
len_buf[0], len_buf[1], len_buf[2], len_buf[3]
|
|
||||||
),
|
|
||||||
proto = ?proto_tag,
|
|
||||||
tls_like = looks_like_tls,
|
|
||||||
http_like = looks_like_http,
|
|
||||||
frames_ok = *frame_counter,
|
|
||||||
"Frame too large — crypto desync forensics"
|
|
||||||
);
|
|
||||||
return Err(ProxyError::Proxy(format!(
|
|
||||||
"Frame too large: {len} (max {max_frame}), frames_ok={}",
|
|
||||||
*frame_counter
|
|
||||||
)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let secure_payload_len = if proto_tag == ProtoTag::Secure {
|
let secure_payload_len = if proto_tag == ProtoTag::Secure {
|
||||||
@@ -382,7 +549,7 @@ where
|
|||||||
|
|
||||||
match proto_tag {
|
match proto_tag {
|
||||||
ProtoTag::Abridged => {
|
ProtoTag::Abridged => {
|
||||||
if data.len() % 4 != 0 {
|
if !data.len().is_multiple_of(4) {
|
||||||
return Err(ProxyError::Proxy(format!(
|
return Err(ProxyError::Proxy(format!(
|
||||||
"Abridged payload must be 4-byte aligned, got {}",
|
"Abridged payload must be 4-byte aligned, got {}",
|
||||||
data.len()
|
data.len()
|
||||||
@@ -400,7 +567,7 @@ where
|
|||||||
frame_buf.push(first);
|
frame_buf.push(first);
|
||||||
frame_buf.extend_from_slice(data);
|
frame_buf.extend_from_slice(data);
|
||||||
client_writer
|
client_writer
|
||||||
.write_all(&frame_buf)
|
.write_all(frame_buf)
|
||||||
.await
|
.await
|
||||||
.map_err(ProxyError::Io)?;
|
.map_err(ProxyError::Io)?;
|
||||||
} else if len_words < (1 << 24) {
|
} else if len_words < (1 << 24) {
|
||||||
@@ -414,7 +581,7 @@ where
|
|||||||
frame_buf.extend_from_slice(&[first, lw[0], lw[1], lw[2]]);
|
frame_buf.extend_from_slice(&[first, lw[0], lw[1], lw[2]]);
|
||||||
frame_buf.extend_from_slice(data);
|
frame_buf.extend_from_slice(data);
|
||||||
client_writer
|
client_writer
|
||||||
.write_all(&frame_buf)
|
.write_all(frame_buf)
|
||||||
.await
|
.await
|
||||||
.map_err(ProxyError::Io)?;
|
.map_err(ProxyError::Io)?;
|
||||||
} else {
|
} else {
|
||||||
@@ -451,7 +618,7 @@ where
|
|||||||
rng.fill(&mut frame_buf[start..]);
|
rng.fill(&mut frame_buf[start..]);
|
||||||
}
|
}
|
||||||
client_writer
|
client_writer
|
||||||
.write_all(&frame_buf)
|
.write_all(frame_buf)
|
||||||
.await
|
.await
|
||||||
.map_err(ProxyError::Io)?;
|
.map_err(ProxyError::Io)?;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,9 @@ pub mod middle_relay;
|
|||||||
pub mod relay;
|
pub mod relay;
|
||||||
|
|
||||||
pub use client::ClientHandler;
|
pub use client::ClientHandler;
|
||||||
|
#[allow(unused_imports)]
|
||||||
pub use handshake::*;
|
pub use handshake::*;
|
||||||
|
#[allow(unused_imports)]
|
||||||
pub use masking::*;
|
pub use masking::*;
|
||||||
|
#[allow(unused_imports)]
|
||||||
pub use relay::*;
|
pub use relay::*;
|
||||||
|
|||||||
117
src/stats/beobachten.rs
Normal file
117
src/stats/beobachten.rs
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
//! Per-IP forensic buckets for scanner and handshake failure observation.
|
||||||
|
|
||||||
|
use std::collections::{BTreeMap, HashMap};
|
||||||
|
use std::net::IpAddr;
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
|
use parking_lot::Mutex;
|
||||||
|
|
||||||
|
const CLEANUP_INTERVAL: Duration = Duration::from_secs(30);
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct BeobachtenInner {
|
||||||
|
entries: HashMap<(String, IpAddr), BeobachtenEntry>,
|
||||||
|
last_cleanup: Option<Instant>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
struct BeobachtenEntry {
|
||||||
|
tries: u64,
|
||||||
|
last_seen: Instant,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// In-memory, TTL-scoped per-IP counters keyed by source class.
|
||||||
|
pub struct BeobachtenStore {
|
||||||
|
inner: Mutex<BeobachtenInner>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for BeobachtenStore {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BeobachtenStore {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
inner: Mutex::new(BeobachtenInner::default()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn record(&self, class: &str, ip: IpAddr, ttl: Duration) {
|
||||||
|
if class.is_empty() || ttl.is_zero() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let now = Instant::now();
|
||||||
|
let mut guard = self.inner.lock();
|
||||||
|
Self::cleanup_if_needed(&mut guard, now, ttl);
|
||||||
|
|
||||||
|
let key = (class.to_string(), ip);
|
||||||
|
let entry = guard.entries.entry(key).or_insert(BeobachtenEntry {
|
||||||
|
tries: 0,
|
||||||
|
last_seen: now,
|
||||||
|
});
|
||||||
|
entry.tries = entry.tries.saturating_add(1);
|
||||||
|
entry.last_seen = now;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn snapshot_text(&self, ttl: Duration) -> String {
|
||||||
|
if ttl.is_zero() {
|
||||||
|
return "beobachten disabled\n".to_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
let now = Instant::now();
|
||||||
|
let mut guard = self.inner.lock();
|
||||||
|
Self::cleanup(&mut guard, now, ttl);
|
||||||
|
guard.last_cleanup = Some(now);
|
||||||
|
|
||||||
|
let mut grouped = BTreeMap::<String, Vec<(IpAddr, u64)>>::new();
|
||||||
|
for ((class, ip), entry) in &guard.entries {
|
||||||
|
grouped
|
||||||
|
.entry(class.clone())
|
||||||
|
.or_default()
|
||||||
|
.push((*ip, entry.tries));
|
||||||
|
}
|
||||||
|
|
||||||
|
if grouped.is_empty() {
|
||||||
|
return "empty\n".to_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut out = String::with_capacity(grouped.len() * 64);
|
||||||
|
for (class, entries) in &mut grouped {
|
||||||
|
out.push('[');
|
||||||
|
out.push_str(class);
|
||||||
|
out.push_str("]\n");
|
||||||
|
|
||||||
|
entries.sort_by(|(ip_a, tries_a), (ip_b, tries_b)| {
|
||||||
|
tries_b
|
||||||
|
.cmp(tries_a)
|
||||||
|
.then_with(|| ip_a.to_string().cmp(&ip_b.to_string()))
|
||||||
|
});
|
||||||
|
|
||||||
|
for (ip, tries) in entries {
|
||||||
|
out.push_str(&format!("{ip}-{tries}\n"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
fn cleanup_if_needed(inner: &mut BeobachtenInner, now: Instant, ttl: Duration) {
|
||||||
|
let should_cleanup = match inner.last_cleanup {
|
||||||
|
Some(last) => now.saturating_duration_since(last) >= CLEANUP_INTERVAL,
|
||||||
|
None => true,
|
||||||
|
};
|
||||||
|
if should_cleanup {
|
||||||
|
Self::cleanup(inner, now, ttl);
|
||||||
|
inner.last_cleanup = Some(now);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn cleanup(inner: &mut BeobachtenInner, now: Instant, ttl: Duration) {
|
||||||
|
inner.entries.retain(|_, entry| {
|
||||||
|
now.saturating_duration_since(entry.last_seen) <= ttl
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
161
src/stats/mod.rs
161
src/stats/mod.rs
@@ -1,7 +1,10 @@
|
|||||||
//! Statistics and replay protection
|
//! Statistics and replay protection
|
||||||
|
|
||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
|
pub mod beobachten;
|
||||||
|
|
||||||
use std::sync::atomic::{AtomicU64, Ordering};
|
use std::sync::atomic::{AtomicU64, Ordering};
|
||||||
use std::sync::Arc;
|
|
||||||
use std::time::{Instant, Duration};
|
use std::time::{Instant, Duration};
|
||||||
use dashmap::DashMap;
|
use dashmap::DashMap;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
@@ -31,6 +34,24 @@ pub struct Stats {
|
|||||||
me_route_drop_channel_closed: AtomicU64,
|
me_route_drop_channel_closed: AtomicU64,
|
||||||
me_route_drop_queue_full: AtomicU64,
|
me_route_drop_queue_full: AtomicU64,
|
||||||
secure_padding_invalid: AtomicU64,
|
secure_padding_invalid: AtomicU64,
|
||||||
|
desync_total: AtomicU64,
|
||||||
|
desync_full_logged: AtomicU64,
|
||||||
|
desync_suppressed: AtomicU64,
|
||||||
|
desync_frames_bucket_0: AtomicU64,
|
||||||
|
desync_frames_bucket_1_2: AtomicU64,
|
||||||
|
desync_frames_bucket_3_10: AtomicU64,
|
||||||
|
desync_frames_bucket_gt_10: AtomicU64,
|
||||||
|
pool_swap_total: AtomicU64,
|
||||||
|
pool_drain_active: AtomicU64,
|
||||||
|
pool_force_close_total: AtomicU64,
|
||||||
|
pool_stale_pick_total: AtomicU64,
|
||||||
|
me_writer_removed_total: AtomicU64,
|
||||||
|
me_writer_removed_unexpected_total: AtomicU64,
|
||||||
|
me_refill_triggered_total: AtomicU64,
|
||||||
|
me_refill_skipped_inflight_total: AtomicU64,
|
||||||
|
me_refill_failed_total: AtomicU64,
|
||||||
|
me_writer_restored_same_endpoint_total: AtomicU64,
|
||||||
|
me_writer_restored_fallback_total: 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>>,
|
||||||
}
|
}
|
||||||
@@ -76,6 +97,81 @@ impl Stats {
|
|||||||
pub fn increment_secure_padding_invalid(&self) {
|
pub fn increment_secure_padding_invalid(&self) {
|
||||||
self.secure_padding_invalid.fetch_add(1, Ordering::Relaxed);
|
self.secure_padding_invalid.fetch_add(1, Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
|
pub fn increment_desync_total(&self) {
|
||||||
|
self.desync_total.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
pub fn increment_desync_full_logged(&self) {
|
||||||
|
self.desync_full_logged.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
pub fn increment_desync_suppressed(&self) {
|
||||||
|
self.desync_suppressed.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
pub fn observe_desync_frames_ok(&self, frames_ok: u64) {
|
||||||
|
match frames_ok {
|
||||||
|
0 => {
|
||||||
|
self.desync_frames_bucket_0.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
1..=2 => {
|
||||||
|
self.desync_frames_bucket_1_2.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
3..=10 => {
|
||||||
|
self.desync_frames_bucket_3_10.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
self.desync_frames_bucket_gt_10.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_pool_swap_total(&self) {
|
||||||
|
self.pool_swap_total.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
pub fn increment_pool_drain_active(&self) {
|
||||||
|
self.pool_drain_active.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
pub fn decrement_pool_drain_active(&self) {
|
||||||
|
let mut current = self.pool_drain_active.load(Ordering::Relaxed);
|
||||||
|
loop {
|
||||||
|
if current == 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
match self.pool_drain_active.compare_exchange_weak(
|
||||||
|
current,
|
||||||
|
current - 1,
|
||||||
|
Ordering::Relaxed,
|
||||||
|
Ordering::Relaxed,
|
||||||
|
) {
|
||||||
|
Ok(_) => break,
|
||||||
|
Err(actual) => current = actual,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn increment_pool_force_close_total(&self) {
|
||||||
|
self.pool_force_close_total.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
pub fn increment_pool_stale_pick_total(&self) {
|
||||||
|
self.pool_stale_pick_total.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
pub fn increment_me_writer_removed_total(&self) {
|
||||||
|
self.me_writer_removed_total.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
pub fn increment_me_writer_removed_unexpected_total(&self) {
|
||||||
|
self.me_writer_removed_unexpected_total.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
pub fn increment_me_refill_triggered_total(&self) {
|
||||||
|
self.me_refill_triggered_total.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
pub fn increment_me_refill_skipped_inflight_total(&self) {
|
||||||
|
self.me_refill_skipped_inflight_total.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
pub fn increment_me_refill_failed_total(&self) {
|
||||||
|
self.me_refill_failed_total.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
pub fn increment_me_writer_restored_same_endpoint_total(&self) {
|
||||||
|
self.me_writer_restored_same_endpoint_total.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
pub fn increment_me_writer_restored_fallback_total(&self) {
|
||||||
|
self.me_writer_restored_fallback_total.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
pub fn get_connects_all(&self) -> u64 { self.connects_all.load(Ordering::Relaxed) }
|
pub fn get_connects_all(&self) -> u64 { self.connects_all.load(Ordering::Relaxed) }
|
||||||
pub fn get_connects_bad(&self) -> u64 { self.connects_bad.load(Ordering::Relaxed) }
|
pub fn get_connects_bad(&self) -> u64 { self.connects_bad.load(Ordering::Relaxed) }
|
||||||
pub fn get_me_keepalive_sent(&self) -> u64 { self.me_keepalive_sent.load(Ordering::Relaxed) }
|
pub fn get_me_keepalive_sent(&self) -> u64 { self.me_keepalive_sent.load(Ordering::Relaxed) }
|
||||||
@@ -96,6 +192,60 @@ impl Stats {
|
|||||||
pub fn get_secure_padding_invalid(&self) -> u64 {
|
pub fn get_secure_padding_invalid(&self) -> u64 {
|
||||||
self.secure_padding_invalid.load(Ordering::Relaxed)
|
self.secure_padding_invalid.load(Ordering::Relaxed)
|
||||||
}
|
}
|
||||||
|
pub fn get_desync_total(&self) -> u64 {
|
||||||
|
self.desync_total.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_desync_full_logged(&self) -> u64 {
|
||||||
|
self.desync_full_logged.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_desync_suppressed(&self) -> u64 {
|
||||||
|
self.desync_suppressed.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_desync_frames_bucket_0(&self) -> u64 {
|
||||||
|
self.desync_frames_bucket_0.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_desync_frames_bucket_1_2(&self) -> u64 {
|
||||||
|
self.desync_frames_bucket_1_2.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_desync_frames_bucket_3_10(&self) -> u64 {
|
||||||
|
self.desync_frames_bucket_3_10.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_desync_frames_bucket_gt_10(&self) -> u64 {
|
||||||
|
self.desync_frames_bucket_gt_10.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_pool_swap_total(&self) -> u64 {
|
||||||
|
self.pool_swap_total.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_pool_drain_active(&self) -> u64 {
|
||||||
|
self.pool_drain_active.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_pool_force_close_total(&self) -> u64 {
|
||||||
|
self.pool_force_close_total.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_pool_stale_pick_total(&self) -> u64 {
|
||||||
|
self.pool_stale_pick_total.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_me_writer_removed_total(&self) -> u64 {
|
||||||
|
self.me_writer_removed_total.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_me_writer_removed_unexpected_total(&self) -> u64 {
|
||||||
|
self.me_writer_removed_unexpected_total.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_me_refill_triggered_total(&self) -> u64 {
|
||||||
|
self.me_refill_triggered_total.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_me_refill_skipped_inflight_total(&self) -> u64 {
|
||||||
|
self.me_refill_skipped_inflight_total.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_me_refill_failed_total(&self) -> u64 {
|
||||||
|
self.me_refill_failed_total.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_me_writer_restored_same_endpoint_total(&self) -> u64 {
|
||||||
|
self.me_writer_restored_same_endpoint_total.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
pub fn get_me_writer_restored_fallback_total(&self) -> u64 {
|
||||||
|
self.me_writer_restored_fallback_total.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()
|
||||||
@@ -227,10 +377,10 @@ impl ReplayShard {
|
|||||||
|
|
||||||
// Use key.as_ref() to get &[u8] — avoids Borrow<Q> ambiguity
|
// Use key.as_ref() to get &[u8] — avoids Borrow<Q> ambiguity
|
||||||
// between Borrow<[u8]> and Borrow<Box<[u8]>>
|
// between Borrow<[u8]> and Borrow<Box<[u8]>>
|
||||||
if let Some(entry) = self.cache.peek(key.as_ref()) {
|
if let Some(entry) = self.cache.peek(key.as_ref())
|
||||||
if entry.seq == queue_seq {
|
&& entry.seq == queue_seq
|
||||||
self.cache.pop(key.as_ref());
|
{
|
||||||
}
|
self.cache.pop(key.as_ref());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -398,6 +548,7 @@ impl ReplayStats {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_stats_shared_counters() {
|
fn test_stats_shared_counters() {
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
//! This module provides a thread-safe pool of BytesMut buffers
|
//! This module provides a thread-safe pool of BytesMut buffers
|
||||||
//! that can be reused across connections to reduce allocation pressure.
|
//! that can be reused across connections to reduce allocation pressure.
|
||||||
|
|
||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
use crossbeam_queue::ArrayQueue;
|
use crossbeam_queue::ArrayQueue;
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
|
|||||||
@@ -18,6 +18,8 @@
|
|||||||
//! is either written to upstream or stored in our pending buffer
|
//! is either written to upstream or stored in our pending buffer
|
||||||
//! - when upstream is pending -> ciphertext is buffered/bounded and backpressure is applied
|
//! - when upstream is pending -> ciphertext is buffered/bounded and backpressure is applied
|
||||||
//!
|
//!
|
||||||
|
|
||||||
|
#![allow(dead_code)]
|
||||||
//! =======================
|
//! =======================
|
||||||
//! Writer state machine
|
//! Writer state machine
|
||||||
//! =======================
|
//! =======================
|
||||||
@@ -45,7 +47,7 @@
|
|||||||
//! - when upstream is Pending but pending still has room: accept `to_accept` bytes and
|
//! - when upstream is Pending but pending still has room: accept `to_accept` bytes and
|
||||||
//! encrypt+append ciphertext directly into pending (in-place encryption of appended range)
|
//! encrypt+append ciphertext directly into pending (in-place encryption of appended range)
|
||||||
|
|
||||||
//! Encrypted stream wrappers using AES-CTR
|
//! Encrypted stream wrappers using AES-CTR
|
||||||
//!
|
//!
|
||||||
//! This module provides stateful async stream wrappers that handle
|
//! This module provides stateful async stream wrappers that handle
|
||||||
//! encryption/decryption with proper partial read/write handling.
|
//! encryption/decryption with proper partial read/write handling.
|
||||||
@@ -55,7 +57,7 @@ use std::io::{self, ErrorKind, Result};
|
|||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
|
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
|
||||||
use tracing::{debug, trace, warn};
|
use tracing::{debug, trace};
|
||||||
|
|
||||||
use crate::crypto::AesCtr;
|
use crate::crypto::AesCtr;
|
||||||
use super::state::{StreamState, YieldBuffer};
|
use super::state::{StreamState, YieldBuffer};
|
||||||
@@ -151,9 +153,9 @@ impl<R> CryptoReader<R> {
|
|||||||
fn take_poison_error(&mut self) -> io::Error {
|
fn take_poison_error(&mut self) -> io::Error {
|
||||||
match &mut self.state {
|
match &mut self.state {
|
||||||
CryptoReaderState::Poisoned { error } => error.take().unwrap_or_else(|| {
|
CryptoReaderState::Poisoned { error } => error.take().unwrap_or_else(|| {
|
||||||
io::Error::new(ErrorKind::Other, "stream previously poisoned")
|
io::Error::other("stream previously poisoned")
|
||||||
}),
|
}),
|
||||||
_ => io::Error::new(ErrorKind::Other, "stream not poisoned"),
|
_ => io::Error::other("stream not poisoned"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -166,6 +168,7 @@ impl<R: AsyncRead + Unpin> AsyncRead for CryptoReader<R> {
|
|||||||
) -> Poll<Result<()>> {
|
) -> Poll<Result<()>> {
|
||||||
let this = self.get_mut();
|
let this = self.get_mut();
|
||||||
|
|
||||||
|
#[allow(clippy::never_loop)]
|
||||||
loop {
|
loop {
|
||||||
match &mut this.state {
|
match &mut this.state {
|
||||||
CryptoReaderState::Poisoned { .. } => {
|
CryptoReaderState::Poisoned { .. } => {
|
||||||
@@ -483,14 +486,14 @@ impl<W> CryptoWriter<W> {
|
|||||||
fn take_poison_error(&mut self) -> io::Error {
|
fn take_poison_error(&mut self) -> io::Error {
|
||||||
match &mut self.state {
|
match &mut self.state {
|
||||||
CryptoWriterState::Poisoned { error } => error.take().unwrap_or_else(|| {
|
CryptoWriterState::Poisoned { error } => error.take().unwrap_or_else(|| {
|
||||||
io::Error::new(ErrorKind::Other, "stream previously poisoned")
|
io::Error::other("stream previously poisoned")
|
||||||
}),
|
}),
|
||||||
_ => io::Error::new(ErrorKind::Other, "stream not poisoned"),
|
_ => io::Error::other("stream not poisoned"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Ensure we are in Flushing state and return mutable pending buffer.
|
/// Ensure we are in Flushing state and return mutable pending buffer.
|
||||||
fn ensure_pending<'a>(state: &'a mut CryptoWriterState, max_pending: usize) -> &'a mut PendingCiphertext {
|
fn ensure_pending(state: &mut CryptoWriterState, max_pending: usize) -> &mut PendingCiphertext {
|
||||||
if matches!(state, CryptoWriterState::Idle) {
|
if matches!(state, CryptoWriterState::Idle) {
|
||||||
*state = CryptoWriterState::Flushing {
|
*state = CryptoWriterState::Flushing {
|
||||||
pending: PendingCiphertext::new(max_pending),
|
pending: PendingCiphertext::new(max_pending),
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
//! This module defines the common types and traits used by all
|
//! This module defines the common types and traits used by all
|
||||||
//! frame encoding/decoding implementations.
|
//! frame encoding/decoding implementations.
|
||||||
|
|
||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use std::io::Result;
|
use std::io::Result;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
//! This module provides Encoder/Decoder implementations compatible
|
//! This module provides Encoder/Decoder implementations compatible
|
||||||
//! with tokio-util's Framed wrapper for easy async frame I/O.
|
//! with tokio-util's Framed wrapper for easy async frame I/O.
|
||||||
|
|
||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
use bytes::{Bytes, BytesMut, BufMut};
|
use bytes::{Bytes, BytesMut, BufMut};
|
||||||
use std::io::{self, Error, ErrorKind};
|
use std::io::{self, Error, ErrorKind};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
@@ -137,7 +139,7 @@ fn encode_abridged(frame: &Frame, dst: &mut BytesMut) -> io::Result<()> {
|
|||||||
let data = &frame.data;
|
let data = &frame.data;
|
||||||
|
|
||||||
// Validate alignment
|
// Validate alignment
|
||||||
if data.len() % 4 != 0 {
|
if !data.len().is_multiple_of(4) {
|
||||||
return Err(Error::new(
|
return Err(Error::new(
|
||||||
ErrorKind::InvalidInput,
|
ErrorKind::InvalidInput,
|
||||||
format!("abridged frame must be 4-byte aligned, got {} bytes", data.len())
|
format!("abridged frame must be 4-byte aligned, got {} bytes", data.len())
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
//! MTProto frame stream wrappers
|
//! MTProto frame stream wrappers
|
||||||
|
|
||||||
use bytes::{Bytes, BytesMut};
|
#![allow(dead_code)]
|
||||||
|
|
||||||
|
use bytes::Bytes;
|
||||||
use std::io::{Error, ErrorKind, Result};
|
use std::io::{Error, ErrorKind, Result};
|
||||||
use tokio::io::{AsyncRead, AsyncWrite, AsyncReadExt, AsyncWriteExt};
|
use tokio::io::{AsyncRead, AsyncWrite, AsyncReadExt, AsyncWriteExt};
|
||||||
use crate::protocol::constants::*;
|
use crate::protocol::constants::*;
|
||||||
@@ -76,7 +78,7 @@ impl<W> AbridgedFrameWriter<W> {
|
|||||||
impl<W: AsyncWrite + Unpin> AbridgedFrameWriter<W> {
|
impl<W: AsyncWrite + Unpin> AbridgedFrameWriter<W> {
|
||||||
/// Write a frame
|
/// Write a frame
|
||||||
pub async fn write_frame(&mut self, data: &[u8], meta: &FrameMeta) -> Result<()> {
|
pub async fn write_frame(&mut self, data: &[u8], meta: &FrameMeta) -> Result<()> {
|
||||||
if data.len() % 4 != 0 {
|
if !data.len().is_multiple_of(4) {
|
||||||
return Err(Error::new(
|
return Err(Error::new(
|
||||||
ErrorKind::InvalidInput,
|
ErrorKind::InvalidInput,
|
||||||
format!("Abridged frame must be aligned to 4 bytes, got {}", data.len()),
|
format!("Abridged frame must be aligned to 4 bytes, got {}", data.len()),
|
||||||
@@ -329,7 +331,7 @@ impl<R: AsyncRead + Unpin> MtprotoFrameReader<R> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Validate length
|
// Validate length
|
||||||
if len < MIN_MSG_LEN || len > MAX_MSG_LEN || len % PADDING_FILLER.len() != 0 {
|
if !(MIN_MSG_LEN..=MAX_MSG_LEN).contains(&len) || !len.is_multiple_of(PADDING_FILLER.len()) {
|
||||||
return Err(Error::new(
|
return Err(Error::new(
|
||||||
ErrorKind::InvalidData,
|
ErrorKind::InvalidData,
|
||||||
format!("Invalid message length: {}", len),
|
format!("Invalid message length: {}", len),
|
||||||
|
|||||||
@@ -12,28 +12,34 @@ pub mod frame_codec;
|
|||||||
pub mod frame_stream;
|
pub mod frame_stream;
|
||||||
|
|
||||||
// Re-export state machine types
|
// Re-export state machine types
|
||||||
|
#[allow(unused_imports)]
|
||||||
pub use state::{
|
pub use state::{
|
||||||
StreamState, Transition, PollResult,
|
StreamState, Transition, PollResult,
|
||||||
ReadBuffer, WriteBuffer, HeaderBuffer, YieldBuffer,
|
ReadBuffer, WriteBuffer, HeaderBuffer, YieldBuffer,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Re-export buffer pool
|
// Re-export buffer pool
|
||||||
|
#[allow(unused_imports)]
|
||||||
pub use buffer_pool::{BufferPool, PooledBuffer, PoolStats};
|
pub use buffer_pool::{BufferPool, PooledBuffer, PoolStats};
|
||||||
|
|
||||||
// Re-export stream implementations
|
// Re-export stream implementations
|
||||||
|
#[allow(unused_imports)]
|
||||||
pub use crypto_stream::{CryptoReader, CryptoWriter, PassthroughStream};
|
pub use crypto_stream::{CryptoReader, CryptoWriter, PassthroughStream};
|
||||||
pub use tls_stream::{FakeTlsReader, FakeTlsWriter};
|
pub use tls_stream::{FakeTlsReader, FakeTlsWriter};
|
||||||
|
|
||||||
// Re-export frame types
|
// Re-export frame types
|
||||||
|
#[allow(unused_imports)]
|
||||||
pub use frame::{Frame, FrameMeta, FrameCodec as FrameCodecTrait, create_codec};
|
pub use frame::{Frame, FrameMeta, FrameCodec as FrameCodecTrait, create_codec};
|
||||||
|
|
||||||
// Re-export tokio-util compatible codecs
|
// Re-export tokio-util compatible codecs
|
||||||
|
#[allow(unused_imports)]
|
||||||
pub use frame_codec::{
|
pub use frame_codec::{
|
||||||
FrameCodec,
|
FrameCodec,
|
||||||
AbridgedCodec, IntermediateCodec, SecureCodec,
|
AbridgedCodec, IntermediateCodec, SecureCodec,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Legacy re-exports for compatibility
|
// Legacy re-exports for compatibility
|
||||||
|
#[allow(unused_imports)]
|
||||||
pub use frame_stream::{
|
pub use frame_stream::{
|
||||||
AbridgedFrameReader, AbridgedFrameWriter,
|
AbridgedFrameReader, AbridgedFrameWriter,
|
||||||
IntermediateFrameReader, IntermediateFrameWriter,
|
IntermediateFrameReader, IntermediateFrameWriter,
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
//! This module provides core types and traits for implementing
|
//! This module provides core types and traits for implementing
|
||||||
//! stateful async streams with proper partial read/write handling.
|
//! stateful async streams with proper partial read/write handling.
|
||||||
|
|
||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,8 @@
|
|||||||
//! - Explicit state machines for all async operations
|
//! - Explicit state machines for all async operations
|
||||||
//! - Never lose data on partial reads
|
//! - Never lose data on partial reads
|
||||||
//! - Atomic TLS record formation for writes
|
//! - Atomic TLS record formation for writes
|
||||||
|
|
||||||
|
#![allow(dead_code)]
|
||||||
//! - Proper handling of all TLS record types
|
//! - Proper handling of all TLS record types
|
||||||
//!
|
//!
|
||||||
//! Important nuance (Telegram FakeTLS):
|
//! Important nuance (Telegram FakeTLS):
|
||||||
@@ -133,7 +135,7 @@ impl TlsRecordHeader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Build header bytes
|
/// Build header bytes
|
||||||
fn to_bytes(&self) -> [u8; 5] {
|
fn to_bytes(self) -> [u8; 5] {
|
||||||
[
|
[
|
||||||
self.record_type,
|
self.record_type,
|
||||||
self.version[0],
|
self.version[0],
|
||||||
@@ -258,9 +260,9 @@ impl<R> FakeTlsReader<R> {
|
|||||||
fn take_poison_error(&mut self) -> io::Error {
|
fn take_poison_error(&mut self) -> io::Error {
|
||||||
match &mut self.state {
|
match &mut self.state {
|
||||||
TlsReaderState::Poisoned { error } => error.take().unwrap_or_else(|| {
|
TlsReaderState::Poisoned { error } => error.take().unwrap_or_else(|| {
|
||||||
io::Error::new(ErrorKind::Other, "stream previously poisoned")
|
io::Error::other("stream previously poisoned")
|
||||||
}),
|
}),
|
||||||
_ => io::Error::new(ErrorKind::Other, "stream not poisoned"),
|
_ => io::Error::other("stream not poisoned"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -295,7 +297,7 @@ impl<R: AsyncRead + Unpin> AsyncRead for FakeTlsReader<R> {
|
|||||||
TlsReaderState::Poisoned { error } => {
|
TlsReaderState::Poisoned { error } => {
|
||||||
this.state = TlsReaderState::Poisoned { error: None };
|
this.state = TlsReaderState::Poisoned { error: None };
|
||||||
let err = error.unwrap_or_else(|| {
|
let err = error.unwrap_or_else(|| {
|
||||||
io::Error::new(ErrorKind::Other, "stream previously poisoned")
|
io::Error::other("stream previously poisoned")
|
||||||
});
|
});
|
||||||
return Poll::Ready(Err(err));
|
return Poll::Ready(Err(err));
|
||||||
}
|
}
|
||||||
@@ -614,9 +616,9 @@ impl<W> FakeTlsWriter<W> {
|
|||||||
fn take_poison_error(&mut self) -> io::Error {
|
fn take_poison_error(&mut self) -> io::Error {
|
||||||
match &mut self.state {
|
match &mut self.state {
|
||||||
TlsWriterState::Poisoned { error } => error.take().unwrap_or_else(|| {
|
TlsWriterState::Poisoned { error } => error.take().unwrap_or_else(|| {
|
||||||
io::Error::new(ErrorKind::Other, "stream previously poisoned")
|
io::Error::other("stream previously poisoned")
|
||||||
}),
|
}),
|
||||||
_ => io::Error::new(ErrorKind::Other, "stream not poisoned"),
|
_ => io::Error::other("stream not poisoned"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -680,7 +682,7 @@ impl<W: AsyncWrite + Unpin> AsyncWrite for FakeTlsWriter<W> {
|
|||||||
TlsWriterState::Poisoned { error } => {
|
TlsWriterState::Poisoned { error } => {
|
||||||
this.state = TlsWriterState::Poisoned { error: None };
|
this.state = TlsWriterState::Poisoned { error: None };
|
||||||
let err = error.unwrap_or_else(|| {
|
let err = error.unwrap_or_else(|| {
|
||||||
Error::new(ErrorKind::Other, "stream previously poisoned")
|
Error::other("stream previously poisoned")
|
||||||
});
|
});
|
||||||
return Poll::Ready(Err(err));
|
return Poll::Ready(Err(err));
|
||||||
}
|
}
|
||||||
@@ -769,7 +771,7 @@ impl<W: AsyncWrite + Unpin> AsyncWrite for FakeTlsWriter<W> {
|
|||||||
TlsWriterState::Poisoned { error } => {
|
TlsWriterState::Poisoned { error } => {
|
||||||
this.state = TlsWriterState::Poisoned { error: None };
|
this.state = TlsWriterState::Poisoned { error: None };
|
||||||
let err = error.unwrap_or_else(|| {
|
let err = error.unwrap_or_else(|| {
|
||||||
Error::new(ErrorKind::Other, "stream previously poisoned")
|
Error::other("stream previously poisoned")
|
||||||
});
|
});
|
||||||
return Poll::Ready(Err(err));
|
return Poll::Ready(Err(err));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
//! Stream traits and common types
|
//! Stream traits and common types
|
||||||
|
|
||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use std::io::Result;
|
use std::io::Result;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ pub struct TlsFrontCache {
|
|||||||
disk_path: PathBuf,
|
disk_path: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
impl TlsFrontCache {
|
impl TlsFrontCache {
|
||||||
pub fn new(domains: &[String], default_len: usize, disk_path: impl AsRef<Path>) -> Self {
|
pub fn new(domains: &[String], default_len: usize, disk_path: impl AsRef<Path>) -> Self {
|
||||||
let default_template = ParsedServerHello {
|
let default_template = ParsedServerHello {
|
||||||
@@ -114,32 +115,32 @@ impl TlsFrontCache {
|
|||||||
if !name.ends_with(".json") {
|
if !name.ends_with(".json") {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if let Ok(data) = tokio::fs::read(entry.path()).await {
|
if let Ok(data) = tokio::fs::read(entry.path()).await
|
||||||
if let Ok(mut cached) = serde_json::from_slice::<CachedTlsData>(&data) {
|
&& let Ok(mut cached) = serde_json::from_slice::<CachedTlsData>(&data)
|
||||||
if cached.domain.is_empty()
|
{
|
||||||
|| cached.domain.len() > 255
|
if cached.domain.is_empty()
|
||||||
|| !cached.domain.chars().all(|c| c.is_ascii_alphanumeric() || c == '.' || c == '-')
|
|| cached.domain.len() > 255
|
||||||
{
|
|| !cached.domain.chars().all(|c| c.is_ascii_alphanumeric() || c == '.' || c == '-')
|
||||||
warn!(file = %name, "Skipping TLS cache entry with invalid domain");
|
{
|
||||||
continue;
|
warn!(file = %name, "Skipping TLS cache entry with invalid domain");
|
||||||
}
|
continue;
|
||||||
// fetched_at is skipped during deserialization; approximate with file mtime if available.
|
|
||||||
if let Ok(meta) = entry.metadata().await {
|
|
||||||
if let Ok(modified) = meta.modified() {
|
|
||||||
cached.fetched_at = modified;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Drop entries older than 72h
|
|
||||||
if let Ok(age) = cached.fetched_at.elapsed() {
|
|
||||||
if age > Duration::from_secs(72 * 3600) {
|
|
||||||
warn!(domain = %cached.domain, "Skipping stale TLS cache entry (>72h)");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let domain = cached.domain.clone();
|
|
||||||
self.set(&domain, cached).await;
|
|
||||||
loaded += 1;
|
|
||||||
}
|
}
|
||||||
|
// fetched_at is skipped during deserialization; approximate with file mtime if available.
|
||||||
|
if let Ok(meta) = entry.metadata().await
|
||||||
|
&& let Ok(modified) = meta.modified()
|
||||||
|
{
|
||||||
|
cached.fetched_at = modified;
|
||||||
|
}
|
||||||
|
// Drop entries older than 72h
|
||||||
|
if let Ok(age) = cached.fetched_at.elapsed()
|
||||||
|
&& age > Duration::from_secs(72 * 3600)
|
||||||
|
{
|
||||||
|
warn!(domain = %cached.domain, "Skipping stale TLS cache entry (>72h)");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let domain = cached.domain.clone();
|
||||||
|
self.set(&domain, cached).await;
|
||||||
|
loaded += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -173,7 +174,7 @@ impl TlsFrontCache {
|
|||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
loop {
|
loop {
|
||||||
for domain in &domains {
|
for domain in &domains {
|
||||||
fetcher(domain.clone()).await;
|
let _ = fetcher(domain.clone()).await;
|
||||||
}
|
}
|
||||||
sleep(interval).await;
|
sleep(interval).await;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ fn jitter_and_clamp_sizes(sizes: &[usize], rng: &SecureRandom) -> Vec<usize> {
|
|||||||
sizes
|
sizes
|
||||||
.iter()
|
.iter()
|
||||||
.map(|&size| {
|
.map(|&size| {
|
||||||
let base = size.max(MIN_APP_DATA).min(MAX_APP_DATA);
|
let base = size.clamp(MIN_APP_DATA, MAX_APP_DATA);
|
||||||
let jitter_range = ((base as f64) * 0.03).round() as i64;
|
let jitter_range = ((base as f64) * 0.03).round() as i64;
|
||||||
if jitter_range == 0 {
|
if jitter_range == 0 {
|
||||||
return base;
|
return base;
|
||||||
@@ -50,7 +50,7 @@ fn ensure_payload_capacity(mut sizes: Vec<usize>, payload_len: usize) -> Vec<usi
|
|||||||
|
|
||||||
while body_total < payload_len {
|
while body_total < payload_len {
|
||||||
let remaining = payload_len - body_total;
|
let remaining = payload_len - body_total;
|
||||||
let chunk = (remaining + 17).min(MAX_APP_DATA).max(MIN_APP_DATA);
|
let chunk = (remaining + 17).clamp(MIN_APP_DATA, MAX_APP_DATA);
|
||||||
sizes.push(chunk);
|
sizes.push(chunk);
|
||||||
body_total += chunk.saturating_sub(17);
|
body_total += chunk.saturating_sub(17);
|
||||||
}
|
}
|
||||||
@@ -189,7 +189,7 @@ pub fn build_emulated_server_hello(
|
|||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|payload| payload.certificate_message.as_slice())
|
.map(|payload| payload.certificate_message.as_slice())
|
||||||
.filter(|payload| !payload.is_empty())
|
.filter(|payload| !payload.is_empty())
|
||||||
.or_else(|| compact_payload.as_deref())
|
.or(compact_payload.as_deref())
|
||||||
} else {
|
} else {
|
||||||
compact_payload.as_deref()
|
compact_payload.as_deref()
|
||||||
};
|
};
|
||||||
@@ -223,15 +223,13 @@ pub fn build_emulated_server_hello(
|
|||||||
} else {
|
} else {
|
||||||
rec.extend_from_slice(&rng.bytes(size));
|
rec.extend_from_slice(&rng.bytes(size));
|
||||||
}
|
}
|
||||||
|
} else 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 {
|
} else {
|
||||||
if size > 17 {
|
rec.extend_from_slice(&rng.bytes(size));
|
||||||
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);
|
app_data.extend_from_slice(&rec);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -384,7 +384,7 @@ async fn fetch_via_raw_tls(
|
|||||||
for _ in 0..4 {
|
for _ in 0..4 {
|
||||||
match timeout(connect_timeout, read_tls_record(&mut stream)).await {
|
match timeout(connect_timeout, read_tls_record(&mut stream)).await {
|
||||||
Ok(Ok(rec)) => records.push(rec),
|
Ok(Ok(rec)) => records.push(rec),
|
||||||
Ok(Err(e)) => return Err(e.into()),
|
Ok(Err(e)) => return Err(e),
|
||||||
Err(_) => break,
|
Err(_) => break,
|
||||||
}
|
}
|
||||||
if records.len() >= 3 && records.iter().any(|(t, _)| *t == TLS_RECORD_APPLICATION) {
|
if records.len() >= 3 && records.iter().any(|(t, _)| *t == TLS_RECORD_APPLICATION) {
|
||||||
|
|||||||
@@ -4,4 +4,5 @@ pub mod fetcher;
|
|||||||
pub mod emulator;
|
pub mod emulator;
|
||||||
|
|
||||||
pub use cache::TlsFrontCache;
|
pub use cache::TlsFrontCache;
|
||||||
|
#[allow(unused_imports)]
|
||||||
pub use types::{CachedTlsData, TlsFetchResult};
|
pub use types::{CachedTlsData, TlsFetchResult};
|
||||||
|
|||||||
@@ -165,11 +165,10 @@ fn process_pid16() -> u16 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn process_utime() -> u32 {
|
fn process_utime() -> u32 {
|
||||||
let utime = std::time::SystemTime::now()
|
std::time::SystemTime::now()
|
||||||
.duration_since(std::time::UNIX_EPOCH)
|
.duration_since(std::time::UNIX_EPOCH)
|
||||||
.unwrap_or_default()
|
.unwrap_or_default()
|
||||||
.as_secs() as u32;
|
.as_secs() as u32
|
||||||
utime
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn cbc_encrypt_padded(
|
pub(crate) fn cbc_encrypt_padded(
|
||||||
|
|||||||
@@ -1,15 +1,18 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::hash::{DefaultHasher, Hash, Hasher};
|
||||||
use std::net::IpAddr;
|
use std::net::IpAddr;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use httpdate;
|
use httpdate;
|
||||||
|
use tokio::sync::watch;
|
||||||
use tracing::{debug, info, warn};
|
use tracing::{debug, info, warn};
|
||||||
|
|
||||||
|
use crate::config::ProxyConfig;
|
||||||
use crate::error::Result;
|
use crate::error::Result;
|
||||||
|
|
||||||
use super::MePool;
|
use super::MePool;
|
||||||
use super::secret::download_proxy_secret;
|
use super::secret::download_proxy_secret_with_max_len;
|
||||||
use crate::crypto::SecureRandom;
|
use crate::crypto::SecureRandom;
|
||||||
use std::time::SystemTime;
|
use std::time::SystemTime;
|
||||||
|
|
||||||
@@ -37,15 +40,103 @@ pub struct ProxyConfigData {
|
|||||||
pub default_dc: Option<i32>,
|
pub default_dc: Option<i32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_host_port(s: &str) -> Option<(IpAddr, u16)> {
|
#[derive(Debug, Default)]
|
||||||
if let Some(bracket_end) = s.rfind(']') {
|
struct StableSnapshot {
|
||||||
if s.starts_with('[') && bracket_end + 1 < s.len() && s.as_bytes().get(bracket_end + 1) == Some(&b':') {
|
candidate_hash: Option<u64>,
|
||||||
let host = &s[1..bracket_end];
|
candidate_hits: u8,
|
||||||
let port_str = &s[bracket_end + 2..];
|
applied_hash: Option<u64>,
|
||||||
let ip = host.parse::<IpAddr>().ok()?;
|
}
|
||||||
let port = port_str.parse::<u16>().ok()?;
|
|
||||||
return Some((ip, port));
|
impl StableSnapshot {
|
||||||
|
fn observe(&mut self, hash: u64) -> u8 {
|
||||||
|
if self.candidate_hash == Some(hash) {
|
||||||
|
self.candidate_hits = self.candidate_hits.saturating_add(1);
|
||||||
|
} else {
|
||||||
|
self.candidate_hash = Some(hash);
|
||||||
|
self.candidate_hits = 1;
|
||||||
}
|
}
|
||||||
|
self.candidate_hits
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_applied(&self, hash: u64) -> bool {
|
||||||
|
self.applied_hash == Some(hash)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn mark_applied(&mut self, hash: u64) {
|
||||||
|
self.applied_hash = Some(hash);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
struct UpdaterState {
|
||||||
|
config_v4: StableSnapshot,
|
||||||
|
config_v6: StableSnapshot,
|
||||||
|
secret: StableSnapshot,
|
||||||
|
last_map_apply_at: Option<tokio::time::Instant>,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hash_proxy_config(cfg: &ProxyConfigData) -> u64 {
|
||||||
|
let mut hasher = DefaultHasher::new();
|
||||||
|
cfg.default_dc.hash(&mut hasher);
|
||||||
|
|
||||||
|
let mut by_dc: Vec<(i32, Vec<(IpAddr, u16)>)> =
|
||||||
|
cfg.map.iter().map(|(dc, addrs)| (*dc, addrs.clone())).collect();
|
||||||
|
by_dc.sort_by_key(|(dc, _)| *dc);
|
||||||
|
for (dc, mut addrs) in by_dc {
|
||||||
|
dc.hash(&mut hasher);
|
||||||
|
addrs.sort_unstable();
|
||||||
|
for (ip, port) in addrs {
|
||||||
|
ip.hash(&mut hasher);
|
||||||
|
port.hash(&mut hasher);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
hasher.finish()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hash_secret(secret: &[u8]) -> u64 {
|
||||||
|
let mut hasher = DefaultHasher::new();
|
||||||
|
secret.hash(&mut hasher);
|
||||||
|
hasher.finish()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn map_apply_cooldown_ready(
|
||||||
|
last_applied: Option<tokio::time::Instant>,
|
||||||
|
cooldown: Duration,
|
||||||
|
) -> bool {
|
||||||
|
if cooldown.is_zero() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
match last_applied {
|
||||||
|
Some(ts) => ts.elapsed() >= cooldown,
|
||||||
|
None => true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn map_apply_cooldown_remaining_secs(
|
||||||
|
last_applied: tokio::time::Instant,
|
||||||
|
cooldown: Duration,
|
||||||
|
) -> u64 {
|
||||||
|
if cooldown.is_zero() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
cooldown
|
||||||
|
.checked_sub(last_applied.elapsed())
|
||||||
|
.map(|d| d.as_secs())
|
||||||
|
.unwrap_or(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_host_port(s: &str) -> Option<(IpAddr, u16)> {
|
||||||
|
if let Some(bracket_end) = s.rfind(']')
|
||||||
|
&& 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 idx = s.rfind(':')?;
|
||||||
@@ -82,20 +173,18 @@ pub async fn fetch_proxy_config(url: &str) -> Result<ProxyConfigData> {
|
|||||||
.map_err(|e| crate::error::ProxyError::Proxy(format!("fetch_proxy_config GET failed: {e}")))?
|
.map_err(|e| crate::error::ProxyError::Proxy(format!("fetch_proxy_config GET failed: {e}")))?
|
||||||
;
|
;
|
||||||
|
|
||||||
if let Some(date) = resp.headers().get(reqwest::header::DATE) {
|
if let Some(date) = resp.headers().get(reqwest::header::DATE)
|
||||||
if let Ok(date_str) = date.to_str() {
|
&& let Ok(date_str) = date.to_str()
|
||||||
if let Ok(server_time) = httpdate::parse_http_date(date_str) {
|
&& let Ok(server_time) = httpdate::parse_http_date(date_str)
|
||||||
if let Ok(skew) = SystemTime::now().duration_since(server_time).or_else(|e| {
|
&& let Ok(skew) = SystemTime::now().duration_since(server_time).or_else(|e| {
|
||||||
server_time.duration_since(SystemTime::now()).map_err(|_| e)
|
server_time.duration_since(SystemTime::now()).map_err(|_| e)
|
||||||
}) {
|
})
|
||||||
let skew_secs = skew.as_secs();
|
{
|
||||||
if skew_secs > 60 {
|
let skew_secs = skew.as_secs();
|
||||||
warn!(skew_secs, "Time skew >60s detected from fetch_proxy_config Date header");
|
if skew_secs > 60 {
|
||||||
} else if skew_secs > 30 {
|
warn!(skew_secs, "Time skew >60s detected from fetch_proxy_config Date header");
|
||||||
warn!(skew_secs, "Time skew >30s detected from fetch_proxy_config Date header");
|
} else if skew_secs > 30 {
|
||||||
}
|
warn!(skew_secs, "Time skew >30s detected from fetch_proxy_config Date header");
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -128,50 +217,232 @@ pub async fn fetch_proxy_config(url: &str) -> Result<ProxyConfigData> {
|
|||||||
Ok(ProxyConfigData { map, default_dc })
|
Ok(ProxyConfigData { map, default_dc })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn me_config_updater(pool: Arc<MePool>, rng: Arc<SecureRandom>, interval: Duration) {
|
async fn run_update_cycle(
|
||||||
let mut tick = tokio::time::interval(interval);
|
pool: &Arc<MePool>,
|
||||||
// skip immediate tick to avoid double-fetch right after startup
|
rng: &Arc<SecureRandom>,
|
||||||
tick.tick().await;
|
cfg: &ProxyConfig,
|
||||||
loop {
|
state: &mut UpdaterState,
|
||||||
tick.tick().await;
|
) {
|
||||||
|
pool.update_runtime_reinit_policy(
|
||||||
|
cfg.general.hardswap,
|
||||||
|
cfg.general.me_pool_drain_ttl_secs,
|
||||||
|
cfg.general.effective_me_pool_force_close_secs(),
|
||||||
|
cfg.general.me_pool_min_fresh_ratio,
|
||||||
|
cfg.general.me_hardswap_warmup_delay_min_ms,
|
||||||
|
cfg.general.me_hardswap_warmup_delay_max_ms,
|
||||||
|
cfg.general.me_hardswap_warmup_extra_passes,
|
||||||
|
cfg.general.me_hardswap_warmup_pass_backoff_base_ms,
|
||||||
|
);
|
||||||
|
|
||||||
// Update proxy config v4
|
let required_cfg_snapshots = cfg.general.me_config_stable_snapshots.max(1);
|
||||||
let cfg_v4 = retry_fetch("https://core.telegram.org/getProxyConfig").await;
|
let required_secret_snapshots = cfg.general.proxy_secret_stable_snapshots.max(1);
|
||||||
if let Some(cfg) = cfg_v4 {
|
let apply_cooldown = Duration::from_secs(cfg.general.me_config_apply_cooldown_secs);
|
||||||
let changed = pool.update_proxy_maps(cfg.map.clone(), None).await;
|
let mut maps_changed = false;
|
||||||
if let Some(dc) = cfg.default_dc {
|
|
||||||
pool.default_dc.store(dc, std::sync::atomic::Ordering::Relaxed);
|
let mut ready_v4: Option<(ProxyConfigData, u64)> = None;
|
||||||
}
|
let cfg_v4 = retry_fetch("https://core.telegram.org/getProxyConfig").await;
|
||||||
if changed {
|
if let Some(cfg_v4) = cfg_v4 {
|
||||||
info!("ME config updated (v4), reconciling connections");
|
let cfg_v4_hash = hash_proxy_config(&cfg_v4);
|
||||||
pool.reconcile_connections(&rng).await;
|
let stable_hits = state.config_v4.observe(cfg_v4_hash);
|
||||||
} else {
|
if stable_hits < required_cfg_snapshots {
|
||||||
debug!("ME config v4 unchanged");
|
debug!(
|
||||||
}
|
stable_hits,
|
||||||
|
required_cfg_snapshots,
|
||||||
|
snapshot = format_args!("0x{cfg_v4_hash:016x}"),
|
||||||
|
"ME config v4 candidate observed"
|
||||||
|
);
|
||||||
|
} else if state.config_v4.is_applied(cfg_v4_hash) {
|
||||||
|
debug!(
|
||||||
|
snapshot = format_args!("0x{cfg_v4_hash:016x}"),
|
||||||
|
"ME config v4 stable snapshot already applied"
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
ready_v4 = Some((cfg_v4, cfg_v4_hash));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Update proxy config v6 (optional)
|
let mut ready_v6: Option<(ProxyConfigData, u64)> = None;
|
||||||
let cfg_v6 = retry_fetch("https://core.telegram.org/getProxyConfigV6").await;
|
let cfg_v6 = retry_fetch("https://core.telegram.org/getProxyConfigV6").await;
|
||||||
if let Some(cfg_v6) = cfg_v6 {
|
if let Some(cfg_v6) = cfg_v6 {
|
||||||
let changed = pool.update_proxy_maps(HashMap::new(), Some(cfg_v6.map)).await;
|
let cfg_v6_hash = hash_proxy_config(&cfg_v6);
|
||||||
if changed {
|
let stable_hits = state.config_v6.observe(cfg_v6_hash);
|
||||||
info!("ME config updated (v6), reconciling connections");
|
if stable_hits < required_cfg_snapshots {
|
||||||
pool.reconcile_connections(&rng).await;
|
debug!(
|
||||||
} else {
|
stable_hits,
|
||||||
debug!("ME config v6 unchanged");
|
required_cfg_snapshots,
|
||||||
}
|
snapshot = format_args!("0x{cfg_v6_hash:016x}"),
|
||||||
|
"ME config v6 candidate observed"
|
||||||
|
);
|
||||||
|
} else if state.config_v6.is_applied(cfg_v6_hash) {
|
||||||
|
debug!(
|
||||||
|
snapshot = format_args!("0x{cfg_v6_hash:016x}"),
|
||||||
|
"ME config v6 stable snapshot already applied"
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
ready_v6 = Some((cfg_v6, cfg_v6_hash));
|
||||||
}
|
}
|
||||||
pool.reset_stun_state();
|
}
|
||||||
|
|
||||||
// Update proxy-secret
|
if ready_v4.is_some() || ready_v6.is_some() {
|
||||||
match download_proxy_secret().await {
|
if map_apply_cooldown_ready(state.last_map_apply_at, apply_cooldown) {
|
||||||
|
let update_v4 = ready_v4
|
||||||
|
.as_ref()
|
||||||
|
.map(|(snapshot, _)| snapshot.map.clone())
|
||||||
|
.unwrap_or_default();
|
||||||
|
let update_v6 = ready_v6
|
||||||
|
.as_ref()
|
||||||
|
.map(|(snapshot, _)| snapshot.map.clone());
|
||||||
|
|
||||||
|
let changed = pool.update_proxy_maps(update_v4, update_v6).await;
|
||||||
|
|
||||||
|
if let Some((snapshot, hash)) = ready_v4 {
|
||||||
|
if let Some(dc) = snapshot.default_dc {
|
||||||
|
pool.default_dc
|
||||||
|
.store(dc, std::sync::atomic::Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
state.config_v4.mark_applied(hash);
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some((_snapshot, hash)) = ready_v6 {
|
||||||
|
state.config_v6.mark_applied(hash);
|
||||||
|
}
|
||||||
|
|
||||||
|
state.last_map_apply_at = Some(tokio::time::Instant::now());
|
||||||
|
|
||||||
|
if changed {
|
||||||
|
maps_changed = true;
|
||||||
|
info!("ME config update applied after stable-gate");
|
||||||
|
} else {
|
||||||
|
debug!("ME config stable-gate applied with no map delta");
|
||||||
|
}
|
||||||
|
} else if let Some(last) = state.last_map_apply_at {
|
||||||
|
let wait_secs = map_apply_cooldown_remaining_secs(last, apply_cooldown);
|
||||||
|
debug!(
|
||||||
|
wait_secs,
|
||||||
|
"ME config stable snapshot deferred by cooldown"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if maps_changed {
|
||||||
|
pool.zero_downtime_reinit_after_map_change(rng.as_ref())
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
|
pool.reset_stun_state();
|
||||||
|
|
||||||
|
if cfg.general.proxy_secret_rotate_runtime {
|
||||||
|
match download_proxy_secret_with_max_len(cfg.general.proxy_secret_len_max).await {
|
||||||
Ok(secret) => {
|
Ok(secret) => {
|
||||||
if pool.update_secret(secret).await {
|
let secret_hash = hash_secret(&secret);
|
||||||
info!("proxy-secret updated and pool reconnect scheduled");
|
let stable_hits = state.secret.observe(secret_hash);
|
||||||
|
if stable_hits < required_secret_snapshots {
|
||||||
|
debug!(
|
||||||
|
stable_hits,
|
||||||
|
required_secret_snapshots,
|
||||||
|
snapshot = format_args!("0x{secret_hash:016x}"),
|
||||||
|
"proxy-secret candidate observed"
|
||||||
|
);
|
||||||
|
} else if state.secret.is_applied(secret_hash) {
|
||||||
|
debug!(
|
||||||
|
snapshot = format_args!("0x{secret_hash:016x}"),
|
||||||
|
"proxy-secret stable snapshot already applied"
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
let rotated = pool.update_secret(secret).await;
|
||||||
|
state.secret.mark_applied(secret_hash);
|
||||||
|
if rotated {
|
||||||
|
info!("proxy-secret rotated after stable-gate");
|
||||||
|
} else {
|
||||||
|
debug!("proxy-secret stable snapshot confirmed as unchanged");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => warn!(error = %e, "proxy-secret update failed"),
|
Err(e) => warn!(error = %e, "proxy-secret update failed"),
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
debug!("proxy-secret runtime rotation disabled by config");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn me_config_updater(
|
||||||
|
pool: Arc<MePool>,
|
||||||
|
rng: Arc<SecureRandom>,
|
||||||
|
mut config_rx: watch::Receiver<Arc<ProxyConfig>>,
|
||||||
|
) {
|
||||||
|
let mut state = UpdaterState::default();
|
||||||
|
let mut update_every_secs = config_rx
|
||||||
|
.borrow()
|
||||||
|
.general
|
||||||
|
.effective_update_every_secs()
|
||||||
|
.max(1);
|
||||||
|
let mut update_every = Duration::from_secs(update_every_secs);
|
||||||
|
let mut next_tick = tokio::time::Instant::now() + update_every;
|
||||||
|
info!(update_every_secs, "ME config updater started");
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let sleep = tokio::time::sleep_until(next_tick);
|
||||||
|
tokio::pin!(sleep);
|
||||||
|
|
||||||
|
tokio::select! {
|
||||||
|
_ = &mut sleep => {
|
||||||
|
let cfg = config_rx.borrow().clone();
|
||||||
|
run_update_cycle(&pool, &rng, cfg.as_ref(), &mut state).await;
|
||||||
|
let refreshed_secs = cfg.general.effective_update_every_secs().max(1);
|
||||||
|
if refreshed_secs != update_every_secs {
|
||||||
|
info!(
|
||||||
|
old_update_every_secs = update_every_secs,
|
||||||
|
new_update_every_secs = refreshed_secs,
|
||||||
|
"ME config updater interval changed"
|
||||||
|
);
|
||||||
|
update_every_secs = refreshed_secs;
|
||||||
|
update_every = Duration::from_secs(update_every_secs);
|
||||||
|
}
|
||||||
|
next_tick = tokio::time::Instant::now() + update_every;
|
||||||
|
}
|
||||||
|
changed = config_rx.changed() => {
|
||||||
|
if changed.is_err() {
|
||||||
|
warn!("ME config updater stopped: config channel closed");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let cfg = config_rx.borrow().clone();
|
||||||
|
pool.update_runtime_reinit_policy(
|
||||||
|
cfg.general.hardswap,
|
||||||
|
cfg.general.me_pool_drain_ttl_secs,
|
||||||
|
cfg.general.effective_me_pool_force_close_secs(),
|
||||||
|
cfg.general.me_pool_min_fresh_ratio,
|
||||||
|
cfg.general.me_hardswap_warmup_delay_min_ms,
|
||||||
|
cfg.general.me_hardswap_warmup_delay_max_ms,
|
||||||
|
cfg.general.me_hardswap_warmup_extra_passes,
|
||||||
|
cfg.general.me_hardswap_warmup_pass_backoff_base_ms,
|
||||||
|
);
|
||||||
|
let new_secs = cfg.general.effective_update_every_secs().max(1);
|
||||||
|
if new_secs == update_every_secs {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if new_secs < update_every_secs {
|
||||||
|
info!(
|
||||||
|
old_update_every_secs = update_every_secs,
|
||||||
|
new_update_every_secs = new_secs,
|
||||||
|
"ME config updater interval decreased, running immediate refresh"
|
||||||
|
);
|
||||||
|
update_every_secs = new_secs;
|
||||||
|
update_every = Duration::from_secs(update_every_secs);
|
||||||
|
run_update_cycle(&pool, &rng, cfg.as_ref(), &mut state).await;
|
||||||
|
next_tick = tokio::time::Instant::now() + update_every;
|
||||||
|
} else {
|
||||||
|
info!(
|
||||||
|
old_update_every_secs = update_every_secs,
|
||||||
|
new_update_every_secs = new_secs,
|
||||||
|
"ME config updater interval increased"
|
||||||
|
);
|
||||||
|
update_every_secs = new_secs;
|
||||||
|
update_every = Duration::from_secs(update_every_secs);
|
||||||
|
next_tick = tokio::time::Instant::now() + update_every;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -47,21 +47,21 @@ impl MePool {
|
|||||||
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 connect_fut = async {
|
let connect_fut = async {
|
||||||
if addr.is_ipv6() {
|
if addr.is_ipv6()
|
||||||
if let Some(v6) = self.detected_ipv6 {
|
&& let Some(v6) = self.detected_ipv6
|
||||||
match TcpSocket::new_v6() {
|
{
|
||||||
Ok(sock) => {
|
match TcpSocket::new_v6() {
|
||||||
if let Err(e) = sock.bind(SocketAddr::new(IpAddr::V6(v6), 0)) {
|
Ok(sock) => {
|
||||||
debug!(error = %e, bind_ip = %v6, "ME IPv6 bind failed, falling back to default bind");
|
if let Err(e) = sock.bind(SocketAddr::new(IpAddr::V6(v6), 0)) {
|
||||||
} else {
|
debug!(error = %e, bind_ip = %v6, "ME IPv6 bind failed, falling back to default bind");
|
||||||
match sock.connect(addr).await {
|
} else {
|
||||||
Ok(stream) => return Ok(stream),
|
match sock.connect(addr).await {
|
||||||
Err(e) => debug!(error = %e, target = %addr, "ME IPv6 bound connect failed, retrying default connect"),
|
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"),
|
|
||||||
}
|
}
|
||||||
|
Err(e) => debug!(error = %e, "ME IPv6 socket creation failed, falling back to default connect"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TcpStream::connect(addr).await
|
TcpStream::connect(addr).await
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::HashMap;
|
||||||
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::Rng;
|
use rand::Rng;
|
||||||
|
|
||||||
use crate::crypto::SecureRandom;
|
use crate::crypto::SecureRandom;
|
||||||
@@ -14,6 +13,7 @@ use super::MePool;
|
|||||||
|
|
||||||
const HEALTH_INTERVAL_SECS: u64 = 1;
|
const HEALTH_INTERVAL_SECS: u64 = 1;
|
||||||
const JITTER_FRAC_NUM: u64 = 2; // jitter up to 50% of backoff
|
const JITTER_FRAC_NUM: u64 = 2; // jitter up to 50% of backoff
|
||||||
|
#[allow(dead_code)]
|
||||||
const MAX_CONCURRENT_PER_DC_DEFAULT: usize = 1;
|
const MAX_CONCURRENT_PER_DC_DEFAULT: usize = 1;
|
||||||
|
|
||||||
pub async fn me_health_monitor(pool: Arc<MePool>, rng: Arc<SecureRandom>, _min_connections: usize) {
|
pub async fn me_health_monitor(pool: Arc<MePool>, rng: Arc<SecureRandom>, _min_connections: usize) {
|
||||||
@@ -63,37 +63,50 @@ async fn check_family(
|
|||||||
IpFamily::V4 => pool.proxy_map_v4.read().await.clone(),
|
IpFamily::V4 => pool.proxy_map_v4.read().await.clone(),
|
||||||
IpFamily::V6 => pool.proxy_map_v6.read().await.clone(),
|
IpFamily::V6 => pool.proxy_map_v6.read().await.clone(),
|
||||||
};
|
};
|
||||||
let writer_addrs: HashSet<SocketAddr> = pool
|
|
||||||
|
let mut dc_endpoints = HashMap::<i32, Vec<SocketAddr>>::new();
|
||||||
|
for (dc, addrs) in map {
|
||||||
|
let entry = dc_endpoints.entry(dc.abs()).or_default();
|
||||||
|
for (ip, port) in addrs {
|
||||||
|
entry.push(SocketAddr::new(ip, port));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for endpoints in dc_endpoints.values_mut() {
|
||||||
|
endpoints.sort_unstable();
|
||||||
|
endpoints.dedup();
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut live_addr_counts = HashMap::<SocketAddr, usize>::new();
|
||||||
|
for writer in pool
|
||||||
.writers
|
.writers
|
||||||
.read()
|
.read()
|
||||||
.await
|
.await
|
||||||
.iter()
|
.iter()
|
||||||
.map(|w| w.addr)
|
.filter(|w| !w.draining.load(std::sync::atomic::Ordering::Relaxed))
|
||||||
.collect();
|
{
|
||||||
|
*live_addr_counts.entry(writer.addr).or_insert(0) += 1;
|
||||||
|
}
|
||||||
|
|
||||||
let entries: Vec<(i32, Vec<SocketAddr>)> = map
|
for (dc, endpoints) in dc_endpoints {
|
||||||
.iter()
|
if endpoints.is_empty() {
|
||||||
.map(|(dc, addrs)| {
|
|
||||||
let list = addrs
|
|
||||||
.iter()
|
|
||||||
.map(|(ip, port)| SocketAddr::new(*ip, *port))
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
(*dc, list)
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
for (dc, dc_addrs) in entries {
|
|
||||||
let has_coverage = dc_addrs.iter().any(|a| writer_addrs.contains(a));
|
|
||||||
if has_coverage {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
let required = MePool::required_writers_for_dc(endpoints.len());
|
||||||
|
let alive = endpoints
|
||||||
|
.iter()
|
||||||
|
.map(|addr| *live_addr_counts.get(addr).unwrap_or(&0))
|
||||||
|
.sum::<usize>();
|
||||||
|
if alive >= required {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let missing = required - alive;
|
||||||
|
|
||||||
let key = (dc, family);
|
let key = (dc, family);
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
if let Some(ts) = next_attempt.get(&key) {
|
if let Some(ts) = next_attempt.get(&key)
|
||||||
if now < *ts {
|
&& now < *ts
|
||||||
continue;
|
{
|
||||||
}
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let max_concurrent = pool.me_reconnect_max_concurrent_per_dc.max(1) as usize;
|
let max_concurrent = pool.me_reconnect_max_concurrent_per_dc.max(1) as usize;
|
||||||
@@ -102,32 +115,45 @@ async fn check_family(
|
|||||||
}
|
}
|
||||||
*inflight.entry(key).or_insert(0) += 1;
|
*inflight.entry(key).or_insert(0) += 1;
|
||||||
|
|
||||||
let mut shuffled = dc_addrs.clone();
|
let mut restored = 0usize;
|
||||||
shuffled.shuffle(&mut rand::rng());
|
for _ in 0..missing {
|
||||||
let mut success = false;
|
let res = tokio::time::timeout(
|
||||||
for addr in shuffled {
|
pool.me_one_timeout,
|
||||||
let res = tokio::time::timeout(pool.me_one_timeout, pool.connect_one(addr, rng.as_ref())).await;
|
pool.connect_endpoints_round_robin(&endpoints, rng.as_ref()),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
match res {
|
match res {
|
||||||
Ok(Ok(())) => {
|
Ok(true) => {
|
||||||
info!(%addr, dc = %dc, ?family, "ME reconnected for DC coverage");
|
restored += 1;
|
||||||
pool.stats.increment_me_reconnect_success();
|
pool.stats.increment_me_reconnect_success();
|
||||||
backoff.insert(key, pool.me_reconnect_backoff_base.as_millis() as u64);
|
|
||||||
let jitter = pool.me_reconnect_backoff_base.as_millis() as u64 / JITTER_FRAC_NUM;
|
|
||||||
let wait = pool.me_reconnect_backoff_base
|
|
||||||
+ Duration::from_millis(rand::rng().random_range(0..=jitter.max(1)));
|
|
||||||
next_attempt.insert(key, now + wait);
|
|
||||||
success = true;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
Ok(Err(e)) => {
|
Ok(false) => {
|
||||||
pool.stats.increment_me_reconnect_attempt();
|
pool.stats.increment_me_reconnect_attempt();
|
||||||
debug!(%addr, dc = %dc, error = %e, ?family, "ME reconnect failed")
|
debug!(dc = %dc, ?family, "ME round-robin reconnect failed")
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
pool.stats.increment_me_reconnect_attempt();
|
||||||
|
debug!(dc = %dc, ?family, "ME reconnect timed out");
|
||||||
}
|
}
|
||||||
Err(_) => debug!(%addr, dc = %dc, ?family, "ME reconnect timed out"),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !success {
|
|
||||||
pool.stats.increment_me_reconnect_attempt();
|
let now_alive = alive + restored;
|
||||||
|
if now_alive >= required {
|
||||||
|
info!(
|
||||||
|
dc = %dc,
|
||||||
|
?family,
|
||||||
|
alive = now_alive,
|
||||||
|
required,
|
||||||
|
endpoint_count = endpoints.len(),
|
||||||
|
"ME writer floor restored for DC"
|
||||||
|
);
|
||||||
|
backoff.insert(key, pool.me_reconnect_backoff_base.as_millis() as u64);
|
||||||
|
let jitter = pool.me_reconnect_backoff_base.as_millis() as u64 / JITTER_FRAC_NUM;
|
||||||
|
let wait = pool.me_reconnect_backoff_base
|
||||||
|
+ Duration::from_millis(rand::rng().random_range(0..=jitter.max(1)));
|
||||||
|
next_attempt.insert(key, now + wait);
|
||||||
|
} else {
|
||||||
let curr = *backoff.get(&key).unwrap_or(&(pool.me_reconnect_backoff_base.as_millis() as u64));
|
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);
|
let next_ms = (curr.saturating_mul(2)).min(pool.me_reconnect_backoff_cap.as_millis() as u64);
|
||||||
backoff.insert(key, next_ms);
|
backoff.insert(key, next_ms);
|
||||||
@@ -135,7 +161,15 @@ async fn check_family(
|
|||||||
let wait = Duration::from_millis(next_ms)
|
let wait = Duration::from_millis(next_ms)
|
||||||
+ Duration::from_millis(rand::rng().random_range(0..=jitter.max(1)));
|
+ Duration::from_millis(rand::rng().random_range(0..=jitter.max(1)));
|
||||||
next_attempt.insert(key, now + wait);
|
next_attempt.insert(key, now + wait);
|
||||||
warn!(dc = %dc, backoff_ms = next_ms, ?family, "DC has no ME coverage, scheduled reconnect");
|
warn!(
|
||||||
|
dc = %dc,
|
||||||
|
?family,
|
||||||
|
alive = now_alive,
|
||||||
|
required,
|
||||||
|
endpoint_count = endpoints.len(),
|
||||||
|
backoff_ms = next_ms,
|
||||||
|
"DC writer floor is below required level, scheduled reconnect"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
if let Some(v) = inflight.get_mut(&key) {
|
if let Some(v) = inflight.get_mut(&key) {
|
||||||
*v = v.saturating_sub(1);
|
*v = v.saturating_sub(1);
|
||||||
|
|||||||
@@ -17,8 +17,10 @@ mod wire;
|
|||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
|
|
||||||
pub use health::me_health_monitor;
|
pub use health::me_health_monitor;
|
||||||
|
#[allow(unused_imports)]
|
||||||
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;
|
||||||
|
#[allow(unused_imports)]
|
||||||
pub use pool_nat::{stun_probe, detect_public_ip};
|
pub use pool_nat::{stun_probe, detect_public_ip};
|
||||||
pub use registry::ConnRegistry;
|
pub use registry::ConnRegistry;
|
||||||
pub use secret::fetch_proxy_secret;
|
pub use secret::fetch_proxy_secret;
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ pub struct MePingSample {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
#[allow(dead_code)]
|
||||||
pub struct MePingReport {
|
pub struct MePingReport {
|
||||||
pub dc: i32,
|
pub dc: i32,
|
||||||
pub family: MePingFamily,
|
pub family: MePingFamily,
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::{HashMap, HashSet};
|
||||||
use std::net::{IpAddr, Ipv6Addr, SocketAddr};
|
use std::net::{IpAddr, Ipv6Addr, SocketAddr};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::sync::atomic::{AtomicBool, AtomicI32, AtomicU64, AtomicUsize, Ordering};
|
use std::sync::atomic::{AtomicBool, AtomicI32, AtomicU32, 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, mpsc, Notify};
|
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, Instant};
|
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
use crate::crypto::SecureRandom;
|
use crate::crypto::SecureRandom;
|
||||||
use crate::error::{ProxyError, Result};
|
use crate::error::{ProxyError, Result};
|
||||||
@@ -27,12 +27,16 @@ const ME_ACTIVE_PING_JITTER_SECS: i64 = 5;
|
|||||||
pub struct MeWriter {
|
pub struct MeWriter {
|
||||||
pub id: u64,
|
pub id: u64,
|
||||||
pub addr: SocketAddr,
|
pub addr: SocketAddr,
|
||||||
|
pub generation: u64,
|
||||||
pub tx: mpsc::Sender<WriterCommand>,
|
pub tx: mpsc::Sender<WriterCommand>,
|
||||||
pub cancel: CancellationToken,
|
pub cancel: CancellationToken,
|
||||||
pub degraded: Arc<AtomicBool>,
|
pub degraded: Arc<AtomicBool>,
|
||||||
pub draining: Arc<AtomicBool>,
|
pub draining: Arc<AtomicBool>,
|
||||||
|
pub draining_started_at_epoch_secs: Arc<AtomicU64>,
|
||||||
|
pub allow_drain_fallback: Arc<AtomicBool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
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>>>,
|
||||||
@@ -71,8 +75,18 @@ pub struct MePool {
|
|||||||
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<NatReflectionCache>>,
|
pub(super) nat_reflection_cache: Arc<Mutex<NatReflectionCache>>,
|
||||||
pub(super) writer_available: Arc<Notify>,
|
pub(super) writer_available: Arc<Notify>,
|
||||||
|
pub(super) refill_inflight: Arc<Mutex<HashSet<SocketAddr>>>,
|
||||||
pub(super) conn_count: AtomicUsize,
|
pub(super) conn_count: AtomicUsize,
|
||||||
pub(super) stats: Arc<crate::stats::Stats>,
|
pub(super) stats: Arc<crate::stats::Stats>,
|
||||||
|
pub(super) generation: AtomicU64,
|
||||||
|
pub(super) hardswap: AtomicBool,
|
||||||
|
pub(super) me_pool_drain_ttl_secs: AtomicU64,
|
||||||
|
pub(super) me_pool_force_close_secs: AtomicU64,
|
||||||
|
pub(super) me_pool_min_fresh_ratio_permille: AtomicU32,
|
||||||
|
pub(super) me_hardswap_warmup_delay_min_ms: AtomicU64,
|
||||||
|
pub(super) me_hardswap_warmup_delay_max_ms: AtomicU64,
|
||||||
|
pub(super) me_hardswap_warmup_extra_passes: AtomicU32,
|
||||||
|
pub(super) me_hardswap_warmup_pass_backoff_base_ms: AtomicU64,
|
||||||
pool_size: usize,
|
pool_size: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,6 +97,22 @@ pub struct NatReflectionCache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl MePool {
|
impl MePool {
|
||||||
|
fn ratio_to_permille(ratio: f32) -> u32 {
|
||||||
|
let clamped = ratio.clamp(0.0, 1.0);
|
||||||
|
(clamped * 1000.0).round() as u32
|
||||||
|
}
|
||||||
|
|
||||||
|
fn permille_to_ratio(permille: u32) -> f32 {
|
||||||
|
(permille.min(1000) as f32) / 1000.0
|
||||||
|
}
|
||||||
|
|
||||||
|
fn now_epoch_secs() -> u64 {
|
||||||
|
SystemTime::now()
|
||||||
|
.duration_since(UNIX_EPOCH)
|
||||||
|
.unwrap_or_default()
|
||||||
|
.as_secs()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn new(
|
pub fn new(
|
||||||
proxy_tag: Option<Vec<u8>>,
|
proxy_tag: Option<Vec<u8>>,
|
||||||
proxy_secret: Vec<u8>,
|
proxy_secret: Vec<u8>,
|
||||||
@@ -110,6 +140,14 @@ impl MePool {
|
|||||||
me_reconnect_backoff_base_ms: u64,
|
me_reconnect_backoff_base_ms: u64,
|
||||||
me_reconnect_backoff_cap_ms: u64,
|
me_reconnect_backoff_cap_ms: u64,
|
||||||
me_reconnect_fast_retry_count: u32,
|
me_reconnect_fast_retry_count: u32,
|
||||||
|
hardswap: bool,
|
||||||
|
me_pool_drain_ttl_secs: u64,
|
||||||
|
me_pool_force_close_secs: u64,
|
||||||
|
me_pool_min_fresh_ratio: f32,
|
||||||
|
me_hardswap_warmup_delay_min_ms: u64,
|
||||||
|
me_hardswap_warmup_delay_max_ms: u64,
|
||||||
|
me_hardswap_warmup_extra_passes: u8,
|
||||||
|
me_hardswap_warmup_pass_backoff_base_ms: u64,
|
||||||
) -> Arc<Self> {
|
) -> Arc<Self> {
|
||||||
Arc::new(Self {
|
Arc::new(Self {
|
||||||
registry: Arc::new(ConnRegistry::new()),
|
registry: Arc::new(ConnRegistry::new()),
|
||||||
@@ -151,7 +189,17 @@ impl MePool {
|
|||||||
rtt_stats: Arc::new(Mutex::new(HashMap::new())),
|
rtt_stats: Arc::new(Mutex::new(HashMap::new())),
|
||||||
nat_reflection_cache: Arc::new(Mutex::new(NatReflectionCache::default())),
|
nat_reflection_cache: Arc::new(Mutex::new(NatReflectionCache::default())),
|
||||||
writer_available: Arc::new(Notify::new()),
|
writer_available: Arc::new(Notify::new()),
|
||||||
|
refill_inflight: Arc::new(Mutex::new(HashSet::new())),
|
||||||
conn_count: AtomicUsize::new(0),
|
conn_count: AtomicUsize::new(0),
|
||||||
|
generation: AtomicU64::new(1),
|
||||||
|
hardswap: AtomicBool::new(hardswap),
|
||||||
|
me_pool_drain_ttl_secs: AtomicU64::new(me_pool_drain_ttl_secs),
|
||||||
|
me_pool_force_close_secs: AtomicU64::new(me_pool_force_close_secs),
|
||||||
|
me_pool_min_fresh_ratio_permille: AtomicU32::new(Self::ratio_to_permille(me_pool_min_fresh_ratio)),
|
||||||
|
me_hardswap_warmup_delay_min_ms: AtomicU64::new(me_hardswap_warmup_delay_min_ms),
|
||||||
|
me_hardswap_warmup_delay_max_ms: AtomicU64::new(me_hardswap_warmup_delay_max_ms),
|
||||||
|
me_hardswap_warmup_extra_passes: AtomicU32::new(me_hardswap_warmup_extra_passes as u32),
|
||||||
|
me_hardswap_warmup_pass_backoff_base_ms: AtomicU64::new(me_hardswap_warmup_pass_backoff_base_ms),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,6 +207,37 @@ impl MePool {
|
|||||||
self.proxy_tag.is_some()
|
self.proxy_tag.is_some()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn current_generation(&self) -> u64 {
|
||||||
|
self.generation.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update_runtime_reinit_policy(
|
||||||
|
&self,
|
||||||
|
hardswap: bool,
|
||||||
|
drain_ttl_secs: u64,
|
||||||
|
force_close_secs: u64,
|
||||||
|
min_fresh_ratio: f32,
|
||||||
|
hardswap_warmup_delay_min_ms: u64,
|
||||||
|
hardswap_warmup_delay_max_ms: u64,
|
||||||
|
hardswap_warmup_extra_passes: u8,
|
||||||
|
hardswap_warmup_pass_backoff_base_ms: u64,
|
||||||
|
) {
|
||||||
|
self.hardswap.store(hardswap, Ordering::Relaxed);
|
||||||
|
self.me_pool_drain_ttl_secs.store(drain_ttl_secs, Ordering::Relaxed);
|
||||||
|
self.me_pool_force_close_secs
|
||||||
|
.store(force_close_secs, Ordering::Relaxed);
|
||||||
|
self.me_pool_min_fresh_ratio_permille
|
||||||
|
.store(Self::ratio_to_permille(min_fresh_ratio), Ordering::Relaxed);
|
||||||
|
self.me_hardswap_warmup_delay_min_ms
|
||||||
|
.store(hardswap_warmup_delay_min_ms, Ordering::Relaxed);
|
||||||
|
self.me_hardswap_warmup_delay_max_ms
|
||||||
|
.store(hardswap_warmup_delay_max_ms, Ordering::Relaxed);
|
||||||
|
self.me_hardswap_warmup_extra_passes
|
||||||
|
.store(hardswap_warmup_extra_passes as u32, Ordering::Relaxed);
|
||||||
|
self.me_hardswap_warmup_pass_backoff_base_ms
|
||||||
|
.store(hardswap_warmup_pass_backoff_base_ms, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn reset_stun_state(&self) {
|
pub fn reset_stun_state(&self) {
|
||||||
self.nat_probe_attempts.store(0, Ordering::Relaxed);
|
self.nat_probe_attempts.store(0, Ordering::Relaxed);
|
||||||
self.nat_probe_disabled.store(false, Ordering::Relaxed);
|
self.nat_probe_disabled.store(false, Ordering::Relaxed);
|
||||||
@@ -177,8 +256,43 @@ impl MePool {
|
|||||||
self.writers.clone()
|
self.writers.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn force_close_timeout(&self) -> Option<Duration> {
|
||||||
|
let secs = self.me_pool_force_close_secs.load(Ordering::Relaxed);
|
||||||
|
if secs == 0 {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(Duration::from_secs(secs))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn coverage_ratio(
|
||||||
|
desired_by_dc: &HashMap<i32, HashSet<SocketAddr>>,
|
||||||
|
active_writer_addrs: &HashSet<SocketAddr>,
|
||||||
|
) -> (f32, Vec<i32>) {
|
||||||
|
if desired_by_dc.is_empty() {
|
||||||
|
return (1.0, Vec::new());
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut missing_dc = Vec::<i32>::new();
|
||||||
|
let mut covered = 0usize;
|
||||||
|
for (dc, endpoints) in desired_by_dc {
|
||||||
|
if endpoints.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if endpoints.iter().any(|addr| active_writer_addrs.contains(addr)) {
|
||||||
|
covered += 1;
|
||||||
|
} else {
|
||||||
|
missing_dc.push(*dc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
missing_dc.sort_unstable();
|
||||||
|
let total = desired_by_dc.len().max(1);
|
||||||
|
let ratio = (covered as f32) / (total as f32);
|
||||||
|
(ratio, missing_dc)
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn reconcile_connections(self: &Arc<Self>, rng: &SecureRandom) {
|
pub async fn reconcile_connections(self: &Arc<Self>, rng: &SecureRandom) {
|
||||||
use std::collections::HashSet;
|
|
||||||
let writers = self.writers.read().await;
|
let writers = self.writers.read().await;
|
||||||
let current: HashSet<SocketAddr> = writers
|
let current: HashSet<SocketAddr> = writers
|
||||||
.iter()
|
.iter()
|
||||||
@@ -210,6 +324,458 @@ impl MePool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn desired_dc_endpoints(&self) -> HashMap<i32, HashSet<SocketAddr>> {
|
||||||
|
let mut out: HashMap<i32, HashSet<SocketAddr>> = HashMap::new();
|
||||||
|
|
||||||
|
if self.decision.ipv4_me {
|
||||||
|
let map_v4 = self.proxy_map_v4.read().await.clone();
|
||||||
|
for (dc, addrs) in map_v4 {
|
||||||
|
let entry = out.entry(dc.abs()).or_default();
|
||||||
|
for (ip, port) in addrs {
|
||||||
|
entry.insert(SocketAddr::new(ip, port));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.decision.ipv6_me {
|
||||||
|
let map_v6 = self.proxy_map_v6.read().await.clone();
|
||||||
|
for (dc, addrs) in map_v6 {
|
||||||
|
let entry = out.entry(dc.abs()).or_default();
|
||||||
|
for (ip, port) in addrs {
|
||||||
|
entry.insert(SocketAddr::new(ip, port));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) fn required_writers_for_dc(endpoint_count: usize) -> usize {
|
||||||
|
endpoint_count.max(3)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hardswap_warmup_connect_delay_ms(&self) -> u64 {
|
||||||
|
let min_ms = self
|
||||||
|
.me_hardswap_warmup_delay_min_ms
|
||||||
|
.load(Ordering::Relaxed);
|
||||||
|
let max_ms = self
|
||||||
|
.me_hardswap_warmup_delay_max_ms
|
||||||
|
.load(Ordering::Relaxed);
|
||||||
|
let (min_ms, max_ms) = if min_ms <= max_ms {
|
||||||
|
(min_ms, max_ms)
|
||||||
|
} else {
|
||||||
|
(max_ms, min_ms)
|
||||||
|
};
|
||||||
|
if min_ms == max_ms {
|
||||||
|
return min_ms;
|
||||||
|
}
|
||||||
|
rand::rng().random_range(min_ms..=max_ms)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hardswap_warmup_backoff_ms(&self, pass_idx: usize) -> u64 {
|
||||||
|
let base_ms = self
|
||||||
|
.me_hardswap_warmup_pass_backoff_base_ms
|
||||||
|
.load(Ordering::Relaxed);
|
||||||
|
let cap_ms = (self.me_reconnect_backoff_cap.as_millis() as u64).max(base_ms);
|
||||||
|
let shift = (pass_idx as u32).min(20);
|
||||||
|
let scaled = base_ms.saturating_mul(1u64 << shift);
|
||||||
|
let core = scaled.min(cap_ms);
|
||||||
|
let jitter = (core / 2).max(1);
|
||||||
|
core.saturating_add(rand::rng().random_range(0..=jitter))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn fresh_writer_count_for_endpoints(
|
||||||
|
&self,
|
||||||
|
generation: u64,
|
||||||
|
endpoints: &HashSet<SocketAddr>,
|
||||||
|
) -> usize {
|
||||||
|
let ws = self.writers.read().await;
|
||||||
|
ws.iter()
|
||||||
|
.filter(|w| !w.draining.load(Ordering::Relaxed))
|
||||||
|
.filter(|w| w.generation == generation)
|
||||||
|
.filter(|w| endpoints.contains(&w.addr))
|
||||||
|
.count()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) async fn connect_endpoints_round_robin(
|
||||||
|
self: &Arc<Self>,
|
||||||
|
endpoints: &[SocketAddr],
|
||||||
|
rng: &SecureRandom,
|
||||||
|
) -> bool {
|
||||||
|
if endpoints.is_empty() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
let start = (self.rr.fetch_add(1, Ordering::Relaxed) as usize) % endpoints.len();
|
||||||
|
for offset in 0..endpoints.len() {
|
||||||
|
let idx = (start + offset) % endpoints.len();
|
||||||
|
let addr = endpoints[idx];
|
||||||
|
match self.connect_one(addr, rng).await {
|
||||||
|
Ok(()) => return true,
|
||||||
|
Err(e) => debug!(%addr, error = %e, "ME connect failed during round-robin warmup"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn warmup_generation_for_all_dcs(
|
||||||
|
self: &Arc<Self>,
|
||||||
|
rng: &SecureRandom,
|
||||||
|
generation: u64,
|
||||||
|
desired_by_dc: &HashMap<i32, HashSet<SocketAddr>>,
|
||||||
|
) {
|
||||||
|
let extra_passes = self
|
||||||
|
.me_hardswap_warmup_extra_passes
|
||||||
|
.load(Ordering::Relaxed)
|
||||||
|
.min(10) as usize;
|
||||||
|
let total_passes = 1 + extra_passes;
|
||||||
|
|
||||||
|
for (dc, endpoints) in desired_by_dc {
|
||||||
|
if endpoints.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut endpoint_list: Vec<SocketAddr> = endpoints.iter().copied().collect();
|
||||||
|
endpoint_list.sort_unstable();
|
||||||
|
let required = Self::required_writers_for_dc(endpoint_list.len());
|
||||||
|
let mut completed = false;
|
||||||
|
let mut last_fresh_count = self
|
||||||
|
.fresh_writer_count_for_endpoints(generation, endpoints)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
for pass_idx in 0..total_passes {
|
||||||
|
if last_fresh_count >= required {
|
||||||
|
completed = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
let missing = required.saturating_sub(last_fresh_count);
|
||||||
|
debug!(
|
||||||
|
dc = *dc,
|
||||||
|
pass = pass_idx + 1,
|
||||||
|
total_passes,
|
||||||
|
fresh_count = last_fresh_count,
|
||||||
|
required,
|
||||||
|
missing,
|
||||||
|
endpoint_count = endpoint_list.len(),
|
||||||
|
"ME hardswap warmup pass started"
|
||||||
|
);
|
||||||
|
|
||||||
|
for attempt_idx in 0..missing {
|
||||||
|
let delay_ms = self.hardswap_warmup_connect_delay_ms();
|
||||||
|
tokio::time::sleep(Duration::from_millis(delay_ms)).await;
|
||||||
|
|
||||||
|
let connected = self.connect_endpoints_round_robin(&endpoint_list, rng).await;
|
||||||
|
debug!(
|
||||||
|
dc = *dc,
|
||||||
|
pass = pass_idx + 1,
|
||||||
|
total_passes,
|
||||||
|
attempt = attempt_idx + 1,
|
||||||
|
delay_ms,
|
||||||
|
connected,
|
||||||
|
"ME hardswap warmup connect attempt finished"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
last_fresh_count = self
|
||||||
|
.fresh_writer_count_for_endpoints(generation, endpoints)
|
||||||
|
.await;
|
||||||
|
if last_fresh_count >= required {
|
||||||
|
completed = true;
|
||||||
|
info!(
|
||||||
|
dc = *dc,
|
||||||
|
pass = pass_idx + 1,
|
||||||
|
total_passes,
|
||||||
|
fresh_count = last_fresh_count,
|
||||||
|
required,
|
||||||
|
"ME hardswap warmup floor reached for DC"
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if pass_idx + 1 < total_passes {
|
||||||
|
let backoff_ms = self.hardswap_warmup_backoff_ms(pass_idx);
|
||||||
|
debug!(
|
||||||
|
dc = *dc,
|
||||||
|
pass = pass_idx + 1,
|
||||||
|
total_passes,
|
||||||
|
fresh_count = last_fresh_count,
|
||||||
|
required,
|
||||||
|
backoff_ms,
|
||||||
|
"ME hardswap warmup pass incomplete, delaying next pass"
|
||||||
|
);
|
||||||
|
tokio::time::sleep(Duration::from_millis(backoff_ms)).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !completed {
|
||||||
|
warn!(
|
||||||
|
dc = *dc,
|
||||||
|
fresh_count = last_fresh_count,
|
||||||
|
required,
|
||||||
|
endpoint_count = endpoint_list.len(),
|
||||||
|
total_passes,
|
||||||
|
"ME warmup stopped: unable to reach required writer floor for DC"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn zero_downtime_reinit_after_map_change(
|
||||||
|
self: &Arc<Self>,
|
||||||
|
rng: &SecureRandom,
|
||||||
|
) {
|
||||||
|
let desired_by_dc = self.desired_dc_endpoints().await;
|
||||||
|
if desired_by_dc.is_empty() {
|
||||||
|
warn!("ME endpoint map is empty; skipping stale writer drain");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let previous_generation = self.current_generation();
|
||||||
|
let generation = self.generation.fetch_add(1, Ordering::Relaxed) + 1;
|
||||||
|
let hardswap = self.hardswap.load(Ordering::Relaxed);
|
||||||
|
|
||||||
|
if hardswap {
|
||||||
|
self.warmup_generation_for_all_dcs(rng, generation, &desired_by_dc)
|
||||||
|
.await;
|
||||||
|
} else {
|
||||||
|
self.reconcile_connections(rng).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
let writers = self.writers.read().await;
|
||||||
|
let active_writer_addrs: HashSet<SocketAddr> = writers
|
||||||
|
.iter()
|
||||||
|
.filter(|w| !w.draining.load(Ordering::Relaxed))
|
||||||
|
.map(|w| w.addr)
|
||||||
|
.collect();
|
||||||
|
let min_ratio = Self::permille_to_ratio(
|
||||||
|
self.me_pool_min_fresh_ratio_permille
|
||||||
|
.load(Ordering::Relaxed),
|
||||||
|
);
|
||||||
|
let (coverage_ratio, missing_dc) = Self::coverage_ratio(&desired_by_dc, &active_writer_addrs);
|
||||||
|
if !hardswap && coverage_ratio < min_ratio {
|
||||||
|
warn!(
|
||||||
|
previous_generation,
|
||||||
|
generation,
|
||||||
|
coverage_ratio = format_args!("{coverage_ratio:.3}"),
|
||||||
|
min_ratio = format_args!("{min_ratio:.3}"),
|
||||||
|
missing_dc = ?missing_dc,
|
||||||
|
"ME reinit coverage below threshold; keeping stale writers"
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if hardswap {
|
||||||
|
let mut fresh_missing_dc = Vec::<(i32, usize, usize)>::new();
|
||||||
|
for (dc, endpoints) in &desired_by_dc {
|
||||||
|
if endpoints.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let required = Self::required_writers_for_dc(endpoints.len());
|
||||||
|
let fresh_count = writers
|
||||||
|
.iter()
|
||||||
|
.filter(|w| !w.draining.load(Ordering::Relaxed))
|
||||||
|
.filter(|w| w.generation == generation)
|
||||||
|
.filter(|w| endpoints.contains(&w.addr))
|
||||||
|
.count();
|
||||||
|
if fresh_count < required {
|
||||||
|
fresh_missing_dc.push((*dc, fresh_count, required));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !fresh_missing_dc.is_empty() {
|
||||||
|
warn!(
|
||||||
|
previous_generation,
|
||||||
|
generation,
|
||||||
|
missing_dc = ?fresh_missing_dc,
|
||||||
|
"ME hardswap pending: fresh generation coverage incomplete"
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else if !missing_dc.is_empty() {
|
||||||
|
warn!(
|
||||||
|
missing_dc = ?missing_dc,
|
||||||
|
// Keep stale writers alive when fresh coverage is incomplete.
|
||||||
|
"ME reinit coverage incomplete; keeping stale writers"
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let desired_addrs: HashSet<SocketAddr> = desired_by_dc
|
||||||
|
.values()
|
||||||
|
.flat_map(|set| set.iter().copied())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let stale_writer_ids: Vec<u64> = writers
|
||||||
|
.iter()
|
||||||
|
.filter(|w| !w.draining.load(Ordering::Relaxed))
|
||||||
|
.filter(|w| {
|
||||||
|
if hardswap {
|
||||||
|
w.generation < generation
|
||||||
|
} else {
|
||||||
|
!desired_addrs.contains(&w.addr)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.map(|w| w.id)
|
||||||
|
.collect();
|
||||||
|
drop(writers);
|
||||||
|
|
||||||
|
if stale_writer_ids.is_empty() {
|
||||||
|
debug!("ME reinit cycle completed with no stale writers");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let drain_timeout = self.force_close_timeout();
|
||||||
|
let drain_timeout_secs = drain_timeout.map(|d| d.as_secs()).unwrap_or(0);
|
||||||
|
info!(
|
||||||
|
stale_writers = stale_writer_ids.len(),
|
||||||
|
previous_generation,
|
||||||
|
generation,
|
||||||
|
hardswap,
|
||||||
|
coverage_ratio = format_args!("{coverage_ratio:.3}"),
|
||||||
|
min_ratio = format_args!("{min_ratio:.3}"),
|
||||||
|
drain_timeout_secs,
|
||||||
|
"ME reinit cycle covered; draining stale writers"
|
||||||
|
);
|
||||||
|
self.stats.increment_pool_swap_total();
|
||||||
|
for writer_id in stale_writer_ids {
|
||||||
|
self.mark_writer_draining_with_timeout(writer_id, drain_timeout, !hardswap)
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn zero_downtime_reinit_periodic(
|
||||||
|
self: &Arc<Self>,
|
||||||
|
rng: &SecureRandom,
|
||||||
|
) {
|
||||||
|
self.zero_downtime_reinit_after_map_change(rng).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn endpoints_for_same_dc(&self, addr: SocketAddr) -> Vec<SocketAddr> {
|
||||||
|
let mut target_dc = HashSet::<i32>::new();
|
||||||
|
let mut endpoints = HashSet::<SocketAddr>::new();
|
||||||
|
|
||||||
|
if self.decision.ipv4_me {
|
||||||
|
let map = self.proxy_map_v4.read().await.clone();
|
||||||
|
for (dc, addrs) in &map {
|
||||||
|
if addrs
|
||||||
|
.iter()
|
||||||
|
.any(|(ip, port)| SocketAddr::new(*ip, *port) == addr)
|
||||||
|
{
|
||||||
|
target_dc.insert(dc.abs());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for dc in &target_dc {
|
||||||
|
for key in [*dc, -*dc] {
|
||||||
|
if let Some(addrs) = map.get(&key) {
|
||||||
|
for (ip, port) in addrs {
|
||||||
|
endpoints.insert(SocketAddr::new(*ip, *port));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.decision.ipv6_me {
|
||||||
|
let map = self.proxy_map_v6.read().await.clone();
|
||||||
|
for (dc, addrs) in &map {
|
||||||
|
if addrs
|
||||||
|
.iter()
|
||||||
|
.any(|(ip, port)| SocketAddr::new(*ip, *port) == addr)
|
||||||
|
{
|
||||||
|
target_dc.insert(dc.abs());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for dc in &target_dc {
|
||||||
|
for key in [*dc, -*dc] {
|
||||||
|
if let Some(addrs) = map.get(&key) {
|
||||||
|
for (ip, port) in addrs {
|
||||||
|
endpoints.insert(SocketAddr::new(*ip, *port));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut sorted: Vec<SocketAddr> = endpoints.into_iter().collect();
|
||||||
|
sorted.sort_unstable();
|
||||||
|
sorted
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn refill_writer_after_loss(self: &Arc<Self>, addr: SocketAddr) -> bool {
|
||||||
|
let fast_retries = self.me_reconnect_fast_retry_count.max(1);
|
||||||
|
|
||||||
|
for attempt in 0..fast_retries {
|
||||||
|
self.stats.increment_me_reconnect_attempt();
|
||||||
|
match self.connect_one(addr, self.rng.as_ref()).await {
|
||||||
|
Ok(()) => {
|
||||||
|
self.stats.increment_me_reconnect_success();
|
||||||
|
self.stats.increment_me_writer_restored_same_endpoint_total();
|
||||||
|
info!(
|
||||||
|
%addr,
|
||||||
|
attempt = attempt + 1,
|
||||||
|
"ME writer restored on the same endpoint"
|
||||||
|
);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
debug!(
|
||||||
|
%addr,
|
||||||
|
attempt = attempt + 1,
|
||||||
|
error = %e,
|
||||||
|
"ME immediate same-endpoint reconnect failed"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let dc_endpoints = self.endpoints_for_same_dc(addr).await;
|
||||||
|
if dc_endpoints.is_empty() {
|
||||||
|
self.stats.increment_me_refill_failed_total();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for attempt in 0..fast_retries {
|
||||||
|
self.stats.increment_me_reconnect_attempt();
|
||||||
|
if self
|
||||||
|
.connect_endpoints_round_robin(&dc_endpoints, self.rng.as_ref())
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
self.stats.increment_me_reconnect_success();
|
||||||
|
self.stats.increment_me_writer_restored_fallback_total();
|
||||||
|
info!(
|
||||||
|
%addr,
|
||||||
|
attempt = attempt + 1,
|
||||||
|
"ME writer restored via DC fallback endpoint"
|
||||||
|
);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.stats.increment_me_refill_failed_total();
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn trigger_immediate_refill(self: &Arc<Self>, addr: SocketAddr) {
|
||||||
|
let pool = Arc::clone(self);
|
||||||
|
tokio::spawn(async move {
|
||||||
|
{
|
||||||
|
let mut guard = pool.refill_inflight.lock().await;
|
||||||
|
if !guard.insert(addr) {
|
||||||
|
pool.stats.increment_me_refill_skipped_inflight_total();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pool.stats.increment_me_refill_triggered_total();
|
||||||
|
|
||||||
|
let restored = pool.refill_writer_after_loss(addr).await;
|
||||||
|
if !restored {
|
||||||
|
warn!(%addr, "ME immediate refill failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut guard = pool.refill_inflight.lock().await;
|
||||||
|
guard.remove(&addr);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn update_proxy_maps(
|
pub async fn update_proxy_maps(
|
||||||
&self,
|
&self,
|
||||||
new_v4: HashMap<i32, Vec<(IpAddr, u16)>>,
|
new_v4: HashMap<i32, Vec<(IpAddr, u16)>>,
|
||||||
@@ -235,10 +801,10 @@ impl MePool {
|
|||||||
let mut guard = self.proxy_map_v4.write().await;
|
let mut guard = self.proxy_map_v4.write().await;
|
||||||
let keys: Vec<i32> = guard.keys().cloned().collect();
|
let keys: Vec<i32> = guard.keys().cloned().collect();
|
||||||
for k in keys.iter().cloned().filter(|k| *k > 0) {
|
for k in keys.iter().cloned().filter(|k| *k > 0) {
|
||||||
if !guard.contains_key(&-k) {
|
if !guard.contains_key(&-k)
|
||||||
if let Some(addrs) = guard.get(&k).cloned() {
|
&& let Some(addrs) = guard.get(&k).cloned()
|
||||||
guard.insert(-k, addrs);
|
{
|
||||||
}
|
guard.insert(-k, addrs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -246,10 +812,10 @@ impl MePool {
|
|||||||
let mut guard = self.proxy_map_v6.write().await;
|
let mut guard = self.proxy_map_v6.write().await;
|
||||||
let keys: Vec<i32> = guard.keys().cloned().collect();
|
let keys: Vec<i32> = guard.keys().cloned().collect();
|
||||||
for k in keys.iter().cloned().filter(|k| *k > 0) {
|
for k in keys.iter().cloned().filter(|k| *k > 0) {
|
||||||
if !guard.contains_key(&-k) {
|
if !guard.contains_key(&-k)
|
||||||
if let Some(addrs) = guard.get(&k).cloned() {
|
&& let Some(addrs) = guard.get(&k).cloned()
|
||||||
guard.insert(-k, addrs);
|
{
|
||||||
}
|
guard.insert(-k, addrs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -411,9 +977,12 @@ impl MePool {
|
|||||||
let hs = self.handshake_only(stream, addr, rng).await?;
|
let hs = self.handshake_only(stream, addr, rng).await?;
|
||||||
|
|
||||||
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 generation = self.current_generation();
|
||||||
let cancel = CancellationToken::new();
|
let cancel = CancellationToken::new();
|
||||||
let degraded = Arc::new(AtomicBool::new(false));
|
let degraded = Arc::new(AtomicBool::new(false));
|
||||||
let draining = Arc::new(AtomicBool::new(false));
|
let draining = Arc::new(AtomicBool::new(false));
|
||||||
|
let draining_started_at_epoch_secs = Arc::new(AtomicU64::new(0));
|
||||||
|
let allow_drain_fallback = Arc::new(AtomicBool::new(false));
|
||||||
let (tx, mut rx) = mpsc::channel::<WriterCommand>(4096);
|
let (tx, mut rx) = mpsc::channel::<WriterCommand>(4096);
|
||||||
let mut rpc_writer = RpcWriter {
|
let mut rpc_writer = RpcWriter {
|
||||||
writer: hs.wr,
|
writer: hs.wr,
|
||||||
@@ -444,10 +1013,13 @@ impl MePool {
|
|||||||
let writer = MeWriter {
|
let writer = MeWriter {
|
||||||
id: writer_id,
|
id: writer_id,
|
||||||
addr,
|
addr,
|
||||||
|
generation,
|
||||||
tx: tx.clone(),
|
tx: tx.clone(),
|
||||||
cancel: cancel.clone(),
|
cancel: cancel.clone(),
|
||||||
degraded: degraded.clone(),
|
degraded: degraded.clone(),
|
||||||
draining: draining.clone(),
|
draining: draining.clone(),
|
||||||
|
draining_started_at_epoch_secs: draining_started_at_epoch_secs.clone(),
|
||||||
|
allow_drain_fallback: allow_drain_fallback.clone(),
|
||||||
};
|
};
|
||||||
self.writers.write().await.push(writer.clone());
|
self.writers.write().await.push(writer.clone());
|
||||||
self.conn_count.fetch_add(1, Ordering::Relaxed);
|
self.conn_count.fetch_add(1, Ordering::Relaxed);
|
||||||
@@ -491,13 +1063,12 @@ impl MePool {
|
|||||||
cancel_reader_token.clone(),
|
cancel_reader_token.clone(),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
if let Some(pool) = pool.upgrade() {
|
if let Some(pool) = pool.upgrade()
|
||||||
if cleanup_for_reader
|
&& cleanup_for_reader
|
||||||
.compare_exchange(false, true, Ordering::AcqRel, Ordering::Relaxed)
|
.compare_exchange(false, true, Ordering::AcqRel, Ordering::Relaxed)
|
||||||
.is_ok()
|
.is_ok()
|
||||||
{
|
{
|
||||||
pool.remove_writer_and_close_clients(writer_id).await;
|
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");
|
||||||
@@ -565,13 +1136,12 @@ impl MePool {
|
|||||||
stats_ping.increment_me_keepalive_failed();
|
stats_ping.increment_me_keepalive_failed();
|
||||||
debug!("ME ping failed, removing dead writer");
|
debug!("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()
|
||||||
if cleanup_for_ping
|
&& cleanup_for_ping
|
||||||
.compare_exchange(false, true, Ordering::AcqRel, Ordering::Relaxed)
|
.compare_exchange(false, true, Ordering::AcqRel, Ordering::Relaxed)
|
||||||
.is_ok()
|
.is_ok()
|
||||||
{
|
{
|
||||||
pool.remove_writer_and_close_clients(writer_id).await;
|
pool.remove_writer_and_close_clients(writer_id).await;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -613,13 +1183,25 @@ impl MePool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn remove_writer_only(&self, writer_id: u64) -> Vec<BoundConn> {
|
async fn remove_writer_only(self: &Arc<Self>, writer_id: u64) -> Vec<BoundConn> {
|
||||||
let mut close_tx: Option<mpsc::Sender<WriterCommand>> = None;
|
let mut close_tx: Option<mpsc::Sender<WriterCommand>> = None;
|
||||||
|
let mut removed_addr: Option<SocketAddr> = None;
|
||||||
|
let mut trigger_refill = false;
|
||||||
{
|
{
|
||||||
let mut ws = self.writers.write().await;
|
let mut ws = self.writers.write().await;
|
||||||
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);
|
||||||
|
let was_draining = w.draining.load(Ordering::Relaxed);
|
||||||
|
if was_draining {
|
||||||
|
self.stats.decrement_pool_drain_active();
|
||||||
|
}
|
||||||
|
self.stats.increment_me_writer_removed_total();
|
||||||
w.cancel.cancel();
|
w.cancel.cancel();
|
||||||
|
removed_addr = Some(w.addr);
|
||||||
|
trigger_refill = !was_draining;
|
||||||
|
if trigger_refill {
|
||||||
|
self.stats.increment_me_writer_removed_unexpected_total();
|
||||||
|
}
|
||||||
close_tx = Some(w.tx.clone());
|
close_tx = Some(w.tx.clone());
|
||||||
self.conn_count.fetch_sub(1, Ordering::Relaxed);
|
self.conn_count.fetch_sub(1, Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
@@ -627,42 +1209,102 @@ impl MePool {
|
|||||||
if let Some(tx) = close_tx {
|
if let Some(tx) = close_tx {
|
||||||
let _ = tx.send(WriterCommand::Close).await;
|
let _ = tx.send(WriterCommand::Close).await;
|
||||||
}
|
}
|
||||||
|
if trigger_refill
|
||||||
|
&& let Some(addr) = removed_addr
|
||||||
|
{
|
||||||
|
self.trigger_immediate_refill(addr);
|
||||||
|
}
|
||||||
self.rtt_stats.lock().await.remove(&writer_id);
|
self.rtt_stats.lock().await.remove(&writer_id);
|
||||||
self.registry.writer_lost(writer_id).await
|
self.registry.writer_lost(writer_id).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn mark_writer_draining(self: &Arc<Self>, writer_id: u64) {
|
pub(crate) async fn mark_writer_draining_with_timeout(
|
||||||
{
|
self: &Arc<Self>,
|
||||||
|
writer_id: u64,
|
||||||
|
timeout: Option<Duration>,
|
||||||
|
allow_drain_fallback: bool,
|
||||||
|
) {
|
||||||
|
let timeout = timeout.filter(|d| !d.is_zero());
|
||||||
|
let found = {
|
||||||
let mut ws = self.writers.write().await;
|
let mut ws = self.writers.write().await;
|
||||||
if let Some(w) = ws.iter_mut().find(|w| w.id == writer_id) {
|
if let Some(w) = ws.iter_mut().find(|w| w.id == writer_id) {
|
||||||
|
let already_draining = w.draining.swap(true, Ordering::Relaxed);
|
||||||
|
w.allow_drain_fallback
|
||||||
|
.store(allow_drain_fallback, Ordering::Relaxed);
|
||||||
|
w.draining_started_at_epoch_secs
|
||||||
|
.store(Self::now_epoch_secs(), Ordering::Relaxed);
|
||||||
|
if !already_draining {
|
||||||
|
self.stats.increment_pool_drain_active();
|
||||||
|
}
|
||||||
w.draining.store(true, Ordering::Relaxed);
|
w.draining.store(true, Ordering::Relaxed);
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if !found {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let timeout_secs = timeout.map(|d| d.as_secs()).unwrap_or(0);
|
||||||
|
debug!(
|
||||||
|
writer_id,
|
||||||
|
timeout_secs,
|
||||||
|
allow_drain_fallback,
|
||||||
|
"ME writer marked draining"
|
||||||
|
);
|
||||||
|
|
||||||
let pool = Arc::downgrade(self);
|
let pool = Arc::downgrade(self);
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
let deadline = Instant::now() + Duration::from_secs(300);
|
let deadline = timeout.map(|t| Instant::now() + t);
|
||||||
loop {
|
while let Some(p) = pool.upgrade() {
|
||||||
if let Some(p) = pool.upgrade() {
|
if let Some(deadline_at) = deadline
|
||||||
if Instant::now() >= deadline {
|
&& Instant::now() >= deadline_at
|
||||||
warn!(writer_id, "Drain timeout, force-closing");
|
{
|
||||||
let _ = p.remove_writer_and_close_clients(writer_id).await;
|
warn!(writer_id, "Drain timeout, force-closing");
|
||||||
break;
|
p.stats.increment_pool_force_close_total();
|
||||||
}
|
let _ = p.remove_writer_and_close_clients(writer_id).await;
|
||||||
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;
|
break;
|
||||||
}
|
}
|
||||||
|
if p.registry.is_writer_empty(writer_id).await {
|
||||||
|
let _ = p.remove_writer_only(writer_id).await;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn mark_writer_draining(self: &Arc<Self>, writer_id: u64) {
|
||||||
|
self.mark_writer_draining_with_timeout(writer_id, Some(Duration::from_secs(300)), false)
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) fn writer_accepts_new_binding(&self, writer: &MeWriter) -> bool {
|
||||||
|
if !writer.draining.load(Ordering::Relaxed) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if !writer.allow_drain_fallback.load(Ordering::Relaxed) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
let ttl_secs = self.me_pool_drain_ttl_secs.load(Ordering::Relaxed);
|
||||||
|
if ttl_secs == 0 {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
let started = writer.draining_started_at_epoch_secs.load(Ordering::Relaxed);
|
||||||
|
if started == 0 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Self::now_epoch_secs().saturating_sub(started) <= ttl_secs
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
fn hex_dump(data: &[u8]) -> String {
|
fn hex_dump(data: &[u8]) -> String {
|
||||||
const MAX: usize = 64;
|
const MAX: usize = 64;
|
||||||
let mut out = String::with_capacity(data.len() * 2 + 3);
|
let mut out = String::with_capacity(data.len() * 2 + 3);
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use std::net::{IpAddr, Ipv4Addr};
|
use std::net::{IpAddr, Ipv4Addr};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use tracing::{info, warn, debug};
|
use tracing::{info, warn};
|
||||||
|
|
||||||
use crate::error::{ProxyError, Result};
|
use crate::error::{ProxyError, Result};
|
||||||
use crate::network::probe::is_bogon;
|
use crate::network::probe::is_bogon;
|
||||||
@@ -9,11 +9,14 @@ use crate::network::stun::{stun_probe_dual, IpFamily, StunProbeResult};
|
|||||||
|
|
||||||
use super::MePool;
|
use super::MePool;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
pub async fn stun_probe(stun_addr: Option<String>) -> Result<crate::network::stun::DualStunResult> {
|
pub async fn stun_probe(stun_addr: Option<String>) -> Result<crate::network::stun::DualStunResult> {
|
||||||
let stun_addr = stun_addr.unwrap_or_else(|| "stun.l.google.com:19302".to_string());
|
let stun_addr = stun_addr.unwrap_or_else(|| "stun.l.google.com:19302".to_string());
|
||||||
stun_probe_dual(&stun_addr).await
|
stun_probe_dual(&stun_addr).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
pub async fn detect_public_ip() -> Option<IpAddr> {
|
pub async fn detect_public_ip() -> Option<IpAddr> {
|
||||||
fetch_public_ipv4_with_retry().await.ok().flatten().map(IpAddr::V4)
|
fetch_public_ipv4_with_retry().await.ok().flatten().map(IpAddr::V4)
|
||||||
}
|
}
|
||||||
@@ -22,7 +25,7 @@ impl MePool {
|
|||||||
pub(super) fn translate_ip_for_nat(&self, ip: IpAddr) -> IpAddr {
|
pub(super) fn translate_ip_for_nat(&self, ip: IpAddr) -> IpAddr {
|
||||||
let nat_ip = self
|
let nat_ip = self
|
||||||
.nat_ip_cfg
|
.nat_ip_cfg
|
||||||
.or_else(|| self.nat_ip_detected.try_read().ok().and_then(|g| (*g).clone()));
|
.or_else(|| self.nat_ip_detected.try_read().ok().and_then(|g| *g));
|
||||||
|
|
||||||
let Some(nat_ip) = nat_ip else {
|
let Some(nat_ip) = nat_ip else {
|
||||||
return ip;
|
return ip;
|
||||||
@@ -72,7 +75,7 @@ impl MePool {
|
|||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(ip) = self.nat_ip_detected.read().await.clone() {
|
if let Some(ip) = *self.nat_ip_detected.read().await {
|
||||||
return Some(ip);
|
return Some(ip);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,17 +102,17 @@ impl MePool {
|
|||||||
) -> Option<std::net::SocketAddr> {
|
) -> Option<std::net::SocketAddr> {
|
||||||
const STUN_CACHE_TTL: Duration = Duration::from_secs(600);
|
const STUN_CACHE_TTL: Duration = Duration::from_secs(600);
|
||||||
// Backoff window
|
// Backoff window
|
||||||
if let Some(until) = *self.stun_backoff_until.read().await {
|
if let Some(until) = *self.stun_backoff_until.read().await
|
||||||
if Instant::now() < until {
|
&& Instant::now() < until
|
||||||
if let Ok(cache) = self.nat_reflection_cache.try_lock() {
|
{
|
||||||
let slot = match family {
|
if let Ok(cache) = self.nat_reflection_cache.try_lock() {
|
||||||
IpFamily::V4 => cache.v4,
|
let slot = match family {
|
||||||
IpFamily::V6 => cache.v6,
|
IpFamily::V4 => cache.v4,
|
||||||
};
|
IpFamily::V6 => cache.v6,
|
||||||
return slot.map(|(_, addr)| addr);
|
};
|
||||||
}
|
return slot.map(|(_, addr)| addr);
|
||||||
return None;
|
|
||||||
}
|
}
|
||||||
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Ok(mut cache) = self.nat_reflection_cache.try_lock() {
|
if let Ok(mut cache) = self.nat_reflection_cache.try_lock() {
|
||||||
@@ -117,10 +120,10 @@ impl MePool {
|
|||||||
IpFamily::V4 => &mut cache.v4,
|
IpFamily::V4 => &mut cache.v4,
|
||||||
IpFamily::V6 => &mut cache.v6,
|
IpFamily::V6 => &mut cache.v6,
|
||||||
};
|
};
|
||||||
if let Some((ts, addr)) = slot {
|
if let Some((ts, addr)) = slot
|
||||||
if ts.elapsed() < STUN_CACHE_TTL {
|
&& ts.elapsed() < STUN_CACHE_TTL
|
||||||
return Some(*addr);
|
{
|
||||||
}
|
return Some(*addr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ pub enum RouteResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
#[allow(dead_code)]
|
||||||
pub struct ConnMeta {
|
pub struct ConnMeta {
|
||||||
pub target_dc: i16,
|
pub target_dc: i16,
|
||||||
pub client_addr: SocketAddr,
|
pub client_addr: SocketAddr,
|
||||||
@@ -29,6 +30,7 @@ pub struct ConnMeta {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
#[allow(dead_code)]
|
||||||
pub struct BoundConn {
|
pub struct BoundConn {
|
||||||
pub conn_id: u64,
|
pub conn_id: u64,
|
||||||
pub meta: ConnMeta,
|
pub meta: ConnMeta,
|
||||||
@@ -167,6 +169,7 @@ impl ConnRegistry {
|
|||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
pub async fn get_meta(&self, conn_id: u64) -> Option<ConnMeta> {
|
pub async fn get_meta(&self, conn_id: u64) -> Option<ConnMeta> {
|
||||||
let inner = self.inner.read().await;
|
let inner = self.inner.read().await;
|
||||||
inner.meta.get(&conn_id).cloned()
|
inner.meta.get(&conn_id).cloned()
|
||||||
|
|||||||
@@ -1,50 +1,87 @@
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::sync::atomic::Ordering;
|
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use tokio::sync::watch;
|
||||||
use tracing::{info, warn};
|
use tracing::{info, warn};
|
||||||
|
|
||||||
|
use crate::config::ProxyConfig;
|
||||||
use crate::crypto::SecureRandom;
|
use crate::crypto::SecureRandom;
|
||||||
|
|
||||||
use super::MePool;
|
use super::MePool;
|
||||||
|
|
||||||
/// Periodically refresh ME connections to avoid long-lived degradation.
|
/// Periodically reinitialize ME generations and swap them after full warmup.
|
||||||
pub async fn me_rotation_task(pool: Arc<MePool>, rng: Arc<SecureRandom>, interval: Duration) {
|
pub async fn me_rotation_task(
|
||||||
let interval = interval.max(Duration::from_secs(600));
|
pool: Arc<MePool>,
|
||||||
|
rng: Arc<SecureRandom>,
|
||||||
|
mut config_rx: watch::Receiver<Arc<ProxyConfig>>,
|
||||||
|
) {
|
||||||
|
let mut interval_secs = config_rx
|
||||||
|
.borrow()
|
||||||
|
.general
|
||||||
|
.effective_me_reinit_every_secs()
|
||||||
|
.max(1);
|
||||||
|
let mut interval = Duration::from_secs(interval_secs);
|
||||||
|
let mut next_tick = tokio::time::Instant::now() + interval;
|
||||||
|
|
||||||
|
info!(interval_secs, "ME periodic reinit task started");
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
tokio::time::sleep(interval).await;
|
let sleep = tokio::time::sleep_until(next_tick);
|
||||||
|
tokio::pin!(sleep);
|
||||||
|
|
||||||
let candidate = {
|
tokio::select! {
|
||||||
let ws = pool.writers.read().await;
|
_ = &mut sleep => {
|
||||||
if ws.is_empty() {
|
pool.zero_downtime_reinit_periodic(rng.as_ref()).await;
|
||||||
None
|
let refreshed_secs = config_rx
|
||||||
} else {
|
.borrow()
|
||||||
let idx = (pool.rr.load(std::sync::atomic::Ordering::Relaxed) as usize) % ws.len();
|
.general
|
||||||
ws.get(idx).cloned()
|
.effective_me_reinit_every_secs()
|
||||||
}
|
.max(1);
|
||||||
};
|
if refreshed_secs != interval_secs {
|
||||||
|
info!(
|
||||||
let Some(w) = candidate else {
|
old_me_reinit_every_secs = interval_secs,
|
||||||
continue;
|
new_me_reinit_every_secs = refreshed_secs,
|
||||||
};
|
"ME periodic reinit interval changed"
|
||||||
|
);
|
||||||
info!(addr = %w.addr, writer_id = w.id, "Rotating ME connection");
|
interval_secs = refreshed_secs;
|
||||||
match pool.connect_one(w.addr, rng.as_ref()).await {
|
interval = Duration::from_secs(interval_secs);
|
||||||
Ok(()) => {
|
|
||||||
tokio::time::sleep(Duration::from_secs(2)).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");
|
|
||||||
}
|
}
|
||||||
|
next_tick = tokio::time::Instant::now() + interval;
|
||||||
}
|
}
|
||||||
Err(e) => {
|
changed = config_rx.changed() => {
|
||||||
warn!(addr = %w.addr, writer_id = w.id, error = %e, "ME rotation connect failed");
|
if changed.is_err() {
|
||||||
|
warn!("ME periodic reinit task stopped: config channel closed");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let new_secs = config_rx
|
||||||
|
.borrow()
|
||||||
|
.general
|
||||||
|
.effective_me_reinit_every_secs()
|
||||||
|
.max(1);
|
||||||
|
if new_secs == interval_secs {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if new_secs < interval_secs {
|
||||||
|
info!(
|
||||||
|
old_me_reinit_every_secs = interval_secs,
|
||||||
|
new_me_reinit_every_secs = new_secs,
|
||||||
|
"ME periodic reinit interval decreased, running immediate reinit"
|
||||||
|
);
|
||||||
|
interval_secs = new_secs;
|
||||||
|
interval = Duration::from_secs(interval_secs);
|
||||||
|
pool.zero_downtime_reinit_periodic(rng.as_ref()).await;
|
||||||
|
next_tick = tokio::time::Instant::now() + interval;
|
||||||
|
} else {
|
||||||
|
info!(
|
||||||
|
old_me_reinit_every_secs = interval_secs,
|
||||||
|
new_me_reinit_every_secs = new_secs,
|
||||||
|
"ME periodic reinit interval increased"
|
||||||
|
);
|
||||||
|
interval_secs = new_secs;
|
||||||
|
interval = Duration::from_secs(interval_secs);
|
||||||
|
next_tick = tokio::time::Instant::now() + interval;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,45 @@
|
|||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
use tracing::{debug, info, warn};
|
use tracing::{debug, info, warn};
|
||||||
use std::time::SystemTime;
|
use std::time::SystemTime;
|
||||||
use httpdate;
|
use httpdate;
|
||||||
|
|
||||||
use crate::error::{ProxyError, Result};
|
use crate::error::{ProxyError, Result};
|
||||||
|
|
||||||
|
pub const PROXY_SECRET_MIN_LEN: usize = 32;
|
||||||
|
|
||||||
|
pub(super) fn validate_proxy_secret_len(data_len: usize, max_len: usize) -> Result<()> {
|
||||||
|
if max_len < PROXY_SECRET_MIN_LEN {
|
||||||
|
return Err(ProxyError::Proxy(format!(
|
||||||
|
"proxy-secret max length is invalid: {} bytes (must be >= {})",
|
||||||
|
max_len,
|
||||||
|
PROXY_SECRET_MIN_LEN
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
if data_len < PROXY_SECRET_MIN_LEN {
|
||||||
|
return Err(ProxyError::Proxy(format!(
|
||||||
|
"proxy-secret too short: {} bytes (need >= {})",
|
||||||
|
data_len,
|
||||||
|
PROXY_SECRET_MIN_LEN
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
if data_len > max_len {
|
||||||
|
return Err(ProxyError::Proxy(format!(
|
||||||
|
"proxy-secret too long: {} bytes (limit = {})",
|
||||||
|
data_len,
|
||||||
|
max_len
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
/// Fetch Telegram proxy-secret binary.
|
/// Fetch Telegram proxy-secret binary.
|
||||||
pub async fn fetch_proxy_secret(cache_path: Option<&str>) -> Result<Vec<u8>> {
|
pub async fn fetch_proxy_secret(cache_path: Option<&str>, max_len: usize) -> Result<Vec<u8>> {
|
||||||
let cache = cache_path.unwrap_or("proxy-secret");
|
let cache = cache_path.unwrap_or("proxy-secret");
|
||||||
|
|
||||||
// 1) Try fresh download first.
|
// 1) Try fresh download first.
|
||||||
match download_proxy_secret().await {
|
match download_proxy_secret_with_max_len(max_len).await {
|
||||||
Ok(data) => {
|
Ok(data) => {
|
||||||
if let Err(e) = tokio::fs::write(cache, &data).await {
|
if let Err(e) = tokio::fs::write(cache, &data).await {
|
||||||
warn!(error = %e, "Failed to cache proxy-secret (non-fatal)");
|
warn!(error = %e, "Failed to cache proxy-secret (non-fatal)");
|
||||||
@@ -26,9 +54,9 @@ pub async fn fetch_proxy_secret(cache_path: Option<&str>) -> Result<Vec<u8>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2) Fallback to cache/file regardless of age; require len>=32.
|
// 2) Fallback to cache/file regardless of age; require len in bounds.
|
||||||
match tokio::fs::read(cache).await {
|
match tokio::fs::read(cache).await {
|
||||||
Ok(data) if data.len() >= 32 => {
|
Ok(data) if validate_proxy_secret_len(data.len(), max_len).is_ok() => {
|
||||||
let age_hours = tokio::fs::metadata(cache)
|
let age_hours = tokio::fs::metadata(cache)
|
||||||
.await
|
.await
|
||||||
.ok()
|
.ok()
|
||||||
@@ -43,17 +71,14 @@ pub async fn fetch_proxy_secret(cache_path: Option<&str>) -> Result<Vec<u8>> {
|
|||||||
);
|
);
|
||||||
Ok(data)
|
Ok(data)
|
||||||
}
|
}
|
||||||
Ok(data) => Err(ProxyError::Proxy(format!(
|
Ok(data) => validate_proxy_secret_len(data.len(), max_len).map(|_| data),
|
||||||
"Cached proxy-secret too short: {} bytes (need >= 32)",
|
|
||||||
data.len()
|
|
||||||
))),
|
|
||||||
Err(e) => Err(ProxyError::Proxy(format!(
|
Err(e) => Err(ProxyError::Proxy(format!(
|
||||||
"Failed to read proxy-secret cache after download failure: {e}"
|
"Failed to read proxy-secret cache after download failure: {e}"
|
||||||
))),
|
))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn download_proxy_secret() -> Result<Vec<u8>> {
|
pub async fn download_proxy_secret_with_max_len(max_len: usize) -> Result<Vec<u8>> {
|
||||||
let resp = reqwest::get("https://core.telegram.org/getProxySecret")
|
let resp = reqwest::get("https://core.telegram.org/getProxySecret")
|
||||||
.await
|
.await
|
||||||
.map_err(|e| ProxyError::Proxy(format!("Failed to download proxy-secret: {e}")))?;
|
.map_err(|e| ProxyError::Proxy(format!("Failed to download proxy-secret: {e}")))?;
|
||||||
@@ -65,20 +90,18 @@ pub async fn download_proxy_secret() -> Result<Vec<u8>> {
|
|||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(date) = resp.headers().get(reqwest::header::DATE) {
|
if let Some(date) = resp.headers().get(reqwest::header::DATE)
|
||||||
if let Ok(date_str) = date.to_str() {
|
&& let Ok(date_str) = date.to_str()
|
||||||
if let Ok(server_time) = httpdate::parse_http_date(date_str) {
|
&& let Ok(server_time) = httpdate::parse_http_date(date_str)
|
||||||
if let Ok(skew) = SystemTime::now().duration_since(server_time).or_else(|e| {
|
&& let Ok(skew) = SystemTime::now().duration_since(server_time).or_else(|e| {
|
||||||
server_time.duration_since(SystemTime::now()).map_err(|_| e)
|
server_time.duration_since(SystemTime::now()).map_err(|_| e)
|
||||||
}) {
|
})
|
||||||
let skew_secs = skew.as_secs();
|
{
|
||||||
if skew_secs > 60 {
|
let skew_secs = skew.as_secs();
|
||||||
warn!(skew_secs, "Time skew >60s detected from proxy-secret Date header");
|
if skew_secs > 60 {
|
||||||
} else if skew_secs > 30 {
|
warn!(skew_secs, "Time skew >60s detected from proxy-secret Date header");
|
||||||
warn!(skew_secs, "Time skew >30s detected from proxy-secret Date header");
|
} else if skew_secs > 30 {
|
||||||
}
|
warn!(skew_secs, "Time skew >30s detected from proxy-secret Date header");
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,12 +111,7 @@ pub async fn download_proxy_secret() -> Result<Vec<u8>> {
|
|||||||
.map_err(|e| ProxyError::Proxy(format!("Read proxy-secret body: {e}")))?
|
.map_err(|e| ProxyError::Proxy(format!("Read proxy-secret body: {e}")))?
|
||||||
.to_vec();
|
.to_vec();
|
||||||
|
|
||||||
if data.len() < 32 {
|
validate_proxy_secret_len(data.len(), max_len)?;
|
||||||
return Err(ProxyError::Proxy(format!(
|
|
||||||
"proxy-secret too short: {} bytes (need >= 32)",
|
|
||||||
data.len()
|
|
||||||
)));
|
|
||||||
}
|
|
||||||
|
|
||||||
info!(len = data.len(), "Downloaded proxy-secret OK");
|
info!(len = data.len(), "Downloaded proxy-secret OK");
|
||||||
Ok(data)
|
Ok(data)
|
||||||
|
|||||||
@@ -134,8 +134,8 @@ impl MePool {
|
|||||||
candidate_indices.sort_by_key(|idx| {
|
candidate_indices.sort_by_key(|idx| {
|
||||||
let w = &writers_snapshot[*idx];
|
let w = &writers_snapshot[*idx];
|
||||||
let degraded = w.degraded.load(Ordering::Relaxed);
|
let degraded = w.degraded.load(Ordering::Relaxed);
|
||||||
let draining = w.draining.load(Ordering::Relaxed);
|
let stale = (w.generation < self.current_generation()) as usize;
|
||||||
(draining as usize, degraded as usize)
|
(stale, degraded as usize)
|
||||||
});
|
});
|
||||||
|
|
||||||
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();
|
||||||
@@ -143,13 +143,23 @@ 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 w.draining.load(Ordering::Relaxed) {
|
if !self.writer_accepts_new_binding(w) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if w.tx.send(WriterCommand::Data(payload.clone())).await.is_ok() {
|
if w.tx.send(WriterCommand::Data(payload.clone())).await.is_ok() {
|
||||||
self.registry
|
self.registry
|
||||||
.bind_writer(conn_id, w.id, w.tx.clone(), meta.clone())
|
.bind_writer(conn_id, w.id, w.tx.clone(), meta.clone())
|
||||||
.await;
|
.await;
|
||||||
|
if w.generation < self.current_generation() {
|
||||||
|
self.stats.increment_pool_stale_pick_total();
|
||||||
|
debug!(
|
||||||
|
conn_id,
|
||||||
|
writer_id = w.id,
|
||||||
|
writer_generation = w.generation,
|
||||||
|
current_generation = self.current_generation(),
|
||||||
|
"Selected stale ME writer for fallback bind"
|
||||||
|
);
|
||||||
|
}
|
||||||
return Ok(());
|
return Ok(());
|
||||||
} else {
|
} else {
|
||||||
warn!(writer_id = w.id, "ME writer channel closed");
|
warn!(writer_id = w.id, "ME writer channel closed");
|
||||||
@@ -159,7 +169,7 @@ impl MePool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let w = writers_snapshot[candidate_indices[start]].clone();
|
let w = writers_snapshot[candidate_indices[start]].clone();
|
||||||
if w.draining.load(Ordering::Relaxed) {
|
if !self.writer_accepts_new_binding(&w) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
match w.tx.send(WriterCommand::Data(payload.clone())).await {
|
match w.tx.send(WriterCommand::Data(payload.clone())).await {
|
||||||
@@ -167,6 +177,9 @@ impl MePool {
|
|||||||
self.registry
|
self.registry
|
||||||
.bind_writer(conn_id, w.id, w.tx.clone(), meta.clone())
|
.bind_writer(conn_id, w.id, w.tx.clone(), meta.clone())
|
||||||
.await;
|
.await;
|
||||||
|
if w.generation < self.current_generation() {
|
||||||
|
self.stats.increment_pool_stale_pick_total();
|
||||||
|
}
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
@@ -229,10 +242,10 @@ impl MePool {
|
|||||||
}
|
}
|
||||||
if preferred.is_empty() {
|
if preferred.is_empty() {
|
||||||
let def = self.default_dc.load(Ordering::Relaxed);
|
let def = self.default_dc.load(Ordering::Relaxed);
|
||||||
if def != 0 {
|
if def != 0
|
||||||
if let Some(v) = map_guard.get(&def) {
|
&& let Some(v) = map_guard.get(&def)
|
||||||
preferred.extend(v.iter().map(|(ip, port)| SocketAddr::new(*ip, *port)));
|
{
|
||||||
}
|
preferred.extend(v.iter().map(|(ip, port)| SocketAddr::new(*ip, *port)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -245,22 +258,22 @@ impl MePool {
|
|||||||
|
|
||||||
if preferred.is_empty() {
|
if preferred.is_empty() {
|
||||||
return (0..writers.len())
|
return (0..writers.len())
|
||||||
.filter(|i| !writers[*i].draining.load(Ordering::Relaxed))
|
.filter(|i| self.writer_accepts_new_binding(&writers[*i]))
|
||||||
.collect();
|
.collect();
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut out = Vec::new();
|
let mut out = Vec::new();
|
||||||
for (idx, w) in writers.iter().enumerate() {
|
for (idx, w) in writers.iter().enumerate() {
|
||||||
if w.draining.load(Ordering::Relaxed) {
|
if !self.writer_accepts_new_binding(w) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if preferred.iter().any(|p| *p == w.addr) {
|
if preferred.contains(&w.addr) {
|
||||||
out.push(idx);
|
out.push(idx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if out.is_empty() {
|
if out.is_empty() {
|
||||||
return (0..writers.len())
|
return (0..writers.len())
|
||||||
.filter(|i| !writers[*i].draining.load(Ordering::Relaxed))
|
.filter(|i| self.writer_accepts_new_binding(&writers[*i]))
|
||||||
.collect();
|
.collect();
|
||||||
}
|
}
|
||||||
out
|
out
|
||||||
|
|||||||
@@ -6,9 +6,13 @@ pub mod socket;
|
|||||||
pub mod socks;
|
pub mod socks;
|
||||||
pub mod upstream;
|
pub mod upstream;
|
||||||
|
|
||||||
|
#[allow(unused_imports)]
|
||||||
pub use pool::ConnectionPool;
|
pub use pool::ConnectionPool;
|
||||||
|
#[allow(unused_imports)]
|
||||||
pub use proxy_protocol::{ProxyProtocolInfo, parse_proxy_protocol};
|
pub use proxy_protocol::{ProxyProtocolInfo, parse_proxy_protocol};
|
||||||
pub use socket::*;
|
pub use socket::*;
|
||||||
|
#[allow(unused_imports)]
|
||||||
pub use socks::*;
|
pub use socks::*;
|
||||||
|
#[allow(unused_imports)]
|
||||||
pub use upstream::{DcPingResult, StartupPingResult, UpstreamManager};
|
pub use upstream::{DcPingResult, StartupPingResult, UpstreamManager};
|
||||||
pub mod middle_proxy;
|
pub mod middle_proxy;
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
//! Connection Pool
|
//! Connection Pool
|
||||||
|
|
||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
@@ -8,7 +10,7 @@ use tokio::net::TcpStream;
|
|||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
use tokio::time::timeout;
|
use tokio::time::timeout;
|
||||||
use parking_lot::RwLock;
|
use parking_lot::RwLock;
|
||||||
use tracing::{debug, warn};
|
use tracing::debug;
|
||||||
use crate::error::{ProxyError, Result};
|
use crate::error::{ProxyError, Result};
|
||||||
use super::socket::configure_tcp_socket;
|
use super::socket::configure_tcp_socket;
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ mod address_family {
|
|||||||
|
|
||||||
/// Information extracted from PROXY protocol header
|
/// Information extracted from PROXY protocol header
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
#[allow(dead_code)]
|
||||||
pub struct ProxyProtocolInfo {
|
pub struct ProxyProtocolInfo {
|
||||||
/// Source (client) address
|
/// Source (client) address
|
||||||
pub src_addr: SocketAddr,
|
pub src_addr: SocketAddr,
|
||||||
@@ -37,6 +38,7 @@ pub struct ProxyProtocolInfo {
|
|||||||
pub version: u8,
|
pub version: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
impl ProxyProtocolInfo {
|
impl ProxyProtocolInfo {
|
||||||
/// Create info with just source address
|
/// Create info with just source address
|
||||||
pub fn new(src_addr: SocketAddr) -> Self {
|
pub fn new(src_addr: SocketAddr) -> Self {
|
||||||
@@ -231,12 +233,14 @@ async fn parse_v2<R: AsyncRead + Unpin>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Builder for PROXY protocol v1 header
|
/// Builder for PROXY protocol v1 header
|
||||||
|
#[allow(dead_code)]
|
||||||
pub struct ProxyProtocolV1Builder {
|
pub struct ProxyProtocolV1Builder {
|
||||||
family: &'static str,
|
family: &'static str,
|
||||||
src_addr: Option<SocketAddr>,
|
src_addr: Option<SocketAddr>,
|
||||||
dst_addr: Option<SocketAddr>,
|
dst_addr: Option<SocketAddr>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
impl ProxyProtocolV1Builder {
|
impl ProxyProtocolV1Builder {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
@@ -284,11 +288,13 @@ impl Default for ProxyProtocolV1Builder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Builder for PROXY protocol v2 header
|
/// Builder for PROXY protocol v2 header
|
||||||
|
#[allow(dead_code)]
|
||||||
pub struct ProxyProtocolV2Builder {
|
pub struct ProxyProtocolV2Builder {
|
||||||
src: Option<SocketAddr>,
|
src: Option<SocketAddr>,
|
||||||
dst: Option<SocketAddr>,
|
dst: Option<SocketAddr>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
impl ProxyProtocolV2Builder {
|
impl ProxyProtocolV2Builder {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self { src: None, dst: None }
|
Self { src: None, dst: None }
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ use socket2::{Socket, TcpKeepalive, Domain, Type, Protocol};
|
|||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
|
|
||||||
/// Configure TCP socket with recommended settings for proxy use
|
/// Configure TCP socket with recommended settings for proxy use
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn configure_tcp_socket(
|
pub fn configure_tcp_socket(
|
||||||
stream: &TcpStream,
|
stream: &TcpStream,
|
||||||
keepalive: bool,
|
keepalive: bool,
|
||||||
@@ -82,6 +83,7 @@ pub fn configure_client_socket(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Set socket to send RST on close (for masking)
|
/// Set socket to send RST on close (for masking)
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn set_linger_zero(stream: &TcpStream) -> Result<()> {
|
pub fn set_linger_zero(stream: &TcpStream) -> Result<()> {
|
||||||
let socket = socket2::SockRef::from(stream);
|
let socket = socket2::SockRef::from(stream);
|
||||||
socket.set_linger(Some(Duration::ZERO))?;
|
socket.set_linger(Some(Duration::ZERO))?;
|
||||||
@@ -89,6 +91,7 @@ pub fn set_linger_zero(stream: &TcpStream) -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Create a new TCP socket for outgoing connections
|
/// Create a new TCP socket for outgoing connections
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn create_outgoing_socket(addr: SocketAddr) -> Result<Socket> {
|
pub fn create_outgoing_socket(addr: SocketAddr) -> Result<Socket> {
|
||||||
create_outgoing_socket_bound(addr, None)
|
create_outgoing_socket_bound(addr, None)
|
||||||
}
|
}
|
||||||
@@ -120,6 +123,7 @@ pub fn create_outgoing_socket_bound(addr: SocketAddr, bind_addr: Option<IpAddr>)
|
|||||||
|
|
||||||
|
|
||||||
/// Get local address of a socket
|
/// Get local address of a socket
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn get_local_addr(stream: &TcpStream) -> Option<SocketAddr> {
|
pub fn get_local_addr(stream: &TcpStream) -> Option<SocketAddr> {
|
||||||
stream.local_addr().ok()
|
stream.local_addr().ok()
|
||||||
}
|
}
|
||||||
@@ -132,17 +136,17 @@ pub fn resolve_interface_ip(name: &str, want_ipv6: bool) -> Option<IpAddr> {
|
|||||||
|
|
||||||
if let Ok(addrs) = getifaddrs() {
|
if let Ok(addrs) = getifaddrs() {
|
||||||
for iface in addrs {
|
for iface in addrs {
|
||||||
if iface.interface_name == name {
|
if iface.interface_name == name
|
||||||
if let Some(address) = iface.address {
|
&& let Some(address) = iface.address
|
||||||
if let Some(v4) = address.as_sockaddr_in() {
|
{
|
||||||
if !want_ipv6 {
|
if let Some(v4) = address.as_sockaddr_in() {
|
||||||
return Some(IpAddr::V4(v4.ip()));
|
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()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} else if let Some(v6) = address.as_sockaddr_in6()
|
||||||
|
&& want_ipv6
|
||||||
|
{
|
||||||
|
return Some(IpAddr::V6(v6.ip()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -157,11 +161,13 @@ pub fn resolve_interface_ip(_name: &str, _want_ipv6: bool) -> Option<IpAddr> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Get peer address of a socket
|
/// Get peer address of a socket
|
||||||
|
#[allow(dead_code)]
|
||||||
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()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check if address is IPv6
|
/// Check if address is IPv6
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn is_ipv6(addr: &SocketAddr) -> bool {
|
pub fn is_ipv6(addr: &SocketAddr) -> bool {
|
||||||
addr.is_ipv6()
|
addr.is_ipv6()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//! SOCKS4/5 Client Implementation
|
//! SOCKS4/5 Client Implementation
|
||||||
|
|
||||||
use std::net::{IpAddr, SocketAddr};
|
use std::net::{IpAddr, SocketAddr};
|
||||||
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||||
use tokio::net::TcpStream;
|
use tokio::net::TcpStream;
|
||||||
use crate::error::{ProxyError, Result};
|
use crate::error::{ProxyError, Result};
|
||||||
|
|
||||||
@@ -27,11 +27,11 @@ pub async fn connect_socks4(
|
|||||||
buf.extend_from_slice(user);
|
buf.extend_from_slice(user);
|
||||||
buf.push(0); // NULL
|
buf.push(0); // NULL
|
||||||
|
|
||||||
stream.write_all(&buf).await.map_err(|e| ProxyError::Io(e))?;
|
stream.write_all(&buf).await.map_err(ProxyError::Io)?;
|
||||||
|
|
||||||
// Response: VN (1) | CD (1) | DSTPORT (2) | DSTIP (4)
|
// Response: VN (1) | CD (1) | DSTPORT (2) | DSTIP (4)
|
||||||
let mut resp = [0u8; 8];
|
let mut resp = [0u8; 8];
|
||||||
stream.read_exact(&mut resp).await.map_err(|e| ProxyError::Io(e))?;
|
stream.read_exact(&mut resp).await.map_err(ProxyError::Io)?;
|
||||||
|
|
||||||
if resp[1] != 90 {
|
if resp[1] != 90 {
|
||||||
return Err(ProxyError::Proxy(format!("SOCKS4 request rejected: code {}", resp[1])));
|
return Err(ProxyError::Proxy(format!("SOCKS4 request rejected: code {}", resp[1])));
|
||||||
@@ -56,10 +56,10 @@ pub async fn connect_socks5(
|
|||||||
let mut buf = vec![5u8, methods.len() as u8];
|
let mut buf = vec![5u8, methods.len() as u8];
|
||||||
buf.extend_from_slice(&methods);
|
buf.extend_from_slice(&methods);
|
||||||
|
|
||||||
stream.write_all(&buf).await.map_err(|e| ProxyError::Io(e))?;
|
stream.write_all(&buf).await.map_err(ProxyError::Io)?;
|
||||||
|
|
||||||
let mut resp = [0u8; 2];
|
let mut resp = [0u8; 2];
|
||||||
stream.read_exact(&mut resp).await.map_err(|e| ProxyError::Io(e))?;
|
stream.read_exact(&mut resp).await.map_err(ProxyError::Io)?;
|
||||||
|
|
||||||
if resp[0] != 5 {
|
if resp[0] != 5 {
|
||||||
return Err(ProxyError::Proxy("Invalid SOCKS5 version".to_string()));
|
return Err(ProxyError::Proxy("Invalid SOCKS5 version".to_string()));
|
||||||
@@ -80,10 +80,10 @@ pub async fn connect_socks5(
|
|||||||
auth_buf.push(p_bytes.len() as u8);
|
auth_buf.push(p_bytes.len() as u8);
|
||||||
auth_buf.extend_from_slice(p_bytes);
|
auth_buf.extend_from_slice(p_bytes);
|
||||||
|
|
||||||
stream.write_all(&auth_buf).await.map_err(|e| ProxyError::Io(e))?;
|
stream.write_all(&auth_buf).await.map_err(ProxyError::Io)?;
|
||||||
|
|
||||||
let mut auth_resp = [0u8; 2];
|
let mut auth_resp = [0u8; 2];
|
||||||
stream.read_exact(&mut auth_resp).await.map_err(|e| ProxyError::Io(e))?;
|
stream.read_exact(&mut auth_resp).await.map_err(ProxyError::Io)?;
|
||||||
|
|
||||||
if auth_resp[1] != 0 {
|
if auth_resp[1] != 0 {
|
||||||
return Err(ProxyError::Proxy("SOCKS5 authentication failed".to_string()));
|
return Err(ProxyError::Proxy("SOCKS5 authentication failed".to_string()));
|
||||||
@@ -112,11 +112,11 @@ pub async fn connect_socks5(
|
|||||||
|
|
||||||
req.extend_from_slice(&target.port().to_be_bytes());
|
req.extend_from_slice(&target.port().to_be_bytes());
|
||||||
|
|
||||||
stream.write_all(&req).await.map_err(|e| ProxyError::Io(e))?;
|
stream.write_all(&req).await.map_err(ProxyError::Io)?;
|
||||||
|
|
||||||
// Response
|
// Response
|
||||||
let mut head = [0u8; 4];
|
let mut head = [0u8; 4];
|
||||||
stream.read_exact(&mut head).await.map_err(|e| ProxyError::Io(e))?;
|
stream.read_exact(&mut head).await.map_err(ProxyError::Io)?;
|
||||||
|
|
||||||
if head[1] != 0 {
|
if head[1] != 0 {
|
||||||
return Err(ProxyError::Proxy(format!("SOCKS5 request failed: code {}", head[1])));
|
return Err(ProxyError::Proxy(format!("SOCKS5 request failed: code {}", head[1])));
|
||||||
@@ -126,17 +126,17 @@ pub async fn connect_socks5(
|
|||||||
match head[3] {
|
match head[3] {
|
||||||
1 => { // IPv4
|
1 => { // IPv4
|
||||||
let mut addr = [0u8; 4 + 2];
|
let mut addr = [0u8; 4 + 2];
|
||||||
stream.read_exact(&mut addr).await.map_err(|e| ProxyError::Io(e))?;
|
stream.read_exact(&mut addr).await.map_err(ProxyError::Io)?;
|
||||||
},
|
},
|
||||||
3 => { // Domain
|
3 => { // Domain
|
||||||
let mut len = [0u8; 1];
|
let mut len = [0u8; 1];
|
||||||
stream.read_exact(&mut len).await.map_err(|e| ProxyError::Io(e))?;
|
stream.read_exact(&mut len).await.map_err(ProxyError::Io)?;
|
||||||
let mut addr = vec![0u8; len[0] as usize + 2];
|
let mut addr = vec![0u8; len[0] as usize + 2];
|
||||||
stream.read_exact(&mut addr).await.map_err(|e| ProxyError::Io(e))?;
|
stream.read_exact(&mut addr).await.map_err(ProxyError::Io)?;
|
||||||
},
|
},
|
||||||
4 => { // IPv6
|
4 => { // IPv6
|
||||||
let mut addr = [0u8; 16 + 2];
|
let mut addr = [0u8; 16 + 2];
|
||||||
stream.read_exact(&mut addr).await.map_err(|e| ProxyError::Io(e))?;
|
stream.read_exact(&mut addr).await.map_err(ProxyError::Io)?;
|
||||||
},
|
},
|
||||||
_ => return Err(ProxyError::Proxy("Invalid address type in SOCKS5 response".to_string())),
|
_ => return Err(ProxyError::Proxy("Invalid address type in SOCKS5 response".to_string())),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
//! Upstream Management with per-DC latency-weighted selection
|
//! Upstream Management with per-DC latency-weighted selection
|
||||||
//!
|
//!
|
||||||
//! IPv6/IPv4 connectivity checks with configurable preference.
|
//! IPv6/IPv4 connectivity checks with configurable preference.
|
||||||
|
|
||||||
|
#![allow(deprecated)]
|
||||||
|
|
||||||
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;
|
||||||
@@ -55,9 +57,10 @@ impl LatencyEma {
|
|||||||
// ============= Per-DC IP Preference Tracking =============
|
// ============= Per-DC IP Preference Tracking =============
|
||||||
|
|
||||||
/// Tracks which IP version works for each DC
|
/// Tracks which IP version works for each DC
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||||
pub enum IpPreference {
|
pub enum IpPreference {
|
||||||
/// Not yet tested
|
/// Not yet tested
|
||||||
|
#[default]
|
||||||
Unknown,
|
Unknown,
|
||||||
/// IPv6 works
|
/// IPv6 works
|
||||||
PreferV6,
|
PreferV6,
|
||||||
@@ -69,12 +72,6 @@ pub enum IpPreference {
|
|||||||
Unavailable,
|
Unavailable,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for IpPreference {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self::Unknown
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ============= Upstream State =============
|
// ============= Upstream State =============
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -110,7 +107,7 @@ impl UpstreamState {
|
|||||||
if abs_dc == 0 {
|
if abs_dc == 0 {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
if abs_dc >= 1 && abs_dc <= NUM_DCS {
|
if (1..=NUM_DCS).contains(&abs_dc) {
|
||||||
Some(abs_dc - 1)
|
Some(abs_dc - 1)
|
||||||
} else {
|
} else {
|
||||||
// Unknown DC → default cluster (DC 2, index 1)
|
// Unknown DC → default cluster (DC 2, index 1)
|
||||||
@@ -120,10 +117,10 @@ impl UpstreamState {
|
|||||||
|
|
||||||
/// Get latency for a specific DC, falling back to average across all known DCs
|
/// Get latency for a specific DC, falling back to average across all known DCs
|
||||||
fn effective_latency(&self, dc_idx: Option<i16>) -> Option<f64> {
|
fn effective_latency(&self, dc_idx: Option<i16>) -> Option<f64> {
|
||||||
if let Some(di) = dc_idx.and_then(Self::dc_array_idx) {
|
if let Some(di) = dc_idx.and_then(Self::dc_array_idx)
|
||||||
if let Some(ms) = self.dc_latency[di].get() {
|
&& let Some(ms) = self.dc_latency[di].get()
|
||||||
return Some(ms);
|
{
|
||||||
}
|
return Some(ms);
|
||||||
}
|
}
|
||||||
|
|
||||||
let (sum, count) = self.dc_latency.iter()
|
let (sum, count) = self.dc_latency.iter()
|
||||||
@@ -549,7 +546,7 @@ impl UpstreamManager {
|
|||||||
/// Tests BOTH IPv6 and IPv4, returns separate results for each.
|
/// Tests BOTH IPv6 and IPv4, returns separate results for each.
|
||||||
pub async fn ping_all_dcs(
|
pub async fn ping_all_dcs(
|
||||||
&self,
|
&self,
|
||||||
prefer_ipv6: bool,
|
_prefer_ipv6: bool,
|
||||||
dc_overrides: &HashMap<String, Vec<String>>,
|
dc_overrides: &HashMap<String, Vec<String>>,
|
||||||
ipv4_enabled: bool,
|
ipv4_enabled: bool,
|
||||||
ipv6_enabled: bool,
|
ipv6_enabled: bool,
|
||||||
@@ -580,7 +577,7 @@ impl UpstreamManager {
|
|||||||
|
|
||||||
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, Some(bind_rr.clone()), addr_v6)
|
self.ping_single_dc(upstream_config, Some(bind_rr.clone()), addr_v6)
|
||||||
).await;
|
).await;
|
||||||
|
|
||||||
let ping_result = match result {
|
let ping_result = match result {
|
||||||
@@ -631,7 +628,7 @@ impl UpstreamManager {
|
|||||||
|
|
||||||
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, Some(bind_rr.clone()), 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 {
|
||||||
@@ -694,7 +691,7 @@ impl UpstreamManager {
|
|||||||
}
|
}
|
||||||
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, Some(bind_rr.clone()), addr)
|
self.ping_single_dc(upstream_config, Some(bind_rr.clone()), addr)
|
||||||
).await;
|
).await;
|
||||||
|
|
||||||
let ping_result = match result {
|
let ping_result = match result {
|
||||||
@@ -907,6 +904,7 @@ impl UpstreamManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Get the preferred IP for a DC (for use by other components)
|
/// Get the preferred IP for a DC (for use by other components)
|
||||||
|
#[allow(dead_code)]
|
||||||
pub async fn get_dc_ip_preference(&self, dc_idx: i16) -> Option<IpPreference> {
|
pub async fn get_dc_ip_preference(&self, dc_idx: i16) -> Option<IpPreference> {
|
||||||
let guard = self.upstreams.read().await;
|
let guard = self.upstreams.read().await;
|
||||||
if guard.is_empty() {
|
if guard.is_empty() {
|
||||||
@@ -918,6 +916,7 @@ impl UpstreamManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Get preferred DC address based on config preference
|
/// Get preferred DC address based on config preference
|
||||||
|
#[allow(dead_code)]
|
||||||
pub async fn get_dc_addr(&self, dc_idx: i16, prefer_ipv6: bool) -> Option<SocketAddr> {
|
pub async fn get_dc_addr(&self, dc_idx: i16, prefer_ipv6: bool) -> Option<SocketAddr> {
|
||||||
let arr_idx = UpstreamState::dc_array_idx(dc_idx)?;
|
let arr_idx = UpstreamState::dc_array_idx(dc_idx)?;
|
||||||
|
|
||||||
|
|||||||
@@ -1,22 +1,24 @@
|
|||||||
//! IP Addr Detect
|
//! IP Addr Detect
|
||||||
|
|
||||||
use std::net::{IpAddr, SocketAddr, UdpSocket};
|
use std::net::{IpAddr, UdpSocket};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use tracing::{debug, warn};
|
use tracing::{debug, warn};
|
||||||
|
|
||||||
/// Detected IP addresses
|
/// Detected IP addresses
|
||||||
#[derive(Debug, Clone, Default)]
|
#[derive(Debug, Clone, Default)]
|
||||||
|
#[allow(dead_code)]
|
||||||
pub struct IpInfo {
|
pub struct IpInfo {
|
||||||
pub ipv4: Option<IpAddr>,
|
pub ipv4: Option<IpAddr>,
|
||||||
pub ipv6: Option<IpAddr>,
|
pub ipv6: Option<IpAddr>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
impl IpInfo {
|
impl IpInfo {
|
||||||
/// Check if any IP is detected
|
/// Check if any IP is detected
|
||||||
pub fn has_any(&self) -> bool {
|
pub fn has_any(&self) -> bool {
|
||||||
self.ipv4.is_some() || self.ipv6.is_some()
|
self.ipv4.is_some() || self.ipv6.is_some()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get preferred IP (IPv6 if available and preferred)
|
/// Get preferred IP (IPv6 if available and preferred)
|
||||||
pub fn preferred(&self, prefer_ipv6: bool) -> Option<IpAddr> {
|
pub fn preferred(&self, prefer_ipv6: bool) -> Option<IpAddr> {
|
||||||
if prefer_ipv6 {
|
if prefer_ipv6 {
|
||||||
@@ -28,12 +30,14 @@ impl IpInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// URLs for IP detection
|
/// URLs for IP detection
|
||||||
|
#[allow(dead_code)]
|
||||||
const IPV4_URLS: &[&str] = &[
|
const IPV4_URLS: &[&str] = &[
|
||||||
"http://v4.ident.me/",
|
"http://v4.ident.me/",
|
||||||
"http://ipv4.icanhazip.com/",
|
"http://ipv4.icanhazip.com/",
|
||||||
"http://api.ipify.org/",
|
"http://api.ipify.org/",
|
||||||
];
|
];
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
const IPV6_URLS: &[&str] = &[
|
const IPV6_URLS: &[&str] = &[
|
||||||
"http://v6.ident.me/",
|
"http://v6.ident.me/",
|
||||||
"http://ipv6.icanhazip.com/",
|
"http://ipv6.icanhazip.com/",
|
||||||
@@ -42,12 +46,14 @@ const IPV6_URLS: &[&str] = &[
|
|||||||
|
|
||||||
/// Detect local IP address by connecting to a public DNS
|
/// Detect local IP address by connecting to a public DNS
|
||||||
/// This does not actually send any packets
|
/// This does not actually send any packets
|
||||||
|
#[allow(dead_code)]
|
||||||
fn get_local_ip(target: &str) -> Option<IpAddr> {
|
fn get_local_ip(target: &str) -> Option<IpAddr> {
|
||||||
let socket = UdpSocket::bind("0.0.0.0:0").ok()?;
|
let socket = UdpSocket::bind("0.0.0.0:0").ok()?;
|
||||||
socket.connect(target).ok()?;
|
socket.connect(target).ok()?;
|
||||||
socket.local_addr().ok().map(|addr| addr.ip())
|
socket.local_addr().ok().map(|addr| addr.ip())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
fn get_local_ipv6(target: &str) -> Option<IpAddr> {
|
fn get_local_ipv6(target: &str) -> Option<IpAddr> {
|
||||||
let socket = UdpSocket::bind("[::]:0").ok()?;
|
let socket = UdpSocket::bind("[::]:0").ok()?;
|
||||||
socket.connect(target).ok()?;
|
socket.connect(target).ok()?;
|
||||||
@@ -55,59 +61,62 @@ fn get_local_ipv6(target: &str) -> Option<IpAddr> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Detect public IP addresses
|
/// Detect public IP addresses
|
||||||
|
#[allow(dead_code)]
|
||||||
pub async fn detect_ip() -> IpInfo {
|
pub async fn detect_ip() -> IpInfo {
|
||||||
let mut info = IpInfo::default();
|
let mut info = IpInfo::default();
|
||||||
|
|
||||||
// Try to get local interface IP first (default gateway interface)
|
// Try to get local interface IP first (default gateway interface)
|
||||||
// We connect to Google DNS to find out which interface is used for routing
|
// We connect to Google DNS to find out which interface is used for routing
|
||||||
if let Some(ip) = get_local_ip("8.8.8.8:80") {
|
if let Some(ip) = get_local_ip("8.8.8.8:80")
|
||||||
if ip.is_ipv4() && !ip.is_loopback() {
|
&& ip.is_ipv4()
|
||||||
info.ipv4 = Some(ip);
|
&& !ip.is_loopback()
|
||||||
debug!(ip = %ip, "Detected local IPv4 address via routing");
|
{
|
||||||
}
|
info.ipv4 = Some(ip);
|
||||||
|
debug!(ip = %ip, "Detected local IPv4 address via routing");
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(ip) = get_local_ipv6("[2001:4860:4860::8888]:80") {
|
if let Some(ip) = get_local_ipv6("[2001:4860:4860::8888]:80")
|
||||||
if ip.is_ipv6() && !ip.is_loopback() {
|
&& ip.is_ipv6()
|
||||||
info.ipv6 = Some(ip);
|
&& !ip.is_loopback()
|
||||||
debug!(ip = %ip, "Detected local IPv6 address via routing");
|
{
|
||||||
}
|
info.ipv6 = Some(ip);
|
||||||
|
debug!(ip = %ip, "Detected local IPv6 address via routing");
|
||||||
}
|
}
|
||||||
|
|
||||||
// If local detection failed or returned private IP (and we want public),
|
// If local detection failed or returned private IP (and we want public),
|
||||||
// or just as a fallback/verification, we might want to check external services.
|
// or just as a fallback/verification, we might want to check external services.
|
||||||
// However, the requirement is: "if IP for listening is not set... it should be IP from interface...
|
// However, the requirement is: "if IP for listening is not set... it should be IP from interface...
|
||||||
// if impossible - request external resources".
|
// if impossible - request external resources".
|
||||||
|
|
||||||
// So if we found a local IP, we might be good. But often servers are behind NAT.
|
// So if we found a local IP, we might be good. But often servers are behind NAT.
|
||||||
// If the local IP is private, we probably want the public IP for the tg:// link.
|
// If the local IP is private, we probably want the public IP for the tg:// link.
|
||||||
// Let's check if the detected IPs are private.
|
// Let's check if the detected IPs are private.
|
||||||
|
|
||||||
let need_external_v4 = info.ipv4.map_or(true, |ip| is_private_ip(ip));
|
let need_external_v4 = info.ipv4.is_none_or(is_private_ip);
|
||||||
let need_external_v6 = info.ipv6.map_or(true, |ip| is_private_ip(ip));
|
let need_external_v6 = info.ipv6.is_none_or(is_private_ip);
|
||||||
|
|
||||||
if need_external_v4 {
|
if need_external_v4 {
|
||||||
debug!("Local IPv4 is private or missing, checking external services...");
|
debug!("Local IPv4 is private or missing, checking external services...");
|
||||||
for url in IPV4_URLS {
|
for url in IPV4_URLS {
|
||||||
if let Some(ip) = fetch_ip(url).await {
|
if let Some(ip) = fetch_ip(url).await
|
||||||
if ip.is_ipv4() {
|
&& ip.is_ipv4()
|
||||||
info.ipv4 = Some(ip);
|
{
|
||||||
debug!(ip = %ip, "Detected public IPv4 address");
|
info.ipv4 = Some(ip);
|
||||||
break;
|
debug!(ip = %ip, "Detected public IPv4 address");
|
||||||
}
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if need_external_v6 {
|
if need_external_v6 {
|
||||||
debug!("Local IPv6 is private or missing, checking external services...");
|
debug!("Local IPv6 is private or missing, checking external services...");
|
||||||
for url in IPV6_URLS {
|
for url in IPV6_URLS {
|
||||||
if let Some(ip) = fetch_ip(url).await {
|
if let Some(ip) = fetch_ip(url).await
|
||||||
if ip.is_ipv6() {
|
&& ip.is_ipv6()
|
||||||
info.ipv6 = Some(ip);
|
{
|
||||||
debug!(ip = %ip, "Detected public IPv6 address");
|
info.ipv6 = Some(ip);
|
||||||
break;
|
debug!(ip = %ip, "Detected public IPv6 address");
|
||||||
}
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -119,6 +128,7 @@ pub async fn detect_ip() -> IpInfo {
|
|||||||
info
|
info
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
fn is_private_ip(ip: IpAddr) -> bool {
|
fn is_private_ip(ip: IpAddr) -> bool {
|
||||||
match ip {
|
match ip {
|
||||||
IpAddr::V4(ipv4) => {
|
IpAddr::V4(ipv4) => {
|
||||||
@@ -131,19 +141,21 @@ fn is_private_ip(ip: IpAddr) -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Fetch IP from URL
|
/// Fetch IP from URL
|
||||||
|
#[allow(dead_code)]
|
||||||
async fn fetch_ip(url: &str) -> Option<IpAddr> {
|
async fn fetch_ip(url: &str) -> Option<IpAddr> {
|
||||||
let client = reqwest::Client::builder()
|
let client = reqwest::Client::builder()
|
||||||
.timeout(Duration::from_secs(5))
|
.timeout(Duration::from_secs(5))
|
||||||
.build()
|
.build()
|
||||||
.ok()?;
|
.ok()?;
|
||||||
|
|
||||||
let response = client.get(url).send().await.ok()?;
|
let response = client.get(url).send().await.ok()?;
|
||||||
let text = response.text().await.ok()?;
|
let text = response.text().await.ok()?;
|
||||||
|
|
||||||
text.trim().parse().ok()
|
text.trim().parse().ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Synchronous IP detection (for startup)
|
/// Synchronous IP detection (for startup)
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn detect_ip_sync() -> IpInfo {
|
pub fn detect_ip_sync() -> IpInfo {
|
||||||
tokio::runtime::Handle::current().block_on(detect_ip())
|
tokio::runtime::Handle::current().block_on(detect_ip())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,5 +3,7 @@
|
|||||||
pub mod ip;
|
pub mod ip;
|
||||||
pub mod time;
|
pub mod time;
|
||||||
|
|
||||||
|
#[allow(unused_imports)]
|
||||||
pub use ip::*;
|
pub use ip::*;
|
||||||
|
#[allow(unused_imports)]
|
||||||
pub use time::*;
|
pub use time::*;
|
||||||
@@ -4,11 +4,14 @@ use std::time::Duration;
|
|||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use tracing::{debug, warn, error};
|
use tracing::{debug, warn, error};
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
const TIME_SYNC_URL: &str = "https://core.telegram.org/getProxySecret";
|
const TIME_SYNC_URL: &str = "https://core.telegram.org/getProxySecret";
|
||||||
|
#[allow(dead_code)]
|
||||||
const MAX_TIME_SKEW_SECS: i64 = 30;
|
const MAX_TIME_SKEW_SECS: i64 = 30;
|
||||||
|
|
||||||
/// Time sync result
|
/// Time sync result
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
#[allow(dead_code)]
|
||||||
pub struct TimeSyncResult {
|
pub struct TimeSyncResult {
|
||||||
pub server_time: DateTime<Utc>,
|
pub server_time: DateTime<Utc>,
|
||||||
pub local_time: DateTime<Utc>,
|
pub local_time: DateTime<Utc>,
|
||||||
@@ -17,6 +20,7 @@ pub struct TimeSyncResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Check time synchronization with Telegram servers
|
/// Check time synchronization with Telegram servers
|
||||||
|
#[allow(dead_code)]
|
||||||
pub async fn check_time_sync() -> Option<TimeSyncResult> {
|
pub async fn check_time_sync() -> Option<TimeSyncResult> {
|
||||||
let client = reqwest::Client::builder()
|
let client = reqwest::Client::builder()
|
||||||
.timeout(Duration::from_secs(10))
|
.timeout(Duration::from_secs(10))
|
||||||
@@ -60,17 +64,18 @@ pub async fn check_time_sync() -> Option<TimeSyncResult> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Background time sync task
|
/// Background time sync task
|
||||||
|
#[allow(dead_code)]
|
||||||
pub async fn time_sync_task(check_interval: Duration) -> ! {
|
pub async fn time_sync_task(check_interval: Duration) -> ! {
|
||||||
loop {
|
loop {
|
||||||
if let Some(result) = check_time_sync().await {
|
if let Some(result) = check_time_sync().await
|
||||||
if result.is_skewed {
|
&& result.is_skewed
|
||||||
error!(
|
{
|
||||||
"System clock is off by {} seconds. Please sync your clock.",
|
error!(
|
||||||
result.skew_secs
|
"System clock is off by {} seconds. Please sync your clock.",
|
||||||
);
|
result.skew_secs
|
||||||
}
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
tokio::time::sleep(check_interval).await;
|
tokio::time::sleep(check_interval).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
276
tools/zbx_telemt_template.yaml
Normal file
276
tools/zbx_telemt_template.yaml
Normal file
@@ -0,0 +1,276 @@
|
|||||||
|
zabbix_export:
|
||||||
|
version: '7.0'
|
||||||
|
template_groups:
|
||||||
|
- uuid: 43d0fe04c7094000829b0d28c6e3470c
|
||||||
|
name: 'Custom Templates'
|
||||||
|
templates:
|
||||||
|
- uuid: f2a694213c3d49d88cc03bffb111429e
|
||||||
|
template: Telemt
|
||||||
|
name: Telemt
|
||||||
|
description: |
|
||||||
|
A simple template using Prometheus metrics.
|
||||||
|
Set the {$TELEMT_URL} macro with the metrics URL
|
||||||
|
groups:
|
||||||
|
- name: 'Custom Templates'
|
||||||
|
items:
|
||||||
|
- uuid: fb95391c7f894e3eb6984b92885813a2
|
||||||
|
name: 'Connections bad total'
|
||||||
|
type: DEPENDENT
|
||||||
|
key: telemt.conn_bad_total
|
||||||
|
delay: '0'
|
||||||
|
trends: '0'
|
||||||
|
preprocessing:
|
||||||
|
- type: PROMETHEUS_PATTERN
|
||||||
|
parameters:
|
||||||
|
- telemt_connections_bad_total
|
||||||
|
- value
|
||||||
|
- ''
|
||||||
|
master_item:
|
||||||
|
key: telemt.prom_metrics
|
||||||
|
tags:
|
||||||
|
- tag: Application
|
||||||
|
value: 'Server connections'
|
||||||
|
- uuid: f36c9632394a4af3853583857ca8dbf1
|
||||||
|
name: 'Connections total'
|
||||||
|
type: DEPENDENT
|
||||||
|
key: telemt.conn_total
|
||||||
|
delay: '0'
|
||||||
|
trends: '0'
|
||||||
|
preprocessing:
|
||||||
|
- type: PROMETHEUS_PATTERN
|
||||||
|
parameters:
|
||||||
|
- telemt_connections_total
|
||||||
|
- value
|
||||||
|
- ''
|
||||||
|
master_item:
|
||||||
|
key: telemt.prom_metrics
|
||||||
|
tags:
|
||||||
|
- tag: Application
|
||||||
|
value: 'Server connections'
|
||||||
|
- uuid: 1618272cf68e44509425f5fab029db7b
|
||||||
|
name: 'Handshake timeouts total'
|
||||||
|
type: DEPENDENT
|
||||||
|
key: telemt.handshake_timeouts_total
|
||||||
|
delay: '0'
|
||||||
|
trends: '0'
|
||||||
|
preprocessing:
|
||||||
|
- type: PROMETHEUS_PATTERN
|
||||||
|
parameters:
|
||||||
|
- telemt_handshake_timeouts_total
|
||||||
|
- value
|
||||||
|
- ''
|
||||||
|
master_item:
|
||||||
|
key: telemt.prom_metrics
|
||||||
|
tags:
|
||||||
|
- tag: Application
|
||||||
|
value: 'Server connections'
|
||||||
|
- uuid: fb95391c7f894e3eb6984b92885813d2
|
||||||
|
name: 'ME keepalive send failures'
|
||||||
|
type: DEPENDENT
|
||||||
|
key: telemt.me_keepalive_failed_total
|
||||||
|
delay: '0'
|
||||||
|
trends: '0'
|
||||||
|
preprocessing:
|
||||||
|
- type: PROMETHEUS_PATTERN
|
||||||
|
parameters:
|
||||||
|
- telemt_me_keepalive_failed_total
|
||||||
|
- value
|
||||||
|
- ''
|
||||||
|
master_item:
|
||||||
|
key: telemt.prom_metrics
|
||||||
|
tags:
|
||||||
|
- tag: Application
|
||||||
|
value: 'Middle-End connections'
|
||||||
|
- uuid: fb95391c7f894e3eb6984b92885813c2
|
||||||
|
name: 'ME keepalive frames sent'
|
||||||
|
type: DEPENDENT
|
||||||
|
key: telemt.me_keepalive_sent_total
|
||||||
|
delay: '0'
|
||||||
|
trends: '0'
|
||||||
|
preprocessing:
|
||||||
|
- type: PROMETHEUS_PATTERN
|
||||||
|
parameters:
|
||||||
|
- telemt_me_keepalive_sent_total
|
||||||
|
- value
|
||||||
|
- ''
|
||||||
|
master_item:
|
||||||
|
key: telemt.prom_metrics
|
||||||
|
tags:
|
||||||
|
- tag: Application
|
||||||
|
value: 'Middle-End connections'
|
||||||
|
- uuid: fb95391c7f894e3eb6984b92885811a2
|
||||||
|
name: 'ME reconnect attempts'
|
||||||
|
type: DEPENDENT
|
||||||
|
key: telemt.me_reconnect_attempts_total
|
||||||
|
delay: '0'
|
||||||
|
trends: '0'
|
||||||
|
preprocessing:
|
||||||
|
- type: PROMETHEUS_PATTERN
|
||||||
|
parameters:
|
||||||
|
- telemt_me_reconnect_attempts_total
|
||||||
|
- value
|
||||||
|
- ''
|
||||||
|
master_item:
|
||||||
|
key: telemt.prom_metrics
|
||||||
|
tags:
|
||||||
|
- tag: Application
|
||||||
|
value: 'Middle-End connections'
|
||||||
|
- uuid: fb95391c7f894e3eb6984b92885812a2
|
||||||
|
name: 'ME reconnect successes'
|
||||||
|
type: DEPENDENT
|
||||||
|
key: telemt.me_reconnect_success_total
|
||||||
|
delay: '0'
|
||||||
|
trends: '0'
|
||||||
|
preprocessing:
|
||||||
|
- type: PROMETHEUS_PATTERN
|
||||||
|
parameters:
|
||||||
|
- telemt_me_reconnect_success_total
|
||||||
|
- value
|
||||||
|
- ''
|
||||||
|
master_item:
|
||||||
|
key: telemt.prom_metrics
|
||||||
|
tags:
|
||||||
|
- tag: Application
|
||||||
|
value: 'Middle-End connections'
|
||||||
|
- uuid: 991b1858e3f94b3098ff0f84859efc41
|
||||||
|
name: 'Prometheus metrics'
|
||||||
|
type: HTTP_AGENT
|
||||||
|
key: telemt.prom_metrics
|
||||||
|
value_type: TEXT
|
||||||
|
trends: '0'
|
||||||
|
url: '{$TELEMT_URL}'
|
||||||
|
- uuid: fb95391c7f894e3eb6984b92885813b2
|
||||||
|
name: 'Telemt Uptime'
|
||||||
|
type: DEPENDENT
|
||||||
|
key: telemt.uptime
|
||||||
|
delay: '0'
|
||||||
|
trends: '0'
|
||||||
|
units: s
|
||||||
|
preprocessing:
|
||||||
|
- type: PROMETHEUS_PATTERN
|
||||||
|
parameters:
|
||||||
|
- telemt_uptime_seconds
|
||||||
|
- value
|
||||||
|
- ''
|
||||||
|
master_item:
|
||||||
|
key: telemt.prom_metrics
|
||||||
|
tags:
|
||||||
|
- tag: Application
|
||||||
|
value: 'Server connections'
|
||||||
|
discovery_rules:
|
||||||
|
- uuid: 22727585c14049fbb0863c15dd68634c
|
||||||
|
name: 'Get users'
|
||||||
|
type: DEPENDENT
|
||||||
|
key: telemt.users
|
||||||
|
delay: '0'
|
||||||
|
item_prototypes:
|
||||||
|
- uuid: 137e371a47714a21b5c0c89d535dd717
|
||||||
|
name: 'Active connections by {#TELEMT_USER}'
|
||||||
|
type: DEPENDENT
|
||||||
|
key: 'telemt.active_conn_[{#TELEMT_USER}]'
|
||||||
|
delay: '0'
|
||||||
|
preprocessing:
|
||||||
|
- type: PROMETHEUS_PATTERN
|
||||||
|
parameters:
|
||||||
|
- 'telemt_user_connections_current{user="{#TELEMT_USER}"}'
|
||||||
|
- value
|
||||||
|
- ''
|
||||||
|
master_item:
|
||||||
|
key: telemt.prom_metrics
|
||||||
|
tags:
|
||||||
|
- tag: Application
|
||||||
|
value: 'Users connections'
|
||||||
|
- uuid: 3ccce91ab5d54b4d972280c7b7bda910
|
||||||
|
name: 'Messages received from {#TELEMT_USER}'
|
||||||
|
type: DEPENDENT
|
||||||
|
key: 'telemt.msgs_from_[{#TELEMT_USER}]'
|
||||||
|
delay: '0'
|
||||||
|
preprocessing:
|
||||||
|
- type: PROMETHEUS_PATTERN
|
||||||
|
parameters:
|
||||||
|
- 'telemt_user_msgs_from_client{user="{#TELEMT_USER}"}'
|
||||||
|
- value
|
||||||
|
- ''
|
||||||
|
master_item:
|
||||||
|
key: telemt.prom_metrics
|
||||||
|
tags:
|
||||||
|
- tag: Application
|
||||||
|
value: 'Users connections'
|
||||||
|
- uuid: e539126215f2419bbfd0d8099aabe1cb
|
||||||
|
name: 'Messages sent to {#TELEMT_USER}'
|
||||||
|
type: DEPENDENT
|
||||||
|
key: 'telemt.msgs_to_[{#TELEMT_USER}]'
|
||||||
|
delay: '0'
|
||||||
|
preprocessing:
|
||||||
|
- type: PROMETHEUS_PATTERN
|
||||||
|
parameters:
|
||||||
|
- 'telemt_user_msgs_to_client{user="{#TELEMT_USER}"}'
|
||||||
|
- value
|
||||||
|
- ''
|
||||||
|
master_item:
|
||||||
|
key: telemt.prom_metrics
|
||||||
|
tags:
|
||||||
|
- tag: Application
|
||||||
|
value: 'Users connections'
|
||||||
|
- uuid: 810a8f6346a44ae7bd79a357dbfe2b3c
|
||||||
|
name: 'Bytes received from {#TELEMT_USER}'
|
||||||
|
type: DEPENDENT
|
||||||
|
key: 'telemt.octets_from_[{#TELEMT_USER}]'
|
||||||
|
delay: '0'
|
||||||
|
units: B
|
||||||
|
preprocessing:
|
||||||
|
- type: PROMETHEUS_PATTERN
|
||||||
|
parameters:
|
||||||
|
- 'telemt_user_octets_from_client{user="{#TELEMT_USER}"}'
|
||||||
|
- value
|
||||||
|
- ''
|
||||||
|
master_item:
|
||||||
|
key: telemt.prom_metrics
|
||||||
|
tags:
|
||||||
|
- tag: Application
|
||||||
|
value: 'Users connections'
|
||||||
|
- uuid: d0cc3b4d618b4f0d97f8127b51f872c8
|
||||||
|
name: 'Bytes sent to {#TELEMT_USER}'
|
||||||
|
type: DEPENDENT
|
||||||
|
key: 'telemt.octets_to_[{#TELEMT_USER}]'
|
||||||
|
delay: '0'
|
||||||
|
units: B
|
||||||
|
preprocessing:
|
||||||
|
- type: PROMETHEUS_PATTERN
|
||||||
|
parameters:
|
||||||
|
- 'telemt_user_octets_to_client{user="{#TELEMT_USER}"}'
|
||||||
|
- value
|
||||||
|
- ''
|
||||||
|
master_item:
|
||||||
|
key: telemt.prom_metrics
|
||||||
|
tags:
|
||||||
|
- tag: Application
|
||||||
|
value: 'Users connections'
|
||||||
|
- uuid: e9735aef967b4af28ed59f6c76ad493d
|
||||||
|
name: 'Total connections by {#TELEMT_USER}'
|
||||||
|
type: DEPENDENT
|
||||||
|
key: 'telemt.total_conn_[{#TELEMT_USER}]'
|
||||||
|
delay: '0'
|
||||||
|
preprocessing:
|
||||||
|
- type: PROMETHEUS_PATTERN
|
||||||
|
parameters:
|
||||||
|
- 'telemt_user_connections_total{user="{#TELEMT_USER}"}'
|
||||||
|
- value
|
||||||
|
- ''
|
||||||
|
master_item:
|
||||||
|
key: telemt.prom_metrics
|
||||||
|
tags:
|
||||||
|
- tag: Application
|
||||||
|
value: 'Users connections'
|
||||||
|
master_item:
|
||||||
|
key: telemt.prom_metrics
|
||||||
|
lld_macro_paths:
|
||||||
|
- lld_macro: '{#TELEMT_USER}'
|
||||||
|
path: '$.labels[''user'']'
|
||||||
|
preprocessing:
|
||||||
|
- type: PROMETHEUS_TO_JSON
|
||||||
|
parameters:
|
||||||
|
- ''
|
||||||
|
tags:
|
||||||
|
- tag: target
|
||||||
|
value: Telemt
|
||||||
Reference in New Issue
Block a user