@obitosouls/tizenbrew-netmirror 0.0.0-stage → 1.0.0
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.
- package/README.md +79 -2
- package/package.json +42 -4
- package/service/index.js +498 -0
- package/web/config.xml +21 -0
- package/web/css/app.css +540 -0
- package/web/icon.png +0 -0
- package/web/index.html +258 -0
- package/web/js/api.js +685 -0
- package/web/js/app.js +3012 -0
- package/web/js/boot.js +230 -0
- package/web/js/hls.min.js +2 -0
package/README.md
CHANGED
|
@@ -1,3 +1,80 @@
|
|
|
1
|
-
#
|
|
1
|
+
# NetMirror TV — TizenBrew Module
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
NetMirror TV client packaged for Samsung Tizen TV running **TizenBrew**.
|
|
4
|
+
Includes a local background Node.js proxy service that bypasses Tizen WebKit's header and CORS restrictions (`User-Agent`, `Referer`, and `file://` origin limitations).
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## 🚀 Architecture
|
|
9
|
+
|
|
10
|
+
```
|
|
11
|
+
┌─────────────────────────────────────────────────────────────┐
|
|
12
|
+
│ Samsung Tizen TV / WebKit │
|
|
13
|
+
│ http://127.0.0.1:8787/ (or http://<PC_IP>:8787/) │
|
|
14
|
+
│ │
|
|
15
|
+
│ UI (index.html, app.js, api.js, hls.min.js) │
|
|
16
|
+
└───────────────────────┬─────────────────────────▲───────────┘
|
|
17
|
+
│ │
|
|
18
|
+
│ HTTP GET /proxy/api │ Permissive CORS
|
|
19
|
+
│ HTTP GET /proxy/media │
|
|
20
|
+
▼ │
|
|
21
|
+
┌─────────────────────────────────────────────────┴───────────┐
|
|
22
|
+
│ TizenBrew Background Service (service/index.js) │
|
|
23
|
+
│ Node.js proxy on port 8787 │
|
|
24
|
+
│ │
|
|
25
|
+
│ • Attaches /OS.Gatu v1.0 on otp.php │
|
|
26
|
+
│ • Attaches /OS.GatuNewTV v1.0 on other endpoints │
|
|
27
|
+
│ • Attaches Referer on stream requests │
|
|
28
|
+
│ • Rewrites .m3u8 playlists & proxies segments │
|
|
29
|
+
│ • Sniffs disguised MPEG-TS packets & sets video/mp2t │
|
|
30
|
+
│ • Persistent HTTP/HTTPS connection pooling (keep-alive) │
|
|
31
|
+
└───────────────────────┬─────────────────────────────────────┘
|
|
32
|
+
│
|
|
33
|
+
▼
|
|
34
|
+
┌─────────────────────────────────────────────────────────────┐
|
|
35
|
+
│ NetMirror Backend & CDN Servers │
|
|
36
|
+
└─────────────────────────────────────────────────────────────┘
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
## 📺 Installation in TizenBrew (Samsung Tizen TV)
|
|
42
|
+
|
|
43
|
+
### Prerequisites
|
|
44
|
+
1. **TizenBrew** installed and running on your Samsung TV.
|
|
45
|
+
2. TV and PC connected to the same local Wi-Fi / LAN network.
|
|
46
|
+
|
|
47
|
+
### Method 1: Install Directly via TizenBrew Module Manager (from NPM)
|
|
48
|
+
1. Open **TizenBrew** on your Samsung TV.
|
|
49
|
+
2. Navigate to **Manage Modules** > **Add Module** (or **Install from NPM**).
|
|
50
|
+
3. Type the package name:
|
|
51
|
+
```text
|
|
52
|
+
tizenbrew-netmirror
|
|
53
|
+
```
|
|
54
|
+
4. Confirm installation. TizenBrew will fetch the package from NPM, install it to the TV's storage, launch the background proxy service (`service/index.js`), and open the UI (`web/index.html`).
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
### Method 2: Load via Local Development Server (LAN)
|
|
59
|
+
If you want to test live without publishing or downloading each time:
|
|
60
|
+
1. On your PC, navigate to the module directory and start the service:
|
|
61
|
+
```bash
|
|
62
|
+
cd "F:\Net Mirror\tizenbrew-netmirror"
|
|
63
|
+
npm start
|
|
64
|
+
```
|
|
65
|
+
2. Find your PC's local IP address (e.g. `192.168.1.100` via `ipconfig` on Windows).
|
|
66
|
+
3. On your Samsung TV in TizenBrew, choose **Load from URL**:
|
|
67
|
+
```text
|
|
68
|
+
http://192.168.1.100:8787
|
|
69
|
+
```
|
|
70
|
+
4. The full NetMirror TV app will load directly with live streaming and proxying active.
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
## 🛠 Features Included
|
|
75
|
+
- **Multi-Season Web Series Support:** Choose any season (Season 1, 2, etc.) with real episode titles, synopses, and durations.
|
|
76
|
+
- **Auto-Pagination:** Loads all episodes beyond page 1 without truncation.
|
|
77
|
+
- **Buffer Optimization:** Persistent keep-alive TCP connections and 60s/120s HLS buffers eliminate 5-second stuttering.
|
|
78
|
+
- **Provider Switching:** Easily switch between Netflix (`nf`), Prime Video (`pv`), and Disney+ Hotstar (`hs`).
|
|
79
|
+
- **On-Screen Diagnostics:** Press **Debug** in the top navigation or press key **0** on the remote to inspect real-time connection status and API responses.
|
|
80
|
+
- **Remote Control Hotkeys:** Full D-pad navigation, `MediaPlay`, `MediaPause`, `MediaFastForward`, and `MediaRewind` support.
|
package/package.json
CHANGED
|
@@ -1,6 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@obitosouls/tizenbrew-netmirror",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Full-featured NetMirror TV application for Samsung Tizen TVs with background HLS streaming proxy and multi-season support",
|
|
5
|
+
"packageType": "app",
|
|
6
|
+
"appName": "NetMirror TV",
|
|
7
|
+
"appPath": "web/index.html",
|
|
8
|
+
"serviceFile": "service/index.js",
|
|
9
|
+
"icon": "web/icon.png",
|
|
10
|
+
"evaluateScriptOnDocumentStart": false,
|
|
11
|
+
"keys": [
|
|
12
|
+
"MediaPlay",
|
|
13
|
+
"MediaPause",
|
|
14
|
+
"MediaPlayPause",
|
|
15
|
+
"MediaRewind",
|
|
16
|
+
"MediaFastForward",
|
|
17
|
+
"MediaStop",
|
|
18
|
+
"ColorF0Red",
|
|
19
|
+
"ColorF1Green",
|
|
20
|
+
"ColorF2Yellow",
|
|
21
|
+
"ColorF3Blue"
|
|
22
|
+
],
|
|
23
|
+
"main": "service/index.js",
|
|
24
|
+
"scripts": {
|
|
25
|
+
"start": "node service/index.js",
|
|
26
|
+
"test": "node --check service/index.js && node --check web/js/api.js && node --check web/js/app.js"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"package.json",
|
|
30
|
+
"README.md",
|
|
31
|
+
"service",
|
|
32
|
+
"web"
|
|
33
|
+
],
|
|
34
|
+
"keywords": [
|
|
35
|
+
"tizenbrew",
|
|
36
|
+
"tizenbrew-module",
|
|
37
|
+
"tizen",
|
|
38
|
+
"samsung-tv",
|
|
39
|
+
"smart-tv",
|
|
40
|
+
"netmirror"
|
|
41
|
+
],
|
|
42
|
+
"author": "obitosouls",
|
|
43
|
+
"license": "MIT"
|
|
44
|
+
}
|
package/service/index.js
ADDED
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TizenBrew Background Service for NetMirror TV
|
|
3
|
+
*
|
|
4
|
+
* Lightweight, zero-dependency Node.js HTTP server:
|
|
5
|
+
* 1. Serves static web UI files from ../web/
|
|
6
|
+
* 2. API Proxy (/proxy/api): executes GET requests with exact headers & unrestricted User-Agent
|
|
7
|
+
* 3. Media & HLS Proxy (/proxy/media): attaches Referer & UA, rewrites .m3u8 playlists,
|
|
8
|
+
* sniffs disguised MPEG-TS video (.jpg) and audio (.js) segments (0x47 sync byte)
|
|
9
|
+
* and overrides Content-Type to 'video/mp2t' so TV media engines decode properly.
|
|
10
|
+
* 4. Diagnostics & Debug Bridge (/proxy/debug): allows TV app to export debug logs,
|
|
11
|
+
* and allows developers/users to read/copy them from any phone/PC on the LAN.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const http = require('http');
|
|
15
|
+
const https = require('https');
|
|
16
|
+
const url = require('url');
|
|
17
|
+
const fs = require('fs');
|
|
18
|
+
const path = require('path');
|
|
19
|
+
const zlib = require('zlib');
|
|
20
|
+
|
|
21
|
+
const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 8787;
|
|
22
|
+
const WEB_DIR = path.resolve(__dirname, '..', 'web');
|
|
23
|
+
|
|
24
|
+
const UA_DEFAULT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:136.0) Gecko/20100101 Firefox/136.0 /OS.GatuNewTV v1.0';
|
|
25
|
+
const UA_OTP = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:136.0) Gecko/20100101 Firefox/136.0 /OS.Gatu v1.0';
|
|
26
|
+
const XRW = 'NetmirrorNewTV v1.0';
|
|
27
|
+
const DEFAULT_REFERER = 'https://net52.cc';
|
|
28
|
+
|
|
29
|
+
const MIME = {
|
|
30
|
+
'.html': 'text/html; charset=utf-8',
|
|
31
|
+
'.js': 'application/javascript; charset=utf-8',
|
|
32
|
+
'.css': 'text/css; charset=utf-8',
|
|
33
|
+
'.json': 'application/json; charset=utf-8',
|
|
34
|
+
'.png': 'image/png',
|
|
35
|
+
'.jpg': 'image/jpeg',
|
|
36
|
+
'.jpeg': 'image/jpeg',
|
|
37
|
+
'.gif': 'image/gif',
|
|
38
|
+
'.svg': 'image/svg+xml',
|
|
39
|
+
'.ico': 'image/x-icon',
|
|
40
|
+
'.webp': 'image/webp',
|
|
41
|
+
'.m3u8': 'application/vnd.apple.mpegurl; charset=utf-8',
|
|
42
|
+
'.ts': 'video/mp2t',
|
|
43
|
+
'.mp4': 'video/mp4',
|
|
44
|
+
'.xml': 'application/xml; charset=utf-8',
|
|
45
|
+
'.woff2': 'font/woff2',
|
|
46
|
+
'.woff': 'font/woff',
|
|
47
|
+
'.ttf': 'font/ttf'
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// In-memory debug storage for TV export
|
|
51
|
+
|
|
52
|
+
const httpsAgent = new https.Agent({
|
|
53
|
+
keepAlive: true,
|
|
54
|
+
keepAliveMsecs: 60000,
|
|
55
|
+
maxSockets: 64,
|
|
56
|
+
maxFreeSockets: 32,
|
|
57
|
+
timeout: 60000,
|
|
58
|
+
rejectUnauthorized: false
|
|
59
|
+
});
|
|
60
|
+
const httpAgent = new http.Agent({
|
|
61
|
+
keepAlive: true,
|
|
62
|
+
keepAliveMsecs: 60000,
|
|
63
|
+
maxSockets: 64,
|
|
64
|
+
maxFreeSockets: 32,
|
|
65
|
+
timeout: 60000
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
var clientDebugLog = 'No client debug logs captured yet.';
|
|
69
|
+
var clientDebugTime = new Date().toISOString();
|
|
70
|
+
|
|
71
|
+
function addCors(res) {
|
|
72
|
+
if (!res || res.headersSent) return;
|
|
73
|
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
74
|
+
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, HEAD');
|
|
75
|
+
res.setHeader('Access-Control-Allow-Headers', '*');
|
|
76
|
+
res.setHeader('Access-Control-Expose-Headers', '*');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function handleOptions(req, res) {
|
|
80
|
+
addCors(res);
|
|
81
|
+
res.writeHead(204);
|
|
82
|
+
res.end();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function serveStatic(req, res, pathname) {
|
|
86
|
+
var clean = pathname.replace(/^\/+/g, '') || 'index.html';
|
|
87
|
+
var filePath = path.join(WEB_DIR, clean);
|
|
88
|
+
|
|
89
|
+
// Security check: stay within WEB_DIR
|
|
90
|
+
if (filePath.indexOf(WEB_DIR) !== 0) {
|
|
91
|
+
addCors(res);
|
|
92
|
+
res.writeHead(403);
|
|
93
|
+
res.end('Forbidden');
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
fs.stat(filePath, function (err, stat) {
|
|
98
|
+
if (err) {
|
|
99
|
+
addCors(res);
|
|
100
|
+
res.writeHead(404);
|
|
101
|
+
res.end('File Not Found');
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
if (stat.isDirectory()) {
|
|
105
|
+
filePath = path.join(filePath, 'index.html');
|
|
106
|
+
}
|
|
107
|
+
var ext = path.extname(filePath).toLowerCase();
|
|
108
|
+
var mime = MIME[ext] || 'application/octet-stream';
|
|
109
|
+
addCors(res);
|
|
110
|
+
res.setHeader('Content-Type', mime);
|
|
111
|
+
res.setHeader('Content-Length', stat.size);
|
|
112
|
+
fs.createReadStream(filePath).pipe(res);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function httpFetch(targetUrl, headers, maxRedirects, callback) {
|
|
117
|
+
if (maxRedirects === undefined) maxRedirects = 5;
|
|
118
|
+
var called = false;
|
|
119
|
+
function done(err, upstream) {
|
|
120
|
+
if (called) return;
|
|
121
|
+
called = true;
|
|
122
|
+
callback(err, upstream);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
var parsed;
|
|
126
|
+
try {
|
|
127
|
+
parsed = new URL(targetUrl);
|
|
128
|
+
} catch (e) {
|
|
129
|
+
done(e, null);
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
var lib = parsed.protocol === 'https:' ? https : http;
|
|
133
|
+
|
|
134
|
+
var opts = {
|
|
135
|
+
protocol: parsed.protocol,
|
|
136
|
+
hostname: parsed.hostname,
|
|
137
|
+
port: parsed.port || (parsed.protocol === 'https:' ? 443 : 80),
|
|
138
|
+
path: parsed.pathname + (parsed.search || ''),
|
|
139
|
+
method: 'GET',
|
|
140
|
+
headers: headers,
|
|
141
|
+
rejectUnauthorized: false,
|
|
142
|
+
agent: parsed.protocol === 'https:' ? httpsAgent : httpAgent
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
var req = lib.request(opts, function (upstream) {
|
|
146
|
+
if ((upstream.statusCode === 301 || upstream.statusCode === 302 || upstream.statusCode === 307 || upstream.statusCode === 308) && upstream.headers.location) {
|
|
147
|
+
if (maxRedirects <= 0) {
|
|
148
|
+
done(new Error('Too many redirects'), null);
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
var redirectUrl;
|
|
152
|
+
try {
|
|
153
|
+
redirectUrl = new URL(upstream.headers.location, targetUrl).href;
|
|
154
|
+
} catch (e2) {
|
|
155
|
+
redirectUrl = url.resolve(targetUrl, upstream.headers.location);
|
|
156
|
+
}
|
|
157
|
+
httpFetch(redirectUrl, headers, maxRedirects - 1, done);
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
done(null, upstream);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
req.on('error', function (err) { done(err, null); });
|
|
164
|
+
req.setTimeout(25000, function () { req.destroy(new Error('Upstream timeout')); });
|
|
165
|
+
req.end();
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function handleApiProxy(req, res, query) {
|
|
169
|
+
var targetUrl = query.url || req.headers['x-target-url'];
|
|
170
|
+
if (!targetUrl) {
|
|
171
|
+
addCors(res);
|
|
172
|
+
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
173
|
+
res.end(JSON.stringify({ error: 'Missing target url parameter' }));
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
var isOtp = targetUrl.indexOf('/newtv/otp.php') !== -1 || query.ua === 'otp';
|
|
178
|
+
var ua = query.ua || (isOtp ? UA_OTP : UA_DEFAULT);
|
|
179
|
+
|
|
180
|
+
var fwdHeaders = {
|
|
181
|
+
'User-Agent': ua,
|
|
182
|
+
'Accept': '*/*',
|
|
183
|
+
'Cache-Control': 'no-cache, no-store, must-revalidate',
|
|
184
|
+
'Pragma': 'no-cache',
|
|
185
|
+
'Expires': '0'
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
if (!isOtp) {
|
|
189
|
+
fwdHeaders['X-Requested-With'] = XRW;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// Forward specific custom NetMirror headers
|
|
193
|
+
var passKeys = ['ott', 'usertoken', 'otp', 'page', 'lastep', 'videoid', 'deleteid', 'recentplay', 'watchlist'];
|
|
194
|
+
passKeys.forEach(function (k) {
|
|
195
|
+
if (req.headers[k]) {
|
|
196
|
+
var headerName = k.charAt(0).toUpperCase() + k.slice(1);
|
|
197
|
+
if (k === 'usertoken') headerName = 'Usertoken';
|
|
198
|
+
if (k === 'deleteid') headerName = 'Deleteid';
|
|
199
|
+
if (k === 'videoid') headerName = 'Videoid';
|
|
200
|
+
if (k === 'recentplay') headerName = 'Recentplay';
|
|
201
|
+
fwdHeaders[headerName] = req.headers[k];
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
// Query parameter overrides
|
|
206
|
+
if (query.ott) fwdHeaders['Ott'] = query.ott;
|
|
207
|
+
if (query.usertoken) fwdHeaders['Usertoken'] = query.usertoken;
|
|
208
|
+
if (query.page) fwdHeaders['Page'] = query.page;
|
|
209
|
+
|
|
210
|
+
// Normalize Ott value: server only accepts 'nf', 'pv', 'hs'
|
|
211
|
+
var ottVal = String(fwdHeaders['Ott'] || '').trim().toLowerCase();
|
|
212
|
+
if (ottVal === 'netflix') ottVal = 'nf';
|
|
213
|
+
else if (ottVal === 'prime' || ottVal === 'amazon') ottVal = 'pv';
|
|
214
|
+
else if (ottVal === 'hotstar' || ottVal === 'disney') ottVal = 'hs';
|
|
215
|
+
else if (!ottVal || ottVal === 'undefined' || ottVal === 'null') ottVal = 'nf';
|
|
216
|
+
fwdHeaders['Ott'] = ottVal;
|
|
217
|
+
|
|
218
|
+
httpFetch(targetUrl, fwdHeaders, 5, function (err, upstream) {
|
|
219
|
+
if (res.headersSent || res.writableEnded) return;
|
|
220
|
+
if (err) {
|
|
221
|
+
try {
|
|
222
|
+
addCors(res);
|
|
223
|
+
res.writeHead(502, { 'Content-Type': 'application/json' });
|
|
224
|
+
res.end(JSON.stringify({ error: 'Proxy request failed: ' + err.message }));
|
|
225
|
+
} catch (e) {}
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
try {
|
|
230
|
+
addCors(res);
|
|
231
|
+
res.setHeader('Content-Type', upstream.headers['content-type'] || 'text/plain; charset=utf-8');
|
|
232
|
+
res.writeHead(upstream.statusCode);
|
|
233
|
+
upstream.pipe(res);
|
|
234
|
+
} catch (e2) {}
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Rewrite master and child HLS playlists so that all keys, audio tracks, subtitle tracks,
|
|
240
|
+
* and media segments route through the /proxy/media endpoint with referer preserved.
|
|
241
|
+
*/
|
|
242
|
+
function rewriteM3u8(body, playlistUrl, referer, ua, host) {
|
|
243
|
+
var lines = body.split(/\r?\n/);
|
|
244
|
+
var out = [];
|
|
245
|
+
var baseProxy = 'http://' + (host || '127.0.0.1:8787') + '/proxy/media';
|
|
246
|
+
|
|
247
|
+
for (var i = 0; i < lines.length; i++) {
|
|
248
|
+
var line = lines[i];
|
|
249
|
+
var trimmed = line.trim();
|
|
250
|
+
|
|
251
|
+
if (!trimmed) {
|
|
252
|
+
out.push(line);
|
|
253
|
+
continue;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// Rewrite DRM / encryption key URI: #EXT-X-KEY:METHOD=...,URI="url"
|
|
257
|
+
if (trimmed.indexOf('#EXT-X-KEY:') === 0) {
|
|
258
|
+
var matchKey = trimmed.match(/URI=["']([^"']+)["']/);
|
|
259
|
+
if (matchKey) {
|
|
260
|
+
var keyUrl = url.resolve(playlistUrl, matchKey[1]);
|
|
261
|
+
var proxiedKey = baseProxy + '?url=' + encodeURIComponent(keyUrl) +
|
|
262
|
+
(referer ? '&referer=' + encodeURIComponent(referer) : '') +
|
|
263
|
+
(ua ? '&ua=' + encodeURIComponent(ua) : '');
|
|
264
|
+
line = line.replace(matchKey[0], 'URI="' + proxiedKey + '"');
|
|
265
|
+
}
|
|
266
|
+
out.push(line);
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// Rewrite Audio / Subtitle track URI: #EXT-X-MEDIA:TYPE=AUDIO|SUBTITLES,...,URI="url"
|
|
271
|
+
if (trimmed.indexOf('#EXT-X-MEDIA:') === 0) {
|
|
272
|
+
var matchMedia = trimmed.match(/URI=["']([^"']+)["']/);
|
|
273
|
+
if (matchMedia) {
|
|
274
|
+
var mediaUrl = url.resolve(playlistUrl, matchMedia[1]);
|
|
275
|
+
var proxiedMedia = baseProxy + '?url=' + encodeURIComponent(mediaUrl) +
|
|
276
|
+
(referer ? '&referer=' + encodeURIComponent(referer) : '') +
|
|
277
|
+
(ua ? '&ua=' + encodeURIComponent(ua) : '');
|
|
278
|
+
line = line.replace(matchMedia[0], 'URI="' + proxiedMedia + '"');
|
|
279
|
+
}
|
|
280
|
+
out.push(line);
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// Other HLS tags
|
|
285
|
+
if (trimmed.charAt(0) === '#') {
|
|
286
|
+
out.push(line);
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// Media segment or variant playlist URI
|
|
291
|
+
var absUrl = url.resolve(playlistUrl, trimmed);
|
|
292
|
+
var proxied = baseProxy + '?url=' + encodeURIComponent(absUrl) +
|
|
293
|
+
(referer ? '&referer=' + encodeURIComponent(referer) : '') +
|
|
294
|
+
(ua ? '&ua=' + encodeURIComponent(ua) : '');
|
|
295
|
+
out.push(proxied);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
return out.join('\n');
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function handleMediaProxy(req, res, query) {
|
|
302
|
+
var targetUrl = query.url;
|
|
303
|
+
if (!targetUrl) {
|
|
304
|
+
addCors(res);
|
|
305
|
+
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
306
|
+
res.end(JSON.stringify({ error: 'Missing target url parameter' }));
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
var referer = query.referer || DEFAULT_REFERER;
|
|
311
|
+
var ua = query.ua || UA_DEFAULT;
|
|
312
|
+
|
|
313
|
+
var fwdHeaders = {
|
|
314
|
+
'User-Agent': ua,
|
|
315
|
+
'Accept': '*/*'
|
|
316
|
+
};
|
|
317
|
+
if (referer) fwdHeaders['Referer'] = referer;
|
|
318
|
+
if (req.headers['range']) fwdHeaders['Range'] = req.headers['range'];
|
|
319
|
+
|
|
320
|
+
httpFetch(targetUrl, fwdHeaders, 5, function (err, upstream) {
|
|
321
|
+
if (res.headersSent || res.writableEnded) return;
|
|
322
|
+
if (err) {
|
|
323
|
+
try {
|
|
324
|
+
addCors(res);
|
|
325
|
+
res.writeHead(502, { 'Content-Type': 'application/json' });
|
|
326
|
+
res.end(JSON.stringify({ error: 'Media proxy failed: ' + err.message }));
|
|
327
|
+
} catch (e) {}
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
var ctype = upstream.headers['content-type'] || '';
|
|
332
|
+
var isM3u8 = targetUrl.indexOf('.m3u8') !== -1 || ctype.indexOf('mpegurl') !== -1;
|
|
333
|
+
|
|
334
|
+
addCors(res);
|
|
335
|
+
|
|
336
|
+
if (isM3u8) {
|
|
337
|
+
var chunks = [];
|
|
338
|
+
var enc = upstream.headers['content-encoding'];
|
|
339
|
+
var stream = upstream;
|
|
340
|
+
if (enc === 'gzip') stream = upstream.pipe(zlib.createGunzip());
|
|
341
|
+
else if (enc === 'deflate') stream = upstream.pipe(zlib.createInflate());
|
|
342
|
+
|
|
343
|
+
stream.on('data', function (c) { chunks.push(c); });
|
|
344
|
+
stream.on('end', function () {
|
|
345
|
+
var raw = Buffer.concat(chunks).toString('utf8');
|
|
346
|
+
var host = req.headers.host || ('127.0.0.1:' + DEFAULT_PORT);
|
|
347
|
+
var rewritten = rewriteM3u8(raw, targetUrl, referer, ua, host);
|
|
348
|
+
var outBuf = Buffer.from(rewritten, 'utf8');
|
|
349
|
+
res.setHeader('Content-Type', 'application/vnd.apple.mpegurl; charset=utf-8');
|
|
350
|
+
res.setHeader('Content-Length', outBuf.length);
|
|
351
|
+
res.writeHead(upstream.statusCode);
|
|
352
|
+
res.end(outBuf);
|
|
353
|
+
});
|
|
354
|
+
stream.on('error', function (e) {
|
|
355
|
+
res.writeHead(500);
|
|
356
|
+
res.end('Decompression error: ' + e.message);
|
|
357
|
+
});
|
|
358
|
+
} else {
|
|
359
|
+
// Binary streaming with byte sniffing for disguised MPEG-TS packets
|
|
360
|
+
// Many upstream CDNs disguise .ts segments as .jpg or .js
|
|
361
|
+
var firstChunk = true;
|
|
362
|
+
var outHeaders = {};
|
|
363
|
+
if (upstream.headers['content-length']) outHeaders['Content-Length'] = upstream.headers['content-length'];
|
|
364
|
+
if (upstream.headers['content-range']) outHeaders['Content-Range'] = upstream.headers['content-range'];
|
|
365
|
+
if (upstream.headers['accept-ranges']) outHeaders['Accept-Ranges'] = upstream.headers['accept-ranges'];
|
|
366
|
+
|
|
367
|
+
upstream.once('data', function (chunk) {
|
|
368
|
+
firstChunk = false;
|
|
369
|
+
var detectedMime = ctype;
|
|
370
|
+
// Check for MPEG-TS sync byte 0x47 (71)
|
|
371
|
+
if (chunk && chunk.length > 0 && chunk[0] === 0x47) {
|
|
372
|
+
detectedMime = 'video/mp2t';
|
|
373
|
+
} else if (chunk && chunk.length > 2 && chunk[0] === 0xFF && chunk[1] === 0xD8) {
|
|
374
|
+
detectedMime = 'image/jpeg';
|
|
375
|
+
} else if (chunk && chunk.length > 3 && chunk[0] === 0x89 && chunk[1] === 0x50) {
|
|
376
|
+
detectedMime = 'image/png';
|
|
377
|
+
}
|
|
378
|
+
outHeaders['Content-Type'] = detectedMime;
|
|
379
|
+
try {
|
|
380
|
+
res.writeHead(upstream.statusCode, outHeaders);
|
|
381
|
+
res.write(chunk);
|
|
382
|
+
upstream.pipe(res);
|
|
383
|
+
} catch (ePipe) {}
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
upstream.on('end', function () {
|
|
387
|
+
if (firstChunk && !res.headersSent) {
|
|
388
|
+
try {
|
|
389
|
+
res.writeHead(upstream.statusCode, outHeaders);
|
|
390
|
+
res.end();
|
|
391
|
+
} catch (eEnd) {}
|
|
392
|
+
}
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
upstream.on('error', function (e) {
|
|
396
|
+
if (!res.headersSent && !res.writableEnded) {
|
|
397
|
+
try { res.writeHead(500); } catch (e2) {}
|
|
398
|
+
}
|
|
399
|
+
try { res.end(); } catch (e3) {}
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
req.on('close', function () {
|
|
403
|
+
try { upstream.destroy(); } catch (e) {}
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
function handleDebug(req, res) {
|
|
410
|
+
addCors(res);
|
|
411
|
+
if (req.method === 'POST') {
|
|
412
|
+
var chunks = [];
|
|
413
|
+
req.on('data', function (c) { chunks.push(c); });
|
|
414
|
+
req.on('end', function () {
|
|
415
|
+
clientDebugLog = Buffer.concat(chunks).toString('utf8');
|
|
416
|
+
clientDebugTime = new Date().toISOString();
|
|
417
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
418
|
+
res.end(JSON.stringify({ status: 'ok', size: clientDebugLog.length, timestamp: clientDebugTime }));
|
|
419
|
+
});
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// GET: return full text logs
|
|
424
|
+
res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
|
|
425
|
+
res.end('=======================================================\n' +
|
|
426
|
+
' NETMIRROR TV - LIVE ON-SCREEN DIAGNOSTICS LOG\n' +
|
|
427
|
+
'=======================================================\n' +
|
|
428
|
+
'Server Timestamp : ' + clientDebugTime + '\n' +
|
|
429
|
+
'Proxy Uptime : ' + Math.floor(process.uptime()) + 's\n' +
|
|
430
|
+
'Node Version : ' + process.version + '\n' +
|
|
431
|
+
'=======================================================\n\n' +
|
|
432
|
+
clientDebugLog + '\n');
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
const server = http.createServer(function (req, res) {
|
|
436
|
+
var parsed = url.parse(req.url, true);
|
|
437
|
+
var pathname = parsed.pathname;
|
|
438
|
+
|
|
439
|
+
if (req.method === 'OPTIONS') {
|
|
440
|
+
handleOptions(req, res);
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
if (pathname === '/proxy/health' || pathname === '/proxy/status') {
|
|
445
|
+
addCors(res);
|
|
446
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
447
|
+
res.end(JSON.stringify({
|
|
448
|
+
status: 'ok',
|
|
449
|
+
service: 'tizenbrew-netmirror',
|
|
450
|
+
uptime: process.uptime(),
|
|
451
|
+
debugSize: clientDebugLog.length
|
|
452
|
+
}));
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
if (pathname === '/proxy/debug' || pathname === '/proxy/debug.txt') {
|
|
457
|
+
handleDebug(req, res);
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
if (pathname === '/proxy/api') {
|
|
462
|
+
handleApiProxy(req, res, parsed.query);
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
if (pathname === '/proxy/media') {
|
|
467
|
+
handleMediaProxy(req, res, parsed.query);
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
serveStatic(req, res, pathname);
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
function startServer(port) {
|
|
475
|
+
server.listen(port, '0.0.0.0', function () {
|
|
476
|
+
console.log('[TizenBrew NetMirror] Service listening on http://127.0.0.1:' + port + ' (and LAN)');
|
|
477
|
+
});
|
|
478
|
+
server.on('error', function (err) {
|
|
479
|
+
if (err.code === 'EADDRINUSE') {
|
|
480
|
+
console.log('[TizenBrew NetMirror] Port ' + port + ' in use, trying ' + (port + 1));
|
|
481
|
+
startServer(port + 1);
|
|
482
|
+
} else {
|
|
483
|
+
console.error('[TizenBrew NetMirror] Server error:', err);
|
|
484
|
+
}
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
startServer(DEFAULT_PORT);
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
process.on('uncaughtException', function (err) {
|
|
492
|
+
console.error('[TizenBrew NetMirror Service] Safe uncaught exception:', err.message);
|
|
493
|
+
});
|
|
494
|
+
process.on('unhandledRejection', function (reason) {
|
|
495
|
+
console.error('[TizenBrew NetMirror Service] Safe unhandled rejection:', reason);
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
module.exports = server;
|
package/web/config.xml
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<widget xmlns="http://www.w3.org/ns/widgets"
|
|
3
|
+
xmlns:tizen="http://tizen.org/ns/widgets"
|
|
4
|
+
id="http://example.com/tvplayer" version="1.0.2" viewmodes="maximized">
|
|
5
|
+
<tizen:application id="TvPlyr0001.TVPlayer" package="TvPlyr0001" required_version="2.3"/>
|
|
6
|
+
<content src="index.html"/>
|
|
7
|
+
<name>NetMirror TV</name>
|
|
8
|
+
<icon src="icon.png"/>
|
|
9
|
+
<feature name="http://tizen.org/feature/screen.size.all"/>
|
|
10
|
+
<tizen:profile name="tv-samsung"/>
|
|
11
|
+
<tizen:privilege name="http://tizen.org/privilege/internet"/>
|
|
12
|
+
<tizen:privilege name="http://tizen.org/privilege/tv.inputdevice"/>
|
|
13
|
+
<access origin="*" subdomains="true"/>
|
|
14
|
+
<tizen:allow-navigation>*</tizen:allow-navigation>
|
|
15
|
+
<!-- Explicit policy: scripts only from the package (+ hls.js CDN fallback);
|
|
16
|
+
images, media, subtitle blobs and API calls from anywhere. -->
|
|
17
|
+
<tizen:content-security-policy>default-src * data: blob:; script-src 'self' https://cdn.jsdelivr.net; style-src 'self' 'unsafe-inline'; img-src * data: blob:; media-src * data: blob:; connect-src * data: blob:; object-src 'none'</tizen:content-security-policy>
|
|
18
|
+
<tizen:setting screen-orientation="landscape" context-menu="disable"
|
|
19
|
+
background-support="disable" encryption="disable"
|
|
20
|
+
install-location="auto" hwkey-event="enable"/>
|
|
21
|
+
</widget>
|