Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
8c2b046
Merge pull request #1 from vispar-tech/fix/critical-bugs-and-cleanup
vispar-tech Aug 31, 2026
4606707
Merge branch 'main' of https://github.com/vispar-tech/openrot
vispar-tech Aug 31, 2026
f7b0212
fix: stop_proxy returns False on timeout instead of True
vispar-tech Aug 31, 2026
afc939b
fix: :bug: isolate _internal/ per app to prevent cross-install conflicts
vispar-tech Sep 1, 2026
4a729f6
feat: :sparkles: add ip-based node deduplication and bridge 502 retry
vispar-tech Sep 1, 2026
54ac98a
feat: :sparkles: add openrot provider command to toggle opencode prov…
vispar-tech Sep 1, 2026
d94f4e8
fix: :bug: use tag with v prefix in self-update download filename
vispar-tech Sep 1, 2026
ffb4c1f
fix: avoid rich live conflicts in warp connect and hide ctrl-c tip in…
vispar-tech Sep 2, 2026
1557f62
Merge pull request #4 from vispar-tech/fix/warp-live-daemon-log
vispar-tech Sep 2, 2026
47a49f9
feat: :sparkles: stop daemons before self-update and restart after
vispar-tech Sep 2, 2026
2400f85
style: :art: ruff format
vispar-tech Sep 2, 2026
a916907
feat: :sparkles: log model, prompt length, and response time in bridg…
vispar-tech Sep 3, 2026
7817ab0
fix: :bug: serialize WARP rotation to prevent concurrent races
vispar-tech Sep 3, 2026
3347b65
fix: :bug: fix BrokenPipeError in bridge headers and make bridge logg…
vispar-tech Sep 3, 2026
c661c47
fix: :bug: unify logging — single openrot.log via events logger, daem…
vispar-tech Sep 3, 2026
d0d654f
fix: :bug: wait for in-progress rotation instead of skipping
vispar-tech Sep 4, 2026
96e0340
feat: :sparkles: smart bridge rate-limit handling, shared client, con…
vispar-tech Sep 7, 2026
761b7a7
refactor: :recycle: unify abstractions, drop probe, rename free→proxy…
vispar-tech Sep 10, 2026
bf205d0
feat(bridge): :sparkles: add start pacing, session ids and port guards
vispar-tech Sep 12, 2026
a4afd4f
feat(bridge): :sparkles: gateway-compatible session ids, stub tools, …
vispar-tech Sep 15, 2026
f268084
fix(warp): :bug: robust rotate() retry tests and bridge lint fixes
vispar-tech Sep 18, 2026
b54be9b
chore: :wrench: track deepwork state in gitignore and .ignore
vispar-tech Sep 19, 2026
ca345cb
refactor: :recycle: reorder module members per clean-code layout
vispar-tech Sep 19, 2026
a8679d9
fix(bridge): :bug: annotate stub tools to satisfy mypy
vispar-tech Sep 19, 2026
da69f35
build(deps): bump the python-deps group across 1 directory with 3 upd…
dependabot[bot] Oct 1, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ dist/

# Codegraph state
.codegraph

