-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c827139
commit a4b3287
Showing
6 changed files
with
153 additions
and
7 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
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,15 +1,41 @@ | ||
module.exports = function() { | ||
module.exports = function(site) { | ||
const fs = require('fs-extra'); | ||
const liveServer = require('live-server'); | ||
const { BUILD } = require('../utils/constants'); | ||
const { BUILD, CERT_FILE, KEY_FILE } = require('../utils/constants'); | ||
|
||
const params = { | ||
let config = site.getConfig(); | ||
let options = Object.assign({ | ||
port: 8181, | ||
host: '0.0.0.0', | ||
host: '127.0.0.1', | ||
root: BUILD, | ||
open: true, | ||
wait: 50, | ||
logLevel: 0 | ||
}; | ||
}, config.server); | ||
|
||
liveServer.start(params); | ||
let ssl = config.server && config.server.ssl; | ||
if (ssl) { | ||
let httpsOptions = ssl; | ||
|
||
if (ssl.key && ssl.cert) { | ||
httpsOptions = { | ||
key: fs.readFileSync(ssl.key), | ||
cert: fs.readFileSync(ssl.cert) | ||
}; | ||
} else { | ||
require('../utils/generateSslCert')(); | ||
|
||
httpsOptions = { | ||
cert: fs.readFileSync(CERT_FILE), | ||
key: fs.readFileSync(KEY_FILE) | ||
}; | ||
} | ||
|
||
options = { | ||
...options, | ||
https: httpsOptions | ||
}; | ||
} | ||
|
||
liveServer.start(options); | ||
} |
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,101 @@ | ||
module.exports = function() { | ||
const fs = require('fs-extra'); | ||
|
||
const { CERT_FILE, KEY_FILE } = require('./constants'); | ||
if (fs.existsSync(KEY_FILE) && fs.existsSync(CERT_FILE)) { | ||
return; | ||
} | ||
|
||
fs.mkdirSync('tmp'); | ||
|
||
const forge = require('node-forge') | ||
const pki = forge.pki; | ||
|
||
let keys = pki.rsa.generateKeyPair(2048); | ||
let cert = pki.createCertificate(); | ||
|
||
cert.publicKey = keys.publicKey; | ||
|
||
// NOTE: serialNumber is the hex encoded value of an ASN.1 INTEGER. | ||
// Conforming CAs should ensure serialNumber is: | ||
// - no more than 20 octets | ||
// - non-negative (prefix a '00' if your value starts with a '1' bit) | ||
|
||
// set serialNumber to a unique value ~ timestamp | ||
cert.serialNumber = Date.now().toString(); | ||
cert.validity.notBefore = new Date(); | ||
cert.validity.notAfter = new Date(); | ||
cert.validity.notAfter.setFullYear(cert.validity.notBefore.getFullYear() + 1); | ||
|
||
let attrs = [{ | ||
name: 'commonName', | ||
value: 'sivasubramanyam.me' | ||
}, { | ||
name: 'countryName', | ||
value: 'IN' | ||
}, { | ||
shortName: 'ST', | ||
value: 'TN' | ||
}, { | ||
name: 'localityName', | ||
value: 'legoland' | ||
}, { | ||
name: 'organizationName', | ||
value: '@astronomersiva/lego' | ||
}, { | ||
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' | ||
}]); | ||
|
||
// self-sign certificate | ||
cert.sign(keys.privateKey, forge.md.sha256.create()); | ||
|
||
// convert a Forge certificate to PEM | ||
let crt = pki.certificateToPem(cert); | ||
let key = pki.privateKeyToPem(keys.privateKey); | ||
|
||
fs.writeFileSync(KEY_FILE, key); | ||
fs.writeFileSync(CERT_FILE, crt); | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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