file-handle 0.0.1
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/README.md +91 -0
- package/README.zh-CN.md +93 -0
- package/package.json +36 -0
- package/server/bin/cli.js +3 -0
- package/server/common/adapter.js +90 -0
- package/server/common/i18n.js +109 -0
- package/server/config.js +123 -0
- package/server/fh.app.dat +0 -0
- package/server/index.js +153 -0
- package/server/middlewares/ipValidation.js +33 -0
- package/server/middlewares/security.js +172 -0
- package/server/middlewares/upload.js +57 -0
- package/server/routes/api.js +174 -0
- package/server/routes/fh.js +82 -0
- package/server/routes/res.js +18 -0
- package/server/types/d.js +1 -0
- package/server/types/index.js +83 -0
- package/server/types/response.js +20 -0
- package/server/utils/Preview/Markdown/registrie.js +8 -0
- package/server/utils/Preview/Text/registrie.js +92 -0
- package/server/utils/Preview/types.js +1 -0
- package/server/utils/logger.js +55 -0
- package/server/utils/path.js +62 -0
package/README.md
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
|
|
2
|
+
# File-Handle
|
|
3
|
+
|
|
4
|
+

|
|
5
|
+
|
|
6
|
+
A simple local web file server with a user-friendly interface, allowing easy file upload, download, preview, and management through a browser.
|
|
7
|
+
|
|
8
|
+
[中文文档](README.zh-CN.md)
|
|
9
|
+
|
|
10
|
+
## Completed Features
|
|
11
|
+
|
|
12
|
+
- √ Quick start of a local file server
|
|
13
|
+
- √ Intuitive web interface with drag-and-drop upload support
|
|
14
|
+
- √ Support for file and directory upload and download
|
|
15
|
+
- √ File preview functionality (images, videos, audio, Markdown, etc.)
|
|
16
|
+
- X File search and filtering
|
|
17
|
+
- √ Directory compression and download
|
|
18
|
+
- √ Responsive design, supporting mobile access
|
|
19
|
+
- X Optional file deletion and editing functionality
|
|
20
|
+
- X Secure file operations and access control
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install -g file-handle
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
### Basic Usage
|
|
31
|
+
|
|
32
|
+
Run the following command in the directory you wish to share to start the file server:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
file-handle
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
This will start the server on the default port (6688) and use the current working directory as the root directory.
|
|
39
|
+
|
|
40
|
+
### Command Line Options
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
file-handle
|
|
44
|
+
-d <directory path>
|
|
45
|
+
-p <port number>
|
|
46
|
+
-i <IP addresses, ...>
|
|
47
|
+
-w
|
|
48
|
+
--tls <certificate path> <key path>
|
|
49
|
+
--del
|
|
50
|
+
--password <password>
|
|
51
|
+
--edit
|
|
52
|
+
--limit <file size limit>
|
|
53
|
+
--log
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
#### Parameter Description
|
|
57
|
+
|
|
58
|
+
- `-d`: Specify the root directory of the server, default is the current working directory
|
|
59
|
+
- `-p`: Specify the port number the server listens to, default is 6688
|
|
60
|
+
- `-i`: Specify allowed IP addresses, separated by commas (null by default, all IP allowed addresses)
|
|
61
|
+
- `-w`: Allow public network access (disabled by default)
|
|
62
|
+
- `--tls`: Enable HTTPS secure transmission, requires certificate and key file paths
|
|
63
|
+
- `--del`: Enable file deletion functionality (disabled by default)
|
|
64
|
+
- `--password`: Set the access password for the web server, default is empty
|
|
65
|
+
- `--edit`: Enable file editing functionality (disabled by default)
|
|
66
|
+
- `--limit`: Set the file upload size limit, e.g., 300MB (supports PB, TB, GB, MB, KB units, default is 3GB)
|
|
67
|
+
- `--log`: Keep log files (disabled by default)
|
|
68
|
+
|
|
69
|
+
### Access URLs
|
|
70
|
+
|
|
71
|
+
- Main interface: `http://localhost:6688`
|
|
72
|
+
- File access: `/fh/[filepath]`
|
|
73
|
+
- API interface: `/api/*`
|
|
74
|
+
|
|
75
|
+
## License
|
|
76
|
+
|
|
77
|
+
Copyright [gaoyuan]
|
|
78
|
+
Link: www.npmjs.com/package/file-handle
|
|
79
|
+
|
|
80
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
81
|
+
you may not use this file except in compliance with the License.
|
|
82
|
+
You may obtain a copy of the License at
|
|
83
|
+
|
|
84
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
85
|
+
|
|
86
|
+
Unless required by applicable law or agreed to in writing, software
|
|
87
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
88
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
89
|
+
See the License for the specific language governing permissions and
|
|
90
|
+
limitations under the License.
|
|
91
|
+
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# File-Handle
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
一个功能简单的本地Web文件服务器,友好的界面,方便通过浏览器来对文件进行上传、下载、预览和管理。
|
|
6
|
+
|
|
7
|
+
## 已经开发完成的特性
|
|
8
|
+
|
|
9
|
+
- √ 快速启动本地文件服务器
|
|
10
|
+
- √ 直观的Web界面,支持拖拽上传
|
|
11
|
+
- √ 支持文件和目录的上传、下载
|
|
12
|
+
- √ 文件预览功能(图片、视频、音频、Markdown等)
|
|
13
|
+
- X 文件搜索和过滤
|
|
14
|
+
- √ 目录压缩下载
|
|
15
|
+
- √ 响应式设计,支持移动端访问
|
|
16
|
+
- X 可选的文件删除和编辑功能
|
|
17
|
+
- X 安全的文件操作和访问控制
|
|
18
|
+
|
|
19
|
+
## 安装
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install -g file-handle
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## 使用方法
|
|
26
|
+
|
|
27
|
+
### 基本使用
|
|
28
|
+
|
|
29
|
+
在希望共享的目录下运行以下命令,以启动文件服务器:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
> file-handle
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
> fh
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
这将在默认端口(6688)启动服务器,并使用当前工作目录作为根目录。
|
|
40
|
+
|
|
41
|
+
### 命令行选项
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
> file-handle
|
|
45
|
+
-d <目录路径>
|
|
46
|
+
-p <端口号>
|
|
47
|
+
-i <IP地址, ...>
|
|
48
|
+
-w
|
|
49
|
+
--tls <证书路径> <密钥路径>
|
|
50
|
+
--del
|
|
51
|
+
--password <密码>
|
|
52
|
+
--edit
|
|
53
|
+
--limit <文件大小限制>
|
|
54
|
+
--log
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
#### 参数说明
|
|
58
|
+
|
|
59
|
+
- `-d`:指定服务器的根目录,默认为当前工作目录
|
|
60
|
+
- `-p`:指定服务器监听的端口号,默认为6688
|
|
61
|
+
- `-i`:指定服务器允许访问的IP地址,多个IP地址用逗号分隔,默认为空(即允许所有IP访问)
|
|
62
|
+
- `-w`:允许公网访问(默认关闭)
|
|
63
|
+
- `--tls`:启用HTTPS安全传输,需提供证书和密钥文件路径
|
|
64
|
+
- `--del`:启用文件删除功能(默认关闭)
|
|
65
|
+
- `--password`:设置Web服务器的访问密码,默认为空
|
|
66
|
+
- `--edit`:启用文件编辑修改功能(默认关闭)
|
|
67
|
+
- `--limit`:设置文件上传大小限制,如:300MB(支持PB、TB、GB、MB、KB单位,默认3GB)
|
|
68
|
+
- `--log`:生成日志文件(默认关闭)
|
|
69
|
+
|
|
70
|
+
### 访问地址
|
|
71
|
+
|
|
72
|
+
- 主界面:`http://localhost:6688`
|
|
73
|
+
- 文件访问:`/fh/[filepath]`
|
|
74
|
+
- API接口:`/api/*`
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
## 许可证
|
|
78
|
+
|
|
79
|
+
Copyright [gaoyuan]
|
|
80
|
+
|
|
81
|
+
Link: www.npmjs.com/package/file-handle
|
|
82
|
+
|
|
83
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
84
|
+
you may not use this file except in compliance with the License.
|
|
85
|
+
You may obtain a copy of the License at
|
|
86
|
+
|
|
87
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
88
|
+
|
|
89
|
+
Unless required by applicable law or agreed to in writing, software
|
|
90
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
91
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
92
|
+
See the License for the specific language governing permissions and
|
|
93
|
+
limitations under the License.
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "file-handle",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"author": "jsonhandle@gmail.com",
|
|
5
|
+
"description": "一个简易的文件服务器,方便通过浏览器来浏览、上传或管理目录和文件. \nA simple local web file server with a user-friendly interface, allowing easy file upload, download, preview, and management through a browser.",
|
|
6
|
+
"main": "server/index.js",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"bin": {
|
|
9
|
+
"file-handle": "server/bin/cli.js",
|
|
10
|
+
"fh": "server/bin/cli.js"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/purplestone/File-Handle/issues"
|
|
14
|
+
},
|
|
15
|
+
"homepage": "https://github.com/purplestone/File-Handle",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/purplestone/File-Handle.git"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@electron/asar": "^3.3.1",
|
|
25
|
+
"archiver": "^7.0.1",
|
|
26
|
+
"cli-color": "^2.0.4",
|
|
27
|
+
"cookie-parser": "^1.4.7",
|
|
28
|
+
"express": "^4.18.2",
|
|
29
|
+
"http-proxy-middleware": "^3.0.3",
|
|
30
|
+
"mime-types": "^2.1.35",
|
|
31
|
+
"multer": "1.4.5-lts.1",
|
|
32
|
+
"winston": "^3.17.0",
|
|
33
|
+
"yargs": "^17.7.2"
|
|
34
|
+
},
|
|
35
|
+
"license": "Apache-2.0"
|
|
36
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { createProxyMiddleware } from 'http-proxy-middleware';
|
|
2
|
+
import { loadConfig } from '../config.js';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import { resolve } from 'path';
|
|
5
|
+
import { debug } from '../utils/logger.js';
|
|
6
|
+
import asar from '@electron/asar';
|
|
7
|
+
import mime from 'mime-types';
|
|
8
|
+
const isDevelopment = process.env.NODE_ENV === 'development';
|
|
9
|
+
let resMap;
|
|
10
|
+
export function buildPreviewerHtml(html) {
|
|
11
|
+
const modifiedBody = html.replace('<base id="baseUrl" />', `<base id="baseUrl" href="/fh/" />`);
|
|
12
|
+
return modifiedBody;
|
|
13
|
+
}
|
|
14
|
+
export function sendFeReq(uri, req, res, next, sendBefore) {
|
|
15
|
+
const config = loadConfig();
|
|
16
|
+
// 获取node入口目录的绝对路径
|
|
17
|
+
const fhDir = config.mdDir;
|
|
18
|
+
debug('sendFeReq uri: ' + uri + ' | path: ' + req.path);
|
|
19
|
+
if (!isDevelopment && !resMap) {
|
|
20
|
+
resMap = getRes(resolve(fhDir, './fh.app.dat'));
|
|
21
|
+
}
|
|
22
|
+
const bCb = !!sendBefore;
|
|
23
|
+
const filePath = req.path;
|
|
24
|
+
let resFilePath = req.path.slice(1);
|
|
25
|
+
const opt = {
|
|
26
|
+
target: 'http://localhost:5173',
|
|
27
|
+
changeOrigin: true,
|
|
28
|
+
ws: true,
|
|
29
|
+
pathRewrite: {
|
|
30
|
+
'^.*': '/res' + req.url
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
if ({}.toString.call(uri) == '[object String]') {
|
|
34
|
+
opt.pathRewrite = {
|
|
35
|
+
'^.*': '/res/' + uri
|
|
36
|
+
};
|
|
37
|
+
resFilePath = uri;
|
|
38
|
+
}
|
|
39
|
+
else if (!!uri) {
|
|
40
|
+
opt.pathRewrite = {};
|
|
41
|
+
for (const [key, value] of Object.entries(uri)) {
|
|
42
|
+
opt.pathRewrite[key] = `/res/${value}`;
|
|
43
|
+
// console.log(`Key: ${key}, Value: ${value}`);
|
|
44
|
+
}
|
|
45
|
+
Object.entries(opt.pathRewrite).some(([key, value]) => {
|
|
46
|
+
const sP = req.path.replace(new RegExp(key), value);
|
|
47
|
+
resFilePath = sP;
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
if (bCb) {
|
|
51
|
+
opt.on = {
|
|
52
|
+
proxyRes: (proxyRes, reqP, resP) => {
|
|
53
|
+
const body = [];
|
|
54
|
+
proxyRes.on('data', function (chunk) {
|
|
55
|
+
body.push(chunk);
|
|
56
|
+
});
|
|
57
|
+
proxyRes.on('end', function () {
|
|
58
|
+
const bodyString = Buffer.concat(body).toString();
|
|
59
|
+
// 在响应体中添加标记文本
|
|
60
|
+
const modifiedBody = sendBefore(bodyString, req, res);
|
|
61
|
+
res.end(modifiedBody);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
if (isDevelopment) {
|
|
67
|
+
createProxyMiddleware(opt)(req, res, next);
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
debug('req.path: ' + req.path);
|
|
71
|
+
debug('resFilePath: ' + resFilePath);
|
|
72
|
+
res.set('Content-Type', mime.lookup(resFilePath) || 'application/octet-stream');
|
|
73
|
+
res.end(resMap.get((resFilePath).replace(/\//g, '\\')));
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
// 获取asar资源
|
|
77
|
+
function getRes(asarPath) {
|
|
78
|
+
const aFilePath = asar.listPackage(asarPath, { isPack: false });
|
|
79
|
+
// 创建资源映射
|
|
80
|
+
const resourceMap = new Map();
|
|
81
|
+
aFilePath.forEach(filePath => {
|
|
82
|
+
filePath = filePath.replace(/[\\\/]/g, '/').split('/').filter(s => s.trim()).join(path.sep);
|
|
83
|
+
try {
|
|
84
|
+
resourceMap.set(filePath, asar.extractFile(asarPath, filePath));
|
|
85
|
+
}
|
|
86
|
+
catch (error) { }
|
|
87
|
+
});
|
|
88
|
+
asar.uncacheAll();
|
|
89
|
+
return resourceMap;
|
|
90
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
const i18n = {
|
|
2
|
+
'zh-CN': {
|
|
3
|
+
ui: {
|
|
4
|
+
title: 'File-Handle',
|
|
5
|
+
passwordPlaceholder: '请输入访问密码',
|
|
6
|
+
submit: '确认',
|
|
7
|
+
switchLang: 'Switch to English'
|
|
8
|
+
},
|
|
9
|
+
server: {
|
|
10
|
+
startingHttps: '正在启动HTTPS服务器...',
|
|
11
|
+
startingHttp: '正在启动HTTP服务器...',
|
|
12
|
+
running: 'File-Handle Server正在运行,端口:',
|
|
13
|
+
rootDirectory: '根目录:',
|
|
14
|
+
accessibleAddresses: '可访问的地址:',
|
|
15
|
+
allowedIpsOnly: '仅允许指定IP访问',
|
|
16
|
+
localAndLanOnly: '仅允许本地和局域网访问',
|
|
17
|
+
loginPassword: '登录密码是:',
|
|
18
|
+
portInUse: '端口 {port} 正在使用中, 正在尝试下一个端口。'
|
|
19
|
+
},
|
|
20
|
+
config: {
|
|
21
|
+
allowWanDesc: '允许公网访问',
|
|
22
|
+
dirDesc: '指定服务器的根目录',
|
|
23
|
+
portDesc: '指定服务器监听的端口号',
|
|
24
|
+
ipDesc: '指定服务器允许访问的IP地址,多个IP地址用逗号分隔',
|
|
25
|
+
tlsDesc: '启用HTTPS安全传输,需提供证书和密钥文件路径',
|
|
26
|
+
delDesc: '启用文件删除功能',
|
|
27
|
+
passwordDesc: '设置服务器访问的密码',
|
|
28
|
+
editDesc: '启用文件编辑修改功能',
|
|
29
|
+
logDesc: '保持日志文件',
|
|
30
|
+
limitDesc: '设置文件上传大小限制'
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
'en-US': {
|
|
34
|
+
ui: {
|
|
35
|
+
title: 'File-Handle',
|
|
36
|
+
passwordPlaceholder: 'Enter access password',
|
|
37
|
+
submit: 'Submit',
|
|
38
|
+
switchLang: '切换到中文'
|
|
39
|
+
},
|
|
40
|
+
server: {
|
|
41
|
+
startingHttps: 'Starting HTTPS server...',
|
|
42
|
+
startingHttp: 'Starting HTTP server...',
|
|
43
|
+
running: 'File-Handle Server is running on port',
|
|
44
|
+
rootDirectory: 'Root directory:',
|
|
45
|
+
accessibleAddresses: 'Accessible addresses:',
|
|
46
|
+
allowedIpsOnly: 'Only allowed IPs can access',
|
|
47
|
+
localAndLanOnly: 'Only local and LAN access allowed',
|
|
48
|
+
loginPassword: 'Login password is:',
|
|
49
|
+
portInUse: 'Port {port} is being used, trying next port...'
|
|
50
|
+
},
|
|
51
|
+
config: {
|
|
52
|
+
allowWanDesc: 'Allow public network access',
|
|
53
|
+
dirDesc: 'Specify server root directory',
|
|
54
|
+
portDesc: 'Specify server listening port',
|
|
55
|
+
ipDesc: 'Specify allowed IP addresses, separated by commas',
|
|
56
|
+
tlsDesc: 'Enable HTTPS secure transmission, requires certificate and key file paths',
|
|
57
|
+
delDesc: 'Enable file deletion',
|
|
58
|
+
passwordDesc: 'Set server access password',
|
|
59
|
+
editDesc: 'Enable file editing',
|
|
60
|
+
logDesc: 'Keep log file',
|
|
61
|
+
limitDesc: 'Set file upload size limit'
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
// 获取系统语言
|
|
66
|
+
function getSystemLanguage() {
|
|
67
|
+
const locale = getSystemLocale();
|
|
68
|
+
return locale?.startsWith('zh') ? 'zh-CN' : 'en-US';
|
|
69
|
+
}
|
|
70
|
+
// 当前语言
|
|
71
|
+
let currentLanguage = getSystemLanguage();
|
|
72
|
+
// 获取当前语言的文本
|
|
73
|
+
export function getCurrentLanguage() {
|
|
74
|
+
return currentLanguage;
|
|
75
|
+
}
|
|
76
|
+
// 设置当前语言
|
|
77
|
+
export function setLanguage(lang) {
|
|
78
|
+
currentLanguage = lang;
|
|
79
|
+
}
|
|
80
|
+
// 获取文本
|
|
81
|
+
export function getText() {
|
|
82
|
+
return i18n[currentLanguage];
|
|
83
|
+
}
|
|
84
|
+
// 获取语言包
|
|
85
|
+
export function getPack(name) {
|
|
86
|
+
const packs = [].concat(name);
|
|
87
|
+
const res = {};
|
|
88
|
+
packs.forEach((pack) => {
|
|
89
|
+
for (const lang in i18n) {
|
|
90
|
+
res[lang] = res[lang] || {};
|
|
91
|
+
// @ts-ignore
|
|
92
|
+
res[lang][pack] = i18n[lang][pack];
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
return res;
|
|
96
|
+
}
|
|
97
|
+
function getSystemLocale() {
|
|
98
|
+
try {
|
|
99
|
+
// Windows
|
|
100
|
+
if (process.platform === 'win32') {
|
|
101
|
+
return Intl.DateTimeFormat().resolvedOptions().locale;
|
|
102
|
+
}
|
|
103
|
+
// macOS/Linux
|
|
104
|
+
return (process.env.LANG || '').split('.')[0].replace('_', '-');
|
|
105
|
+
}
|
|
106
|
+
catch (e) {
|
|
107
|
+
return 'en-US'; // 默认值
|
|
108
|
+
}
|
|
109
|
+
}
|
package/server/config.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import yargs from 'yargs';
|
|
2
|
+
import { hideBin } from 'yargs/helpers';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import crypto from 'crypto';
|
|
5
|
+
import { fileURLToPath } from 'url';
|
|
6
|
+
import { getText } from './common/i18n.js';
|
|
7
|
+
const runtimeConfig = {
|
|
8
|
+
config: null
|
|
9
|
+
};
|
|
10
|
+
// 文件大小单位转换
|
|
11
|
+
const sizeUnits = {
|
|
12
|
+
PB: 1024 * 1024 * 1024 * 1024 * 1024,
|
|
13
|
+
TB: 1024 * 1024 * 1024 * 1024,
|
|
14
|
+
GB: 1024 * 1024 * 1024,
|
|
15
|
+
MB: 1024 * 1024,
|
|
16
|
+
KB: 1024,
|
|
17
|
+
B: 1
|
|
18
|
+
};
|
|
19
|
+
// 解析文件大小字符串
|
|
20
|
+
function parseFileSize(sizeStr) {
|
|
21
|
+
if (sizeStr.toLowerCase() === 'no')
|
|
22
|
+
return Infinity;
|
|
23
|
+
const match = sizeStr.match(/^(\d+)\s*([PTGMK]B|B)?$/i);
|
|
24
|
+
if (!match)
|
|
25
|
+
throw new Error('Invalid file size format');
|
|
26
|
+
const size = parseInt(match[1]);
|
|
27
|
+
const unit = (match[2] || 'B').toUpperCase();
|
|
28
|
+
return size * (sizeUnits[unit] || 1);
|
|
29
|
+
}
|
|
30
|
+
// 加载配置
|
|
31
|
+
export function loadConfig(opt) {
|
|
32
|
+
if (runtimeConfig.config) {
|
|
33
|
+
return runtimeConfig.config;
|
|
34
|
+
}
|
|
35
|
+
const lang = getText();
|
|
36
|
+
const argv = yargs(hideBin(process.argv))
|
|
37
|
+
.options({
|
|
38
|
+
'w': {
|
|
39
|
+
alias: 'wan',
|
|
40
|
+
type: 'boolean',
|
|
41
|
+
description: lang.config.allowWanDesc,
|
|
42
|
+
default: false
|
|
43
|
+
},
|
|
44
|
+
'd': {
|
|
45
|
+
alias: 'dir',
|
|
46
|
+
type: 'string',
|
|
47
|
+
description: lang.config.dirDesc,
|
|
48
|
+
default: process.cwd()
|
|
49
|
+
},
|
|
50
|
+
'p': {
|
|
51
|
+
alias: 'port',
|
|
52
|
+
type: 'number',
|
|
53
|
+
description: lang.config.portDesc,
|
|
54
|
+
default: 6688
|
|
55
|
+
},
|
|
56
|
+
'i': {
|
|
57
|
+
alias: 'ip',
|
|
58
|
+
type: 'string',
|
|
59
|
+
description: lang.config.ipDesc
|
|
60
|
+
},
|
|
61
|
+
'tls': {
|
|
62
|
+
type: 'array',
|
|
63
|
+
description: lang.config.tlsDesc
|
|
64
|
+
},
|
|
65
|
+
'del': {
|
|
66
|
+
type: 'boolean',
|
|
67
|
+
description: lang.config.delDesc,
|
|
68
|
+
default: false
|
|
69
|
+
},
|
|
70
|
+
'password': {
|
|
71
|
+
type: 'string',
|
|
72
|
+
description: lang.config.passwordDesc
|
|
73
|
+
},
|
|
74
|
+
'edit': {
|
|
75
|
+
type: 'boolean',
|
|
76
|
+
description: lang.config.editDesc,
|
|
77
|
+
default: false
|
|
78
|
+
},
|
|
79
|
+
'log': {
|
|
80
|
+
type: 'boolean',
|
|
81
|
+
description: lang.config.logDesc,
|
|
82
|
+
default: false
|
|
83
|
+
},
|
|
84
|
+
'limit': {
|
|
85
|
+
type: 'string',
|
|
86
|
+
description: lang.config.limitDesc,
|
|
87
|
+
default: '3GB'
|
|
88
|
+
}
|
|
89
|
+
})
|
|
90
|
+
.help()
|
|
91
|
+
.argv;
|
|
92
|
+
const config = {
|
|
93
|
+
rootDir: path.resolve(argv.d),
|
|
94
|
+
mdDir: path.dirname(fileURLToPath(import.meta.url)),
|
|
95
|
+
port: argv.p,
|
|
96
|
+
allowDelete: argv.del,
|
|
97
|
+
allowEdit: argv.edit,
|
|
98
|
+
writeLog: argv.log ? true : undefined,
|
|
99
|
+
password: argv.password === '' ? createRandomPassword(16) : argv.password,
|
|
100
|
+
uploadLimit: parseFileSize(argv.limit),
|
|
101
|
+
allowWan: argv.w || opt?.allowWan || false,
|
|
102
|
+
allowedIPs: argv.i ? argv.i.split(',').map((ip) => ip.trim()) : undefined
|
|
103
|
+
};
|
|
104
|
+
// 配置TLS
|
|
105
|
+
if (Array.isArray(argv.tls) && argv.tls.length === 2) {
|
|
106
|
+
config.tls = {
|
|
107
|
+
cert: path.resolve(argv.tls[0]),
|
|
108
|
+
key: path.resolve(argv.tls[1])
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
runtimeConfig.config = config;
|
|
112
|
+
return config;
|
|
113
|
+
}
|
|
114
|
+
function createRandomPassword(n) {
|
|
115
|
+
// 生成一个包括大小写字母和数字的n位随机密码
|
|
116
|
+
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
117
|
+
let password = '';
|
|
118
|
+
for (let i = 0; i < n; i++) {
|
|
119
|
+
const randomIndex = crypto.randomInt(chars.length);
|
|
120
|
+
password += chars[randomIndex];
|
|
121
|
+
}
|
|
122
|
+
return password;
|
|
123
|
+
}
|
|
Binary file
|