oipage 1.8.0-alpha.0 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/FUNDING.yml +11 -11
- package/AUTHORS.txt +6 -6
- package/CHANGELOG +3 -2
- package/LICENSE +20 -20
- package/bin/WebSocket/decodeWsFrame.js +43 -43
- package/bin/WebSocket/encodeWsFrame.js +28 -28
- package/bin/WebSocket/headersToJSON.js +26 -26
- package/bin/WebSocket/index.js +80 -80
- package/bin/data/mineTypes.json +104 -104
- package/bin/disk.js +42 -42
- package/bin/intercept.js +15 -15
- package/bin/network.js +21 -21
- package/bin/run +4 -1
- package/bin/serve.js +4 -4
- package/bin/template/404.html +186 -186
- package/bin/tools/deleteEmptyFolder.js +23 -23
- package/bin/tools/format.js +75 -75
- package/bin/tools/network.js +42 -42
- package/bin/tools/resolve404.js +83 -83
- package/bin/tools/resolveImport.js +88 -88
- package/bin/website-htmls/components/ui-select-file/index.html +7 -7
- package/bin/website-htmls/components/ui-select-file/index.js +37 -37
- package/bin/website-htmls/components/ui-select-file/index.scss +44 -44
- package/bin/website-htmls/dialogs/imageSize/index.html +54 -54
- package/bin/website-htmls/dialogs/imageSize/index.js +53 -53
- package/bin/website-htmls/dialogs/imageSize/index.scss +138 -138
- package/bin/website-htmls/dialogs/index.js +52 -52
- package/bin/website-htmls/images/addFile.svg +1 -1
- package/bin/website-htmls/images/img-to-pdf.svg +1 -1
- package/bin/website-htmls/index.html +17 -17
- package/bin/website-htmls/main.js +13 -13
- package/bin/website-htmls/pages/App/index.html +6 -6
- package/bin/website-htmls/pages/App/index.js +22 -22
- package/bin/website-htmls/pages/App/index.scss +43 -43
- package/bin/website-htmls/pages/appStore/index.html +5 -5
- package/bin/website-htmls/pages/appStore/index.js +19 -19
- package/bin/website-htmls/pages/chart/index.html +8 -8
- package/bin/website-htmls/pages/chart/index.js +72 -72
- package/bin/website-htmls/pages/chart/index.scss +73 -73
- package/bin/website-htmls/pages/home/index.html +21 -21
- package/bin/website-htmls/pages/home/index.js +19 -19
- package/bin/website-htmls/pages/image-editor/index.html +27 -27
- package/bin/website-htmls/pages/image-editor/index.js +106 -106
- package/bin/website-htmls/pages/image-editor/index.scss +47 -47
- package/bin/website-htmls/pages/img-to-pdf/index.html +3 -3
- package/bin/website-htmls/pages/img-to-pdf/index.js +44 -44
- package/bin/website-htmls/pages/img-to-pdf/index.scss +4 -4
- package/bin/website-htmls/router.config.js +26 -26
- package/bin/website-htmls/styles/normalize.css +94 -94
- package/bin/website-plugins/intercept/chart.js +33 -33
- package/bin/website-plugins/intercept/getPackage.js +17 -17
- package/bin/website-plugins/intercept/head.js +8 -8
- package/bin/website-plugins/intercept/index.js +8 -8
- package/bin/website-plugins/intercept/oipage-vislite-intercept.js +30 -30
- package/bin/website-plugins/intercept/oipage-zipaper-intercept.js +36 -36
- package/bin/website-plugins/loader/index.js +11 -11
- package/bin/website-plugins/loader/oipage-html-loader.js +7 -7
- package/bin/website-plugins/loader/oipage-scss-loader.js +149 -149
- package/nodejs/animation/index.d.ts +19 -19
- package/nodejs/animation/index.js +120 -112
- package/nodejs/cmdlog/index.d.ts +20 -20
- package/nodejs/cmdlog/index.js +75 -75
- package/nodejs/disk/index.d.ts +47 -47
- package/nodejs/disk/index.js +166 -165
- package/nodejs/format/index.d.ts +29 -29
- package/nodejs/format/index.js +107 -107
- package/nodejs/json/index.d.ts +9 -9
- package/nodejs/json/index.js +206 -206
- package/nodejs/logform/index.d.ts +18 -18
- package/nodejs/logform/index.js +94 -94
- package/nodejs/reader/index.d.ts +32 -32
- package/nodejs/reader/index.js +20 -20
- package/nodejs/throttle/index.d.ts +30 -30
- package/nodejs/throttle/index.js +50 -50
- package/package.json +3 -3
- package/web/XMLHttpRequest/index.d.ts +17 -17
- package/web/XMLHttpRequest/index.js +60 -60
- package/web/animation/index.d.ts +19 -19
- package/web/animation/index.js +120 -112
- package/web/format/index.d.ts +29 -29
- package/web/format/index.js +107 -107
- package/web/json/index.d.ts +9 -9
- package/web/json/index.js +206 -206
- package/web/onReady/index.d.ts +7 -7
- package/web/onReady/index.js +8 -8
- package/web/performChunk/index.d.ts +4 -4
- package/web/performChunk/index.js +19 -19
- package/web/reader/index.d.ts +32 -32
- package/web/reader/index.js +20 -20
- package/web/style/index.d.ts +21 -21
- package/web/style/index.js +19 -19
- package/web/throttle/index.d.ts +30 -30
- package/web/throttle/index.js +50 -50
package/.github/FUNDING.yml
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
# These are supported funding model platforms
|
|
2
|
-
|
|
3
|
-
github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
|
|
4
|
-
patreon: # Replace with a single Patreon username
|
|
5
|
-
open_collective: # Replace with a single Open Collective username
|
|
6
|
-
ko_fi: # Replace with a single Ko-fi username
|
|
7
|
-
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
|
|
8
|
-
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
|
|
9
|
-
liberapay: # Replace with a single Liberapay username
|
|
10
|
-
issuehunt: # Replace with a single IssueHunt username
|
|
11
|
-
otechie: # Replace with a single Otechie username
|
|
1
|
+
# These are supported funding model platforms
|
|
2
|
+
|
|
3
|
+
github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
|
|
4
|
+
patreon: # Replace with a single Patreon username
|
|
5
|
+
open_collective: # Replace with a single Open Collective username
|
|
6
|
+
ko_fi: # Replace with a single Ko-fi username
|
|
7
|
+
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
|
|
8
|
+
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
|
|
9
|
+
liberapay: # Replace with a single Liberapay username
|
|
10
|
+
issuehunt: # Replace with a single IssueHunt username
|
|
11
|
+
otechie: # Replace with a single Otechie username
|
|
12
12
|
custom: ['https://zxl20070701.github.io/notebook/home.html']
|
package/AUTHORS.txt
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Authors ordered by first contribution.
|
|
2
|
-
|
|
3
|
-
zxl20070701 <1904314465@qq.com> <https://github.com/zxl20070701>
|
|
4
|
-
|
|
5
|
-
-----------------------------------------------------
|
|
6
|
-
We thank all of them for their contributions!
|
|
1
|
+
Authors ordered by first contribution.
|
|
2
|
+
|
|
3
|
+
zxl20070701 <1904314465@qq.com> <https://github.com/zxl20070701>
|
|
4
|
+
|
|
5
|
+
-----------------------------------------------------
|
|
6
|
+
We thank all of them for their contributions!
|
package/CHANGELOG
CHANGED
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) zxl20070701 走一步,再走一步
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) zxl20070701 走一步,再走一步
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
21
|
SOFTWARE.
|
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
|
|
2
|
-
// 处理收到的数据
|
|
3
|
-
module.exports = function decodeWsFrame(data) {
|
|
4
|
-
let start = 0;
|
|
5
|
-
let frame = {
|
|
6
|
-
isFinal: (data[start] & 0x80) === 0x80,
|
|
7
|
-
opcode: data[start++] & 0xF,
|
|
8
|
-
masked: (data[start] & 0x80) === 0x80,
|
|
9
|
-
payloadLen: data[start++] & 0x7F,
|
|
10
|
-
maskingKey: '',
|
|
11
|
-
payloadData: null
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
if (frame.payloadLen === 126) {
|
|
15
|
-
frame.payloadLen = (data[start++] << 8) + data[start++];
|
|
16
|
-
} else if (frame.payloadLen === 127) {
|
|
17
|
-
frame.payloadLen = 0;
|
|
18
|
-
for (let i = 7; i >= 0; --i) {
|
|
19
|
-
frame.payloadLen += (data[start++] << (i * 8));
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
if (frame.payloadLen) {
|
|
24
|
-
if (frame.masked) {
|
|
25
|
-
const maskingKey = [
|
|
26
|
-
data[start++],
|
|
27
|
-
data[start++],
|
|
28
|
-
data[start++],
|
|
29
|
-
data[start++]
|
|
30
|
-
];
|
|
31
|
-
|
|
32
|
-
frame.maskingKey = maskingKey;
|
|
33
|
-
|
|
34
|
-
frame.payloadData = data
|
|
35
|
-
.slice(start, start + frame.payloadLen)
|
|
36
|
-
.map((byte, idx) => byte ^ maskingKey[idx % 4]);
|
|
37
|
-
} else {
|
|
38
|
-
frame.payloadData = data.slice(start, start + frame.payloadLen);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return frame;
|
|
43
|
-
};
|
|
1
|
+
|
|
2
|
+
// 处理收到的数据
|
|
3
|
+
module.exports = function decodeWsFrame(data) {
|
|
4
|
+
let start = 0;
|
|
5
|
+
let frame = {
|
|
6
|
+
isFinal: (data[start] & 0x80) === 0x80,
|
|
7
|
+
opcode: data[start++] & 0xF,
|
|
8
|
+
masked: (data[start] & 0x80) === 0x80,
|
|
9
|
+
payloadLen: data[start++] & 0x7F,
|
|
10
|
+
maskingKey: '',
|
|
11
|
+
payloadData: null
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
if (frame.payloadLen === 126) {
|
|
15
|
+
frame.payloadLen = (data[start++] << 8) + data[start++];
|
|
16
|
+
} else if (frame.payloadLen === 127) {
|
|
17
|
+
frame.payloadLen = 0;
|
|
18
|
+
for (let i = 7; i >= 0; --i) {
|
|
19
|
+
frame.payloadLen += (data[start++] << (i * 8));
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (frame.payloadLen) {
|
|
24
|
+
if (frame.masked) {
|
|
25
|
+
const maskingKey = [
|
|
26
|
+
data[start++],
|
|
27
|
+
data[start++],
|
|
28
|
+
data[start++],
|
|
29
|
+
data[start++]
|
|
30
|
+
];
|
|
31
|
+
|
|
32
|
+
frame.maskingKey = maskingKey;
|
|
33
|
+
|
|
34
|
+
frame.payloadData = data
|
|
35
|
+
.slice(start, start + frame.payloadLen)
|
|
36
|
+
.map((byte, idx) => byte ^ maskingKey[idx % 4]);
|
|
37
|
+
} else {
|
|
38
|
+
frame.payloadData = data.slice(start, start + frame.payloadLen);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return frame;
|
|
43
|
+
};
|
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
// 处理发出的数据
|
|
3
|
-
module.exports = function encodeWsFrame(data) {
|
|
4
|
-
const isFinal = data.isFinal !== undefined ? data.isFinal : true,
|
|
5
|
-
opcode = data.opcode !== undefined ? data.opcode : 1,
|
|
6
|
-
payloadData = data.payloadData ? Buffer.from(data.payloadData) : null,
|
|
7
|
-
payloadLen = payloadData ? payloadData.length : 0;
|
|
8
|
-
|
|
9
|
-
let frame = [];
|
|
10
|
-
|
|
11
|
-
if (isFinal) frame.push((1 << 7) + opcode);
|
|
12
|
-
else frame.push(opcode);
|
|
13
|
-
|
|
14
|
-
if (payloadLen < 126) {
|
|
15
|
-
frame.push(payloadLen);
|
|
16
|
-
} else if (payloadLen < 65536) {
|
|
17
|
-
frame.push(126, payloadLen >> 8, payloadLen & 0xFF);
|
|
18
|
-
} else {
|
|
19
|
-
frame.push(127);
|
|
20
|
-
for (let i = 7; i >= 0; --i) {
|
|
21
|
-
frame.push((payloadLen & (0xFF << (i * 8))) >> (i * 8));
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
frame = payloadData ? Buffer.concat([Buffer.from(frame), payloadData]) : Buffer.from(frame);
|
|
26
|
-
|
|
27
|
-
return frame;
|
|
28
|
-
};
|
|
1
|
+
|
|
2
|
+
// 处理发出的数据
|
|
3
|
+
module.exports = function encodeWsFrame(data) {
|
|
4
|
+
const isFinal = data.isFinal !== undefined ? data.isFinal : true,
|
|
5
|
+
opcode = data.opcode !== undefined ? data.opcode : 1,
|
|
6
|
+
payloadData = data.payloadData ? Buffer.from(data.payloadData) : null,
|
|
7
|
+
payloadLen = payloadData ? payloadData.length : 0;
|
|
8
|
+
|
|
9
|
+
let frame = [];
|
|
10
|
+
|
|
11
|
+
if (isFinal) frame.push((1 << 7) + opcode);
|
|
12
|
+
else frame.push(opcode);
|
|
13
|
+
|
|
14
|
+
if (payloadLen < 126) {
|
|
15
|
+
frame.push(payloadLen);
|
|
16
|
+
} else if (payloadLen < 65536) {
|
|
17
|
+
frame.push(126, payloadLen >> 8, payloadLen & 0xFF);
|
|
18
|
+
} else {
|
|
19
|
+
frame.push(127);
|
|
20
|
+
for (let i = 7; i >= 0; --i) {
|
|
21
|
+
frame.push((payloadLen & (0xFF << (i * 8))) >> (i * 8));
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
frame = payloadData ? Buffer.concat([Buffer.from(frame), payloadData]) : Buffer.from(frame);
|
|
26
|
+
|
|
27
|
+
return frame;
|
|
28
|
+
};
|
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
module.exports = function (headersStr) {
|
|
2
|
-
|
|
3
|
-
let headersArray = headersStr.split(/\r{0,1}\n/);
|
|
4
|
-
let headersJSON = {};
|
|
5
|
-
|
|
6
|
-
// 第一行的内容:GET / HTTP/1.1
|
|
7
|
-
let firstLine = headersArray.shift().split(' ');
|
|
8
|
-
headersJSON.method = firstLine[0];
|
|
9
|
-
headersJSON.url = firstLine[1];
|
|
10
|
-
headersJSON.protocol = firstLine[2];
|
|
11
|
-
|
|
12
|
-
for (let item of headersArray) {
|
|
13
|
-
let temp = item.split(':');
|
|
14
|
-
|
|
15
|
-
if (temp.length > 1) {
|
|
16
|
-
|
|
17
|
-
let key = temp.shift();
|
|
18
|
-
let value = temp.join(':');
|
|
19
|
-
|
|
20
|
-
headersJSON[key] = value.trim();
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
return headersJSON;
|
|
26
|
-
};
|
|
1
|
+
module.exports = function (headersStr) {
|
|
2
|
+
|
|
3
|
+
let headersArray = headersStr.split(/\r{0,1}\n/);
|
|
4
|
+
let headersJSON = {};
|
|
5
|
+
|
|
6
|
+
// 第一行的内容:GET / HTTP/1.1
|
|
7
|
+
let firstLine = headersArray.shift().split(' ');
|
|
8
|
+
headersJSON.method = firstLine[0];
|
|
9
|
+
headersJSON.url = firstLine[1];
|
|
10
|
+
headersJSON.protocol = firstLine[2];
|
|
11
|
+
|
|
12
|
+
for (let item of headersArray) {
|
|
13
|
+
let temp = item.split(':');
|
|
14
|
+
|
|
15
|
+
if (temp.length > 1) {
|
|
16
|
+
|
|
17
|
+
let key = temp.shift();
|
|
18
|
+
let value = temp.join(':');
|
|
19
|
+
|
|
20
|
+
headersJSON[key] = value.trim();
|
|
21
|
+
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return headersJSON;
|
|
26
|
+
};
|
package/bin/WebSocket/index.js
CHANGED
|
@@ -1,80 +1,80 @@
|
|
|
1
|
-
const net = require('net');
|
|
2
|
-
const crypto = require('crypto');
|
|
3
|
-
|
|
4
|
-
const headersToJSON = require('./headersToJSON');
|
|
5
|
-
const decodeWsFrame = require('./decodeWsFrame');
|
|
6
|
-
const encodeWsFrame = require('./encodeWsFrame');
|
|
7
|
-
|
|
8
|
-
module.exports = function (port, name, version) {
|
|
9
|
-
let sockets = [];
|
|
10
|
-
|
|
11
|
-
let handler = {
|
|
12
|
-
notifyBrowser: (msg) => {
|
|
13
|
-
for (let socket of sockets) {
|
|
14
|
-
socket.write(encodeWsFrame(msg));
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const server = net.createServer(socket => {
|
|
20
|
-
|
|
21
|
-
// 错误
|
|
22
|
-
socket.on('error', (error) => {
|
|
23
|
-
console.log('Connection error:', error.message);
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
// 数据
|
|
27
|
-
socket.on('data', buffer => {
|
|
28
|
-
console.log('Connection data:' + (decodeWsFrame(buffer).payloadData?.toString() || "断开连接......"));
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
// 连接
|
|
32
|
-
socket.once('data', buffer => {
|
|
33
|
-
|
|
34
|
-
// 把请求头变成容易操作的json
|
|
35
|
-
const headers = headersToJSON(buffer.toString());
|
|
36
|
-
|
|
37
|
-
// 如果是ws请求
|
|
38
|
-
// 就需要建立连接
|
|
39
|
-
if (headers.Upgrade == 'websocket') {
|
|
40
|
-
|
|
41
|
-
if (headers['Sec-WebSocket-Version'] !== '13') {
|
|
42
|
-
console.log('\r\n警告:当前WebSocket版本为' + headers['Sec-WebSocket-Version'] + ",可能存在兼容问题~\r\n");
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// 开始连接
|
|
46
|
-
/*
|
|
47
|
-
协议中规定的校验用GUID,可参考如下链接:
|
|
48
|
-
https://tools.ietf.org/html/rfc6455#section-5.5.2
|
|
49
|
-
https://stackoverflow.com/questions/13456017/what-does-258eafa5-e914-47da-95ca-c5ab0dc85b11-means-in-websocket-protocol
|
|
50
|
-
*/
|
|
51
|
-
const GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
|
|
52
|
-
const key = headers['Sec-WebSocket-Key'];
|
|
53
|
-
const hash = crypto.createHash('sha1'); // 创建一个签名算法为sha1的哈希对象
|
|
54
|
-
|
|
55
|
-
hash.update(`${key}${GUID}`); // 将key和GUID连接后,更新到hash
|
|
56
|
-
const result = hash.digest('base64'); // 生成base64字符串
|
|
57
|
-
const header = `HTTP/1.1 101 Switching Protocols\r\nServer: Powered by ${name}@${version}\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-Websocket-Accept: ${result}\r\n\r\n`; // 生成供前端校验用的请求头
|
|
58
|
-
|
|
59
|
-
socket.write(header); // 返回HTTP头,告知客户端校验结果,HTTP状态码101表示切换协议:https://httpstatuses.com/101。
|
|
60
|
-
// 若客户端校验结果正确,在控制台的Network模块可以看到HTTP请求的状态码变为101 Switching Protocols,同时客户端的ws.onopen事件被触发。
|
|
61
|
-
|
|
62
|
-
sockets.push(socket);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// 否则就普通的,
|
|
66
|
-
// 普通的由http服务器提供支持
|
|
67
|
-
else {
|
|
68
|
-
socket.end();
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
// 启动
|
|
75
|
-
server.listen(port);
|
|
76
|
-
|
|
77
|
-
console.log('<i> \x1b[1m\x1b[32m[' + name + '] Loopback: \x1b[36mws://localhost:' + port + '/\x1b[0m\n');
|
|
78
|
-
|
|
79
|
-
return handler;
|
|
80
|
-
};
|
|
1
|
+
const net = require('net');
|
|
2
|
+
const crypto = require('crypto');
|
|
3
|
+
|
|
4
|
+
const headersToJSON = require('./headersToJSON');
|
|
5
|
+
const decodeWsFrame = require('./decodeWsFrame');
|
|
6
|
+
const encodeWsFrame = require('./encodeWsFrame');
|
|
7
|
+
|
|
8
|
+
module.exports = function (port, name, version) {
|
|
9
|
+
let sockets = [];
|
|
10
|
+
|
|
11
|
+
let handler = {
|
|
12
|
+
notifyBrowser: (msg) => {
|
|
13
|
+
for (let socket of sockets) {
|
|
14
|
+
socket.write(encodeWsFrame(msg));
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const server = net.createServer(socket => {
|
|
20
|
+
|
|
21
|
+
// 错误
|
|
22
|
+
socket.on('error', (error) => {
|
|
23
|
+
console.log('Connection error:', error.message);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// 数据
|
|
27
|
+
socket.on('data', buffer => {
|
|
28
|
+
console.log('Connection data:' + (decodeWsFrame(buffer).payloadData?.toString() || "断开连接......"));
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
// 连接
|
|
32
|
+
socket.once('data', buffer => {
|
|
33
|
+
|
|
34
|
+
// 把请求头变成容易操作的json
|
|
35
|
+
const headers = headersToJSON(buffer.toString());
|
|
36
|
+
|
|
37
|
+
// 如果是ws请求
|
|
38
|
+
// 就需要建立连接
|
|
39
|
+
if (headers.Upgrade == 'websocket') {
|
|
40
|
+
|
|
41
|
+
if (headers['Sec-WebSocket-Version'] !== '13') {
|
|
42
|
+
console.log('\r\n警告:当前WebSocket版本为' + headers['Sec-WebSocket-Version'] + ",可能存在兼容问题~\r\n");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// 开始连接
|
|
46
|
+
/*
|
|
47
|
+
协议中规定的校验用GUID,可参考如下链接:
|
|
48
|
+
https://tools.ietf.org/html/rfc6455#section-5.5.2
|
|
49
|
+
https://stackoverflow.com/questions/13456017/what-does-258eafa5-e914-47da-95ca-c5ab0dc85b11-means-in-websocket-protocol
|
|
50
|
+
*/
|
|
51
|
+
const GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
|
|
52
|
+
const key = headers['Sec-WebSocket-Key'];
|
|
53
|
+
const hash = crypto.createHash('sha1'); // 创建一个签名算法为sha1的哈希对象
|
|
54
|
+
|
|
55
|
+
hash.update(`${key}${GUID}`); // 将key和GUID连接后,更新到hash
|
|
56
|
+
const result = hash.digest('base64'); // 生成base64字符串
|
|
57
|
+
const header = `HTTP/1.1 101 Switching Protocols\r\nServer: Powered by ${name}@${version}\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-Websocket-Accept: ${result}\r\n\r\n`; // 生成供前端校验用的请求头
|
|
58
|
+
|
|
59
|
+
socket.write(header); // 返回HTTP头,告知客户端校验结果,HTTP状态码101表示切换协议:https://httpstatuses.com/101。
|
|
60
|
+
// 若客户端校验结果正确,在控制台的Network模块可以看到HTTP请求的状态码变为101 Switching Protocols,同时客户端的ws.onopen事件被触发。
|
|
61
|
+
|
|
62
|
+
sockets.push(socket);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// 否则就普通的,
|
|
66
|
+
// 普通的由http服务器提供支持
|
|
67
|
+
else {
|
|
68
|
+
socket.end();
|
|
69
|
+
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// 启动
|
|
75
|
+
server.listen(port);
|
|
76
|
+
|
|
77
|
+
console.log('<i> \x1b[1m\x1b[32m[' + name + '] Loopback: \x1b[36mws://localhost:' + port + '/\x1b[0m\n');
|
|
78
|
+
|
|
79
|
+
return handler;
|
|
80
|
+
};
|