terabox-gateway 2.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.
- terabox_gateway-2.0.0/LICENSE +21 -0
- terabox_gateway-2.0.0/PKG-INFO +599 -0
- terabox_gateway-2.0.0/README.md +580 -0
- terabox_gateway-2.0.0/pyproject.toml +42 -0
- terabox_gateway-2.0.0/setup.cfg +4 -0
- terabox_gateway-2.0.0/src/terabox_gateway/__init__.py +4 -0
- terabox_gateway-2.0.0/src/terabox_gateway/api.py +502 -0
- terabox_gateway-2.0.0/src/terabox_gateway/cache.py +78 -0
- terabox_gateway-2.0.0/src/terabox_gateway/config.py +124 -0
- terabox_gateway-2.0.0/src/terabox_gateway/endpoints/__init__.py +73 -0
- terabox_gateway-2.0.0/src/terabox_gateway/main.py +13 -0
- terabox_gateway-2.0.0/src/terabox_gateway/rate_limiter.py +92 -0
- terabox_gateway-2.0.0/src/terabox_gateway/swagger/swagger.json +168 -0
- terabox_gateway-2.0.0/src/terabox_gateway/terabox_client.py +417 -0
- terabox_gateway-2.0.0/src/terabox_gateway/utils.py +164 -0
- terabox_gateway-2.0.0/src/terabox_gateway.egg-info/PKG-INFO +599 -0
- terabox_gateway-2.0.0/src/terabox_gateway.egg-info/SOURCES.txt +19 -0
- terabox_gateway-2.0.0/src/terabox_gateway.egg-info/dependency_links.txt +1 -0
- terabox_gateway-2.0.0/src/terabox_gateway.egg-info/entry_points.txt +2 -0
- terabox_gateway-2.0.0/src/terabox_gateway.egg-info/requires.txt +2 -0
- terabox_gateway-2.0.0/src/terabox_gateway.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
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,599 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: terabox-gateway
|
|
3
|
+
Version: 2.0.0
|
|
4
|
+
Summary: Fast Python gateway for extracting metadata, thumbnails, and direct download links from Terabox share URLs.
|
|
5
|
+
Author-email: "@Saahiyo" <linkg518@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/saahiyo/terabox-gateway
|
|
8
|
+
Project-URL: Bug Tracker, https://github.com/saahiyo/terabox-gateway/issues
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Framework :: Flask
|
|
13
|
+
Requires-Python: >=3.8
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
License-File: LICENSE
|
|
16
|
+
Requires-Dist: flask>=3.0.0
|
|
17
|
+
Requires-Dist: aiohttp>=3.9.0
|
|
18
|
+
Dynamic: license-file
|
|
19
|
+
|
|
20
|
+
# TeraBox Gateway API
|
|
21
|
+
|
|
22
|
+
Fast Python gateway for extracting metadata, thumbnails and direct download links from Terabox share URLs.
|
|
23
|
+
|
|
24
|
+
[](https://github.com/saahiyo/terabox-gateway/stargazers)
|
|
25
|
+
[](https://github.com/saahiyo/terabox-gateway/network/members)
|
|
26
|
+
[](https://github.com/saahiyo/terabox-gateway/blob/main/LICENSE)
|
|
27
|
+
[](https://github.com/saahiyo/terabox-gateway)
|
|
28
|
+
[](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fsaahiyo%2Fterabox-gateway&env=COOKIE_JSON)
|
|
29
|
+
|
|
30
|
+

|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
This project provides:
|
|
34
|
+
- **Interactive Swagger Playground** at `/docs` for testing endpoints directly in the browser
|
|
35
|
+
- **Web API** with endpoints for listing files and retrieving direct links
|
|
36
|
+
- **Unified Cloudflare Worker Proxy** for optimized TeraBox API access
|
|
37
|
+
- **Vercel-ready deployment** configuration
|
|
38
|
+
- **Flexible cookie authentication** with support for simple string or JSON formats
|
|
39
|
+
|
|
40
|
+
The API uses Flask 3.x native async support with `aiohttp` for asynchronous requests and leverages a unified Cloudflare Worker proxy with mode-based operations for efficient TeraBox API interaction.
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
## Features
|
|
45
|
+
|
|
46
|
+
- **Web API Endpoints**:
|
|
47
|
+
- `GET /docs`: Interactive Swagger UI documentation playground (API playground)
|
|
48
|
+
- `GET /swagger.json`: OpenAPI 3.0.0 schema specification
|
|
49
|
+
- `GET /api`: Unified endpoint - file listing with short links (resolve with `resolve=true`), and proxy modes (resolve, lookup, stream, page, api, segment, health)
|
|
50
|
+
- `GET /admin/*`: Path-based admin endpoints to inspect database records and analytics (overview, shares, files, thumbnails, kv/entry)
|
|
51
|
+
- `GET /health`: Simple health check endpoint
|
|
52
|
+
- `GET /`: API information and status
|
|
53
|
+
|
|
54
|
+
- **Flexible Cookie Configuration**:
|
|
55
|
+
- Simple string format: Just paste your `ndus` token
|
|
56
|
+
- Full JSON format: Supports multiple cookie fields
|
|
57
|
+
- Environment variable or file-based configuration
|
|
58
|
+
|
|
59
|
+
- **Rate Limiting**:
|
|
60
|
+
- Per-IP sliding-window rate limiter
|
|
61
|
+
- Configurable limits via environment variables
|
|
62
|
+
- Returns `429 Too Many Requests` with `Retry-After` header
|
|
63
|
+
|
|
64
|
+
- **Response Caching**:
|
|
65
|
+
- In-memory LRU cache with TTL expiration
|
|
66
|
+
- Reduces redundant upstream requests on repeated lookups
|
|
67
|
+
- Configurable TTL and max cache size
|
|
68
|
+
|
|
69
|
+
- **Production Ready**:
|
|
70
|
+
- CORS enabled for browser clients
|
|
71
|
+
- Vercel deployment configuration included
|
|
72
|
+
- Error handling with detailed logging
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
## Directory Structure
|
|
77
|
+
|
|
78
|
+
```
|
|
79
|
+
terabox-gateway/
|
|
80
|
+
├── api.py # Main Flask application and API routes
|
|
81
|
+
├── config.py # Configuration and constants (proxy URLs, headers)
|
|
82
|
+
├── terabox_client.py # TeraBox API client with unified proxy integration
|
|
83
|
+
├── utils.py # Utility functions (validation, formatting)
|
|
84
|
+
├── rate_limiter.py # Per-IP sliding-window rate limiter
|
|
85
|
+
├── cache.py # In-memory LRU cache with TTL expiration
|
|
86
|
+
├── main.py # Entry point for running Flask locally
|
|
87
|
+
├── .env # Environment variables (not tracked in git)
|
|
88
|
+
├── .env.example # Example environment configuration
|
|
89
|
+
├── requirements.txt # Python dependencies
|
|
90
|
+
├── vercel.json # Vercel deployment configuration
|
|
91
|
+
├── tboxproxy_usage.md # Unified proxy API documentation
|
|
92
|
+
├── .gitignore # Git ignore file
|
|
93
|
+
├── LICENSE # MIT License
|
|
94
|
+
├── README.md # This file
|
|
95
|
+
└── endpoints/ # Reserved for future route modularization
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
---
|
|
99
|
+
|
|
100
|
+
## Requirements
|
|
101
|
+
|
|
102
|
+
- **Python 3.10+**
|
|
103
|
+
- **Dependencies**:
|
|
104
|
+
- `Flask[async]>=3.1,<4`
|
|
105
|
+
- `Werkzeug>=3.1,<4`
|
|
106
|
+
- `aiohttp>=3.8,<4`
|
|
107
|
+
|
|
108
|
+
### Installation
|
|
109
|
+
|
|
110
|
+
Install the required packages using pip:
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
pip install -r requirements.txt
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
---
|
|
117
|
+
|
|
118
|
+
## Quick Start
|
|
119
|
+
|
|
120
|
+
### 1. Configure Cookies
|
|
121
|
+
|
|
122
|
+
Create a `.env` file in the project root (copy from `.env.example`):
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
cp .env.example .env
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Add your TeraBox `ndus` cookie token to `.env`:
|
|
129
|
+
|
|
130
|
+
**Option 1: Simple String Format (Recommended)**
|
|
131
|
+
```env
|
|
132
|
+
COOKIE_JSON=YdPCtvYteHui3XC6demNk-M2HgRzVrnh0txZQG6X
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
**Option 2: Full JSON Format**
|
|
136
|
+
```env
|
|
137
|
+
COOKIE_JSON={"ndus": "YdPCtvYteHui3XC6demNk-M2HgRzVrnh0txZQG6X", "other": "value"}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
The API automatically detects the format and handles it accordingly.
|
|
141
|
+
|
|
142
|
+
### 2. Run the API Locally
|
|
143
|
+
|
|
144
|
+
Execute the `main.py` script:
|
|
145
|
+
|
|
146
|
+
```bash
|
|
147
|
+
python main.py
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
The server will start at:
|
|
151
|
+
- `http://localhost:5000`
|
|
152
|
+
- `http://0.0.0.0:5000` (accessible from network)
|
|
153
|
+
|
|
154
|
+
---
|
|
155
|
+
|
|
156
|
+
## Getting Your TeraBox Cookies
|
|
157
|
+
|
|
158
|
+
The API requires a valid `ndus` cookie to authenticate with TeraBox:
|
|
159
|
+
|
|
160
|
+
1. Log in to [terabox.com](https://www.terabox.com) or [1024terabox.com](https://1024terabox.com)
|
|
161
|
+
2. Open your browser's Developer Tools (press `F12`)
|
|
162
|
+
3. Go to the **Application** or **Storage** tab
|
|
163
|
+
4. Navigate to **Cookies** → Select the TeraBox domain
|
|
164
|
+
5. Find the cookie named `ndus` and copy its **Value**
|
|
165
|
+
6. Paste the value into your `.env` file as shown above
|
|
166
|
+
|
|
167
|
+
**Note**: Cookies expire periodically. If you start getting authentication errors, refresh your cookies.
|
|
168
|
+
|
|
169
|
+
---
|
|
170
|
+
|
|
171
|
+
## API Usage
|
|
172
|
+
|
|
173
|
+
### Endpoints
|
|
174
|
+
|
|
175
|
+
#### `GET /` - API Information
|
|
176
|
+
Returns metadata about the API, available endpoints, and status.
|
|
177
|
+
|
|
178
|
+
```bash
|
|
179
|
+
curl http://localhost:5000/
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
#### `GET /health` - Health Check
|
|
183
|
+
Simple health check endpoint that returns the current status.
|
|
184
|
+
|
|
185
|
+
```bash
|
|
186
|
+
curl http://localhost:5000/health
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
#### `GET /docs` - Interactive Swagger UI Playground
|
|
191
|
+
An interactive developer playground to browse endpoints and execute requests directly from the browser.
|
|
192
|
+
|
|
193
|
+
Access in browser at: `http://localhost:5000/docs`
|
|
194
|
+
|
|
195
|
+
#### `GET /swagger.json` - OpenAPI Specification
|
|
196
|
+
Exposes the OpenAPI 3.0.0 JSON schema defining the entire API.
|
|
197
|
+
|
|
198
|
+
```bash
|
|
199
|
+
curl http://localhost:5000/swagger.json
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
#### `GET /api` - Unified Endpoint (File Listing + Proxy Modes)
|
|
204
|
+
|
|
205
|
+
The `/api` endpoint handles **all use cases** in one place:
|
|
206
|
+
|
|
207
|
+
**Pattern 1: File Listing with Short Download Links (Fast - Default)**
|
|
208
|
+
Retrieves file metadata and the shorter proxy-redirect links. This is the default mode and resolves in milliseconds since no slow redirect checks are run.
|
|
209
|
+
|
|
210
|
+
```bash
|
|
211
|
+
curl "http://localhost:5000/api?url=https://1024terabox.com/s/1LNr3tyl5pI5KUM8BecGtyQ"
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
**Pattern 2: File Listing with Fully-Resolved Direct Download Links**
|
|
215
|
+
Retrieves file metadata and follows redirects to get the long target PCS direct download URLs.
|
|
216
|
+
|
|
217
|
+
```bash
|
|
218
|
+
curl "http://localhost:5000/api?url=https://1024terabox.com/s/1LNr3tyl5pI5KUM8BecGtyQ&resolve=true"
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
**Pattern 3: Proxy Modes**
|
|
222
|
+
Direct access to the Cloudflare Worker proxy with multiple modes for different use cases.
|
|
223
|
+
|
|
224
|
+
**Mode: `resolve` (Recommended)**
|
|
225
|
+
Auto extract jsToken + fetch share API in one call.
|
|
226
|
+
```bash
|
|
227
|
+
curl "http://localhost:5000/api?mode=resolve&surl=abc123"
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
**Mode: `lookup` (🚀 Fastest)**
|
|
231
|
+
Query the D1 database cache directly with no upstream calls. Requires `surl` or `fid`.
|
|
232
|
+
```bash
|
|
233
|
+
curl "http://localhost:5000/api?mode=lookup&surl=abc123"
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
**Mode: `page`**
|
|
237
|
+
Proxy raw share HTML page for debugging.
|
|
238
|
+
```bash
|
|
239
|
+
curl "http://localhost:5000/api?mode=page&surl=abc123"
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
**Mode: `api`**
|
|
243
|
+
Manual share API proxy when jsToken is already known.
|
|
244
|
+
```bash
|
|
245
|
+
curl "http://localhost:5000/api?mode=api&jsToken=XYZ&shorturl=abc123"
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
**Mode: `stream`**
|
|
249
|
+
Fetch and rewrite M3U8 playlist for HLS streaming.
|
|
250
|
+
```bash
|
|
251
|
+
curl "http://localhost:5000/api?mode=stream&surl=abc123"
|
|
252
|
+
# Optional: specify quality
|
|
253
|
+
curl "http://localhost:5000/api?mode=stream&surl=abc123&type=M3U8_AUTO_720"
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
**Mode: `segment`**
|
|
257
|
+
Proxy media segments (.ts, .m4s) - used automatically by rewritten playlists.
|
|
258
|
+
```bash
|
|
259
|
+
curl "http://localhost:5000/api?mode=segment&url=ENCODED_URL"
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
**Mode: `health`**
|
|
263
|
+
Check the health status of the worker service.
|
|
264
|
+
```bash
|
|
265
|
+
curl "http://localhost:5000/api?mode=health"
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
**Notes**:
|
|
269
|
+
- Cookies are forwarded from client request if provided in `Cookie` header
|
|
270
|
+
- Use `mode=resolve` for most use cases
|
|
271
|
+
- `stream` and `segment` modes enable HLS video playback in browsers and players
|
|
272
|
+
- Legacy `/api?url=...` usage remains fully supported
|
|
273
|
+
|
|
274
|
+
#### `GET /admin/*` - Admin Endpoints
|
|
275
|
+
Path-based admin endpoints to inspect database records and analytics. All requests require authentication if configured in the worker. Pass the key using either `?key=YOUR_ADMIN_KEY` or `x-admin-key` header.
|
|
276
|
+
- `GET /admin/overview`: Summary metrics of shares and files
|
|
277
|
+
- `GET /admin/shares`: Search and list resolved shares
|
|
278
|
+
- `GET /admin/files`: Search and filter media files
|
|
279
|
+
- `GET /admin/kv/entry?surl=...`: Lookup raw cached D1 share record
|
|
280
|
+
|
|
281
|
+
---
|
|
282
|
+
|
|
283
|
+
## Supported TeraBox Domains
|
|
284
|
+
|
|
285
|
+
The API validates and supports the following TeraBox domains:
|
|
286
|
+
- `terabox.app`
|
|
287
|
+
- `teraboxshare.com`
|
|
288
|
+
- `terabox.com`
|
|
289
|
+
- `1024terabox.com`
|
|
290
|
+
- `teraboxlink.com`
|
|
291
|
+
- `terasharefile.com`
|
|
292
|
+
- `terafileshare.com`
|
|
293
|
+
- `terasharelink.com`
|
|
294
|
+
|
|
295
|
+
Both `http://` and `https://` protocols are supported.
|
|
296
|
+
|
|
297
|
+
---
|
|
298
|
+
|
|
299
|
+
## Integration Examples
|
|
300
|
+
|
|
301
|
+
You can easily query the gateway API using your preferred programming language. Here are examples of retrieving direct download links for a TeraBox share URL using the `/api` endpoint:
|
|
302
|
+
|
|
303
|
+
### 🐍 Python (using `requests`)
|
|
304
|
+
|
|
305
|
+
```python
|
|
306
|
+
import requests
|
|
307
|
+
|
|
308
|
+
def get_direct_links(gateway_url, share_url):
|
|
309
|
+
response = requests.get(
|
|
310
|
+
f"{gateway_url}/api",
|
|
311
|
+
params={"url": share_url}
|
|
312
|
+
)
|
|
313
|
+
if response.status_code == 200:
|
|
314
|
+
data = response.json()
|
|
315
|
+
if data.get("status") == "success":
|
|
316
|
+
for file in data.get("files", []):
|
|
317
|
+
print(f"File: {file['filename']}")
|
|
318
|
+
print(f"Size: {file['size']}")
|
|
319
|
+
print(f"Direct Link: {file['download_link']}\n")
|
|
320
|
+
else:
|
|
321
|
+
print("Error:", data.get("message"))
|
|
322
|
+
else:
|
|
323
|
+
print("HTTP Error:", response.status_code)
|
|
324
|
+
|
|
325
|
+
# Example Usage:
|
|
326
|
+
# get_direct_links("http://localhost:5000", "https://teraboxshare.com/s/1LNr3tyl5pI5KUM8BecGtyQ")
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
### 🟨 JavaScript (Node.js / Browser)
|
|
330
|
+
|
|
331
|
+
```javascript
|
|
332
|
+
async function getDirectLinks(gatewayUrl, shareUrl) {
|
|
333
|
+
try {
|
|
334
|
+
const response = await fetch(`${gatewayUrl}/api?url=${encodeURIComponent(shareUrl)}`);
|
|
335
|
+
const data = await response.json();
|
|
336
|
+
|
|
337
|
+
if (response.ok && data.status === 'success') {
|
|
338
|
+
data.files.forEach(file => {
|
|
339
|
+
console.log(`File: ${file.filename}`);
|
|
340
|
+
console.log(`Size: ${file.size}`);
|
|
341
|
+
console.log(`Direct Link: ${file.download_link}\n`);
|
|
342
|
+
});
|
|
343
|
+
} else {
|
|
344
|
+
console.error('Error:', data.message || 'Failed to resolve links');
|
|
345
|
+
}
|
|
346
|
+
} catch (error) {
|
|
347
|
+
console.error('Fetch Error:', error);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
// Example Usage:
|
|
352
|
+
// getDirectLinks("http://localhost:5000", "https://teraboxshare.com/s/1LNr3tyl5pI5KUM8BecGtyQ");
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
### 🐹 Go
|
|
356
|
+
|
|
357
|
+
```go
|
|
358
|
+
package main
|
|
359
|
+
|
|
360
|
+
import (
|
|
361
|
+
"encoding/json"
|
|
362
|
+
"fmt"
|
|
363
|
+
"net/http"
|
|
364
|
+
"net/url"
|
|
365
|
+
)
|
|
366
|
+
|
|
367
|
+
type FileItem struct {
|
|
368
|
+
Filename string `json:"filename"`
|
|
369
|
+
Size string `json:"size"`
|
|
370
|
+
DownloadLink string `json:"download_link"`
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
type APIResponse struct {
|
|
374
|
+
Status string `json:"status"`
|
|
375
|
+
Files []FileItem `json:"files"`
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
func getDirectLinks(gatewayURL, shareURL string) {
|
|
379
|
+
resp, err := http.Get(fmt.Sprintf("%s/api?url=%s", gatewayURL, url.QueryEscape(shareURL)))
|
|
380
|
+
if err != nil {
|
|
381
|
+
fmt.Println("Error:", err)
|
|
382
|
+
return
|
|
383
|
+
}
|
|
384
|
+
defer resp.Body.Close()
|
|
385
|
+
|
|
386
|
+
var result APIResponse
|
|
387
|
+
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
|
388
|
+
fmt.Println("Decode Error:", err)
|
|
389
|
+
return
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
if result.Status == "success" {
|
|
393
|
+
for _, file := range result.Files {
|
|
394
|
+
fmt.Printf("File: %s\nSize: %s\nDirect Link: %s\n\n", file.Filename, file.Size, file.DownloadLink)
|
|
395
|
+
}
|
|
396
|
+
} else {
|
|
397
|
+
fmt.Println("API returned an error status")
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
func main() {
|
|
402
|
+
// getDirectLinks("http://localhost:5000", "https://teraboxshare.com/s/1LNr3tyl5pI5KUM8BecGtyQ")
|
|
403
|
+
}
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
---
|
|
407
|
+
|
|
408
|
+
## Environment Variables
|
|
409
|
+
|
|
410
|
+
You can configure the API using environment variables in your `.env` file:
|
|
411
|
+
|
|
412
|
+
| Variable | Description | Default |
|
|
413
|
+
|----------|-------------|---------|
|
|
414
|
+
| `COOKIE_JSON` | TeraBox cookies (simple string or JSON) | - |
|
|
415
|
+
| `TERABOX_COOKIES_JSON` | Alternative: JSON string of cookies | - |
|
|
416
|
+
| `TERABOX_COOKIES_FILE` | Alternative: Path to cookies JSON file | - |
|
|
417
|
+
| `HOST` | Server host address | `0.0.0.0` |
|
|
418
|
+
| `PORT` | Server port | `5000` |
|
|
419
|
+
| `FLASK_DEBUG` | Enable Flask debug mode (`1` or `0`) | `0` |
|
|
420
|
+
| `RATE_LIMIT` | Max requests per window per IP | `30` |
|
|
421
|
+
| `RATE_WINDOW` | Rate limit window size in seconds | `60` |
|
|
422
|
+
| `CACHE_TTL` | Cache entry time-to-live in seconds | `60` |
|
|
423
|
+
| `CACHE_MAX_SIZE` | Maximum number of cached entries | `500` |
|
|
424
|
+
|
|
425
|
+
**Cookie Priority**:
|
|
426
|
+
1. `COOKIE_JSON` (from `.env`)
|
|
427
|
+
2. `TERABOX_COOKIES_JSON`
|
|
428
|
+
3. `TERABOX_COOKIES_FILE`
|
|
429
|
+
|
|
430
|
+
---
|
|
431
|
+
|
|
432
|
+
## Technical Implementation
|
|
433
|
+
|
|
434
|
+
### Unified Proxy Architecture
|
|
435
|
+
|
|
436
|
+
The TeraBox Gateway uses a **unified Cloudflare Worker proxy** for all TeraBox API interactions. This architecture provides:
|
|
437
|
+
|
|
438
|
+
- **Reduced Network Overhead**: 33-50% fewer HTTP requests (1-2 instead of 2-3 per request)
|
|
439
|
+
- **Server-Side Token Handling**: Automatic jsToken extraction handled by the proxy
|
|
440
|
+
- **Mode-Based Operations**: Three distinct modes for different use cases
|
|
441
|
+
|
|
442
|
+
#### Proxy Modes
|
|
443
|
+
|
|
444
|
+
The unified proxy supports 5 distinct modes, all accessible via the `/proxy` endpoint:
|
|
445
|
+
|
|
446
|
+
**1. `mode=resolve` (Recommended)**
|
|
447
|
+
- Automatically fetches the share page, extracts jsToken, and returns file metadata
|
|
448
|
+
- **Single HTTP call** for most common use cases
|
|
449
|
+
- Returns JSON with file list and metadata
|
|
450
|
+
- Best for production use
|
|
451
|
+
|
|
452
|
+
**2. `mode=page`**
|
|
453
|
+
- Returns raw HTML of TeraBox share page
|
|
454
|
+
- Useful for debugging and manual token extraction
|
|
455
|
+
- Returns HTML content
|
|
456
|
+
|
|
457
|
+
**3. `mode=api`**
|
|
458
|
+
- Direct API access when jsToken is already known
|
|
459
|
+
- Used for directory listings and advanced scenarios
|
|
460
|
+
- Returns JSON from TeraBox API
|
|
461
|
+
|
|
462
|
+
**4. `mode=stream`**
|
|
463
|
+
- Fetches M3U8 playlist and rewrites segment URLs
|
|
464
|
+
- Requires `surl` parameter (short URL ID)
|
|
465
|
+
- Optional `type` parameter for quality selection (default: M3U8_AUTO_360)
|
|
466
|
+
- Enables HLS video streaming in browsers and players
|
|
467
|
+
- Returns modified M3U8 playlist
|
|
468
|
+
|
|
469
|
+
**5. `mode=segment`**
|
|
470
|
+
- Proxies individual video segments (.ts, .m4s files)
|
|
471
|
+
- Automatically used by rewritten playlists
|
|
472
|
+
- Returns binary media data
|
|
473
|
+
|
|
474
|
+
### Request Flow
|
|
475
|
+
|
|
476
|
+
```
|
|
477
|
+
Client Request → Flask API → Unified Proxy (mode=resolve) → TeraBox API → Response
|
|
478
|
+
↓
|
|
479
|
+
(if directory)
|
|
480
|
+
↓
|
|
481
|
+
Unified Proxy (mode=api) → Directory Contents → Response
|
|
482
|
+
```
|
|
483
|
+
|
|
484
|
+
**Key Benefits**:
|
|
485
|
+
- ✅ Simplified codebase with no manual token parsing
|
|
486
|
+
- ✅ Faster response times due to fewer roundtrips
|
|
487
|
+
- ✅ More reliable token extraction (handled server-side)
|
|
488
|
+
- ✅ Better error handling and reporting
|
|
489
|
+
|
|
490
|
+
For detailed proxy documentation, see [tboxproxy_usage.md](tboxproxy_usage.md).
|
|
491
|
+
|
|
492
|
+
---
|
|
493
|
+
|
|
494
|
+
## Deployment
|
|
495
|
+
|
|
496
|
+
### Deploy to Vercel
|
|
497
|
+
|
|
498
|
+
The easiest way to deploy your own instance of the TeraBox Gateway API is to use Vercel.
|
|
499
|
+
|
|
500
|
+
Click the button below to clone and deploy this repository with one click:
|
|
501
|
+
|
|
502
|
+
[](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fsaahiyo%2Fterabox-gateway&env=COOKIE_JSON)
|
|
503
|
+
|
|
504
|
+
Alternatively, deploy manually using the Vercel CLI:
|
|
505
|
+
|
|
506
|
+
1. Install Vercel CLI (optional):
|
|
507
|
+
```bash
|
|
508
|
+
npm i -g vercel
|
|
509
|
+
```
|
|
510
|
+
|
|
511
|
+
2. Deploy:
|
|
512
|
+
```bash
|
|
513
|
+
vercel
|
|
514
|
+
```
|
|
515
|
+
|
|
516
|
+
3. Set environment variables in Vercel dashboard:
|
|
517
|
+
- Go to your project settings
|
|
518
|
+
- Add `COOKIE_JSON` with your `ndus` token value
|
|
519
|
+
|
|
520
|
+
The `vercel.json` configuration is already set up for you.
|
|
521
|
+
|
|
522
|
+
---
|
|
523
|
+
|
|
524
|
+
## Troubleshooting
|
|
525
|
+
|
|
526
|
+
### Common Issues
|
|
527
|
+
|
|
528
|
+
**Error 400141 - Verification Required**
|
|
529
|
+
- The link is password-protected
|
|
530
|
+
- Solution: Add `&pwd=PASSWORD` parameter to your request
|
|
531
|
+
|
|
532
|
+
**HTTP 5xx Errors**
|
|
533
|
+
- Your cookies may have expired
|
|
534
|
+
- Solution: Update your `ndus` cookie following the steps above
|
|
535
|
+
|
|
536
|
+
**No Direct Link Returned**
|
|
537
|
+
- Cookies are invalid or expired
|
|
538
|
+
- The share link itself has expired
|
|
539
|
+
- Solution: Refresh cookies or verify the link is still active
|
|
540
|
+
|
|
541
|
+
**"Cookies not loaded" Warning**
|
|
542
|
+
- `.env` file is missing or `COOKIE_JSON` is not set
|
|
543
|
+
- Solution: Create `.env` file and add your `ndus` token
|
|
544
|
+
|
|
545
|
+
**Authentication Failures**
|
|
546
|
+
- The `ndus` token is invalid or malformed
|
|
547
|
+
- Solution: Copy the token again from your browser, ensuring no extra spaces
|
|
548
|
+
|
|
549
|
+
---
|
|
550
|
+
|
|
551
|
+
## Development
|
|
552
|
+
|
|
553
|
+
### Running in Debug Mode
|
|
554
|
+
|
|
555
|
+
Set `FLASK_DEBUG=1` in your `.env` file:
|
|
556
|
+
|
|
557
|
+
```env
|
|
558
|
+
FLASK_DEBUG=1
|
|
559
|
+
```
|
|
560
|
+
|
|
561
|
+
This enables:
|
|
562
|
+
- Auto-reload on code changes
|
|
563
|
+
- Detailed error pages
|
|
564
|
+
- Interactive debugger
|
|
565
|
+
|
|
566
|
+
### Logging
|
|
567
|
+
|
|
568
|
+
The API uses Python's `logging` module with INFO level by default. Logs include:
|
|
569
|
+
- Request URLs and parameters
|
|
570
|
+
- Cookie loading status (format detection)
|
|
571
|
+
- API responses and errors
|
|
572
|
+
- Share page tokens and authentication status
|
|
573
|
+
|
|
574
|
+
---
|
|
575
|
+
|
|
576
|
+
## License
|
|
577
|
+
|
|
578
|
+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
|
|
579
|
+
|
|
580
|
+
---
|
|
581
|
+
|
|
582
|
+
## Contributing
|
|
583
|
+
|
|
584
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
585
|
+
|
|
586
|
+
---
|
|
587
|
+
|
|
588
|
+
## Contact
|
|
589
|
+
|
|
590
|
+
For questions or support, contact [@Saahiyo](https://github.com/Saahiyo)
|
|
591
|
+
|
|
592
|
+
---
|
|
593
|
+
|
|
594
|
+
## Acknowledgments
|
|
595
|
+
|
|
596
|
+
- Built with Flask 3.x (native async) and aiohttp for efficient async operations
|
|
597
|
+
- Unified Cloudflare Worker proxy for optimized API access
|
|
598
|
+
- Designed for seamless Vercel deployment
|
|
599
|
+
- Supports multiple TeraBox domains and share URL formats
|