@lzwme/sserver 0.0.5 → 0.0.6
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/bin/sserver.js +0 -0
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +61 -54
- package/dist/config.d.ts +47 -27
- package/dist/config.js +61 -49
- package/dist/get-logger.d.ts +2 -2
- package/dist/get-logger.js +9 -9
- package/dist/index.d.ts +1 -1
- package/dist/index.js +5 -5
- package/dist/lib/get-cert.d.ts +6 -6
- package/dist/lib/get-cert.js +79 -79
- package/dist/lib/mkcert.d.ts +26 -26
- package/dist/lib/mkcert.js +86 -86
- package/dist/proxy/express-proxy.d.ts +2 -2
- package/dist/proxy/express-proxy.js +24 -24
- package/dist/proxy/php-proxy.d.ts +3 -0
- package/dist/proxy/php-proxy.js +188 -0
- package/dist/proxy/socket-proxy.d.ts +7 -7
- package/dist/proxy/socket-proxy.js +30 -30
- package/dist/static-server.d.ts +3 -3
- package/dist/static-server.js +66 -50
- package/package.json +9 -9
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2018 任侠
|
|
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
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 任侠
|
|
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
|
+
SOFTWARE.
|
package/bin/sserver.js
CHANGED
|
File without changes
|
package/dist/cli.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export {};
|
|
1
|
+
export {};
|
package/dist/cli.js
CHANGED
|
@@ -1,54 +1,61 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const pkg = require('../package.json');
|
|
11
|
-
commander_1.program
|
|
12
|
-
.aliases(['ss'])
|
|
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)}]`)
|
|
15
|
-
// .option('-c, --config-path [filepath]', `配置文件 ${color.yellow(config.configPath)} 的路径`)
|
|
16
|
-
.option('-d, --root-dir [dirpath]', '静态服务器的根目录路径')
|
|
17
|
-
.option('-p, --port [port]', '端口号')
|
|
18
|
-
.option('--host <hostname>', '域名。默认为 `localhost`')
|
|
19
|
-
.option('-H, --https', '启用 https 模式')
|
|
20
|
-
.option('--cert <filepath>', 'https 模式使用的 ssl cert。默认根据 host 随机生成')
|
|
21
|
-
.option('--key <filepath>', 'https 模式使用的 ssl key。默认根据 host 随机生成')
|
|
22
|
-
.option('--ssl-cache <dir>', '随机生成 cert 证书缓存的路径。未指定则不缓存')
|
|
23
|
-
.option('-o, --open', '启动后是否打开静态服务器首页')
|
|
24
|
-
.option('--
|
|
25
|
-
.option('--
|
|
26
|
-
.
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
get_logger_1.logger.debug
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const node_fs_1 = require("node:fs");
|
|
4
|
+
const node_path_1 = require("node:path");
|
|
5
|
+
const commander_1 = require("commander");
|
|
6
|
+
const console_log_colors_1 = require("console-log-colors");
|
|
7
|
+
const config_1 = require("./config");
|
|
8
|
+
const get_logger_1 = require("./get-logger");
|
|
9
|
+
const static_server_1 = require("./static-server");
|
|
10
|
+
const pkg = require('../package.json');
|
|
11
|
+
commander_1.program
|
|
12
|
+
.aliases(['ss'])
|
|
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)}]`)
|
|
15
|
+
// .option('-c, --config-path [filepath]', `配置文件 ${color.yellow(config.configPath)} 的路径`)
|
|
16
|
+
.option('-d, --root-dir [dirpath]', '静态服务器的根目录路径')
|
|
17
|
+
.option('-p, --port [port]', '端口号')
|
|
18
|
+
.option('--host <hostname>', '域名。默认为 `localhost`')
|
|
19
|
+
.option('-H, --https', '启用 https 模式')
|
|
20
|
+
.option('--cert <filepath>', 'https 模式使用的 ssl cert。默认根据 host 随机生成')
|
|
21
|
+
.option('--key <filepath>', 'https 模式使用的 ssl key。默认根据 host 随机生成')
|
|
22
|
+
.option('--ssl-cache <dir>', '随机生成 cert 证书缓存的路径。未指定则不缓存')
|
|
23
|
+
.option('-o, --open', '启动后是否打开静态服务器首页')
|
|
24
|
+
.option('--coop-coep', '启用跨域隔离', false)
|
|
25
|
+
.option('--log [dirpath]', `指定日志路径`)
|
|
26
|
+
.option('--debug', `开启调试模式。`, false)
|
|
27
|
+
.action(async (opts) => {
|
|
28
|
+
if (opts.cert)
|
|
29
|
+
opts.ssl.cert = opts.cert;
|
|
30
|
+
if (opts.key)
|
|
31
|
+
opts.ssl.key = opts.key;
|
|
32
|
+
if (opts.debug) {
|
|
33
|
+
get_logger_1.logger.updateOptions({ levelType: 'debug' });
|
|
34
|
+
get_logger_1.logger.debug(opts);
|
|
35
|
+
}
|
|
36
|
+
if (opts.coopCoep) {
|
|
37
|
+
opts.headers = {
|
|
38
|
+
'Cross-Origin-Opener-Policy': 'same-origin',
|
|
39
|
+
'Cross-Origin-Embedder-Policy': 'require-corp',
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
(0, config_1.getConfig)(false, opts).then(() => (0, static_server_1.initServer)());
|
|
43
|
+
});
|
|
44
|
+
commander_1.program
|
|
45
|
+
.command('init [destination]')
|
|
46
|
+
.description('初始化一个 ss.config.js 文件至指定的目录。默认为当前目录')
|
|
47
|
+
.action((destination = process.cwd()) => {
|
|
48
|
+
const confFile = (0, node_path_1.resolve)(destination, 'ss.config.js');
|
|
49
|
+
if ((0, node_fs_1.existsSync)(confFile)) {
|
|
50
|
+
get_logger_1.logger.warn('配置文件已存在,请确认:', console_log_colors_1.color.cyanBright(confFile));
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const tplFile = (0, node_path_1.resolve)(__dirname, '../ss.config.sample.js');
|
|
54
|
+
if (!(0, node_fs_1.existsSync)(tplFile)) {
|
|
55
|
+
get_logger_1.logger.error('未找到配置文件模板:', console_log_colors_1.color.red(tplFile));
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
(0, node_fs_1.writeFileSync)(confFile, (0, node_fs_1.readFileSync)(tplFile));
|
|
59
|
+
get_logger_1.logger.info('已生成配置文件:', console_log_colors_1.color.greenBright(confFile));
|
|
60
|
+
});
|
|
61
|
+
commander_1.program.parse(process.argv);
|
package/dist/config.d.ts
CHANGED
|
@@ -1,27 +1,47 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
|
|
11
|
-
/** host 地址。默认为 127.0.0.1 */
|
|
12
|
-
host?: string;
|
|
13
|
-
/**
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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';
|
|
8
|
+
export interface SSConfig {
|
|
9
|
+
/** 自定义 app 应用逻辑 */
|
|
10
|
+
custom?(app: Express, config: SSConfig, logger: NLogger, c: typeof color): void;
|
|
11
|
+
/** host 地址。默认为 127.0.0.1 */
|
|
12
|
+
host?: string;
|
|
13
|
+
/** 是否开启日志记录。默认为 false。若设置为 true 则默认为 ~/.sserver/log.log;若为字符串,则设置为日志路径 */
|
|
14
|
+
log?: boolean | string;
|
|
15
|
+
/** 启动后是否打开首页 */
|
|
16
|
+
open?: boolean;
|
|
17
|
+
/** 监听端口号。默认 80,开启 https 则为 443 */
|
|
18
|
+
port?: number;
|
|
19
|
+
/** 代理转发配置 */
|
|
20
|
+
proxyConfig?: {
|
|
21
|
+
api: string;
|
|
22
|
+
config: Options;
|
|
23
|
+
}[];
|
|
24
|
+
phpConfig?: {
|
|
25
|
+
/** 执行 php-cgi 的路径。默认系统可执行路径获取 */
|
|
26
|
+
cgiPath?: string;
|
|
27
|
+
/** php-cgi port。默认为 9010 */
|
|
28
|
+
port?: number | string;
|
|
29
|
+
/** php-cgi 命令行参数 */
|
|
30
|
+
args?: Record<string, string>;
|
|
31
|
+
/** 是否仅处理 php 文件的请求。否则会处理所有静态文件的请求。默认 true */
|
|
32
|
+
onlyPhpFile?: boolean;
|
|
33
|
+
};
|
|
34
|
+
/** 根目录 */
|
|
35
|
+
rootDir?: string;
|
|
36
|
+
/** 是否为 https 模式 */
|
|
37
|
+
https?: boolean;
|
|
38
|
+
/** 指定 https 模式相关的 ssl 配置 */
|
|
39
|
+
ssl?: ServerOptions;
|
|
40
|
+
/** 自动生成 ssl 的缓存目录 */
|
|
41
|
+
sslCache?: string;
|
|
42
|
+
/** 设置公共自定义 headers */
|
|
43
|
+
headers?: Record<string, string | string[] | undefined>;
|
|
44
|
+
/** 按请求自定义 headers。返回结果与 headers 内容合并 */
|
|
45
|
+
setHeaders?: (res: Response<any, Record<string, any>>, path: string, stat: Stats) => Record<string, string | string[] | undefined>;
|
|
46
|
+
}
|
|
47
|
+
export declare function getConfig(useCache?: boolean, cfg?: SSConfig): Promise<SSConfig>;
|
package/dist/config.js
CHANGED
|
@@ -1,49 +1,61 @@
|
|
|
1
|
-
"use strict";
|
|
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");
|
|
6
|
-
const node_fs_1 = require("node:fs");
|
|
7
|
-
const fe_utils_1 = require("@lzwme/fe-utils");
|
|
8
|
-
const get_logger_1 = require("./get-logger");
|
|
9
|
-
const get_cert_1 = require("./lib/get-cert");
|
|
10
|
-
const ssConfig = {
|
|
11
|
-
|
|
12
|
-
open: false,
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
1
|
+
"use strict";
|
|
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");
|
|
6
|
+
const node_fs_1 = require("node:fs");
|
|
7
|
+
const fe_utils_1 = require("@lzwme/fe-utils");
|
|
8
|
+
const get_logger_1 = require("./get-logger");
|
|
9
|
+
const get_cert_1 = require("./lib/get-cert");
|
|
10
|
+
const ssConfig = {
|
|
11
|
+
log: false,
|
|
12
|
+
open: false,
|
|
13
|
+
phpConfig: {
|
|
14
|
+
onlyPhpFile: true,
|
|
15
|
+
args: {},
|
|
16
|
+
},
|
|
17
|
+
proxyConfig: [],
|
|
18
|
+
rootDir: '.',
|
|
19
|
+
ssl: {
|
|
20
|
+
rejectUnauthorized: false,
|
|
21
|
+
requestCert: true,
|
|
22
|
+
},
|
|
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
|
+
},
|
|
30
|
+
};
|
|
31
|
+
async function getConfig(useCache = true, cfg) {
|
|
32
|
+
if (ssConfig.port && useCache)
|
|
33
|
+
return ssConfig;
|
|
34
|
+
const cfgFile = (0, node_path_1.resolve)(process.cwd(), './ss.config.js');
|
|
35
|
+
if ((0, node_fs_1.existsSync)(cfgFile))
|
|
36
|
+
(0, fe_utils_1.assign)(ssConfig, require(cfgFile));
|
|
37
|
+
if (cfg)
|
|
38
|
+
(0, fe_utils_1.assign)(ssConfig, cfg);
|
|
39
|
+
ssConfig.rootDir = (0, node_path_1.resolve)(process.cwd(), typeof ssConfig.rootDir === 'string' ? ssConfig.rootDir : '.');
|
|
40
|
+
if (ssConfig.log) {
|
|
41
|
+
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
|
+
}
|
|
43
|
+
if (ssConfig.https && ssConfig.ssl) {
|
|
44
|
+
const { ssl } = ssConfig;
|
|
45
|
+
if (ssConfig.https && !ssl.cert) {
|
|
46
|
+
const info = await (0, get_cert_1.getCert)(ssConfig.host, ssConfig.sslCache || '');
|
|
47
|
+
ssl.key = Buffer.from(info.certKey);
|
|
48
|
+
ssl.cert = Buffer.from(info.certCrt);
|
|
49
|
+
ssl.ca = Buffer.from(info.caKey);
|
|
50
|
+
}
|
|
51
|
+
for (const key of ['key', 'cert', 'ca']) {
|
|
52
|
+
const val = ssl[key];
|
|
53
|
+
if (typeof val === 'string' && (0, node_fs_1.existsSync)(val))
|
|
54
|
+
ssl[key] = (0, node_fs_1.readFileSync)(val);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (!ssConfig.port)
|
|
58
|
+
ssConfig.port = ssConfig.https ? 443 : 80;
|
|
59
|
+
return ssConfig;
|
|
60
|
+
}
|
|
61
|
+
exports.getConfig = getConfig;
|
package/dist/get-logger.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { NLogger } from '@lzwme/fe-utils';
|
|
2
|
-
export declare const logger: NLogger;
|
|
1
|
+
import { NLogger } from '@lzwme/fe-utils';
|
|
2
|
+
export declare const logger: NLogger;
|
package/dist/get-logger.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.logger = void 0;
|
|
4
|
-
const console_log_colors_1 = require("console-log-colors");
|
|
5
|
-
const fe_utils_1 = require("@lzwme/fe-utils");
|
|
6
|
-
function getLogger(levelType, logDir) {
|
|
7
|
-
return fe_utils_1.NLogger.getLogger('[sserver]', { levelType, color: console_log_colors_1.color, logDir });
|
|
8
|
-
}
|
|
9
|
-
exports.logger = getLogger();
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.logger = void 0;
|
|
4
|
+
const console_log_colors_1 = require("console-log-colors");
|
|
5
|
+
const fe_utils_1 = require("@lzwme/fe-utils");
|
|
6
|
+
function getLogger(levelType, logDir) {
|
|
7
|
+
return fe_utils_1.NLogger.getLogger('[sserver]', { levelType, color: console_log_colors_1.color, logDir });
|
|
8
|
+
}
|
|
9
|
+
exports.logger = getLogger();
|
package/dist/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { initServer } from './static-server';
|
|
1
|
+
export { initServer } from './static-server';
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.initServer = void 0;
|
|
4
|
-
var static_server_1 = require("./static-server");
|
|
5
|
-
Object.defineProperty(exports, "initServer", { enumerable: true, get: function () { return static_server_1.initServer; } });
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.initServer = void 0;
|
|
4
|
+
var static_server_1 = require("./static-server");
|
|
5
|
+
Object.defineProperty(exports, "initServer", { enumerable: true, get: function () { return static_server_1.initServer; } });
|
package/dist/lib/get-cert.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export declare function getCert(host?: string, cacheDir?: string): Promise<{
|
|
2
|
-
caKey: string;
|
|
3
|
-
caCrt: string;
|
|
4
|
-
certKey: string;
|
|
5
|
-
certCrt: string;
|
|
6
|
-
}>;
|
|
1
|
+
export declare function getCert(host?: string, cacheDir?: string): Promise<{
|
|
2
|
+
caKey: string;
|
|
3
|
+
caCrt: string;
|
|
4
|
+
certKey: string;
|
|
5
|
+
certCrt: string;
|
|
6
|
+
}>;
|
package/dist/lib/get-cert.js
CHANGED
|
@@ -1,79 +1,79 @@
|
|
|
1
|
-
"use strict";
|
|
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");
|
|
9
|
-
const console_log_colors_1 = require("console-log-colors");
|
|
10
|
-
async function getCert(host = 'localhost', cacheDir = '') {
|
|
11
|
-
const result = {
|
|
12
|
-
caKey: '',
|
|
13
|
-
caCrt: '',
|
|
14
|
-
certKey: '',
|
|
15
|
-
certCrt: '',
|
|
16
|
-
};
|
|
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`),
|
|
22
|
-
};
|
|
23
|
-
if (cacheDir) {
|
|
24
|
-
if (!(0, fs_1.existsSync)(cacheDir))
|
|
25
|
-
(0, promises_1.mkdir)(cacheDir, { recursive: true });
|
|
26
|
-
for (const [key, filepath] of Object.entries(dirs)) {
|
|
27
|
-
if ((0, fs_1.existsSync)(filepath))
|
|
28
|
-
result[key] = await (0, promises_1.readFile)(filepath, 'utf8');
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
if (!result.caKey) {
|
|
32
|
-
const attributes = [
|
|
33
|
-
{ name: 'commonName', value: 'sserver' },
|
|
34
|
-
{ name: 'countryName', value: 'CN' },
|
|
35
|
-
{ name: 'stateOrProvinceName', value: 'gd' },
|
|
36
|
-
{ name: 'localityName', value: 'gz' },
|
|
37
|
-
{ name: 'organizationName', value: 'lzw.me' },
|
|
38
|
-
{ name: 'organizationalUnitName', value: 'lzw.me' },
|
|
39
|
-
// { name: 'emailAddress', value: `webmaster@${host}` },
|
|
40
|
-
];
|
|
41
|
-
// create a certificate authority
|
|
42
|
-
const ca = await (0, mkcert_1.createCA)({
|
|
43
|
-
subject: attributes,
|
|
44
|
-
issuer: attributes,
|
|
45
|
-
});
|
|
46
|
-
result.caKey = ca.key;
|
|
47
|
-
result.caCrt = ca.cert;
|
|
48
|
-
if (cacheDir) {
|
|
49
|
-
await (0, promises_1.writeFile)(dirs.caKey, result.caKey, 'utf8');
|
|
50
|
-
await (0, promises_1.writeFile)(dirs.caCrt, result.caCrt, 'utf8');
|
|
51
|
-
get_logger_1.logger.info('save SSL CA certificate privateKey:', (0, console_log_colors_1.greenBright)(dirs.caKey));
|
|
52
|
-
get_logger_1.logger.info('save SSL CA certificate publicKey:', (0, console_log_colors_1.greenBright)(dirs.caCrt));
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
if (!result.certKey) {
|
|
56
|
-
const cert = await (0, mkcert_1.createCert)({
|
|
57
|
-
domains: [host, '127.0.0.1'],
|
|
58
|
-
caKey: result.caKey,
|
|
59
|
-
caCert: result.caCrt,
|
|
60
|
-
subject: [
|
|
61
|
-
{ name: 'countryName', value: 'CN' },
|
|
62
|
-
{ name: 'stateOrProvinceName', value: 'gd' },
|
|
63
|
-
{ name: 'localityName', value: 'gz' },
|
|
64
|
-
{ name: 'organizationName', value: 'sserver' },
|
|
65
|
-
{ name: 'organizationalUnitName', value: 'sserver' },
|
|
66
|
-
],
|
|
67
|
-
});
|
|
68
|
-
result.certKey = cert.key;
|
|
69
|
-
result.certCrt = cert.cert;
|
|
70
|
-
if (cacheDir) {
|
|
71
|
-
await (0, promises_1.writeFile)(dirs.certKey, result.certKey, 'utf8');
|
|
72
|
-
await (0, promises_1.writeFile)(dirs.certCrt, result.certCrt, 'utf8');
|
|
73
|
-
get_logger_1.logger.info('save SSL server certificate privateKey:', (0, console_log_colors_1.greenBright)(dirs.certKey));
|
|
74
|
-
get_logger_1.logger.info('save SSL server certificate publicKey:', (0, console_log_colors_1.greenBright)(dirs.certCrt));
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
return result;
|
|
78
|
-
}
|
|
79
|
-
exports.getCert = getCert;
|
|
1
|
+
"use strict";
|
|
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");
|
|
9
|
+
const console_log_colors_1 = require("console-log-colors");
|
|
10
|
+
async function getCert(host = 'localhost', cacheDir = '') {
|
|
11
|
+
const result = {
|
|
12
|
+
caKey: '',
|
|
13
|
+
caCrt: '',
|
|
14
|
+
certKey: '',
|
|
15
|
+
certCrt: '',
|
|
16
|
+
};
|
|
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`),
|
|
22
|
+
};
|
|
23
|
+
if (cacheDir) {
|
|
24
|
+
if (!(0, fs_1.existsSync)(cacheDir))
|
|
25
|
+
(0, promises_1.mkdir)(cacheDir, { recursive: true });
|
|
26
|
+
for (const [key, filepath] of Object.entries(dirs)) {
|
|
27
|
+
if ((0, fs_1.existsSync)(filepath))
|
|
28
|
+
result[key] = await (0, promises_1.readFile)(filepath, 'utf8');
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
if (!result.caKey) {
|
|
32
|
+
const attributes = [
|
|
33
|
+
{ name: 'commonName', value: 'sserver' },
|
|
34
|
+
{ name: 'countryName', value: 'CN' },
|
|
35
|
+
{ name: 'stateOrProvinceName', value: 'gd' },
|
|
36
|
+
{ name: 'localityName', value: 'gz' },
|
|
37
|
+
{ name: 'organizationName', value: 'lzw.me' },
|
|
38
|
+
{ name: 'organizationalUnitName', value: 'lzw.me' },
|
|
39
|
+
// { name: 'emailAddress', value: `webmaster@${host}` },
|
|
40
|
+
];
|
|
41
|
+
// create a certificate authority
|
|
42
|
+
const ca = await (0, mkcert_1.createCA)({
|
|
43
|
+
subject: attributes,
|
|
44
|
+
issuer: attributes,
|
|
45
|
+
});
|
|
46
|
+
result.caKey = ca.key;
|
|
47
|
+
result.caCrt = ca.cert;
|
|
48
|
+
if (cacheDir) {
|
|
49
|
+
await (0, promises_1.writeFile)(dirs.caKey, result.caKey, 'utf8');
|
|
50
|
+
await (0, promises_1.writeFile)(dirs.caCrt, result.caCrt, 'utf8');
|
|
51
|
+
get_logger_1.logger.info('save SSL CA certificate privateKey:', (0, console_log_colors_1.greenBright)(dirs.caKey));
|
|
52
|
+
get_logger_1.logger.info('save SSL CA certificate publicKey:', (0, console_log_colors_1.greenBright)(dirs.caCrt));
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (!result.certKey) {
|
|
56
|
+
const cert = await (0, mkcert_1.createCert)({
|
|
57
|
+
domains: [host, '127.0.0.1'],
|
|
58
|
+
caKey: result.caKey,
|
|
59
|
+
caCert: result.caCrt,
|
|
60
|
+
subject: [
|
|
61
|
+
{ name: 'countryName', value: 'CN' },
|
|
62
|
+
{ name: 'stateOrProvinceName', value: 'gd' },
|
|
63
|
+
{ name: 'localityName', value: 'gz' },
|
|
64
|
+
{ name: 'organizationName', value: 'sserver' },
|
|
65
|
+
{ name: 'organizationalUnitName', value: 'sserver' },
|
|
66
|
+
],
|
|
67
|
+
});
|
|
68
|
+
result.certKey = cert.key;
|
|
69
|
+
result.certCrt = cert.cert;
|
|
70
|
+
if (cacheDir) {
|
|
71
|
+
await (0, promises_1.writeFile)(dirs.certKey, result.certKey, 'utf8');
|
|
72
|
+
await (0, promises_1.writeFile)(dirs.certCrt, result.certCrt, 'utf8');
|
|
73
|
+
get_logger_1.logger.info('save SSL server certificate privateKey:', (0, console_log_colors_1.greenBright)(dirs.certKey));
|
|
74
|
+
get_logger_1.logger.info('save SSL server certificate publicKey:', (0, console_log_colors_1.greenBright)(dirs.certCrt));
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return result;
|
|
78
|
+
}
|
|
79
|
+
exports.getCert = getCert;
|
package/dist/lib/mkcert.d.ts
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
import { pki } from 'node-forge';
|
|
2
|
-
interface GenCertOptions {
|
|
3
|
-
subject: pki.CertificateField[];
|
|
4
|
-
issuer: pki.CertificateField[];
|
|
5
|
-
extensions?: any[];
|
|
6
|
-
validityDays?: number;
|
|
7
|
-
signWith?: string;
|
|
8
|
-
}
|
|
9
|
-
export declare function generateCert(options: GenCertOptions): Promise<{
|
|
10
|
-
key: string;
|
|
11
|
-
cert: string;
|
|
12
|
-
}>;
|
|
13
|
-
export declare function createCA(options: GenCertOptions): Promise<{
|
|
14
|
-
key: string;
|
|
15
|
-
cert: string;
|
|
16
|
-
}>;
|
|
17
|
-
interface CreateCert extends Omit<Partial<GenCertOptions>, 'issuer'> {
|
|
18
|
-
domains: string[];
|
|
19
|
-
caKey: string;
|
|
20
|
-
caCert: string;
|
|
21
|
-
}
|
|
22
|
-
export declare function createCert(options: CreateCert): Promise<{
|
|
23
|
-
key: string;
|
|
24
|
-
cert: string;
|
|
25
|
-
}>;
|
|
26
|
-
export {};
|
|
1
|
+
import { pki } from 'node-forge';
|
|
2
|
+
interface GenCertOptions {
|
|
3
|
+
subject: pki.CertificateField[];
|
|
4
|
+
issuer: pki.CertificateField[];
|
|
5
|
+
extensions?: any[];
|
|
6
|
+
validityDays?: number;
|
|
7
|
+
signWith?: string;
|
|
8
|
+
}
|
|
9
|
+
export declare function generateCert(options: GenCertOptions): Promise<{
|
|
10
|
+
key: string;
|
|
11
|
+
cert: string;
|
|
12
|
+
}>;
|
|
13
|
+
export declare function createCA(options: GenCertOptions): Promise<{
|
|
14
|
+
key: string;
|
|
15
|
+
cert: string;
|
|
16
|
+
}>;
|
|
17
|
+
interface CreateCert extends Omit<Partial<GenCertOptions>, 'issuer'> {
|
|
18
|
+
domains: string[];
|
|
19
|
+
caKey: string;
|
|
20
|
+
caCert: string;
|
|
21
|
+
}
|
|
22
|
+
export declare function createCert(options: CreateCert): Promise<{
|
|
23
|
+
key: string;
|
|
24
|
+
cert: string;
|
|
25
|
+
}>;
|
|
26
|
+
export {};
|