UNPKG

trusted-cert

Version:

A simple tool to generate self signed x509 certificate

8 lines (7 loc) 27.7 kB
/*! * trusted-cert.js v1.1.4 * (c) 2020-2021 sprying * (c) 2022 zhyupe * Released under the MIT License. */ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("util"),e=require("fs-extra"),r=require("node-forge"),n=require("path"),i=require("debug"),o=require("child_process"),a=require("os");function s(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var c=s(n),u=s(i),l=s(a);function f(t,e,r,n,i,o,a){try{var s=t[o](a),c=s.value}catch(t){return void r(t)}s.done?e(c):Promise.resolve(c).then(n,i)}function p(t){return function(){var e=this,r=arguments;return new Promise((function(n,i){var o=t.apply(e,r);function a(t){f(o,n,i,a,s,"next",t)}function s(t){f(o,n,i,a,s,"throw",t)}a(void 0)}))}}function h(t){var e={exports:{}};return t(e,e.exports),e.exports}var d=h((function(t){function e(r){return t.exports=e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},t.exports.__esModule=!0,t.exports.default=t.exports,e(r)}t.exports=e,t.exports.__esModule=!0,t.exports.default=t.exports})),y=h((function(t){var e=d.default;function r(){t.exports=r=function(){return n},t.exports.__esModule=!0,t.exports.default=t.exports;var n={},i=Object.prototype,o=i.hasOwnProperty,a=Object.defineProperty||function(t,e,r){t[e]=r.value},s="function"==typeof Symbol?Symbol:{},c=s.iterator||"@@iterator",u=s.asyncIterator||"@@asyncIterator",l=s.toStringTag||"@@toStringTag";function f(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{f({},"")}catch(t){f=function(t,e,r){return t[e]=r}}function p(t,e,r,n){var i=e&&e.prototype instanceof y?e:y,o=Object.create(i.prototype),s=new E(n||[]);return a(o,"_invoke",{value:A(t,r,s)}),o}function h(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}n.wrap=p;var d={};function y(){}function v(){}function m(){}var g={};f(g,c,(function(){return this}));var w=Object.getPrototypeOf,_=w&&w(w($([])));_&&_!==i&&o.call(_,c)&&(g=_);var b=m.prototype=y.prototype=Object.create(g);function x(t){["next","throw","return"].forEach((function(e){f(t,e,(function(t){return this._invoke(e,t)}))}))}function k(t,r){function n(i,a,s,c){var u=h(t[i],t,a);if("throw"!==u.type){var l=u.arg,f=l.value;return f&&"object"==e(f)&&o.call(f,"__await")?r.resolve(f.__await).then((function(t){n("next",t,s,c)}),(function(t){n("throw",t,s,c)})):r.resolve(f).then((function(t){l.value=t,s(l)}),(function(t){return n("throw",t,s,c)}))}c(u.arg)}var i;a(this,"_invoke",{value:function(t,e){function o(){return new r((function(r,i){n(t,e,r,i)}))}return i=i?i.then(o,o):o()}})}function A(t,e,r){var n="suspendedStart";return function(i,o){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===i)throw o;return N()}for(r.method=i,r.arg=o;;){var a=r.delegate;if(a){var s=O(a,r);if(s){if(s===d)continue;return s}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var c=h(t,e,r);if("normal"===c.type){if(n=r.done?"completed":"suspendedYield",c.arg===d)continue;return{value:c.arg,done:r.done}}"throw"===c.type&&(n="completed",r.method="throw",r.arg=c.arg)}}}function O(t,e){var r=t.iterator[e.method];if(void 0===r){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,O(t,e),"throw"===e.method))return d;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return d}var n=h(r,t.iterator,e.arg);if("throw"===n.type)return e.method="throw",e.arg=n.arg,e.delegate=null,d;var i=n.arg;return i?i.done?(e[t.resultName]=i.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,d):i:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,d)}function P(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function j(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function E(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(P,this),this.reset(!0)}function $(t){if(t){var e=t[c];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var r=-1,n=function e(){for(;++r<t.length;)if(o.call(t,r))return e.value=t[r],e.done=!1,e;return e.value=void 0,e.done=!0,e};return n.next=n}}return{next:N}}function N(){return{value:void 0,done:!0}}return v.prototype=m,a(b,"constructor",{value:m,configurable:!0}),a(m,"constructor",{value:v,configurable:!0}),v.displayName=f(m,l,"GeneratorFunction"),n.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===v||"GeneratorFunction"===(e.displayName||e.name))},n.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,m):(t.__proto__=m,f(t,l,"GeneratorFunction")),t.prototype=Object.create(b),t},n.awrap=function(t){return{__await:t}},x(k.prototype),f(k.prototype,u,(function(){return this})),n.AsyncIterator=k,n.async=function(t,e,r,i,o){void 0===o&&(o=Promise);var a=new k(p(t,e,r,i),o);return n.isGeneratorFunction(e)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},x(b),f(b,l,"Generator"),f(b,c,(function(){return this})),f(b,"toString",(function(){return"[object Generator]"})),n.keys=function(t){var e=Object(t),r=[];for(var n in e)r.push(n);return r.reverse(),function t(){for(;r.length;){var n=r.pop();if(n in e)return t.value=n,t.done=!1,t}return t.done=!0,t}},n.values=$,E.prototype={constructor:E,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(j),!t)for(var e in this)"t"===e.charAt(0)&&o.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var e=this;function r(r,n){return a.type="throw",a.arg=t,e.next=r,n&&(e.method="next",e.arg=void 0),!!n}for(var n=this.tryEntries.length-1;n>=0;--n){var i=this.tryEntries[n],a=i.completion;if("root"===i.tryLoc)return r("end");if(i.tryLoc<=this.prev){var s=o.call(i,"catchLoc"),c=o.call(i,"finallyLoc");if(s&&c){if(this.prev<i.catchLoc)return r(i.catchLoc,!0);if(this.prev<i.finallyLoc)return r(i.finallyLoc)}else if(s){if(this.prev<i.catchLoc)return r(i.catchLoc,!0)}else{if(!c)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return r(i.finallyLoc)}}}},abrupt:function(t,e){for(var r=this.tryEntries.length-1;r>=0;--r){var n=this.tryEntries[r];if(n.tryLoc<=this.prev&&o.call(n,"finallyLoc")&&this.prev<n.finallyLoc){var i=n;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=e,i?(this.method="next",this.next=i.finallyLoc,d):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),d},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),j(r),d}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var i=n.arg;j(r)}return i}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:$(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),d}},n}t.exports=r,t.exports.__esModule=!0,t.exports.default=t.exports})),v=y(),m=v;try{regeneratorRuntime=v}catch(t){"object"==typeof globalThis?globalThis.regeneratorRuntime=v:Function("r","regeneratorRuntime = r")(v)}function g(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=new Array(e);r<e;r++)n[r]=t[r];return n}function w(t){return function(t){if(Array.isArray(t))return g(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||function(t,e){if(t){if("string"==typeof t)return g(t,e);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?g(t,e):void 0}}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function _(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function b(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function x(t,e,r){return e in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}const k=t=>t&&t.includeBoundaries?"(?:(?<=\\s|^)(?=[a-fA-F\\d:])|(?<=[a-fA-F\\d:])(?=\\s|$))":"",A="(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}",O="[a-fA-F\\d]{1,4}",P=`\n(?:\n(?:${O}:){7}(?:${O}|:)| // 1:2:3:4:5:6:7:: 1:2:3:4:5:6:7:8\n(?:${O}:){6}(?:${A}|:${O}|:)| // 1:2:3:4:5:6:: 1:2:3:4:5:6::8 1:2:3:4:5:6::8 1:2:3:4:5:6::1.2.3.4\n(?:${O}:){5}(?::${A}|(?::${O}){1,2}|:)| // 1:2:3:4:5:: 1:2:3:4:5::7:8 1:2:3:4:5::8 1:2:3:4:5::7:1.2.3.4\n(?:${O}:){4}(?:(?::${O}){0,1}:${A}|(?::${O}){1,3}|:)| // 1:2:3:4:: 1:2:3:4::6:7:8 1:2:3:4::8 1:2:3:4::6:7:1.2.3.4\n(?:${O}:){3}(?:(?::${O}){0,2}:${A}|(?::${O}){1,4}|:)| // 1:2:3:: 1:2:3::5:6:7:8 1:2:3::8 1:2:3::5:6:7:1.2.3.4\n(?:${O}:){2}(?:(?::${O}){0,3}:${A}|(?::${O}){1,5}|:)| // 1:2:: 1:2::4:5:6:7:8 1:2::8 1:2::4:5:6:7:1.2.3.4\n(?:${O}:){1}(?:(?::${O}){0,4}:${A}|(?::${O}){1,6}|:)| // 1:: 1::3:4:5:6:7:8 1::8 1::3:4:5:6:7:1.2.3.4\n(?::(?:(?::${O}){0,5}:${A}|(?::${O}){1,7}|:)) // ::2:3:4:5:6:7:8 ::2:3:4:5:6:7:8 ::8 ::1.2.3.4\n)(?:%[0-9a-zA-Z]{1,})? // %eth0 %1\n`.replace(/\s*\/\/.*$/gm,"").replace(/\n/g,"").trim(),j=new RegExp(`(?:^${A}$)|(?:^${P}$)`),E=new RegExp(`^${A}$`),$=new RegExp(`^${P}$`),N=t=>t&&t.exact?j:new RegExp(`(?:${k(t)}${A}${k(t)})|(?:${k(t)}${P}${k(t)})`,"g");N.v4=t=>t&&t.exact?E:new RegExp(`${k(t)}${A}${k(t)}`,"g"),N.v6=t=>t&&t.exact?$:new RegExp(`${k(t)}${P}${k(t)}`,"g");var S=N;const C=t=>S({exact:!0}).test(t);C.v4=t=>S.v4({exact:!0}).test(t),C.v6=t=>S.v6({exact:!0}).test(t),C.version=t=>C(t)?C.v4(t)?4:6:void 0;var L=C,T="generated by trusted-cert",K="localhost",F=function(t){return[{name:"commonName",value:t}]},D=function(t){var e=t.caPrivKey,n=t.caCertAttrs,i=void 0===n?F(T):n,o=t.publicKey,a=t.certAttrs,s=void 0===a?F(K):a,c=t.extensions,u=void 0===c?[]:c,l=t.expiresIn,f=void 0===l?343008e5:l,p=new Date,h=r.pki.createCertificate();return h.publicKey=o,h.serialNumber=p.getTime().toString(16),h.serialNumber.length%2?h.serialNumber="0"+h.serialNumber:h.serialNumber.charCodeAt(0)>=56&&(h.serialNumber="00"+h.serialNumber),h.validity.notBefore=p,h.validity.notAfter=new Date(p.getTime()+f),h.setSubject(s),h.setIssuer(i),h.setExtensions(u),h.sign(e,r.md.sha256.create()),h};function I(t){var e=t.caPrivKey,r=t.caCertAttrs,n=t.publicKey,i=t.hosts,o=t.expiresIn,a=i.findIndex((function(t){return!L(t)})),s=[{name:"commonName",value:-1===a?"localhost":i[a]}],c=[{name:"keyUsage",digitalSignature:!0,keyEncipherment:!0,nonRepudiation:!0},{name:"extKeyUsage",serverAuth:!0,clientAuth:!0}],u=2,l=7,f=[];return i.forEach((function(t){L(t)?f.push({type:l,ip:t}):f.push({type:u,value:t})})),c.push({name:"subjectAltName",altNames:f}),D({caPrivKey:e,caCertAttrs:r,certAttrs:s,publicKey:n,extensions:c,expiresIn:o})}var R=function(t){var e=new Set;e.add(M(t));var r=t.getExtension("subjectAltName");return null!=r&&r.altNames&&r.altNames.forEach((function(t){2===t.type?e.add(t.value):7===t.type&&e.add(t.ip)})),Array.from(e)},G=function(t){var e=r.asn1.toDer(r.pki.certificateToAsn1(t)).getBytes();return r.md.sha1.create().update(e).digest().toHex().toUpperCase()},q=function(t){return"".concat(t.validity.notBefore," ~ ").concat(t.validity.notAfter)},M=function(t){var e=t.subject.getField({name:"commonName"});if(!e)throw new Error("Failed reading commonName of cert");return e.value},H=function(t,e){return e.verify(t)},U=function(t){var e=new Date;return!(e<t.validity.notBefore||e>t.validity.notAfter)},B=u.default("trusted-cert:platform:darwin"),X=function(){var t=p(m.mark((function t(e){return m.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,new Promise((function(t){try{B('sudo security delete-certificate -c "'.concat(e,'"')),o.execSync('sudo security delete-certificate -c "'.concat(e,'"')),t(!0)}catch(e){t(!1)}}));case 2:return t.abrupt("return",t.sent);case 3:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}(),Y=u.default("trusted-cert:platform:win32"),Z=function(){var t=p(m.mark((function t(e){return m.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,new Promise((function(t,r){try{o.execSync('certutil -delstore -user root "'.concat(e,'"')),t(!0)}catch(e){t(!1)}}));case 2:return t.abrupt("return",t.sent);case 3:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}(),z="darwin"===process.platform,J="win32"===process.platform,Q=z?function(t){B("添加证书%o到系统钥匙串",t),o.execSync("sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain '".concat(t,"'"))}:J?function(t){o.exec("certutil -addstore -user root ".concat(t))}:function(t){o.execSync("sudo cp ".concat(t," /usr/local/share/ca-certificates/devcert.crt")),o.execSync("sudo update-ca-certificates")},V=z?function(t){var e;B("查询钥匙串里名称是%o的证书",t);try{e=o.execSync("security find-certificate -a -c '".concat(t,"' -Z | grep ^SHA-1"),{encoding:"utf-8"}).replace(/SHA-1\shash:\s/g,"").split("\n").filter((function(t){return t}))}catch(t){e=[]}return B("查询到的sha1 %o",e),e}:J?function(t){var e;try{e=o.execSync('certutil -verifystore -user root "'.concat(t,'" | findstr sha1'),{encoding:"utf8"}).split("\n").map((function(t){return t.replace(/.*\(sha1\):\s/,"").replace(/[\s\r]/g,"").toUpperCase()})).filter(Boolean)}catch(t){Y("获取钥匙串里证书失败%o",t),e=[]}return e}:function(){return[]},W=z?X:J?Z:function(){};function tt(t){if("string"!=typeof t)throw new TypeError("`name` must be string");switch(l.default.platform()){case"darwin":return c.default.join(process.env.HOME,".trusted-cert");case"linux":return function(t){return null!=process.env.XDG_CONFIG_HOME?c.default.join(process.env.XDG_CONFIG_HOME,t):c.default.join(process.env.HOME,".config",t)}(t);case"win32":return function(t){return null!=process.env.LOCALAPPDATA?c.default.join(process.env.LOCALAPPDATA,t):c.default.join(process.env.USERPROFILE,"Local Settings","Application Data",t)}(t)}throw new Error("Platform not supported")}var et={ca_create_failed:"生成 CA 根证书失败: %s",ca_not_created:"❌ CA 根证书未生成",ca_info_name:"CA 证书名称: %s",ca_info_fingerprint:"CA 证书指纹 (sha-1): %s",ca_info_valid_period:"CA 证书有效期: %s",ca_info_trusted:"✅ CA 根证书已经添加到钥匙串并被始终信任",ca_info_not_trusted:"❌ CA 根证书未被信任,请执行以下命令添加信任\n$ trusted-cert trust",info_no_install:"还没有安装自签名证书,运行下面命令安装使用\n$ trusted-cert install",info_ssl_key_path:"密钥文件路径: %s",info_ssl_cert_path:"证书文件路径: %s",info_ssl_cert_support_hosts:"支持的域名:\n%s",info_ssl_cert_valid_period:"证书有效期: %s",add_trust_process:"正在将 CA 证书写入系统信任区,请输入密码并同意",add_trust_succeed:"添加成功",add_trust_failed:"添加失败: %s",sign_ca_mismatch:"当前证书不由本地的 CA 签署,需要重新签署证书",sign_cert_satisfied:"现有证书已经满足需求",sign_complete:"证书签发完成",sign_cert_expired:"现有证书已过期,需要重新签署证书",sign_host_empty:"主机名列表为空,无法签发证书",uninstall_del_keychain:"正在删除钥匙串里名称「%s」的证书",uninstall_del_keychain_success:"删除成功",uninstall_del_keychain_failure:"删除失败: %s",uninstall_complete:"删除完成"};function rt(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),r.push.apply(r,n)}return r}function nt(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?rt(Object(r),!0).forEach((function(e){x(t,e,r[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):rt(Object(r)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))}))}return t}function it(t){return nt(nt({},et),t)}var ot=function(t,e){return e.every((function(e){return t.find((function(t){return t.includes("*")?new RegExp(t.replace("*","^[^.]+")+"$").test(e):t===e}))}))},at=function(t,e){return e.filter((function(e){return null==t.find((function(t){return t.includes("*")?new RegExp(t.replace("*","^[^.]+")+"$").test(e):t===e}))}))};function st(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),r.push.apply(r,n)}return r}function ct(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?st(Object(r),!0).forEach((function(e){x(t,e,r[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):st(Object(r)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))}))}return t}var ut=u.default("trusted-cert:class"),lt=function(t,e){return n.join(t,"".concat(e,".crt"))},ft=function(t,e){return n.join(t,"".concat(e,".key"))},pt=function(t,n,i){e.writeFileSync(lt(t,n),r.pki.certificateToPem(i))},ht=function(t,n,i){e.writeFileSync(ft(t,n),r.pki.privateKeyToPem(i))},dt=function(t){return Promise.all(t.map((function(t){return e.rm(t).catch((function(){}))})))},yt=function(){function n(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=t.dir,r=void 0===e?tt("trusted-cert"):e,i=t.caName,o=void 0===i?"ca":i,a=t.sslName,s=void 0===a?"ssl":a,c=t.quiet,u=void 0!==c&&c,l=t.i18n,f=void 0===l?{}:l;_(this,n),x(this,"dir",void 0),x(this,"caName",void 0),x(this,"sslName",void 0),x(this,"quiet",void 0),x(this,"i18n",void 0),this.dir=r,this.caName=o,this.sslName=s,this.quiet=u,this.i18n=it(f)}var i,o,a,s,c,u;return i=n,o=[{key:"install",value:function(){var t=p(m.mark((function t(e){var r,n,i,o,a,s;return m.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.hosts,n=e.overwrite,i=void 0!==n&&n,t.next=3,this.ensureCA();case 3:return o=t.sent,t.next=6,this.trust(o);case 6:return a=t.sent,t.next=9,this.sign({ca:o,hosts:r,overwrite:i});case 9:return s=t.sent,t.abrupt("return",ct(ct({},s),{},{trusted:a}));case 11:case"end":return t.stop()}}),t,this)})));return function(e){return t.apply(this,arguments)}}()},{key:"uninstall",value:function(){var t=p(m.mark((function t(){var e,r;return m.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(e=this.loadCA())){t.next=17;break}if(!this.isCertTrusted(e.cert)){t.next=15;break}return r=M(e.cert),this.log(this.l("uninstall_del_keychain",r)),t.prev=5,t.next=8,W(r);case 8:this.log(this.l("uninstall_del_keychain_success")),t.next=15;break;case 11:return t.prev=11,t.t0=t.catch(5),console.error(this.l("uninstall_del_keychain_failure",t.t0.message)),t.abrupt("return",!1);case 15:return t.next=17,dt([lt(this.dir,this.caName),ft(this.dir,this.caName)]);case 17:return t.next=19,dt([lt(this.dir,this.sslName),ft(this.dir,this.sslName)]);case 19:this.log(this.l("uninstall_complete"));case 20:case"end":return t.stop()}}),t,this,[[5,11]])})));return function(){return t.apply(this,arguments)}}()},{key:"sign",value:(u=p(m.mark((function t(n){var i,o,a,s,c,u,l,f,p,h,d,y,v;return m.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(i=n.ca,o=n.hosts,a=n.overwrite,s=void 0!==a&&a,i){t.next=5;break}return t.next=4,this.ensureCA();case 4:i=t.sent;case 5:if(e.ensureDirSync(this.dir),c=this.loadSSL(),u=w(o),s||!c){t.next=18;break}if(l=!0,H(c.cert,i.cert)||(this.log(this.l("sign_ca_mismatch")),l=!1),U(c.cert)||(this.log(this.l("sign_cert_expired")),l=!1),f=R(c.cert),!l||!ot(f,o)){t.next=16;break}return this.log(this.l("sign_cert_satisfied")),t.abrupt("return",{key:r.pki.privateKeyToPem(c.key),cert:r.pki.certificateToPem(c.cert),keyFilePath:ft(this.dir,this.sslName),certFilePath:lt(this.dir,this.sslName)});case 16:p=at(f,u),u=[].concat(w(f),w(p));case 18:if(0!==u.length){t.next=20;break}throw new Error(this.l("sign_host_empty"));case 20:if(!(h=null==c?void 0:c.key)){t.next=25;break}d=r.pki.rsa.setPublicKey(h.n,h.e),t.next=31;break;case 25:return t.next=27,this.generateKeyPair();case 27:y=t.sent,ht(this.dir,this.sslName,y.privateKey),h=y.privateKey,d=y.publicKey;case 31:return v=I({caPrivKey:i.key,caCertAttrs:i.cert.subject.attributes,publicKey:d,hosts:Array.from(u)}),pt(this.dir,this.sslName,v),this.log(this.l("sign_complete")),t.abrupt("return",{key:r.pki.privateKeyToPem(h),cert:r.pki.certificateToPem(v),keyFilePath:ft(this.dir,this.sslName),certFilePath:lt(this.dir,this.sslName)});case 35:case"end":return t.stop()}}),t,this)}))),function(t){return u.apply(this,arguments)})},{key:"doTrust",value:function(){var t=p(m.mark((function t(){var e;return m.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,this.ensureCA();case 2:return e=t.sent,t.abrupt("return",this.trust(e));case 4:case"end":return t.stop()}}),t,this)})));return function(){return t.apply(this,arguments)}}()},{key:"info",value:function(){var t=this.loadSSL();if(t){var e=q(t.cert),r=R(t.cert);this.log(this.l("info_ssl_key_path",ft(this.dir,this.sslName))),this.log(this.l("info_ssl_cert_path",lt(this.dir,this.sslName))),this.log(this.l("info_ssl_cert_valid_period",e)),this.log(this.l("info_ssl_cert_support_hosts",r.join(", ")))}else this.log(this.l("info_no_install"))}},{key:"caInfo",value:function(){var t=this.loadCA();if(t){var e=G(t.cert),r=q(t.cert),n=M(t.cert);this.log(this.l("ca_info_name",n)),this.log(this.l("ca_info_fingerprint",e)),this.log(this.l("ca_info_valid_period",r)),this.log(""),this.isCertTrusted(t.cert)?this.log(this.l("ca_info_trusted")):this.log(this.l("ca_info_not_trusted"))}else this.log(this.l("ca_not_created"))}},{key:"trust",value:(c=p(m.mark((function t(e){var r;return m.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=!1,!this.isCertTrusted(e.cert)){this.log(this.l("add_trust_process"));try{Q(lt(this.dir,this.caName)),r=!0,this.log(this.l("add_trust_succeed"))}catch(t){console.warn(this.l("add_trust_failed",t.message)),r=!1}}return t.abrupt("return",r);case 3:case"end":return t.stop()}}),t,this)}))),function(t){return c.apply(this,arguments)})},{key:"generateKeyPair",value:function(){return t={bits:2048,workers:4},new Promise((function(e,n){r.pki.rsa.generateKeyPair(t,(function(t,r){t?n(t):e(r)}))}));var t}},{key:"loadCertAndKey",value:function(t){try{var n=function(t,n){return r.pki.certificateFromPem(e.readFileSync(lt(t,n),"utf-8"))}(this.dir,t),i=function(t,n){return r.pki.privateKeyFromPem(e.readFileSync(ft(t,n),"utf-8"))}(this.dir,t);return{cert:n,key:i}}catch(t){if("ENOENT"!==t.code)throw t;return null}}},{key:"loadSSL",value:function(){return this.loadCertAndKey(this.sslName)}},{key:"loadCA",value:function(){return this.loadCertAndKey(this.caName)}},{key:"ensureCA",value:(s=p(m.mark((function t(){var r,n,i,o;return m.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(r=this.loadCA())){t.next=3;break}return t.abrupt("return",r);case 3:return t.prev=3,t.next=6,this.generateKeyPair();case 6:o=t.sent,n=D({caPrivKey:(a=o).privateKey,publicKey:a.publicKey,certAttrs:F(T),extensions:[{name:"basicConstraints",cA:!0}],expiresIn:31536e7}),i=o.privateKey,e.ensureDirSync(this.dir),pt(this.dir,this.caName,n),ht(this.dir,this.caName,i),t.next=17;break;case 14:throw t.prev=14,t.t0=t.catch(3),new Error(this.l("ca_create_failed",t.t0.message));case 17:return t.abrupt("return",{cert:n,key:i});case 18:case"end":return t.stop()}var a}),t,this,[[3,14]])}))),function(){return s.apply(this,arguments)})},{key:"isCertTrusted",value:function(t){if(!U(t))return!1;var e=M(t),r=V(e),n=G(t);return ut("已经添加信任的证书sha1 ".concat(r.join(","))),ut("证书文件的sha1 ".concat(n)),r.includes(n)}},{key:"l",value:function(e){for(var r=this.i18n[e],n=arguments.length,i=new Array(n>1?n-1:0),o=1;o<n;o++)i[o-1]=arguments[o];return i.length?t.format.apply(void 0,[r].concat(i)):r}},{key:"log",value:function(t){this.quiet||console.log(t)}}],o&&b(i.prototype,o),a&&b(i,a),Object.defineProperty(i,"prototype",{writable:!1}),n}();function vt(t){return mt.apply(this,arguments)}function mt(){return(mt=p(m.mark((function t(e){return m.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.abrupt("return",(new yt).install({hosts:e}));case 1:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function gt(){return(gt=p(m.mark((function t(e){var r;return m.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=new yt,t.next=3,r.install({hosts:e});case 3:r.info();case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function wt(){return(wt=p(m.mark((function t(){return m.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.abrupt("return",(new yt).uninstall());case 1:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _t(){return(_t=p(m.mark((function t(){var e;return m.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:(e=new yt).info(),e.caInfo();case 3:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function bt(){return(bt=p(m.mark((function t(){var e;return m.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return e=new yt,t.next=3,e.doTrust();case 3:e.caInfo();case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var xt=t.deprecate(p(m.mark((function t(){var e,r=arguments;return m.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return e=r.length>0&&void 0!==r[0]?r[0]:[],t.prev=1,t.next=4,vt(e);case 4:return t.abrupt("return",!0);case 7:return t.prev=7,t.t0=t.catch(1),t.abrupt("return",!1);case 10:case"end":return t.stop()}}),t,null,[[1,7]])}))),"addHosts() is deprecated. Use certificateFor() instead."),kt=t.deprecate((function(){}),"setConfig() is deprecated and has no effect. Please create your own TrustedCert instance."),At=t.deprecate((function(){}),"mergeLan() is deprecated and has no effect. Please create your own TrustedCert instance.");exports.TrustedCert=yt,exports.addHosts=xt,exports.certificateFor=vt,exports.doTrust=function(){return bt.apply(this,arguments)},exports.info=function(){return _t.apply(this,arguments)},exports.install=function(t){return gt.apply(this,arguments)},exports.mergeLan=At,exports.setConfig=kt,exports.uninstall=function(){return wt.apply(this,arguments)};