stremiok 2.2.2 → 2.2.4
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/CHANGELOG.md +28 -0
- package/README.md +10 -7
- package/config.xml +1 -1
- package/css/style.css +3 -1
- package/index.html +34 -33
- package/js/app.js +9 -2
- package/js/epg.js +1 -1
- package/js/focus.js +25 -5
- package/js/iptv.js +104 -8
- package/js/player-core.js +9 -9
- package/js/player-store.js +1 -1
- package/js/stremio.js +45 -8
- package/js/util.js +82 -3
- package/js/view-core.js +20 -2
- package/js/view-detail.js +37 -10
- package/js/view-guide.js +16 -14
- package/js/view-home.js +82 -12
- package/js/view-settings.js +62 -5
- package/js/views.js +1 -1
- package/js/xtvod.js +366 -0
- package/js/ytplay.js +24 -1
- package/package.json +1 -1
- package/service/index.js +543 -10
package/service/index.js
CHANGED
|
@@ -20,6 +20,16 @@
|
|
|
20
20
|
* /aiom/addons?base=<AIOManager address> (+ X-AIOM-Key: the account's API key) -> JSON
|
|
21
21
|
* { addons: [{ url, name, enabled }] }: the account's addons (its Hydra
|
|
22
22
|
* API), StremioK registered as a Pull app; StremioK only (entryAllowed)
|
|
23
|
+
* /xtvod/list?base=<server>&user=<user>&pass=<password>&type=movie|series[&q=<words>][&skip=n][&force=1] -> JSON
|
|
24
|
+
* { state, at, items: [{ id, name, year, tmdb, cat, ext, pic, rating }] }: an Xtream
|
|
25
|
+
* account's newest films or series (200), or those whose names have the words
|
|
26
|
+
* (force: its list read again now, as Reload asks);
|
|
27
|
+
* /xtvod/find?…&type&[tmdb=][&name=&year=] -> the same: a title's copies there
|
|
28
|
+
* (see xtvControl); StremioK only (entryAllowed)
|
|
29
|
+
* /stalker/list?portal=<address>&mac=<MAC> -> JSON { m3u, count } or { refused | unreachable,
|
|
30
|
+
* why }: a Stalker portal's channels as an M3U playlist; /stalker/link?portal&mac
|
|
31
|
+
* &u=<a channel's link in it> -> JSON { url } or { why }: its link to play now
|
|
32
|
+
* (see stalkerControl); StremioK only (entryAllowed)
|
|
23
33
|
* /epg/load?u=<guide>&k=<bitset>[&force=1] (or POSTed: the same in its body, as StremioK
|
|
24
34
|
* asks) -> JSON { state, at, channels, programmes, partial,
|
|
25
35
|
* error }: a playlist's TV guide (XMLTV) read for StremioK's channels
|
|
@@ -27,7 +37,8 @@
|
|
|
27
37
|
* [[start, stop, title, desc, picture?, about?]], _icons: { <key>: logo } }: their programs and
|
|
28
38
|
* the guide's logos for them (see epgControl)
|
|
29
39
|
* /epg/ticket -> JSON { ticket }: for guide POSTs from an app without the token (see hasTicket)
|
|
30
|
-
* /epg/files -> JSON { count, bytes }: the saved guides
|
|
40
|
+
* /epg/files -> JSON { count, bytes, xtv: { count, bytes } }: the saved guides, and Xtream lists;
|
|
41
|
+
* /epg/clear (POST) -> { cleared }: all of them deleted
|
|
31
42
|
* /live/open?u=<playlist>[&ua=&ref=&force=1] -> JSON { fix, sure, why, url }: is a live HLS
|
|
32
43
|
* channel's AAC sound cut mid-frame (the TV's player stutters on it)?
|
|
33
44
|
* If so, url: a relay that repacks it (/live/<id>/m|s?n=, see liveControl)
|
|
@@ -1806,7 +1817,9 @@ function handle(req, res) {
|
|
|
1806
1817
|
return;
|
|
1807
1818
|
}
|
|
1808
1819
|
if (/^\/epg\//.test(path)) { epgControl(req, res, path, parts[1]); return; } // (a playlist's TV guide: see epgControl)
|
|
1809
|
-
if (path === '/aiom/addons') { aiomControl(req, res, parts[1], leaving); return; }
|
|
1820
|
+
if (path === '/aiom/addons') { aiomControl(req, res, parts[1], leaving); return; }
|
|
1821
|
+
if (path === '/xtvod/list' || path === '/xtvod/find') { xtvControl(req, res, path, parts[1]); return; } // (an Xtream account's films and series: see xtvControl)
|
|
1822
|
+
if (path === '/stalker/list' || path === '/stalker/link') { stalkerControl(req, res, path, parts[1], leaving); return; } // (a Stalker portal: see stalkerControl) // (AIOManager's addons: see aiomControl) // (a live channel's sound repacked: see liveControl) // (the phone's one-time page: see below)
|
|
1810
1823
|
if (path === '/fsfile') { // a USB file over HTTP, with Range: the player reads a big one from here (see fsfile in the header)
|
|
1811
1824
|
var whyF = fsAllowed(req, parts[1]);
|
|
1812
1825
|
if (whyF) { send(res, 403, 'text/plain', 'not allowed (' + whyF + ')'); return; }
|
|
@@ -2667,6 +2680,524 @@ function aiomControl(req, res, query, leaving) {
|
|
|
2667
2680
|
}));
|
|
2668
2681
|
}
|
|
2669
2682
|
|
|
2683
|
+
// ---- Xtream films and series (/xtvod/list, /xtvod/find) ----
|
|
2684
|
+
// An Xtream account's whole list of films (get_vod_streams) and series (get_series), read a piece
|
|
2685
|
+
// at a time (xtvItems: one title parsed at a time, never the whole answer) into a small record each
|
|
2686
|
+
// ({ i: id, n: name, k: name to match, y: year, t: TMDB id, c: category, a: added, e: file type,
|
|
2687
|
+
// p: picture, r: rating }), up to XTV_MAX bytes, XTV_MS and XTV_ITEMS titles. Kept XTV_KEEP accounts
|
|
2688
|
+
// and types at once; on the sideloaded app saved in its folder (xtvSave), read again after
|
|
2689
|
+
// XTV_FRESH_MS. While it's read again, what's asked is answered with the old list. One download at
|
|
2690
|
+
// a time.
|
|
2691
|
+
var XTV_SLICE = 2000, XTV_IDLE_MS = 30 * 60000, XTV_FILE_MS = 3 * 86400000;
|
|
2692
|
+
var XTV_MAX = 150 * 1048576, XTV_MS = 6 * 60000, XTV_ITEMS = 100000, XTV_FRESH_MS = 86400000, XTV_RETRY_MS = 10 * 60000, XTV_KEEP = 4, XTV_NEWEST = 200, XTV_FOUND = 100;
|
|
2693
|
+
var _xtv = Object.create(null), _xtvQueue = [], _xtvBusy = false;
|
|
2694
|
+
// A name to match: no language tag (EN - , |EN|, [EN]), brackets, year, quality words or accents.
|
|
2695
|
+
var XTV_WORDS = /\b(4k|uhd|fhd|hd|sd|hdr|hevc|x265|h265|1080p|720p|2160p|multi|multisub|vostfr|vost|dual|sub|subs|dub|dubbed|latino|castellano|vf|vo)\b/g;
|
|
2696
|
+
function xtvKey(name) {
|
|
2697
|
+
var s = String(name || '').toLowerCase().replace(/[áàâäãåéèêëíìîïóòôöõúùûüñç]/g, function (c) { return EPG_ACCENTS[c]; });
|
|
2698
|
+
for (var i = 0; i < 3; i++) { s = s.replace(/^\s*(\|[^|]{1,12}\||\[[^\]]{1,12}\]|[a-z]{2,3}\s*[-:|]\s+)\s*/, ''); }
|
|
2699
|
+
return s.replace(/\([^()]*\)|\[[^\[\]]*\]/g, ' ').replace(XTV_WORDS, ' ').trim().replace(/\b(19|20)\d{2}$/, ' ').replace(/&/g, ' and ').replace(/[^a-z0-9]+/g, ' ').trim();
|
|
2700
|
+
}
|
|
2701
|
+
function xtvYear(o, name) {
|
|
2702
|
+
var y = parseInt(o.year, 10) || parseInt(String(o.releaseDate || o.release_date || '').slice(0, 4), 10);
|
|
2703
|
+
if (!(y > 1880 && y < 2100)) { var n = String(name || '').trim(), m = /[(\s\[](19\d{2}|20\d{2})[)\]]?$/.exec(n) || /\((19\d{2}|20\d{2})\)/.exec(n); y = m ? +m[1] : 0; }
|
|
2704
|
+
return y > 1880 && y < 2100 ? y : 0;
|
|
2705
|
+
}
|
|
2706
|
+
function xtvRecord(o, type) {
|
|
2707
|
+
if (!o || typeof o !== 'object') { return null; }
|
|
2708
|
+
var id = type === 'movie' ? o.stream_id : o.series_id, name = String(o.name || '').replace(/\s+/g, ' ').trim().slice(0, 200);
|
|
2709
|
+
if (!(+id > 0) || !name) { return null; }
|
|
2710
|
+
var r = { i: +id, n: name, k: xtvKey(name), y: xtvYear(o, name), c: String(o.category_id || '').slice(0, 20), a: +(o.added || o.last_modified || 0) || 0 };
|
|
2711
|
+
var t = parseInt(o.tmdb || o.tmdb_id, 10);
|
|
2712
|
+
if (t > 0) { r.t = t; }
|
|
2713
|
+
if (type === 'movie' && /^[a-z0-9]{2,5}$/i.test(String(o.container_extension || ''))) { r.e = String(o.container_extension).toLowerCase(); }
|
|
2714
|
+
var pic = String(o.stream_icon || o.cover || '');
|
|
2715
|
+
if (/^https?:\/\//i.test(pic) && pic.length < 600) { r.p = pic; }
|
|
2716
|
+
var rating = parseFloat(o.rating_5based ? o.rating_5based * 2 : o.rating);
|
|
2717
|
+
if (rating > 0 && rating <= 10) { r.r = Math.round(rating * 10) / 10; }
|
|
2718
|
+
return r;
|
|
2719
|
+
}
|
|
2720
|
+
// The titles in a JSON array, one at a time: feed(text) as it comes; onItem(each object directly
|
|
2721
|
+
// in the array). A title bigger than 1 MB is skipped.
|
|
2722
|
+
function xtvItems(onItem) {
|
|
2723
|
+
var depth = 0, inStr = false, esc = false, inItem = false, carry = '', skip = false;
|
|
2724
|
+
return function (text) {
|
|
2725
|
+
var start = inItem ? 0 : -1;
|
|
2726
|
+
for (var i = 0; i < text.length; i++) {
|
|
2727
|
+
var ch = text.charCodeAt(i);
|
|
2728
|
+
if (inStr) { if (esc) { esc = false; } else if (ch === 92) { esc = true; } else if (ch === 34) { inStr = false; } continue; }
|
|
2729
|
+
if (ch === 34) { inStr = true; }
|
|
2730
|
+
else if (ch === 123 || ch === 91) { depth++; if (depth === 2 && ch === 123 && !inItem) { inItem = true; start = i; carry = ''; skip = false; } }
|
|
2731
|
+
else if (ch === 125 || ch === 93) {
|
|
2732
|
+
if (depth === 2 && inItem) {
|
|
2733
|
+
var piece = carry + text.slice(start, i + 1);
|
|
2734
|
+
inItem = false; carry = '';
|
|
2735
|
+
if (!skip) { try { onItem(JSON.parse(piece)); } catch (e) {} }
|
|
2736
|
+
}
|
|
2737
|
+
depth--;
|
|
2738
|
+
}
|
|
2739
|
+
}
|
|
2740
|
+
if (inItem && !skip) { carry += text.slice(start); if (carry.length > 1048576) { skip = true; carry = ''; } }
|
|
2741
|
+
};
|
|
2742
|
+
}
|
|
2743
|
+
// A GET read as it comes (onText each piece, as text): redirects on the same server, XTV_MAX bytes,
|
|
2744
|
+
// XTV_MS in all. cb(err) once it's done.
|
|
2745
|
+
function xtvGet(url, onText, cb) {
|
|
2746
|
+
cb = once(cb);
|
|
2747
|
+
var first = null, r = null, size = 0, done = false;
|
|
2748
|
+
try { first = urllib.parse(url); } catch (e) { first = null; }
|
|
2749
|
+
var timer = setTimeout(function () { finish(upError('took too long', 504)); }, XTV_MS);
|
|
2750
|
+
function finish(err) { if (done) { return; } done = true; clearTimeout(timer); try { if (err && r) { r.abort(); } } catch (e) {} cb(err); }
|
|
2751
|
+
function go(u, hops) {
|
|
2752
|
+
var pu;
|
|
2753
|
+
try { pu = urllib.parse(u); } catch (e) { finish(upError('bad address', 400)); return; }
|
|
2754
|
+
if ((pu.protocol !== 'http:' && pu.protocol !== 'https:') || !dlHostOk(pu.hostname)) { finish(upError('an address it can\'t reach', 400)); return; }
|
|
2755
|
+
var opts = { hostname: pu.hostname, port: pu.port, path: pu.path, method: 'GET', headers: { 'User-Agent': TV_UA, 'Accept': 'application/json', 'Accept-Encoding': 'identity' } };
|
|
2756
|
+
if (pu.protocol === 'https:' && caList().length) { opts.ca = caList(); }
|
|
2757
|
+
try {
|
|
2758
|
+
r = (pu.protocol === 'https:' ? https : http).request(opts, function (resp) {
|
|
2759
|
+
if (done) { resp.resume(); return; }
|
|
2760
|
+
var sc = resp.statusCode;
|
|
2761
|
+
if (sc >= 300 && sc < 400 && resp.headers.location && hops < 4) {
|
|
2762
|
+
resp.resume();
|
|
2763
|
+
var next = urllib.resolve(u, resp.headers.location), np = null;
|
|
2764
|
+
try { np = urllib.parse(next); } catch (e) { np = null; }
|
|
2765
|
+
if (!sameServerRedirect(first, np)) { finish(upError('redirected to another server: not followed (your login stays with it)', 502)); return; }
|
|
2766
|
+
go(next, hops + 1);
|
|
2767
|
+
return;
|
|
2768
|
+
}
|
|
2769
|
+
if (sc !== 200) { resp.resume(); finish(upError('HTTP ' + sc, sc === 401 || sc === 403 ? 401 : 502)); return; }
|
|
2770
|
+
var dec = new StringDecoder('utf8');
|
|
2771
|
+
resp.on('data', function (c) {
|
|
2772
|
+
if (done) { return; }
|
|
2773
|
+
size += c.length;
|
|
2774
|
+
if (size > XTV_MAX) { finish(upError('too large', 502)); return; }
|
|
2775
|
+
try { onText(dec.write(c)); } catch (e) { finish(e); }
|
|
2776
|
+
});
|
|
2777
|
+
resp.on('end', function () { if (!done) { try { onText(dec.end()); } catch (e) {} finish(null); } });
|
|
2778
|
+
resp.on('error', function (e) { finish(e); });
|
|
2779
|
+
});
|
|
2780
|
+
r.on('error', function (e) { finish(upError('couldn\'t reach it (' + (e.code || e.message) + ')', 502)); });
|
|
2781
|
+
guardSelf(r, function () { finish(upError('an address it can\'t reach', 400)); });
|
|
2782
|
+
r.end();
|
|
2783
|
+
} catch (e) { finish(e); }
|
|
2784
|
+
}
|
|
2785
|
+
go(url, 0);
|
|
2786
|
+
}
|
|
2787
|
+
// The saved list: xtv-<hash>.jsonl in the app's folder, a line for its key and date, then a line a
|
|
2788
|
+
// title, written and read in small pieces (the service also relays live channels, YouTube and
|
|
2789
|
+
// guides: nothing here holds it up for long). Saved lists not used for XTV_FILE_MS are deleted.
|
|
2790
|
+
function xtvFile(key) { var d = tokenDir(); return d ? d + '/xtv-' + epgHash(key).toString(36) + '.jsonl' : null; }
|
|
2791
|
+
var _xtvSwept = false, XTV_FILES = /^xtv-[0-9a-z]+\.jsonl?(\.part)?$/;
|
|
2792
|
+
function xtvSweep() {
|
|
2793
|
+
if (_xtvSwept) { return; }
|
|
2794
|
+
_xtvSwept = true;
|
|
2795
|
+
var d = tokenDir(), fs = require('fs');
|
|
2796
|
+
if (!d) { return; }
|
|
2797
|
+
fs.readdir(d, function (err, names) {
|
|
2798
|
+
(err ? [] : names).filter(function (n) { return XTV_FILES.test(n); }).forEach(function (n) {
|
|
2799
|
+
if (/\.json$/.test(n)) { fs.unlink(d + '/' + n, noop); return; } // (the first version's)
|
|
2800
|
+
fs.stat(d + '/' + n, function (e, st) { if (!e && st && Date.now() - st.mtime.getTime() > XTV_FILE_MS) { fs.unlink(d + '/' + n, noop); } });
|
|
2801
|
+
});
|
|
2802
|
+
});
|
|
2803
|
+
}
|
|
2804
|
+
function xtvSave(x) {
|
|
2805
|
+
var f = xtvFile(x.key), fs = require('fs'), items = x.items, at = 0, failed = false, s;
|
|
2806
|
+
if (!f) { return; }
|
|
2807
|
+
try { s = fs.createWriteStream(f + '.part', { mode: 384 }); } catch (e) { return; }
|
|
2808
|
+
s.on('error', function (e) { failed = true; log('Xtream list not saved (' + (e.code || e.message) + ')'); });
|
|
2809
|
+
s.write(JSON.stringify({ key: x.key, at: x.at }) + '\n');
|
|
2810
|
+
(function more() {
|
|
2811
|
+
if (failed) { return; }
|
|
2812
|
+
var lines = [], end = Math.min(items.length, at + XTV_SLICE);
|
|
2813
|
+
for (; at < end; at++) { lines.push(JSON.stringify(items[at])); }
|
|
2814
|
+
var room = s.write(lines.length ? lines.join('\n') + '\n' : '');
|
|
2815
|
+
if (at >= items.length) {
|
|
2816
|
+
s.end(function () {
|
|
2817
|
+
if (failed) { return; }
|
|
2818
|
+
if (_xtv[x.key] !== x) { fs.unlink(f + '.part', noop); return; } // (cleared meanwhile: Clear saved copies)
|
|
2819
|
+
fs.rename(f + '.part', f, function (e) { if (!e && x.items === items) { x.saved = true; xtvIdle(x); } });
|
|
2820
|
+
});
|
|
2821
|
+
return;
|
|
2822
|
+
}
|
|
2823
|
+
if (room) { setTimeout(more, 0); } else { s.once('drain', more); }
|
|
2824
|
+
})();
|
|
2825
|
+
}
|
|
2826
|
+
// cb(items, at): the saved list, or nothing (none, another's, unreadable).
|
|
2827
|
+
function xtvLoad(x, cb) {
|
|
2828
|
+
var f = xtvFile(x.key), fs = require('fs'), items = [], at = 0, first = true, rest = '', dec = new StringDecoder('utf8'), s;
|
|
2829
|
+
cb = once(cb);
|
|
2830
|
+
try { s = fs.createReadStream(f, { highWaterMark: 65536 }); } catch (e) { cb(); return; }
|
|
2831
|
+
function line(l) {
|
|
2832
|
+
if (!l) { return; }
|
|
2833
|
+
var o = null;
|
|
2834
|
+
try { o = JSON.parse(l); } catch (e) { o = null; }
|
|
2835
|
+
if (first) {
|
|
2836
|
+
first = false;
|
|
2837
|
+
if (!o || o.key !== x.key) { items = null; try { s.destroy(); } catch (e2) {} cb(); return; }
|
|
2838
|
+
at = +o.at || 0;
|
|
2839
|
+
} else if (o && items) { items.push(o); }
|
|
2840
|
+
}
|
|
2841
|
+
s.on('data', function (c) {
|
|
2842
|
+
var ls = (rest + dec.write(c)).split('\n');
|
|
2843
|
+
rest = ls.pop();
|
|
2844
|
+
ls.forEach(line);
|
|
2845
|
+
});
|
|
2846
|
+
s.on('end', function () { line(rest); if (items && items.length) { cb(items, at); } else { cb(); } });
|
|
2847
|
+
s.on('error', function () { cb(); });
|
|
2848
|
+
}
|
|
2849
|
+
// The newest titles, without sorting the whole list: the XTV_NEWEST latest kept in order.
|
|
2850
|
+
function xtvNewestOf(items) {
|
|
2851
|
+
var top = [];
|
|
2852
|
+
items.forEach(function (r) {
|
|
2853
|
+
if (top.length >= XTV_NEWEST && r.a <= top[top.length - 1].a) { return; }
|
|
2854
|
+
var lo = 0, hi = top.length;
|
|
2855
|
+
while (lo < hi) { var mid = (lo + hi) >> 1; if (top[mid].a >= r.a) { lo = mid + 1; } else { hi = mid; } }
|
|
2856
|
+
top.splice(lo, 0, r);
|
|
2857
|
+
if (top.length > XTV_NEWEST) { top.pop(); }
|
|
2858
|
+
});
|
|
2859
|
+
return top;
|
|
2860
|
+
}
|
|
2861
|
+
function xtvHave(x, items, at) {
|
|
2862
|
+
x.items = items; x.at = at; x.newest = xtvNewestOf(items); x.state = 'ready'; x.error = '';
|
|
2863
|
+
xtvIdle(x);
|
|
2864
|
+
}
|
|
2865
|
+
// Unused for XTV_IDLE_MS, a list that's saved leaves memory (its newest titles stay: Home's row),
|
|
2866
|
+
// and comes back from the file when it's asked for.
|
|
2867
|
+
function xtvIdle(x) {
|
|
2868
|
+
if (x.idleTimer) { return; }
|
|
2869
|
+
x.idleTimer = setTimeout(function () {
|
|
2870
|
+
x.idleTimer = null;
|
|
2871
|
+
if (_xtv[x.key] !== x || !x.items) { return; }
|
|
2872
|
+
if (!x.saved || x.busy || Date.now() - x.used < XTV_IDLE_MS) { xtvIdle(x); return; }
|
|
2873
|
+
x.items = null; x.triedFile = false; x.state = 'new';
|
|
2874
|
+
}, XTV_IDLE_MS);
|
|
2875
|
+
if (x.idleTimer.unref) { x.idleTimer.unref(); }
|
|
2876
|
+
}
|
|
2877
|
+
// What it needs now: its saved list read (at once: one account's download doesn't hold up
|
|
2878
|
+
// another's file), or its list downloaded (queued, one at a time). Those waiting for the file are
|
|
2879
|
+
// answered when it's read.
|
|
2880
|
+
function xtvDue(x, force) {
|
|
2881
|
+
if (force) { x.force = true; }
|
|
2882
|
+
if (x.busy || _xtvQueue.indexOf(x) !== -1) { return; }
|
|
2883
|
+
if (!x.items && !x.triedFile && xtvFile(x.key)) {
|
|
2884
|
+
x.triedFile = true; x.busy = true; x.loading = true; x.state = 'reading';
|
|
2885
|
+
xtvLoad(x, function (items, at) {
|
|
2886
|
+
x.busy = false; x.loading = false;
|
|
2887
|
+
if (items) { x.saved = true; xtvHave(x, items, at); } else { x.state = 'new'; }
|
|
2888
|
+
xtvDue(x);
|
|
2889
|
+
var w = x.waiters; x.waiters = [];
|
|
2890
|
+
w.forEach(function (fn) { try { fn(); } catch (e) {} });
|
|
2891
|
+
});
|
|
2892
|
+
return;
|
|
2893
|
+
}
|
|
2894
|
+
var stale = x.force || !x.at || Date.now() - x.at > XTV_FRESH_MS, retry = x.force || Date.now() - (x.failedAt || 0) > XTV_RETRY_MS;
|
|
2895
|
+
if (!stale || !retry) { return; }
|
|
2896
|
+
if (!x.items) { x.state = 'queued'; }
|
|
2897
|
+
_xtvQueue.push(x);
|
|
2898
|
+
xtvPump();
|
|
2899
|
+
}
|
|
2900
|
+
// The list of this account and type: kept, saved, or read (queued); x.state 'ready' with x.items.
|
|
2901
|
+
function xtvList(base, user, pass, type, force) {
|
|
2902
|
+
var key = base + '|' + user + '|' + type, x = _xtv[key];
|
|
2903
|
+
xtvSweep();
|
|
2904
|
+
if (!x) {
|
|
2905
|
+
var keys = Object.keys(_xtv);
|
|
2906
|
+
if (keys.length >= XTV_KEEP) { keys.sort(function (a, b) { return _xtv[a].used - _xtv[b].used; }); delete _xtv[keys[0]]; }
|
|
2907
|
+
x = _xtv[key] = { key: key, base: base, user: user, pass: pass, type: type, state: 'new', items: null, newest: null, at: 0, waiters: [] };
|
|
2908
|
+
}
|
|
2909
|
+
x.pass = pass; x.used = Date.now();
|
|
2910
|
+
xtvDue(x, force);
|
|
2911
|
+
return x;
|
|
2912
|
+
}
|
|
2913
|
+
function xtvPump() {
|
|
2914
|
+
if (_xtvBusy || !_xtvQueue.length) { return; }
|
|
2915
|
+
var x = _xtvQueue.shift();
|
|
2916
|
+
if (_xtv[x.key] !== x) { xtvPump(); return; }
|
|
2917
|
+
_xtvBusy = true; x.busy = true; x.force = false;
|
|
2918
|
+
if (!x.items) { x.state = 'reading'; }
|
|
2919
|
+
var items = [], url = x.base + '/player_api.php?username=' + encodeURIComponent(x.user) + '&password=' + encodeURIComponent(x.pass) + '&action=' + (x.type === 'movie' ? 'get_vod_streams' : 'get_series');
|
|
2920
|
+
var feed = xtvItems(function (o) { if (items.length < XTV_ITEMS) { var r = xtvRecord(o, x.type); if (r) { items.push(r); } } });
|
|
2921
|
+
xtvGet(url, feed, function (err) {
|
|
2922
|
+
_xtvBusy = false; x.busy = false;
|
|
2923
|
+
try {
|
|
2924
|
+
if (err || (!items.length && !x.items)) {
|
|
2925
|
+
x.failedAt = Date.now(); x.error = err ? (err.message || 'failed') : 'no titles in its answer';
|
|
2926
|
+
if (!x.items) { x.state = 'failed'; }
|
|
2927
|
+
log('Xtream ' + x.type + ' list not read: ' + x.error);
|
|
2928
|
+
} else if (_xtv[x.key] === x) {
|
|
2929
|
+
xtvHave(x, items, Date.now());
|
|
2930
|
+
x.failedAt = 0;
|
|
2931
|
+
xtvSave(x);
|
|
2932
|
+
}
|
|
2933
|
+
} catch (e) { x.state = x.items ? 'ready' : 'failed'; }
|
|
2934
|
+
xtvPump();
|
|
2935
|
+
});
|
|
2936
|
+
}
|
|
2937
|
+
function xtvOut(r) { var o = { id: r.i, name: r.n, year: r.y || 0, cat: r.c }; if (r.t) { o.tmdb = r.t; } if (r.e) { o.ext = r.e; } if (r.p) { o.pic = r.p; } if (r.r) { o.rating = r.r; } return o; }
|
|
2938
|
+
function xtvControl(req, res, path, query) {
|
|
2939
|
+
if (!entryAllowed(req, query)) { send(res, 403, 'text/plain', 'not allowed (token)'); return; }
|
|
2940
|
+
var q = {};
|
|
2941
|
+
(query || '').split('&').forEach(function (kv) { var i = kv.indexOf('='); if (i > 0) { try { q[kv.slice(0, i)] = decodeURIComponent(kv.slice(i + 1)); } catch (e) {} } });
|
|
2942
|
+
var base = String(q.base || '').replace(/\/+$/, ''), type = q.type === 'series' ? 'series' : 'movie';
|
|
2943
|
+
if (!/^https?:\/\/[^\s]+$/i.test(base) || base.length > 500 || !q.user || !q.pass || q.user.length > 200 || q.pass.length > 200) { send(res, 400, 'text/plain', 'bad account'); return; }
|
|
2944
|
+
var x = xtvList(base, String(q.user), String(q.pass), type, q.force === '1');
|
|
2945
|
+
function answer() {
|
|
2946
|
+
var items = x.items || [], out = [], i;
|
|
2947
|
+
if (path === '/xtvod/find') {
|
|
2948
|
+
var tmdb = parseInt(q.tmdb, 10) || 0, k = xtvKey(q.name), y = parseInt(q.year, 10) || 0;
|
|
2949
|
+
for (i = 0; i < items.length && out.length < 12; i++) {
|
|
2950
|
+
var r = items[i];
|
|
2951
|
+
if (tmdb && r.t) { if (r.t === tmdb) { out.push(r); } continue; }
|
|
2952
|
+
if (k && r.k === k && (!y || !r.y || Math.abs(r.y - y) <= (type === 'series' ? 0 : 1))) { out.push(r); }
|
|
2953
|
+
}
|
|
2954
|
+
} else if (q.q) {
|
|
2955
|
+
var words = xtvKey(q.q).split(' ').filter(Boolean), first = [], rest = [], whole = words.join(' ');
|
|
2956
|
+
for (i = 0; words.length && i < items.length && first.length + rest.length < XTV_FOUND * 3; i++) {
|
|
2957
|
+
var t = items[i], all = true;
|
|
2958
|
+
for (var w = 0; all && w < words.length; w++) { var at = t.k.indexOf(words[w]); all = at === 0 || (at > 0 && t.k.indexOf(' ' + words[w]) !== -1); }
|
|
2959
|
+
if (all) { (t.k.indexOf(whole) === 0 ? first : rest).push(t); }
|
|
2960
|
+
}
|
|
2961
|
+
out = first.concat(rest).slice(0, XTV_FOUND);
|
|
2962
|
+
} else {
|
|
2963
|
+
out = x.newest || [];
|
|
2964
|
+
}
|
|
2965
|
+
var skip = parseInt(q.skip, 10) || 0;
|
|
2966
|
+
if (skip > 0) { out = out.slice(skip); }
|
|
2967
|
+
send(res, 200, 'application/json', JSON.stringify({ state: x.state, at: x.at, error: x.error || '', items: out.map(xtvOut) }));
|
|
2968
|
+
}
|
|
2969
|
+
// (its saved list on its way: a moment; the newest row doesn't need it)
|
|
2970
|
+
if (x.loading && !(path === '/xtvod/list' && !q.q && x.newest)) { x.waiters.push(answer); return; }
|
|
2971
|
+
answer();
|
|
2972
|
+
}
|
|
2973
|
+
|
|
2974
|
+
// ---- Stalker portals (/stalker/list, /stalker/link) ----
|
|
2975
|
+
// A Stalker (Ministra) portal, asked as a MAG box asks: its MAC address in a cookie, a MAG
|
|
2976
|
+
// User-Agent, a session token from a handshake (none of which a web page can send). Its API at
|
|
2977
|
+
// <address>/portal.php, else /server/load.php (the address's /c/ left out). list: its genres and
|
|
2978
|
+
// channels (get_all_channels, else get_ordered_list a page at a time, STALKER_PAGES at most; all=1:
|
|
2979
|
+
// the whole portal, up to STALKER_CHANNELS, a failed page tried again; cut: it stopped short) as an
|
|
2980
|
+
// M3U playlist, genres as groups, xmltv_id as tvg-id, each channel's link the one in its cmd (often
|
|
2981
|
+
// a stand-in, http://localhost/ch/123_), kept STALKER_LIST_MS (adding a portal just checked reads it
|
|
2982
|
+
// once). link: the link to play now (create_link, with the channel's own cmd). A session whose
|
|
2983
|
+
// token the portal no longer takes is opened again, once. Redirects only on the same server.
|
|
2984
|
+
var STALKER_UA = 'Mozilla/5.0 (QtEmbedded; U; Linux; C) AppleWebKit/533.3 (KHTML, like Gecko) MAG200 stbapp ver: 2 rev: 250 Safari/533.3';
|
|
2985
|
+
var STALKER_MS = 20000, STALKER_MAX = 16 * 1048576, STALKER_PAGES = 100, STALKER_CHANNELS = 30000, STALKER_LIST_MS = 2 * 60000, STALKER_KEEP = 4, STALKER_RETRIES = 2, STALKER_RETRY_MS = 3000;
|
|
2986
|
+
var _stalker = Object.create(null); // portal|mac -> { api, base, mac, token, cmds: { link: cmd }, list: { at, m3u, count }, used }
|
|
2987
|
+
function stalkerKeep(key) {
|
|
2988
|
+
var s = _stalker[key];
|
|
2989
|
+
if (!s) {
|
|
2990
|
+
var keys = Object.keys(_stalker);
|
|
2991
|
+
if (keys.length >= STALKER_KEEP) { keys.sort(function (a, b) { return _stalker[a].used - _stalker[b].used; }); delete _stalker[keys[0]]; }
|
|
2992
|
+
s = _stalker[key] = { token: '', cmds: {}, list: null };
|
|
2993
|
+
}
|
|
2994
|
+
s.used = Date.now();
|
|
2995
|
+
return s;
|
|
2996
|
+
}
|
|
2997
|
+
function stalkerApis(portal) {
|
|
2998
|
+
var b = portal.replace(/[?#].*$/, '').replace(/\/+$/, '').replace(/\/(portal|load)\.php$/i, '').replace(/\/(c|server)$/i, '').replace(/\/+$/, '');
|
|
2999
|
+
var apis = [b + '/portal.php', b + '/server/load.php'];
|
|
3000
|
+
if (!/\/stalker_portal$/i.test(b)) { apis.push(b + '/stalker_portal/server/load.php'); }
|
|
3001
|
+
return { base: b, apis: apis };
|
|
3002
|
+
}
|
|
3003
|
+
// One GET to the portal: cb(err, js) with its answer's "js"; an answer that isn't JSON (a portal
|
|
3004
|
+
// that refuses the session says "Authorization failed.") is an error, err.auth when it says so.
|
|
3005
|
+
function stalkerAsk(s, params, ctl, cb) {
|
|
3006
|
+
cb = once(cb);
|
|
3007
|
+
var url = s.api + '?' + params + '&JsHttpRequest=1-xml', first = null, done = false;
|
|
3008
|
+
try { first = urllib.parse(url); } catch (e) { first = null; }
|
|
3009
|
+
var h = { 'User-Agent': STALKER_UA, 'X-User-Agent': 'Model: MAG250; Link: WiFi', 'Accept': '*/*', 'Accept-Encoding': 'gzip',
|
|
3010
|
+
'Cookie': 'mac=' + s.mac + '; stb_lang=en; timezone=UTC', 'Referer': s.base + '/c/' };
|
|
3011
|
+
if (s.token) { h.Authorization = 'Bearer ' + s.token; }
|
|
3012
|
+
var timer = setTimeout(function () { finish(upError('the portal didn\'t answer', 504)); }, STALKER_MS), r = null;
|
|
3013
|
+
function finish(err, js) { if (done) { return; } done = true; clearTimeout(timer); if (ctl.abort === abort) { ctl.abort = null; } if (err) { try { if (r) { r.abort(); } } catch (e) {} } cb(err, js); }
|
|
3014
|
+
function abort() { finish(upError('stopped', 499)); }
|
|
3015
|
+
if (ctl.gone) { finish(upError('stopped', 499)); return; }
|
|
3016
|
+
ctl.abort = abort;
|
|
3017
|
+
function go(u, hops) {
|
|
3018
|
+
var pu;
|
|
3019
|
+
try { pu = urllib.parse(u); } catch (e) { finish(upError('bad address', 400)); return; }
|
|
3020
|
+
if ((pu.protocol !== 'http:' && pu.protocol !== 'https:') || !dlHostOk(pu.hostname)) { finish(upError('an address it can\'t reach', 400)); return; }
|
|
3021
|
+
var opts = { hostname: pu.hostname, port: pu.port, path: pu.path, method: 'GET', headers: h };
|
|
3022
|
+
if (pu.protocol === 'https:' && caList().length) { opts.ca = caList(); }
|
|
3023
|
+
var chunks = [], size = 0;
|
|
3024
|
+
try {
|
|
3025
|
+
r = (pu.protocol === 'https:' ? https : http).request(opts, function (resp) {
|
|
3026
|
+
if (done) { resp.resume(); return; }
|
|
3027
|
+
var sc = resp.statusCode;
|
|
3028
|
+
if (sc >= 300 && sc < 400 && resp.headers.location && hops < 4) {
|
|
3029
|
+
resp.resume();
|
|
3030
|
+
var next = urllib.resolve(u, resp.headers.location), np = null;
|
|
3031
|
+
try { np = urllib.parse(next); } catch (e) { np = null; }
|
|
3032
|
+
if (!sameServerRedirect(first, np)) { finish(upError('the portal redirected to another server: not followed (your MAC address stays with it)', 502)); return; }
|
|
3033
|
+
go(next, hops + 1);
|
|
3034
|
+
return;
|
|
3035
|
+
}
|
|
3036
|
+
resp.on('data', function (c) { if (done) { return; } chunks.push(c); size += c.length; if (size > STALKER_MAX) { finish(upError('too large an answer', 502)); } });
|
|
3037
|
+
resp.on('end', function () {
|
|
3038
|
+
if (done) { return; }
|
|
3039
|
+
if (sc !== 200) { finish(upError('HTTP ' + sc, sc === 401 || sc === 403 ? 401 : 502)); return; }
|
|
3040
|
+
decompress(Buffer.concat(chunks, size), resp.headers['content-encoding'], function (e, buf) {
|
|
3041
|
+
if (e) { finish(e); return; }
|
|
3042
|
+
var text = buf.toString('utf8').replace(/^/, ''), j = null;
|
|
3043
|
+
try { j = JSON.parse(text); } catch (e2) { j = null; }
|
|
3044
|
+
if (!j || typeof j !== 'object') {
|
|
3045
|
+
var err = upError('not a Stalker answer' + (text.trim() ? ' (' + text.trim().slice(0, 80) + ')' : ''), 502);
|
|
3046
|
+
err.auth = /authori[sz]ation|unauthori[sz]ed|access denied/i.test(text);
|
|
3047
|
+
finish(err);
|
|
3048
|
+
return;
|
|
3049
|
+
}
|
|
3050
|
+
finish(null, j.js);
|
|
3051
|
+
});
|
|
3052
|
+
});
|
|
3053
|
+
resp.on('error', function (e) { finish(e); });
|
|
3054
|
+
});
|
|
3055
|
+
r.on('error', function (e) { finish(upError('couldn\'t reach it (' + (e.code || e.message) + ')', 502)); });
|
|
3056
|
+
guardSelf(r, function () { finish(upError('an address it can\'t reach', 400)); });
|
|
3057
|
+
r.end();
|
|
3058
|
+
} catch (e) { finish(e); }
|
|
3059
|
+
}
|
|
3060
|
+
go(url, 0);
|
|
3061
|
+
}
|
|
3062
|
+
// A MAG box's profile request: its serial number and device id made from the MAC, as other
|
|
3063
|
+
// players do (some portals check that they're there).
|
|
3064
|
+
function stalkerProfile(mac) {
|
|
3065
|
+
var sn = crypto.createHash('md5').update(mac).digest('hex').toUpperCase().slice(0, 13);
|
|
3066
|
+
var id = crypto.createHash('sha256').update(mac).digest('hex').toUpperCase();
|
|
3067
|
+
return 'type=stb&action=get_profile&hd=1&ver=' + encodeURIComponent('ImageDescription: 0.2.18-r23-250; PORTAL version: 5.6.2; API Version: JS API version: 343; STB API version: 146') +
|
|
3068
|
+
'&num_banks=2&sn=' + sn + '&stb_type=MAG250&client_type=STB&image_version=218&video_out=hdmi&device_id=' + id + '&device_id2=' + id +
|
|
3069
|
+
'&signature=&auth_second_step=1&hw_version=1.7-BD-00¬_valid_token=0&api_signature=262&prehash=';
|
|
3070
|
+
}
|
|
3071
|
+
// The session: its API found (the first that answers a handshake with a token), its profile asked.
|
|
3072
|
+
function stalkerOpen(s, portal, mac, ctl, cb) {
|
|
3073
|
+
var p = stalkerApis(portal), i = 0, why = null;
|
|
3074
|
+
s.base = p.base; s.mac = mac; s.token = '';
|
|
3075
|
+
(function next() {
|
|
3076
|
+
if (i >= p.apis.length) { cb(why || upError('not a Stalker portal at that address', 502)); return; }
|
|
3077
|
+
s.api = p.apis[i++];
|
|
3078
|
+
stalkerAsk(s, 'type=stb&action=handshake&token=&prehash=0', ctl, function (err, js) {
|
|
3079
|
+
if (ctl.gone) { cb(upError('stopped', 499)); return; }
|
|
3080
|
+
if (err || !js || !js.token) { if (!why || /couldn.t reach/.test(why.message)) { why = err || upError('not a Stalker portal at that address', 502); } next(); return; }
|
|
3081
|
+
s.token = String(js.token).slice(0, 200);
|
|
3082
|
+
stalkerAsk(s, stalkerProfile(mac), ctl, function (e2, prof) {
|
|
3083
|
+
if (e2) { s.token = ''; cb(e2); return; }
|
|
3084
|
+
s.block = prof && typeof prof === 'object' && prof.block_msg ? String(prof.block_msg).slice(0, 120) : '';
|
|
3085
|
+
cb(null, s);
|
|
3086
|
+
});
|
|
3087
|
+
});
|
|
3088
|
+
})();
|
|
3089
|
+
}
|
|
3090
|
+
// job(s, done) with the session open; done(err) with err.auth: opened again and job run once more.
|
|
3091
|
+
function stalkerWith(s, portal, mac, ctl, job, cb) {
|
|
3092
|
+
function run(again) {
|
|
3093
|
+
job(s, function (err, v) {
|
|
3094
|
+
if (err && err.auth && !again && !ctl.gone) { s.token = ''; open(true); return; }
|
|
3095
|
+
cb(err, v);
|
|
3096
|
+
});
|
|
3097
|
+
}
|
|
3098
|
+
function open(again) { stalkerOpen(s, portal, mac, ctl, function (err) { if (err) { cb(err); return; } run(again); }); }
|
|
3099
|
+
if (s.token && s.api) { run(false); } else { open(false); }
|
|
3100
|
+
}
|
|
3101
|
+
function stalkerSlim(c) { // (a page's channel: what the playlist takes; thousands of them are held meanwhile)
|
|
3102
|
+
if (!c || typeof c !== 'object') { return null; }
|
|
3103
|
+
return { name: c.name, cmd: c.cmd, cmds: Array.isArray(c.cmds) ? c.cmds.slice(0, 1) : null, logo: c.logo, xmltv_id: c.xmltv_id, number: c.number, tv_genre_id: c.tv_genre_id };
|
|
3104
|
+
}
|
|
3105
|
+
function stalkerChannels(s, ctl, whole, cb) {
|
|
3106
|
+
stalkerAsk(s, 'type=itv&action=get_genres', ctl, function (e0, genres) {
|
|
3107
|
+
if (e0 && e0.auth) { cb(e0); return; }
|
|
3108
|
+
var g = {};
|
|
3109
|
+
if (Array.isArray(genres)) { genres.forEach(function (x) { if (x && x.id != null && x.title) { g[String(x.id)] = String(x.title); } }); }
|
|
3110
|
+
stalkerAsk(s, 'type=itv&action=get_all_channels', ctl, function (e1, js) {
|
|
3111
|
+
if (e1 && e1.auth) { cb(e1); return; }
|
|
3112
|
+
var all = js && Array.isArray(js.data) ? js.data : Array.isArray(js) ? js : [];
|
|
3113
|
+
if (all.length) { cb(null, all.slice(0, STALKER_CHANNELS), g, all.length > STALKER_CHANNELS ? { total: all.length } : null); return; }
|
|
3114
|
+
var page = 1, tries = 0;
|
|
3115
|
+
(function more() {
|
|
3116
|
+
stalkerAsk(s, 'type=itv&action=get_ordered_list&genre=*&fav=0&sortby=number&hd=0&p=' + page, ctl, function (e2, o) {
|
|
3117
|
+
if (e2 && (!all.length || (whole && e2.auth))) { cb(e2); return; } // (whole: a session gone midway read again, once: stalkerWith)
|
|
3118
|
+
if (e2 && whole && !ctl.gone && tries < STALKER_RETRIES) { tries++; setTimeout(more, STALKER_RETRY_MS); return; }
|
|
3119
|
+
tries = 0;
|
|
3120
|
+
var d = !e2 && o && Array.isArray(o.data) ? o.data : [], total = +(o && o.total_items) || 0, room = STALKER_CHANNELS - all.length;
|
|
3121
|
+
d.slice(0, room).forEach(function (c) { var x = stalkerSlim(c); if (x) { all.push(x); } });
|
|
3122
|
+
var got = total && all.length >= total, full = all.length >= STALKER_CHANNELS, last = !whole && page >= STALKER_PAGES;
|
|
3123
|
+
if (!d.length || got || full || last) {
|
|
3124
|
+
var cut = e2 || (!got && d.length && (full || last)) ? { total: total, why: e2 ? (e2.message || 'failed') : '' } : null;
|
|
3125
|
+
cb(null, all, g, cut);
|
|
3126
|
+
return;
|
|
3127
|
+
}
|
|
3128
|
+
page++;
|
|
3129
|
+
more();
|
|
3130
|
+
});
|
|
3131
|
+
})();
|
|
3132
|
+
});
|
|
3133
|
+
});
|
|
3134
|
+
}
|
|
3135
|
+
function stalkerM3u(s, list, genres) {
|
|
3136
|
+
function clean(v) { return String(v == null ? '' : v).replace(/[\r\n"]+/g, ' ').trim().slice(0, 300); }
|
|
3137
|
+
var out = ['#EXTM3U'], cmds = {}, n = 0;
|
|
3138
|
+
list.forEach(function (c) {
|
|
3139
|
+
if (!c || typeof c !== 'object') { return; }
|
|
3140
|
+
var cmd = String(c.cmd || (Array.isArray(c.cmds) && c.cmds[0] && c.cmds[0].url) || ''), m = /(https?:\/\/[^\s"]+)/i.exec(cmd);
|
|
3141
|
+
if (!m || m[1].length > 1000) { return; }
|
|
3142
|
+
cmds[m[1]] = cmd.slice(0, 2000);
|
|
3143
|
+
var logo = clean(c.logo);
|
|
3144
|
+
if (logo && !/^https?:\/\//i.test(logo)) { logo = s.base + '/misc/logos/320/' + logo.replace(/^\/+/, ''); }
|
|
3145
|
+
out.push('#EXTINF:-1' + (c.xmltv_id ? ' tvg-id="' + clean(c.xmltv_id) + '"' : '') + (logo ? ' tvg-logo="' + logo + '"' : '') + (c.number ? ' tvg-chno="' + clean(c.number) + '"' : '') +
|
|
3146
|
+
' group-title="' + clean(genres[String(c.tv_genre_id)] || 'Live TV') + '",' + (clean(c.name).replace(/,/g, ' ·') || 'Channel'));
|
|
3147
|
+
out.push(m[1]);
|
|
3148
|
+
n++;
|
|
3149
|
+
});
|
|
3150
|
+
s.cmds = cmds;
|
|
3151
|
+
return { m3u: out.join('\n') + '\n', count: n };
|
|
3152
|
+
}
|
|
3153
|
+
function stalkerControl(req, res, path, query, leaving) {
|
|
3154
|
+
if (!entryAllowed(req, query)) { send(res, 403, 'text/plain', 'not allowed (token)'); return; }
|
|
3155
|
+
var q = {};
|
|
3156
|
+
(query || '').split('&').forEach(function (kv) { var i = kv.indexOf('='); if (i > 0) { try { q[kv.slice(0, i)] = decodeURIComponent(kv.slice(i + 1)); } catch (e) {} } });
|
|
3157
|
+
var portal = String(q.portal || '').trim(), mac = String(q.mac || '').toUpperCase();
|
|
3158
|
+
if (!/^https?:\/\/[^\s]+$/i.test(portal) || portal.length > 500) { send(res, 400, 'text/plain', 'bad address'); return; }
|
|
3159
|
+
if (!/^[0-9A-F]{2}(:[0-9A-F]{2}){5}$/.test(mac)) { send(res, 400, 'text/plain', 'bad MAC address'); return; }
|
|
3160
|
+
var key = portal + '|' + mac, s = stalkerKeep(key), ctl = { gone: false, abort: null };
|
|
3161
|
+
leaving.push(function () { ctl.gone = true; if (ctl.abort) { ctl.abort(); } });
|
|
3162
|
+
function json(o) { send(res, 200, 'application/json', JSON.stringify(o)); }
|
|
3163
|
+
if (path === '/stalker/link') {
|
|
3164
|
+
var u = String(q.u || '');
|
|
3165
|
+
if (!/^https?:\/\/[^\s]+$/i.test(u) || u.length > 2000) { send(res, 400, 'text/plain', 'bad link'); return; }
|
|
3166
|
+
stalkerWith(s, portal, mac, ctl, function (ss, done) {
|
|
3167
|
+
var cmd = ss.cmds[u] || 'ffmpeg ' + u; // (a session opened after a restart: as most portals write it)
|
|
3168
|
+
stalkerAsk(ss, 'type=itv&action=create_link&cmd=' + encodeURIComponent(cmd) + '&series=&forced_storage=0&disable_ad=0&download=0&force_ch_link_check=0', ctl, function (err, js) {
|
|
3169
|
+
if (err) { done(err); return; }
|
|
3170
|
+
var m = /(https?:\/\/[^\s"]+)/i.exec(String((js && js.cmd) || ''));
|
|
3171
|
+
done(m ? null : upError('the portal gave no link for this channel', 502), m ? m[1] : null);
|
|
3172
|
+
});
|
|
3173
|
+
}, function (err, link) { json(err ? { why: err.message || 'failed' } : { url: link }); });
|
|
3174
|
+
return;
|
|
3175
|
+
}
|
|
3176
|
+
var whole = q.all === '1', sock = req.socket;
|
|
3177
|
+
if (whole && sock && sock.setTimeout) { // (a read of many minutes: Node's two idle minutes would end it)
|
|
3178
|
+
sock.setTimeout(0);
|
|
3179
|
+
if (res.on) { res.on('finish', function () { sock.setTimeout(120000); }); }
|
|
3180
|
+
}
|
|
3181
|
+
function listed(l) { var o = { m3u: l.m3u, count: l.count }; if (l.cut) { o.cut = l.cut; } return o; }
|
|
3182
|
+
if (s.list && Date.now() - s.list.at < STALKER_LIST_MS && (s.list.whole || !whole)) { json(listed(s.list)); return; }
|
|
3183
|
+
stalkerWith(s, portal, mac, ctl, function (ss, done) {
|
|
3184
|
+
stalkerChannels(ss, ctl, whole, function (err, list, genres, cut) {
|
|
3185
|
+
var m = err ? null : stalkerM3u(ss, list, genres);
|
|
3186
|
+
if (m && cut) { m.cut = cut; }
|
|
3187
|
+
done(err, m);
|
|
3188
|
+
});
|
|
3189
|
+
}, function (err, r) {
|
|
3190
|
+
if (err) {
|
|
3191
|
+
var refused = err.auth || err.status === 401 || /^not a Stalker answer/.test(err.message || '') && !!s.token;
|
|
3192
|
+
json(refused ? { refused: true, why: s.block || 'the portal refused that MAC address' } : { unreachable: true, why: err.message || 'failed' });
|
|
3193
|
+
return;
|
|
3194
|
+
}
|
|
3195
|
+
if (!r.count) { json({ refused: true, why: s.block || 'the portal listed no channels for that MAC address' }); return; }
|
|
3196
|
+
s.list = { at: Date.now(), m3u: r.m3u, count: r.count, cut: r.cut || null, whole: whole };
|
|
3197
|
+
json(listed(s.list));
|
|
3198
|
+
});
|
|
3199
|
+
}
|
|
3200
|
+
|
|
2670
3201
|
// ---- TV guide (/epg/load, /epg/get) ----
|
|
2671
3202
|
// A playlist's XMLTV guide, read for StremioK's channels only. The app sends their keys (epgKey:
|
|
2672
3203
|
// "i:" and a tvg-id, "n:" and a name without capitals, accents, spaces or HD / FHD / 4K tags) as
|
|
@@ -2905,6 +3436,7 @@ function epgFile(url) { var d = tokenDir(); return d ? d + '/epg-' + epgHash(url
|
|
|
2905
3436
|
// EPG_KEEP_MS ago, of any guide, is deleted (looked at once, as the first guide is asked for).
|
|
2906
3437
|
var EPG_FILE_MAX = 32 * 1048576, EPG_KEEP_MS = 2 * 86400000, _epgSwept = false;
|
|
2907
3438
|
function epgSweep() {
|
|
3439
|
+
xtvSweep();
|
|
2908
3440
|
if (_epgSwept) { return; }
|
|
2909
3441
|
_epgSwept = true;
|
|
2910
3442
|
var d = tokenDir(), fs = require('fs');
|
|
@@ -2997,21 +3529,22 @@ function hasTicket(query) {
|
|
|
2997
3529
|
}
|
|
2998
3530
|
// Settings › Storage: the saved guides (/epg/files: how many, their size), and Clear saved
|
|
2999
3531
|
// copies (/epg/clear, a POST: every saved guide deleted, and those kept here let go).
|
|
3000
|
-
function epgSavedFiles() {
|
|
3532
|
+
function epgSavedFiles(re) {
|
|
3001
3533
|
var d = tokenDir(), fs = require('fs'), out = { count: 0, bytes: 0 };
|
|
3002
3534
|
if (!d) { return out; }
|
|
3003
3535
|
try {
|
|
3004
|
-
fs.readdirSync(d).filter(function (n) { return /^epg-[0-9a-z]+\.json(\.part)
|
|
3536
|
+
fs.readdirSync(d).filter(function (n) { return (re || /^epg-[0-9a-z]+\.json(\.part)?$/).test(n); }).forEach(function (n) {
|
|
3005
3537
|
try { var st = fs.statSync(d + '/' + n); out.count++; out.bytes += st.size; out.names = (out.names || []).concat(n); } catch (e) {}
|
|
3006
3538
|
});
|
|
3007
3539
|
} catch (e) {}
|
|
3008
3540
|
return out;
|
|
3009
3541
|
}
|
|
3010
3542
|
function epgClear() {
|
|
3011
|
-
var d = tokenDir(), files = epgSavedFiles();
|
|
3012
|
-
(files.names || []).forEach(function (n) { try { require('fs').unlinkSync(d + '/' + n); } catch (e) {} });
|
|
3543
|
+
var d = tokenDir(), files = epgSavedFiles(), lists = epgSavedFiles(XTV_FILES);
|
|
3544
|
+
(files.names || []).concat(lists.names || []).forEach(function (n) { try { require('fs').unlinkSync(d + '/' + n); } catch (e) {} });
|
|
3013
3545
|
_epg = Object.create(null); _epgQueue = []; // (one being read: not saved, see epgPump)
|
|
3014
|
-
|
|
3546
|
+
_xtv = Object.create(null); _xtvQueue = []; // (the Xtream lists: read again when asked for)
|
|
3547
|
+
return files.count + lists.count;
|
|
3015
3548
|
}
|
|
3016
3549
|
function epgControl(req, res, path, query) {
|
|
3017
3550
|
if (!entryAllowed(req, query) && !(req.method === 'POST' && hasTicket(query))) { send(res, 403, 'text/plain', 'not allowed (token)'); return; } // (the guide ticket: its POSTs, see hasTicket)
|
|
@@ -3020,7 +3553,7 @@ function epgControl(req, res, path, query) {
|
|
|
3020
3553
|
send(res, 200, 'application/json', JSON.stringify({ ticket: _epgTicket }));
|
|
3021
3554
|
return;
|
|
3022
3555
|
}
|
|
3023
|
-
if (path === '/epg/files') { var sf = epgSavedFiles(); send(res, 200, 'application/json', JSON.stringify({ count: sf.count, bytes: sf.bytes })); return; }
|
|
3556
|
+
if (path === '/epg/files') { var sf = epgSavedFiles(), xf = epgSavedFiles(XTV_FILES); send(res, 200, 'application/json', JSON.stringify({ count: sf.count, bytes: sf.bytes, xtv: { count: xf.count, bytes: xf.bytes } })); return; }
|
|
3024
3557
|
if (path === '/epg/clear') {
|
|
3025
3558
|
if (req.method !== 'POST') { send(res, 405, 'text/plain', 'POST only'); return; }
|
|
3026
3559
|
send(res, 200, 'application/json', JSON.stringify({ cleared: epgClear() }));
|
|
@@ -3081,8 +3614,8 @@ var ENTRY_PORT = PORT + 10, ENTRY_MS = 5 * 60 * 1000, ENTRY_IDLE_MS = 30000, ENT
|
|
|
3081
3614
|
var ENTRY_OPEN_MAX = 2, ENTRY_OPENS = 12, ENTRY_CONNECTIONS = 8, ENTRY_VISIT_MS = 20000, ENTRY_QUIET_MS = 10000;
|
|
3082
3615
|
var ENTRY_TITLES = { link: 'Play a link on StremioK', addon: 'Add an addon to StremioK', playlist: 'Add an IPTV playlist to StremioK', config: 'Import a configuration file into StremioK',
|
|
3083
3616
|
search: 'Search on StremioK', youtube: 'Add a YouTube catalog to StremioK', aiom: 'Your AIOManager address for StremioK', aiomkey: 'Your AIOManager API key for StremioK',
|
|
3084
|
-
server: 'Your streaming server’s address for StremioK' };
|
|
3085
|
-
var ENTRY_TEXT = { search: 'Words to search for', youtube: 'A channel, a playlist link, or words to search YouTube for', aiomkey: 'Your account API key' }; // (plain text, not a link)
|
|
3617
|
+
server: 'Your streaming server’s address for StremioK', mac: 'Your Stalker portal’s MAC address for StremioK' };
|
|
3618
|
+
var ENTRY_TEXT = { search: 'Words to search for', youtube: 'A channel, a playlist link, or words to search YouTube for', aiomkey: 'Your account API key', mac: 'Its MAC address (00:1A:79:…)' }; // (plain text, not a link)
|
|
3086
3619
|
var _entries = Object.create(null), _entryServer = null, _entrySockets = [], _entryOpened = [];
|
|
3087
3620
|
function lanAddress() {
|
|
3088
3621
|
try {
|