61 lines
1.7 KiB
TypeScript
61 lines
1.7 KiB
TypeScript
import {WS2PConnection, WS2PPubkeyLocalAuth, WS2PPubkeyRemoteAuth} from "duniter/app/modules/ws2p/lib/WS2PConnection";
|
|
import {watcherLoop} from "../../watcherLoop";
|
|
import {MessageHandler} from "./message-handler";
|
|
import {Conf, ConfWS2P} from "../../types/conf";
|
|
import {Scrypt} from "duniter/app/modules/keypair/lib/scrypt";
|
|
import {Key} from "duniter/app/lib/common-libs/crypto/keyring";
|
|
import {mail} from "../../mail";
|
|
|
|
export function ws2pWatcher(conf: Conf) {
|
|
|
|
let c: WS2PConnection
|
|
|
|
return async (wserver: ConfWS2P) => {
|
|
|
|
const keys = await Scrypt(wserver.salt, wserver.passwd)
|
|
const keypair = new Key(keys.pub, keys.sec)
|
|
const target = `${wserver.address} (${wserver.expectedPubkey.substr(0, 8)})`
|
|
|
|
return watcherLoop(
|
|
`WS2P ${wserver.address}`,
|
|
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.expectedPubkey
|
|
)
|
|
|
|
await c.connectAsInitiator()
|
|
},
|
|
|
|
() => c.closed,
|
|
|
|
conf.waitingDelay,
|
|
|
|
mail.onEstablished(conf, target),
|
|
|
|
// When a disconnection is detected
|
|
mail.onDisconnect(conf, target),
|
|
|
|
async () => {
|
|
console.log('Trying to connect to %s', target)
|
|
},
|
|
|
|
mail.onRestartSuccess(conf, target),
|
|
)
|
|
|
|
}
|
|
}
|