[enh] Init project

This commit is contained in:
2019-05-28 12:57:37 +02:00
commit fa85ddcc1d
9 changed files with 3561 additions and 0 deletions

52
src/lib/dwatch.ts Normal file
View File

@@ -0,0 +1,52 @@
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'
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 => {
const localAuth = new WS2PPubkeyLocalAuth(conf.currency, keypair, "", async () => true)
const remoteAuth = new WS2PPubkeyRemoteAuth(conf.currency, keypair, async () => true)
const 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.then(async () => {
console.log('Connection closed')
await sendMail(conf.mail, '[dwatcher] Connection closed', `
<p>
Connection to ${c.pubkey} was lost on ${moment().format('dd-MM-YYYY HH:mm:ss')}.
</p>
`)
})
await c.connected
console.log('Connected to %s', c.pubkey)
}))
}