fix: cert fix

This commit is contained in:
熊潇 2025-06-05 19:24:14 +08:00
parent adaf954ae7
commit 51305b71c3
3 changed files with 42 additions and 9 deletions

1
demo/simple/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
pem

View 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,
),
);

View File

@ -1,28 +1,33 @@
import { generate } from 'selfsigned';
type Attributes = {
export type Attributes = {
name: string;
value: string;
};
type AltNames = {
export type AltNames = {
type: number;
value?: string;
ip?: string;
};
export const createCert = (attrs: Attributes[] = [], altNames: AltNames[] = []) => {
let attributes = [
{ name: 'commonName', value: '*' }, // 通配符域名
{ name: 'countryName', value: 'CN' }, // 国家代码
{ name: 'stateOrProvinceName', value: 'ZheJiang' }, // 州名
{ name: 'localityName', value: 'Hangzhou' }, // 城市名
{ name: 'organizationName', value: 'Envision' }, // 组织名
{ name: 'organizationalUnitName', value: 'IT' }, // 组织单位
{ name: 'localityName', value: 'HangZhou' }, // 城市名
{ name: 'organizationName', value: 'Kevisual' }, // 组织名
{ name: 'organizationalUnitName', value: 'ev' }, // 组织单位
...attrs,
];
// attribute 根据name去重复, 后面的覆盖前面的
attributes = attributes.filter((item, index, self) => {
return self.findIndex((t) => t.name === item.name) === index;
});
attributes = Object.values(
attributes.reduce(
(acc, attr) => ({
...acc,
[attr.name]: attr,
}),
{} as Record<string, Attributes>,
),
);
const options = {
days: 365, // 证书有效期(天)
@ -32,6 +37,14 @@ export const createCert = (attrs: Attributes[] = [], altNames: AltNames[] = [])
altNames: [
{ type: 2, value: '*' }, // DNS 名称
{ type: 2, value: 'localhost' }, // DNS
{
type: 2,
value: '[::1]',
},
{
type: 7,
ip: 'fe80::1',
},
{ type: 7, ip: '127.0.0.1' }, // IP 地址
...altNames,
],