@lzwme/sserver 0.0.10 → 1.0.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/LICENSE +21 -21
- package/README.MD +72 -100
- package/bin/sserver.js +0 -0
- package/dist/assets/assets/localsend/style.css +2 -0
- package/dist/assets/assets/localsend/vendor/qr-code-styling.js +2 -0
- package/dist/assets/localsend/style.css +2 -0
- package/dist/assets/localsend/vendor/qr-code-styling.js +2 -0
- package/dist/cli.js +26 -4
- package/dist/config.d.ts +38 -1
- package/dist/config.js +54 -9
- package/dist/lib/get-cert.js +1 -1
- package/dist/lib/merge-config.d.ts +9 -0
- package/dist/lib/merge-config.js +66 -0
- package/dist/lib/utils.d.ts +27 -1
- package/dist/lib/utils.js +94 -14
- package/dist/localsend/audit.d.ts +36 -0
- package/dist/localsend/audit.js +104 -0
- package/dist/localsend/auth.d.ts +94 -0
- package/dist/localsend/auth.js +227 -0
- package/dist/localsend/cache.d.ts +34 -0
- package/dist/localsend/cache.js +269 -0
- package/dist/localsend/chunk.d.ts +114 -0
- package/dist/localsend/chunk.js +475 -0
- package/dist/localsend/cleaner.d.ts +19 -0
- package/dist/localsend/cleaner.js +80 -0
- package/dist/localsend/client/app.js +4131 -0
- package/dist/localsend/client/crypto.js +233 -0
- package/dist/localsend/client/favicon.png +0 -0
- package/dist/localsend/client/icons/icon-192.png +0 -0
- package/dist/localsend/client/icons/icon-512.png +0 -0
- package/dist/localsend/client/index.html +451 -0
- package/dist/localsend/client/manifest.webmanifest +33 -0
- package/dist/localsend/client/sw.js +128 -0
- package/dist/localsend/guard.d.ts +58 -0
- package/dist/localsend/guard.js +210 -0
- package/dist/localsend/index.d.ts +52 -0
- package/dist/localsend/index.js +1771 -0
- package/dist/localsend/manage.d.ts +96 -0
- package/dist/localsend/manage.js +304 -0
- package/dist/localsend/metrics.d.ts +26 -0
- package/dist/localsend/metrics.js +58 -0
- package/dist/localsend/paste.d.ts +72 -0
- package/dist/localsend/paste.js +125 -0
- package/dist/localsend/path.d.ts +33 -0
- package/dist/localsend/path.js +151 -0
- package/dist/localsend/query.d.ts +78 -0
- package/dist/localsend/query.js +147 -0
- package/dist/localsend/secret.d.ts +31 -0
- package/dist/localsend/secret.js +96 -0
- package/dist/localsend/settings.d.ts +53 -0
- package/dist/localsend/settings.js +285 -0
- package/dist/localsend/share.d.ts +35 -0
- package/dist/localsend/share.js +117 -0
- package/dist/localsend/temp.d.ts +10 -0
- package/dist/localsend/temp.js +47 -0
- package/dist/localsend/ticket.d.ts +40 -0
- package/dist/localsend/ticket.js +98 -0
- package/dist/localsend/trash.d.ts +39 -0
- package/dist/localsend/trash.js +261 -0
- package/dist/localsend/webdav.d.ts +50 -0
- package/dist/localsend/webdav.js +530 -0
- package/dist/localsend/zip.d.ts +20 -0
- package/dist/localsend/zip.js +186 -0
- package/dist/proxy/php-proxy.js +54 -21
- package/dist/static-server.js +10 -3
- package/package.json +21 -13
- package/dist/localsend/index.html +0 -481
- package/dist/localsend.d.ts +0 -2
- package/dist/localsend.js +0 -148
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 内部目录名(位于存储目录下,但不参与文件列表、WebDAV 目录浏览与自动清理)。
|
|
3
|
+
* 用于存放分片上传的临时分片等内部数据。
|
|
4
|
+
*/
|
|
5
|
+
export declare const INTERNAL_DIR_NAMES: string[];
|
|
6
|
+
/** 判断目录名是否为内部目录 */
|
|
7
|
+
export declare function isInternalDirName(name: string): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* 规范化相对路径:去掉首尾空白与多余分隔符,拒绝 `..`、绝对路径与盘符。
|
|
10
|
+
* 返回以 `/` 分隔的相对路径,非法时返回空串。
|
|
11
|
+
*/
|
|
12
|
+
export declare function safeRelPath(input?: unknown): string;
|
|
13
|
+
/**
|
|
14
|
+
* 把相对路径解析为绝对路径,并确保结果仍在存储目录内。
|
|
15
|
+
* 非法时返回空串。
|
|
16
|
+
*/
|
|
17
|
+
export declare function resolveInDir(baseDir: string, relPath: string): string;
|
|
18
|
+
export interface FileEntry {
|
|
19
|
+
/** 相对路径,使用 `/` 分隔 */
|
|
20
|
+
name: string;
|
|
21
|
+
size: number;
|
|
22
|
+
mtime: number;
|
|
23
|
+
ext: string;
|
|
24
|
+
isDir: boolean;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* 递归扫描存储目录,返回所有文件的相对路径列表(不含目录)。
|
|
28
|
+
*/
|
|
29
|
+
export declare function listFiles(baseDir: string, filter?: (relPath: string) => boolean): FileEntry[];
|
|
30
|
+
/** 目录下的直接子目录(相对路径),不含内部目录 */
|
|
31
|
+
export declare function listDirs(baseDir: string): string[];
|
|
32
|
+
/** 判断路径是否位于指定目录内(用于二次校验) */
|
|
33
|
+
export declare function isInsideDir(baseDir: string, target: string): boolean;
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.INTERNAL_DIR_NAMES = void 0;
|
|
4
|
+
exports.isInternalDirName = isInternalDirName;
|
|
5
|
+
exports.safeRelPath = safeRelPath;
|
|
6
|
+
exports.resolveInDir = resolveInDir;
|
|
7
|
+
exports.listFiles = listFiles;
|
|
8
|
+
exports.listDirs = listDirs;
|
|
9
|
+
exports.isInsideDir = isInsideDir;
|
|
10
|
+
/*
|
|
11
|
+
* LocalSend 存储目录的路径处理
|
|
12
|
+
*
|
|
13
|
+
* 存储目录下可能存在子目录(上传文件夹时保留相对路径),
|
|
14
|
+
* 因此所有对外接口都用「相对路径」标识文件,如 `子目录/照片.jpg`。
|
|
15
|
+
* 这里统一做规范化与安全校验,杜绝目录穿越。
|
|
16
|
+
*/
|
|
17
|
+
const node_fs_1 = require("node:fs");
|
|
18
|
+
const node_path_1 = require("node:path");
|
|
19
|
+
/** 存储目录中允许出现的最大层级,避免异常深的目录 */
|
|
20
|
+
const MAX_DEPTH = 16;
|
|
21
|
+
/**
|
|
22
|
+
* 内部目录名(位于存储目录下,但不参与文件列表、WebDAV 目录浏览与自动清理)。
|
|
23
|
+
* 用于存放分片上传的临时分片等内部数据。
|
|
24
|
+
*/
|
|
25
|
+
exports.INTERNAL_DIR_NAMES = ['.chunks', '.trash'];
|
|
26
|
+
/** 判断目录名是否为内部目录 */
|
|
27
|
+
function isInternalDirName(name) {
|
|
28
|
+
return exports.INTERNAL_DIR_NAMES.includes(name);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* 规范化相对路径:去掉首尾空白与多余分隔符,拒绝 `..`、绝对路径与盘符。
|
|
32
|
+
* 返回以 `/` 分隔的相对路径,非法时返回空串。
|
|
33
|
+
*/
|
|
34
|
+
function safeRelPath(input) {
|
|
35
|
+
const raw = String(input || '').trim();
|
|
36
|
+
if (!raw)
|
|
37
|
+
return '';
|
|
38
|
+
// 统一分隔符,并去掉开头的斜杠(不允许绝对路径)
|
|
39
|
+
const parts = raw.split(/[\\/]+/).filter((p) => p && p !== '.');
|
|
40
|
+
if (!parts.length)
|
|
41
|
+
return '';
|
|
42
|
+
if (parts.length > MAX_DEPTH)
|
|
43
|
+
return '';
|
|
44
|
+
const safe = [];
|
|
45
|
+
for (const part of parts) {
|
|
46
|
+
// 拒绝上级目录穿越与盘符
|
|
47
|
+
if (part === '..' || part.includes(':'))
|
|
48
|
+
return '';
|
|
49
|
+
// 拒绝控制字符
|
|
50
|
+
// biome-ignore lint/suspicious/noControlCharactersInRegex: 需要过滤控制字符
|
|
51
|
+
if (/[\u0000-\u001f]/.test(part))
|
|
52
|
+
return '';
|
|
53
|
+
safe.push(part);
|
|
54
|
+
}
|
|
55
|
+
return safe.join('/');
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* 把相对路径解析为绝对路径,并确保结果仍在存储目录内。
|
|
59
|
+
* 非法时返回空串。
|
|
60
|
+
*/
|
|
61
|
+
function resolveInDir(baseDir, relPath) {
|
|
62
|
+
const rel = safeRelPath(relPath);
|
|
63
|
+
if (!rel)
|
|
64
|
+
return '';
|
|
65
|
+
// 内部目录(如分片暂存区)不对外暴露
|
|
66
|
+
if (rel.split('/').some((part) => isInternalDirName(part)))
|
|
67
|
+
return '';
|
|
68
|
+
const base = (0, node_path_1.resolve)(baseDir);
|
|
69
|
+
const full = (0, node_path_1.resolve)(base, ...rel.split('/'));
|
|
70
|
+
// 二次校验,防止符号链接或边界情况越界
|
|
71
|
+
const inside = (0, node_path_1.relative)(base, full);
|
|
72
|
+
if (!inside || inside.startsWith('..') || (0, node_path_1.resolve)(base, inside) !== full)
|
|
73
|
+
return '';
|
|
74
|
+
if (full === base)
|
|
75
|
+
return '';
|
|
76
|
+
return full;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* 递归扫描存储目录,返回所有文件的相对路径列表(不含目录)。
|
|
80
|
+
*/
|
|
81
|
+
function listFiles(baseDir, filter) {
|
|
82
|
+
const entries = [];
|
|
83
|
+
if (!(0, node_fs_1.existsSync)(baseDir))
|
|
84
|
+
return entries;
|
|
85
|
+
const walk = (dir, prefix, depth) => {
|
|
86
|
+
if (depth > MAX_DEPTH)
|
|
87
|
+
return;
|
|
88
|
+
let names;
|
|
89
|
+
try {
|
|
90
|
+
names = (0, node_fs_1.readdirSync)(dir);
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
for (const name of names) {
|
|
96
|
+
const full = (0, node_path_1.join)(dir, name);
|
|
97
|
+
const rel = prefix ? `${prefix}/${name}` : name;
|
|
98
|
+
let stat;
|
|
99
|
+
try {
|
|
100
|
+
stat = (0, node_fs_1.statSync)(full);
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (stat.isDirectory()) {
|
|
106
|
+
// 内部目录(如分片暂存区)不对外暴露
|
|
107
|
+
if (isInternalDirName(name))
|
|
108
|
+
continue;
|
|
109
|
+
walk(full, rel, depth + 1);
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
if (!stat.isFile())
|
|
113
|
+
continue;
|
|
114
|
+
if (filter && !filter(rel))
|
|
115
|
+
continue;
|
|
116
|
+
entries.push({
|
|
117
|
+
name: rel,
|
|
118
|
+
size: stat.size,
|
|
119
|
+
mtime: stat.mtimeMs,
|
|
120
|
+
ext: name.slice(name.lastIndexOf('.') + 1).toLowerCase(),
|
|
121
|
+
isDir: false,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
walk(baseDir, '', 0);
|
|
126
|
+
return entries;
|
|
127
|
+
}
|
|
128
|
+
/** 目录下的直接子目录(相对路径),不含内部目录 */
|
|
129
|
+
function listDirs(baseDir) {
|
|
130
|
+
if (!(0, node_fs_1.existsSync)(baseDir))
|
|
131
|
+
return [];
|
|
132
|
+
const dirs = [];
|
|
133
|
+
for (const name of (0, node_fs_1.readdirSync)(baseDir)) {
|
|
134
|
+
// 内部目录(如分片暂存区、回收站)不对外暴露
|
|
135
|
+
if (isInternalDirName(name))
|
|
136
|
+
continue;
|
|
137
|
+
try {
|
|
138
|
+
if ((0, node_fs_1.statSync)((0, node_path_1.join)(baseDir, name)).isDirectory())
|
|
139
|
+
dirs.push(name);
|
|
140
|
+
}
|
|
141
|
+
catch {
|
|
142
|
+
// 忽略无法读取的条目
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return dirs;
|
|
146
|
+
}
|
|
147
|
+
/** 判断路径是否位于指定目录内(用于二次校验) */
|
|
148
|
+
function isInsideDir(baseDir, target) {
|
|
149
|
+
const rel = (0, node_path_1.relative)((0, node_path_1.resolve)(baseDir), (0, node_path_1.resolve)(target));
|
|
150
|
+
return !!rel && !rel.startsWith('..') && !rel.startsWith(node_path_1.sep);
|
|
151
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { FileEntry } from './path';
|
|
2
|
+
/** 根目录筛选的占位值(真实目录名不会与它冲突) */
|
|
3
|
+
export declare const ROOT_DIR = "__root__";
|
|
4
|
+
/** 默认每页条数 */
|
|
5
|
+
export declare const DEFAULT_PAGE_SIZE = 50;
|
|
6
|
+
/** 允许的最大每页条数,避免一次返回过多数据拖垮浏览器 */
|
|
7
|
+
export declare const MAX_PAGE_SIZE = 500;
|
|
8
|
+
/** 排序字段:time(修改时间)、size(大小)、name(名称) */
|
|
9
|
+
export type SortField = 'time' | 'size' | 'name';
|
|
10
|
+
export interface ListQuery {
|
|
11
|
+
/** 排序字段:time(修改时间)、size(大小)或 name(名称) */
|
|
12
|
+
sort: SortField;
|
|
13
|
+
/** 是否升序 */
|
|
14
|
+
asc: boolean;
|
|
15
|
+
/** 页码,从 1 开始 */
|
|
16
|
+
page: number;
|
|
17
|
+
/** 每页条数 */
|
|
18
|
+
pageSize: number;
|
|
19
|
+
/** 搜索关键词(匹配相对路径,不区分大小写) */
|
|
20
|
+
q: string;
|
|
21
|
+
/** 目录筛选:空表示全部,ROOT_DIR 表示只看根目录 */
|
|
22
|
+
dir: string;
|
|
23
|
+
/** 扩展名筛选(不含点,小写);空表示全部 */
|
|
24
|
+
ext: string;
|
|
25
|
+
/** 只看该时间点之后修改过的文件(毫秒时间戳),0 表示不限 */
|
|
26
|
+
since: number;
|
|
27
|
+
}
|
|
28
|
+
export interface ListResult {
|
|
29
|
+
/** 当前页的文件 */
|
|
30
|
+
items: FileEntry[];
|
|
31
|
+
/** 符合筛选条件的文件总数 */
|
|
32
|
+
total: number;
|
|
33
|
+
/** 当前页码 */
|
|
34
|
+
page: number;
|
|
35
|
+
/** 每页条数 */
|
|
36
|
+
pageSize: number;
|
|
37
|
+
/** 总页数(至少为 1) */
|
|
38
|
+
totalPages: number;
|
|
39
|
+
}
|
|
40
|
+
/** 目录筛选项 */
|
|
41
|
+
export interface DirFacet {
|
|
42
|
+
/** 目录的相对路径 */
|
|
43
|
+
path: string;
|
|
44
|
+
/** 该目录下的文件数 */
|
|
45
|
+
count: number;
|
|
46
|
+
}
|
|
47
|
+
/** 扩展名筛选项 */
|
|
48
|
+
export interface ExtFacet {
|
|
49
|
+
/** 扩展名(不含点,小写) */
|
|
50
|
+
ext: string;
|
|
51
|
+
/** 该类型的文件数 */
|
|
52
|
+
count: number;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* 解析列表查询参数。
|
|
56
|
+
*
|
|
57
|
+
* 所有参数都做区间与类型收敛,非法输入一律回退到默认值,
|
|
58
|
+
* 避免一个越界的 page 或 pageSize 把服务端拖垮。
|
|
59
|
+
*/
|
|
60
|
+
export declare function parseListQuery(query: Record<string, unknown>): ListQuery;
|
|
61
|
+
/** 取文件所在目录(相对存储目录),根目录返回空串 */
|
|
62
|
+
export declare function dirOfName(name: string): string;
|
|
63
|
+
/** 按查询条件过滤文件 */
|
|
64
|
+
export declare function filterEntries(entries: FileEntry[], query: ListQuery): FileEntry[];
|
|
65
|
+
/**
|
|
66
|
+
* 排序:主字段相同时按名称兜底,
|
|
67
|
+
* 保证翻页过程中同一条记录不会在两页之间来回跳。
|
|
68
|
+
*/
|
|
69
|
+
export declare function sortEntries(entries: FileEntry[], sort: SortField, asc: boolean): FileEntry[];
|
|
70
|
+
/** 切片出当前页;页码越界时收敛到有效范围 */
|
|
71
|
+
export declare function paginate(items: FileEntry[], page: number, pageSize: number): ListResult;
|
|
72
|
+
/** 统计目录筛选项:每个目录下的文件数与根目录文件数 */
|
|
73
|
+
export declare function collectDirs(entries: FileEntry[]): {
|
|
74
|
+
dirs: DirFacet[];
|
|
75
|
+
rootCount: number;
|
|
76
|
+
};
|
|
77
|
+
/** 统计扩展名筛选项:按文件数从多到少排列 */
|
|
78
|
+
export declare function collectExts(entries: FileEntry[]): ExtFacet[];
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MAX_PAGE_SIZE = exports.DEFAULT_PAGE_SIZE = exports.ROOT_DIR = void 0;
|
|
4
|
+
exports.parseListQuery = parseListQuery;
|
|
5
|
+
exports.dirOfName = dirOfName;
|
|
6
|
+
exports.filterEntries = filterEntries;
|
|
7
|
+
exports.sortEntries = sortEntries;
|
|
8
|
+
exports.paginate = paginate;
|
|
9
|
+
exports.collectDirs = collectDirs;
|
|
10
|
+
exports.collectExts = collectExts;
|
|
11
|
+
/** 根目录筛选的占位值(真实目录名不会与它冲突) */
|
|
12
|
+
exports.ROOT_DIR = '__root__';
|
|
13
|
+
/** 默认每页条数 */
|
|
14
|
+
exports.DEFAULT_PAGE_SIZE = 50;
|
|
15
|
+
/** 允许的最大每页条数,避免一次返回过多数据拖垮浏览器 */
|
|
16
|
+
exports.MAX_PAGE_SIZE = 500;
|
|
17
|
+
/**
|
|
18
|
+
* 把任意输入规整成区间内的整数。
|
|
19
|
+
*
|
|
20
|
+
* 小于下限的值(负数、0 等)视为无效,回退到默认值;
|
|
21
|
+
* 超过上限的值收敛到上限,避免一次拉取过多数据。
|
|
22
|
+
*/
|
|
23
|
+
function intOr(value, fallback, min, max) {
|
|
24
|
+
const n = Number(value);
|
|
25
|
+
if (!Number.isFinite(n))
|
|
26
|
+
return fallback;
|
|
27
|
+
if (n < min)
|
|
28
|
+
return fallback;
|
|
29
|
+
return Math.min(max, Math.floor(n));
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* 解析列表查询参数。
|
|
33
|
+
*
|
|
34
|
+
* 所有参数都做区间与类型收敛,非法输入一律回退到默认值,
|
|
35
|
+
* 避免一个越界的 page 或 pageSize 把服务端拖垮。
|
|
36
|
+
*/
|
|
37
|
+
function parseListQuery(query) {
|
|
38
|
+
const sortRaw = String(query.sort ?? 'time').toLowerCase();
|
|
39
|
+
const orderRaw = String(query.order ?? 'desc').toLowerCase();
|
|
40
|
+
const sinceRaw = Number(query.since);
|
|
41
|
+
return {
|
|
42
|
+
sort: sortRaw === 'size' || sortRaw === 'name' ? sortRaw : 'time',
|
|
43
|
+
asc: orderRaw === 'asc',
|
|
44
|
+
page: intOr(query.page, 1, 1, 1_000_000),
|
|
45
|
+
pageSize: intOr(query.pageSize, exports.DEFAULT_PAGE_SIZE, 1, exports.MAX_PAGE_SIZE),
|
|
46
|
+
q: String(query.q ?? '')
|
|
47
|
+
.trim()
|
|
48
|
+
.toLowerCase(),
|
|
49
|
+
dir: String(query.dir ?? '').trim(),
|
|
50
|
+
ext: String(query.ext ?? '')
|
|
51
|
+
.trim()
|
|
52
|
+
.replace(/^\./, '')
|
|
53
|
+
.toLowerCase(),
|
|
54
|
+
since: Number.isFinite(sinceRaw) && sinceRaw > 0 ? sinceRaw : 0,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
/** 取文件所在目录(相对存储目录),根目录返回空串 */
|
|
58
|
+
function dirOfName(name) {
|
|
59
|
+
const i = name.lastIndexOf('/');
|
|
60
|
+
return i < 0 ? '' : name.slice(0, i);
|
|
61
|
+
}
|
|
62
|
+
/** 按查询条件过滤文件 */
|
|
63
|
+
function filterEntries(entries, query) {
|
|
64
|
+
return entries.filter((e) => {
|
|
65
|
+
if (query.q && !e.name.toLowerCase().includes(query.q))
|
|
66
|
+
return false;
|
|
67
|
+
if (query.ext && e.ext !== query.ext)
|
|
68
|
+
return false;
|
|
69
|
+
if (query.since && e.mtime < query.since)
|
|
70
|
+
return false;
|
|
71
|
+
if (query.dir) {
|
|
72
|
+
const dir = dirOfName(e.name);
|
|
73
|
+
// ROOT_DIR 只看根目录文件,其他取值要求文件的父目录完全匹配
|
|
74
|
+
if (query.dir === exports.ROOT_DIR ? dir !== '' : dir !== query.dir)
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
return true;
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* 名称比较器:中文按拼音、数字按大小(file2 排在 file10 前)。
|
|
82
|
+
* 数值等价的名称(file1 / file01)比较结果为 0,此时再按原始字符串兜底,
|
|
83
|
+
* 保证排序是全序——出现并列会让翻页顺序抖动。
|
|
84
|
+
*/
|
|
85
|
+
const nameCollator = new Intl.Collator('zh-Hans-CN', { numeric: true });
|
|
86
|
+
function compareName(a, b) {
|
|
87
|
+
const diff = nameCollator.compare(a, b);
|
|
88
|
+
if (diff !== 0)
|
|
89
|
+
return diff;
|
|
90
|
+
if (a === b)
|
|
91
|
+
return 0;
|
|
92
|
+
return a < b ? -1 : 1;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* 排序:主字段相同时按名称兜底,
|
|
96
|
+
* 保证翻页过程中同一条记录不会在两页之间来回跳。
|
|
97
|
+
*/
|
|
98
|
+
function sortEntries(entries, sort, asc) {
|
|
99
|
+
const sign = asc ? 1 : -1;
|
|
100
|
+
return entries.slice().sort((a, b) => {
|
|
101
|
+
if (sort === 'name')
|
|
102
|
+
return compareName(a.name, b.name) * sign;
|
|
103
|
+
const diff = sort === 'size' ? a.size - b.size : a.mtime - b.mtime;
|
|
104
|
+
if (diff !== 0)
|
|
105
|
+
return diff * sign;
|
|
106
|
+
return compareName(a.name, b.name) * sign;
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
/** 切片出当前页;页码越界时收敛到有效范围 */
|
|
110
|
+
function paginate(items, page, pageSize) {
|
|
111
|
+
const total = items.length;
|
|
112
|
+
const totalPages = Math.max(1, Math.ceil(total / pageSize));
|
|
113
|
+
const current = Math.min(Math.max(page, 1), totalPages);
|
|
114
|
+
const start = (current - 1) * pageSize;
|
|
115
|
+
return {
|
|
116
|
+
items: items.slice(start, start + pageSize),
|
|
117
|
+
total,
|
|
118
|
+
page: current,
|
|
119
|
+
pageSize,
|
|
120
|
+
totalPages,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
/** 统计目录筛选项:每个目录下的文件数与根目录文件数 */
|
|
124
|
+
function collectDirs(entries) {
|
|
125
|
+
const counts = new Map();
|
|
126
|
+
let rootCount = 0;
|
|
127
|
+
for (const e of entries) {
|
|
128
|
+
const dir = dirOfName(e.name);
|
|
129
|
+
if (!dir) {
|
|
130
|
+
rootCount += 1;
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
counts.set(dir, (counts.get(dir) ?? 0) + 1);
|
|
134
|
+
}
|
|
135
|
+
const dirs = [...counts.entries()].map(([path, count]) => ({ path, count })).sort((a, b) => a.path.localeCompare(b.path));
|
|
136
|
+
return { dirs, rootCount };
|
|
137
|
+
}
|
|
138
|
+
/** 统计扩展名筛选项:按文件数从多到少排列 */
|
|
139
|
+
function collectExts(entries) {
|
|
140
|
+
const counts = new Map();
|
|
141
|
+
for (const e of entries) {
|
|
142
|
+
if (!e.ext)
|
|
143
|
+
continue;
|
|
144
|
+
counts.set(e.ext, (counts.get(e.ext) ?? 0) + 1);
|
|
145
|
+
}
|
|
146
|
+
return [...counts.entries()].map(([ext, count]) => ({ ext, count })).sort((a, b) => b.count - a.count || a.ext.localeCompare(b.ext));
|
|
147
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** 密钥种类 */
|
|
2
|
+
export type SecretKind = 'token' | 'password' | 'visitorPassword';
|
|
3
|
+
/** PBKDF2 迭代次数:口令越弱,破解成本越重要 */
|
|
4
|
+
export declare const SECRET_ITERATIONS = 10000;
|
|
5
|
+
/** 某个密钥的公开信息 */
|
|
6
|
+
export interface SecretInfo {
|
|
7
|
+
/** 密钥种类 */
|
|
8
|
+
kind: SecretKind;
|
|
9
|
+
/** 公开盐值(hex) */
|
|
10
|
+
salt: string;
|
|
11
|
+
/** 迭代次数 */
|
|
12
|
+
iterations: number;
|
|
13
|
+
}
|
|
14
|
+
/** 密钥摘要库 */
|
|
15
|
+
export interface SecretStore {
|
|
16
|
+
/** 用明文刷新某个密钥:重新生成盐值并计算摘要 */
|
|
17
|
+
refresh: (kind: SecretKind, plaintext: string) => void;
|
|
18
|
+
/** 某个密钥的公开信息;未配置返回 null */
|
|
19
|
+
info: (kind: SecretKind) => SecretInfo | null;
|
|
20
|
+
/** 已配置密钥的公开信息列表 */
|
|
21
|
+
list: () => SecretInfo[];
|
|
22
|
+
/** 是否配置了某个密钥 */
|
|
23
|
+
has: (kind: SecretKind) => boolean;
|
|
24
|
+
/** 明文的摘要(用于只能传明文的场景,如 WebDAV Basic Auth) */
|
|
25
|
+
digestOf: (kind: SecretKind, plaintext: string) => string;
|
|
26
|
+
/** 校验客户端提交的摘要 */
|
|
27
|
+
verify: (kind: SecretKind, presented: string) => boolean;
|
|
28
|
+
/** 在所有已配置的密钥里找匹配的种类;没有匹配返回 null */
|
|
29
|
+
match: (presented: string) => SecretKind | null;
|
|
30
|
+
}
|
|
31
|
+
export declare function createSecretStore(): SecretStore;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SECRET_ITERATIONS = void 0;
|
|
4
|
+
exports.createSecretStore = createSecretStore;
|
|
5
|
+
/*
|
|
6
|
+
* LocalSend 密钥摘要
|
|
7
|
+
*
|
|
8
|
+
* 口令与访问令牌不再以明文形式在网络上流转,也不再出现在 URL 里:
|
|
9
|
+
*
|
|
10
|
+
* 1. 服务端为每个密钥生成一枚公开的随机盐值,并公布出去
|
|
11
|
+
* 2. 客户端用盐值把口令/令牌派生成摘要(PBKDF2-HMAC-SHA256)
|
|
12
|
+
* 3. 客户端把摘要放在请求体或请求头里提交,明文始终留在用户手里
|
|
13
|
+
* 4. 服务端只保存摘要,收到摘要后比对即可
|
|
14
|
+
*
|
|
15
|
+
* 为什么用 PBKDF2 而不是直接 SHA-256:
|
|
16
|
+
* - 盐值随机且公开给客户端,但让摘要无法被彩虹表直接命中
|
|
17
|
+
* - 迭代次数把穷举弱口令的成本抬高若干个数量级
|
|
18
|
+
* - 摘要与明文一一对应,客户端算一次、服务端存一份,双方都不再处理明文
|
|
19
|
+
*
|
|
20
|
+
* 只有 WebDAV 是个例外:资源管理器等客户端只能用 Basic Auth 传明文,
|
|
21
|
+
* 这类请求由服务端用同一套盐值现场算摘要后再比对(见 webdav.ts)。
|
|
22
|
+
*/
|
|
23
|
+
const node_crypto_1 = require("node:crypto");
|
|
24
|
+
/** PBKDF2 迭代次数:口令越弱,破解成本越重要 */
|
|
25
|
+
exports.SECRET_ITERATIONS = 10000;
|
|
26
|
+
/** 盐值字节数 */
|
|
27
|
+
const SALT_BYTES = 16;
|
|
28
|
+
/** 派生密钥字节数 */
|
|
29
|
+
const KEY_BYTES = 32;
|
|
30
|
+
/** 计算 PBKDF2 摘要(hex) */
|
|
31
|
+
function derive(plaintext, saltHex, iterations) {
|
|
32
|
+
return (0, node_crypto_1.pbkdf2Sync)(String(plaintext ?? ''), Buffer.from(saltHex, 'hex'), iterations, KEY_BYTES, 'sha256').toString('hex');
|
|
33
|
+
}
|
|
34
|
+
/** 十六进制字符串转字节 */
|
|
35
|
+
function fromHex(hex) {
|
|
36
|
+
return Buffer.from(String(hex || ''), 'hex');
|
|
37
|
+
}
|
|
38
|
+
/** 恒定时间比较两个 hex 摘要 */
|
|
39
|
+
function safeEqualHex(a, b) {
|
|
40
|
+
const bufA = fromHex(a);
|
|
41
|
+
const bufB = fromHex(b);
|
|
42
|
+
if (!bufA.length || bufA.length !== bufB.length)
|
|
43
|
+
return false;
|
|
44
|
+
return (0, node_crypto_1.timingSafeEqual)(bufA, bufB);
|
|
45
|
+
}
|
|
46
|
+
function createSecretStore() {
|
|
47
|
+
/** kind -> { salt, digest } */
|
|
48
|
+
const secrets = new Map();
|
|
49
|
+
const refresh = (kind, plaintext) => {
|
|
50
|
+
const value = String(plaintext ?? '');
|
|
51
|
+
if (!value) {
|
|
52
|
+
// 未配置:清掉旧的盐值与摘要
|
|
53
|
+
secrets.delete(kind);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const salt = (0, node_crypto_1.randomBytes)(SALT_BYTES).toString('hex');
|
|
57
|
+
secrets.set(kind, { salt, digest: derive(value, salt, exports.SECRET_ITERATIONS) });
|
|
58
|
+
};
|
|
59
|
+
const info = (kind) => {
|
|
60
|
+
const item = secrets.get(kind);
|
|
61
|
+
if (!item)
|
|
62
|
+
return null;
|
|
63
|
+
return { kind, salt: item.salt, iterations: exports.SECRET_ITERATIONS };
|
|
64
|
+
};
|
|
65
|
+
const digestOf = (kind, plaintext) => {
|
|
66
|
+
const item = secrets.get(kind);
|
|
67
|
+
if (!item)
|
|
68
|
+
return '';
|
|
69
|
+
return derive(plaintext, item.salt, exports.SECRET_ITERATIONS);
|
|
70
|
+
};
|
|
71
|
+
const verify = (kind, presented) => {
|
|
72
|
+
const item = secrets.get(kind);
|
|
73
|
+
if (!item)
|
|
74
|
+
return false;
|
|
75
|
+
return safeEqualHex(item.digest, String(presented || ''));
|
|
76
|
+
};
|
|
77
|
+
const match = (presented) => {
|
|
78
|
+
const value = String(presented || '');
|
|
79
|
+
if (!value)
|
|
80
|
+
return null;
|
|
81
|
+
for (const [kind, item] of secrets) {
|
|
82
|
+
if (safeEqualHex(item.digest, value))
|
|
83
|
+
return kind;
|
|
84
|
+
}
|
|
85
|
+
return null;
|
|
86
|
+
};
|
|
87
|
+
return {
|
|
88
|
+
refresh,
|
|
89
|
+
info,
|
|
90
|
+
list: () => [...secrets.keys()].map((kind) => info(kind)),
|
|
91
|
+
has: (kind) => secrets.has(kind),
|
|
92
|
+
digestOf,
|
|
93
|
+
verify,
|
|
94
|
+
match,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/** 允许运行时修改的配置项 */
|
|
2
|
+
export type SettingKey = 'token' | 'password' | 'visitorPassword' | 'sessionTtl' | 'maxSize' | 'maxTotalSize' | 'maxConcurrent' | 'rateLimit' | 'blockExtensions' | 'trashDays' | 'autoCleanDays' | 'autoCleanMaxSize';
|
|
3
|
+
export interface SettingDef {
|
|
4
|
+
/** 中文名称 */
|
|
5
|
+
label: string;
|
|
6
|
+
/** 取值形态 */
|
|
7
|
+
kind: 'secret' | 'text' | 'number' | 'numberList' | 'rate';
|
|
8
|
+
/** 单位说明,用于界面提示 */
|
|
9
|
+
unit?: string;
|
|
10
|
+
/** 数值下界 */
|
|
11
|
+
min?: number;
|
|
12
|
+
/** 数值上界 */
|
|
13
|
+
max?: number;
|
|
14
|
+
/** 说明 */
|
|
15
|
+
desc: string;
|
|
16
|
+
/** 是否为敏感值(接口返回时脱敏) */
|
|
17
|
+
secret?: boolean;
|
|
18
|
+
}
|
|
19
|
+
/** 各配置项的元信息,网页端按这份定义渲染表单 */
|
|
20
|
+
export declare const SETTING_DEFS: Record<SettingKey, SettingDef>;
|
|
21
|
+
/** 需要重启服务才能生效的配置项(仅作展示,不可修改) */
|
|
22
|
+
export declare const RESTART_REQUIRED_KEYS: readonly ['dir', 'webdav'];
|
|
23
|
+
export interface SettingsStore {
|
|
24
|
+
/** 设置文件路径 */
|
|
25
|
+
readonly file: string;
|
|
26
|
+
/** 读取运行时设置(损坏时忽略) */
|
|
27
|
+
load: () => void;
|
|
28
|
+
/** 把运行时设置合并到配置文件的值上,返回最终生效的配置 */
|
|
29
|
+
apply: <T extends Record<string, unknown>>(base: T) => T;
|
|
30
|
+
/** 写一项覆盖;value 为 undefined 表示清除覆盖,回到配置文件的值 */
|
|
31
|
+
set: (key: SettingKey, value: unknown, baseValue: unknown) => void;
|
|
32
|
+
/** 清除一项覆盖 */
|
|
33
|
+
clear: (key: SettingKey) => void;
|
|
34
|
+
/** 某一项当前的生效值 */
|
|
35
|
+
effective: <T extends Record<string, unknown>>(base: T, key: SettingKey) => unknown;
|
|
36
|
+
/** 某一项当前来自运行时覆盖还是配置文件 */
|
|
37
|
+
source: (key: SettingKey) => 'runtime' | 'config';
|
|
38
|
+
/** 列出全部被覆盖的配置项 */
|
|
39
|
+
overridden: () => SettingKey[];
|
|
40
|
+
/** 把内存中的状态落盘 */
|
|
41
|
+
flush: () => void;
|
|
42
|
+
}
|
|
43
|
+
/** 运行时设置文件的默认位置:用户级配置目录下 */
|
|
44
|
+
export declare function resolveSettingsFile(): string;
|
|
45
|
+
/**
|
|
46
|
+
* 校验并规范化一个配置项的取值。
|
|
47
|
+
* 返回 error 表示取值不合法;返回 value 表示规范化后的值。
|
|
48
|
+
*/
|
|
49
|
+
export declare function normalizeSetting(key: SettingKey, raw: unknown): {
|
|
50
|
+
value?: unknown;
|
|
51
|
+
error?: string;
|
|
52
|
+
};
|
|
53
|
+
export declare function createSettingsStore(file?: string): SettingsStore;
|