@lzwme/sserver 0.0.8 → 0.0.10

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 CHANGED
@@ -47,6 +47,9 @@ ss -H --host local.lzw.me
47
47
 
48
48
  # 启用 https,并保存默认生成的 ssl 证书
49
49
  ss -H --ssl-cache ./cache
50
+
51
+ # 开启 localsend 局域网文件传输功能
52
+ ss --localsend
50
53
  ```
51
54
 
52
55
  ### 配置代理转发
@@ -86,6 +89,34 @@ module.exports = {
86
89
  }
87
90
  ```
88
91
 
92
+ ### LocalSend 局域网文件传输
93
+
94
+ 开启后可通过 `http://<本机IP>:<port>/localsend` 访问,实现同网段设备间的文件快速传输(支持多文件上传/下载、实时进度、二维码扫码访问、扫码直连下载、文件预览与排序等):
95
+
96
+ ```bash
97
+ # 命令行方式开启
98
+ ss --localsend
99
+
100
+ # 指定文件存储目录(也可用环境变量 SS_LOCALSEND_DIR 指定)
101
+ ss --localsend --localsend-dir /tmp/localsend
102
+ ```
103
+
104
+ 配置文件方式开启:
105
+
106
+ ```js
107
+ // ss.config.js
108
+ module.exports = {
109
+ port: 8888,
110
+ localsend: {
111
+ enabled: true,
112
+ // 文件存储目录。默认 ~/.static-server/localsend,可用环境变量 SS_LOCALSEND_DIR 覆盖
113
+ dir: '/tmp/localsend',
114
+ // 单个文件上传大小限制(字节),默认 1GB
115
+ maxSize: 1024 * 1024 * 1024,
116
+ },
117
+ };
118
+ ```
119
+
89
120
  ## 开发
90
121
 
91
122
  本地二次开发:
package/dist/cli.js CHANGED
@@ -11,7 +11,7 @@ const pkg = require('../package.json');
11
11
  commander_1.program
12
12
  .aliases(['ss'])
13
13
  .version(pkg.version, '-v, --version')
14
- .description(console_log_colors_1.color.yellow(pkg.description) + ` [version@${console_log_colors_1.color.cyanBright(pkg.version)}]`)
14
+ .description(`${console_log_colors_1.color.yellow(pkg.description)} [version@${console_log_colors_1.color.cyanBright(pkg.version)}]`)
15
15
  // .option('-c, --config-path [filepath]', `配置文件 ${color.yellow(config.configPath)} 的路径`)
16
16
  .option('-d, --root-dir [dirpath]', '静态服务器的根目录路径')
17
17
  .option('-a, --autoindex', '是否开启目录列表浏览')
@@ -23,7 +23,11 @@ commander_1.program
23
23
  .option('--ssl-cache <dir>', '随机生成 cert 证书缓存的路径。未指定则不缓存')
24
24
  .option('-o, --open', '启动后是否打开静态服务器首页')
25
25
  .option('--coop-coep', '启用跨域隔离', false)
26
+ .option('--cors', '是否启用 cors。会全局设置 cors 的 header')
27
+ .option('--no-cors', '是否禁用 cors')
26
28
  .option('--log [dirpath]', `指定日志路径`)
29
+ .option('--localsend', '开启 localsend 局域网文件传输功能')
30
+ .option('--localsend-dir <dir>', 'localsend 文件存储目录。默认 ~/.static-server/localsend,可用环境变量 SS_LOCALSEND_DIR 覆盖')
27
31
  .option('--debug', `开启调试模式。`, false)
28
32
  .action(async (opts) => {
29
33
  if (opts.cert)
@@ -40,6 +44,9 @@ commander_1.program
40
44
  'Cross-Origin-Embedder-Policy': 'require-corp',
41
45
  };
42
46
  }
47
+ if (opts.localsend) {
48
+ opts.localsend = { enabled: true, dir: opts.localsendDir || undefined };
49
+ }
43
50
  (0, config_1.getConfig)(false, opts).then(() => (0, static_server_1.initServer)());
44
51
  });
45
52
  commander_1.program
