clashctl 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. clashctl-0.1.0/.github/workflows/pylint.yml +23 -0
  2. clashctl-0.1.0/.github/workflows/python-publish.yml +70 -0
  3. clashctl-0.1.0/.gitignore +220 -0
  4. clashctl-0.1.0/LICENSE +21 -0
  5. clashctl-0.1.0/PKG-INFO +170 -0
  6. clashctl-0.1.0/README.md +147 -0
  7. clashctl-0.1.0/pyproject.toml +55 -0
  8. clashctl-0.1.0/src/clashctl/__init__.py +1 -0
  9. clashctl-0.1.0/src/clashctl/__main__.py +4 -0
  10. clashctl-0.1.0/src/clashctl/api/__init__.py +20 -0
  11. clashctl-0.1.0/src/clashctl/api/client.py +164 -0
  12. clashctl-0.1.0/src/clashctl/api/errors.py +24 -0
  13. clashctl-0.1.0/src/clashctl/api/streams.py +45 -0
  14. clashctl-0.1.0/src/clashctl/cli.py +55 -0
  15. clashctl-0.1.0/src/clashctl/config/__init__.py +23 -0
  16. clashctl-0.1.0/src/clashctl/config/__main__.py +29 -0
  17. clashctl-0.1.0/src/clashctl/config/schema.py +135 -0
  18. clashctl-0.1.0/src/clashctl/config/store.py +56 -0
  19. clashctl-0.1.0/src/clashctl/models/__init__.py +28 -0
  20. clashctl-0.1.0/src/clashctl/models/base.py +24 -0
  21. clashctl-0.1.0/src/clashctl/models/config.py +37 -0
  22. clashctl-0.1.0/src/clashctl/models/connection.py +46 -0
  23. clashctl-0.1.0/src/clashctl/models/enums.py +115 -0
  24. clashctl-0.1.0/src/clashctl/models/log.py +20 -0
  25. clashctl-0.1.0/src/clashctl/models/proxy.py +94 -0
  26. clashctl-0.1.0/src/clashctl/models/rule.py +39 -0
  27. clashctl-0.1.0/src/clashctl/models/traffic.py +6 -0
  28. clashctl-0.1.0/src/clashctl/models/version.py +6 -0
  29. clashctl-0.1.0/src/clashctl/state/__init__.py +26 -0
  30. clashctl-0.1.0/src/clashctl/state/app_state.py +148 -0
  31. clashctl-0.1.0/src/clashctl/state/sort.py +280 -0
  32. clashctl-0.1.0/src/clashctl/state/speed.py +69 -0
  33. clashctl-0.1.0/src/clashctl/tui/__init__.py +3 -0
  34. clashctl-0.1.0/src/clashctl/tui/app.py +314 -0
  35. clashctl-0.1.0/src/clashctl/tui/messages.py +101 -0
  36. clashctl-0.1.0/src/clashctl/tui/poller.py +194 -0
  37. clashctl-0.1.0/src/clashctl/tui/screens/__init__.py +4 -0
  38. clashctl-0.1.0/src/clashctl/tui/screens/main.py +185 -0
  39. clashctl-0.1.0/src/clashctl/tui/screens/server_select.py +317 -0
  40. clashctl-0.1.0/src/clashctl/tui/theme.py +69 -0
  41. clashctl-0.1.0/src/clashctl/tui/widgets/__init__.py +15 -0
  42. clashctl-0.1.0/src/clashctl/tui/widgets/connection_list.py +101 -0
  43. clashctl-0.1.0/src/clashctl/tui/widgets/log_view.py +41 -0
  44. clashctl-0.1.0/src/clashctl/tui/widgets/movable_table.py +191 -0
  45. clashctl-0.1.0/src/clashctl/tui/widgets/proxy_tree.py +262 -0
  46. clashctl-0.1.0/src/clashctl/tui/widgets/rule_list.py +74 -0
  47. clashctl-0.1.0/src/clashctl/tui/widgets/status_panel.py +170 -0
  48. clashctl-0.1.0/src/clashctl/utils/__init__.py +0 -0
  49. clashctl-0.1.0/src/clashctl/utils/bytesize.py +47 -0
  50. clashctl-0.1.0/src/clashctl/utils/duration.py +27 -0
  51. clashctl-0.1.0/tests/__init__.py +0 -0
  52. clashctl-0.1.0/tests/conftest.py +63 -0
  53. clashctl-0.1.0/tests/fixtures/configs.json +13 -0
  54. clashctl-0.1.0/tests/fixtures/connections.json +24 -0
  55. clashctl-0.1.0/tests/fixtures/delay.json +1 -0
  56. clashctl-0.1.0/tests/fixtures/logs.ndjson +4 -0
  57. clashctl-0.1.0/tests/fixtures/proxies.json +48 -0
  58. clashctl-0.1.0/tests/fixtures/rules.json +9 -0
  59. clashctl-0.1.0/tests/fixtures/traffic.ndjson +3 -0
  60. clashctl-0.1.0/tests/fixtures/version.json +1 -0
  61. clashctl-0.1.0/tests/test_api.py +237 -0
  62. clashctl-0.1.0/tests/test_app_state.py +214 -0
  63. clashctl-0.1.0/tests/test_bytesize.py +36 -0
  64. clashctl-0.1.0/tests/test_config.py +203 -0
  65. clashctl-0.1.0/tests/test_duration.py +38 -0
  66. clashctl-0.1.0/tests/test_models.py +225 -0
  67. clashctl-0.1.0/tests/test_phase5_widgets.py +272 -0
  68. clashctl-0.1.0/tests/test_poller.py +236 -0
  69. clashctl-0.1.0/tests/test_proxy_tree.py +325 -0
  70. clashctl-0.1.0/tests/test_server_screens.py +281 -0
  71. clashctl-0.1.0/tests/test_sort.py +250 -0
  72. clashctl-0.1.0/tests/test_state.py +104 -0
  73. clashctl-0.1.0/tests/test_theme.py +46 -0
  74. clashctl-0.1.0/tests/test_tui_widgets.py +151 -0
