-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
125 lines (119 loc) · 4.24 KB
/
index.html
File metadata and controls
125 lines (119 loc) · 4.24 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="http://localhost:8999/static/xterm/xterm.js"></script>
<script src="http://localhost:8999/static/zmodem/zmodem.js"></script>
<link href="http://localhost:8999/static/xterm/xterm.css" rel="stylesheet">
</head>
<body>
<button onclick="init()">start</button>
<input id="upload" type="file" onchange="upload(event)" style="display: none;">
<div id="terminal"></div>
<script>
var wsUrl = "ws://localhost:8999/terminal";
function init() {
shellWebSocket();
}
function shellWebSocket() {
var ws = new WebSocket(wsUrl);
ws.binaryType = "arraybuffer"
var term = new Terminal({
cursorBlink: true,
cursorStyle: "bar",
bellStyle: "sound"
})
term.onData(function (data) {
ws.send(JSON.stringify({
type: "cmd",
cmd: data
}));
});
createZsentry()
var intervalId
ws.onopen = function (evt) {
console.log("websocket open ...");
term.open(document.getElementById('terminal'))
term.focus()
console.log(this)
intervalId = login(ws)
};
ws.onclose = function (evt) {
console.log("websocket close ...");
term.dispose();
clearInterval(intervalId)
};
ws.onmessage = function (evt) {
arraybuffer2Str(evt.data).then(function(m){
term.write(m);
})
window.zsentry.consume(evt.data)
}
}
function login(ws) {
ws.send(JSON.stringify({
type: "login",
form: {
host: {{ .Host }},
port: {{ .Port }},
user: {{ .User }},
auth_type: {{ .AuthType }},
password: {{ .Password }},
key: {{ .SecretKey }}
}
}))
intervalId = setInterval(function() {
ws.send(JSON.stringify({
type: "heartbeat"
}))
}, 10000)
return intervalId
}
function arraybuffer2Str(buf) {
return new Promise(function(resolve, reject) {
let bo = new Blob([buf])
let reader = new FileReader()
reader.readAsText(bo, "utf-8")
reader.onload = function(e) {
resolve(reader.result)
}
})
}
function createZsentry() {
window.zsentry = new Zmodem.Sentry({
to_terminal: function(octets){},
sender: function(octets){},
on_detect: function(detection){
console.log("on_detect", detection)
window.session = detection.confirm()
if (window.session.type === "send") {
console.log("send")
document.getElementById("upload").click()
} else {
console.log("recive")
window.session.on("offer", function(xfer){
xfer.accept().then(function(){
Zmodem.Browser.save_to_disk(
xfer.get_payloads(),
xfer.get_details().name
)
})
})
window.session.start()
}
},
on_retract: function(){}
})
}
function upload(evt) {
Zmodem.Browser.send_files(window.session, evt.target.files).then(function() {
window.session.close()
window.session = null
})
}
</script>
</body>
</html>