Skip to content

Commit

Permalink
added support for segments
Browse files Browse the repository at this point in the history
  • Loading branch information
Toasty360 committed Nov 26, 2024
1 parent 68cf49b commit f68d022
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 43 deletions.
51 changes: 14 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,63 +1,40 @@
# HLS Stream Proxy

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.
This application serves as a proxy for HLS streams, enabling secure access to media content.

## Usage Instructions
---

To use this application, please follow the instructions below.
## Proxy Endpoint

### Proxy Endpoint

The proxy endpoint can be accessed using the following format:
Use the following format to access the proxy:

```
/proxy?url=<encoded_m3u8_url>&headers=<encoded_headers>
```

- **`encoded_m3u8_url`**: Base64-encoded URL of the M3U8 file you want to stream.
- **`encoded_headers`**: Base64-encoded JSON string of any custom headers needed for the request.

### Why base64Encoding?
- **`url`**: Base64-encoded M3U8 URL.
- **`headers`**: (Optional) Base64-encoded JSON string for custom headers.

Because it looks cool
---

### Encoding the M3U8 URL
## Encoding Instructions

To encode the M3U8 URL, use the `base64` encoding method. For example, using JavaScript:
### Encode M3U8 URL:

```javascript
const encodedUrl = btoa('http://example.com/stream.m3u8');
btoa('http://example.com/stream.m3u8');
```

### Encoding the Headers

To encode the headers, you can use the following JavaScript code:
### Encode Headers (if needed):

```javascript
const headers = JSON.stringify({ Authorization: 'Bearer token' });
const encodedHeaders = btoa(headers);
btoa(JSON.stringify({ Referrer: 'https://anitaku.bz' }));
```

Ensure that the headers are in valid JSON format before encoding.
---

## Example

Here’s an example of how to construct a request to the proxy:
## Example Request

```
/proxy?url=aHR0cDovL2V4YW1wbGUuY29tL3N0cmVhbS5tM3U4&headers=eyJBdXRob3JpemF0aW9uIjoiQmVhcmVyIHRva2VuIn0=
```

## Deploy Your Instance

You can deploy your own instance of this application on Cloudflare Workers by clicking the button below:

[![Deploy to Cloudflare Workers](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/Toasty360/Roxy)

## Contributing

If you would like to contribute to this project, please fork the repository and submit a pull request.

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
29 changes: 23 additions & 6 deletions src/proxy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
const m3u8ContentTypes = ['application/vnd.apple.mpegurl', 'application/x-mpegurl', 'audio/x-mpegurl', 'audio/mpegurl', 'video/x-mpegurl'];
const videoContentTypes = ['video/mp4', 'video/webm', 'video/ogg', 'application/mp4', 'video/x-m4v', ...m3u8ContentTypes];
const m3u8ContentTypes = [
'application/vnd.apple.mpegurl', // Standard HLS playlist
'application/x-mpegurl', // Common alias
'audio/x-mpegurl', // Audio HLS playlists
'audio/mpegurl', // Less common
'video/x-mpegurl', // Video HLS playlists
'application/mpegurl', // Alternate generic HLS
'application/x-hls', // Explicit HLS content type
'application/x-apple-hls', // Apple-specific HLS type
];

const videoContentTypes = [
'video/mp4',
'video/webm',
'video/ogg',
'video/quicktime',
'video/MP2T', // Transport Stream (HLS segments)
'application/mp4',
'video/x-m4v',
'application/octet-stream', // Generic binary stream
...m3u8ContentTypes, // Include HLS playlist MIME types
];

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

const cacheKey = new Request(url.toString(), request);

try {
let response = await cache.match(cacheKey);
if (response) return response;
Expand All @@ -65,7 +84,7 @@ async function proxy(request) {
const headersBase64 = urlParams.get('headers');

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

const baseUrl = new URL(mediaUrl);
const basePath = `${baseUrl.protocol}//${baseUrl.host}${baseUrl.pathname.substring(0, baseUrl.pathname.lastIndexOf('/') + 1)}`;

response = await fetch(mediaUrl, {
headers: {
...Object.fromEntries(decodedHeaders.entries()),
Expand Down Expand Up @@ -136,7 +154,6 @@ async function proxy(request) {
: match.startsWith('/')
? `${baseUrl.protocol}//${baseUrl.host}${match}`
: `${basePath}${match}`;

return `${new URL(request.url).origin}/proxy?url=${encodeURIComponent(btoa(fullUrl))}&headers=${encodeURIComponent(headersBase64)}`;
});

Expand Down

0 comments on commit f68d022

Please # to comment.