@@ -0,0 +1,23 @@
1
+ name: Pylint
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ strategy:
9
+ matrix:
10
+ python-version: ["3.11"]
11
+ steps:
12
+ - uses: actions/checkout@v4
13
+ - name: Set up Python ${{ matrix.python-version }}
14
+ uses: actions/setup-python@v3
15
+ with:
16
+ python-version: ${{ matrix.python-version }}
17
+ - name: Install dependencies
18
+ run: |
19
+ python -m pip install --upgrade pip
20
+ pip install pylint
21
+ - name: Analysing the code with pylint
22
+ run: |
23
+ pylint $(git ls-files '*.py')
@@ -0,0 +1,70 @@
1
+ # This workflow will upload a Python Package to PyPI when a release is created
2
+ # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python#publishing-to-package-registries
3
+
4
+ # This workflow uses actions that are not certified by GitHub.
5
+ # They are provided by a third-party and are governed by
6
+ # separate terms of service, privacy policy, and support
7
+ # documentation.
8
+
9
+ name: Upload Python Package
10
+
11
+ on:
12
+ release:
13
+ types: [published]
14
+
15
+ permissions:
16
+ contents: read
17
+
18
+ jobs:
19
+ release-build:
20
+ runs-on: ubuntu-latest
21
+
22
+ steps:
23
+ - uses: actions/checkout@v4
24
+
25
+ - uses: actions/setup-python@v5
26
+ with:
27
+ python-version: "3.x"
28
+
29
+ - name: Build release distributions
30
+ run: |
31
+ # NOTE: put your own distribution build steps here.
32
+ python -m pip install build
33
+ python -m build
34
+
35
+ - name: Upload distributions
36
+ uses: actions/upload-artifact@v4
37
+ with:
38
+ name: release-dists
39
+ path: dist/
40
+
41
+ pypi-publish:
42
+ runs-on: ubuntu-latest
43
+ needs:
44
+ - release-build
45
+ permissions:
46
+ # IMPORTANT: this permission is mandatory for trusted publishing
47
+ id-token: write
48
+
49
+ # Dedicated environments with protections for publishing are strongly recommended.
50
+ # For more information, see: https://docs.github.com/en/actions/deployment/targeting-different-environments/using-environments-for-deployment#deployment-protection-rules
51
+ environment:
52
+ name: pypi
53
+ # OPTIONAL: uncomment and update to include your PyPI project URL in the deployment status:
54
+ # url: https://pypi.org/p/YOURPROJECT
55
+ #
56
+ # ALTERNATIVE: if your GitHub Release name is the PyPI project version string
57
+ # ALTERNATIVE: exactly, uncomment the following line instead:
58
+ # url: https://pypi.org/project/YOURPROJECT/${{ github.event.release.name }}
59
+
60
+ steps:
61
+ - name: Retrieve release distributions
62
+ uses: actions/download-artifact@v4
63
+ with:
64
+ name: release-dists
65
+ path: dist/
66
+
67
+ - name: Publish release distributions to PyPI
68
+ uses: pypa/gh-action-pypi-publish@release/v1
69
+ with:
70
+ packages-dir: dist/
@@ -0,0 +1,220 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[codz]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py.cover
50
+ *.lcov
51
+ .hypothesis/
52
+ .pytest_cache/
53
+ cover/
54
+
55
+ # Translations
56
+ *.mo
57
+ *.pot
58
+
59
+ # Django stuff:
60
+ *.log
61
+ local_settings.py
62
+ db.sqlite3
63
+ db.sqlite3-journal
64
+
65
+ # Flask stuff:
66
+ instance/
67
+ .webassets-cache
68
+
69
+ # Scrapy stuff:
70
+ .scrapy
71
+
72
+ # Sphinx documentation
73
+ docs/_build/
74
+
75
+ # PyBuilder
76
+ .pybuilder/
77
+ target/
78
+
79
+ # Jupyter Notebook
80
+ .ipynb_checkpoints
81
+
82
+ # IPython
83
+ profile_default/
84
+ ipython_config.py
85
+
86
+ # pyenv
87
+ # For a library or package, you might want to ignore these files since the code is
88
+ # intended to run in multiple environments; otherwise, check them in:
89
+ # .python-version
90
+
91
+ # pipenv
92
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
93
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
94
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
95
+ # install all needed dependencies.
96
+ # Pipfile.lock
97
+
98
+ # UV
99
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
100
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
101
+ # commonly ignored for libraries.
102
+ # uv.lock
103
+
104
+ # poetry
105
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
106
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
107
+ # commonly ignored for libraries.
108
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
109
+ # poetry.lock
110
+ # poetry.toml
111
+
112
+ # pdm
113
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
114
+ # pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
115
+ # https://pdm-project.org/en/latest/usage/project/#working-with-version-control
116
+ # pdm.lock
117
+ # pdm.toml
118
+ .pdm-python
119
+ .pdm-build/
120
+
121
+ # pixi
122
+ # Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
123
+ # pixi.lock
124
+ # Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
125
+ # in the .venv directory. It is recommended not to include this directory in version control.
126
+ .pixi/*
127
+ !.pixi/config.toml
128
+
129
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
130
+ __pypackages__/
131
+
132
+ # Celery stuff
133
+ celerybeat-schedule*
134
+ celerybeat.pid
135
+
136
+ # Redis
137
+ *.rdb
138
+ *.aof
139
+ *.pid
140
+
141
+ # RabbitMQ
142
+ mnesia/
143
+ rabbitmq/
144
+ rabbitmq-data/
145
+
146
+ # ActiveMQ
147
+ activemq-data/
148
+
149
+ # SageMath parsed files
150
+ *.sage.py
151
+
152
+ # Environments
153
+ .env
154
+ .envrc
155
+ .venv
156
+ env/
157
+ venv/
158
+ ENV/
159
+ env.bak/
160
+ venv.bak/
161
+
162
+ # Spyder project settings
163
+ .spyderproject
164
+ .spyproject
165
+
166
+ # Rope project settings
167
+ .ropeproject
168
+
169
+ # mkdocs documentation
170
+ /site
171
+
172
+ # mypy
173
+ .mypy_cache/
174
+ .dmypy.json
175
+ dmypy.json
176
+
177
+ # Pyre type checker
178
+ .pyre/
179
+
180
+ # pytype static type analyzer
181
+ .pytype/
182
+
183
+ # Cython debug symbols
184
+ cython_debug/
185
+
186
+ # PyCharm
187
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
188
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
189
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
190
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
191
+ # .idea/
192
+
193
+ # Abstra
194
+ # Abstra is an AI-powered process automation framework.
195
+ # Ignore directories containing user credentials, local state, and settings.
196
+ # Learn more at https://abstra.io/docs
197
+ .abstra/
198
+
199
+ # Visual Studio Code
200
+ # Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
201
+ # that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
202
+ # and can be added to the global gitignore or merged into this file. However, if you prefer,
203
+ # you could uncomment the following to ignore the entire vscode folder
204
+ # .vscode/
205
+ # Temporary file for partial code execution
206
+ tempCodeRunnerFile.py
207
+
208
+ # Ruff stuff:
209
+ .ruff_cache/
210
+
211
+ # PyPI configuration file
212
+ .pypirc
213
+
214
+ # Marimo
215
+ marimo/_static/
216
+ marimo/_lsp/
217
+ __marimo__/
218
+
219
+ # Streamlit
220
+ .streamlit/secrets.toml
clashctl-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Zixuan Tan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,170 @@
1
+ Metadata-Version: 2.4
2
+ Name: clashctl
3
+ Version: 0.1.0
4
+ Summary: TUI for the Clash/Mihomo proxy
5
+ Author-email: Zixuan Tan <tanzixuan.me@gmail.com>
6
+ License-Expression: MIT
7
+ License-File: LICENSE
8
+ Requires-Python: >=3.11
9
+ Requires-Dist: httpx>=0.27
10
+ Requires-Dist: platformdirs>=4.2
11
+ Requires-Dist: pydantic-settings>=2.3
12
+ Requires-Dist: pydantic>=2.7
13
+ Requires-Dist: textual>=0.85
14
+ Requires-Dist: tomli-w>=1.0
15
+ Provides-Extra: dev
16
+ Requires-Dist: mypy>=1.10; extra == 'dev'
17
+ Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
18
+ Requires-Dist: pytest-textual-snapshot>=0.4; extra == 'dev'
19
+ Requires-Dist: pytest>=8; extra == 'dev'
20
+ Requires-Dist: respx>=0.21; extra == 'dev'
21
+ Requires-Dist: ruff>=0.5; extra == 'dev'
22
+ Description-Content-Type: text/markdown
23
+
24
+ # clashctl
25
+
26
+ Python 3 rewrite of [clashctl](https://github.com/George-Miao/clashctl) — a [Textual](https://textual.textualize.io/)-based TUI for the [Clash](https://github.com/Dreamacro/clash) (and [mihomo](https://github.com/MetaCubeX/mihomo)) external-controller REST API.
27
+
28
+ A live dashboard with five tabs: **Status / Proxies / Rules / Conns / Logs**. Server management lives entirely inside the TUI — no `add`/`use`/`del` subcommands.
29
+
30
+ ```text
31
+ ┌─ Status ───────────────────────────────────────────────────┐
32
+ │ ⇉ Connections 12 │ ▲ ▁▁▃▂▄▇▆▆▅▇▆▆▆▆▇▆▅▆▇▆▆▇▇▆▇▆▆▇ │
33
+ │ ▲ Upload 12.4 KiB/s │ ▼ ▁▂▁▁▂▁▁▁▁▂▂▂▂▁▂▂▁▂▁▂▁▂▁▁▂▁▂▁ │
34
+ │ ▼ Download 1.2 MiB/s │ │
35
+ │ ▲ Avg. 9.1 KiB/s │ max ↑ 487 KiB/s ↓ 4.3 MiB/s │
36
+ │ ▼ Avg. 812 KiB/s │ │
37
+ │ Clash Ver. v1.18.0 │ │
38
+ └────────────────────────────────────────────────────────────┘
39
+ ```
40
+
41
+ ## Status
42
+
43
+ **MVP complete** — all five tabs functional, in-TUI server management, 213 unit tests, headless E2E verified.
44
+
45
+ Notable differences from the Rust original:
46
+
47
+ - **Real instantaneous connection speeds** (the Rust version reports `total_bytes / seconds_since_connection_start`, which mis-represents short bursts; here speeds are computed by diffing successive `/connections` polls).
48
+ - **`ConnSort` is actually implemented** (the Rust counterpart was `todo!()` for all 11 keys).
49
+ - **Config in TOML** instead of RON.
50
+ - **Server management lives in the TUI**, not in CLI subcommands.
51
+
52
+ ## Install
53
+
54
+ ```bash
55
+ git clone https://github.com/frezcirno/clashctl.git
56
+ cd clashctl
57
+ pip install -e ".[dev]" # editable + test deps
58
+ # or, with uv:
59
+ uv pip install -e ".[dev]"
60
+ ```
61
+
62
+ Python 3.11 or newer is required.
63
+
64
+ ## Run
65
+
66
+ ```bash
67
+ clashctl # default config path
68
+ clashctl -c /path/to/config.toml # custom path
69
+ clashctl --debug # verbose log to /tmp/clashctl.log
70
+ ```
71
+
72
+ On first launch with no configured server, a modal pops up and walks you through adding one. The form probes `/version` against the supplied URL before saving so typos and bad secrets are caught early.
73
+
74
+ ## Keybindings
75
+
76
+ ### Global (any tab)
77
+
78
+ | Key | Action |
79
+ | ------------- | ---------------------------------------------- |
80
+ | `q`, `Ctrl+C` | Quit |
81
+ | `1`–`5` | Switch tabs |
82
+ | `Ctrl+S` | Open the server picker (add / delete / switch) |
83
+
84
+ ### Tab-specific
85
+
86
+ | Tab | Key | Action |
87
+ | ------- | -------------------- | -------------------------------------------------------------------------------------------- |
88
+ | Proxies | `t` | Test latency for the focused group's normal members (parallel, capped at 8) |
89
+ | Proxies | `Enter` | On a member of a Selector group: switch to it (PUT `/proxies/<group>`) |
90
+ | Proxies | `s` / `Shift+s` | Cycle member sort (name / type / delay × asc / desc) |
91
+ | Rules | `s` / `Shift+s` | Cycle sort (payload / type / proxy × asc / desc) |
92
+ | Rules | `Space` | Toggle HOLD mode (cursor pinned across refreshes) |
93
+ | Rules | `↑↓` | Navigate (in HOLD) |
94
+ | Rules | `Esc` | Exit HOLD |
95
+ | Conns | `s` / `Shift+s` | Cycle sort across 11 keys (host, ▼, ▲, speeds, time, rule, chain, src, dst, type) × asc/desc |
96
+ | Conns | `Space`, `Esc`, `↑↓` | Same HOLD behavior as Rules |
97
+
98
+ ### Server picker
99
+
100
+ | Key | Action |
101
+ | ------------- | ------------------------------------------ |
102
+ | `a` | Add a new server (opens form) |
103
+ | `d` | Delete the focused server |
104
+ | `u` / `Enter` | Use the focused server (swaps connections) |
105
+ | `Esc` | Close (or quit on first run) |
106
+
107
+ ## Config
108
+
109
+ Stored at `~/.config/clashctl/config.toml` by default (respects `$XDG_CONFIG_HOME`; on macOS it lands under `~/Library/Application Support/clashctl/`). Override the path with `-c <path>` or `CLASHCTL_CONFIG_PATH=...`.
110
+
111
+ Schema:
112
+
113
+ ```toml
114
+ using = "http://127.0.0.1:9090"
115
+
116
+ [[servers]]
117
+ name = "local"
118
+ url = "http://127.0.0.1:9090"
119
+ secret = "" # empty == no auth
120
+
121
+ [[servers]]
122
+ name = "remote"
123
+ url = "https://proxy.example.com:9090"
124
+ secret = "hunter2"
125
+
126
+ [ui]
127
+ test_url = "http://www.gstatic.com/generate_204"
128
+ test_timeout_ms = 5000
129
+ log_buffer = 2000 # in-memory log line cap
130
+ traffic_history = 500 # sparkline samples
131
+ refresh_slow_secs = 5.0 # /version, /configs, /proxies, /rules
132
+ refresh_fast_secs = 1.0 # /connections
133
+
134
+ [ui.sort]
135
+ proxies = { by = "delay", order = "asc" }
136
+ rules = { by = "payload", order = "asc" }
137
+ connections = { by = "time", order = "desc" }
138
+ ```
139
+
140
+ The TOML can be hand-edited; the TUI rewrites it via `tomli_w` whenever you add/use/delete a server.
141
+
142
+ ## Architecture
143
+
144
+ ```text
145
+ src/clashctl/
146
+ ├── api/ # httpx async client + line-delimited JSON streams
147
+ ├── models/ # pydantic v2 models for every Clash payload
148
+ ├── state/ # AppState (single source of truth) + sort + speed tracker
149
+ ├── config/ # TOML load/save + AppConfig schema
150
+ ├── tui/ # Textual app, screens, widgets, polling worker
151
+ └── utils/ # bytesize / duration helpers
152
+ ```
153
+
154
+ - **Pollers** are plain asyncio classes that post `Message`s up the Textual tree; the App reduces messages into `AppState` and tells the active screen what to refresh.
155
+ - **Layering rule**: `api/`, `models/`, `state/`, `config/` never import Textual — they're unit-testable without spinning up an `App`.
156
+ - **Hidden tabs**: `RichLog.write()` produces empty strips when the widget has no width, so the Logs tab buffers in `AppState.logs` and replays on tab activation.
157
+
158
+ ## Test
159
+
160
+ ```bash
161
+ pytest # 213 tests, ~9s
162
+ ruff check src tests # lint
163
+ mypy src # type check (strict mode)
164
+ ```
165
+
166
+ Tests are split between pure unit coverage (models, sort, speed tracker, config round-trip) and Textual `run_test()` harnesses for widgets and screens. The API client is exercised against `httpx.MockTransport` with recorded fixtures under `tests/fixtures/`.
167
+
168
+ ## License
169
+
170
+ MIT — see [LICENSE](../LICENSE).
@@ -0,0 +1,147 @@
1
+ # clashctl
2
+
3
+ Python 3 rewrite of [clashctl](https://github.com/George-Miao/clashctl) — a [Textual](https://textual.textualize.io/)-based TUI for the [Clash](https://github.com/Dreamacro/clash) (and [mihomo](https://github.com/MetaCubeX/mihomo)) external-controller REST API.
4
+
5
+ A live dashboard with five tabs: **Status / Proxies / Rules / Conns / Logs**. Server management lives entirely inside the TUI — no `add`/`use`/`del` subcommands.
6
+
7
+ ```text
8
+ ┌─ Status ───────────────────────────────────────────────────┐
9
+ │ ⇉ Connections 12 │ ▲ ▁▁▃▂▄▇▆▆▅▇▆▆▆▆▇▆▅▆▇▆▆▇▇▆▇▆▆▇ │
10
+ │ ▲ Upload 12.4 KiB/s │ ▼ ▁▂▁▁▂▁▁▁▁▂▂▂▂▁▂▂▁▂▁▂▁▂▁▁▂▁▂▁ │
11
+ │ ▼ Download 1.2 MiB/s │ │
12
+ │ ▲ Avg. 9.1 KiB/s │ max ↑ 487 KiB/s ↓ 4.3 MiB/s │
13
+ │ ▼ Avg. 812 KiB/s │ │
14
+ │ Clash Ver. v1.18.0 │ │
15
+ └────────────────────────────────────────────────────────────┘
16
+ ```
17
+
18
+ ## Status
19
+
20
+ **MVP complete** — all five tabs functional, in-TUI server management, 213 unit tests, headless E2E verified.
21
+
22
+ Notable differences from the Rust original:
23
+
24
+ - **Real instantaneous connection speeds** (the Rust version reports `total_bytes / seconds_since_connection_start`, which mis-represents short bursts; here speeds are computed by diffing successive `/connections` polls).
25
+ - **`ConnSort` is actually implemented** (the Rust counterpart was `todo!()` for all 11 keys).
26
+ - **Config in TOML** instead of RON.
27
+ - **Server management lives in the TUI**, not in CLI subcommands.
28
+
29
+ ## Install
30
+
31
+ ```bash
32
+ git clone https://github.com/frezcirno/clashctl.git
33
+ cd clashctl
34
+ pip install -e ".[dev]" # editable + test deps
35
+ # or, with uv:
36
+ uv pip install -e ".[dev]"
37
+ ```
38
+
39
+ Python 3.11 or newer is required.
40
+
41
+ ## Run
42
+
43
+ ```bash
44
+ clashctl # default config path
45
+ clashctl -c /path/to/config.toml # custom path
46
+ clashctl --debug # verbose log to /tmp/clashctl.log
47
+ ```
48
+
49
+ On first launch with no configured server, a modal pops up and walks you through adding one. The form probes `/version` against the supplied URL before saving so typos and bad secrets are caught early.
50
+
51
+ ## Keybindings
52
+
53
+ ### Global (any tab)
54
+
55
+ | Key | Action |
56
+ | ------------- | ---------------------------------------------- |
57
+ | `q`, `Ctrl+C` | Quit |
58
+ | `1`–`5` | Switch tabs |
59
+ | `Ctrl+S` | Open the server picker (add / delete / switch) |
60
+
61
+ ### Tab-specific
62
+
63
+ | Tab | Key | Action |
64
+ | ------- | -------------------- | -------------------------------------------------------------------------------------------- |
65
+ | Proxies | `t` | Test latency for the focused group's normal members (parallel, capped at 8) |
66
+ | Proxies | `Enter` | On a member of a Selector group: switch to it (PUT `/proxies/<group>`) |
67
+ | Proxies | `s` / `Shift+s` | Cycle member sort (name / type / delay × asc / desc) |
68
+ | Rules | `s` / `Shift+s` | Cycle sort (payload / type / proxy × asc / desc) |
69
+ | Rules | `Space` | Toggle HOLD mode (cursor pinned across refreshes) |
70
+ | Rules | `↑↓` | Navigate (in HOLD) |
71
+ | Rules | `Esc` | Exit HOLD |
72
+ | Conns | `s` / `Shift+s` | Cycle sort across 11 keys (host, ▼, ▲, speeds, time, rule, chain, src, dst, type) × asc/desc |
73
+ | Conns | `Space`, `Esc`, `↑↓` | Same HOLD behavior as Rules |
74
+
75
+ ### Server picker
76
+
77
+ | Key | Action |
78
+ | ------------- | ------------------------------------------ |
79
+ | `a` | Add a new server (opens form) |
80
+ | `d` | Delete the focused server |
81
+ | `u` / `Enter` | Use the focused server (swaps connections) |
82
+ | `Esc` | Close (or quit on first run) |
83
+
84
+ ## Config
85
+
86
+ Stored at `~/.config/clashctl/config.toml` by default (respects `$XDG_CONFIG_HOME`; on macOS it lands under `~/Library/Application Support/clashctl/`). Override the path with `-c <path>` or `CLASHCTL_CONFIG_PATH=...`.
87
+
88
+ Schema:
89
+
90
+ ```toml
91
+ using = "http://127.0.0.1:9090"
92
+
93
+ [[servers]]
94
+ name = "local"
95
+ url = "http://127.0.0.1:9090"
96
+ secret = "" # empty == no auth
97
+
98
+ [[servers]]
99
+ name = "remote"
100
+ url = "https://proxy.example.com:9090"
101
+ secret = "hunter2"
102
+
103
+ [ui]
104
+ test_url = "http://www.gstatic.com/generate_204"
105
+ test_timeout_ms = 5000
106
+ log_buffer = 2000 # in-memory log line cap
107
+ traffic_history = 500 # sparkline samples
108
+ refresh_slow_secs = 5.0 # /version, /configs, /proxies, /rules
109
+ refresh_fast_secs = 1.0 # /connections
110
+
111
+ [ui.sort]
112
+ proxies = { by = "delay", order = "asc" }
113
+ rules = { by = "payload", order = "asc" }
114
+ connections = { by = "time", order = "desc" }
115
+ ```
116
+
117
+ The TOML can be hand-edited; the TUI rewrites it via `tomli_w` whenever you add/use/delete a server.
118
+
119
+ ## Architecture
120
+
121
+ ```text
122
+ src/clashctl/
123
+ ├── api/ # httpx async client + line-delimited JSON streams
124
+ ├── models/ # pydantic v2 models for every Clash payload
125
+ ├── state/ # AppState (single source of truth) + sort + speed tracker
126
+ ├── config/ # TOML load/save + AppConfig schema
127
+ ├── tui/ # Textual app, screens, widgets, polling worker
128
+ └── utils/ # bytesize / duration helpers
129
+ ```
130
+
131
+ - **Pollers** are plain asyncio classes that post `Message`s up the Textual tree; the App reduces messages into `AppState` and tells the active screen what to refresh.
132
+ - **Layering rule**: `api/`, `models/`, `state/`, `config/` never import Textual — they're unit-testable without spinning up an `App`.
133
+ - **Hidden tabs**: `RichLog.write()` produces empty strips when the widget has no width, so the Logs tab buffers in `AppState.logs` and replays on tab activation.
134
+
135
+ ## Test
136
+
137
+ ```bash
138
+ pytest # 213 tests, ~9s
139
+ ruff check src tests # lint
140
+ mypy src # type check (strict mode)
141
+ ```
142
+
143
+ Tests are split between pure unit coverage (models, sort, speed tracker, config round-trip) and Textual `run_test()` harnesses for widgets and screens. The API client is exercised against `httpx.MockTransport` with recorded fixtures under `tests/fixtures/`.
144
+
145
+ ## License
146
+
147
+ MIT — see [LICENSE](../LICENSE).