resolve self signed issue

This commit is contained in:
ibukuntraive
2021-04-14 00:12:48 +01:00
parent 8fcdf79182
commit 0bc0d4dc0c
4 changed files with 109 additions and 82 deletions

View File

@@ -4509,10 +4509,6 @@
"version": "1.1.3",
"license": "MIT"
},
"node_modules/react-router/node_modules/isarray": {
"version": "0.0.1",
"license": "MIT"
},
"node_modules/reduce-function-call": {
"version": "1.0.3",
"license": "MIT",
@@ -6888,10 +6884,6 @@
"url": "https://github.com/sponsors/wooorm"
}
},
"node_modules/unset-value/node_modules/isarray": {
"version": "1.0.0",
"license": "MIT"
},
"node_modules/assign-symbols": {
"version": "1.0.0",
"license": "MIT",
@@ -45363,9 +45355,6 @@
"hoist-non-react-statics": {
"version": "2.5.5"
},
"isarray": {
"version": "0.0.1"
},
"path-to-regexp": {
"version": "1.8.0",
"requires": {
@@ -48274,9 +48263,6 @@
},
"has-values": {
"version": "0.1.4"
},
"isarray": {
"version": "1.0.0"
}
}
},

View File

@@ -5169,7 +5169,8 @@
}
},
"ms": {
"version": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
}
}
@@ -5926,15 +5927,18 @@
},
"dependencies": {
"ansi-regex": {
"version": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz",
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz",
"integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8="
},
"camelcase": {
"version": "https://registry.npmjs.org/camelcase/-/camelcase-2.1.1.tgz",
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/camelcase/-/camelcase-2.1.1.tgz",
"integrity": "sha1-fB0W1nmhu+WcoCys7PsBHiAfWh8="
},
"cliui": {
"version": "https://registry.npmjs.org/cliui/-/cliui-3.2.0.tgz",
"version": "3.2.0",
"resolved": "https://registry.npmjs.org/cliui/-/cliui-3.2.0.tgz",
"integrity": "sha1-EgYBU3qRbSmUD5NNo7SNWFo5IT0=",
"requires": {
"string-width": "^1.0.1",
@@ -5943,7 +5947,8 @@
}
},
"is-fullwidth-code-point": {
"version": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-1.0.0.tgz",
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-1.0.0.tgz",
"integrity": "sha1-754xOG8DGn8NZDr4L95QxFfvAMs=",
"requires": {
"number-is-nan": "^1.0.0"
@@ -5958,7 +5963,8 @@
}
},
"string-width": {
"version": "https://registry.npmjs.org/string-width/-/string-width-1.0.2.tgz",
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/string-width/-/string-width-1.0.2.tgz",
"integrity": "sha1-EYvfW4zcUaKn5w0hHgfisLmxB9M=",
"requires": {
"code-point-at": "^1.0.0",
@@ -5967,14 +5973,16 @@
}
},
"strip-ansi": {
"version": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz",
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz",
"integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=",
"requires": {
"ansi-regex": "^2.0.0"
}
},
"wrap-ansi": {
"version": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-2.1.0.tgz",
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-2.1.0.tgz",
"integrity": "sha1-2Pw9KE3QV5T+hJc8rs3Rz4JP3YU=",
"requires": {
"string-width": "^1.0.1",
@@ -5982,7 +5990,8 @@
}
},
"y18n": {
"version": "https://registry.npmjs.org/y18n/-/y18n-3.2.2.tgz",
"version": "3.2.2",
"resolved": "https://registry.npmjs.org/y18n/-/y18n-3.2.2.tgz",
"integrity": "sha512-uGZHXkHnhF0XeeAPgnKfPv1bgKAYyVvmNL1xlKsPYZPaIHxGti2hHqvOCQv71XMsLxu1QjergkqogUnms5D3YQ=="
},
"yargs": {
@@ -7131,7 +7140,8 @@
}
},
"ms": {
"version": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=",
"dev": true
}
@@ -7343,7 +7353,8 @@
}
},
"color-convert": {
"version": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
"integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==",
"dev": true,
"requires": {
@@ -7351,7 +7362,8 @@
}
},
"color-name": {
"version": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
"dev": true
}

View File

