-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDocker.js
99 lines (78 loc) · 2.88 KB
/
Docker.js
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
const { io } = window;
import { Log } from './utils.js';
const MyURL = Subdomain => window.location.hostname.replace(/.+?\./, Subdomain + '.');
let subdomain = 'kbdlab';
export const Subdomain = x => subdomain = x;
let socket, interval, test;
export const Test = () => test = true;
export function Reset() {
Log('Docker reset');
socket && socket.disconnect();
clearInterval(interval);
const url = new URL(window.location);
const searchparams = new URLSearchParams(url.search);
searchparams.delete('Container');
url.search = searchparams;
const path = url.toString();
window.history.pushState({ path }, '', path);
}
const IsYonsei = fetch('https://www.cloudflare.com/cdn-cgi/trace')
.then(res => res.text())
.then(data => {
const text = 'ip=165.132.138.';
return data.split('\n').find(x => x.startsWith(text));
});
export let Container = () => undefined;
export const Handler = (data = new URLSearchParams(window.location.search).get('Container')) => IsYonsei
.then(isYonsei => {
if (!data) return Reset();
socket && socket.disconnect();
const container = data.split('-').pop();
Log(container);
Container = () => container;
socket = io((url => {
const searchparams = new URLSearchParams(url.search);
searchparams.delete('Container');
searchparams.append('Container', data);
url.search = searchparams;
const path = url.toString();
window.history.pushState({ path }, '', path);
url.pathname = '/Browser';
url.search = '';
if (subdomain === 'yonsei')
url.hostname = MyURL(isYonsei ? 'yonsei' : 'kbdlab');
else if (subdomain === 'kbdlab') {
url.hostname = MyURL('kbdlab');
url.port = 8443;
}
url = url.toString();
Log('URL', url);
return url;
})(new URL(window.location)), {
transports: ['websocket'],
query: { Container: container }
});
socket.on('disconnect', Reset);
test && socket.on('connect', () => {
Log('Connected to', Container);
clearInterval(interval);
interval = setInterval(() => {
const data = ['test', '123123'];
Log("Emited", data);
socket.emit('Container', data);
}, 1000);
});
socket.on('Log', data => Log('Log:', data));
return socket;
});
export default (body) => Promise.resolve(body)
.then(JSON.stringify)
.then(body => fetch(`https://${MyURL('proxy')}/robot/ddpg?` + new URLSearchParams({ Subdomain: subdomain }), {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body
}))
.then(res => res.text())
.then(Handler);