raytunnel 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.
@@ -0,0 +1,14 @@
1
+ # Python-generated files
2
+ __pycache__/
3
+ *.py[oc]
4
+ build/
5
+ dist/
6
+ wheels/
7
+ *.egg-info
8
+
9
+ # Virtual environments
10
+ .venv
11
+
12
+ # Local configuration
13
+ .env
14
+ docker/.env
@@ -0,0 +1 @@
1
+ 3.9
@@ -0,0 +1,25 @@
1
+ FROM python:3.11-slim
2
+
3
+ WORKDIR /app
4
+
5
+ # Install dependencies needed for package installation
6
+ RUN pip install --no-cache-dir --upgrade pip
7
+
8
+ # Copy the package source
9
+ COPY . /app
10
+
11
+ # Install the package in editable or standard mode
12
+ RUN pip install .
13
+
14
+ # Expose the API/WS port and the dynamic SSH forwarding port range
15
+ EXPOSE 8000 8001 2200-2300
16
+
17
+ # Environment variables
18
+ ENV RAYTUNNEL_TOKEN="change-me"
19
+ ENV HOST="0.0.0.0"
20
+ ENV PORT="8001"
21
+ ENV TCP_PORT="8000"
22
+ ENV DOMAIN="s.yourdomain.com"
23
+
24
+ # Command to run the server
25
+ CMD raytunnel server --host $HOST --port $PORT --tcp-port $TCP_PORT --token $RAYTUNNEL_TOKEN --domain $DOMAIN
@@ -0,0 +1,357 @@
1
+ Metadata-Version: 2.4
2
+ Name: raytunnel
3
+ Version: 0.1.0
4
+ Summary: A lightweight, secure reverse tunnel and SSH forwarding tool — Ngrok/Gradio alternative for Homelab.
5
+ Project-URL: Homepage, https://github.com/mrtruongleo/raytunnel
6
+ Project-URL: Repository, https://github.com/mrtruongleo/raytunnel
7
+ Project-URL: Issues, https://github.com/mrtruongleo/raytunnel/issues
8
+ Author-email: mrtruongleo <66201727+mrtruongleo@users.noreply.github.com>
9
+ License: MIT
10
+ Keywords: homelab,kaggle,ngrok,reverse-proxy,ssh,tunnel,websocket
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: License :: OSI Approved :: MIT License
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.9
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Topic :: Internet :: Proxy Servers
20
+ Classifier: Topic :: System :: Networking
21
+ Requires-Python: >=3.9
22
+ Requires-Dist: click>=8.0.0
23
+ Requires-Dist: fastapi>=0.100.0
24
+ Requires-Dist: rich>=13.0.0
25
+ Requires-Dist: uvicorn>=0.22.0
26
+ Requires-Dist: websockets>=11.0.0
27
+ Description-Content-Type: text/markdown
28
+
29
+ # Raytunnel 🚀
30
+
31
+ Raytunnel là một giải pháp reverse tunnel (HTTP và SSH forwarding) siêu nhẹ, bảo mật và được thiết kế tối giản nhằm thay thế cho Gradio/Ngrok. Thích hợp nhất cho việc tương tác giữa máy cá nhân và các remote instance (Kaggle, Colab, Cloud Server) kết nối trực tiếp về hệ thống Homelab.
32
+
33
+ ---
34
+
35
+ ## 🌟 Tính năng nổi bật
36
+
37
+ - **Tối giản & Tự chủ**: Hoạt động hoàn toàn trong user-space, không cần cấu hình tài khoản Unix hay SSH keys phức tạp trên Host OS của server.
38
+ - **Tự động cấu hình SSH Daemon**: Client tự phát hiện, cấu hình và khởi chạy SSH Daemon (`sshd`) ở cổng 2222, tạo cặp khóa SSH động giúp bạn SSH trực tiếp vào remote instance (ví dụ: Kaggle) cực kỳ tiện lợi.
39
+ - **Dynamic HTTP Host Routing**: Tự động phân tích trường `Host` trong HTTP request để điều hướng tới đúng client đăng ký (ví dụ: `my-worker.s.yourdomain.com`).
40
+ - **Thân thiện với Notebook**: Có sẵn API bất đồng bộ chạy ngầm (`background=True`) để bạn gọi trực tiếp trong cell của Jupyter/Kaggle notebook mà không làm nghẽn luồng xử lý chính.
41
+ - **Bảo mật tuyệt đối**:
42
+ - Xác thực kết nối điều khiển bằng Token bí mật.
43
+ - SSH daemon được cấu hình chỉ nhận khóa public key, cấm đăng nhập bằng mật khẩu (`PasswordAuthentication no`).
44
+
45
+ ---
46
+
47
+ ## 📐 Kiến trúc hoạt động
48
+
49
+ ```mermaid
50
+ sequenceDiagram
51
+ participant Client as Kaggle / Remote Worker
52
+ participant Server as Raytunnel Server (s.yourdomain.com)
53
+ participant Proxy as Reverse Proxy (Traefik / NPM)
54
+ participant User as Terminal / Trình duyệt
55
+
56
+ Client->>Server: Kết nối wss://s.yourdomain.com/ws/control + Auth Token
57
+ Server-->>Client: Trả về Subdomain: client-abc, SSH Port: 2200
58
+
59
+ Note over User,Client: Chuyển tiếp HTTP Web App
60
+ User->>Proxy: Truy cập https://client-abc.s.yourdomain.com
61
+ Proxy->>Server: Forward HTTP → cổng 8000 (TCP router)
62
+ Server->>Client: Yêu cầu mở kênh HTTP
63
+ Client->>Server: Kết nối wss://s.yourdomain.com/ws/data?channel_id=xxx
64
+ Client->>Client: Forward tới web app local (cổng 8000)
65
+ Server-->>Client: Chuyển tiếp dữ liệu hai chiều (TCP <-> WS)
66
+
67
+ Note over User,Client: Kết nối SSH Terminal
68
+ User->>Server: Chạy ssh -p 2200 user@s.yourdomain.com
69
+ Server->>Client: Yêu cầu mở kênh SSH
70
+ Client->>Server: Kết nối wss://s.yourdomain.com/ws/data?channel_id=yyy
71
+ Client->>Client: Forward tới SSH Daemon nội bộ (cổng 2222)
72
+ Server-->>Client: Chuyển tiếp luồng terminal hai chiều
73
+ ```
74
+
75
+ ### Phân vai các cổng
76
+
77
+ | Cổng | Mục đích |
78
+ |------|----------|
79
+ | `8001` | FastAPI API + WebSocket control (`/ws/control`, `/ws/data`) + Dashboard |
80
+ | `8000` | TCP raw proxy — đọc Host header và route HTTP traffic đến đúng client |
81
+ | `2200-2300` | Dải cổng SSH động được server cấp phát cho từng tunnel |
82
+
83
+ > [!IMPORTANT]
84
+ > Reverse proxy cần **2 route riêng biệt**: `s.yourdomain.com` → cổng **8001** (control WebSocket + Dashboard), `*.s.yourdomain.com` → cổng **8000** (HTTP tunnel traffic). Nếu route tất cả về 8000, client sẽ gặp lỗi `HTTP 404` khi kết nối WebSocket control.
85
+
86
+ ---
87
+
88
+ ## 📦 Cấu trúc dự án
89
+
90
+ ```
91
+ raytunnel/
92
+ ├── src/ # Source code Python
93
+ ├── Dockerfile # Docker image definition
94
+ ├── compose.yaml # Docker Compose config
95
+ ├── example-env # Template file biến môi trường
96
+ ├── raytunnel-server.service # Systemd service file
97
+ ├── pyproject.toml
98
+ └── README.md
99
+ ```
100
+
101
+ ---
102
+
103
+ ## 🛠 Hướng dẫn Cấu hình & Triển khai
104
+
105
+ ### 1. Triển khai Server (Trên Proxmox Container / VPS)
106
+
107
+ Bạn có ba cách để chạy server:
108
+
109
+ #### Cách A: Chạy trực tiếp qua Systemd Service (Khuyên dùng)
110
+
111
+ 1. Cài đặt package `raytunnel` trên container:
112
+ ```bash
113
+ pip install git+https://github.com/mrtruongleo/raytunnel.git
114
+ ```
115
+ 2. Copy file `raytunnel-server.service` vào `/etc/systemd/system/`:
116
+ ```bash
117
+ cp raytunnel-server.service /etc/systemd/system/
118
+ ```
119
+ 3. Chỉnh sửa dòng `ExecStart` trong file service — thêm token và domain thực:
120
+ ```ini
121
+ ExecStart=/usr/local/bin/raytunnel server --host 0.0.0.0 --port 8001 --tcp-port 8000 --token your_very_strong_token --domain s.yourdomain.com
122
+ ```
123
+ 4. Kích hoạt và chạy dịch vụ:
124
+ ```bash
125
+ sudo systemctl daemon-reload
126
+ sudo systemctl enable --now raytunnel-server
127
+ sudo systemctl status raytunnel-server
128
+ ```
129
+
130
+ #### Cách B: Chạy qua Docker Compose
131
+
132
+ 1. Copy file cấu hình môi trường:
133
+ ```bash
134
+ cp example-env .env
135
+ ```
136
+ 2. Chỉnh sửa `.env` với token và domain thực của bạn:
137
+ ```env
138
+ RAYTUNNEL_TOKEN=your_very_strong_token
139
+ DOMAIN=s.yourdomain.com
140
+ ```
141
+ 3. Build và chạy từ thư mục gốc của project:
142
+ ```bash
143
+ docker compose up -d --build
144
+ ```
145
+
146
+ Verify sau khi start:
147
+ ```bash
148
+ docker logs raytunnel-server --tail 20
149
+ curl http://localhost:8001 # → Dashboard hiện "No active tunnels"
150
+ ```
151
+
152
+ > [!WARNING]
153
+ > **Không dùng shell variable** `${RAYTUNNEL_TOKEN:-default}` trong compose.yaml nếu bạn không set env trước khi chạy — sẽ dùng giá trị `default` thay vì token thực. Luôn dùng file `.env` hoặc hardcode trực tiếp.
154
+
155
+ #### Cách C: Chạy qua Docker CLI trực tiếp
156
+
157
+ ```bash
158
+ docker build -t raytunnel-server .
159
+ docker run -d --name raytunnel \
160
+ -p 8000:8000 -p 8001:8001 -p 2200-2300:2200-2300 \
161
+ -e RAYTUNNEL_TOKEN="your_very_strong_token" \
162
+ -e DOMAIN="s.yourdomain.com" \
163
+ raytunnel-server
164
+ ```
165
+
166
+ ---
167
+
168
+ ### 2. Cấu hình DNS & Reverse Proxy
169
+
170
+ #### 2.1 DNS trên Cloudflare
171
+
172
+ Tạo 2 bản ghi CNAME (hoặc A) trỏ về IP public của homelab:
173
+
174
+ | Type | Name | Target | Proxy |
175
+ |------|------|--------|-------|
176
+ | `CNAME` | `s` | `ip.yourdomain.com` | Proxied ☁️ |
177
+ | `CNAME` | `*.s` | `ip.yourdomain.com` | **DNS only** 🔘 |
178
+
179
+ > [!WARNING]
180
+ > Cloudflare **Free plan không hỗ trợ Proxied wildcard** — bắt buộc để `*.s` là **DNS only** (grey cloud). Verify bằng: `dig rvc.s.yourdomain.com @1.1.1.1 +short`
181
+
182
+ #### 2.2 Port Forwarding trên Router
183
+
184
+ | External Port | Internal IP | Internal Port | Protocol |
185
+ |--------------|-------------|---------------|----------|
186
+ | `443` | IP của reverse proxy | `443` | TCP |
187
+ | `2200-2300` | IP của raytunnel server | `2200-2300` | TCP |
188
+
189
+ > [!NOTE]
190
+ > Cổng `2200-2300` chỉ thực sự listen sau khi có client kết nối. `Connection refused` trên cổng này là bình thường khi chưa có tunnel nào hoạt động.
191
+
192
+ ---
193
+
194
+ #### 2.3 Cấu hình Traefik (Nếu bạn dùng Traefik)
195
+
196
+ Thêm service raytunnel vào Traefik bằng cách tạo một file config động trong thư mục dynamic config của Traefik (ví dụ: `./external/raytunnel.yaml`):
197
+
198
+ ```yaml
199
+ http:
200
+ routers:
201
+ # Control plane: WebSocket /ws/control + Dashboard — cổng 8001 (FastAPI)
202
+ raytunnel-control:
203
+ rule: "Host(`s.yourdomain.com`)"
204
+ entryPoints:
205
+ - websecure
206
+ service: raytunnel-api
207
+ tls:
208
+ certResolver: production
209
+
210
+ # Tunnel traffic: worker.s.yourdomain.com — cổng 8000 (TCP router)
211
+ raytunnel-tunnel:
212
+ rule: "HostRegexp(`{subdomain:[a-z0-9-]+}.s.yourdomain.com`)"
213
+ entryPoints:
214
+ - websecure
215
+ service: raytunnel-tcp
216
+ tls:
217
+ certResolver: production
218
+
219
+ services:
220
+ raytunnel-api:
221
+ loadBalancer:
222
+ servers:
223
+ - url: "http://<IP_RAYTUNNEL_SERVER>:8001" # Control/WS API
224
+
225
+ raytunnel-tcp:
226
+ loadBalancer:
227
+ servers:
228
+ - url: "http://<IP_RAYTUNNEL_SERVER>:8000" # HTTP tunnel routing
229
+ ```
230
+
231
+ > [!NOTE]
232
+ > Thay `<IP_RAYTUNNEL_SERVER>` bằng IP thực của container/VPS đang chạy raytunnel server. Đảm bảo thư mục dynamic config của Traefik đang được watch (thường cấu hình qua `providers.file.directory` trong `traefik.yaml`).
233
+
234
+ ---
235
+
236
+ #### 2.4 Cấu hình Nginx Proxy Manager (Nếu bạn dùng NPM)
237
+
238
+ Nginx Proxy Manager cần **2 Proxy Host** riêng biệt. Cả hai đều cần bật **SSL với Let's Encrypt**.
239
+
240
+ ##### Proxy Host 1 — Control Plane (`s.yourdomain.com`)
241
+
242
+ Vào **Proxy Hosts → Add Proxy Host**, điền như sau:
243
+
244
+ | Trường | Giá trị |
245
+ |--------|---------|
246
+ | Domain Names | `s.yourdomain.com` |
247
+ | Scheme | `http` |
248
+ | Forward Hostname/IP | `<IP_RAYTUNNEL_SERVER>` |
249
+ | Forward Port | `8001` |
250
+ | Websockets Support | ✅ Bật |
251
+ | Block Common Exploits | ✅ Bật |
252
+
253
+ Tab **SSL**: Chọn **Request a new SSL Certificate**, bật **Force SSL** và **HTTP/2 Support**.
254
+
255
+ ##### Proxy Host 2 — Tunnel Traffic (`*.s.yourdomain.com`)
256
+
257
+ Vào **Proxy Hosts → Add Proxy Host**, điền như sau:
258
+
259
+ | Trường | Giá trị |
260
+ |--------|---------|
261
+ | Domain Names | `*.s.yourdomain.com` |
262
+ | Scheme | `http` |
263
+ | Forward Hostname/IP | `<IP_RAYTUNNEL_SERVER>` |
264
+ | Forward Port | `8000` |
265
+ | Websockets Support | ✅ Bật |
266
+ | Block Common Exploits | ✅ Bật |
267
+
268
+ Tab **SSL**: Chọn **Request a new SSL Certificate** — NPM sẽ issue wildcard cert qua DNS Challenge.
269
+
270
+ > [!IMPORTANT]
271
+ > **Wildcard SSL (`*.s.yourdomain.com`) bắt buộc phải dùng DNS Challenge** (HTTP Challenge không hỗ trợ wildcard). Trong NPM, tab SSL → chọn DNS Challenge provider (ví dụ Cloudflare) → nhập API Token Cloudflare có quyền `Zone:DNS:Edit`.
272
+
273
+ > [!WARNING]
274
+ > NPM không thực hiện routing theo subdomain ở tầng ứng dụng — toàn bộ `*.s.yourdomain.com` được forward thẳng về cổng `8000` của raytunnel server, và **raytunnel server tự xử lý routing** dựa vào `Host` header. Đây là hành vi đúng và mong muốn.
275
+
276
+ Tab **Advanced** của Proxy Host 2, thêm custom Nginx config để đảm bảo `Host` header được truyền đúng:
277
+
278
+ ```nginx
279
+ proxy_set_header Host $host;
280
+ proxy_set_header X-Real-IP $remote_addr;
281
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
282
+ proxy_set_header X-Forwarded-Proto $scheme;
283
+ ```
284
+
285
+ ---
286
+
287
+ ### 3. Cài đặt & Sử dụng Client (Trên Kaggle / Colab / Máy trạm)
288
+
289
+ ```bash
290
+ # Cài đặt qua pip từ Git (môi trường remote)
291
+ pip install git+https://github.com/mrtruongleo/raytunnel.git
292
+
293
+ # Hoặc cài đặt ở chế độ editable (phát triển local)
294
+ uv pip install -e /path/to/raytunnel
295
+ ```
296
+
297
+ #### Cách A: Sử dụng Command Line (CLI)
298
+
299
+ ```bash
300
+ raytunnel client --server s.yourdomain.com --token your_very_strong_token --port 8000 --ssh --subdomain my-kaggle-job
301
+ ```
302
+
303
+ #### Cách B: Chạy Programmatic trong Python / Jupyter Notebook
304
+
305
+ > [!WARNING]
306
+ > **Kaggle không hỗ trợ background process qua `!command &`** — sẽ gặp lỗi `OSError: Background processes not supported`. Phải dùng `subprocess.Popen`.
307
+
308
+ ```python
309
+ import subprocess, time
310
+
311
+ proc = subprocess.Popen(
312
+ [
313
+ "raytunnel", "client",
314
+ "--server", "s.yourdomain.com",
315
+ "--token", "your_very_strong_token",
316
+ "--port", "8000", # Cổng local của Web App (RVC, Gradio, v.v.)
317
+ "--ssh", # Bật SSH Terminal
318
+ "--subdomain", "my-worker" # Subdomain mong muốn
319
+ ],
320
+ stdout=open("raytunnel.log", "w"),
321
+ stderr=subprocess.STDOUT
322
+ )
323
+ print(f"Raytunnel PID: {proc.pid}")
324
+
325
+ # Chờ kết nối thiết lập
326
+ time.sleep(8)
327
+ with open("raytunnel.log") as f:
328
+ print(f.read())
329
+ ```
330
+
331
+ Kết quả mong đợi trong log:
332
+ ```
333
+ ✔ Raytunnel Established Successfully!
334
+ HTTP Web App URL: https://my-worker.s.yourdomain.com
335
+ SSH Command: ssh -i ~/.ssh/raytunnel_id_ed25519 -p 2200 root@s.yourdomain.com
336
+ ```
337
+
338
+ Dừng tunnel:
339
+ ```python
340
+ proc.terminate()
341
+ ```
342
+
343
+ ---
344
+
345
+ ## 🔒 Cơ chế Bảo mật của SSH
346
+
347
+ Khi bạn khởi chạy client với tùy chọn `--ssh`:
348
+ 1. Client kiểm tra và tự động cài đặt `openssh-server` nếu môi trường chưa có.
349
+ 2. Sinh một cặp khóa SSH tạm thời: Private key `~/.ssh/raytunnel_id_ed25519` và Public key được tự động thêm vào `~/.ssh/authorized_keys`.
350
+ 3. Khởi chạy `sshd` ở cổng 2222 trong phạm vi user-space.
351
+ 4. Server cấp phát một cổng SSH ngẫu nhiên trong dải `2200-2300` chuyển tiếp về client.
352
+ 5. Để kết nối SSH vào Kaggle từ máy cá nhân:
353
+ ```bash
354
+ # Copy private key từ Kaggle về máy, sau đó:
355
+ ssh -i raytunnel_id_ed25519 -p 2200 root@s.yourdomain.com
356
+ ```
357
+ Server cấm đăng nhập bằng mật khẩu và chỉ nhận đúng khóa đã đăng ký — đảm bảo không ai brute-force được.