Merge branch 'feature/memberships'

This commit is contained in:
2020-09-20 13:32:42 +02:00
8 changed files with 87 additions and 17 deletions

12
app.yml
View File

@@ -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"

View File

@@ -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
}

View File

@@ -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[] {
@@ -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 = () => `
<p>
Connection from [${os.hostname}] to ${target} established on ${moment().format('DD-MM-YYYY HH:mm:ss')}.
</p>
`) => {
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) => `
<p>
Connection from [${os.hostname}] to ${target} was lost on ${moment().format('dd-MM-YYYY HH:mm:ss')}.
</p>
@@ -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 = () => `
<p>
Connection from [${os.hostname}] to ${target} was recovered on ${moment().format('dd-MM-YYYY HH:mm:ss')}.
</p>
`) => {
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)
}
},
}

View File

@@ -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

View File

@@ -17,7 +17,7 @@ export function urlWatcher(conf: Conf, checkValidity: (data: any) => Promise<Url
async function checkResult(data: any) {
const validity = await checkValidity(data)
if (validity.error) {
throw new UrlWatcherError('Validity error')
throw new UrlWatcherError(validity.error)
}
}
@@ -47,7 +47,7 @@ export function urlWatcher(conf: Conf, checkValidity: (data: any) => Promise<Url
conf.waitingDelay,
conf.recallDelay,
mail.onEstablished(urlConf.address, getOkTitle()),
mail.onEstablished(urlConf.address, () => getOkTitle()),
// When a disconnection is detected
(waitingDelay: number, recallDelay, error?: any) => {
@@ -57,14 +57,14 @@ export function urlWatcher(conf: Conf, checkValidity: (data: any) => Promise<Url
koTitle = getKoTitle()
koMessage = () => `<p>${error.errorMessage}</p>`
}
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()),
)
}

View File

@@ -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))
}
}

View File

@@ -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}
<p>
Waiting ${(waitingDelay / 1000).toFixed(0)} seconds before trying to reconnect.

View File

@@ -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}`),
)
}