@@ -66,24 +66,23 @@ module.exports = {
const pingfetch = async (url, method, body, headers) => {
const now = new Date().getTime();
let resp = null;
let res = null;
let sslCertificate, response, data;
let resp, res, response;
try {
let sslCertificate, data;
const urlObject = new URL(url);
const payload = {
method: method,
timeout: 120000,
};
if (headers && Object.keys(headers).length) {
payload.headers = headers;
}
if (body && Object.keys(body).length) {
payload.body = body;
}
try {
const urlObject = new URL(url);
const payload = {
method: method,
timeout: 120000,
agent: urlObject.protocol === 'https:' ? httpsAgent : httpAgent,
};
if (headers && Object.keys(headers).length) {
payload.headers = headers;
}
if (body && Object.keys(body).length) {
payload.body = body;
}
response = await fetch(url, payload);
response = await fetch(url, { ...payload });
res = new Date().getTime() - now;
data = await response.json();
if (urlObject.protocol === 'https:') {
@@ -98,20 +97,33 @@ const pingfetch = async (url, method, body, headers) => {
}
}
} catch (e) {
if (e.code === 'DEPTH_ZERO_SELF_SIGNED_CERT') {
response = { status: 200 };
sslCertificate = {
selfSigned: true,
};
} else {
throw e;
response = await fetch(url, {
...payload,
...(url.startsWith('https')
? { agent: httpsAgent }
: { agent: httpAgent }),
});
res = new Date().getTime() - now;
data = await response.json();
if (urlObject.protocol === 'https:') {
const certificate = await sslCert.get(urlObject.hostname);
if (certificate) {
sslCertificate = {
issuer: certificate.issuer,
expires: certificate.valid_to,
fingerprint: certificate.fingerprint,
selfSigned: e.code === 'DEPTH_ZERO_SELF_SIGNED_CERT',
};
}
}
}
resp = { status: response.status, body: data, sslCertificate };
} catch (error) {
res = new Date().getTime() - now;
resp = { status: 408, body: error };
}
return {
res,
resp,

View File

@@ -6,10 +6,11 @@ const sslCert = require('get-ssl-certificate');
const { fork } = require('child_process');
const moment = require('moment');
const https = require('https');
const http = require('http');
const httpsAgent = new https.Agent({
rejectUnauthorized: false,
});
const httpAgent = new http.Agent();
// it collects all monitors then ping them one by one to store their response
// checks if the website of the url in the monitors is up or down
// creates incident if a website is down and resolves it when they come back up
@@ -106,24 +107,19 @@ module.exports = {
const pingfetch = async url => {
const now = new Date().getTime();
let resp = null;
let rawResp = null;
let res = null;
let resp, res, response;
try {
let sslCertificate, response, data;
let sslCertificate, data;
const urlObject = new URL(url);
const headers = {
'User-Agent':
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36',
};
try {
response = await fetch(url, {
timeout: 120000,
...(url.startsWith('https') && { agent: httpsAgent }),
headers: {
'User-Agent':
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36',
},
});
response = await fetch(url, { timeout: 120000, headers });
res = new Date().getTime() - now;
data = await response.text();
const urlObject = new URL(url);
if (urlObject.protocol === 'https:') {
const certificate = await sslCert.get(urlObject.hostname);
if (certificate) {
@@ -136,33 +132,54 @@ const pingfetch = async url => {
}
}
} catch (e) {
if (e.code === 'DEPTH_ZERO_SELF_SIGNED_CERT') {
response = { status: 200 };
sslCertificate = {
selfSigned: true,
};
} else {
throw e;
response = await fetch(url, {
timeout: 120000,
...(url.startsWith('https')
? { agent: httpsAgent }
: { agent: httpAgent }),
headers,
});
res = new Date().getTime() - now;
data = await response.text();
if (urlObject.protocol === 'https:') {
const certificate = await sslCert.get(urlObject.hostname);
if (certificate) {
sslCertificate = {
issuer: certificate.issuer,
expires: certificate.valid_to,
fingerprint: certificate.fingerprint,
selfSigned: e.code === 'DEPTH_ZERO_SELF_SIGNED_CERT',
};
}
}
}
resp = {
status: response.status,
body: data,
sslCertificate,
};
rawResp = {
headers:
response && response.headers && response.headers.raw()
? response.headers.raw()
: null,
};
resp = { status: response.status, body: data, sslCertificate };
} catch (error) {
res = new Date().getTime() - now;
resp = { status: 408, body: error };
}
return { res, resp, rawResp };
return {
res,
resp,
rawResp: {
ok: response && response.ok ? response.ok : null,
status:
response && response.status
? response.status
: resp && resp.status
? resp.status
: null,
statusText:
response && response.statusText ? response.statusText : null,
headers:
response && response.headers && response.headers.raw()
? response.headers.raw()
: null,
body: resp && resp.body ? resp.body : null,
},
};
};
const lighthouseFetch = (monitor, url) => {