[enh] moving WS2P code to ws2p-watcher
This commit is contained in:
@@ -1,7 +1,4 @@
|
||||
export interface Conf {
|
||||
currency: string
|
||||
salt: string
|
||||
passwd: string
|
||||
connectionTimeout: number
|
||||
reconnectionDelays: number[]
|
||||
ws2pServers: ConfServers[]
|
||||
@@ -11,6 +8,9 @@ export interface Conf {
|
||||
export interface ConfServers {
|
||||
address: string
|
||||
expectedKey: string
|
||||
salt: string
|
||||
passwd: string
|
||||
currency: string
|
||||
}
|
||||
|
||||
export interface ConfMail {
|
||||
|
||||
@@ -1,92 +1,12 @@
|
||||
import {Conf} from './conf'
|
||||
import * as yaml from 'js-yaml';
|
||||
import * as fs from 'fs';
|
||||
import {WS2PConnection, WS2PPubkeyLocalAuth, WS2PPubkeyRemoteAuth} from 'duniter/app/modules/ws2p/lib/WS2PConnection'
|
||||
import {MessageHandler} from './message-handler'
|
||||
import {Key} from 'duniter/app/lib/common-libs/crypto/keyring'
|
||||
import {Scrypt} from 'duniter/app/modules/keypair/lib/scrypt'
|
||||
import {sendMail} from './sendMail'
|
||||
import {moment} from 'duniter/app/lib/common-libs/moment'
|
||||
import {processHandler} from './processHandler'
|
||||
import {ws2pWatcher} from "./ws2p/ws2p-watcher";
|
||||
|
||||
export async function dwatch(confFile: string) {
|
||||
|
||||
const yml = fs.readFileSync(confFile, 'utf8')
|
||||
const conf = yaml.load(yml) as Conf
|
||||
const keys = await Scrypt(conf.salt, conf.passwd)
|
||||
const keypair = new Key(keys.pub, keys.sec)
|
||||
|
||||
await Promise.all(conf.ws2pServers.map(async wserver => {
|
||||
|
||||
let c: WS2PConnection
|
||||
|
||||
await processHandler(
|
||||
async () => {
|
||||
|
||||
const localAuth = new WS2PPubkeyLocalAuth(conf.currency, keypair, "", async () => true)
|
||||
const remoteAuth = new WS2PPubkeyRemoteAuth(conf.currency, keypair, async () => true)
|
||||
|
||||
c = WS2PConnection.newConnectionToAddress(
|
||||
1,
|
||||
wserver.address,
|
||||
new MessageHandler(),
|
||||
localAuth,
|
||||
remoteAuth,
|
||||
undefined,
|
||||
{
|
||||
connectionTimeout: conf.connectionTimeout,
|
||||
requestTimeout: 0 // No request anyway
|
||||
},
|
||||
wserver.expectedKey
|
||||
)
|
||||
|
||||
await c.connectAsInitiator()
|
||||
},
|
||||
|
||||
() => c.closed,
|
||||
|
||||
conf.reconnectionDelays,
|
||||
|
||||
async () => {
|
||||
console.log('Connection established')
|
||||
await sendMail(conf.mail, '[dwatcher] Connection established', `
|
||||
<p>
|
||||
Connection to ${c.pubkey} established on ${moment().format('DD-MM-YYYY HH:mm:ss')}.
|
||||
</p>
|
||||
`)
|
||||
},
|
||||
|
||||
// When a disconnection is detected
|
||||
async (waitingDelay) => {
|
||||
console.log('Connection closed')
|
||||
console.log('Waiting %s seconds...', waitingDelay)
|
||||
await sendMail(conf.mail, '[dwatcher] Connection closed', `
|
||||
<p>
|
||||
Connection to ${c.pubkey} was lost on ${moment().format('dd-MM-YYYY HH:mm:ss')}.
|
||||
</p>
|
||||
<p>
|
||||
Waiting ${(waitingDelay / 1000).toFixed(0)} seconds before trying to reconnect.
|
||||
</p>
|
||||
`)
|
||||
},
|
||||
|
||||
async () => {
|
||||
console.log('Trying to connect to %s', c.pubkey)
|
||||
},
|
||||
|
||||
async () => {
|
||||
console.log('Connection recovered')
|
||||
await sendMail(conf.mail, '[dwatcher] Connection recovered', `
|
||||
<p>
|
||||
Connection to ${c.pubkey} was lost on ${moment().format('dd-MM-YYYY HH:mm:ss')}.
|
||||
</p>
|
||||
`)
|
||||
},
|
||||
|
||||
async (e) => {
|
||||
console.error(e)
|
||||
}
|
||||
)
|
||||
|
||||
}))
|
||||
await Promise.all(conf.ws2pServers.map(ws2pWatcher(conf)))
|
||||
}
|
||||
|
||||
88
src/lib/ws2p/ws2p-watcher.ts
Normal file
88
src/lib/ws2p/ws2p-watcher.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import {WS2PConnection, WS2PPubkeyLocalAuth, WS2PPubkeyRemoteAuth} from "duniter/app/modules/ws2p/lib/WS2PConnection";
|
||||
import {processHandler} from "../processHandler";
|
||||
import {MessageHandler} from "../message-handler";
|
||||
import {sendMail} from "../sendMail";
|
||||
import {moment} from "duniter/app/lib/common-libs/moment";
|
||||
import {Conf, ConfServers} from "../conf";
|
||||
import {Scrypt} from "duniter/app/modules/keypair/lib/scrypt";
|
||||
import {Key} from "duniter/app/lib/common-libs/crypto/keyring";
|
||||
|
||||
export function ws2pWatcher(conf: Conf) {
|
||||
|
||||
return async (wserver: ConfServers) => {
|
||||
|
||||
const keys = await Scrypt(wserver.salt, wserver.passwd)
|
||||
const keypair = new Key(keys.pub, keys.sec)
|
||||
|
||||
let c: WS2PConnection
|
||||
|
||||
await processHandler(
|
||||
async () => {
|
||||
|
||||
const localAuth = new WS2PPubkeyLocalAuth(wserver.currency, keypair, "", async () => true)
|
||||
const remoteAuth = new WS2PPubkeyRemoteAuth(wserver.currency, keypair, async () => true)
|
||||
|
||||
c = WS2PConnection.newConnectionToAddress(
|
||||
1,
|
||||
wserver.address,
|
||||
new MessageHandler(),
|
||||
localAuth,
|
||||
remoteAuth,
|
||||
undefined,
|
||||
{
|
||||
connectionTimeout: conf.connectionTimeout,
|
||||
requestTimeout: 0 // No request anyway
|
||||
},
|
||||
wserver.expectedKey
|
||||
)
|
||||
|
||||
await c.connectAsInitiator()
|
||||
},
|
||||
|
||||
() => c.closed,
|
||||
|
||||
conf.reconnectionDelays,
|
||||
|
||||
async () => {
|
||||
console.log('Connection established')
|
||||
await sendMail(conf.mail, '[dwatcher] Connection established', `
|
||||
<p>
|
||||
Connection to ${c.pubkey} established on ${moment().format('DD-MM-YYYY HH:mm:ss')}.
|
||||
</p>
|
||||
`)
|
||||
},
|
||||
|
||||
// When a disconnection is detected
|
||||
async (waitingDelay) => {
|
||||
console.log('Connection closed')
|
||||
console.log('Waiting %s seconds...', waitingDelay)
|
||||
await sendMail(conf.mail, '[dwatcher] Connection closed', `
|
||||
<p>
|
||||
Connection to ${c.pubkey} was lost on ${moment().format('dd-MM-YYYY HH:mm:ss')}.
|
||||
</p>
|
||||
<p>
|
||||
Waiting ${(waitingDelay / 1000).toFixed(0)} seconds before trying to reconnect.
|
||||
</p>
|
||||
`)
|
||||
},
|
||||
|
||||
async () => {
|
||||
console.log('Trying to connect to %s', c.pubkey)
|
||||
},
|
||||
|
||||
async () => {
|
||||
console.log('Connection recovered')
|
||||
await sendMail(conf.mail, '[dwatcher] Connection recovered', `
|
||||
<p>
|
||||
Connection to ${c.pubkey} was lost on ${moment().format('dd-MM-YYYY HH:mm:ss')}.
|
||||
</p>
|
||||
`)
|
||||
},
|
||||
|
||||
async (e) => {
|
||||
console.error(e)
|
||||
}
|
||||
)
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user