|
@@ -10,6 +10,7 @@ import { Database, ServiceChangedStatus } from './database.class';
|
|
|
import { FCMController } from './fcm-controller.class';
|
|
import { FCMController } from './fcm-controller.class';
|
|
|
|
|
|
|
|
type Subscriber = { id: number; interval: number; conf: HttpCheckConfig };
|
|
type Subscriber = { id: number; interval: number; conf: HttpCheckConfig };
|
|
|
|
|
+type ContentCheckError = { type: 'contentCheck'; status: HttpCheckStatus; message: string };
|
|
|
|
|
|
|
|
export class HttpCheckController {
|
|
export class HttpCheckController {
|
|
|
private subscriptions: Array<Subscriber> = [];
|
|
private subscriptions: Array<Subscriber> = [];
|
|
@@ -104,15 +105,11 @@ export class HttpCheckController {
|
|
|
options.timeout = conf.timeout;
|
|
options.timeout = conf.timeout;
|
|
|
let response = await axios.get(conf.url, options);
|
|
let response = await axios.get(conf.url, options);
|
|
|
const responseText = new String(response.data).toString();
|
|
const responseText = new String(response.data).toString();
|
|
|
|
|
+ const errors = this.recurseDisjunctChecks(conf.checks, responseText);
|
|
|
|
|
|
|
|
- for (const check of conf.checks) {
|
|
|
|
|
- // TODO: new CheckDisjunction app logic
|
|
|
|
|
- // const reg = new RegExp(check, 'i');
|
|
|
|
|
- // if (!reg.test(responseText)) {
|
|
|
|
|
- // Logger.debug(`[DEBUG] Regular expression /${check}/i not found in response`);
|
|
|
|
|
- // await this.db.insertHealthCheckData(conf.id, now, HttpCheckStatus.CheckFailed, `Regular expression /${check}/i not found in response`);
|
|
|
|
|
- // success = false;
|
|
|
|
|
- // }
|
|
|
|
|
|
|
+ for (const error of errors) {
|
|
|
|
|
+ await this.db.insertHealthCheckData(conf.id, now, error.status, error.message);
|
|
|
|
|
+ success = false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (success) {
|
|
if (success) {
|
|
@@ -181,6 +178,61 @@ export class HttpCheckController {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private recurseDisjunctChecks(checks: CheckDisjunction, responseText: string): ContentCheckError[] {
|
|
|
|
|
+ const errorBuffer: ContentCheckError[] = [];
|
|
|
|
|
+ for (const check of checks) {
|
|
|
|
|
+ const errors: ContentCheckError[] = [];
|
|
|
|
|
+ if (typeof check === 'string') {
|
|
|
|
|
+ try {
|
|
|
|
|
+ this.doCheck(check, responseText);
|
|
|
|
|
+ } catch (error: any) {
|
|
|
|
|
+ if (error.type === 'contentCheck') {
|
|
|
|
|
+ errors.push(error as ContentCheckError);
|
|
|
|
|
+ } else throw error;
|
|
|
|
|
+ }
|
|
|
|
|
+ } else if (Array.isArray(check)) {
|
|
|
|
|
+ errors.push(...this.recurseDisjunctChecks(check, responseText));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ errors.push(...this.recurseConjunctChecks(check, responseText));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (errors.length) {
|
|
|
|
|
+ errorBuffer.push(...errors);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return [];
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return errorBuffer;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private recurseConjunctChecks(check: CheckConjunction, responseText: string): ContentCheckError[] {
|
|
|
|
|
+ const errorBuffer: ContentCheckError[] = [];
|
|
|
|
|
+ for (const con of check.and) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (typeof con === 'string') {
|
|
|
|
|
+ this.doCheck(con, responseText);
|
|
|
|
|
+ } else if (Array.isArray(con)) {
|
|
|
|
|
+ errorBuffer.push(...this.recurseDisjunctChecks(con, responseText));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ errorBuffer.push(...this.recurseConjunctChecks(con, responseText));
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (error: any) {
|
|
|
|
|
+ if (error.type === 'contentCheck') {
|
|
|
|
|
+ errorBuffer.push(error as ContentCheckError);
|
|
|
|
|
+ } else throw error;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return errorBuffer;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private async doCheck(check: string, responseText: string) {
|
|
|
|
|
+ const reg = new RegExp(check, 'i');
|
|
|
|
|
+ if (!reg.test(responseText)) {
|
|
|
|
|
+ Logger.debug(`[DEBUG] Regular expression /${check}/i not found in response`);
|
|
|
|
|
+ throw { type: 'contentCheck', status: HttpCheckStatus.CheckFailed, message: `Regular expression /${check}/i not found in response` };
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
async close() {
|
|
async close() {
|
|
|
if (!this.db) return;
|
|
if (!this.db) return;
|
|
|
await this.db.close();
|
|
await this.db.close();
|