Skip to content

Commit f68d022

Browse files
committed
added support for segments
1 parent 68cf49b commit f68d022

File tree

2 files changed

+37
-43
lines changed

2 files changed

+37
-43
lines changed

README.md

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,40 @@
11
# HLS Stream Proxy
22

3-
This application serves as a proxy for HLS streams. It allows you to securely access and stream media content by passing through a proxy endpoint.
3+
This application serves as a proxy for HLS streams, enabling secure access to media content.
44

5-
## Usage Instructions
5+
---
66

7-
To use this application, please follow the instructions below.
7+
## Proxy Endpoint
88

9-
### Proxy Endpoint
10-
11-
The proxy endpoint can be accessed using the following format:
9+
Use the following format to access the proxy:
1210

1311
```
1412
/proxy?url=<encoded_m3u8_url>&headers=<encoded_headers>
1513
```
1614

17-
- **`encoded_m3u8_url`**: Base64-encoded URL of the M3U8 file you want to stream.
18-
- **`encoded_headers`**: Base64-encoded JSON string of any custom headers needed for the request.
19-
20-
### Why base64Encoding?
15+
- **`url`**: Base64-encoded M3U8 URL.
16+
- **`headers`**: (Optional) Base64-encoded JSON string for custom headers.
2117

22-
Because it looks cool
18+
---
2319

24-
### Encoding the M3U8 URL
20+
## Encoding Instructions
2521

26-
To encode the M3U8 URL, use the `base64` encoding method. For example, using JavaScript:
22+
### Encode M3U8 URL:
2723

2824
```javascript
29-
const encodedUrl = btoa('http://example.com/stream.m3u8');
25+
btoa('http://example.com/stream.m3u8');
3026
```
3127

32-
### Encoding the Headers
33-
34-
To encode the headers, you can use the following JavaScript code:
28+
### Encode Headers (if needed):
3529

3630
```javascript
37-
const headers = JSON.stringify({ Authorization: 'Bearer token' });
38-
const encodedHeaders = btoa(headers);
31+
btoa(JSON.stringify({ Referrer: 'https://anitaku.bz' }));
3932
```
4033

41-
Ensure that the headers are in valid JSON format before encoding.
34+
---
4235

43-
## Example
44-
45-
Here’s an example of how to construct a request to the proxy:
36+
## Example Request
4637

4738
```
4839
/proxy?url=aHR0cDovL2V4YW1wbGUuY29tL3N0cmVhbS5tM3U4&headers=eyJBdXRob3JpemF0aW9uIjoiQmVhcmVyIHRva2VuIn0=
4940
```
50-
51-
## Deploy Your Instance
52-
53-
You can deploy your own instance of this application on Cloudflare Workers by clicking the button below:
54-
55-
[![Deploy to Cloudflare Workers](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/Toasty360/Roxy)
56-
57-
## Contributing
58-
59-
If you would like to contribute to this project, please fork the repository and submit a pull request.
60-
61-
## License
62-
63-
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

src/proxy.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
1-
const m3u8ContentTypes = ['application/vnd.apple.mpegurl', 'application/x-mpegurl', 'audio/x-mpegurl', 'audio/mpegurl', 'video/x-mpegurl'];
2-
const videoContentTypes = ['video/mp4', 'video/webm', 'video/ogg', 'application/mp4', 'video/x-m4v', ...m3u8ContentTypes];
1+
const m3u8ContentTypes = [
2+
'application/vnd.apple.mpegurl', // Standard HLS playlist
3+
'application/x-mpegurl', // Common alias
4+
'audio/x-mpegurl', // Audio HLS playlists
5+
'audio/mpegurl', // Less common
6+
'video/x-mpegurl', // Video HLS playlists
7+
'application/mpegurl', // Alternate generic HLS
8+
'application/x-hls', // Explicit HLS content type
9+
'application/x-apple-hls', // Apple-specific HLS type
10+
];
11+
12+
const videoContentTypes = [
13+
'video/mp4',
14+
'video/webm',
15+
'video/ogg',
16+
'video/quicktime',
17+
'video/MP2T', // Transport Stream (HLS segments)
18+
'application/mp4',
19+
'video/x-m4v',
20+
'application/octet-stream', // Generic binary stream
21+
...m3u8ContentTypes, // Include HLS playlist MIME types
22+
];
323

424
const CACHE_CONTROL_SETTINGS = {
525
MASTER: 'public, max-age=30, s-maxage=30',
@@ -55,7 +75,6 @@ async function proxy(request) {
5575
}
5676

5777
const cacheKey = new Request(url.toString(), request);
58-
5978
try {
6079
let response = await cache.match(cacheKey);
6180
if (response) return response;
@@ -65,7 +84,7 @@ async function proxy(request) {
6584
const headersBase64 = urlParams.get('headers');
6685

6786
if (!encodedUrl) {
68-
return new Response('Both "url" and "headers" query parameters are required', {
87+
return new Response('"url" query parameters are required', {
6988
status: 400,
7089
headers: {
7190
'Cache-Control': 'no-store',
@@ -89,7 +108,6 @@ async function proxy(request) {
89108

90109
const baseUrl = new URL(mediaUrl);
91110
const basePath = `${baseUrl.protocol}//${baseUrl.host}${baseUrl.pathname.substring(0, baseUrl.pathname.lastIndexOf('/') + 1)}`;
92-
93111
response = await fetch(mediaUrl, {
94112
headers: {
95113
...Object.fromEntries(decodedHeaders.entries()),
@@ -136,7 +154,6 @@ async function proxy(request) {
136154
: match.startsWith('/')
137155
? `${baseUrl.protocol}//${baseUrl.host}${match}`
138156
: `${basePath}${match}`;
139-
140157
return `${new URL(request.url).origin}/proxy?url=${encodeURIComponent(btoa(fullUrl))}&headers=${encodeURIComponent(headersBase64)}`;
141158
});
142159

0 commit comments

Comments
 (0)