tunnelmate-cli 1.0.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 (41) hide show
  1. tunnelmate_cli-1.0.0/PKG-INFO +352 -0
  2. tunnelmate_cli-1.0.0/README.md +334 -0
  3. tunnelmate_cli-1.0.0/pyproject.toml +36 -0
  4. tunnelmate_cli-1.0.0/setup.cfg +4 -0
  5. tunnelmate_cli-1.0.0/tests/test_cli.py +36 -0
  6. tunnelmate_cli-1.0.0/tests/test_cloudflared.py +18 -0
  7. tunnelmate_cli-1.0.0/tests/test_config.py +42 -0
  8. tunnelmate_cli-1.0.0/tests/test_db.py +75 -0
  9. tunnelmate_cli-1.0.0/tests/test_health.py +54 -0
  10. tunnelmate_cli-1.0.0/tests/test_ipc.py +99 -0
  11. tunnelmate_cli-1.0.0/tunnelmate/__init__.py +9 -0
  12. tunnelmate_cli-1.0.0/tunnelmate/bot/__init__.py +5 -0
  13. tunnelmate_cli-1.0.0/tunnelmate/bot/telegram_bot.py +767 -0
  14. tunnelmate_cli-1.0.0/tunnelmate/cli/__init__.py +5 -0
  15. tunnelmate_cli-1.0.0/tunnelmate/cli/formatting.py +253 -0
  16. tunnelmate_cli-1.0.0/tunnelmate/cli/interactive.py +220 -0
  17. tunnelmate_cli-1.0.0/tunnelmate/cli/main.py +616 -0
  18. tunnelmate_cli-1.0.0/tunnelmate/cli/ssh_client.py +111 -0
  19. tunnelmate_cli-1.0.0/tunnelmate/client/__init__.py +5 -0
  20. tunnelmate_cli-1.0.0/tunnelmate/client/ipc_client.py +165 -0
  21. tunnelmate_cli-1.0.0/tunnelmate/config.py +162 -0
  22. tunnelmate_cli-1.0.0/tunnelmate/core/__init__.py +35 -0
  23. tunnelmate_cli-1.0.0/tunnelmate/core/cloudflared.py +125 -0
  24. tunnelmate_cli-1.0.0/tunnelmate/core/health.py +102 -0
  25. tunnelmate_cli-1.0.0/tunnelmate/core/models.py +84 -0
  26. tunnelmate_cli-1.0.0/tunnelmate/core/process.py +224 -0
  27. tunnelmate_cli-1.0.0/tunnelmate/core/supervisor.py +388 -0
  28. tunnelmate_cli-1.0.0/tunnelmate/daemon/__init__.py +14 -0
  29. tunnelmate_cli-1.0.0/tunnelmate/daemon/protocol.py +47 -0
  30. tunnelmate_cli-1.0.0/tunnelmate/daemon/server.py +295 -0
  31. tunnelmate_cli-1.0.0/tunnelmate/daemon/service.py +148 -0
  32. tunnelmate_cli-1.0.0/tunnelmate/db/__init__.py +6 -0
  33. tunnelmate_cli-1.0.0/tunnelmate/db/repository.py +435 -0
  34. tunnelmate_cli-1.0.0/tunnelmate/db/schema.py +57 -0
  35. tunnelmate_cli-1.0.0/tunnelmate/exceptions.py +40 -0
  36. tunnelmate_cli-1.0.0/tunnelmate_cli.egg-info/PKG-INFO +352 -0
  37. tunnelmate_cli-1.0.0/tunnelmate_cli.egg-info/SOURCES.txt +39 -0
  38. tunnelmate_cli-1.0.0/tunnelmate_cli.egg-info/dependency_links.txt +1 -0
  39. tunnelmate_cli-1.0.0/tunnelmate_cli.egg-info/entry_points.txt +3 -0
  40. tunnelmate_cli-1.0.0/tunnelmate_cli.egg-info/requires.txt +10 -0
  41. tunnelmate_cli-1.0.0/tunnelmate_cli.egg-info/top_level.txt +1 -0
