- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample.js
More file actions
Latest commit
106 lines (82 loc) Β· 3.08 KB
/
Copy pathexample.js
File metadata and controls
106 lines (82 loc) Β· 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
constcertnode=require('./lib')
constfs=require('fs')
consthttps=require('https')
constpath=require('path')
const{
dirname =path.join(__dirname,'private'),
domain,
email,
passphrase =''
}=process.env
if(!domain){
console.error('Must specify "domain"')
process.exit(1)
}
if(!email){
console.error('Must specify "email"')
process.exit(1)
}
constaccountDir=path.join(dirname,'account')
constdomainDir=path.join(dirname,domain)
constcertificateFile=path.join(domainDir,'certificate.pem')
constprivateKeyFile=path.join(domainDir,'privateKey.pem')
constmain=async()=>{
awaitPromise.all([
fs.promises.mkdir(accountDir,{recursive: true}).catch(()=>{}),
fs.promises.mkdir(domainDir,{recursive: true}).catch(()=>{})
])
constclient=newcertnode.Client()
// Generate fresh account keys for Let's Encrypt
awaitclient.generateAccountKeyPair()
console.log('Generated account keys')
{
// Generate privateKey and certificate for `domain` with `email` address.
// Then, initialize HTTPS server with the credentials.
const{ certificate, privateKeyData }=awaitclient.generateCertificate(domain,email)
console.log('Generated certificate and private key')
constserver=https.createServer({cert: certificate,key: privateKeyData})
/* register event listeners */
server.listen(443,'0.0.0.0',()=>{
console.log('Started server with credentials')
server.close()
})
// Export the account keys and write them to files in a directory.
// Account private key is encrypted with passphrase, if provided.
awaitclient.exportAccountKeyPair(accountDir,passphrase)
console.log('Exported account keys')
// Export private key and write it + certificate to filesystem.
// Certificate private key is encrypted with passphrase, if provided.
awaitPromise.all([
fs.promises.writeFile(certificateFile,certificate),
certnode.writeKeyToFile(privateKeyFile,privateKeyData,passphrase)
])
console.log('Exported certificate and private key')
}
// Later: create another client for same account
constanotherClient=newcertnode.Client()
// If you previously exported with passphrase, provide the same passphrase.
awaitanotherClient.importAccountKeyPair(accountDir,passphrase)
console.log('Imported account keys')
/* generate certificate with `anotherClient` */
// Later: import private key and certificate and initialize HTTPS server with them.
const[certificate,privateKeyData]=awaitPromise.all([
fs.promises.readFile(certificateFile,'utf8'),
fs.promises.readFile(privateKeyFile,'utf8')
])
console.log('Exported certificate and private key')
// If you previously exported with passphrase, provide the same passphrase.
constserver=https.createServer({
cert: certificate,
key: privateKeyData,
passphrase
})
/* register event listeners */
server.listen(443,'0.0.0.0',()=>{
console.log('Started server with credentials')
server.close()
})
}
main().catch(err=>{
console.error(err)
process.exit(1)
})