This repository has been archived by the owner on Sep 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.html
321 lines (312 loc) · 8.6 KB
/
debug.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
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
<html>
<head>
<meta charset="utf-8">
<title>WebSocket Debugger</title>
<script src="https://blog-static.cnblogs.com/files/7qin/md5.js"></script>
<script src="https://cdn.staticfile.org/crypto-js/4.0.0/crypto-js.min.js"></script>
<style>
.wsinfo {
font-size: 16px;
color: rgb(61, 61, 61);
margin-bottom: 10px;
}
.wsinfo input {
outline: none;
width: 160px;
height: 28px;
border: 1px solid #ccc;
border-radius: 5px;
padding: 0 10px;
font-size: 12px;
}
.wsinfo button {
margin-left: 10px;
outline: none;
width: 100px;
height: 28px;
border: 1px solid #ccc;
border-radius: 5px;
}
.wsinfo button:hover {
background-color: #fff;
}
.wsoutput textarea {
outline: none;
width: 80%;
height: 60%;
border: 1px solid #ccc;
resize: none;
}
.wssender input {
margin-top: 10px;
outline: none;
width: 80%;
height: 32px;
border: 1px solid #ccc;
border-radius: 5px;
padding: 0 10px;
font-size: 16px;
}
#connect {
margin-left: 0;
}
</style>
</head>
<body style="background-color: rgb(231, 233, 233);">
<div class="wsinfo">
WebSocket Server Address
<input id="addr" type="text" value="" onkeydown="onAddrChange(event)"></input>
</div>
<div class="wsinfo">
WebSocket Password
<input id="passwd" type="text" value="" onkeydown="onPasswdChange(event)"></input>
</div>
<div class="wsinfo">
<button id="connect" onclick="connect()">Connect</button>
<button id="disconnect" onclick="disconnect()">Disconnect</button>
<button id="clear" onclick="storage.set('history','[]');history = [];">Clear History</button>
</div>
<div class="wsoutput">
<textarea id="output" style="font-size: 18px;" readonly></textarea>
</div>
<div class="wssender"><input id="input" type="text" onkeydown="onInputKeyDown(event)"></input></div>
</body>
<script>
function init_storage() {
var ms = "WebSocketDebugger";
var storage = window.localStorage;
if(!window.localStorage){
alert("浏览器不支持localstorage");
return false;
}
//存储方法
var set = function(key,value){
var mydata = storage.getItem(ms);
if(!mydata){
this.init();
mydata = storage.getItem(ms);
}
mydata = JSON.parse(mydata);
mydata.data[key] = value;
storage.setItem(ms,JSON.stringify(mydata));
return mydata.data;
};
//读取方法
var get = function(key){
var mydata = storage.getItem(ms);
if(!mydata){
return false;
}
mydata = JSON.parse(mydata);
return mydata.data[key];
};
var remove = function(key){
//读取
var mydata = storage.getItem(ms);
if(!mydata){
return false;
}
mydata = JSON.parse(mydata);
delete mydata.data[key];
storage.setItem(ms,JSON.stringify(mydata));
return mydata.data;
};
var clear = function(){
//清除对象
storage.removeItem(ms);
};
var init = function(){
storage.setItem(ms,'{"data":{}}');
};
return {
set : set,
get : get,
remove : remove,
init : init,
clear : clear
};
};
var storage = init_storage();
var ws = null;
var wsuri = "";
let md5key = hex_md5("wowow");
let skey = md5key.substring(0, 16);
let siv = md5key.substring(16);
let history = [];
init();
function init() {
let addr = storage.get("addr");
if (addr != null) {
wsuri = addr;
document.getElementById("addr").value = addr;
textareaAddLine("Set WebSocket Server Address: " + addr);
}
let passwd = storage.get("passwd");
if (passwd != null) {
md5key = hex_md5(passwd);
skey = md5key.substring(0, 16);
siv = md5key.substring(16);
document.getElementById("passwd").value = passwd;
textareaAddLine("Set WebSocket Server Password: " + passwd);
}
let _history = storage.get("history");
if (_history != null) {
history = JSON.parse(_history);
}
}
function textareaAddLine(text) {
var textarea = document.getElementById("output");
textarea.value += text + "\n";
textarea.scrollTop = textarea.scrollHeight;
}
function prettyPrint(space, obj) {
let keys = Object.keys(obj);
var str = "";
for (let i = 0; i < keys.length; i++) {
let key = keys[i];
if (typeof obj[key] == "object") {
str += space + "- " + key + ": \n";
str += prettyPrint(space + " ", obj[key]);
} else {
str += space + "- " + key + ": " + obj[key] + "\n";
}
}
return str;
}
function connect() {
if (ws != null)
ws.close();
ws = new WebSocket("ws://" + wsuri);
ws.onopen = function () {
WS_Send('{"type":"hello","data":{"name":"Debugger","introduction":"WebSocket Debugger","others":null,"xuidString":true}}');
console.log("WebSocket connected!");
textareaAddLine("WebSocket connected!");
}
ws.onerror = function (err) {
textareaAddLine('WebSocket error!(See console)');
console.log(err);
}
ws.onmessage = function (msg) {
if (JSON.parse(msg.data).encrypted) {
let plain = decrypt(JSON.parse(msg.data).data);
let obj = JSON.parse(plain);
//textareaAddLine("Received: " + msg.data + "(" + plain +")");
//textareaAddLine("Received: " + plain);
textareaAddLine("Received: ");
if (obj["id"] != null)
textareaAddLine("id: " + obj.id);
if (obj["type"] != null)
textareaAddLine("type: " + obj.type);
if (obj["event"] != null)
textareaAddLine("event: " + obj.event);
if (obj["data"] != null) {
textareaAddLine("data: ");
textareaAddLine(prettyPrint("", obj.data));
}
} else {
console.log(msg.data);
textareaAddLine("Received: " + msg.data);
}
}
ws.onclose = function () {
console.log("WebSocket disconnected!");
textareaAddLine("WebSocket disconnected!");
}
}
function disconnect() {
ws.close(0, "Bye!");
}
function onAddrChange(event) {
if (event.keyCode == 13) {
wsuri = document.getElementById("addr").value;
storage.set("addr", wsuri);
textareaAddLine("WebSocket Address Modified: " + wsuri);
}
}
function onPasswdChange(event) {
if (event.keyCode == 13) {
let passwd = document.getElementById("passwd").value;
storage.set("passwd", passwd);
md5key = hex_md5(passwd);
skey = md5key.substring(0, 16);
siv = md5key.substring(16);
textareaAddLine("WebSocket Password Modified: " + skey + ' ' + siv);
}
}
let now = -1;
function onInputKeyDown(event) {
if (event.keyCode == 13) {
let text = document.getElementById("input").value;
if (text == "") return;
if (history.indexOf(text) != -1) {
history.splice(history.indexOf(text), 1);
}
history.push(text);
storage.set("history", JSON.stringify(history));
now = -1;
document.getElementById("input").value = "";
WS_Send(text);
}
else if (event.keyCode == 38) {
if (history.length > 0) {
if (now == -1) {
now = history.length - 1;
} else if (now > 0) {
now--;
}
document.getElementById("input").value = history[now];
}
}
else if (event.keyCode == 40) {
if (history.length > 0) {
if (now == -1) {
return;
} else if (now < history.length - 1) {
now++;
}
else {
now = -1;
document.getElementById("input").value = "";
return;
}
document.getElementById("input").value = history[now];
}
}
}
function WS_Send(data) {
if (ws != null && ws.readyState == WebSocket.OPEN) {
let cipher = encrypt(data);
let text = JSON.stringify({ encrypted: true, mode: "AES/CBC/PKCS7Padding", data: cipher });
ws.send(text);
textareaAddLine("Sent: " + text);
} else {
console.log("WebSocket is not open.");
textareaAddLine("WebSocket is not open.");
}
}
function parse(string) {
return CryptoJS.enc.Utf8.parse(string);
}
function encrypt(data) {
// Encrypt
return CryptoJS.AES.encrypt(parse(data), parse(skey), {
iv: parse(siv),
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
}).toString();
}
function decrypt(code) {
return CryptoJS.AES.decrypt(code, parse(skey),{
iv: parse(siv),
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
}).toString(CryptoJS.enc.Utf8);
}
function base64_encode(str) {
return CryptoJS.enc.Base64.stringify(str);
}
function base64_decode(str) {
return CryptoJS.enc.Base64.parse(str).toString();
}
</script>
</html>