Skip to content

Commit 388fff8

Browse files
committed
Fixes for the server reachability test.
- Do not apply HTTPs redirection for challenge used by the test. - Set the `User-Agent` to avoid 403 answer from site24x7.com. - Handle JSON parsing failure of the received body. - Better handling of different error cases.
1 parent e08a4d4 commit 388fff8

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

backend/internal/certificate.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -1167,6 +1167,7 @@ const internalCertificate = {
11671167
const options = {
11681168
method: 'POST',
11691169
headers: {
1170+
'User-Agent': 'Mozilla/5.0',
11701171
'Content-Type': 'application/x-www-form-urlencoded',
11711172
'Content-Length': Buffer.byteLength(formBody)
11721173
}
@@ -1179,12 +1180,22 @@ const internalCertificate = {
11791180

11801181
res.on('data', (chunk) => responseBody = responseBody + chunk);
11811182
res.on('end', function () {
1182-
const parsedBody = JSON.parse(responseBody + '');
1183-
if (res.statusCode !== 200) {
1184-
logger.warn(`Failed to test HTTP challenge for domain ${domain}`, res);
1183+
try {
1184+
const parsedBody = JSON.parse(responseBody + '');
1185+
if (res.statusCode !== 200) {
1186+
logger.warn(`Failed to test HTTP challenge for domain ${domain} because HTTP status code ${res.statusCode} was returned: ${parsedBody.message}`);
1187+
resolve(undefined);
1188+
} else {
1189+
resolve(parsedBody);
1190+
}
1191+
} catch (err) {
1192+
if (res.statusCode !== 200) {
1193+
logger.warn(`Failed to test HTTP challenge for domain ${domain} because HTTP status code ${res.statusCode} was returned`);
1194+
} else {
1195+
logger.warn(`Failed to test HTTP challenge for domain ${domain} because response failed to be parsed: ${err.message}`);
1196+
}
11851197
resolve(undefined);
11861198
}
1187-
resolve(parsedBody);
11881199
});
11891200
});
11901201

@@ -1198,6 +1209,9 @@ const internalCertificate = {
11981209
if (!result) {
11991210
// Some error occurred while trying to get the data
12001211
return 'failed';
1212+
} else if (result.error) {
1213+
logger.info(`HTTP challenge test failed for domain ${domain} because error was returned: ${result.error.msg}`);
1214+
return `other:${result.error.msg}`;
12011215
} else if (`${result.responsecode}` === '200' && result.htmlresponse === 'Success') {
12021216
// Server exists and has responded with the correct data
12031217
return 'ok';
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
set $test "";
12
if ($scheme = "http") {
3+
set $test "H";
4+
}
5+
if ($request_uri = /.well-known/acme-challenge/test-challenge) {
6+
set $test "${test}T";
7+
}
8+
if ($test = H) {
29
return 301 https://$host$request_uri;
310
}

0 commit comments

Comments
 (0)