-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhook-http.js
98 lines (78 loc) · 2.71 KB
/
hook-http.js
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
'use strict';
// Require fs and async for file processing
const fs = require('fs-extra');
const async = require('async');
// Require the base Hook class
const Hook = require('./hook');
/**
* HookHttp class
* Dehydrated hook for 'http-01' challenge type
*/
class HookHttp extends Hook {
/**
* Constructor - Build a HookHttp object
*/
constructor() {
// Build the parent Hook() class first
super();
// Build directory variables as properties of this instance
this.domainDir = `${this.settings.challenge_webroot}/${this.domain}`;
this.wellKnownDir = `${this.domainDir}/.well-known`;
this.challengeDir = `${this.wellKnownDir}/acme-challenge`;
this.challengeFile = `${this.challengeDir}/${this.token}`;
}
/**
* Deploy challenge as file to be accessed via HTTP
*/
deployChallenge() {
this.debug('Deploying challenge...');
// Verify or create the challenge directory (for this domain)
fs.ensureDir(this.challengeDir, err => {
// Show & throw error if something went wrong
if (err) {
this.error(err)
}
// Look through each directory (no recursive way to do this?) to make sure it's executable for web server to see
async.each([ this.domainDir, this.wellKnownDir, this.challengeDir ], (dir, next) => {
// Change each directory mode to 755 (rwx r-x r-x), needs to be executable to be viewable
fs.chmod(dir, 0o755, next);
}, err => {
// Show & throw error if something went wrong
if (err) {
this.error(err)
}
// Show debug msg that permissions were successfully updated
this.debug('Updated permissions')
});
// Write the challenge string to the challenge file
fs.writeFile(this.challengeFile, this.challenge, { flag: 'w' }, err => {
// Show & throw error if something went wrong
if (err) {
this.error(err);
}
// Verify permissions of this file (only need read, not execute on this file) (644 = rw- r-- r--)
fs.chmodSync(this.challengeFile, 0o644);
// Debug messages when completed
this.debug(`Saved challenge to file: ${this.challengeFile}`);
this.debug('Challenge deployed.');
});
});
}
/**
* Clean challenge from the filesystem
*/
cleanChallenge() {
this.debug('Cleaning challenge...');
// Delete the entire domain directory for this challenge
fs.remove(this.domainDir, err => {
// Show & throw error if something went wrong
if (err) {
this.error(err);
}
// Debug message when completed
this.debug('Challenge cleaned.');
});
}
}
// Export the HookHttp class
module.exports = HookHttp;