@@ -0,0 +1,352 @@
1
+ Metadata-Version: 2.4
2
+ Name: tunnelmate-cli
3
+ Version: 1.0.0
4
+ Summary: Cloudflare Quick Tunnel manager with CLI, Interactive TUI, Daemon IPC, and Telegram Bot
5
+ Author-email: Shashan Lumbhani <lumbhanishashan1510@gmail.com>
6
+ License: MIT
7
+ Requires-Python: >=3.10
8
+ Description-Content-Type: text/markdown
9
+ Requires-Dist: typer>=0.9.0
10
+ Requires-Dist: rich>=13.0.0
11
+ Requires-Dist: questionary>=2.0.0
12
+ Requires-Dist: pydantic>=2.0.0
13
+ Requires-Dist: python-telegram-bot>=20.0
14
+ Requires-Dist: httpx>=0.24.0
15
+ Provides-Extra: dev
16
+ Requires-Dist: pytest>=7.0.0; extra == "dev"
17
+ Requires-Dist: pytest-asyncio>=0.21.0; extra == "dev"
18
+
19
+ # โšก TunnelMate โ€” Cloudflare Quick Tunnel Manager
20
+
21
+ **TunnelMate** is a production-grade Cloudflare Quick Tunnel manager for Linux, macOS, and Windows. It provides seamless localhost ingress with zero configuration or domain registration requirements.
22
+
23
+ It features a **shared background daemon**, a **Unix Domain Socket IPC interface**, an **interactive arrow-key TUI**, a **direct CLI**, and a **Telegram bot** for remote management with inline menus, salted password authentication, and real-time subscriber notifications.
24
+
25
+ ---
26
+
27
+ ## ๐ŸŒŸ Key Features
28
+
29
+ - ๐Ÿš€ **Python Library:** Complete API for tunnel creation, port remapping, URL auto-discovery, lifecycle controls, and diagnostics.
30
+ - ๐Ÿ’ป **Interactive Arrow-Key TUI:** Built with [Typer](https://typer.tiangolo.com/), [Rich](https://github.com/Textualize/rich), and [Questionary](https://github.com/tmbo/questionary) for keyboard-driven navigation.
31
+ - โšก **Direct CLI:** Scriptable subcommands (`start`, `stop`, `restart`, `url`, `port`, `health`, `logs`, `history`).
32
+ - ๐Ÿค– **Telegram Bot Remote Management:** Full menu-based controls via inline keyboards, salted SHA-256 password protection, admin whitelist, one-click latest-link retrieval, and automatic broadcast notifications to subscribers.
33
+ - ๐Ÿ”’ **Shared Background Daemon & IPC:** Single source of truth running over a secured Unix Domain Socket (`0600` permissions) or local TCP fallback. CLI and Telegram bot share the exact same state without process conflicts or SQLite file lock contention.
34
+ - ๐Ÿ—„๏ธ **Persistent SQLite Storage:** WAL-mode database storing tunnels, audit history, subscriber preferences, and settings.
35
+ - ๐Ÿ›ก๏ธ **Subprocess Management without `shell=True`:** Spawns `cloudflared` using explicit argument lists, POSIX process groups, and graceful SIGTERM/SIGKILL shutdown.
36
+ - ๐Ÿ” **Resilience & Auto-Recovery:** Automatic regex extraction of `https://*.trycloudflare.com` URLs, crash detection, and exponential backoff restart.
37
+ - ๐Ÿฉบ **Health Checks:** Diagnostic probing of both local ports/HTTP services and Cloudflare edge availability.
38
+ - ๐Ÿ“ฆ **Deployment Ready:** Includes `install.sh`, systemd service files, and full test suite.
39
+
40
+ ---
41
+
42
+ ## ๐Ÿ›๏ธ System Architecture
43
+
44
+ ```text
45
+ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
46
+ โ”‚ Interactive TUI / CLI โ”‚ โ”‚ Telegram Bot Client โ”‚
47
+ โ”‚ (Questionary + Rich + Typer) โ”‚ โ”‚ (Inline Keyboards & Events) โ”‚
48
+ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
49
+ โ”‚ โ”‚
50
+ โ”‚ Unix Domain Socket IPC โ”‚
51
+ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
52
+ โ”‚
53
+ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
54
+ โ”‚ TunnelMate Daemon Service โ”‚
55
+ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
56
+ โ”‚ โ”‚ Async IPC Server โ”‚ โ”‚
57
+ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
58
+ โ”‚ โ”‚ โ”‚
59
+ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
60
+ โ”‚ โ”‚ Tunnel Supervisor โ”‚ โ”‚
61
+ โ”‚ โ”‚ (Auto-Recovery & Health) โ”‚ โ”‚
62
+ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
63
+ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
64
+ โ”‚ โ”‚
65
+ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ” โ”Œโ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
66
+ โ”‚ cloudflared โ”‚ โ”‚ SQLite Database โ”‚
67
+ โ”‚ Subprocesses โ”‚ โ”‚ (WAL Mode) โ”‚
68
+ โ”‚ (without shell) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
69
+ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
70
+ ```
71
+
72
+ ---
73
+
74
+ ## ๐Ÿš€ Installation
75
+
76
+ ### Automated Installer (Linux / macOS)
77
+
78
+ Run the included automated installer script:
79
+
80
+ ```bash
81
+ chmod +x install.sh
82
+ ./install.sh
83
+ ```
84
+
85
+ The script will:
86
+ 1. Verify Python 3.10+
87
+ 2. Download the official `cloudflared` binary for your architecture (if missing)
88
+ 3. Set up the `~/.tunnelmate` directory with secure `0700` permissions
89
+ 4. Install the `tunnelmate` package
90
+ 5. Register systemd user service units
91
+
92
+ ### Manual Setup
93
+
94
+ ```bash
95
+ # 1. Clone repository & create virtual environment
96
+ git clone <repo_url> /opt/tunnelmate
97
+ cd /opt/tunnelmate
98
+ python3 -m venv .venv
99
+ source .venv/bin/activate
100
+
101
+ # 2. Install dependencies & TunnelMate package
102
+ pip install -e .
103
+
104
+ # 3. Download cloudflared binary (if not already installed on PATH)
105
+ tunnelmate install-cloudflared
106
+ ```
107
+
108
+ ---
109
+
110
+ ## ๐Ÿ Quick Startup Guide
111
+
112
+ ### Step 1: Start the Background Daemon
113
+
114
+ ```bash
115
+ tunnelmate daemon start
116
+ ```
117
+
118
+ Verify daemon health:
119
+ ```bash
120
+ tunnelmate daemon status
121
+ ```
122
+
123
+ ### Step 2: Open Interactive TUI Menu
124
+
125
+ Launch the interactive arrow-key menu:
126
+
127
+ ```bash
128
+ tunnelmate menu
129
+ ```
130
+
131
+ *(Or simply execute `tunnelmate` without arguments in an interactive terminal).*
132
+
133
+ ### Step 3: Command-Line Operations
134
+
135
+ #### Create and Start a Tunnel
136
+ ```bash
137
+ # Create a tunnel for a local service on port 8080
138
+ tunnelmate create my-web --port 8080
139
+
140
+ # Start the tunnel and get the Quick Tunnel URL
141
+ tunnelmate start my-web
142
+ ```
143
+
144
+ #### Fetch URL for Shell Scripts
145
+ ```bash
146
+ # Prints only the URL (e.g. https://xxxx.trycloudflare.com)
147
+ tunnelmate url my-web
148
+
149
+ # Example: Open in browser directly
150
+ xdg-open $(tunnelmate url my-web)
151
+ ```
152
+
153
+ #### Remap Target Port on the Fly
154
+ ```bash
155
+ # Remaps tunnel from port 8080 to 3000 (safely replaces the process)
156
+ tunnelmate port my-web 3000
157
+ ```
158
+
159
+ #### Diagnostic Health Check
160
+ ```bash
161
+ tunnelmate health my-web
162
+ ```
163
+
164
+ #### Inspect Live Logs & Audit History
165
+ ```bash
166
+ # View last 50 lines of cloudflared process output
167
+ tunnelmate logs my-web --lines 50
168
+
169
+ # View lifecycle history (creation, start, URL assignments, crashes)
170
+ tunnelmate history my-web
171
+ ```
172
+
173
+ #### List, Stop, and Delete Tunnels
174
+ ```bash
175
+ # List all tunnels with color-coded status
176
+ tunnelmate list
177
+
178
+ # Stop a running tunnel
179
+ tunnelmate stop my-web
180
+
181
+ # Delete tunnel
182
+ tunnelmate delete my-web --yes
183
+ #### Connect to Cloudflare SSH Tunnels (`tunnelmate-ssh`)
184
+ Client computers can install TunnelMate and connect to any SSH Quick Tunnel directly without writing manual `ProxyCommand` arguments (TunnelMate auto-bootstraps `cloudflared` if missing):
185
+
186
+ ```bash
187
+ # Direct SSH connection command
188
+ tunnelmate-ssh user@<trycloudflare_url>
189
+
190
+ # Example
191
+ tunnelmate-ssh shashansoni@subjective-lenses-working-subscription.trycloudflare.com
192
+
193
+ # (Or using the sub-command)
194
+ tunnelmate ssh shashansoni@subjective-lenses-working-subscription.trycloudflare.com
195
+ ```
196
+
197
+ ---
198
+
199
+ ## ๐Ÿค– Telegram Bot Remote Management
200
+
201
+ TunnelMate includes a remote Telegram Bot interface.
202
+
203
+ ### 1. Bot Configuration
204
+
205
+ Set your bot token (from [@BotFather](https://t.me/BotFather)) and configure an admin password:
206
+
207
+ ```bash
208
+ # Set bot token
209
+ tunnelmate bot set-token "123456789:ABCdefGhIJKlmNoPQRstuVWXyz"
210
+
211
+ # Set administrator password (stored as salted SHA-256 hash)
212
+ tunnelmate bot set-password "YourStrongPassword"
213
+
214
+ # View all historical chats & discovered Chat IDs
215
+ tunnelmate bot chats
216
+
217
+ # Whitelist a Chat ID as Administrator
218
+ tunnelmate bot whitelist <CHAT_ID>
219
+ # (or: tunnelmate bot add-admin <CHAT_ID>)
220
+
221
+ # Remove a Chat ID from Admin Whitelist
222
+ tunnelmate bot unwhitelist <CHAT_ID>
223
+ # (or: tunnelmate bot remove-admin <CHAT_ID>)
224
+ ```
225
+
226
+ ### 2. Bot Process Management
227
+
228
+ ```bash
229
+ # Start bot in background
230
+ tunnelmate bot start
231
+
232
+ # Check bot running status & PID
233
+ tunnelmate bot status
234
+
235
+ # Stop background bot
236
+ tunnelmate bot stop
237
+
238
+ # (Optional) Run in foreground for live debugging
239
+ tunnelmate bot run
240
+ ```
241
+
242
+ ### 3. Telegram Bot Features
243
+
244
+ - `/start`: Displays the interactive dashboard with inline buttons.
245
+ - `/login <password>`: Authenticates the user as an Administrator (the message is automatically deleted for privacy).
246
+ - `/logout`: Clears the admin session.
247
+ - `/create <name> <port> [protocol]`: Creates a new tunnel.
248
+ - `/port <name> <new_port>`: Updates the target port for an existing tunnel.
249
+ - **Inline Menus:**
250
+ - `[๐Ÿ“‹ Tunnels List]`: Shows all tunnels with statuses (`๐ŸŸข Running`, `๐Ÿ”ด Stopped`).
251
+ - `[โ–ถ๏ธ Start] / [โน๏ธ Stop] / [๐Ÿ”„ Restart]`: Controls tunnels in real-time.
252
+ - `[๐Ÿ”— Latest Link]`: Retrieves the current URL with an inline `[๐ŸŒ Open]` web button.
253
+ - `[๐Ÿฉบ Health Check]`: Diagnoses local port response and Cloudflare edge status.
254
+ - `[๐Ÿ”” Notifications]`: Subscribes users to instant alerts when tunnels start, change links, crash, or recover.
255
+
256
+ ---
257
+
258
+ ## โš™๏ธ Systemd Service Deployment
259
+
260
+ TunnelMate includes systemd service unit files for running both the daemon and the Telegram bot continuously in the background.
261
+
262
+ ### User Mode (Recommended)
263
+
264
+ ```bash
265
+ mkdir -p ~/.config/systemd/user
266
+ cp systemd/tunnelmate-daemon.service ~/.config/systemd/user/
267
+ cp systemd/tunnelmate-bot.service ~/.config/systemd/user/
268
+
269
+ systemctl --user daemon-reload
270
+
271
+ # Enable and start the daemon
272
+ systemctl --user enable --now tunnelmate-daemon
273
+
274
+ # Enable and start the Telegram bot (if token is configured)
275
+ systemctl --user enable --now tunnelmate-bot
276
+
277
+ # Check status
278
+ systemctl --user status tunnelmate-daemon
279
+ ```
280
+
281
+ To enable user services to run without an active SSH session:
282
+ ```bash
283
+ loginctl enable-linger $USER
284
+ ```
285
+
286
+ ---
287
+
288
+ ## ๐Ÿ Python Library Usage
289
+
290
+ You can embed TunnelMate directly into your Python applications:
291
+
292
+ ```python
293
+ from tunnelmate.client import TunnelMateClient
294
+
295
+ client = TunnelMateClient()
296
+
297
+ # Check daemon connectivity
298
+ if not client.is_daemon_online():
299
+ print("Daemon is offline!")
300
+ exit(1)
301
+
302
+ # Create a tunnel
303
+ tunnel = client.create_tunnel(name="fastapi-server", port=8000)
304
+
305
+ # Start tunnel and wait for Cloudflare URL
306
+ running_tunnel = client.start_tunnel("fastapi-server")
307
+ print(f"Public URL: {running_tunnel.current_url}")
308
+
309
+ # Health check
310
+ health, details = client.check_health("fastapi-server")
311
+ print(f"Health: {health.value} - {details['message']}")
312
+
313
+ # Change port
314
+ client.change_port("fastapi-server", 8080)
315
+
316
+ # Stop tunnel
317
+ client.stop_tunnel("fastapi-server")
318
+ ```
319
+
320
+ ---
321
+
322
+ ## ๐Ÿงช Running Tests
323
+
324
+ The test suite validates configuration, database transactions, health checks, cloudflared discovery, and IPC roundtrips:
325
+
326
+ ```bash
327
+ pytest -v tests/
328
+ ```
329
+
330
+ Test coverage includes:
331
+ - `test_config.py`: Salted SHA-256 password hashing and setting persistence.
332
+ - `test_db.py`: SQLite schema migrations, CRUD, history tracking, subscriber preferences.
333
+ - `test_cloudflared.py`: Architecture detection and version extraction.
334
+ - `test_health.py`: Ephemeral HTTP origin and TCP latency verification.
335
+ - `test_ipc.py`: Unix socket client-server communications and serialization.
336
+ - `test_cli.py`: Typer CLI command dispatch.
337
+
338
+ ---
339
+
340
+ ## ๐Ÿ”’ Security Specifications
341
+
342
+ 1. **Subprocess Execution:** Subprocesses are started with `subprocess.Popen` without `shell=True` and with strict argument lists to eliminate shell injection vulnerabilities.
343
+ 2. **IPC Socket:** The Unix domain socket file (`~/.tunnelmate/tunnelmate.sock`) is restricted to mode `0600` (readable/writable only by the owner).
344
+ 3. **Database & Configuration:** State directory `~/.tunnelmate` is set to `0700` and config files are set to `0600`.
345
+ 4. **Password Storage:** Telegram admin passwords use high-entropy random salts (`secrets.token_hex(16)`) and SHA-256 with constant-time equality comparisons (`secrets.compare_digest`).
346
+ 5. **Systemd Sandboxing:** Included unit files feature `ProtectSystem=strict`, `PrivateTmp=true`, and explicit `ReadWritePaths`.
347
+
348
+ ---
349
+
350
+ ## ๐Ÿ“„ License
351
+
352
+ MIT License. Built with โค๏ธ for seamless localhost ingress.
@@ -0,0 +1,334 @@
1
+ # โšก TunnelMate โ€” Cloudflare Quick Tunnel Manager
2
+
3
+ **TunnelMate** is a production-grade Cloudflare Quick Tunnel manager for Linux, macOS, and Windows. It provides seamless localhost ingress with zero configuration or domain registration requirements.
4
+
5
+ It features a **shared background daemon**, a **Unix Domain Socket IPC interface**, an **interactive arrow-key TUI**, a **direct CLI**, and a **Telegram bot** for remote management with inline menus, salted password authentication, and real-time subscriber notifications.
6
+
7
+ ---
8
+
9
+ ## ๐ŸŒŸ Key Features
10
+
11
+ - ๐Ÿš€ **Python Library:** Complete API for tunnel creation, port remapping, URL auto-discovery, lifecycle controls, and diagnostics.
12
+ - ๐Ÿ’ป **Interactive Arrow-Key TUI:** Built with [Typer](https://typer.tiangolo.com/), [Rich](https://github.com/Textualize/rich), and [Questionary](https://github.com/tmbo/questionary) for keyboard-driven navigation.
13
+ - โšก **Direct CLI:** Scriptable subcommands (`start`, `stop`, `restart`, `url`, `port`, `health`, `logs`, `history`).
14
+ - ๐Ÿค– **Telegram Bot Remote Management:** Full menu-based controls via inline keyboards, salted SHA-256 password protection, admin whitelist, one-click latest-link retrieval, and automatic broadcast notifications to subscribers.
15
+ - ๐Ÿ”’ **Shared Background Daemon & IPC:** Single source of truth running over a secured Unix Domain Socket (`0600` permissions) or local TCP fallback. CLI and Telegram bot share the exact same state without process conflicts or SQLite file lock contention.
16
+ - ๐Ÿ—„๏ธ **Persistent SQLite Storage:** WAL-mode database storing tunnels, audit history, subscriber preferences, and settings.
17
+ - ๐Ÿ›ก๏ธ **Subprocess Management without `shell=True`:** Spawns `cloudflared` using explicit argument lists, POSIX process groups, and graceful SIGTERM/SIGKILL shutdown.
18
+ - ๐Ÿ” **Resilience & Auto-Recovery:** Automatic regex extraction of `https://*.trycloudflare.com` URLs, crash detection, and exponential backoff restart.
19
+ - ๐Ÿฉบ **Health Checks:** Diagnostic probing of both local ports/HTTP services and Cloudflare edge availability.
20
+ - ๐Ÿ“ฆ **Deployment Ready:** Includes `install.sh`, systemd service files, and full test suite.
21
+
22
+ ---
23
+
24
+ ## ๐Ÿ›๏ธ System Architecture
25
+
26
+ ```text
27
+ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
28
+ โ”‚ Interactive TUI / CLI โ”‚ โ”‚ Telegram Bot Client โ”‚
29
+ โ”‚ (Questionary + Rich + Typer) โ”‚ โ”‚ (Inline Keyboards & Events) โ”‚
30
+ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
31
+ โ”‚ โ”‚
32
+ โ”‚ Unix Domain Socket IPC โ”‚
33
+ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
34
+ โ”‚
35
+ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
36
+ โ”‚ TunnelMate Daemon Service โ”‚
37
+ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
38
+ โ”‚ โ”‚ Async IPC Server โ”‚ โ”‚
39
+ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
40
+ โ”‚ โ”‚ โ”‚
41
+ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
42
+ โ”‚ โ”‚ Tunnel Supervisor โ”‚ โ”‚
43
+ โ”‚ โ”‚ (Auto-Recovery & Health) โ”‚ โ”‚
44
+ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
45
+ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
46
+ โ”‚ โ”‚
47
+ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ” โ”Œโ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
48
+ โ”‚ cloudflared โ”‚ โ”‚ SQLite Database โ”‚
49
+ โ”‚ Subprocesses โ”‚ โ”‚ (WAL Mode) โ”‚
50
+ โ”‚ (without shell) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
51
+ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
52
+ ```
53
+
54
+ ---
55
+
56
+ ## ๐Ÿš€ Installation
57
+
58
+ ### Automated Installer (Linux / macOS)
59
+
60
+ Run the included automated installer script:
61
+
62
+ ```bash
63
+ chmod +x install.sh
64
+ ./install.sh
65
+ ```
66
+
67
+ The script will:
68
+ 1. Verify Python 3.10+
69
+ 2. Download the official `cloudflared` binary for your architecture (if missing)
70
+ 3. Set up the `~/.tunnelmate` directory with secure `0700` permissions
71
+ 4. Install the `tunnelmate` package
72
+ 5. Register systemd user service units
73
+
74
+ ### Manual Setup
75
+
76
+ ```bash
77
+ # 1. Clone repository & create virtual environment
78
+ git clone <repo_url> /opt/tunnelmate
79
+ cd /opt/tunnelmate
80
+ python3 -m venv .venv
81
+ source .venv/bin/activate
82
+
83
+ # 2. Install dependencies & TunnelMate package
84
+ pip install -e .
85
+
86
+ # 3. Download cloudflared binary (if not already installed on PATH)
87
+ tunnelmate install-cloudflared
88
+ ```
89
+
90
+ ---
91
+
92
+ ## ๐Ÿ Quick Startup Guide
93
+
94
+ ### Step 1: Start the Background Daemon
95
+
96
+ ```bash
97
+ tunnelmate daemon start
98
+ ```
99
+
100
+ Verify daemon health:
101
+ ```bash
102
+ tunnelmate daemon status
103
+ ```
104
+
105
+ ### Step 2: Open Interactive TUI Menu
106
+
107
+ Launch the interactive arrow-key menu:
108
+
109
+ ```bash
110
+ tunnelmate menu
111
+ ```
112
+
113
+ *(Or simply execute `tunnelmate` without arguments in an interactive terminal).*
114
+
115
+ ### Step 3: Command-Line Operations
116
+
117
+ #### Create and Start a Tunnel
118
+ ```bash
119
+ # Create a tunnel for a local service on port 8080
120
+ tunnelmate create my-web --port 8080
121
+
122
+ # Start the tunnel and get the Quick Tunnel URL
123
+ tunnelmate start my-web
124
+ ```
125
+
126
+ #### Fetch URL for Shell Scripts
127
+ ```bash
128
+ # Prints only the URL (e.g. https://xxxx.trycloudflare.com)
129
+ tunnelmate url my-web
130
+
131
+ # Example: Open in browser directly
132
+ xdg-open $(tunnelmate url my-web)
133
+ ```
134
+
135
+ #### Remap Target Port on the Fly
136
+ ```bash
137
+ # Remaps tunnel from port 8080 to 3000 (safely replaces the process)
138
+ tunnelmate port my-web 3000
139
+ ```
140
+
141
+ #### Diagnostic Health Check
142
+ ```bash
143
+ tunnelmate health my-web
144
+ ```
145
+
146
+ #### Inspect Live Logs & Audit History
147
+ ```bash
148
+ # View last 50 lines of cloudflared process output
149
+ tunnelmate logs my-web --lines 50
150
+
151
+ # View lifecycle history (creation, start, URL assignments, crashes)
152
+ tunnelmate history my-web
153
+ ```
154
+
155
+ #### List, Stop, and Delete Tunnels
156
+ ```bash
157
+ # List all tunnels with color-coded status
158
+ tunnelmate list
159
+
160
+ # Stop a running tunnel
161
+ tunnelmate stop my-web
162
+
163
+ # Delete tunnel
164
+ tunnelmate delete my-web --yes
165
+ #### Connect to Cloudflare SSH Tunnels (`tunnelmate-ssh`)
166
+ Client computers can install TunnelMate and connect to any SSH Quick Tunnel directly without writing manual `ProxyCommand` arguments (TunnelMate auto-bootstraps `cloudflared` if missing):
167
+
168
+ ```bash
169
+ # Direct SSH connection command
170
+ tunnelmate-ssh user@<trycloudflare_url>
171
+
172
+ # Example
173
+ tunnelmate-ssh shashansoni@subjective-lenses-working-subscription.trycloudflare.com
174
+
175
+ # (Or using the sub-command)
176
+ tunnelmate ssh shashansoni@subjective-lenses-working-subscription.trycloudflare.com
177
+ ```
178
+
179
+ ---
180
+
181
+ ## ๐Ÿค– Telegram Bot Remote Management
182
+
183
+ TunnelMate includes a remote Telegram Bot interface.
184
+
185
+ ### 1. Bot Configuration
186
+
187
+ Set your bot token (from [@BotFather](https://t.me/BotFather)) and configure an admin password:
188
+
189
+ ```bash
190
+ # Set bot token
191
+ tunnelmate bot set-token "123456789:ABCdefGhIJKlmNoPQRstuVWXyz"
192
+
193
+ # Set administrator password (stored as salted SHA-256 hash)
194
+ tunnelmate bot set-password "YourStrongPassword"
195
+
196
+ # View all historical chats & discovered Chat IDs
197
+ tunnelmate bot chats
198
+
199
+ # Whitelist a Chat ID as Administrator
200
+ tunnelmate bot whitelist <CHAT_ID>
201
+ # (or: tunnelmate bot add-admin <CHAT_ID>)
202
+
203
+ # Remove a Chat ID from Admin Whitelist
204
+ tunnelmate bot unwhitelist <CHAT_ID>
205
+ # (or: tunnelmate bot remove-admin <CHAT_ID>)
206
+ ```
207
+
208
+ ### 2. Bot Process Management
209
+
210
+ ```bash
211
+ # Start bot in background
212
+ tunnelmate bot start
213
+
214
+ # Check bot running status & PID
215
+ tunnelmate bot status
216
+
217
+ # Stop background bot
218
+ tunnelmate bot stop
219
+
220
+ # (Optional) Run in foreground for live debugging
221
+ tunnelmate bot run
222
+ ```
223
+
224
+ ### 3. Telegram Bot Features
225
+
226
+ - `/start`: Displays the interactive dashboard with inline buttons.
227
+ - `/login <password>`: Authenticates the user as an Administrator (the message is automatically deleted for privacy).
228
+ - `/logout`: Clears the admin session.
229
+ - `/create <name> <port> [protocol]`: Creates a new tunnel.
230
+ - `/port <name> <new_port>`: Updates the target port for an existing tunnel.
231
+ - **Inline Menus:**
232
+ - `[๐Ÿ“‹ Tunnels List]`: Shows all tunnels with statuses (`๐ŸŸข Running`, `๐Ÿ”ด Stopped`).
233
+ - `[โ–ถ๏ธ Start] / [โน๏ธ Stop] / [๐Ÿ”„ Restart]`: Controls tunnels in real-time.
234
+ - `[๐Ÿ”— Latest Link]`: Retrieves the current URL with an inline `[๐ŸŒ Open]` web button.
235
+ - `[๐Ÿฉบ Health Check]`: Diagnoses local port response and Cloudflare edge status.
236
+ - `[๐Ÿ”” Notifications]`: Subscribes users to instant alerts when tunnels start, change links, crash, or recover.
237
+
238
+ ---
239
+
240
+ ## โš™๏ธ Systemd Service Deployment
241
+
242
+ TunnelMate includes systemd service unit files for running both the daemon and the Telegram bot continuously in the background.
243
+
244
+ ### User Mode (Recommended)
245
+
246
+ ```bash
247
+ mkdir -p ~/.config/systemd/user
248
+ cp systemd/tunnelmate-daemon.service ~/.config/systemd/user/
249
+ cp systemd/tunnelmate-bot.service ~/.config/systemd/user/
250
+
251
+ systemctl --user daemon-reload
252
+
253
+ # Enable and start the daemon
254
+ systemctl --user enable --now tunnelmate-daemon
255
+
256
+ # Enable and start the Telegram bot (if token is configured)
257
+ systemctl --user enable --now tunnelmate-bot
258
+
259
+ # Check status
260
+ systemctl --user status tunnelmate-daemon
261
+ ```
262
+
263
+ To enable user services to run without an active SSH session:
264
+ ```bash
265
+ loginctl enable-linger $USER
266
+ ```
267
+
268
+ ---
269
+
270
+ ## ๐Ÿ Python Library Usage
271
+
272
+ You can embed TunnelMate directly into your Python applications:
273
+
274
+ ```python
275
+ from tunnelmate.client import TunnelMateClient
276
+
277
+ client = TunnelMateClient()
278
+
279
+ # Check daemon connectivity
280
+ if not client.is_daemon_online():
281
+ print("Daemon is offline!")
282
+ exit(1)
283
+
284
+ # Create a tunnel
285
+ tunnel = client.create_tunnel(name="fastapi-server", port=8000)
286
+
287
+ # Start tunnel and wait for Cloudflare URL
288
+ running_tunnel = client.start_tunnel("fastapi-server")
289
+ print(f"Public URL: {running_tunnel.current_url}")
290
+
291
+ # Health check
292
+ health, details = client.check_health("fastapi-server")
293
+ print(f"Health: {health.value} - {details['message']}")
294
+
295
+ # Change port
296
+ client.change_port("fastapi-server", 8080)
297
+
298
+ # Stop tunnel
299
+ client.stop_tunnel("fastapi-server")
300
+ ```
301
+
302
+ ---
303
+
304
+ ## ๐Ÿงช Running Tests
305
+
306
+ The test suite validates configuration, database transactions, health checks, cloudflared discovery, and IPC roundtrips:
307
+
308
+ ```bash
309
+ pytest -v tests/
310
+ ```
311
+
312
+ Test coverage includes:
313
+ - `test_config.py`: Salted SHA-256 password hashing and setting persistence.
314
+ - `test_db.py`: SQLite schema migrations, CRUD, history tracking, subscriber preferences.
315
+ - `test_cloudflared.py`: Architecture detection and version extraction.
316
+ - `test_health.py`: Ephemeral HTTP origin and TCP latency verification.
317
+ - `test_ipc.py`: Unix socket client-server communications and serialization.
318
+ - `test_cli.py`: Typer CLI command dispatch.
319
+
320
+ ---
321
+
322
+ ## ๐Ÿ”’ Security Specifications
323
+
324
+ 1. **Subprocess Execution:** Subprocesses are started with `subprocess.Popen` without `shell=True` and with strict argument lists to eliminate shell injection vulnerabilities.
325
+ 2. **IPC Socket:** The Unix domain socket file (`~/.tunnelmate/tunnelmate.sock`) is restricted to mode `0600` (readable/writable only by the owner).
326
+ 3. **Database & Configuration:** State directory `~/.tunnelmate` is set to `0700` and config files are set to `0600`.
327
+ 4. **Password Storage:** Telegram admin passwords use high-entropy random salts (`secrets.token_hex(16)`) and SHA-256 with constant-time equality comparisons (`secrets.compare_digest`).
328
+ 5. **Systemd Sandboxing:** Included unit files feature `ProtectSystem=strict`, `PrivateTmp=true`, and explicit `ReadWritePaths`.
329
+
330
+ ---
331
+
332
+ ## ๐Ÿ“„ License
333
+
334
+ MIT License. Built with โค๏ธ for seamless localhost ingress.