[enh] moving WS2P code to ws2p-watcher
This commit is contained in:
6
app.yml
6
app.yml
@@ -1,6 +1,3 @@
|
|||||||
currency: g1-test # Ensure we connect to the correct currency
|
|
||||||
salt: test
|
|
||||||
passwd: test
|
|
||||||
connectionTimeout: 10000
|
connectionTimeout: 10000
|
||||||
reconnectionDelays:
|
reconnectionDelays:
|
||||||
- 5000
|
- 5000
|
||||||
@@ -17,6 +14,9 @@ reconnectionDelays:
|
|||||||
ws2pServers:
|
ws2pServers:
|
||||||
- address: ws://g1-test.cgeek.fr:20902
|
- address: ws://g1-test.cgeek.fr:20902
|
||||||
expectedPubkey: 2ny7YAdmzReQxAayyJZsyVYwYhVyax2thKcGknmQy5nQ
|
expectedPubkey: 2ny7YAdmzReQxAayyJZsyVYwYhVyax2thKcGknmQy5nQ
|
||||||
|
salt: test
|
||||||
|
passwd: test
|
||||||
|
currency: g1-test # Ensure we connect to the correct currency
|
||||||
|
|
||||||
mail:
|
mail:
|
||||||
enabled: false
|
enabled: false
|
||||||
|
|||||||
@@ -1,7 +1,4 @@
|
|||||||
export interface Conf {
|
export interface Conf {
|
||||||
currency: string
|
|
||||||
salt: string
|
|
||||||
passwd: string
|
|
||||||
connectionTimeout: number
|
connectionTimeout: number
|
||||||
reconnectionDelays: number[]
|
reconnectionDelays: number[]
|
||||||
ws2pServers: ConfServers[]
|
ws2pServers: ConfServers[]
|
||||||
@@ -11,6 +8,9 @@ export interface Conf {
|
|||||||
export interface ConfServers {
|
export interface ConfServers {
|
||||||
address: string
|
address: string
|
||||||
expectedKey: string
|
expectedKey: string
|
||||||
|
salt: string
|
||||||
|
passwd: string
|
||||||
|
currency: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ConfMail {
|
export interface ConfMail {
|
||||||
|
|||||||
@@ -1,92 +1,12 @@
|
|||||||
import {Conf} from './conf'
|
import {Conf} from './conf'
|
||||||
import * as yaml from 'js-yaml';
|
import * as yaml from 'js-yaml';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import {WS2PConnection, WS2PPubkeyLocalAuth, WS2PPubkeyRemoteAuth} from 'duniter/app/modules/ws2p/lib/WS2PConnection'
|
import {ws2pWatcher} from "./ws2p/ws2p-watcher";
|
||||||
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'
|
|
||||||
|
|
||||||
export async function dwatch(confFile: string) {
|
export async function dwatch(confFile: string) {
|
||||||
|
|
||||||
const yml = fs.readFileSync(confFile, 'utf8')
|
const yml = fs.readFileSync(confFile, 'utf8')
|
||||||
const conf = yaml.load(yml) as Conf
|
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 => {
|
await Promise.all(conf.ws2pServers.map(ws2pWatcher(conf)))
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|||||||
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