# Deepwork state (git-local, OpenCode-readable)
.slim/deepwork/
2 changes: 2 additions & 0 deletions .ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
!.slim/deepwork/
!.slim/deepwork/**
241 changes: 241 additions & 0 deletions RAIONALITY_REVIEW.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
# Рациональность кода: openrot

**4 параллельных агента | Проверка каждого блока на необходимость**

---

## СУММАРНАЯ ТАБЛИЦА

| Категория | Найдено | Самое критичное |
|-----------|---------|-----------------|
| Мёртвый код (dead code) | 10 | `node_from_records` — никогда не вызывается |
| Over-engineering | 40+ | 6 Pydantic-моделей для простых данных, 8 обёрточных функций |
| Дублирование | 25+ | `cfg.load/save` в 10+ местах, 8+ `httpx.Client`, 3 раза `HEALTH_URL` |
| Ненужные фичи / непротестированные | 10 | `self_update_cmd`, `config_edit` — без тестов |
| **ИТОГО** | **~85+** | |

---

## 1. МЁРТВЫЙ КОД И НЕИСПОЛЬЗУЕМЫЕ КОМПОНЕНТЫ

### Критические

**1. `node_from_records()` — никогда не вызывается в продакшене**
- `core/nodes.py:28-39` — определена, но не используется нигде кроме тестов
- Продакшен-код использует `verify.nodes_from_vless_survivors()` и `verify.nodes_from_proxy_survivors()` вместо неё
- **Действие**: Удалить или сделать `_` приватной функцией

**2. `check_for_update()` всегда возвращает `updated=False`**
- `self_update.py:146-151` — возвращает `UpdateResult(updated=False)` даже когда `latest > current`
- CLI в `cli.py:835` проверяет `result.updated`, но оно всегда `False` — обновление никогда не запускается через эту ветку
- **Действие**: Исправить логику: `updated=(latest > current)`

**3. `TypeVar` в `verify.py` — мёртвый импорт**
- `verify.py:22,40` — `T = TypeVar("T")` импортируется и определяется, но никогда не используется (PEP 695 generic syntax `[T]` создаёт новый scope)
- **Действие**: Удалить `from typing import TypeVar` и `T = TypeVar("T")`

### Умеренные

**4. Пустые `__init__.py`** — `core/__init__.py`, `providers/__init__.py` (0 байт)
**5. `# type: ignore[attr-defined]` в `bridge.py:114`** — маскирует несуществующую ошибку
**6. `profile_id` в `models/__init__.py:18`** — экспортируется но не импортируется продакшен-кодом
**7. `openrot provider` команда** — вызывает `opencode.toggle()`; `opencode.py` импортируется только из `cli.py`, нет тестов

### Мелкие

**8. Закомментированные enum'ы** — `models/enums.py:17` — `# follow-up: HYSTERIA2, VMESS, SS`
**9. Непроверенные CLI-команды** — `config_edit`, `self_update_cmd`, `warp install` (реальный путь)

---

## 2. OVER-ENGINEERING: КОГДА КОД СЛОЖНЕЕ ЗАДАЧИ

### Pydantic-модели, где достаточно dict/ничего

| # | Класс | Файл | Почему избыточен |
|---|-------|------|-----------------|
| 1 | `LogOptions` | `singbox.py:74` | Одно поле `level: str = "warn"`. Pydantic- overhead за один string |
| 2 | `RealityTLSOptions` | `singbox.py:20` | 2-3 поля, ни одной валидации |
| 3 | `UTLSOptions` | `singbox.py:28` | 2 поля, `type` всегда `"utls"` |
| 4 | `TransportOptions` | `singbox.py:35` | `type` всегда `"ws"`, ничего не валидирует |
| 5 | `MixedInbound` | `singbox.py:65` | Hardcoded `type` и `tag`, всегда одно и то же |
| 6 | `ServiceState` / `UpdateResult` | `self_update.py:26,67` | NamedTuple сразу распаковывается |

**Исправление**: Заменить на dict-литералы или `dataclass`. Pydantic оправдан только для `Config`, `Node`, `Profile`.

### Обёрточные функции (делают ровно то же)

| # | Функция | Файл | Просто вызывает |
|---|---------|------|---------------|
| 1 | `start_proxy` / `start_free_proxy` | `proxy.py:35-44` | `_launch(config_gen, bin)` |
| 2 | `_log` | `bridge.py:45-47` | `events.info(msg)` |
| 3 | `_running_level` | `bridge.py:205-206` | `cfg.active_level != NONE and check(cfg)` |
| 4 | `refresh_profile` | `refresh.py:19-29` | `fetch_profile_nodes()` |
| 5 | `_progress_reports` | `cli.py:698-719` | Два closure, отличающиеся одним полем |
| 6 | `save_pid` / `load_pid` | `proxy.py:83-95` | `_write_pid` / чтение с разным path |
| 7 | `save_daemon_pid` / `load_daemon_pid` | `proxy.py:98-110` | То же, что и выше |

**Исправление**: Удалить обёртки, вызвать оригинал напрямую.

### Абстракции, добавляющие ничего

| # | Что | Файл | Почему не нужно |
|---|-----|------|---------------|
| 1 | `Bridge(ThreadingHTTPServer)` | `bridge.py:386` | Наследует только чтобы передать `BridgeHandler` в `__init__` |
| 2 | `UpstreamError(Exception)` | `bridge.py:60` | Used once; `httpx.HTTPError` suffice |
| 3 | `Stage` / `ProgressFn` type aliases | `verify.py:36-37` | Используются < 5 раз; не улучшают читаемость |
| 4 | `update_config[T]` generic | `config.py:201` | Generic `[T]` добавляет сложность без benefit |
| 5 | `_running_level` callback | `bridge.py:205-206` | Called once |

**Исправление**: Снять наследование, удалить исключение, использовать inline-типы.

### Enum overuse

| # | Enum | Файл | Почему не нужен |
|---|------|------|---------------|
| 1 | `WarpStatus` | `warp.py:24-32` | `.value` везде; string constants эквивалентны |
| 2 | `ActiveLevel` | `models/enums.py` | `if cfg.active_level == ActiveLevel.WARP` ≡ `== "warp"` |
| 3 | `NodeProtocol` | `models/enums.py` | Используется в `if protocol in (HTTP, SOCKS5)` — string check works |

**Исправление**: String constants. Enums не дают валидации (значения приходят из YAML).

### Premature optimization

| # | Что | Файл | Почему не нужно |
|---|-----|------|---------------|
| 1 | Double-checked locking for `httpx.Client` | `bridge.py:157-195` | CLI tool, не high-concurrency сервер. Module-level init thread-safe |
| 2 | `_dedupe_by_ip` complex logic | `verify.py:182-200` | Could be a simple dict accumulation |
| 3 | `_file_lock` context manager | `config.py:97-109` | No-op on non-POSIX; for single-user CLI tool |

---

## 3. ДУБЛИРОВАНИЕ: КОГДА ОДНО И ТО ЖЕ ПОВТОРЯЕТСЯ

### Top-10 highest-impact redundancies

| # | Что | Где | Suggested fix |
|---|-----|-----|---------------|
| **1** | `cfg.load_config()` + `save_config()` цикл | `cli.py`, `cascade.py`, `probe.py`, `refresh.py`, `health.py` — **10+ мест** | `with cfg.transaction() as cfg_obj:` context manager |
| **2** | `HEALTH_URL` / `DEFAULT_URLTEST_URL` | `singbox.py:17`, `free.py:9`, `models/config.py:7` — **3 раза** | Один источник в `models/config.py` |
| **3** | `_get_egress_ip` | `singbox.py:242-248`, `free.py:74-80` — **идентичны** | Один shared utility |
| **4** | `httpx.Client` creation | `singbox.py`, `nodes.py`, `cascade.py`, `bridge.py`, `probe.py`, `free.py`, `warp.py`, `self_update.py` — **8+ раз** | `core/http.py` factory |
| **5** | `IPIFY_URL` | `warp.py:15`, hardcoded в 3 файлах | Один constant |
| **6** | `_write_pid` / `save_pid` / `load_pid` / `save_daemon_pid` / `load_daemon_pid` | `proxy.py:74-110` — 5 функций | `core/pid.py` utility |
| **7** | `subprocess.Popen` sing-box run | `singbox.py:207-211`, `proxy.py:21-25` — одинаковый паттерн | Extract `_run_singbox()` |
| **8** | `subprocess.run` sing-box check | `verify.py:86-88` | Extract `_check_singbox_config()` |
| **9** | `(host, port)` tuple | `warp.py`, `free.py`, `singbox.py`, `proxy.py`, `verify.py` — 5+ мест | `Endpoint` type alias |
| **10** | `"No nodes configured"` | `cli.py:249,286,548`, `cascade.py:130`, `probe.py:155` — **6 мест** | `MESSAGES` constant |

### Duplicate CLI test files

**`tests/unit/cli/test_cli.py` + `tests/unit/cli/test_cli_commands.py`** — `test_cli.py` (54 lines) tests `logs` command, overlapping with `test_cli_commands.py`. Consolidate.

### Duplicate `status` output

**`status --json --verbose`** vs **`warp status --json`** — `cli.py:358-465` vs `cli.py:800-822`. The `warp status` output is a subset of `status --json --verbose`.

---

## 4. НЕПРОТЕСТИРОВАННЫЕ И НЕВИСПОЛЬЗУЕМЫЕ ФИЧИ

### CLI commands without test coverage

| Команда | Файл | Статус |
|---------|------|--------|
| `config` (edit) | `cli.py:728-737` | Нисколько не тестируется |
| `self-update` | `cli.py:828-853` | Модуль тестируется, но CLI command нет |
| `warp install` (реальный путь) | `cli.py:760-770` | Тестируется только "already installed" |

### Features that exist but may not be needed

**1. `openrot config edit`** — Opens `$EDITOR` to edit YAML. Could be replaced by `openrot profile set` commands for most operations.

**2. `openrot self-update`** — Downloads and installs new binary. Complex code path with 0 CLI test coverage. If the project doesn't ship releases frequently, this is dead weight.

**3. `warp install` command** — Downloads WARP package. Only tested for "already installed" case. The actual download+install path is untested.

**4. `node_from_records()`** — Dead function (covered in Section 1).

**5. 20+ fields on `Config`** — Many are internal constants (`bridge_retry_statuses`, `bridge_max_concurrent`, `deduplicate_by_ip`) that users rarely change. These could be code defaults instead of persisted config fields.

---

## 5. КОНКРЕТНЫЙ ПЛАН РЕФАКТОРИНГА (по приоритету)

### Фаза 1: Удалить мёртвый код (1 день)
```
[x] Удалить node_from_records()
[x] Исправить check_for_update() updated=False bug
[x] Удалить TypeVar в verify.py
[x] Удалить пустые __init__.py (или добавить __all__)
[x] Удалить закомментированные enum'ы
```

### Фаза 2: Убрать over-engineering (2-3 дня)
```
[x] Заменить LogOptions, RealityTLSOptions, UTLSOptions,
TransportOptions, MixedInbound на dict construction
[x] Удалить обёрточные функции (start_proxy, _log, _running_level,
refresh_profile, save_pid/load_pid duplicates)
[x] Снять Bridge(ThreadingHTTPServer) наследование
[x] Заменить Enums на string constants (WarpStatus, ActiveLevel)
[x] Удалить update_config[T] generic
```

### Фаза 3: Устранить дублирование (2-3 дня)
```
[x] Создать cfg.transaction() context manager
[x] Вынести HEALTH_URL, IPIFY_URL в один constant-файл
[x] Объединить _get_egress_ip в один utility
[x] Создать core/http.py client factory
[x] Создать core/pid.py для PID-операций
[x] Объединить test_cli.py в test_cli_commands.py
```

### Фаза 4: Добавить тесты для существующих фич (1-2 дня)
```
[x] Добавить тесты для config_edit
[x] Добавить тесты для self_update_cmd
[x] Добавить тесты для warp install path
```

### Фаза 5: Config simplification (1 день)
```
[x] Вынести internal constants (bridge_retry_statuses, etc.)
из Config в код defaults
[x] Разделить Config на user-facing и internal
```

---

## 6. ОЦЕНКА РАЦИОНАЛЬНОСТИ КАЖДОГО МОДУЛЯ

### Модуль | Оценка | Комментарий
|---------|--------|-----------|
| `cli.py` (856 LOC) | ⚠️ Слишком большой | 7 ответственностей. Нужен split на `cli/profile.py`, `cli/warp.py`, `cli/status.py` |
| `core/cascade.py` (347 LOC) | ⚠️ Слишком большой | 6 ответственностей. Нужен split |
| `core/bridge.py` | ✅ Нормальный | Чистый модуль, хорошая документация |
| `core/verify.py` | ✅ Нормальный | Но 50 workers — слишком много |
| `core/health.py` | ⚠️ | `select_node()` здесь — не по теме |
| `core/probe.py` | ❌ Нужен удалить | Чисто презентационный код → `cli/probe.py` |
| `core/singbox.py` | ⚠️ | 6 Pydantic-моделей для dict-структур |
| `core/proxy.py` | ⚠️ | PID-операции должны быть отдельно |
| `core/refresh.py` | ✅ Нормальный | Чистый модуль |
| `core/daemon.py` | ✅ Нормальный | Чистый модуль |
| `models/config.py` | ✅ Нормальный | Центральная модель |
| `models/node.py` | ⚠️ | `Node.raw` — primitive obsession |
| `models/profile.py` | ✅ Нормальный | |
| `models/enums.py` | ⚠️ | Enums → string constants |
| `providers/vless.py` | ✅ Нормальный | Хороший парсер |
| `providers/warp.py` | ⚠️ | `WarpStatus` enum → string; boolean returns |
| `providers/free.py` | ✅ Нормальный | |
| `self_update.py` | ⚠️ | Слишком сложный; `_check_services`, `_stop_services`, `_restart_services` — inline |
| `config.py` | ⚠️ | Re-exports models; file locking could be simpler |
| `opencode.py` | ✅ Нормальный | Простой модуль |
| `signals.py` | ✅ Нормальный | Простой модуль |
| `log.py` | ✅ Нормальный | Простой модуль |
| `tests/unit/cli/test_cli.py` | ❌ Удалить | Дублирует test_cli_commands.py |

---

**Итого**: ~85 находок рациональности. Основной паттерн — избыточная сложность в ядре (Pydantic-модели для dict-структур, обёрточные функции, enum-оверкус), дублирование конфигурации, и несколько мёртвых компонентов. Устранение фазами 1-3 уберёт ~60% проблем.
32 changes: 27 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ image from GHCR:
curl -fsSL https://raw.githubusercontent.com/vispar-tech/openrot/main/install-docker.sh | bash
```

> The first method installs the standalone binary to `~/.local/bin/openrot`.
> The first method installs the standalone binary to `~/.local/lib/openrot`
> (symlink in `~/.local/bin/openrot`).
> If your distro already has the prerequisites, you can skip them:
> `OPENROT_SKIP_WARP=1 ... | bash`.

Expand Down Expand Up @@ -112,7 +113,6 @@ poetry run openrot stop
| `openrot warp on\|off\|install` | Enable/connect or disable/disconnect WARP |
| `openrot warp status [--json]` | Show WARP status |
| `openrot run -- <cmd>` | Run a command with the proxy env vars set |
| `openrot probe <url>` | Request `url` through the active stack, showing each stage |

## opencode (bridge)

Expand Down Expand Up @@ -160,6 +160,29 @@ The upstream endpoint and port are configurable — see the config fields
`https://opencode.ai/zen/v1`, the endpoint opencode's built-in provider uses
natively), or the env overrides `OPENROT_BRIDGE_PORT` and `OPENROT_UPSTREAM`.

### Free-tier session headers

opencode's free tier keys on the `X-Opencode-Session` header: a bare request
that lacks it lands in the anonymous quota and can be rejected with
`FreeUsageLimitError`. The bridge injects `X-Opencode-Session` (`ses_`+22
alnum) and `X-Opencode-Request` (`msg_`+24 alnum) on every upstream request
that doesn't already carry them, so scripts and `curl` hitting the bridge
share the free bucket instead of being rate-limited. Values sent by the client
are respected (never overwritten). Toggle this off with
`bridge_inject_session: false` in config or
`OPENROT_BRIDGE_INJECT_SESSION=0`:

```bash
# via the bridge: headers are added automatically
curl -s http://127.0.0.1:7891/v1/chat/completions \
-H 'Content-Type: application/json' -d '{"model":"opencode/sonnet","messages":[{"role":"user","content":"hi"}]}'

# hitting the upstream directly: add the header yourself
curl -s https://opencode.ai/zen/v1/chat/completions \
-H 'X-Opencode-Session: ses_<22 alnum>' \
-H 'Content-Type: application/json' -d '{"model":"opencode/sonnet","messages":[{"role":"user","content":"hi"}]}'
```

## Free sources (profiles)

Everything you add is a **profile**: a URL that returns a list of nodes.
Expand Down Expand Up @@ -279,9 +302,8 @@ in parallel), survivors are ranked by latency (the measured request time) and
the **best 20** (`TOP_LIMIT`) are published per profile.

Progress is printed live: `update` shows a per-stage counter/bar
(`verify parse: 3/3`, `verify probe: 12/100`), and `openrot probe <url>` prints
the same stage counts as plain lines. `openrot test` re-verifies the currently
published nodes through the same pipeline.
(`verify parse: 3/3`, `verify probe: 12/100`). `openrot test` re-verifies the
currently published nodes through the same pipeline.

## Strategies

Expand Down
6 changes: 2 additions & 4 deletions README.ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ poetry run openrot stop
| `openrot warp on\|off\|install` | Включить/выключить (подключить/отключить) WARP |
| `openrot warp status [--json]` | Статус WARP |
| `openrot run -- <cmd>` | Выполнить команду с переменными окружения прокси |
| `openrot probe <url>` | Прогнать `url` через активный стек, показывая каждый этап |

## opencode (bridge)

Expand Down Expand Up @@ -260,9 +259,8 @@ parse (дедуп vless:// / proto://host:port)
**топ-20** (`TOP_LIMIT`) на профиль.

Прогресс печатается вживую: `update` показывает постадийный счётчик/бар
(`verify parse: 3/3`, `verify probe: 12/100`), `openrot probe <url>` печатает
те же стадии строками. `openrot test` заново проверяет опубликованные ноды
тем же пайплайном.
(`verify parse: 3/3`, `verify probe: 12/100`). `openrot test` заново проверяет
опубликованные ноды тем же пайплайном.

## Стратегии

Expand Down
Loading
Loading