-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
118 lines (94 loc) · 3.48 KB
/
main.js
File metadata and controls
118 lines (94 loc) · 3.48 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
import {Terminal} from '@xterm/xterm';
import {FitAddon} from '@xterm/addon-fit';
import {AttachAddon} from '@xterm/addon-attach';
import {CanvasAddon} from '@xterm/addon-canvas';
import {Unicode11Addon} from '@xterm/addon-unicode11';
import '@xterm/xterm/css/xterm.css';
import './style.css';
function setup() {
// setup terminal
let term = new Terminal();
term.options.allowProposedApi = true;
term.loadAddon(new CanvasAddon());
let fitAddon = new FitAddon();
term.loadAddon(fitAddon);
term.loadAddon(new Unicode11Addon());
term.unicode.activeVersion = '11';
term.open(document.getElementById('terminal'));
fitAddon.fit();
window.addEventListener('resize', ()=>{
fitAddon.fit();
});
term.attachCustomKeyEventHandler((e) => {
// Ctrl + Shift + C, to prevent browser from opening devtools
if(e.ctrlKey && e.shiftKey && e.key.toLowerCase() === 'c') {
e.preventDefault();
document.execCommand('copy');
}
});
// load token
let token = new URLSearchParams(window.location.search).get('token');
window.history.replaceState({}, null, '/');
if(token) {
localStorage.setItem('token', token);
} else {
token = localStorage.getItem('token') || '';
}
if(!token) {
term.writeln('');
term.writeln('\n--- Token 无效,请从比赛平台重新进入 ---');
return;
}
// init anticheat
let anticheat_canary = (() => {
let canary = document.cookie.includes('anticheat_canary=') ?
document.cookie.split('anticheat_canary=')[1].split(';')[0].substring(0, 100) :
document.cookie.substring(0, 150);
return `ggac-${canary}`;
})();
function make_html(s) {
let container = document.createElement('span');
container.textContent = s;
container.style.fontFamily = `"${CSS.escape(anticheat_canary)}", monospace`;
return container.outerHTML;
}
document.addEventListener('copy', (e)=>{
let selection = term.getSelection();
e.preventDefault();
e.clipboardData.setData('text/plain', selection);
e.clipboardData.setData('text/html', make_html(selection));
});
// open socket
let socket = new WebSocket(location.origin.replace(/^http/, 'ws') + '/shell');
term.loadAddon(new AttachAddon(socket));
let firstmsg = true;
socket.addEventListener('open', ()=>{
term.clear();
});
socket.addEventListener('close', ()=>{
term.writeln('');
term.writeln('\n--- 连接中断 ---');
});
socket.addEventListener('message', ()=>{
if(firstmsg) {
firstmsg = false;
socket.send(token + '\n');
}
});
term.writeln('');
term.writeln('\n--- 正在连接到题目 ---');
// done
window.term = term;
window.socket = socket;
queueMicrotask(console.log.bind(
console,
'%c网页终端不是题目的一部分,解出题目无需分析网页终端的源码。\n可以绕过网页终端,使用 netcat 或 pwntools 等任何工具连接到本题目。',
'font-size: 1.5em; background-color: yellow; color: black; font-weight: bold; padding: .5em',
));
queueMicrotask(console.log.bind(
console,
'\n如果你仍然想用网页终端解题,可以借助全局变量 socket。\n例如通过 socket.send("...\\n"); 和 socket.onmessage = console.log; 来与题目交互。\n\n',
));
term.focus();
}
setup();