Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
474 lines (430 loc) · 14.2 KB
/
Copy pathmain.js
File metadata and controls
474 lines (430 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
let homepage = "https://tiki7.github.io/TKPage3/";
let blankpage = window.location.href;
let blackurl = '';
//.....
let instance = 0;
loc_url = new URL(location);
let video_service_custom = {
// 'param1': param1_main_function,
};
let video_service = {
'youtube': /(.*youtube\..*)|(.*youtu\.be.*)/i,
'twitch': /.*\.twitch\..*/i,
'vimeo': /.*vimeo\.\w+\//i,
'soundcloud': /\w*.?soundcloud\.\w+/i,
'bilibili': /\.bilibili\./i,
'zeno': /zeno/i,
'mixcloud': /\.mixcloud\./i,
'video': /\.mp4|\.m4v|\.m3u8|\.webm|\.ogv|\.mp3/i,
}
let novideo_service = {
'radio': radio_main,
};
function cmd_open_v2() {
let link = packet.body;
//--------------------------- LOG
log = `...REQUEST`;
if (packet.id != "") { log += `@${packet.id}`; }
console_(log);
//---------------------------
try {
link_url = null;
if (link == 'off') {
link = blankpage;
link_url = new URL(link);
console_(`URL off`);
}
else if (link == 'homepage') {
link = homepage;
link_url = new URL(link);
console_(`URL homepage`);
} else {
link_url = new URL(link);
if (link_url.host == location.host || '-' in packet.param) {
log = `URL [*][*][*][*][*] -{___}`
} else {
log = `URL [${link_url.protocol}//][${link_url.host}][${link_url.pathname}][${link_url.search}][${link_url.hash}] -${JSON.stringify(packet.param)}`;
}
console_(log);
}
prehub();
// -------------
showConsole();
} catch (e) {
console.log('URL Failed');
if (link_url == null) { console_(`URL ${packet.body} -${JSON.stringify(packet.param)}`, debug_cl_warning) };
console_(e, debug_cl_warning);
console_('...URL Failed', debug_cl_warning);
// -------------
showConsole();
}
}
function prehub() {
if (['radio'].some(p => p in packet.param)) {
reload_check('NOVIDEO');
//--> if going to add a blacklist of urls, this spot may be the best spot
//<--
cleanup('NOVIDEO');
restyle('NOVIDEO');
//-->
hub_novideo();
} else {
reload_check('VIDEO');
cleanup('VIDEO');
restyle('VIDEO');
//-->
hub_video();
}
}
function reload_check(v) {
if (instance > 0 && !('noreload' in packet.param)) { window.location.reload(); }
if (v == 'VIDEO') { instance += 1; }
}
function cleanup(v) {
if (v == 'VIDEO') {
{ //1.1. loading a video so: cleaning videos first
if (video != null) {
video.remove();
video = null;
}
iframe.setAttribute('src', '');
}
//1.2. ...
if (!('mute' in packet.param)) {
audio.setAttribute('src', '');
}
} else if (v == 'NOVIDEO') {
audio.setAttribute('src', '');
}
}
function restyle(v) {
if ('debug' in packet.param) { console_div.style.zIndex = debug_zIndex; } else { console_div.style.zIndex = 0; }
if (v == 'VIDEO') {
console_div_inner.style.backgroundColor = `rgb(0,0,0,${debug_opacity})`;
b_iframe.setAttribute('src', '');
} else if (v == 'NOVIDEO') {
console_div_inner.style.backgroundColor = `rgb(0,0,0,0.0)`;
b_iframe.setAttribute('src', 'flow\\index.html');
}
}
function hub_video() {
let video_custom_param_ = Object.keys(video_service_custom).filter(p => p in packet.param)[0];
if (video_custom_param_ != undefined) { // SPECIAL
video_service_custom[video_custom_param_]();
} else if (video_service['youtube'].test(link_url.host)) {
youtube_main();
} else if (video_service['twitch'].test(link_url.host)) {
twitch_main();
} else if (video_service['vimeo'].test(link_url.host)) {
vimeo_main();
} else if (video_service['soundcloud'].test(link_url.host)) {
soundcloud_main();
} else if (video_service['bilibili'].test(link_url.host)) {
bilibili_main();
} else if (video_service['zeno'].test(decodeURI(link_url.host))) {
zeno_main();
} else if (video_service['mixcloud'].test(decodeURI(link_url.host))) {
mixcloud_main();
} else if (video_service['video'].test(decodeURI(link_url.href))) {
video_main();
} else { // SUPER SPECIAL
script_direct();
}
}
function hub_novideo() {
let param_ = Object.keys(novideo_service).filter(p => p in packet.param)[0];
if (param_ != undefined) {
novideo_service[param_]();
}
}
function cmd_debug() {
if (console_div.style.zIndex == '0') {
i_want_debug = true;
console_div.style.zIndex = debug_zIndex;
} else {
i_want_debug = false;
console_div.style.zIndex = '0';
}
}
window.addEventListener("hashchange", () => {
hashchange(false);
});
let packet = {
func: 'default',
body: '',
param: {},
id: '',
extra: {}
};
function isBlocked2() {
let check = false;
let currentUser;
let currentUrl;
/* todo:
1. fetch a url for its string content
2. put the output inside an array spliting by lines
3. raw inputs in each line are formatted like this: [USER/URL]:[userValue/urlAddress]. example, USER:Tiki-8590 \n URL:www.google.com
4. url addresses may have wild cards like this example in the raw input. example, www.google.com/* so convert all of the urls into regex type.
5. make an object like this template
{
url:[],
user:[]
}
for each item in array, fill the inputs in their respectable place.
6. use regex and check if either current user or current url is inside the block object list by checking them in the list one by one. consider that
we may have wild cards inside the url address raw input.
then set check to true if you found any match in regex or false if not.
*/
return check;
}
function isValidURL(url) {
try {
new URL(url);
return true;
} catch (error) {
return false;
}
}
async function isBlocked(local_packet_temp) {
let currentUrl = local_packet_temp.body ?? "";
let currentUser = local_packet_temp.id ?? "";
if (currentUrl == null || undefined || "") {
currentUrl = "";
} else {
// if (!currentUrl.startsWith('http://') && !currentUrl.startsWith('https://')) {
// currentUrl = 'https://' + currentUrl; // Prepend with http://
// }
currentUrl = decodeURI(currentUrl);
// currentUrl = new URL(currentUrl).href;
}
// Function to convert wildcard URL to regex
const urlToRegex = (url) => {
// Allow both case-insensitive and trailing slash variations
const regexString = url
.replace(/\./g, '\\.')
.replace(/\*/g, '.*')
.replace(/\/?$/, '/?')
.toLowerCase(); // Convert to lowercase for case insensitivity
return new RegExp(`^${regexString}$`);
};
let blocked_byBlack = false;
let blocked_byWhite = false;
let gateURL = local_packet_temp.env.gate;
if (gateURL && isValidURL(gateURL)) {
try {
const response = await fetch(gateURL);
const content = await response.text();
const lines = content.split('\n');
const blackObj = {
url: [],
user: []
};
const whiteObj = {
url: [/^homepage$/],
user: []
};
lines.forEach(line => {
if (!line.startsWith('#')) {
const [type, value] = line.split(':');
if (type.trim() === '-USER') {
blackObj.user.push(new RegExp(`^${value.trim().toLowerCase()}$`));
} else if (type.trim() === '-URL') {
blackObj.url.push(urlToRegex(value.trim()));
} else if (type.trim() === '+USER') {
whiteObj.user.push(new RegExp(`^${value.trim().toLowerCase()}$`));
} else if (type.trim() === '+URL') {
whiteObj.url.push(urlToRegex(value.trim()));
}
}
});
console.log('whiteObj:')
console.log(whiteObj);
console.log('blackObj:')
console.log(blackObj);
//black check
blocked_byBlack = blackObj.user.some(userRegex => {
return userRegex.test(currentUser.toLowerCase());
}) || blackObj.url.some(urlRegex => {
return urlRegex.test(currentUrl);
});
//white check
if (whiteObj.user.length > 0) {
blocked_byWhite = !whiteObj.user.some(userRegex => {
return userRegex.test(currentUser.toLowerCase())
})
}
if (whiteObj.url.length > 0) {
blocked_byWhite = blocked_byWhite || !whiteObj.url.some(urlRegex => {
return urlRegex.test(currentUrl);
})
}
} catch (error) {
console.error('Error fetching or parsing data:', error);
}
}
if (blocked_byBlack) { console.log("blocked_byBlack") } else { console.log("NOT blocked_byBlack") }
if (blocked_byWhite) { console.log("blocked_byWhite") } else { console.log("NOT blocked_byWhite") }
return blocked_byBlack || blocked_byWhite;
}
async function hashchange(skipCheck) {
if ((skipCheck == false) && (await hashVerified() == false)) {
history.pushState(null, null, ' ');
return;
}
if (packet.func == 'comment') { //comment
cmd_comment(packet.body);
} else if (packet.func == 'debug') {
cmd_debug();
} else if (packet.func == 'default') {
if ('que' in packet.param) {
cmd_que();
} else if ('cmd' in packet.param) {
cmd_cmd();
} else if ('help' in packet.param) {
cmd_help();
} else {
cmd_open_v2();
}
}
history.pushState(null, null, ' ');
}
async function hashVerified() {
// #{ "overparam", "func", "body", "param", "id", "extra" }
let h = decodeURI(location.hash).substring(1);
console.log(`#${h}`);
try {
let obj = {};
try { obj = JSON.parse(h); } catch { }
let packet_temp = {
overparam: {},
func: 'default',
body: '',
param: {},
id: '',
extra: {},
env: {}
};
let superUser = ['gate'];
// this is filling up the packet object
Object.keys(obj).forEach(key => {
if (!superUser.includes(key.toLowerCase()) && key in packet_temp && obj[key] != null) { packet_temp[key] = obj[key]; }
});
// override params with overparams that are added by authorities(world owners)
Object.keys(packet_temp.overparam).forEach(key => {
if (superUser.includes(key.toLowerCase())) {
packet_temp.env[key.toLowerCase()] = packet_temp.overparam[key];
} else {
packet_temp.param[key] = packet_temp.overparam[key];
}
});
delete packet_temp.overparam;
console.log("> Hash Verified.");
let isBlockedResult = await isBlocked(packet_temp);
if (isBlockedResult) {
history.pushState(null, null, ' ');
return false;
}
else {
packet = packet_temp;
console.log(packet);
return true;
}
// }
} catch (e) {
return false;
}
}
let quedPacket = [];
function cmd_que() {
delete packet.param.que;
packet.param['noreload'] = null;
quedPacket.push(packet);
log = `...QUE-REQUEST`;
if (packet.id != "") { log += `@${packet.id}`; }
console.log(log);
console_(log);
// -------------
showConsole();
}
function sessionEnded() {
console.log("> Session End!")
if (quedPacket.length > 0) {
packet = quedPacket.shift();
hashchange(true);
}
else if ('endmedia' in packet.param) {
let new_packet = {
func: 'default',
body: packet.param.endmedia,
param: { loop: null }
};
location.hash = JSON.stringify(new_packet);
}
else {
console.log("Exit")
// finished for real...
if (video != null) {
video.remove();
video = null;
}
iframe.parentElement.replaceChild(iframe_temp, iframe);
}
}
function cmd_cmd() {
log = `...CMD-REQUEST`;
if (packet.id != "") { log += `@${packet.id}`; }
console_(log);
// CMD SECURITY SECTION ------------>
packet.body = cmd_cmd_security(packet.body);
// <------------- CMD SECURITY SECTION
try {
let temp_cmd = `"use strict";player.${packet.body};`;
Function(temp_cmd)()
console.log(temp_cmd);
console_(`CMD player.${packet.body}`);
// -------------
showConsole();
} catch (e) {
console.log('CMD Failed');
console_(e, debug_cl_warning);
console_('...CMD Failed', debug_cl_warning);
// -------------
showConsole();
}
}
function cmd_cmd_security(str) {
// Rules
// 1. cant go beyond first ')'
str = str.replace(/[;\.\s]/g, '');
// 1. cant have ';' or '.' or white spaces or new lines ...
str = str.substring(0, str.indexOf(')') == -1 ? str.length : str.indexOf(')') + 1);
// 3. ',' is Allowed only when its between '(' and ')'
let i = -1;
while ((i = str.indexOf(',', i + 1)) != -1) {
let p2 = str.indexOf(')', i);
if (p2 == -1) {
str = str.replaceAt(i, '');
continue;
}
let p1 = str.indexOf('(', i);
if (p1 != -1 && p1 < p2) {
str = str.replaceAt(i, '');
continue;
}
if (p1 == -1 && str.indexOf('(', 0) == -1) {
str = str.replaceAt(i, '');
continue;
}
}
return str;
}
function cmd_help() {
if (packet.body.toLowerCase() == 'youtube') {
console_(JSON.stringify(youtube_cmds, null, 1));
showConsole();
}
}
iframe.style.visibility = 'visible';
v_container.style.visibility = 'visible';
hashchange(false);
// visual();