@oipage/cli 0.3.0-alpha.4 → 0.3.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 +47 -47
- package/LICENSE +20 -20
- package/README.md +132 -132
- 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 +106 -106
- package/bin/data/mineTypes.json +104 -104
- package/bin/disk.js +42 -42
- package/bin/help.js +55 -55
- package/bin/intercept.js +15 -15
- package/bin/network.js +21 -21
- package/bin/notebook/index.js +8 -8
- package/bin/proxy.js +82 -82
- package/bin/run +146 -146
- package/bin/serve.js +318 -318
- package/bin/template/404.html +184 -184
- package/bin/tools/deleteEmptyFolder.js +33 -33
- package/bin/tools/format.js +75 -75
- package/bin/tools/isBinary.js +83 -83
- package/bin/tools/network.js +42 -42
- package/bin/tools/openBrowser.js +34 -34
- package/bin/tools/resolve404.js +98 -98
- package/bin/tools/resolveImport.js +96 -96
- package/bin/website-htmls/components/ui-select-file/index.html +8 -8
- package/bin/website-htmls/components/ui-select-file/index.js +42 -42
- 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 +140 -140
- 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 +7 -7
- package/bin/website-htmls/pages/appStore/index.js +19 -19
- package/bin/website-htmls/pages/appStore/index.scss +54 -54
- 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 +71 -71
- package/bin/website-htmls/pages/home/index.html +19 -19
- package/bin/website-htmls/pages/home/index.js +19 -19
- package/bin/website-htmls/pages/home/index.scss +87 -87
- 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 +69 -69
- 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 +9 -9
- package/bin/website-htmls/pages/recorder-screen/index.html +15 -15
- package/bin/website-htmls/pages/recorder-screen/index.js +227 -227
- package/bin/website-htmls/pages/recorder-screen/index.scss +52 -52
- package/bin/website-htmls/router.config.js +29 -29
- package/bin/website-htmls/styles/common.css +44 -44
- 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 +55 -55
- 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/docs/config.md +106 -106
- package/package.json +42 -42
- package/src/index.js +29 -29
- package/types/serve.d.ts +83 -83
package/bin/tools/isBinary.js
CHANGED
|
@@ -1,83 +1,83 @@
|
|
|
1
|
-
const { openSync, readSync, closeSync } = require("fs");
|
|
2
|
-
const mineTypes = require("../data/mineTypes.json");
|
|
3
|
-
|
|
4
|
-
// 读取文件头部用于判断的字节数
|
|
5
|
-
const CHECK_BYTES = 4096;
|
|
6
|
-
|
|
7
|
-
// 文本类型白名单(即使 Content-Type 是 application/* 也视为文本)
|
|
8
|
-
// 仅在文件内容读取失败时作为回退判断依据
|
|
9
|
-
const TEXT_TYPES = new Set([
|
|
10
|
-
"application/javascript",
|
|
11
|
-
"application/json",
|
|
12
|
-
"application/xml",
|
|
13
|
-
"application/xhtml+xml",
|
|
14
|
-
"application/rss+xml",
|
|
15
|
-
"application/atom+xml",
|
|
16
|
-
"image/svg+xml",
|
|
17
|
-
"application/xspf+xml",
|
|
18
|
-
"application/vnd.google-earth.kml+xml"
|
|
19
|
-
]);
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* 通过后缀名回退判断(仅在文件内容读取失败时使用)
|
|
23
|
-
* @param {string} filepath 文件路径
|
|
24
|
-
* @returns {boolean} 是否为二进制文件
|
|
25
|
-
*/
|
|
26
|
-
function isBinaryByExtension(filepath) {
|
|
27
|
-
const dotName = (filepath.match(/\.([^.]+)$/) || [])[1] || "";
|
|
28
|
-
const fileType = mineTypes[dotName];
|
|
29
|
-
|
|
30
|
-
if (!fileType) {
|
|
31
|
-
// 未知类型默认视为二进制
|
|
32
|
-
return true;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
if (fileType.startsWith("text/")) {
|
|
36
|
-
return false;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
if (TEXT_TYPES.has(fileType)) {
|
|
40
|
-
return false;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return true;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* 判断文件是否是二进制文件
|
|
48
|
-
* 通过读取文件前若干字节,检查是否包含 NULL 字节(\0)来判断
|
|
49
|
-
* 文本文件(UTF-8/ASCII)通常不包含 NULL 字节,而二进制文件通常包含
|
|
50
|
-
* 如果文件读取失败,回退到后缀名判断
|
|
51
|
-
* @param {string} filepath 文件路径
|
|
52
|
-
* @returns {boolean} 是否为二进制文件
|
|
53
|
-
*/
|
|
54
|
-
module.exports = function isBinary(filepath) {
|
|
55
|
-
let fd;
|
|
56
|
-
try {
|
|
57
|
-
fd = openSync(filepath, 'r');
|
|
58
|
-
const buffer = Buffer.alloc(CHECK_BYTES);
|
|
59
|
-
const bytesRead = readSync(fd, buffer, 0, CHECK_BYTES, 0);
|
|
60
|
-
|
|
61
|
-
// 检查是否包含 NULL 字节
|
|
62
|
-
// NULL 字节是二进制文件的可靠标志,文本文件(UTF-8/ASCII)不会包含
|
|
63
|
-
for (let i = 0; i < bytesRead; i++) {
|
|
64
|
-
if (buffer[i] === 0) {
|
|
65
|
-
return true;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
return false;
|
|
70
|
-
} catch (e) {
|
|
71
|
-
// 文件读取失败时,回退到后缀名判断
|
|
72
|
-
console.log(e);
|
|
73
|
-
return isBinaryByExtension(filepath);
|
|
74
|
-
} finally {
|
|
75
|
-
if (fd !== undefined) {
|
|
76
|
-
try {
|
|
77
|
-
closeSync(fd);
|
|
78
|
-
} catch (e) {
|
|
79
|
-
// 关闭失败不影响判断结果
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
};
|
|
1
|
+
const { openSync, readSync, closeSync } = require("fs");
|
|
2
|
+
const mineTypes = require("../data/mineTypes.json");
|
|
3
|
+
|
|
4
|
+
// 读取文件头部用于判断的字节数
|
|
5
|
+
const CHECK_BYTES = 4096;
|
|
6
|
+
|
|
7
|
+
// 文本类型白名单(即使 Content-Type 是 application/* 也视为文本)
|
|
8
|
+
// 仅在文件内容读取失败时作为回退判断依据
|
|
9
|
+
const TEXT_TYPES = new Set([
|
|
10
|
+
"application/javascript",
|
|
11
|
+
"application/json",
|
|
12
|
+
"application/xml",
|
|
13
|
+
"application/xhtml+xml",
|
|
14
|
+
"application/rss+xml",
|
|
15
|
+
"application/atom+xml",
|
|
16
|
+
"image/svg+xml",
|
|
17
|
+
"application/xspf+xml",
|
|
18
|
+
"application/vnd.google-earth.kml+xml"
|
|
19
|
+
]);
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 通过后缀名回退判断(仅在文件内容读取失败时使用)
|
|
23
|
+
* @param {string} filepath 文件路径
|
|
24
|
+
* @returns {boolean} 是否为二进制文件
|
|
25
|
+
*/
|
|
26
|
+
function isBinaryByExtension(filepath) {
|
|
27
|
+
const dotName = (filepath.match(/\.([^.]+)$/) || [])[1] || "";
|
|
28
|
+
const fileType = mineTypes[dotName];
|
|
29
|
+
|
|
30
|
+
if (!fileType) {
|
|
31
|
+
// 未知类型默认视为二进制
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (fileType.startsWith("text/")) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (TEXT_TYPES.has(fileType)) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 判断文件是否是二进制文件
|
|
48
|
+
* 通过读取文件前若干字节,检查是否包含 NULL 字节(\0)来判断
|
|
49
|
+
* 文本文件(UTF-8/ASCII)通常不包含 NULL 字节,而二进制文件通常包含
|
|
50
|
+
* 如果文件读取失败,回退到后缀名判断
|
|
51
|
+
* @param {string} filepath 文件路径
|
|
52
|
+
* @returns {boolean} 是否为二进制文件
|
|
53
|
+
*/
|
|
54
|
+
module.exports = function isBinary(filepath) {
|
|
55
|
+
let fd;
|
|
56
|
+
try {
|
|
57
|
+
fd = openSync(filepath, 'r');
|
|
58
|
+
const buffer = Buffer.alloc(CHECK_BYTES);
|
|
59
|
+
const bytesRead = readSync(fd, buffer, 0, CHECK_BYTES, 0);
|
|
60
|
+
|
|
61
|
+
// 检查是否包含 NULL 字节
|
|
62
|
+
// NULL 字节是二进制文件的可靠标志,文本文件(UTF-8/ASCII)不会包含
|
|
63
|
+
for (let i = 0; i < bytesRead; i++) {
|
|
64
|
+
if (buffer[i] === 0) {
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return false;
|
|
70
|
+
} catch (e) {
|
|
71
|
+
// 文件读取失败时,回退到后缀名判断
|
|
72
|
+
console.log(e);
|
|
73
|
+
return isBinaryByExtension(filepath);
|
|
74
|
+
} finally {
|
|
75
|
+
if (fd !== undefined) {
|
|
76
|
+
try {
|
|
77
|
+
closeSync(fd);
|
|
78
|
+
} catch (e) {
|
|
79
|
+
// 关闭失败不影响判断结果
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
};
|
package/bin/tools/network.js
CHANGED
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
const { networkInterfaces } = require("os");
|
|
2
|
-
|
|
3
|
-
// 网络信息
|
|
4
|
-
module.exports = function (isAll) {
|
|
5
|
-
|
|
6
|
-
let infomation = {
|
|
7
|
-
IPv4: [],
|
|
8
|
-
IPv6: []
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
let networks = networkInterfaces();
|
|
12
|
-
|
|
13
|
-
let IPv4Had = {}, IPv6Had = {};
|
|
14
|
-
|
|
15
|
-
for (let typeName in networks) {
|
|
16
|
-
let network = networks[typeName]
|
|
17
|
-
for (let index = 0; index < network.length; index++) {
|
|
18
|
-
if (network[index].mac != "00:00:00:00:00:00" || isAll) {
|
|
19
|
-
if (network[index].family == 'IPv4') {
|
|
20
|
-
if (!IPv4Had[network[index].mac]) {
|
|
21
|
-
infomation.IPv4.push({
|
|
22
|
-
address: network[index].address,
|
|
23
|
-
mac: network[index].mac
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
IPv4Had[network[index].mac] = true;
|
|
27
|
-
}
|
|
28
|
-
} else if (network[index].family == 'IPv6') {
|
|
29
|
-
if (!IPv6Had[network[index].mac]) {
|
|
30
|
-
infomation.IPv6.push({
|
|
31
|
-
address: network[index].address,
|
|
32
|
-
mac: network[index].mac
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
IPv6Had[network[index].mac] = true;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return infomation;
|
|
1
|
+
const { networkInterfaces } = require("os");
|
|
2
|
+
|
|
3
|
+
// 网络信息
|
|
4
|
+
module.exports = function (isAll) {
|
|
5
|
+
|
|
6
|
+
let infomation = {
|
|
7
|
+
IPv4: [],
|
|
8
|
+
IPv6: []
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
let networks = networkInterfaces();
|
|
12
|
+
|
|
13
|
+
let IPv4Had = {}, IPv6Had = {};
|
|
14
|
+
|
|
15
|
+
for (let typeName in networks) {
|
|
16
|
+
let network = networks[typeName]
|
|
17
|
+
for (let index = 0; index < network.length; index++) {
|
|
18
|
+
if (network[index].mac != "00:00:00:00:00:00" || isAll) {
|
|
19
|
+
if (network[index].family == 'IPv4') {
|
|
20
|
+
if (!IPv4Had[network[index].mac]) {
|
|
21
|
+
infomation.IPv4.push({
|
|
22
|
+
address: network[index].address,
|
|
23
|
+
mac: network[index].mac
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
IPv4Had[network[index].mac] = true;
|
|
27
|
+
}
|
|
28
|
+
} else if (network[index].family == 'IPv6') {
|
|
29
|
+
if (!IPv6Had[network[index].mac]) {
|
|
30
|
+
infomation.IPv6.push({
|
|
31
|
+
address: network[index].address,
|
|
32
|
+
mac: network[index].mac
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
IPv6Had[network[index].mac] = true;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return infomation;
|
|
43
43
|
};
|
package/bin/tools/openBrowser.js
CHANGED
|
@@ -1,35 +1,35 @@
|
|
|
1
|
-
const { exec } = require('child_process');
|
|
2
|
-
const os = require('os');
|
|
3
|
-
|
|
4
|
-
module.exports = function (url) {
|
|
5
|
-
|
|
6
|
-
// 防止命令注入
|
|
7
|
-
// 对 URL 进行严格校验(仅允许 http:// 或 https:// 开头,拒绝特殊字符)
|
|
8
|
-
// 2026年8月9日 于南宁
|
|
9
|
-
if (/^http:\/\//.test(url) || /^https:\/\//.test(url)) {
|
|
10
|
-
|
|
11
|
-
const platform = os.platform();
|
|
12
|
-
let command;
|
|
13
|
-
|
|
14
|
-
switch (platform) {
|
|
15
|
-
case 'win32': // Windows
|
|
16
|
-
command = `start ${url}`;
|
|
17
|
-
break;
|
|
18
|
-
case 'darwin': // macOS
|
|
19
|
-
command = `open ${url}`;
|
|
20
|
-
break;
|
|
21
|
-
default: // Linux及其他
|
|
22
|
-
command = `xdg-open ${url}`;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
exec(command, (error) => {
|
|
26
|
-
if (error) {
|
|
27
|
-
console.error(`无法打开浏览器: ${error}`);
|
|
28
|
-
} else {
|
|
29
|
-
// todo
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
} else {
|
|
33
|
-
console.error('请输入正确的url');
|
|
34
|
-
}
|
|
1
|
+
const { exec } = require('child_process');
|
|
2
|
+
const os = require('os');
|
|
3
|
+
|
|
4
|
+
module.exports = function (url) {
|
|
5
|
+
|
|
6
|
+
// 防止命令注入
|
|
7
|
+
// 对 URL 进行严格校验(仅允许 http:// 或 https:// 开头,拒绝特殊字符)
|
|
8
|
+
// 2026年8月9日 于南宁
|
|
9
|
+
if (/^http:\/\//.test(url) || /^https:\/\//.test(url)) {
|
|
10
|
+
|
|
11
|
+
const platform = os.platform();
|
|
12
|
+
let command;
|
|
13
|
+
|
|
14
|
+
switch (platform) {
|
|
15
|
+
case 'win32': // Windows
|
|
16
|
+
command = `start ${url}`;
|
|
17
|
+
break;
|
|
18
|
+
case 'darwin': // macOS
|
|
19
|
+
command = `open ${url}`;
|
|
20
|
+
break;
|
|
21
|
+
default: // Linux及其他
|
|
22
|
+
command = `xdg-open ${url}`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
exec(command, (error) => {
|
|
26
|
+
if (error) {
|
|
27
|
+
console.error(`无法打开浏览器: ${error}`);
|
|
28
|
+
} else {
|
|
29
|
+
// todo
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
} else {
|
|
33
|
+
console.error('请输入正确的url');
|
|
34
|
+
}
|
|
35
35
|
}
|
package/bin/tools/resolve404.js
CHANGED
|
@@ -1,99 +1,99 @@
|
|
|
1
|
-
const { readdirSync, lstatSync, readFileSync, statSync } = require("fs");
|
|
2
|
-
const { join } = require("path");
|
|
3
|
-
const { formatByte, formatTime } = require("./format");
|
|
4
|
-
const mineTypes = require("../data/mineTypes.json");
|
|
5
|
-
|
|
6
|
-
// HTML 实体转义,防止 XSS
|
|
7
|
-
function escapeHtml(str) {
|
|
8
|
-
return String(str)
|
|
9
|
-
.replace(/&/g, "&")
|
|
10
|
-
.replace(/</g, "<")
|
|
11
|
-
.replace(/>/g, ">")
|
|
12
|
-
.replace(/"/g, """)
|
|
13
|
-
.replace(/'/g, "'");
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
// URL 路径编码,防止 href 属性注入
|
|
17
|
-
function encodePath(str) {
|
|
18
|
-
return encodeURIComponent(str);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const img_folder = "data:image/png;base64," + readFileSync(join(__dirname, "../images/folder.png")).toString('base64');
|
|
22
|
-
const img_file = "data:image/png;base64," + readFileSync(join(__dirname, "../images/file.png")).toString('base64');
|
|
23
|
-
|
|
24
|
-
const template_404 = readFileSync(join(__dirname, "../template/404.html"), {
|
|
25
|
-
encoding: "utf8"
|
|
26
|
-
})
|
|
27
|
-
.replace("${img_folder}", img_folder)
|
|
28
|
-
.replace("${img_file}", img_file);
|
|
29
|
-
|
|
30
|
-
module.exports = function (filePath, url, website, name, rawIp) {
|
|
31
|
-
|
|
32
|
-
let subItems = [];
|
|
33
|
-
|
|
34
|
-
try {
|
|
35
|
-
subItems = readdirSync(filePath);
|
|
36
|
-
console.log("<i> \x1b[1m\x1b[32m[" + name + "-http-server] Read Folder: " + url + '\x1b[0m ' + new Date().toLocaleString() + " " + rawIp);
|
|
37
|
-
} catch (e) {
|
|
38
|
-
console.log("<i> \x1b[1m\x1b[32m[" + name + "-http-server] Read " + (/\/$/.test(url) ? "Folder" : "File") + ": \x1b[35m" + url + ' 404 Not Found\x1b[0m ' + new Date().toLocaleString() + " " + rawIp);
|
|
39
|
-
try {
|
|
40
|
-
if (!/\/$/.test(url) || url === "/") {
|
|
41
|
-
filePath = join(filePath, "../");
|
|
42
|
-
subItems = readdirSync(filePath);
|
|
43
|
-
}
|
|
44
|
-
} catch (e) { }
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
let template = "";
|
|
48
|
-
for (let i in subItems) {
|
|
49
|
-
let isDirectory = lstatSync(join(filePath, subItems[i])).isDirectory();
|
|
50
|
-
let statObj = statSync(join(filePath, subItems[i]));
|
|
51
|
-
|
|
52
|
-
// 文件夹
|
|
53
|
-
if (isDirectory) {
|
|
54
|
-
template += `<tr class="folder">
|
|
55
|
-
<th class="name">
|
|
56
|
-
<a href='./${encodePath(subItems[i])}/'>${escapeHtml(subItems[i])}</a>
|
|
57
|
-
</th>
|
|
58
|
-
<th>
|
|
59
|
-
${formatTime(statObj.mtime)}
|
|
60
|
-
</th>
|
|
61
|
-
<th>
|
|
62
|
-
文件夹
|
|
63
|
-
</th>
|
|
64
|
-
<th>
|
|
65
|
-
-
|
|
66
|
-
</th>
|
|
67
|
-
<th>
|
|
68
|
-
<a href='./${encodePath(subItems[i])}/' class="btn">打开</a>
|
|
69
|
-
</th>
|
|
70
|
-
</tr>`;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// 文件
|
|
74
|
-
else {
|
|
75
|
-
let dotName = (subItems[i].match(/\.([^.]+)$/) || [])[1] || "";
|
|
76
|
-
|
|
77
|
-
template += `<tr class="file">
|
|
78
|
-
<th class="name">
|
|
79
|
-
<a href='./${encodePath(subItems[i])}'>${escapeHtml(subItems[i])}</a>
|
|
80
|
-
</th>
|
|
81
|
-
<th>
|
|
82
|
-
${formatTime(statObj.mtime)}
|
|
83
|
-
</th>
|
|
84
|
-
<th>
|
|
85
|
-
${mineTypes[dotName] || "text/plain"}
|
|
86
|
-
</th>
|
|
87
|
-
<th>
|
|
88
|
-
${formatByte(statObj.size)}
|
|
89
|
-
</th>
|
|
90
|
-
<th>
|
|
91
|
-
<a href='./${encodePath(subItems[i])}' class="btn">访问</a>
|
|
92
|
-
<a href='./${encodePath(subItems[i])}?download' class="btn" download='${escapeHtml(subItems[i])}'>下载</a>
|
|
93
|
-
</th>
|
|
94
|
-
</tr>`;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
return template_404.replace("${website}", website ? '<a class="website-link" href="/@website/index.html">打开应用市场</a>' : '').replace("${current}", escapeHtml(filePath)).replace("${template}", template);
|
|
1
|
+
const { readdirSync, lstatSync, readFileSync, statSync } = require("fs");
|
|
2
|
+
const { join } = require("path");
|
|
3
|
+
const { formatByte, formatTime } = require("./format");
|
|
4
|
+
const mineTypes = require("../data/mineTypes.json");
|
|
5
|
+
|
|
6
|
+
// HTML 实体转义,防止 XSS
|
|
7
|
+
function escapeHtml(str) {
|
|
8
|
+
return String(str)
|
|
9
|
+
.replace(/&/g, "&")
|
|
10
|
+
.replace(/</g, "<")
|
|
11
|
+
.replace(/>/g, ">")
|
|
12
|
+
.replace(/"/g, """)
|
|
13
|
+
.replace(/'/g, "'");
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// URL 路径编码,防止 href 属性注入
|
|
17
|
+
function encodePath(str) {
|
|
18
|
+
return encodeURIComponent(str);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const img_folder = "data:image/png;base64," + readFileSync(join(__dirname, "../images/folder.png")).toString('base64');
|
|
22
|
+
const img_file = "data:image/png;base64," + readFileSync(join(__dirname, "../images/file.png")).toString('base64');
|
|
23
|
+
|
|
24
|
+
const template_404 = readFileSync(join(__dirname, "../template/404.html"), {
|
|
25
|
+
encoding: "utf8"
|
|
26
|
+
})
|
|
27
|
+
.replace("${img_folder}", img_folder)
|
|
28
|
+
.replace("${img_file}", img_file);
|
|
29
|
+
|
|
30
|
+
module.exports = function (filePath, url, website, name, rawIp) {
|
|
31
|
+
|
|
32
|
+
let subItems = [];
|
|
33
|
+
|
|
34
|
+
try {
|
|
35
|
+
subItems = readdirSync(filePath);
|
|
36
|
+
console.log("<i> \x1b[1m\x1b[32m[" + name + "-http-server] Read Folder: " + url + '\x1b[0m ' + new Date().toLocaleString() + " " + rawIp);
|
|
37
|
+
} catch (e) {
|
|
38
|
+
console.log("<i> \x1b[1m\x1b[32m[" + name + "-http-server] Read " + (/\/$/.test(url) ? "Folder" : "File") + ": \x1b[35m" + url + ' 404 Not Found\x1b[0m ' + new Date().toLocaleString() + " " + rawIp);
|
|
39
|
+
try {
|
|
40
|
+
if (!/\/$/.test(url) || url === "/") {
|
|
41
|
+
filePath = join(filePath, "../");
|
|
42
|
+
subItems = readdirSync(filePath);
|
|
43
|
+
}
|
|
44
|
+
} catch (e) { }
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
let template = "";
|
|
48
|
+
for (let i in subItems) {
|
|
49
|
+
let isDirectory = lstatSync(join(filePath, subItems[i])).isDirectory();
|
|
50
|
+
let statObj = statSync(join(filePath, subItems[i]));
|
|
51
|
+
|
|
52
|
+
// 文件夹
|
|
53
|
+
if (isDirectory) {
|
|
54
|
+
template += `<tr class="folder">
|
|
55
|
+
<th class="name">
|
|
56
|
+
<a href='./${encodePath(subItems[i])}/'>${escapeHtml(subItems[i])}</a>
|
|
57
|
+
</th>
|
|
58
|
+
<th>
|
|
59
|
+
${formatTime(statObj.mtime)}
|
|
60
|
+
</th>
|
|
61
|
+
<th>
|
|
62
|
+
文件夹
|
|
63
|
+
</th>
|
|
64
|
+
<th>
|
|
65
|
+
-
|
|
66
|
+
</th>
|
|
67
|
+
<th>
|
|
68
|
+
<a href='./${encodePath(subItems[i])}/' class="btn">打开</a>
|
|
69
|
+
</th>
|
|
70
|
+
</tr>`;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// 文件
|
|
74
|
+
else {
|
|
75
|
+
let dotName = (subItems[i].match(/\.([^.]+)$/) || [])[1] || "";
|
|
76
|
+
|
|
77
|
+
template += `<tr class="file">
|
|
78
|
+
<th class="name">
|
|
79
|
+
<a href='./${encodePath(subItems[i])}'>${escapeHtml(subItems[i])}</a>
|
|
80
|
+
</th>
|
|
81
|
+
<th>
|
|
82
|
+
${formatTime(statObj.mtime)}
|
|
83
|
+
</th>
|
|
84
|
+
<th>
|
|
85
|
+
${mineTypes[dotName] || "text/plain"}
|
|
86
|
+
</th>
|
|
87
|
+
<th>
|
|
88
|
+
${formatByte(statObj.size)}
|
|
89
|
+
</th>
|
|
90
|
+
<th>
|
|
91
|
+
<a href='./${encodePath(subItems[i])}' class="btn">访问</a>
|
|
92
|
+
<a href='./${encodePath(subItems[i])}?download' class="btn" download='${escapeHtml(subItems[i])}'>下载</a>
|
|
93
|
+
</th>
|
|
94
|
+
</tr>`;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return template_404.replace("${website}", website ? '<a class="website-link" href="/@website/index.html">打开应用市场</a>' : '').replace("${current}", escapeHtml(filePath)).replace("${template}", template);
|
|
99
99
|
};
|