package/dist/config.d.ts CHANGED
@@ -1,19 +1,27 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
- import { Stats } from 'node:fs';
4
- import { type ServerOptions } from 'node:https';
5
- import { type Options } from 'http-proxy-middleware';
6
- import { type Response, type Express } from 'express';
7
- import { type NLogger, type color } from '@lzwme/fe-utils';
1
+ import { type Stats } from 'node:fs';
2
+ import type { ServerOptions } from 'node:https';
3
+ import { type color, type NLogger } from '@lzwme/fe-utils';
4
+ import type { Express, Response } from 'express';
5
+ import type { Options } from 'http-proxy-middleware';
6
+ export interface LocalSendConfig {
7
+ /** 是否开启 localsend 局域网文件传输功能 */
8
+ enabled?: boolean;
9
+ /** 文件存储目录。默认 ~/.static-server/localsend,可通过环境变量 SS_LOCALSEND_DIR 指定 */
10
+ dir?: string;
11
+ /** 单个文件上传大小限制(字节)。默认 1GB */
12
+ maxSize?: number;
13
+ }
8
14
  export interface SSConfig {
9
15
  /** 是否开启目录列表 */
10
16
  autoindex?: boolean;
11
17
  /** 自定义 app 应用逻辑 */
12
- custom?(app: Express, config: SSConfig, logger: NLogger, c: typeof color): void;
18
+ custom?(app: Express, config: SSConfig, logger: NLogger, c: Partial<typeof color>): void;
13
19
  /** host 地址。默认为 127.0.0.1 */
14
20
  host?: string;
15
21
  /** 是否开启日志记录。默认为 false。若设置为 true 则默认为 ~/.sserver/log.log;若为字符串,则设置为日志路径 */
16
22
  log?: boolean | string;
23
+ /** localsend 局域网文件传输功能。可设置为 true 开启,或传入配置对象 */
24
+ localsend?: LocalSendConfig | boolean;
17
25
  /** 启动后是否打开首页 */
18
26
  open?: boolean;
19
27
  /** 监听端口号。默认 80,开启 https 则为 443 */
@@ -41,6 +49,8 @@ export interface SSConfig {
41
49
  ssl?: ServerOptions;
42
50
  /** 自动生成 ssl 的缓存目录 */
43
51
  sslCache?: string;
52
+ /** 是否默认开启 cors 设置 */
53
+ cors?: boolean;
44
54
  /** 设置公共自定义 headers */
45
55
  headers?: Record<string, string | string[] | undefined>;
46
56
  /** 按请求自定义 headers。返回结果与 headers 内容合并 */
package/dist/config.js CHANGED
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getConfig = void 0;
4
- const node_path_1 = require("node:path");
5
- const node_os_1 = require("node:os");
3
+ exports.getConfig = getConfig;
6
4
  const node_fs_1 = require("node:fs");
5
+ const node_os_1 = require("node:os");
6
+ const node_path_1 = require("node:path");
7
7
  const fe_utils_1 = require("@lzwme/fe-utils");
8
8
  const get_logger_1 = require("./get-logger");
9
9
  const get_cert_1 = require("./lib/get-cert");
@@ -16,17 +16,12 @@ const ssConfig = {
16
16
  },
17
17
  proxyConfig: [],
18
18
  rootDir: '.',
19
+ cors: true,
19
20
  ssl: {
20
21
  rejectUnauthorized: false,
21
22
  requestCert: true,
22
23
  },
23
- headers: {
24
- 'Access-Control-Allow-Credentials': 'true',
25
- 'Access-Control-Allow-Headers': '*',
26
- 'Access-Control-Allow-Methods': 'GET,POST,OPTIONS',
27
- 'Access-Control-Allow-Origin': '*',
28
- 'Cross-Origin-Opener-Policy': 'cross-origin',
29
- },
24
+ headers: {},
30
25
  };
31
26
  async function getConfig(useCache = true, cfg) {
32
27
  if (ssConfig.port && useCache)
@@ -37,9 +32,26 @@ async function getConfig(useCache = true, cfg) {
37
32
  if (cfg)
38
33
  (0, fe_utils_1.assign)(ssConfig, cfg);
39
34
  ssConfig.rootDir = (0, node_path_1.resolve)(process.cwd(), typeof ssConfig.rootDir === 'string' ? ssConfig.rootDir : '.');
35
+ // localsend 配置归一化:支持 true / 配置对象,支持环境变量 SS_LOCALSEND_DIR 指定存储目录
36
+ if (ssConfig.localsend) {
37
+ const localsend = typeof ssConfig.localsend === 'boolean' ? { enabled: ssConfig.localsend } : { ...ssConfig.localsend };
38
+ localsend.enabled = typeof ssConfig.localsend === 'boolean' ? ssConfig.localsend : localsend.enabled !== false;
39
+ localsend.dir = process.env.SS_LOCALSEND_DIR || localsend.dir || (0, node_path_1.resolve)((0, node_os_1.homedir)(), '.static-server/localsend');
40
+ localsend.maxSize = localsend.maxSize || 1024 * 1024 * 1024;
41
+ ssConfig.localsend = localsend;
42
+ }
40
43
  if (ssConfig.log) {
41
44
  get_logger_1.logger.setLogDir(typeof ssConfig.log === 'boolean' ? (0, node_path_1.resolve)((0, node_os_1.homedir)(), '.sserver/log.log') : ssConfig.log);
42
45
  }
46
+ if (ssConfig.cors) {
47
+ Object.assign(ssConfig.headers, {
48
+ 'Access-Control-Allow-Credentials': 'true',
49
+ 'Access-Control-Allow-Headers': '*',
50
+ 'Access-Control-Allow-Methods': 'GET,POST,OPTIONS',
51
+ 'Access-Control-Allow-Origin': '*',
52
+ 'Cross-Origin-Opener-Policy': 'cross-origin',
53
+ });
54
+ }
43
55
  if (ssConfig.https && ssConfig.ssl) {
44
56
  const { ssl } = ssConfig;
45
57
  if (ssConfig.https && !ssl.cert) {
@@ -58,4 +70,3 @@ async function getConfig(useCache = true, cfg) {
58
70
  ssConfig.port = ssConfig.https ? 443 : 80;
59
71
  return ssConfig;
60
72
  }
61
- exports.getConfig = getConfig;
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.logger = void 0;
4
- const console_log_colors_1 = require("console-log-colors");
5
4
  const fe_utils_1 = require("@lzwme/fe-utils");
5
+ const console_log_colors_1 = require("console-log-colors");
6
6
  function getLogger(levelType, logDir) {
7
7
  return fe_utils_1.NLogger.getLogger('[sserver]', { levelType, color: console_log_colors_1.color, logDir });
8
8
  }
package/dist/index.d.ts CHANGED
@@ -1 +1,2 @@
1
+ export type { SSConfig } from './config';
1
2
  export { initServer } from './static-server';
@@ -1,12 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getCert = void 0;
4
- const fs_1 = require("fs");
5
- const promises_1 = require("fs/promises");
6
- const mkcert_1 = require("./mkcert");
7
- const path_1 = require("path");
8
- const get_logger_1 = require("../get-logger");
3
+ exports.getCert = getCert;
4
+ const node_fs_1 = require("node:fs");
5
+ const promises_1 = require("node:fs/promises");
6
+ const node_path_1 = require("node:path");
9
7
  const console_log_colors_1 = require("console-log-colors");
8
+ const get_logger_1 = require("../get-logger");
9
+ const mkcert_1 = require("./mkcert");
10
10
  async function getCert(host = 'localhost', cacheDir = '') {
11
11
  const result = {
12
12
  caKey: '',
@@ -15,16 +15,16 @@ async function getCert(host = 'localhost', cacheDir = '') {
15
15
  certCrt: '',
16
16
  };
17
17
  const dirs = {
18
- caKey: (0, path_1.resolve)(cacheDir, 'ca.key'),
19
- caCrt: (0, path_1.resolve)(cacheDir, 'ca.crt'),
20
- certKey: (0, path_1.resolve)(cacheDir, `${host}.key`),
21
- certCrt: (0, path_1.resolve)(cacheDir, `${host}.crt`),
18
+ caKey: (0, node_path_1.resolve)(cacheDir, 'ca.key'),
19
+ caCrt: (0, node_path_1.resolve)(cacheDir, 'ca.crt'),
20
+ certKey: (0, node_path_1.resolve)(cacheDir, `${host}.key`),
21
+ certCrt: (0, node_path_1.resolve)(cacheDir, `${host}.crt`),
22
22
  };
23
23
  if (cacheDir) {
24
- if (!(0, fs_1.existsSync)(cacheDir))
24
+ if (!(0, node_fs_1.existsSync)(cacheDir))
25
25
  (0, promises_1.mkdir)(cacheDir, { recursive: true });
26
26
  for (const [key, filepath] of Object.entries(dirs)) {
27
- if ((0, fs_1.existsSync)(filepath))
27
+ if ((0, node_fs_1.existsSync)(filepath))
28
28
  result[key] = await (0, promises_1.readFile)(filepath, 'utf8');
29
29
  }
30
30
  }
@@ -76,4 +76,3 @@ async function getCert(host = 'localhost', cacheDir = '') {
76
76
  }
77
77
  return result;
78
78
  }
79
- exports.getCert = getCert;
@@ -1,12 +1,14 @@
1
1
  "use strict";
2
2
  // @see https://github.com/Subash/mkcert
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.createCert = exports.createCA = exports.generateCert = void 0;
4
+ exports.generateCert = generateCert;
5
+ exports.createCA = createCA;
6
+ exports.createCert = createCert;
7
+ const node_util_1 = require("node:util");
5
8
  const node_forge_1 = require("node-forge");
6
- const util_1 = require("util");
7
- const generateKeyPair = (0, util_1.promisify)(node_forge_1.pki.rsa.generateKeyPair.bind(node_forge_1.pki.rsa));
9
+ const generateKeyPair = (0, node_util_1.promisify)(node_forge_1.pki.rsa.generateKeyPair.bind(node_forge_1.pki.rsa));
8
10
  async function generateCert(options) {
9
- let { subject, issuer, extensions = [], validityDays = 36500, signWith } = options;
11
+ const { subject, issuer, extensions = [], validityDays = 36500, signWith } = options;
10
12
  // create serial from and integer between 50000 and 99999
11
13
  const serial = Math.floor(Math.random() * 95000 + 50000).toString();
12
14
  const keyPair = await generateKeyPair({ bits: 2048, workers: 4 });
@@ -28,7 +30,6 @@ async function generateCert(options) {
28
30
  cert: node_forge_1.pki.certificateToPem(cert),
29
31
  };
30
32
  }
31
- exports.generateCert = generateCert;
32
33
  function createCA(options) {
33
34
  if (!options.extensions)
34
35
  options.extensions = [];
@@ -40,12 +41,11 @@ function createCA(options) {
40
41
  ];
41
42
  return generateCert(options);
42
43
  }
43
- exports.createCA = createCA;
44
44
  function createCert(options) {
45
45
  // certificate Attributes: https://git.io/fptna
46
46
  if (!options.subject)
47
47
  options.subject = [];
48
- if (!options.subject.find(d => d.name === 'commonName')) {
48
+ if (!options.subject.find((d) => d.name === 'commonName')) {
49
49
  // use the first address as common name
50
50
  options.subject.push({ name: 'commonName', value: options.domains[0] });
51
51
  }
@@ -56,7 +56,7 @@ function createCert(options) {
56
56
  { name: 'extKeyUsage', serverAuth: true, clientAuth: true },
57
57
  {
58
58
  name: 'subjectAltName',
59
- altNames: options.domains.map(domain => {
59
+ altNames: options.domains.map((domain) => {
60
60
  const types = { domain: 2, ip: 7 }; // available Types: https://git.io/fptng
61
61
  const isIp = isIP(domain);
62
62
  if (isIp)
@@ -75,7 +75,6 @@ function createCert(options) {
75
75
  signWith: options.caKey,
76
76
  });
77
77
  }
78
- exports.createCert = createCert;
79
78
  function isIP(ip) {
80
79
  if (!ip)
81
80
  return false;
package/dist/lib/utils.js CHANGED
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getIp = void 0;
4
- const os_1 = require("os");
3
+ exports.getIp = getIp;
4
+ const node_os_1 = require("node:os");
5
5
  function getIp() {
6
- const nif = (0, os_1.networkInterfaces)();
6
+ const nif = (0, node_os_1.networkInterfaces)();
7
7
  let list = [];
8
8
  for (const key of Object.keys(nif)) {
9
9
  for (const item of nif[key]) {
@@ -11,7 +11,7 @@ function getIp() {
11
11
  list.push(item);
12
12
  }
13
13
  }
14
- list = list.sort((a, b) => {
14
+ list = list.sort((_a, b) => {
15
15
  if (b.address.startsWith('172.'))
16
16
  return -1;
17
17
  return 0;
@@ -19,5 +19,4 @@ function getIp() {
19
19
  // console.log(list);
20
20
  return list[0]?.address;
21
21
  }
22
- exports.getIp = getIp;
23
22
  // console.log(getIp());