stremiok 2.2.1 → 2.2.3

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/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; /epg/clear (POST) -> { cleared }: deleted
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)
@@ -1589,6 +1600,29 @@ function openPart(fs, part, append, cb) {
1589
1600
  });
1590
1601
  });
1591
1602
  }
1603
+ // A partial download's version tag (ETag / Last-Modified), for resuming it after a restart: kept
1604
+ // in StremioK's own folder by the partial file's path (DL_VALIDATORS at most, the oldest dropped),
1605
+ // never on the drive, where a prepared file could be a link elsewhere (read: its contents sent
1606
+ // as If-Range; written: overwritten). None to keep it in (TizenBrew): downloads are closed there.
1607
+ var DL_VALIDATORS = 64;
1608
+ function dlValFile() { var d = tokenDir(); return d ? d + '/dl-validators.json' : null; }
1609
+ function dlValAll() {
1610
+ var f = dlValFile(), o = null;
1611
+ if (!f) { return {}; }
1612
+ try { o = JSON.parse(require('fs').readFileSync(f, 'utf8')); } catch (e) { o = null; }
1613
+ return o && typeof o === 'object' && !Array.isArray(o) ? o : {};
1614
+ }
1615
+ function dlValGet(part) { var v = dlValAll()[part]; return typeof v === 'string' ? v.slice(0, 300) : ''; }
1616
+ function dlValSet(part, v) {
1617
+ var f = dlValFile();
1618
+ if (!f) { return; }
1619
+ var all = dlValAll();
1620
+ delete all[part];
1621
+ if (v) { all[part] = String(v).slice(0, 300); }
1622
+ var keys = Object.keys(all);
1623
+ while (keys.length > DL_VALIDATORS) { delete all[keys.shift()]; }
1624
+ try { require('fs').writeFileSync(f, JSON.stringify(all), { mode: 384 }); } catch (e) { log('download version not kept: ' + (e && (e.code || e.message))); }
1625
+ }
1592
1626
  function dlRun(e) {
1593
1627
  var fs = require('fs'), part = e.dir + '/' + e.name + '.part';
1594
1628
  e.state = 'downloading'; e.err = '';
@@ -1601,17 +1635,17 @@ function dlRun(e) {
1601
1635
  fs.mkdir(e.dir, function () {
1602
1636
  fsRealOk(e.dir, function (bad) {
1603
1637
  if (bad) { dlEnd(e, 'failed', 'the folder: ' + bad.message); return; }
1604
- fs.stat(part, function (se, st) {
1638
+ fs.lstat(part, function (se, st) { // (not through a link: a partial file is a regular one, see openPart)
1639
+ if (!se && !st.isFile()) { dlEnd(e, 'failed', 'the partial file isn\'t a regular file'); return; }
1605
1640
  var have = se ? 0 : st.size;
1606
1641
  if (!have || e.answered) { get(e.url, 0, have); return; } // (this one's own, from this run: picked up as it was)
1607
- fs.readFile(part + '.v', 'utf8', function (ve, v) { // (a partial file from before a restart: resumed only with its validator)
1608
- e.validator = ve ? '' : String(v).trim().slice(0, 300);
1609
- if (e.validator) { get(e.url, 0, have); } else { dropPart(function () { get(e.url, 0, 0); }); }
1610
- });
1642
+ e.validator = dlValGet(part); // (a partial file from before a restart: resumed only with its version kept)
1643
+ if (e.validator) { get(e.url, 0, have); } else { dropPart(function () { get(e.url, 0, 0); }); }
1611
1644
  });
1612
1645
  });
1613
1646
  });
1614
- function dropPart(then) { fs.unlink(part + '.v', function () { fs.unlink(part, function () { then(); }); }); }
1647
+ // (….part.v: where 2.1.17–2.2.1 kept the version, removed; a link removed, not what it points to)
1648
+ function dropPart(then) { dlValSet(part, ''); fs.unlink(part + '.v', function () { fs.unlink(part, function () { then(); }); }); }
1615
1649
  function get(u, hops, have) {
1616
1650
  if (e.state !== 'downloading') { return; }
1617
1651
  var pu = urllib.parse(u), mod = pu.protocol === 'https:' ? https : http;
@@ -1647,7 +1681,7 @@ function dlRun(e) {
1647
1681
  if (!resumed) { // (asked again on a resume: If-Range; kept beside the partial file, for one after a restart)
1648
1682
  var et = resp.headers.etag;
1649
1683
  e.validator = et && !/^W\//.test(et) ? et : (resp.headers['last-modified'] || '');
1650
- fs.writeFile(part + '.v', e.validator, { mode: 384 }, function () {});
1684
+ dlValSet(part, e.validator);
1651
1685
  }
1652
1686
  if (isPlaylistAnswer(resp.headers['content-type'], '')) { resp.resume(); closed = true; dlEnd(e, 'failed', 'a streamed playlist (HLS or DASH), not a file: it can\'t be downloaded'); return; }
1653
1687
  var total = 0, cr = /\/(\d+)\s*$/.exec(resp.headers['content-range'] || '');
@@ -1715,7 +1749,7 @@ function dlRun(e) {
1715
1749
  fs.rename(part, final, function (re) {
1716
1750
  if (re) { dlEnd(e, 'failed', 'couldn\'t finish the file: ' + (re.code || re.message)); return; }
1717
1751
  e.name = final.split('/').pop(); e.tries = 0;
1718
- fs.unlink(part + '.v', function () {});
1752
+ dlValSet(part, ''); fs.unlink(part + '.v', function () {});
1719
1753
  dlEnd(e, 'done');
1720
1754
  });
1721
1755
  });
@@ -1783,7 +1817,9 @@ function handle(req, res) {
1783
1817
  return;
1784
1818
  }
1785
1819
  if (/^\/epg\//.test(path)) { epgControl(req, res, path, parts[1]); return; } // (a playlist's TV guide: see epgControl)
1786
- if (path === '/aiom/addons') { aiomControl(req, res, parts[1], leaving); return; } // (AIOManager's addons: see aiomControl) // (a live channel's sound repacked: see liveControl) // (the phone's one-time page: see below)
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)
1787
1823
  if (path === '/fsfile') { // a USB file over HTTP, with Range: the player reads a big one from here (see fsfile in the header)
1788
1824
  var whyF = fsAllowed(req, parts[1]);
1789
1825
  if (whyF) { send(res, 403, 'text/plain', 'not allowed (' + whyF + ')'); return; }
@@ -2644,6 +2680,502 @@ function aiomControl(req, res, query, leaving) {
2644
2680
  }));
2645
2681
  }
2646
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) as an
2979
+ // M3U playlist, genres as groups, xmltv_id as tvg-id, each channel's link the one in its cmd (often
2980
+ // a stand-in, http://localhost/ch/123_), kept STALKER_LIST_MS (adding a portal just checked reads it
2981
+ // once). link: the link to play now (create_link, with the channel's own cmd). A session whose
2982
+ // token the portal no longer takes is opened again, once. Redirects only on the same server.
2983
+ 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';
2984
+ var STALKER_MS = 20000, STALKER_MAX = 16 * 1048576, STALKER_PAGES = 100, STALKER_CHANNELS = 30000, STALKER_LIST_MS = 2 * 60000, STALKER_KEEP = 4;
2985
+ var _stalker = Object.create(null); // portal|mac -> { api, base, mac, token, cmds: { link: cmd }, list: { at, m3u, count }, used }
2986
+ function stalkerKeep(key) {
2987
+ var s = _stalker[key];
2988
+ if (!s) {
2989
+ var keys = Object.keys(_stalker);
2990
+ if (keys.length >= STALKER_KEEP) { keys.sort(function (a, b) { return _stalker[a].used - _stalker[b].used; }); delete _stalker[keys[0]]; }
2991
+ s = _stalker[key] = { token: '', cmds: {}, list: null };
2992
+ }
2993
+ s.used = Date.now();
2994
+ return s;
2995
+ }
2996
+ function stalkerApis(portal) {
2997
+ var b = portal.replace(/[?#].*$/, '').replace(/\/+$/, '').replace(/\/(portal|load)\.php$/i, '').replace(/\/(c|server)$/i, '').replace(/\/+$/, '');
2998
+ var apis = [b + '/portal.php', b + '/server/load.php'];
2999
+ if (!/\/stalker_portal$/i.test(b)) { apis.push(b + '/stalker_portal/server/load.php'); }
3000
+ return { base: b, apis: apis };
3001
+ }
3002
+ // One GET to the portal: cb(err, js) with its answer's "js"; an answer that isn't JSON (a portal
3003
+ // that refuses the session says "Authorization failed.") is an error, err.auth when it says so.
3004
+ function stalkerAsk(s, params, ctl, cb) {
3005
+ cb = once(cb);
3006
+ var url = s.api + '?' + params + '&JsHttpRequest=1-xml', first = null, done = false;
3007
+ try { first = urllib.parse(url); } catch (e) { first = null; }
3008
+ var h = { 'User-Agent': STALKER_UA, 'X-User-Agent': 'Model: MAG250; Link: WiFi', 'Accept': '*/*', 'Accept-Encoding': 'gzip',
3009
+ 'Cookie': 'mac=' + s.mac + '; stb_lang=en; timezone=UTC', 'Referer': s.base + '/c/' };
3010
+ if (s.token) { h.Authorization = 'Bearer ' + s.token; }
3011
+ var timer = setTimeout(function () { finish(upError('the portal didn\'t answer', 504)); }, STALKER_MS), r = null;
3012
+ 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); }
3013
+ function abort() { finish(upError('stopped', 499)); }
3014
+ if (ctl.gone) { finish(upError('stopped', 499)); return; }
3015
+ ctl.abort = abort;
3016
+ function go(u, hops) {
3017
+ var pu;
3018
+ try { pu = urllib.parse(u); } catch (e) { finish(upError('bad address', 400)); return; }
3019
+ if ((pu.protocol !== 'http:' && pu.protocol !== 'https:') || !dlHostOk(pu.hostname)) { finish(upError('an address it can\'t reach', 400)); return; }
3020
+ var opts = { hostname: pu.hostname, port: pu.port, path: pu.path, method: 'GET', headers: h };
3021
+ if (pu.protocol === 'https:' && caList().length) { opts.ca = caList(); }
3022
+ var chunks = [], size = 0;
3023
+ try {
3024
+ r = (pu.protocol === 'https:' ? https : http).request(opts, function (resp) {
3025
+ if (done) { resp.resume(); return; }
3026
+ var sc = resp.statusCode;
3027
+ if (sc >= 300 && sc < 400 && resp.headers.location && hops < 4) {
3028
+ resp.resume();
3029
+ var next = urllib.resolve(u, resp.headers.location), np = null;
3030
+ try { np = urllib.parse(next); } catch (e) { np = null; }
3031
+ if (!sameServerRedirect(first, np)) { finish(upError('the portal redirected to another server: not followed (your MAC address stays with it)', 502)); return; }
3032
+ go(next, hops + 1);
3033
+ return;
3034
+ }
3035
+ 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)); } });
3036
+ resp.on('end', function () {
3037
+ if (done) { return; }
3038
+ if (sc !== 200) { finish(upError('HTTP ' + sc, sc === 401 || sc === 403 ? 401 : 502)); return; }
3039
+ decompress(Buffer.concat(chunks, size), resp.headers['content-encoding'], function (e, buf) {
3040
+ if (e) { finish(e); return; }
3041
+ var text = buf.toString('utf8').replace(/^/, ''), j = null;
3042
+ try { j = JSON.parse(text); } catch (e2) { j = null; }
3043
+ if (!j || typeof j !== 'object') {
3044
+ var err = upError('not a Stalker answer' + (text.trim() ? ' (' + text.trim().slice(0, 80) + ')' : ''), 502);
3045
+ err.auth = /authori[sz]ation|unauthori[sz]ed|access denied/i.test(text);
3046
+ finish(err);
3047
+ return;
3048
+ }
3049
+ finish(null, j.js);
3050
+ });
3051
+ });
3052
+ resp.on('error', function (e) { finish(e); });
3053
+ });
3054
+ r.on('error', function (e) { finish(upError('couldn\'t reach it (' + (e.code || e.message) + ')', 502)); });
3055
+ guardSelf(r, function () { finish(upError('an address it can\'t reach', 400)); });
3056
+ r.end();
3057
+ } catch (e) { finish(e); }
3058
+ }
3059
+ go(url, 0);
3060
+ }
3061
+ // A MAG box's profile request: its serial number and device id made from the MAC, as other
3062
+ // players do (some portals check that they're there).
3063
+ function stalkerProfile(mac) {
3064
+ var sn = crypto.createHash('md5').update(mac).digest('hex').toUpperCase().slice(0, 13);
3065
+ var id = crypto.createHash('sha256').update(mac).digest('hex').toUpperCase();
3066
+ 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') +
3067
+ '&num_banks=2&sn=' + sn + '&stb_type=MAG250&client_type=STB&image_version=218&video_out=hdmi&device_id=' + id + '&device_id2=' + id +
3068
+ '&signature=&auth_second_step=1&hw_version=1.7-BD-00&not_valid_token=0&api_signature=262&prehash=';
3069
+ }
3070
+ // The session: its API found (the first that answers a handshake with a token), its profile asked.
3071
+ function stalkerOpen(s, portal, mac, ctl, cb) {
3072
+ var p = stalkerApis(portal), i = 0, why = null;
3073
+ s.base = p.base; s.mac = mac; s.token = '';
3074
+ (function next() {
3075
+ if (i >= p.apis.length) { cb(why || upError('not a Stalker portal at that address', 502)); return; }
3076
+ s.api = p.apis[i++];
3077
+ stalkerAsk(s, 'type=stb&action=handshake&token=&prehash=0', ctl, function (err, js) {
3078
+ if (ctl.gone) { cb(upError('stopped', 499)); return; }
3079
+ 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; }
3080
+ s.token = String(js.token).slice(0, 200);
3081
+ stalkerAsk(s, stalkerProfile(mac), ctl, function (e2, prof) {
3082
+ if (e2) { s.token = ''; cb(e2); return; }
3083
+ s.block = prof && typeof prof === 'object' && prof.block_msg ? String(prof.block_msg).slice(0, 120) : '';
3084
+ cb(null, s);
3085
+ });
3086
+ });
3087
+ })();
3088
+ }
3089
+ // job(s, done) with the session open; done(err) with err.auth: opened again and job run once more.
3090
+ function stalkerWith(s, portal, mac, ctl, job, cb) {
3091
+ function run(again) {
3092
+ job(s, function (err, v) {
3093
+ if (err && err.auth && !again && !ctl.gone) { s.token = ''; open(true); return; }
3094
+ cb(err, v);
3095
+ });
3096
+ }
3097
+ function open(again) { stalkerOpen(s, portal, mac, ctl, function (err) { if (err) { cb(err); return; } run(again); }); }
3098
+ if (s.token && s.api) { run(false); } else { open(false); }
3099
+ }
3100
+ function stalkerChannels(s, ctl, cb) {
3101
+ stalkerAsk(s, 'type=itv&action=get_genres', ctl, function (e0, genres) {
3102
+ if (e0 && e0.auth) { cb(e0); return; }
3103
+ var g = {};
3104
+ if (Array.isArray(genres)) { genres.forEach(function (x) { if (x && x.id != null && x.title) { g[String(x.id)] = String(x.title); } }); }
3105
+ stalkerAsk(s, 'type=itv&action=get_all_channels', ctl, function (e1, js) {
3106
+ if (e1 && e1.auth) { cb(e1); return; }
3107
+ var all = js && Array.isArray(js.data) ? js.data : Array.isArray(js) ? js : [];
3108
+ if (all.length) { cb(null, all.slice(0, STALKER_CHANNELS), g); return; }
3109
+ var page = 1;
3110
+ (function more() {
3111
+ stalkerAsk(s, 'type=itv&action=get_ordered_list&genre=*&fav=0&sortby=number&hd=0&p=' + page, ctl, function (e2, o) {
3112
+ if (e2 && !all.length) { cb(e2); return; }
3113
+ var d = !e2 && o && Array.isArray(o.data) ? o.data : [], total = +(o && o.total_items) || 0;
3114
+ all = all.concat(d);
3115
+ if (!d.length || page >= STALKER_PAGES || (total && all.length >= total) || all.length >= STALKER_CHANNELS) { cb(null, all.slice(0, STALKER_CHANNELS), g); return; }
3116
+ page++;
3117
+ more();
3118
+ });
3119
+ })();
3120
+ });
3121
+ });
3122
+ }
3123
+ function stalkerM3u(s, list, genres) {
3124
+ function clean(v) { return String(v == null ? '' : v).replace(/[\r\n"]+/g, ' ').trim().slice(0, 300); }
3125
+ var out = ['#EXTM3U'], cmds = {}, n = 0;
3126
+ list.forEach(function (c) {
3127
+ if (!c || typeof c !== 'object') { return; }
3128
+ var cmd = String(c.cmd || (Array.isArray(c.cmds) && c.cmds[0] && c.cmds[0].url) || ''), m = /(https?:\/\/[^\s"]+)/i.exec(cmd);
3129
+ if (!m || m[1].length > 1000) { return; }
3130
+ cmds[m[1]] = cmd.slice(0, 2000);
3131
+ var logo = clean(c.logo);
3132
+ if (logo && !/^https?:\/\//i.test(logo)) { logo = s.base + '/misc/logos/320/' + logo.replace(/^\/+/, ''); }
3133
+ out.push('#EXTINF:-1' + (c.xmltv_id ? ' tvg-id="' + clean(c.xmltv_id) + '"' : '') + (logo ? ' tvg-logo="' + logo + '"' : '') + (c.number ? ' tvg-chno="' + clean(c.number) + '"' : '') +
3134
+ ' group-title="' + clean(genres[String(c.tv_genre_id)] || 'Live TV') + '",' + (clean(c.name).replace(/,/g, ' ·') || 'Channel'));
3135
+ out.push(m[1]);
3136
+ n++;
3137
+ });
3138
+ s.cmds = cmds;
3139
+ return { m3u: out.join('\n') + '\n', count: n };
3140
+ }
3141
+ function stalkerControl(req, res, path, query, leaving) {
3142
+ if (!entryAllowed(req, query)) { send(res, 403, 'text/plain', 'not allowed (token)'); return; }
3143
+ var q = {};
3144
+ (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) {} } });
3145
+ var portal = String(q.portal || '').trim(), mac = String(q.mac || '').toUpperCase();
3146
+ if (!/^https?:\/\/[^\s]+$/i.test(portal) || portal.length > 500) { send(res, 400, 'text/plain', 'bad address'); return; }
3147
+ if (!/^[0-9A-F]{2}(:[0-9A-F]{2}){5}$/.test(mac)) { send(res, 400, 'text/plain', 'bad MAC address'); return; }
3148
+ var key = portal + '|' + mac, s = stalkerKeep(key), ctl = { gone: false, abort: null };
3149
+ leaving.push(function () { ctl.gone = true; if (ctl.abort) { ctl.abort(); } });
3150
+ function json(o) { send(res, 200, 'application/json', JSON.stringify(o)); }
3151
+ if (path === '/stalker/link') {
3152
+ var u = String(q.u || '');
3153
+ if (!/^https?:\/\/[^\s]+$/i.test(u) || u.length > 2000) { send(res, 400, 'text/plain', 'bad link'); return; }
3154
+ stalkerWith(s, portal, mac, ctl, function (ss, done) {
3155
+ var cmd = ss.cmds[u] || 'ffmpeg ' + u; // (a session opened after a restart: as most portals write it)
3156
+ 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) {
3157
+ if (err) { done(err); return; }
3158
+ var m = /(https?:\/\/[^\s"]+)/i.exec(String((js && js.cmd) || ''));
3159
+ done(m ? null : upError('the portal gave no link for this channel', 502), m ? m[1] : null);
3160
+ });
3161
+ }, function (err, link) { json(err ? { why: err.message || 'failed' } : { url: link }); });
3162
+ return;
3163
+ }
3164
+ if (s.list && Date.now() - s.list.at < STALKER_LIST_MS) { json({ m3u: s.list.m3u, count: s.list.count }); return; }
3165
+ stalkerWith(s, portal, mac, ctl, function (ss, done) {
3166
+ stalkerChannels(ss, ctl, function (err, list, genres) { done(err, err ? null : stalkerM3u(ss, list, genres)); });
3167
+ }, function (err, r) {
3168
+ if (err) {
3169
+ var refused = err.auth || err.status === 401 || /^not a Stalker answer/.test(err.message || '') && !!s.token;
3170
+ json(refused ? { refused: true, why: s.block || 'the portal refused that MAC address' } : { unreachable: true, why: err.message || 'failed' });
3171
+ return;
3172
+ }
3173
+ if (!r.count) { json({ refused: true, why: s.block || 'the portal listed no channels for that MAC address' }); return; }
3174
+ s.list = { at: Date.now(), m3u: r.m3u, count: r.count };
3175
+ json({ m3u: r.m3u, count: r.count });
3176
+ });
3177
+ }
3178
+
2647
3179
  // ---- TV guide (/epg/load, /epg/get) ----
2648
3180
  // A playlist's XMLTV guide, read for StremioK's channels only. The app sends their keys (epgKey:
2649
3181
  // "i:" and a tvg-id, "n:" and a name without capitals, accents, spaces or HD / FHD / 4K tags) as
@@ -2882,6 +3414,7 @@ function epgFile(url) { var d = tokenDir(); return d ? d + '/epg-' + epgHash(url
2882
3414
  // EPG_KEEP_MS ago, of any guide, is deleted (looked at once, as the first guide is asked for).
2883
3415
  var EPG_FILE_MAX = 32 * 1048576, EPG_KEEP_MS = 2 * 86400000, _epgSwept = false;
2884
3416
  function epgSweep() {
3417
+ xtvSweep();
2885
3418
  if (_epgSwept) { return; }
2886
3419
  _epgSwept = true;
2887
3420
  var d = tokenDir(), fs = require('fs');
@@ -2974,21 +3507,22 @@ function hasTicket(query) {
2974
3507
  }
2975
3508
  // Settings › Storage: the saved guides (/epg/files: how many, their size), and Clear saved
2976
3509
  // copies (/epg/clear, a POST: every saved guide deleted, and those kept here let go).
2977
- function epgSavedFiles() {
3510
+ function epgSavedFiles(re) {
2978
3511
  var d = tokenDir(), fs = require('fs'), out = { count: 0, bytes: 0 };
2979
3512
  if (!d) { return out; }
2980
3513
  try {
2981
- fs.readdirSync(d).filter(function (n) { return /^epg-[0-9a-z]+\.json(\.part)?$/.test(n); }).forEach(function (n) {
3514
+ fs.readdirSync(d).filter(function (n) { return (re || /^epg-[0-9a-z]+\.json(\.part)?$/).test(n); }).forEach(function (n) {
2982
3515
  try { var st = fs.statSync(d + '/' + n); out.count++; out.bytes += st.size; out.names = (out.names || []).concat(n); } catch (e) {}
2983
3516
  });
2984
3517
  } catch (e) {}
2985
3518
  return out;
2986
3519
  }
2987
3520
  function epgClear() {
2988
- var d = tokenDir(), files = epgSavedFiles();
2989
- (files.names || []).forEach(function (n) { try { require('fs').unlinkSync(d + '/' + n); } catch (e) {} });
3521
+ var d = tokenDir(), files = epgSavedFiles(), lists = epgSavedFiles(XTV_FILES);
3522
+ (files.names || []).concat(lists.names || []).forEach(function (n) { try { require('fs').unlinkSync(d + '/' + n); } catch (e) {} });
2990
3523
  _epg = Object.create(null); _epgQueue = []; // (one being read: not saved, see epgPump)
2991
- return files.count;
3524
+ _xtv = Object.create(null); _xtvQueue = []; // (the Xtream lists: read again when asked for)
3525
+ return files.count + lists.count;
2992
3526
  }
2993
3527
  function epgControl(req, res, path, query) {
2994
3528
  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)
@@ -2997,7 +3531,7 @@ function epgControl(req, res, path, query) {
2997
3531
  send(res, 200, 'application/json', JSON.stringify({ ticket: _epgTicket }));
2998
3532
  return;
2999
3533
  }
3000
- if (path === '/epg/files') { var sf = epgSavedFiles(); send(res, 200, 'application/json', JSON.stringify({ count: sf.count, bytes: sf.bytes })); return; }
3534
+ 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; }
3001
3535
  if (path === '/epg/clear') {
3002
3536
  if (req.method !== 'POST') { send(res, 405, 'text/plain', 'POST only'); return; }
3003
3537
  send(res, 200, 'application/json', JSON.stringify({ cleared: epgClear() }));
@@ -3057,8 +3591,9 @@ function epgControl(req, res, path, query) {
3057
3591
  var ENTRY_PORT = PORT + 10, ENTRY_MS = 5 * 60 * 1000, ENTRY_IDLE_MS = 30000, ENTRY_MAX = 8192;
3058
3592
  var ENTRY_OPEN_MAX = 2, ENTRY_OPENS = 12, ENTRY_CONNECTIONS = 8, ENTRY_VISIT_MS = 20000, ENTRY_QUIET_MS = 10000;
3059
3593
  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',
3060
- search: 'Search on StremioK', youtube: 'Add a YouTube catalog to StremioK', aiom: 'Your AIOManager address for StremioK', aiomkey: 'Your AIOManager API key for StremioK' };
3061
- 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)
3594
+ search: 'Search on StremioK', youtube: 'Add a YouTube catalog to StremioK', aiom: 'Your AIOManager address for StremioK', aiomkey: 'Your AIOManager API key for StremioK',
3595
+ server: 'Your streaming server’s address for StremioK', mac: 'Your Stalker portal’s MAC address for StremioK' };
3596
+ 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)
3062
3597
  var _entries = Object.create(null), _entryServer = null, _entrySockets = [], _entryOpened = [];
3063
3598
  function lanAddress() {
3064
3599
  try {