Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
Latest commit
64 lines (50 loc) · 2.44 KB
/
Copy pathindex.js
File metadata and controls
64 lines (50 loc) · 2.44 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
importosfrom'os'
importfsfrom'fs-extra'
importpathfrom'path'
importhttpsfrom'https'
importAutoEncryptfrom'@small-tech/auto-encrypt'
importAutoEncryptLocalhostfrom'@small-tech/auto-encrypt-localhost'
importlogfrom'./lib/util/log.js'
constAUTO_ENCRYPT_STAGING_SERVER_TYPE=1
// Only modify this instance of the https module with our own createServer method.
constsmallTechHttps=Object.assign({},https)
smallTechHttps.createServer=function(options,listener){
// The first parameter is optional. If omitted, listener should be passed as the first argument.
if(typeofoptions==='function'){
listener=options
options={}
}
constdefaultSettingsPath=path.join(os.homedir(),'.small-tech.org','https')
constserverScope=options.domains==undefined||options.domains.includes('localhost') ? 'local' : 'global'
constsettingsPath=options.settingsPath ? path.join(path.resolve(options.settingsPath),serverScope) : path.join(defaultSettingsPath,serverScope)
options.settingsPath=settingsPath
if(options.staging){options.serverType=AUTO_ENCRYPT_STAGING_SERVER_TYPE}
deleteoptions.staging
constlogMessage={
local: 'at localhost with locally-trusted certificates',
global: 'with globally-trusted Let’s Encrypt certificates'
}
constautoEncryptScope={
local: AutoEncryptLocalhost,
global: AutoEncrypt
}
log(` 🔒 ❨https❩ Creating server ${logMessage[serverScope]}.`)
constserver=autoEncryptScope[serverScope].https.createServer(options,listener)
if(serverScope==='global'){
// Migration: 1.1.0 and earlier to 1.2.0+:
// Remove the old Let’s Encrypt client’s certificate settings.
// (And thus force upgrade to certificates managed by Auto Encrypt on first hit of server.)
constoldLetsEncryptSettingsPathFor=directory=>path.join(settingsPath,directory)
fs.removeSync(oldLetsEncryptSettingsPathFor('accounts'))
fs.removeSync(oldLetsEncryptSettingsPathFor('archive'))
fs.removeSync(oldLetsEncryptSettingsPathFor('live'))
fs.removeSync(oldLetsEncryptSettingsPathFor('renewal'))
// Allow AutoEncrypt to perform clean-up (e.g., remove interval timer for renewal check, etc.)
server.on('close',()=>{
AutoEncrypt.shutdown()
})
}
log(' 🔒 ❨https❩ Created HTTPS server.')
returnserver
}
exportdefaultsmallTechHttps