Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

localhost-https

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

localhost-https - npm Package Compare versions

Comparing version
1.0.7
to
1.0.12
certs/localhost.crt

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

+87
const https = require('https');
const { getRootCrt } = require('./utils');
const getLocalhostCerts = require('./getLocalhostCerts');
const path = require('path');
const fs = require('fs-extra');
let testServer = null;
async function startTestServer() {
if (testServer) {
return testServer;
}
const testPort = 11790 + Math.floor(Math.random() * 100);
const options = getLocalhostCerts();
try {
testServer = https.createServer(options, (req, res) => {
res.writeHead(200);
res.end('TESTSERVER');
});
testServer.on('error', (error) => {
testServer.close();
});
testServer.on('close', () => {
testServer = null;
});
testServer.listen(testPort);
return { server: testServer, port: testPort };
} catch (error) {
return null;
}
}
// 检测根证书的安装情况
module.exports = async function checkRootCertificateInstallation() {
try {
// mac
if (process.platform === 'darwin') {
return await checkMacCert();
}
// windows
else if (process.platform === 'win32') {
return await checkWinCert();
}
// linux
else if (process.platform === 'linux') {
return await checkLinuxCert();
}
} catch (error) {
console.log('error', error);
return false;
}
};
function replaceEnter(str) {
try {
return str.replace(/\n/g, '').replace(/\r/g, '').replace(/\t/g, '');
} catch (error) {
return str;
}
}
async function checkMacCert() {
const macCa = require('mac-ca');
const rootCrt = await getRootCrt();
const ca = await macCa.get();
return ca.some((item) => {
return item && replaceEnter(item) === replaceEnter(rootCrt);
});
}
async function checkWinCert() {
const ca = require('win-ca');
rootCAs = [];
// Fetch all certificates in PEM format
ca({
format: ca.der2.pem,
ondata: (crt) => {
rootCAs.push(crt);
},
});
const rootCrt = await getRootCrt();
return rootCAs.some((item) => {
return item && replaceEnter(item) === replaceEnter(rootCrt);
});
}
async function checkLinuxCert() {
return await fs.exists(`/usr/local/share/ca-certificates/root.crt`);
}
const forge = require('node-forge');
const { writeCert } = require('./utils');
module.exports = function createRootCert() {
const keys = forge.pki.rsa.generateKeyPair(2048);
const cert = forge.pki.createCertificate();
cert.publicKey = keys.publicKey;
cert.serialNumber = '01';
cert.validity.notBefore = new Date();
cert.validity.notAfter = new Date();
cert.validity.notAfter.setFullYear(
cert.validity.notBefore.getFullYear() + 20
);
const attrs = [
{
name: 'commonName',
value: 'Localhost Root Ca No.1',
},
{
name: 'countryName',
value: 'US',
},
{
shortName: 'ST',
value: 'Virginia',
},
{
name: 'localityName',
value: 'Blacksburg',
},
{
name: 'organizationName',
value: 'localhost-https',
},
{
shortName: 'OU',
value: 'LH',
},
];
cert.setSubject(attrs);
cert.setIssuer(attrs);
cert.setExtensions([
{
name: 'basicConstraints',
cA: true,
},
{
name: 'keyUsage',
keyCertSign: true,
digitalSignature: true,
nonRepudiation: true,
keyEncipherment: true,
dataEncipherment: true,
},
{
name: 'subjectKeyIdentifier',
},
]);
cert.sign(keys.privateKey, forge.md.sha256.create());
// generate a server keypair
const serverKeys = forge.pki.rsa.generateKeyPair(2048);
const serverCert = forge.pki.createCertificate();
serverCert.publicKey = serverKeys.publicKey;
serverCert.serialNumber = '01';
serverCert.validity.notBefore = new Date();
serverCert.validity.notAfter = new Date();
serverCert.validity.notAfter.setFullYear(
serverCert.validity.notBefore.getFullYear() + 20
);
const serverAttrs = [
{
name: 'commonName',
value: 'Localhost Root Ca No.1',
},
{
name: 'countryName',
value: 'US',
},
{
shortName: 'ST',
value: 'Virginia',
},
{
name: 'localityName',
value: 'Blacksburg',
},
{
name: 'organizationName',
value: 'localhost-https',
},
{
shortName: 'OU',
value: 'LH',
},
];
serverCert.setSubject(serverAttrs);
serverCert.setIssuer(cert.subject.attributes);
serverCert.setExtensions([
{
name: 'basicConstraints',
cA: false,
},
{
name: 'keyUsage',
digitalSignature: true,
nonRepudiation: true,
keyEncipherment: true,
dataEncipherment: true,
},
{
name: 'subjectAltName',
altNames: [
{
type: 2, // DNS
value: 'localhost',
},
],
},
]);
serverCert.sign(keys.privateKey, forge.md.sha256.create());
// save root certificate and key
const rootCrt = forge.pki.certificateToPem(cert);
const localhostCrt = forge.pki.certificateToPem(serverCert);
const localhostKey = forge.pki.privateKeyToPem(serverKeys.privateKey);
writeCert('root.pem', forge.pki.certificateToPem(cert));
writeCert('root.crt', forge.pki.certificateToPem(cert));
// save server certificate and key
writeCert('localhost.crt', forge.pki.certificateToPem(serverCert));
writeCert(
'localhost.key',
forge.pki.privateKeyToPem(serverKeys.privateKey)
);
console.log('certs created');
console.log('-----------------root.pem-----------------');
console.log(rootCrt);
console.log('-----------------localhost.crt-----------------');
console.log(localhostCrt);
console.log('-----------------localhost.key-----------------');
console.log(localhostKey);
};
const { readCert } = require('./utils');
const getLocalhostCerts = () => {
const localhostCrt = readCert('localhost.crt');
const localhostKey = readCert('localhost.key');
return {
cert: localhostCrt,
key: localhostKey,
};
};
module.exports = getLocalhostCerts;
const cp = require('child_process');
const process = require('process');
var sudo = require('sudo-prompt');
const path = require('path');
module.exports = function install() {
// windows 运行 certutil -addstore -enterprise -f "Root" "C:\path\to\your\certificate.crt"
// mac 运行 sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain /path/to/your/certificate.crt
// linux 运行 sudo cp /path/to/your/certificate.crt /usr/local/share/ca-certificates/ && sudo update-ca-certificates
// 以上命令均需要管理员权限
try {
const crtPath = path.resolve(__dirname, '../certs/root.crt');
if (process.platform === 'win32') {
const cmd =
'certutil -addstore -enterprise -f "Root" "' + crtPath + '"';
var options = {
name: 'Localhost Cert',
};
sudo.exec(cmd, options, function (error, stdout, stderr) {
if (error) throw error;
});
} else if (process.platform === 'darwin') {
const proce = cp.exec(
'sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain "' +
crtPath +
'"'
);
proce.on('exit', function (code) {
console.log('child process exited with code ' + code);
});
} else if (process.platform === 'linux') {
cp.exec(
'sudo cp "' +
crtPath +
'" /usr/local/share/ca-certificates/ && sudo update-ca-certificates'
);
} else {
console.log('unsupport platform:' + process.platform);
}
} catch (error) {
console.warn('install root certificate error:' + error);
console.warn(
'please download at:https://github.com/IdeaNest-org/localhost-cert/blob/main/certs/root.crt and install root certificate manually'
);
}
};
const { Octokit } = require('@octokit/rest');
const fs = require('fs');
const { readCert } = require('./utils');
function getGitHubToken() {
const token = process.env.GH_TOKEN;
if (!token) {
throw new Error('Please set the GH_TOKEN environment variable.');
}
return token;
}
module.exports = async function uploadToGithub() {
const octokit = new Octokit({
auth: getGitHubToken(), // 请替换为你的个人访问令牌
});
// 保存证书到本地
async function uploadFileToGitHubRepo(
owner,
repo,
filePath,
branch,
message
) {
const fileContent = fs.readFileSync(filePath, 'utf-8');
const contentEncoded = Buffer.from(fileContent).toString('base64');
try {
const response = await octokit.repos.createOrUpdateFileContents({
owner: owner,
repo: repo,
path: filePath,
message: message,
content: contentEncoded,
branch: branch,
});
console.log(
'File uploaded to GitHub:',
response.data.content.html_url
);
} catch (error) {
console.error('Error uploading file to GitHub:', error.message);
}
}
const rootCrt = readCert('root.crt');
const localhostCrt = readCert('localhost.crt');
const localhostKey = readCert('localhost.key');
console.log(rootCrt);
console.log(localhostCrt);
console.log(localhostKey);
// 使用示例
await uploadFileToGitHubRepo(
'IdeaNest-org',
'localhost-cert',
'certs/root.crt',
'main',
rootCrt
);
await uploadFileToGitHubRepo(
'IdeaNest-org',
'localhost-cert',
'certs/localhost.crt',
'main',
localhostCrt
);
await uploadFileToGitHubRepo(
'IdeaNest-org',
'localhost-cert',
'certs/localhost.key',
'main',
localhostKey
);
};
const path = require('path');
const fs = require('fs-extra');
function readFileFromDir(file) {
return fs.readFileSync(path.resolve(__dirname, '../', file), 'utf-8');
}
function readFileFromDirAsync(file) {
return fs.readFile(path.resolve(__dirname, '../', file), 'utf-8');
}
function getRootCrt() {
return readFileFromDirAsync('certs/root.crt');
}
function writeFileToDir(file, content) {
return fs.writeFileSync(path.resolve(__dirname, '../', file), content);
}
function readCert(name) {
return readFileFromDir('certs/' + name);
}
function writeCert(name, content) {
return writeFileToDir('certs/' + name, content);
}
module.exports = {
readFileFromDir,
readFileFromDirAsync,
getRootCrt,
writeFileToDir,
readCert,
writeCert,
};
const checkRootCertInstall = require('../modules/checkRootCertInstall');
const installRootCert = require('../modules/installRootCert');
checkRootCertInstall().then((res) => {
console.log(res);
if (!res) {
console.log('start install root cert')
installRootCert();
}
});
const createRootCert = require('../modules/createRootCert');
function create() {
createRootCert();
}
create();
const getHttps = require('../index');
console.log(getHttps())
const uploadToGithub = require('../modules/uploadToGithub');
uploadToGithub()
const {
readFileFromDir,
readFileFromDirAsync,
getRootCrt,
} = require('../modules/utils');
console.log(readFileFromDir('certs/root.crt'));
readFileFromDirAsync('certs/root.crt').then((res) => {
console.log(res);
});
getRootCrt().then((res) => {
console.log(res);
});
+2
-2
// set cra https env
const path = require('path');
process.env.HTTPS = true;
process.env.SSL_CRT_FILE = path.resolve(__dirname, '../localhost.crt');
process.env.SSL_KEY_FILE = path.resolve(__dirname, '../localhost.key');
process.env.SSL_CRT_FILE = path.resolve(__dirname, '../certs/localhost.crt');
process.env.SSL_KEY_FILE = path.resolve(__dirname, '../certs/localhost.key');
// 判断当前是否存在文件
const fs = require('fs');
const path = require('path');
const createRootCert = require('./createRootCert');
const saveAndDelete = require('./saveAndDelete');
const createRootCert = require('./modules/createRootCert');
const uploadToGithub = require('./modules/uploadToGithub');

