opencode-unlimited 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 (43) hide show
  1. opencode_unlimited-0.1.0/.gitignore +25 -0
  2. opencode_unlimited-0.1.0/CHANGELOG.md +42 -0
  3. opencode_unlimited-0.1.0/LICENSE +21 -0
  4. opencode_unlimited-0.1.0/PKG-INFO +283 -0
  5. opencode_unlimited-0.1.0/README.md +220 -0
  6. opencode_unlimited-0.1.0/docs/architecture.md +77 -0
  7. opencode_unlimited-0.1.0/docs/configuration.md +50 -0
  8. opencode_unlimited-0.1.0/docs/testing.md +54 -0
  9. opencode_unlimited-0.1.0/docs/tor.md +70 -0
  10. opencode_unlimited-0.1.0/docs/troubleshooting.md +45 -0
  11. opencode_unlimited-0.1.0/pyproject.toml +83 -0
  12. opencode_unlimited-0.1.0/src/opencode_unlimited/__init__.py +12 -0
  13. opencode_unlimited-0.1.0/src/opencode_unlimited/__main__.py +6 -0
  14. opencode_unlimited-0.1.0/src/opencode_unlimited/cli.py +921 -0
  15. opencode_unlimited-0.1.0/src/opencode_unlimited/config.py +213 -0
  16. opencode_unlimited-0.1.0/src/opencode_unlimited/constants.py +113 -0
  17. opencode_unlimited-0.1.0/src/opencode_unlimited/daemon.py +223 -0
  18. opencode_unlimited-0.1.0/src/opencode_unlimited/dashboard/__init__.py +1 -0
  19. opencode_unlimited-0.1.0/src/opencode_unlimited/dashboard/server.py +503 -0
  20. opencode_unlimited-0.1.0/src/opencode_unlimited/egress/__init__.py +0 -0
  21. opencode_unlimited-0.1.0/src/opencode_unlimited/egress/clean_endpoint.py +182 -0
  22. opencode_unlimited-0.1.0/src/opencode_unlimited/egress/tor_installer.py +379 -0
  23. opencode_unlimited-0.1.0/src/opencode_unlimited/egress/tor_manager.py +372 -0
  24. opencode_unlimited-0.1.0/src/opencode_unlimited/egress/warp_keeper.py +208 -0
  25. opencode_unlimited-0.1.0/src/opencode_unlimited/guards/__init__.py +0 -0
  26. opencode_unlimited-0.1.0/src/opencode_unlimited/guards/auto_resume.py +267 -0
  27. opencode_unlimited-0.1.0/src/opencode_unlimited/guards/zen_guard.py +269 -0
  28. opencode_unlimited-0.1.0/src/opencode_unlimited/launcher.py +379 -0
  29. opencode_unlimited-0.1.0/src/opencode_unlimited/pool/__init__.py +1 -0
  30. opencode_unlimited-0.1.0/src/opencode_unlimited/pool/account_pool.py +235 -0
  31. opencode_unlimited-0.1.0/src/opencode_unlimited/proxy/__init__.py +1 -0
  32. opencode_unlimited-0.1.0/src/opencode_unlimited/proxy/proxy_chain.py +344 -0
  33. opencode_unlimited-0.1.0/src/opencode_unlimited/proxy/zen_proxy.py +701 -0
  34. opencode_unlimited-0.1.0/tests/__init__.py +0 -0
  35. opencode_unlimited-0.1.0/tests/test_cli.py +101 -0
  36. opencode_unlimited-0.1.0/tests/test_daemon.py +65 -0
  37. opencode_unlimited-0.1.0/tests/test_lane_config.py +61 -0
  38. opencode_unlimited-0.1.0/tests/test_launcher.py +32 -0
  39. opencode_unlimited-0.1.0/tests/test_network_flow.py +493 -0
  40. opencode_unlimited-0.1.0/tests/test_proxy_chain.py +142 -0
  41. opencode_unlimited-0.1.0/tests/test_tor.py +236 -0
  42. opencode_unlimited-0.1.0/tests/test_zen_proxy.py +134 -0
  43. opencode_unlimited-0.1.0/tests/test_zen_proxy_tunnel.py +324 -0
