-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
144 lines (129 loc) · 3.89 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ssh habra.chat</title>
<link rel="stylesheet" type="text/css" href="https://unpkg.com/[email protected]/css/xterm.css">
<style type="text/css">
html, body, .terminal {
height: 100%;
margin: 0;
}
</style>
</head>
<body>
<div class="terminal"></div>
<script type="text/javascript" src="https://unpkg.com/[email protected]/lib/xterm.js"></script>
<script type="text/javascript" src="https://unpkg.com/[email protected]"></script>
<script type="text/javascript" src="https://unpkg.com/[email protected]"></script>
<script type="text/javascript" src="https://unpkg.com/[email protected]"></script>
<script type="text/javascript" src="https://unpkg.com/[email protected]"></script>
<script type="text/javascript">
if(location.protocol === "http:") {
location.assign(location.href.replace("http", "https"));
}
function isCanvasReadbackEnabled() {
const canvas = new OffscreenCanvas(1, 1);
const context = canvas.getContext("2d");
context.fillStyle = "#123456";
context.fillRect(0, 0, 1, 1);
const imageData = context.getImageData(0, 0, 1, 1);
return (
imageData.data[0] === 0x12
&& imageData.data[1] === 0x34
&& imageData.data[2] === 0x56
);
}
function start() {
const enc = new TextEncoder();
const dec = new TextDecoder();
const term = new Terminal();
const fitAddon = new FitAddon.FitAddon();
term.loadAddon(new WebLinksAddon.WebLinksAddon());
term.loadAddon(fitAddon);
term.open(document.querySelector(".terminal"));
fitAddon.fit();
if(isCanvasReadbackEnabled()) {
try {
const webglAddon = new WebglAddon.WebglAddon();
term.loadAddon(webglAddon);
webglAddon.onContextLoss(e => {
webglAddon.dispose();
term.loadAddon(new CanvasAddon.CanvasAddon());
});
} catch(e) {
term.loadAddon(new CanvasAddon.CanvasAddon());
}
}
window.onresize = () => fitAddon.fit();
let ws = null;
function connect() {
ws = new WebSocket(`${location.protocol.replace("http", "ws")}//${location.host}/chat/ws`);
ws.onopen = () => {
ws.send(`${localStorage.privateKey},${btoa(localStorage.nick)}`);
ws.send(`resize ${term.cols} ${term.rows}`);
term.clear();
};
ws.onmessage = async e => {
const arr = new Uint8Array(await e.data.arrayBuffer());
term.write(arr);
const idx = arr.lastIndexOf(0x0a);
if(idx !== -1 && arr[idx + 1] === 0x5b) {
const closeIdx = arr.indexOf(0x5d, idx);
let nick = dec.decode(arr.slice(idx + 2, closeIdx));
nick = nick.replace(/\x1b\[.*?m/g, "");
nick = nick.split(" ").slice(-1)[0];
localStorage.nick = nick;
}
};
ws.onclose = () => {
ws = null;
setTimeout(() => connect(), 1000);
};
}
connect();
term.onData(text => {
if(ws) {
ws.send(enc.encode(text.replace(/\r/g, "\r\n")));
}
});
term.onBinary(data => {
if(ws) {
ws.send(data);
}
});
term.onResize(({cols, rows}) => {
if(ws) {
ws.send(`resize ${cols} ${rows}`);
}
});
setInterval(() => {
if(ws) {
ws.send("");
}
}, 60 * 1000);
}
(async () => {
if(!localStorage.privateKey) {
const key = await crypto.subtle.generateKey(
{
name: "RSASSA-PKCS1-v1_5",
modulusLength: 2048,
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
hash: {name: "SHA-256"}
},
true,
["sign", "verify"]
);
const pkcs8 = await crypto.subtle.exportKey("pkcs8", key.privateKey);
localStorage.privateKey = btoa(String.fromCharCode(...new Uint8Array(pkcs8)));
}
if(!localStorage.nick) {
localStorage.nick = "";
}
start();
})();
</script>
</body>
</html>