fix: cert fix
This commit is contained in:
parent
adaf954ae7
commit
51305b71c3
1
demo/simple/.gitignore
vendored
Normal file
1
demo/simple/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
pem
|
19
demo/simple/src/cert/index.ts
Normal file
19
demo/simple/src/cert/index.ts
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import { createCert } from '@kevisual/router/src/sign.ts';
|
||||||
|
import fs from 'node:fs';
|
||||||
|
|
||||||
|
const cert = createCert();
|
||||||
|
|
||||||
|
fs.writeFileSync('pem/https-private-key.pem', cert.key);
|
||||||
|
fs.writeFileSync('pem/https-cert.pem', cert.cert);
|
||||||
|
fs.writeFileSync(
|
||||||
|
'pem/https-config.json',
|
||||||
|
JSON.stringify(
|
||||||
|
{
|
||||||
|
createTime: new Date().getTime(),
|
||||||
|
expireDate: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000).getTime(),
|
||||||
|
expireTime: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000).toISOString(),
|
||||||
|
},
|
||||||
|
null,
|
||||||
|
2,
|
||||||
|
),
|
||||||
|
);
|
31
src/sign.ts
31
src/sign.ts
@ -1,28 +1,33 @@
|
|||||||
import { generate } from 'selfsigned';
|
import { generate } from 'selfsigned';
|
||||||
|
|
||||||
type Attributes = {
|
export type Attributes = {
|
||||||
name: string;
|
name: string;
|
||||||
value: string;
|
value: string;
|
||||||
};
|
};
|
||||||
type AltNames = {
|
export type AltNames = {
|
||||||
type: number;
|
type: number;
|
||||||
value?: string;
|
value?: string;
|
||||||
ip?: string;
|
ip?: string;
|
||||||
};
|
};
|
||||||
export const createCert = (attrs: Attributes[] = [], altNames: AltNames[] = []) => {
|
export const createCert = (attrs: Attributes[] = [], altNames: AltNames[] = []) => {
|
||||||
let attributes = [
|
let attributes = [
|
||||||
{ name: 'commonName', value: '*' }, // 通配符域名
|
|
||||||
{ name: 'countryName', value: 'CN' }, // 国家代码
|
{ name: 'countryName', value: 'CN' }, // 国家代码
|
||||||
{ name: 'stateOrProvinceName', value: 'ZheJiang' }, // 州名
|
{ name: 'stateOrProvinceName', value: 'ZheJiang' }, // 州名
|
||||||
{ name: 'localityName', value: 'Hangzhou' }, // 城市名
|
{ name: 'localityName', value: 'HangZhou' }, // 城市名
|
||||||
{ name: 'organizationName', value: 'Envision' }, // 组织名
|
{ name: 'organizationName', value: 'Kevisual' }, // 组织名
|
||||||
{ name: 'organizationalUnitName', value: 'IT' }, // 组织单位
|
{ name: 'organizationalUnitName', value: 'ev' }, // 组织单位
|
||||||
...attrs,
|
...attrs,
|
||||||
];
|
];
|
||||||
// attribute 根据name去重复, 后面的覆盖前面的
|
// attribute 根据name去重复, 后面的覆盖前面的
|
||||||
attributes = attributes.filter((item, index, self) => {
|
attributes = Object.values(
|
||||||
return self.findIndex((t) => t.name === item.name) === index;
|
attributes.reduce(
|
||||||
});
|
(acc, attr) => ({
|
||||||
|
...acc,
|
||||||
|
[attr.name]: attr,
|
||||||
|
}),
|
||||||
|
{} as Record<string, Attributes>,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
const options = {
|
const options = {
|
||||||
days: 365, // 证书有效期(天)
|
days: 365, // 证书有效期(天)
|
||||||
@ -32,6 +37,14 @@ export const createCert = (attrs: Attributes[] = [], altNames: AltNames[] = [])
|
|||||||
altNames: [
|
altNames: [
|
||||||
{ type: 2, value: '*' }, // DNS 名称
|
{ type: 2, value: '*' }, // DNS 名称
|
||||||
{ type: 2, value: 'localhost' }, // DNS
|
{ type: 2, value: 'localhost' }, // DNS
|
||||||
|
{
|
||||||
|
type: 2,
|
||||||
|
value: '[::1]',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 7,
|
||||||
|
ip: 'fe80::1',
|
||||||
|
},
|
||||||
{ type: 7, ip: '127.0.0.1' }, // IP 地址
|
{ type: 7, ip: '127.0.0.1' }, // IP 地址
|
||||||
...altNames,
|
...altNames,
|
||||||
],
|
],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user