@@ -0,0 +1,25 @@
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ *.egg-info/
5
+ build/
6
+ dist/
7
+ .eggs/
8
+
9
+ # Environments
10
+ .venv/
11
+ venv/
12
+ .env
13
+
14
+ # Tooling
15
+ .pytest_cache/
16
+ .ruff_cache/
17
+ .mypy_cache/
18
+ .coverage
19
+ htmlcov/
20
+
21
+ # Editors / OS
22
+ .vscode/
23
+ .idea/
24
+ .DS_Store
25
+ Thumbs.db
@@ -0,0 +1,42 @@
1
+ # Changelog
2
+
3
+ All notable changes are listed here. The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and the project uses [Semantic Versioning](https://semver.org/).
4
+
5
+ ## [Unreleased]
6
+
7
+ ## [0.1.0] - 2026-11-29
8
+
9
+ ### Added
10
+
11
+ - `ocu tor status`, `ocu tor install`, `ocu tor path`, and `ocu tor renew`.
12
+ - Tor installer: the official Tor Expert Bundle on Windows (SHA-256 verified, extracted to `%LOCALAPPDATA%\opencode-unlimited\tor`), with an optional user-PATH update. Package managers on macOS, Linux, and Termux.
13
+ - `ocu start` / `ocu run` offer to install Tor when the Tor lane is on and Tor is missing.
14
+ - Reuse of an existing Tor already listening on `127.0.0.1:9050`.
15
+ - CONNECT failover: if a lane can't open the tunnel, the next lane is tried before returning 502.
16
+ - Streaming pass-through for plain-HTTP responses (server-sent events are no longer buffered).
17
+ - Network-flow test suite (real TLS, fake SOCKS5 lanes, simulated per-lane rate limits), plus docs, contributing guide, code of conduct, and security policy.
18
+
19
+ ### Fixed
20
+
21
+ - HTTPS never worked: aiohttp could not route `CONNECT host:443`, so every tunnel got a 404. The proxy now runs on asyncio streams.
22
+ - Every plain-HTTP request crashed on httpx 0.28 (`proxies=` argument removed).
23
+ - The 429 handler called the guard without its required arguments, and one 429 rotated two lanes.
24
+ - The proxy followed redirects itself instead of passing 3xx responses to the client.
25
+ - The default `lanes_priority` (`clean`) was rejected by the lane manager, so WARP was used first.
26
+ - The clean-endpoint lane pointed SOCKS at public DNS IPs (`1.1.1.1:40000`) and always failed.
27
+ - Lanes that were disabled or whose service failed to start stayed in rotation.
28
+ - Unquoted torrc paths broke Tor on profiles with spaces (for example `C:\Users\Jane Doe`).
29
+ - Tor could stall because its stdout pipe was never drained after bootstrap.
30
+ - Cold Tor bootstraps over 90 seconds were killed. The timeout now resets on progress (5 minute cap).
31
+ - `ocu stop` left `tor.exe` running on Windows.
32
+ - `ocu config set` crashed on every call (it used a pydantic API on a dataclass).
33
+ - `ocu start -d` printed the wrong PID and reported success when the daemon had crashed.
34
+ - `ocu run` could start OpenCode before the proxy was listening.
35
+ - `ocu run` was bypassed by OpenCode 2.x's background service. OpenCode is now launched with `--standalone`.
36
+ - AutoResume treated "no local health port" as a lockout on every poll and deleted files in OpenCode's data directories.
37
+ - Library modules no longer override the application's logging configuration on import.
38
+ - Direct-lane tunnels race IPv6 and IPv4 (Happy Eyeballs) instead of waiting about 2 seconds on a refused address.
39
+ - One shared TLS context for upstream clients (about 0.7 s saved on the first request after each rotation).
40
+
41
+ [Unreleased]: https://github.com/mithun50/opencode-unlimited/compare/v0.1.0...HEAD
42
+ [0.1.0]: https://github.com/mithun50/opencode-unlimited/releases/tag/v0.1.0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mithun Gowda B (mithungowda.b7411@gmail.com)
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,283 @@
1
+ Metadata-Version: 2.5
2
+ Name: opencode-unlimited
3
+ Version: 0.1.0
4
+ Summary: Local HTTP/HTTPS proxy for OpenCode with Tor, WARP, and direct egress lanes and automatic failover.
5
+ Project-URL: Homepage, https://github.com/mithun50/opencode-unlimited
6
+ Project-URL: Documentation, https://github.com/mithun50/opencode-unlimited/tree/main/docs
7
+ Project-URL: Repository, https://github.com/mithun50/opencode-unlimited
8
+ Project-URL: Issues, https://github.com/mithun50/opencode-unlimited/issues
9
+ Project-URL: Changelog, https://github.com/mithun50/opencode-unlimited/blob/main/CHANGELOG.md
10
+ Project-URL: Security, https://github.com/mithun50/opencode-unlimited/blob/main/SECURITY.md
11
+ Author-email: Mithun Gowda B <mithungowda.b7411@gmail.com>
12
+ License: MIT License
13
+
14
+ Copyright (c) 2026 Mithun Gowda B (mithungowda.b7411@gmail.com)
15
+
16
+ Permission is hereby granted, free of charge, to any person obtaining a copy
17
+ of this software and associated documentation files (the "Software"), to deal
18
+ in the Software without restriction, including without limitation the rights
19
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20
+ copies of the Software, and to permit persons to whom the Software is
21
+ furnished to do so, subject to the following conditions:
22
+
23
+ The above copyright notice and this permission notice shall be included in all
24
+ copies or substantial portions of the Software.
25
+
26
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32
+ SOFTWARE.
33
+ License-File: LICENSE
34
+ Keywords: cli,opencode,proxy,rate-limit,termux,tor,warp
35
+ Classifier: Development Status :: 3 - Alpha
36
+ Classifier: Environment :: Console
37
+ Classifier: Intended Audience :: Developers
38
+ Classifier: License :: OSI Approved :: MIT License
39
+ Classifier: Operating System :: OS Independent
40
+ Classifier: Programming Language :: Python :: 3
41
+ Classifier: Programming Language :: Python :: 3.10
42
+ Classifier: Programming Language :: Python :: 3.11
43
+ Classifier: Programming Language :: Python :: 3.12
44
+ Classifier: Programming Language :: Python :: 3.13
45
+ Classifier: Topic :: Internet :: Proxy Servers
46
+ Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
47
+ Requires-Python: >=3.10
48
+ Requires-Dist: aiohttp>=3.9
49
+ Requires-Dist: httpx[socks]>=0.27
50
+ Requires-Dist: rich>=13
51
+ Requires-Dist: stem>=1.8
52
+ Requires-Dist: tomli>=2.0; python_version < '3.11'
53
+ Requires-Dist: typer>=0.12
54
+ Provides-Extra: dashboard
55
+ Requires-Dist: fastapi>=0.110; extra == 'dashboard'
56
+ Requires-Dist: uvicorn>=0.29; extra == 'dashboard'
57
+ Provides-Extra: dev
58
+ Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
59
+ Requires-Dist: pytest>=8.0; extra == 'dev'
60
+ Requires-Dist: ruff>=0.4; extra == 'dev'
61
+ Requires-Dist: trustme>=1.1; extra == 'dev'
62
+ Description-Content-Type: text/markdown
63
+
64
+ # opencode-unlimited
65
+
66
+ [![PyPI version](https://img.shields.io/pypi/v/opencode-unlimited.svg)](https://pypi.org/project/opencode-unlimited/)
67
+ [![Python](https://img.shields.io/pypi/pyversions/opencode-unlimited.svg)](https://pypi.org/project/opencode-unlimited/)
68
+ [![CI](https://github.com/mithun50/opencode-unlimited/actions/workflows/ci.yml/badge.svg)](https://github.com/mithun50/opencode-unlimited/actions/workflows/ci.yml)
69
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/mithun50/opencode-unlimited/blob/main/LICENSE)
70
+ [![Code style: ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
71
+
72
+ **opencode-unlimited** (`ocu`) is a local HTTP/HTTPS proxy for [OpenCode](https://opencode.ai). It sends OpenCode's traffic out through one of several egress "lanes" (direct, Cloudflare WARP, Tor) and moves to the next lane when one fails or, for plain-HTTP traffic, returns `429 Too Many Requests`.
73
+
74
+ > **Know the limits.** Quotas tied to your OpenCode account or API key are counted per account, not per IP address, so changing the egress lane does not reset them. HTTPS traffic is end-to-end encrypted, so `ocu` can't see 429 responses inside it (see [HTTPS and 429s](#https-and-429s)). Follow the terms of the services you use.
75
+
76
+ ---
77
+
78
+ ## Features
79
+
80
+ - Local forward proxy on `127.0.0.1:8877` that handles HTTPS `CONNECT` tunnels and plain HTTP, and streams server-sent events without buffering
81
+ - Egress lanes: direct, Cloudflare WARP, and Tor, with failover when a lane can't connect
82
+ - On plain-HTTP `429`: records the event (with `Retry-After`), rotates to the next lane, and retries once
83
+ - Lanes that are disabled or whose service fails to start are removed from rotation
84
+ - `ocu tor install`: checks for Tor and installs it (the official Tor Expert Bundle verified by SHA-256 on Windows, your package manager elsewhere), and can add it to your PATH
85
+ - `ocu run`: finds or installs OpenCode, starts the proxy in the background, and launches OpenCode through it
86
+ - Optional web dashboard (FastAPI)
87
+ - Pure-Python config (stdlib dataclasses + TOML), so it installs cleanly on Termux
88
+
89
+ ---
90
+
91
+ ## Installation
92
+
93
+ ```bash
94
+ # Recommended (isolated, puts `ocu` on PATH)
95
+ pipx install opencode-unlimited
96
+
97
+ # Or with uv
98
+ uv tool install opencode-unlimited
99
+
100
+ # Or with pip inside a virtual environment
101
+ pip install opencode-unlimited
102
+
103
+ # With the web dashboard
104
+ pipx install "opencode-unlimited[dashboard]"
105
+ ```
106
+
107
+ Requires Python 3.10+. Tor is optional and `ocu` can install it for you. WARP is optional and needs `warp-cli`.
108
+
109
+ <details>
110
+ <summary>Platform notes (Windows, Debian/Ubuntu, Termux)</summary>
111
+
112
+ **Windows (PowerShell)**
113
+
114
+ ```powershell
115
+ python -m pip install --user pipx
116
+ python -m pipx ensurepath # then open a new terminal
117
+ pipx install opencode-unlimited
118
+ ```
119
+
120
+ **Debian / Ubuntu** (`externally-managed-environment` / PEP 668)
121
+
122
+ ```bash
123
+ sudo apt update && sudo apt install -y pipx
124
+ pipx ensurepath
125
+ pipx install opencode-unlimited
126
+ ```
127
+
128
+ **Termux (Android)**
129
+
130
+ ```bash
131
+ pkg install python tor
132
+ pip install opencode-unlimited
133
+ ```
134
+
135
+ </details>
136
+
137
+ ---
138
+
139
+ ## Quick start
140
+
141
+ ```bash
142
+ ocu run # start the proxy in the background and launch OpenCode through it
143
+ ocu run --desktop # same, but launch OpenCode Desktop
144
+ ocu start -d # only start the background proxy
145
+ ocu status # proxy, lanes, and account status
146
+ ocu tor status # is Tor installed / on PATH / listening?
147
+ ocu stop # stop the background proxy (and the Tor it started)
148
+ ```
149
+
150
+ To route any other tool through `ocu`, point it at the proxy:
151
+
152
+ ```bash
153
+ export HTTPS_PROXY=http://127.0.0.1:8877 HTTP_PROXY=http://127.0.0.1:8877
154
+ ```
155
+
156
+ ```powershell
157
+ $env:HTTPS_PROXY = "http://127.0.0.1:8877"; $env:HTTP_PROXY = "http://127.0.0.1:8877"
158
+ ```
159
+
160
+ > OpenCode 2.x normally sends work to a long-running background service that does not inherit these variables. `ocu run` therefore launches OpenCode with `--standalone`. If you launch OpenCode yourself, pass `--standalone` too, for example `opencode run --standalone "..."`.
161
+
162
+ ---
163
+
164
+ ## CLI commands
165
+
166
+ | Command | Description |
167
+ |---|---|
168
+ | `ocu run [-- ARGS]` | Find/install OpenCode, start the proxy in the background, launch OpenCode through it |
169
+ | `ocu setup` | Detect OpenCode CLI/Desktop and install or configure it |
170
+ | `ocu start [-d]` | Start the proxy stack (foreground with live status, or `-d` for background) |
171
+ | `ocu stop` | Stop the background proxy |
172
+ | `ocu status` | Show proxy status, lane health, and the account pool |
173
+ | `ocu logs [-f] [-n N]` | Show or follow the log file |
174
+ | `ocu dashboard [-p PORT]` | Run the web dashboard (needs the `dashboard` extra) |
175
+ | `ocu config show` / `set KEY VALUE` / `reset` | View, change, or reset the configuration |
176
+ | `ocu lanes list` | Show egress lanes and their health |
177
+ | `ocu lanes tor on/off`, `ocu lanes warp on/off` | Enable or disable a lane |
178
+ | `ocu tor status` | Show whether Tor is installed, on PATH, and listening |
179
+ | `ocu tor install [-y] [--no-path] [--force]` | Install Tor and (optionally) add it to PATH |
180
+ | `ocu tor path` | Add an installed Tor's folder to your user PATH (Windows) |
181
+ | `ocu tor renew` | Ask the running Tor for a new circuit |
182
+ | `ocu accounts list/add/remove/rotate` | Manage the local account pool |
183
+ | `ocu version` | Print the version |
184
+
185
+ Common options for `start` and `run`: `--proxy-port`, `--no-tor`, `--no-warp`, `--dashboard`.
186
+
187
+ ---
188
+
189
+ ## How it works
190
+
191
+ ```
192
+ OpenCode (HTTPS_PROXY=http://127.0.0.1:8877)
193
+ |
194
+ v
195
+ +------+--------+ CONNECT host:443 -> tunnel via current lane, fail over on connect error
196
+ | ZenProxy | plain HTTP -> forward, on 429 rotate + retry once
197
+ +------+--------+
198
+ | lane selector (ProxyChain)
199
+ +-----+------+---------+
200
+ v v v
201
+ direct WARP Tor
202
+ (clean) SOCKS5 SOCKS5
203
+ :40000 :9050
204
+ \ | /
205
+ v v v
206
+ opencode.ai / models.opencode.ai / provider APIs
207
+ ```
208
+
209
+ Full details are in [docs/architecture.md](https://github.com/mithun50/opencode-unlimited/blob/main/docs/architecture.md).
210
+
211
+ ### Lanes
212
+
213
+ | Lane | Config name | Route |
214
+ |---|---|---|
215
+ | Clean endpoint | `clean` / `clean_endpoint` | Direct connection |
216
+ | Direct | `direct` | Direct connection (always available) |
217
+ | WARP | `warp` | SOCKS5 `127.0.0.1:40000` (WARP must be in proxy mode) |
218
+ | Tor | `tor` | SOCKS5 `127.0.0.1:9050` |
219
+
220
+ The default order is `["clean", "warp", "tor"]`, followed by any lanes not listed. Change it with:
221
+
222
+ ```bash
223
+ ocu config set lanes_priority "tor,clean"
224
+ ```
225
+
226
+ ### HTTPS and 429s
227
+
228
+ OpenCode uses HTTPS, which reaches `ocu` as `CONNECT` tunnels. `ocu` opens each tunnel through the current lane and tries the next lane if the connection fails. The TLS session runs between OpenCode and the server (`ocu` does not decrypt it), so a `429` inside the tunnel reaches OpenCode unchanged and does not trigger rotation. 429 handling applies to plain-HTTP requests.
229
+
230
+ ### Tor
231
+
232
+ `ocu start` and `ocu run` check for Tor when the Tor lane is on, and offer to install it if it's missing. See [docs/tor.md](https://github.com/mithun50/opencode-unlimited/blob/main/docs/tor.md) for what the installer does on each OS and how it edits PATH on Windows.
233
+
234
+ ---
235
+
236
+ ## Configuration
237
+
238
+ | Platform | Path |
239
+ |---|---|
240
+ | Linux | `~/.config/opencode-unlimited/config.toml` |
241
+ | macOS | `~/Library/Application Support/opencode-unlimited/config.toml` |
242
+ | Windows | `%APPDATA%\opencode-unlimited\config.toml` |
243
+
244
+ ```toml
245
+ proxy_port = 8877
246
+ dashboard_port = 7979
247
+ enable_warp = true
248
+ enable_tor = true
249
+ enable_clean_endpoints = true
250
+ auto_resume = true
251
+ log_level = "INFO"
252
+ lanes_priority = ["clean", "warp", "tor"]
253
+ opencode_api_host = "api.opencode.ai"
254
+ opencode_cli_path = ""
255
+ opencode_desktop_path = ""
256
+ ```
257
+
258
+ Every key is described in [docs/configuration.md](https://github.com/mithun50/opencode-unlimited/blob/main/docs/configuration.md).
259
+
260
+ ---
261
+
262
+ ## Documentation
263
+
264
+ - [Architecture](https://github.com/mithun50/opencode-unlimited/blob/main/docs/architecture.md)
265
+ - [Configuration](https://github.com/mithun50/opencode-unlimited/blob/main/docs/configuration.md)
266
+ - [Tor setup](https://github.com/mithun50/opencode-unlimited/blob/main/docs/tor.md)
267
+ - [Testing](https://github.com/mithun50/opencode-unlimited/blob/main/docs/testing.md)
268
+ - [Troubleshooting](https://github.com/mithun50/opencode-unlimited/blob/main/docs/troubleshooting.md)
269
+ - [Changelog](https://github.com/mithun50/opencode-unlimited/blob/main/CHANGELOG.md)
270
+
271
+ ---
272
+
273
+ ## Contributing
274
+
275
+ Contributions are welcome. Start with [CONTRIBUTING.md](https://github.com/mithun50/opencode-unlimited/blob/main/CONTRIBUTING.md), and please follow the [Code of Conduct](https://github.com/mithun50/opencode-unlimited/blob/main/CODE_OF_CONDUCT.md).
276
+
277
+ Report security issues privately as described in [SECURITY.md](https://github.com/mithun50/opencode-unlimited/blob/main/SECURITY.md), not in public issues.
278
+
279
+ ---
280
+
281
+ ## License
282
+
283
+ MIT. See [LICENSE](https://github.com/mithun50/opencode-unlimited/blob/main/LICENSE).
@@ -0,0 +1,220 @@
1
+ # opencode-unlimited
2
+
3
+ [![PyPI version](https://img.shields.io/pypi/v/opencode-unlimited.svg)](https://pypi.org/project/opencode-unlimited/)
4
+ [![Python](https://img.shields.io/pypi/pyversions/opencode-unlimited.svg)](https://pypi.org/project/opencode-unlimited/)
5
+ [![CI](https://github.com/mithun50/opencode-unlimited/actions/workflows/ci.yml/badge.svg)](https://github.com/mithun50/opencode-unlimited/actions/workflows/ci.yml)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/mithun50/opencode-unlimited/blob/main/LICENSE)
7
+ [![Code style: ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
8
+
9
+ **opencode-unlimited** (`ocu`) is a local HTTP/HTTPS proxy for [OpenCode](https://opencode.ai). It sends OpenCode's traffic out through one of several egress "lanes" (direct, Cloudflare WARP, Tor) and moves to the next lane when one fails or, for plain-HTTP traffic, returns `429 Too Many Requests`.
10
+
11
+ > **Know the limits.** Quotas tied to your OpenCode account or API key are counted per account, not per IP address, so changing the egress lane does not reset them. HTTPS traffic is end-to-end encrypted, so `ocu` can't see 429 responses inside it (see [HTTPS and 429s](#https-and-429s)). Follow the terms of the services you use.
12
+
13
+ ---
14
+
15
+ ## Features
16
+
17
+ - Local forward proxy on `127.0.0.1:8877` that handles HTTPS `CONNECT` tunnels and plain HTTP, and streams server-sent events without buffering
18
+ - Egress lanes: direct, Cloudflare WARP, and Tor, with failover when a lane can't connect
19
+ - On plain-HTTP `429`: records the event (with `Retry-After`), rotates to the next lane, and retries once
20
+ - Lanes that are disabled or whose service fails to start are removed from rotation
21
+ - `ocu tor install`: checks for Tor and installs it (the official Tor Expert Bundle verified by SHA-256 on Windows, your package manager elsewhere), and can add it to your PATH
22
+ - `ocu run`: finds or installs OpenCode, starts the proxy in the background, and launches OpenCode through it
23
+ - Optional web dashboard (FastAPI)
24
+ - Pure-Python config (stdlib dataclasses + TOML), so it installs cleanly on Termux
25
+
26
+ ---
27
+
28
+ ## Installation
29
+
30
+ ```bash
31
+ # Recommended (isolated, puts `ocu` on PATH)
32
+ pipx install opencode-unlimited
33
+
34
+ # Or with uv
35
+ uv tool install opencode-unlimited
36
+
37
+ # Or with pip inside a virtual environment
38
+ pip install opencode-unlimited
39
+
40
+ # With the web dashboard
41
+ pipx install "opencode-unlimited[dashboard]"
42
+ ```
43
+
44
+ Requires Python 3.10+. Tor is optional and `ocu` can install it for you. WARP is optional and needs `warp-cli`.
45
+
46
+ <details>
47
+ <summary>Platform notes (Windows, Debian/Ubuntu, Termux)</summary>
48
+
49
+ **Windows (PowerShell)**
50
+
51
+ ```powershell
52
+ python -m pip install --user pipx
53
+ python -m pipx ensurepath # then open a new terminal
54
+ pipx install opencode-unlimited
55
+ ```
56
+
57
+ **Debian / Ubuntu** (`externally-managed-environment` / PEP 668)
58
+
59
+ ```bash
60
+ sudo apt update && sudo apt install -y pipx
61
+ pipx ensurepath
62
+ pipx install opencode-unlimited
63
+ ```
64
+
65
+ **Termux (Android)**
66
+
67
+ ```bash
68
+ pkg install python tor
69
+ pip install opencode-unlimited
70
+ ```
71
+
72
+ </details>
73
+
74
+ ---
75
+
76
+ ## Quick start
77
+
78
+ ```bash
79
+ ocu run # start the proxy in the background and launch OpenCode through it
80
+ ocu run --desktop # same, but launch OpenCode Desktop
81
+ ocu start -d # only start the background proxy
82
+ ocu status # proxy, lanes, and account status
83
+ ocu tor status # is Tor installed / on PATH / listening?
84
+ ocu stop # stop the background proxy (and the Tor it started)
85
+ ```
86
+
87
+ To route any other tool through `ocu`, point it at the proxy:
88
+
89
+ ```bash
90
+ export HTTPS_PROXY=http://127.0.0.1:8877 HTTP_PROXY=http://127.0.0.1:8877
91
+ ```
92
+
93
+ ```powershell
94
+ $env:HTTPS_PROXY = "http://127.0.0.1:8877"; $env:HTTP_PROXY = "http://127.0.0.1:8877"
95
+ ```
96
+
97
+ > OpenCode 2.x normally sends work to a long-running background service that does not inherit these variables. `ocu run` therefore launches OpenCode with `--standalone`. If you launch OpenCode yourself, pass `--standalone` too, for example `opencode run --standalone "..."`.
98
+
99
+ ---
100
+
101
+ ## CLI commands
102
+
103
+ | Command | Description |
104
+ |---|---|
105
+ | `ocu run [-- ARGS]` | Find/install OpenCode, start the proxy in the background, launch OpenCode through it |
106
+ | `ocu setup` | Detect OpenCode CLI/Desktop and install or configure it |
107
+ | `ocu start [-d]` | Start the proxy stack (foreground with live status, or `-d` for background) |
108
+ | `ocu stop` | Stop the background proxy |
109
+ | `ocu status` | Show proxy status, lane health, and the account pool |
110
+ | `ocu logs [-f] [-n N]` | Show or follow the log file |
111
+ | `ocu dashboard [-p PORT]` | Run the web dashboard (needs the `dashboard` extra) |
112
+ | `ocu config show` / `set KEY VALUE` / `reset` | View, change, or reset the configuration |
113
+ | `ocu lanes list` | Show egress lanes and their health |
114
+ | `ocu lanes tor on/off`, `ocu lanes warp on/off` | Enable or disable a lane |
115
+ | `ocu tor status` | Show whether Tor is installed, on PATH, and listening |
116
+ | `ocu tor install [-y] [--no-path] [--force]` | Install Tor and (optionally) add it to PATH |
117
+ | `ocu tor path` | Add an installed Tor's folder to your user PATH (Windows) |
118
+ | `ocu tor renew` | Ask the running Tor for a new circuit |
119
+ | `ocu accounts list/add/remove/rotate` | Manage the local account pool |
120
+ | `ocu version` | Print the version |
121
+
122
+ Common options for `start` and `run`: `--proxy-port`, `--no-tor`, `--no-warp`, `--dashboard`.
123
+
124
+ ---
125
+
126
+ ## How it works
127
+
128
+ ```
129
+ OpenCode (HTTPS_PROXY=http://127.0.0.1:8877)
130
+ |
131
+ v
132
+ +------+--------+ CONNECT host:443 -> tunnel via current lane, fail over on connect error
133
+ | ZenProxy | plain HTTP -> forward, on 429 rotate + retry once
134
+ +------+--------+
135
+ | lane selector (ProxyChain)
136
+ +-----+------+---------+
137
+ v v v
138
+ direct WARP Tor
139
+ (clean) SOCKS5 SOCKS5
140
+ :40000 :9050
141
+ \ | /
142
+ v v v
143
+ opencode.ai / models.opencode.ai / provider APIs
144
+ ```
145
+
146
+ Full details are in [docs/architecture.md](https://github.com/mithun50/opencode-unlimited/blob/main/docs/architecture.md).
147
+
148
+ ### Lanes
149
+
150
+ | Lane | Config name | Route |
151
+ |---|---|---|
152
+ | Clean endpoint | `clean` / `clean_endpoint` | Direct connection |
153
+ | Direct | `direct` | Direct connection (always available) |
154
+ | WARP | `warp` | SOCKS5 `127.0.0.1:40000` (WARP must be in proxy mode) |
155
+ | Tor | `tor` | SOCKS5 `127.0.0.1:9050` |
156
+
157
+ The default order is `["clean", "warp", "tor"]`, followed by any lanes not listed. Change it with:
158
+
159
+ ```bash
160
+ ocu config set lanes_priority "tor,clean"
161
+ ```
162
+
163
+ ### HTTPS and 429s
164
+
165
+ OpenCode uses HTTPS, which reaches `ocu` as `CONNECT` tunnels. `ocu` opens each tunnel through the current lane and tries the next lane if the connection fails. The TLS session runs between OpenCode and the server (`ocu` does not decrypt it), so a `429` inside the tunnel reaches OpenCode unchanged and does not trigger rotation. 429 handling applies to plain-HTTP requests.
166
+
167
+ ### Tor
168
+
169
+ `ocu start` and `ocu run` check for Tor when the Tor lane is on, and offer to install it if it's missing. See [docs/tor.md](https://github.com/mithun50/opencode-unlimited/blob/main/docs/tor.md) for what the installer does on each OS and how it edits PATH on Windows.
170
+
171
+ ---
172
+
173
+ ## Configuration
174
+
175
+ | Platform | Path |
176
+ |---|---|
177
+ | Linux | `~/.config/opencode-unlimited/config.toml` |
178
+ | macOS | `~/Library/Application Support/opencode-unlimited/config.toml` |
179
+ | Windows | `%APPDATA%\opencode-unlimited\config.toml` |
180
+
181
+ ```toml
182
+ proxy_port = 8877
183
+ dashboard_port = 7979
184
+ enable_warp = true
185
+ enable_tor = true
186
+ enable_clean_endpoints = true
187
+ auto_resume = true
188
+ log_level = "INFO"
189
+ lanes_priority = ["clean", "warp", "tor"]
190
+ opencode_api_host = "api.opencode.ai"
191
+ opencode_cli_path = ""
192
+ opencode_desktop_path = ""
193
+ ```
194
+
195
+ Every key is described in [docs/configuration.md](https://github.com/mithun50/opencode-unlimited/blob/main/docs/configuration.md).
196
+
197
+ ---
198
+
199
+ ## Documentation
200
+
201
+ - [Architecture](https://github.com/mithun50/opencode-unlimited/blob/main/docs/architecture.md)
202
+ - [Configuration](https://github.com/mithun50/opencode-unlimited/blob/main/docs/configuration.md)
203
+ - [Tor setup](https://github.com/mithun50/opencode-unlimited/blob/main/docs/tor.md)
204
+ - [Testing](https://github.com/mithun50/opencode-unlimited/blob/main/docs/testing.md)
205
+ - [Troubleshooting](https://github.com/mithun50/opencode-unlimited/blob/main/docs/troubleshooting.md)
206
+ - [Changelog](https://github.com/mithun50/opencode-unlimited/blob/main/CHANGELOG.md)
207
+
208
+ ---
209
+
210
+ ## Contributing
211
+
212
+ Contributions are welcome. Start with [CONTRIBUTING.md](https://github.com/mithun50/opencode-unlimited/blob/main/CONTRIBUTING.md), and please follow the [Code of Conduct](https://github.com/mithun50/opencode-unlimited/blob/main/CODE_OF_CONDUCT.md).
213
+
214
+ Report security issues privately as described in [SECURITY.md](https://github.com/mithun50/opencode-unlimited/blob/main/SECURITY.md), not in public issues.
215
+
216
+ ---
217
+
218
+ ## License
219
+
220
+ MIT. See [LICENSE](https://github.com/mithun50/opencode-unlimited/blob/main/LICENSE).
@@ -0,0 +1,77 @@
1
+ # Architecture
2
+
3
+ [Back to README](https://github.com/mithun50/opencode-unlimited/blob/main/README.md)
4
+
5
+ ## Components
6
+
7
+ | Module | Role |
8
+ |---|---|
9
+ | [`proxy/zen_proxy.py`](https://github.com/mithun50/opencode-unlimited/blob/main/src/opencode_unlimited/proxy/zen_proxy.py) | Local forward proxy on asyncio streams. Handles `CONNECT` tunnels and plain HTTP. |
10
+ | [`proxy/proxy_chain.py`](https://github.com/mithun50/opencode-unlimited/blob/main/src/opencode_unlimited/proxy/proxy_chain.py) | Lane order, health, failure counts, rotation. |
11
+ | [`guards/zen_guard.py`](https://github.com/mithun50/opencode-unlimited/blob/main/src/opencode_unlimited/guards/zen_guard.py) | Records 429 events (URL, lane, `Retry-After`) for status and the dashboard. |
12
+ | [`egress/tor_manager.py`](https://github.com/mithun50/opencode-unlimited/blob/main/src/opencode_unlimited/egress/tor_manager.py) | Starts, reuses, and stops Tor. |
13
+ | [`egress/tor_installer.py`](https://github.com/mithun50/opencode-unlimited/blob/main/src/opencode_unlimited/egress/tor_installer.py) | Finds and installs Tor, edits the Windows user PATH. |
14
+ | [`egress/warp_keeper.py`](https://github.com/mithun50/opencode-unlimited/blob/main/src/opencode_unlimited/egress/warp_keeper.py) | Connects Cloudflare WARP through `warp-cli`. |
15
+ | [`daemon.py`](https://github.com/mithun50/opencode-unlimited/blob/main/src/opencode_unlimited/daemon.py) | Wires everything together (`run_stack`) and spawns the background process. |
16
+ | [`launcher.py`](https://github.com/mithun50/opencode-unlimited/blob/main/src/opencode_unlimited/launcher.py) | Finds/installs OpenCode and launches it with the proxy environment. |
17
+ | [`cli.py`](https://github.com/mithun50/opencode-unlimited/blob/main/src/opencode_unlimited/cli.py) | The `ocu` command (Typer). |
18
+
19
+ ## Startup (`ocu start -d` / `ocu run`)
20
+
21
+ 1. The CLI loads the config and, if the Tor lane is on and Tor is missing, offers to install it.
22
+ 2. It spawns `python -m opencode_unlimited _serve` as a detached process and waits until the proxy port accepts connections (up to 330 s, because a cold Tor bootstrap can take minutes). If the child exits first, the CLI shows an error that points to the log.
23
+ 3. `run_stack` in the child:
24
+ - connects WARP if enabled and `warp-cli` exists
25
+ - starts Tor if enabled (or reuses a SOCKS5 server already on `127.0.0.1:9050`)
26
+ - removes WARP/Tor from rotation if they didn't come up
27
+ - starts `ZenProxy` on `127.0.0.1:<proxy_port>`
28
+ 4. `ocu run` then launches OpenCode with `HTTP(S)_PROXY` set and `--standalone`, so OpenCode's API calls come from a process that has the proxy settings.
29
+
30
+ ## Request flow
31
+
32
+ ### HTTPS (`CONNECT host:port`)
33
+
34
+ ```
35
+ client --CONNECT--> ZenProxy
36
+ for lane in [current, next, ...]:
37
+ open TCP (direct, Happy Eyeballs) or SOCKS5 (hostname sent, DNS on the lane)
38
+ success -> mark_success, stop
39
+ failure -> mark_failed (rotate if current lane went unhealthy), try next
40
+ all failed -> 502
41
+ "200 Connection Established", then splice bytes both ways
42
+ ```
43
+
44
+ The tunnel carries TLS end-to-end. `ocu` never sees the HTTP status inside it.
45
+
46
+ ### Plain HTTP (absolute-form URL)
47
+
48
+ ```
49
+ client --GET http://host/path--> ZenProxy
50
+ attempt 1 on current lane (httpx, streaming)
51
+ 429 -> mark_failed, guard event, rotate, attempt 2
52
+ transport error -> mark_failed, rotate, attempt 2
53
+ otherwise -> stream response to client
54
+ attempt 2 result is returned as is (a second 429 goes to the client)
55
+ ```
56
+
57
+ Hop-by-hop and identity headers (`X-Forwarded-For`, `X-Real-IP`, `Forwarded`, `Proxy-*`) are removed in both directions. Redirects are passed to the client, not followed.
58
+
59
+ ## Lane health
60
+
61
+ - `mark_failed` increments a counter. After more than 3 failures the lane is unhealthy and skipped by `rotate()`.
62
+ - `mark_success` resets the counter, so only consecutive failures count.
63
+ - If every lane is unhealthy, `rotate()` resets all of them and starts again from the first lane.
64
+ - `disable_lane` removes a lane completely. `direct` can't be removed, so the rotation is never empty.
65
+
66
+ ## Files written
67
+
68
+ | File | Location |
69
+ |---|---|
70
+ | Config | `<config dir>/config.toml` |
71
+ | Log | `<config dir>/ocu.log` |
72
+ | PID | `<config dir>/ocu.pid` |
73
+ | Tor data | `<config dir>/tor-data/` |
74
+ | Accounts | `<config dir>/accounts.json` |
75
+ | Tor bundle (installer) | Windows `%LOCALAPPDATA%\opencode-unlimited\tor`, macOS `~/Library/Application Support/opencode-unlimited/tor`, Linux `~/.local/share/opencode-unlimited/tor` |
76
+
77
+ `<config dir>` is `%APPDATA%\opencode-unlimited` on Windows, `~/Library/Application Support/opencode-unlimited` on macOS, and `~/.config/opencode-unlimited` on Linux.