@@ -17,10 +17,10 @@ function isFileExist(filePath) {

if (
!isFileExist(path.resolve(__dirname, 'root.pem')) ||
!isFileExist(path.resolve(__dirname, 'localhost.key')) ||
!isFileExist(path.resolve(__dirname, 'localhost.crt'))
!isFileExist(path.resolve(__dirname, 'certs/root.crt')) ||
!isFileExist(path.resolve(__dirname, 'certs/localhost.key')) ||
!isFileExist(path.resolve(__dirname, 'certs/localhost.crt'))
) {
createRootCert();
saveAndDelete();
uploadToGithub();
} else {
console.log('certs already exist');
}
+10
-89

@@ -1,91 +0,11 @@

// 引导安装根证书
const cp = require('child_process');
const process = require('process');
const path = require('path');
const fs = require('fs-extra');
const tls = require('tls');
const https = require('https');
function install() {
// windows 运行 certutil -addstore -enterprise -f "Root" "C:\path\to\your\certificate.crt"
// mac 运行 sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain /path/to/your/certificate.crt
// linux 运行 sudo cp /path/to/your/certificate.crt /usr/local/share/ca-certificates/ && sudo update-ca-certificates
// 以上命令均需要管理员权限
try {
console.log('install root certificate');
const crtPath = path.resolve(__dirname, 'root.crt');
if (process.platform === 'win32') {
cp.exec(
'certutil -addstore -enterprise -f "Root" "' + crtPath + '"'
);
} else if (process.platform === 'darwin') {
cp.exec(
'sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain "' +
crtPath +
'"'
);
} else if (process.platform === 'linux') {
cp.exec(
'sudo cp "' +
crtPath +
'" /usr/local/share/ca-certificates/ && sudo update-ca-certificates'
);
} else {
console.log('unsupport platform:' + process.platform);
}
} catch (error) {
console.log(error);
}
}
const localhostCrt = readFileFromDir('localhost.crt');
const localhostKey = readFileFromDir('localhost.key');
// 检测根证书的安装情况
async function checkRootCertificateInstallation() {
const rootCrt = await readFileFromDirAsync('root.crt');
const options = {
ca: [rootCrt], // 添加你要验证的根证书
checkServerIdentity: () => null, // 禁用服务器身份验证
};
// https 启动localhost 服务
startTestServer();
return await new Promise((resolve, reject) => {
const socket = tls.connect(8844, 'localhost', options, () => {
console.log('Root certificate is installed.');
socket.end();
resolve(true);
});
socket.on('error', (error) => {
resolve(false);
socket.end();
});
});
}
async function startTestServer() {
const options = getHttps(true);
https
.createServer(options, (req, res) => {
res.writeHead(200);
res.end('Hello World!');
})
.listen(8843);
console.log('server start at 8843');
}
function readFileFromDir(file) {
return fs.readFileSync(path.resolve(__dirname, file), 'utf-8');
}
function readFileFromDirAsync(file) {
return fs.readFile(path.resolve(__dirname, file), 'utf-8');
}
// 调用检测函数
module.exports = getHttps = ({ autoInstall = false } = {}) => {
const checkRootCertInstall = require('./modules/checkRootCertInstall');
const installRootCert = require('./modules/installRootCert');
const { readCert } = require('./modules/utils');
const getHttpsConfig = ({ autoInstall = true } = {}) => {
if (autoInstall) {
checkRootCertificateInstallation().then((res) => {
checkRootCertInstall().then((res) => {
console.log('is root install', res);
if (!res) {
install();
installRootCert();
}

@@ -95,5 +15,6 @@ });

return {
cert: localhostCrt,
key: localhostKey,
cert: readCert('localhost.crt'),
key: readCert('localhost.key'),
};
};
module.exports = getHttpsConfig;
{
"name": "localhost-https",
"version": "1.0.7",
"version": "1.0.12",
"main": "index.js",

@@ -23,11 +23,15 @@ "scripts": {

"keywords": [
"localhost",
"https",
"ssl",
"tls",
"certificate",
"localhost-certificate",
"localhost-https",
"localhost-ssl",
"localhost-cert"
"localhost",
"https",
"ssl",
"tls",
"certificate",
"localhost-certificate",
"localhost-https",
"localhost-ssl",
"localhost-cert",
"https-localhost",
"https-server",
"minica",
"mkcert"
],

@@ -42,7 +46,11 @@ "npmIgnore": [

"dependencies": {
"fs-extra": "^11.1.1",
"mac-ca": "^2.0.3",
"sudo-prompt": "^9.2.1",
"win-ca": "^3.5.1"
},
"devDependencies": {
"@octokit/rest": "^20.0.1",
"cross-env": "^7.0.3",
"fs-extra": "^11.1.1",
"node-forge": "^1.3.1"
}
}

@@ -36,3 +36,3 @@

```javascript
const getHttps = require('localhost-cert');
const getHttps = require('localhost-https');

@@ -52,3 +52,3 @@ // webpack config

// vite config
const getHttps = require('localhost-cert');
const getHttps = require('localhost-https');
export default defineConfig({

@@ -55,0 +55,0 @@ server: {

@@ -31,3 +31,3 @@ # Localhost Cert

```javascript
const getHttps = require('localhost-cert');
const getHttps = require('localhost-https');

@@ -47,3 +47,3 @@ // webpack config

// vite config
const getHttps = require('localhost-cert');
const getHttps = require('localhost-https');
export default defineConfig({

@@ -50,0 +50,0 @@ server: {

@@ -0,9 +1,7 @@

const getHttps = require('./index.js');
const options = getHttps({
autoInstall: true,
});
console.log(options);
const https = require('https');
const fs = require('fs');
const options = {
key: fs.readFileSync('localhost.key'),
cert: fs.readFileSync('localhost.crt'),
};
https

@@ -15,1 +13,2 @@ .createServer(options, (req, res) => {

.listen(8443);
console.log('Server running at https://localhost:8443/');
const forge = require('node-forge');
const fs = require('fs');
const path = require('path');
module.exports = function createRootCert() {
// generate a root CA keypair
const keys = forge.pki.rsa.generateKeyPair(2048);
const cert = forge.pki.createCertificate();
cert.publicKey = keys.publicKey;
cert.serialNumber = '01';
cert.validity.notBefore = new Date();
cert.validity.notAfter = new Date();
cert.validity.notAfter.setFullYear(
cert.validity.notBefore.getFullYear() + 20
);
const attrs = [
{
name: 'commonName',
value: 'My Root CA',
},
{
name: 'countryName',
value: 'US',
},
{
shortName: 'ST',
value: 'Virginia',
},
{
name: 'localityName',
value: 'Blacksburg',
},
{
name: 'organizationName',
value: 'Test',
},
{
shortName: 'OU',
value: 'Test',
},
];
cert.setSubject(attrs);
cert.setIssuer(attrs);
cert.setExtensions([
{
name: 'basicConstraints',
cA: true,
},
{
name: 'keyUsage',
keyCertSign: true,
digitalSignature: true,
nonRepudiation: true,
keyEncipherment: true,
dataEncipherment: true,
},
{
name: 'subjectKeyIdentifier',
},
]);
cert.sign(keys.privateKey, forge.md.sha256.create());
// generate a server keypair
const serverKeys = forge.pki.rsa.generateKeyPair(2048);
const serverCert = forge.pki.createCertificate();
serverCert.publicKey = serverKeys.publicKey;
serverCert.serialNumber = '01';
serverCert.validity.notBefore = new Date();
serverCert.validity.notAfter = new Date();
serverCert.validity.notAfter.setFullYear(
serverCert.validity.notBefore.getFullYear() + 20
);
const serverAttrs = [
{
name: 'commonName',
value: 'localhost',
},
{
name: 'countryName',
value: 'US',
},
{
shortName: 'ST',
value: 'Virginia',
},
{
name: 'localityName',
value: 'Blacksburg',
},
{
name: 'organizationName',
value: 'Test',
},
{
shortName: 'OU',
value: 'Test',
},
];
serverCert.setSubject(serverAttrs);
serverCert.setIssuer(cert.subject.attributes);
serverCert.setExtensions([
{
name: 'basicConstraints',
cA: false,
},
{
name: 'keyUsage',
digitalSignature: true,
nonRepudiation: true,
keyEncipherment: true,
dataEncipherment: true,
},
{
name: 'subjectAltName',
altNames: [
{
type: 2, // DNS
value: 'localhost',
},
],
},
]);
serverCert.sign(keys.privateKey, forge.md.sha256.create());
// save root certificate and key
const rootCrt = forge.pki.certificateToPem(cert);
const localhostCrt = forge.pki.certificateToPem(serverCert);
const localhostKey = forge.pki.privateKeyToPem(serverKeys.privateKey);
fs.writeFileSync('root.pem', forge.pki.certificateToPem(cert));
fs.writeFileSync('root.crt', forge.pki.certificateToPem(cert));
// save server certificate and key
fs.writeFileSync('localhost.crt', forge.pki.certificateToPem(serverCert));
fs.writeFileSync(
'localhost.key',
forge.pki.privateKeyToPem(serverKeys.privateKey)
);
console.log('certs created')
console.log('-----------------root.pem-----------------')
console.log(rootCrt);
console.log('-----------------localhost.crt-----------------')
console.log(localhostCrt);
console.log('-----------------localhost.key-----------------')
console.log(localhostKey);
};

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

const { Octokit } = require('@octokit/rest');
const fs = require('fs');
function getGitHubToken() {
const token = process.env.GH_TOKEN;
console.log('token', token);
if (!token) {
throw new Error('Please set the GH_TOKEN environment variable.');
}
return token;
}
const octokit = new Octokit({
auth: getGitHubToken(), // 请替换为你的个人访问令牌
});
module.exports = async function saveAndDelete() {
// 保存证书到本地
async function uploadFileToGitHubRepo(
owner,
repo,
filePath,
branch,
message
) {
const fileContent = fs.readFileSync(filePath, 'utf-8');
const contentEncoded = Buffer.from(fileContent).toString('base64');
try {
const response = await octokit.repos.createOrUpdateFileContents({
owner: owner,
repo: repo,
path: filePath,
message: message,
content: contentEncoded,
branch: branch,
});
console.log(
'File uploaded to GitHub:',
response.data.content.html_url
);
} catch (error) {
console.error('Error uploading file to GitHub:', error.message);
}
}
const rootCrt = fs.readFileSync('root.crt', 'utf-8');
const localhostCrt = fs.readFileSync('localhost.crt', 'utf-8');
const localhostKey = fs.readFileSync('localhost.key', 'utf-8');
console.log(rootCrt);
console.log(localhostCrt);
console.log(localhostKey);
// 使用示例
await uploadFileToGitHubRepo(
'IdeaNest-org',
'localhost-cert',
'root.crt',
'main',
fs.readFileSync('root.crt', 'utf-8')
);
await uploadFileToGitHubRepo(
'IdeaNest-org',
'localhost-cert',
'localhost.crt',
'main',
fs.readFileSync('localhost.crt', 'utf-8')
);
await uploadFileToGitHubRepo(
'IdeaNest-org',
'localhost-cert',
'localhost.key',
'main',
fs.readFileSync('localhost.key', 'utf-8')
);
};