tls: server: Remove 'options' object and use monitor properties directly

This commit is contained in:
Martin Rubli 2024-05-31 23:02:33 +02:00
parent 7ea6d9c293
commit e03134c073

View file

@ -10,29 +10,22 @@ class TlsMonitorType extends MonitorType {
* @inheritdoc * @inheritdoc
*/ */
async check(monitor, heartbeat, _server) { async check(monitor, heartbeat, _server) {
const options = {
hostname: monitor.hostname,
port: monitor.port,
useStartTls: monitor.tcpStartTls || false,
request: monitor.tcpRequest || null,
interval: monitor.interval || 30,
};
const abortController = new AbortController(); const abortController = new AbortController();
const timeoutMs = options.interval * 1000 * 0.8; const timeoutMs = (monitor.interval || 30) * 1000 * 0.8;
const timeoutID = setTimeout(() => { const timeoutID = setTimeout(() => {
this.log(`timeout after ${timeoutMs} ms`); this.log(`timeout after ${timeoutMs} ms`);
abortController.abort(); abortController.abort();
}, timeoutMs); }, timeoutMs);
const tlsSocket = await this.connect(abortController.signal, options.hostname, options.port, options.useStartTls); const tlsSocket = await this.connect(abortController.signal, monitor.hostname, monitor.port, monitor.tcpStartTls);
let tlsSocketClosed = false; let tlsSocketClosed = false;
tlsSocket.on("close", () => { tlsSocket.on("close", () => {
tlsSocketClosed = true; tlsSocketClosed = true;
}); });
const result = await this.getResponseFromTlsPort(abortController.signal, tlsSocket, options) const request = monitor.tcpRequest || null;
const result = await this.getResponseFromTlsPort(abortController.signal, tlsSocket, request)
.then((response) => { .then((response) => {
clearTimeout(timeoutID); clearTimeout(timeoutID);
return response; return response;
@ -92,13 +85,13 @@ class TlsMonitorType extends MonitorType {
* Sends the request over the given TLS socket and returns the response. * Sends the request over the given TLS socket and returns the response.
* @param {AbortController} aborter Abort controller used to abort the request * @param {AbortController} aborter Abort controller used to abort the request
* @param {tls.TLSSocket} tlsSocket TLS socket instance * @param {tls.TLSSocket} tlsSocket TLS socket instance
* @param {*} options Monitor options * @param {string} request Request string (optional)
* @returns {Promise<string>} Server response on success or rejected promise on error * @returns {Promise<string>} Server response on success or rejected promise on error
*/ */
async getResponseFromTlsPort(aborter, tlsSocket, options) { async getResponseFromTlsPort(aborter, tlsSocket, request) {
if (options.request) { if (request) {
this.debug_log(`sending request: '${options.request}'`); this.debug_log(`sending request: '${request}'`);
tlsSocket.write(options.request); tlsSocket.write(request);
} }
return await this.readData(aborter, tlsSocket); return await this.readData(aborter, tlsSocket);