-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added https development server support. (#161)
* Added https development server support. * From let to const cachedKey & cachedCert variable. * Updated logger string to display correct url while serving with --https option.
- Loading branch information
1 parent
a2aa64a
commit 90b9684
Showing
10 changed files
with
155 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
{ | ||
"extends": "eslint:recommended", | ||
"parserOptions": { | ||
"ecmaVersion": 8 | ||
"ecmaVersion": 8 | ||
}, | ||
"env": { | ||
"node": true, | ||
"es6": true | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,4 @@ | |
"rules": { | ||
"no-console": 0 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
const forge = require('node-forge'); | ||
const fs = require('fs'); | ||
|
||
const PRIVATE_KEY_PATH = './.cache/private.pem'; | ||
const PRIMARY_KEY_PATH = './.cache/primary.crt'; | ||
|
||
function generateCertificate() { | ||
const cachedKey = | ||
fs.existsSync(PRIVATE_KEY_PATH) && fs.readFileSync(PRIVATE_KEY_PATH); | ||
const cachedCert = | ||
fs.existsSync(PRIMARY_KEY_PATH) && fs.readFileSync(PRIMARY_KEY_PATH); | ||
|
||
if (cachedKey && cachedCert) | ||
return { | ||
key: cachedKey, | ||
cert: cachedCert | ||
}; | ||
|
||
console.log('Generating SSL Certificate...'); | ||
|
||
const pki = forge.pki; | ||
const keys = pki.rsa.generateKeyPair(2048); | ||
const cert = 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() + 1); | ||
|
||
const attrs = [ | ||
{ | ||
name: 'commonName', | ||
value: 'parceljs.org' | ||
}, | ||
{ | ||
name: 'countryName', | ||
value: 'US' | ||
}, | ||
{ | ||
shortName: 'ST', | ||
value: 'Virginia' | ||
}, | ||
{ | ||
name: 'localityName', | ||
value: 'Blacksburg' | ||
}, | ||
{ | ||
name: 'organizationName', | ||
value: 'parcelBundler' | ||
}, | ||
{ | ||
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: 'extKeyUsage', | ||
serverAuth: true, | ||
clientAuth: true, | ||
codeSigning: true, | ||
emailProtection: true, | ||
timeStamping: true | ||
}, | ||
{ | ||
name: 'nsCertType', | ||
client: true, | ||
server: true, | ||
email: true, | ||
objsign: true, | ||
sslCA: true, | ||
emailCA: true, | ||
objCA: true | ||
}, | ||
{ | ||
name: 'subjectAltName', | ||
altNames: [ | ||
{ | ||
type: 6, // URI | ||
value: 'http://example.org/webid#me' | ||
}, | ||
{ | ||
type: 7, // IP | ||
ip: '127.0.0.1' | ||
} | ||
] | ||
}, | ||
{ | ||
name: 'subjectKeyIdentifier' | ||
} | ||
]); | ||
|
||
cert.sign(keys.privateKey, forge.md.sha256.create()); | ||
|
||
const privPem = pki.privateKeyToPem(keys.privateKey); | ||
const certPem = pki.certificateToPem(cert); | ||
|
||
fs.writeFileSync(PRIVATE_KEY_PATH, privPem); | ||
fs.writeFileSync(PRIMARY_KEY_PATH, certPem); | ||
|
||
return { | ||
key: privPem, | ||
cert: certPem | ||
}; | ||
} | ||
|
||
module.exports = generateCertificate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,4 @@ | |
"env": { | ||
"mocha": true | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters