From 68ff472e7dd9f7b6290963b0f64f8e7a6c325fe0 Mon Sep 17 00:00:00 2001 From: cgeek Date: Sun, 20 Sep 2020 13:21:14 +0200 Subject: [PATCH 1/4] mailing: dynamic title --- src/lib/mail.ts | 21 ++++++++++++++------- src/lib/watchers/abstract/url-watcher.ts | 6 +++--- src/lib/watchers/webdiff/webdiff-watcher.ts | 4 ++-- src/lib/watchers/ws2p/ws2p-watcher.ts | 6 +++--- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/lib/mail.ts b/src/lib/mail.ts index 739ba2b..e323910 100644 --- a/src/lib/mail.ts +++ b/src/lib/mail.ts @@ -110,17 +110,22 @@ async function sendEmail(subject: string, html: string, cc?: string) { export const mail = { - onEstablished: (target: string, message = `Connection established for ${target}`, getHtml: () => string = () => ` + onEstablished: (target: string, + message: () => string|undefined = () => `Connection established for ${target}`, + getHtml: () => string = () => `

Connection from [${os.hostname}] to ${target} established on ${moment().format('DD-MM-YYYY HH:mm:ss')}.

`) => { return async (cc?: string) => { - await queueEmail(`[dw] [${os.hostname}] ${message}`, getHtml(), cc) + await queueEmail(`[dw] [${os.hostname}] ${message()}`, getHtml(), cc) } }, - onDisconnect: (target: string, message = `Connection closed for ${target}`, getErrorMessage: () => string = () => '', getHtml: (waitingDelay: number, recallDelay: number) => string = (waitingDelay: number, recallDelay: number) => ` + onDisconnect: (target: string, + message: () => string|undefined = () => `Connection closed for ${target}`, + getErrorMessage: () => string = () => '', + getHtml: (waitingDelay: number, recallDelay: number) => string = (waitingDelay: number, recallDelay: number) => `

Connection from [${os.hostname}] to ${target} was lost on ${moment().format('dd-MM-YYYY HH:mm:ss')}.

@@ -131,18 +136,20 @@ export const mail = { `) => { return async (waitingDelay: number, recallDelay: number, cc?: string) => { console.log('Waiting %s seconds...', (waitingDelay / 1000).toFixed(0)) - await queueEmail(`[dw] [${os.hostname}] ${message}`, getHtml(waitingDelay, recallDelay), cc) + await queueEmail(`[dw] [${os.hostname}] ${message()}`, getHtml(waitingDelay, recallDelay), cc) } }, - onRestartSuccess: (target: string, message = `Connection recovered for ${target}`, getHtml: () => string = () => ` + onRestartSuccess: (target: string, + message: () => string|undefined = () => `Connection recovered for ${target}`, + getHtml: () => string = () => `

Connection from [${os.hostname}] to ${target} was recovered on ${moment().format('dd-MM-YYYY HH:mm:ss')}.

`) => { return async (cc?: string) => { - console.log(`${message}`) - await queueEmail(`[dw] [${os.hostname}] ${message}`, getHtml(), cc) + console.log(`${message()}`) + await queueEmail(`[dw] [${os.hostname}] ${message()}`, getHtml(), cc) } }, } diff --git a/src/lib/watchers/abstract/url-watcher.ts b/src/lib/watchers/abstract/url-watcher.ts index ea12d00..195d01a 100644 --- a/src/lib/watchers/abstract/url-watcher.ts +++ b/src/lib/watchers/abstract/url-watcher.ts @@ -47,7 +47,7 @@ export function urlWatcher(conf: Conf, checkValidity: (data: any) => Promise getOkTitle()), // When a disconnection is detected (waitingDelay: number, recallDelay, error?: any) => { @@ -57,14 +57,14 @@ export function urlWatcher(conf: Conf, checkValidity: (data: any) => Promise `

${error.errorMessage}

` } - return mail.onDisconnect(urlConf.address, koTitle, koMessage)(waitingDelay, recallDelay) + return mail.onDisconnect(urlConf.address, () => koTitle, koMessage)(waitingDelay, recallDelay) }, async () => { console.log('Trying to connect to %s', urlConf.address) }, - mail.onRestartSuccess(urlConf.address, getRecoveredTitle()), + mail.onRestartSuccess(urlConf.address, () => getRecoveredTitle()), ) } diff --git a/src/lib/watchers/webdiff/webdiff-watcher.ts b/src/lib/watchers/webdiff/webdiff-watcher.ts index 6e7d84b..6337cb5 100644 --- a/src/lib/watchers/webdiff/webdiff-watcher.ts +++ b/src/lib/watchers/webdiff/webdiff-watcher.ts @@ -61,10 +61,10 @@ export function webDiffWatcher(conf: Conf) { conf.waitingDelay, conf.recallDelay, - () => mail.onEstablished(target, 'webdiff successfully started')(webDiffConf.cc), + () => mail.onEstablished(target, () => 'webdiff successfully started')(webDiffConf.cc), // When a disconnection is detected - (waitingDelay: number, recallDelay: number) => mail.onDisconnect(target, 'Diff detected', undefined, (waitingDelay: number) => ` + (waitingDelay: number, recallDelay: number) => mail.onDisconnect(target, () => 'Diff detected', undefined, (waitingDelay: number) => ` ${htmlDiff}

Waiting ${(waitingDelay / 1000).toFixed(0)} seconds before trying to reconnect. diff --git a/src/lib/watchers/ws2p/ws2p-watcher.ts b/src/lib/watchers/ws2p/ws2p-watcher.ts index aa156cd..c3b4aca 100644 --- a/src/lib/watchers/ws2p/ws2p-watcher.ts +++ b/src/lib/watchers/ws2p/ws2p-watcher.ts @@ -44,16 +44,16 @@ export function ws2pWatcher(conf: Conf) { conf.waitingDelay, conf.recallDelay, - mail.onEstablished(target, `State OK WS2P on ${wserver.address}`), + mail.onEstablished(target, () => `State OK WS2P on ${wserver.address}`), // When a disconnection is detected - mail.onDisconnect(target, `State FAILURE WS2P on ${wserver.address}`), + mail.onDisconnect(target, () => `State FAILURE WS2P on ${wserver.address}`), async () => { console.log('Trying to connect to %s', target) }, - mail.onRestartSuccess(target, `State RECOVERED WS2P on ${wserver.address}`), + mail.onRestartSuccess(target, () => `State RECOVERED WS2P on ${wserver.address}`), ) } From 49719dcad22d7b5d991a1816d8b75a78ecaacc2d Mon Sep 17 00:00:00 2001 From: cgeek Date: Sun, 20 Sep 2020 13:30:53 +0200 Subject: [PATCH 2/4] mailing: urlwatcher's body's message --- src/lib/watchers/abstract/url-watcher.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/watchers/abstract/url-watcher.ts b/src/lib/watchers/abstract/url-watcher.ts index 195d01a..49ac684 100644 --- a/src/lib/watchers/abstract/url-watcher.ts +++ b/src/lib/watchers/abstract/url-watcher.ts @@ -17,7 +17,7 @@ export function urlWatcher(conf: Conf, checkValidity: (data: any) => Promise Date: Sun, 20 Sep 2020 13:31:17 +0200 Subject: [PATCH 3/4] mailing: see subject --- src/lib/mail.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/mail.ts b/src/lib/mail.ts index e323910..269331e 100644 --- a/src/lib/mail.ts +++ b/src/lib/mail.ts @@ -23,7 +23,7 @@ export function initConfMail(confMail: ConfMail) { export async function queueEmail(subject: string, html: string, cc?: string) { queue.push({ subject, body: html, cc }) - console.log(`[mail] added 1 mail to queue`) + console.log(`[mail] added 1 mail to queue: ${subject}`) } function consumeMessages(cc: string): MailContent[] { From 641a2db322677b9faaf2b25d5090bd6db1eb5464 Mon Sep 17 00:00:00 2001 From: cgeek Date: Sun, 20 Sep 2020 13:32:29 +0200 Subject: [PATCH 4/4] membership watcher --- app.yml | 12 +++++++ src/lib/dwatch.ts | 2 ++ src/lib/types/conf.ts | 8 +++++ src/lib/watchers/bma/membership-watcher.ts | 41 ++++++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 src/lib/watchers/bma/membership-watcher.ts diff --git a/app.yml b/app.yml index 9da37b8..d3b7da9 100644 --- a/app.yml +++ b/app.yml @@ -36,6 +36,18 @@ wwMeta: # frequency: 60000 # 1' # maxLate: 14400 # 4h +memberships: + # Tous les 1j ==> 3 mois de prévenance +# - {address: https://g1.cgeek.fr, currency: ğ1, frequency: 86400, mustRemain: 7776000, pubkey: 2ny7YAdmzReQxAayyJZsyVYwYhVyax2thKcGknmQy5nQ, memberAlias: cgeek} +# - {address: https://g1.cgeek.fr, currency: ğ1, frequency: 86400, mustRemain: 7776000, pubkey: 4GdKJq2LqV1rrCkixUoSpg4w5Abz41knU4h9eov2R3QU, memberAlias: Audrey35} + # Tous les 1j ==> 1 mois de prévenance + - {address: https://g1-test.cgeek.fr, currency: ğtest, frequency: 86400, mustRemain: 2592000, pubkey: 3dnbnYY9i2bHMQUGyFp5GVvJ2wBkVpus31cDJA5cfRpj, memberAlias: cgeek} +# - {address: https://g1-test.cgeek.fr, currency: ğtest, frequency: 86400, mustRemain: 2592000, pubkey: 39YyHCMQNmXY7NkPCXXfzpV1vYct4GBxwgfyd4d72HmB, memberAlias: cgeek-4} +# - {address: https://g1-test.cgeek.fr, currency: ğtest, frequency: 86400, mustRemain: 7776000, pubkey: 36UhAqrkDx11ifN7WaBM6Q5bMUJxhKb1wJnnPFnkLkCF, memberAlias: cgeek-2} # Compte révoqué +# - {address: https://g1-test.cgeek.fr, currency: ğtest, frequency: 86400, mustRemain: 7776000, pubkey: 81jPYhcyruwKJ9Dy4Vz7MtmxiSdeESuJcvjPotxbCTgS, memberAlias: cgeek-3} # Compte révoqué +# - {address: https://g1-test.cgeek.fr, currency: ğtest, frequency: 86400, mustRemain: 7776000, pubkey: 78Tus1ajGnztK6FW7suYsprWFZUkiiG1bakuMNsHooWo, memberAlias: cgeek-dev} # Compte révoqué +# - {address: https://g1-test.cgeek.fr, currency: ğtest, frequency: 86400, mustRemain: 7776000, pubkey: 4Ec3yqwfCxJM2pB3jCGrbUSvxqDGxasQcBCxCyA81Nwh, memberAlias: cgeek-test-revocation} # Compte révoqué + mail: enabled: false frequency: 3000 # 10" diff --git a/src/lib/dwatch.ts b/src/lib/dwatch.ts index 6efb535..1b1ffea 100644 --- a/src/lib/dwatch.ts +++ b/src/lib/dwatch.ts @@ -9,6 +9,7 @@ import {Watcher} from "./types/state"; import {headWatcher} from "./watchers/bma/head-watcher"; import {jsonWatcher} from "./watchers/wotwizard/json-watcher"; import {initConfMail} from './mail' +import {membershipWatcher} from './watchers/bma/membership-watcher' export async function dwatch(confFile: string) { @@ -22,5 +23,6 @@ export async function dwatch(confFile: string) { (await Promise.all((conf.webDiffServers || []).map(webDiffWatcher(conf)))).forEach(w => watchers.push(w)); (await Promise.all((conf.headServers || []).map(headWatcher(conf)))).forEach(w => watchers.push(w)); (await Promise.all((conf.wwMeta || []).map(jsonWatcher(conf)))).forEach(w => watchers.push(w)); + (await Promise.all((conf.memberships || []).map(membershipWatcher(conf)))).forEach(w => watchers.push(w)); return watchers } diff --git a/src/lib/types/conf.ts b/src/lib/types/conf.ts index ef1c7e5..e03e83f 100644 --- a/src/lib/types/conf.ts +++ b/src/lib/types/conf.ts @@ -8,6 +8,7 @@ export interface Conf { webDiffServers: ConfWebDiff[] headServers: ConfHead[] wwMeta: ConfWWMeta[] + memberships: ConfMembership[] mail: ConfMail } @@ -36,6 +37,13 @@ export interface ConfBMA extends ConfURL { maxLate?: number } +export interface ConfMembership extends ConfURL { + pubkey: string + memberAlias: string + currency: string + mustRemain: number +} + export interface ConfHead extends ConfURL { observedPubkey: string maxLateBlocks: number diff --git a/src/lib/watchers/bma/membership-watcher.ts b/src/lib/watchers/bma/membership-watcher.ts new file mode 100644 index 0000000..5fe640b --- /dev/null +++ b/src/lib/watchers/bma/membership-watcher.ts @@ -0,0 +1,41 @@ +import {Conf, ConfMembership} from "../../types/conf"; +import {urlWatcher, UrlWatcherResult} from '../abstract/url-watcher' + +export function membershipWatcher(conf: Conf) { + + const URL_PATH = '/wot/requirements/' + + return async (confMS: ConfMembership) => { + + function getSubjectTitle(state: string) { + return `State ${state} MS of ${confMS.memberAlias} (${confMS.currency})` + } + + let state = 'INIT' + + return urlWatcher(conf, async (data) => { + const json = data as { identities: {membershipExpiresIn: number}[] } + // On se base sur la clé publique donc a priori une seule identité OK + const idty = json.identities[0] + if (idty.membershipExpiresIn < confMS.mustRemain) { + if (idty.membershipExpiresIn > 0) { + state = 'WARNING' + const remainingDays = Math.floor(idty.membershipExpiresIn / (3600 * 24)) + return UrlWatcherResult.ko(`Membership is going to expire in ${remainingDays.toFixed(0)} days`) + } else { + state = 'ERROR' + return UrlWatcherResult.ko(`Membership expired`) + } + } + state = 'OK' + return UrlWatcherResult.ok() + })({ + name: `membership ${confMS.pubkey}`, + address: confMS.address + URL_PATH + confMS.pubkey, + frequency: confMS.frequency + }, + () => getSubjectTitle(state), + () => getSubjectTitle(state), + () => getSubjectTitle(state)) + } +}