diff --git a/demo/simple/.gitignore b/demo/simple/.gitignore new file mode 100644 index 0000000..7d41001 --- /dev/null +++ b/demo/simple/.gitignore @@ -0,0 +1 @@ +pem \ No newline at end of file diff --git a/demo/simple/src/cert/index.ts b/demo/simple/src/cert/index.ts new file mode 100644 index 0000000..b28f878 --- /dev/null +++ b/demo/simple/src/cert/index.ts @@ -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, + ), +); diff --git a/src/sign.ts b/src/sign.ts index d07ed24..bace63e 100644 --- a/src/sign.ts +++ b/src/sign.ts @@ -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, + ), + ); 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, ],