From 11a1b0277d2986be73ccc06872f6ed797ff08f33 Mon Sep 17 00:00:00 2001 From: cgeek <cem.moreau@gmail.com> Date: Mon, 18 Sep 2017 14:57:25 +0200 Subject: [PATCH] [fix] #1084 WS2P: UPnP code under non-UPnP environment makes Duniter crash --- app/modules/ws2p/index.ts | 10 ++++++---- app/modules/ws2p/lib/WS2PConnection.ts | 3 ++- app/modules/ws2p/lib/ws2p-upnp.ts | 13 ++++++++----- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/app/modules/ws2p/index.ts b/app/modules/ws2p/index.ts index 2950107b8..5c72f9cb8 100644 --- a/app/modules/ws2p/index.ts +++ b/app/modules/ws2p/index.ts @@ -107,10 +107,12 @@ export class WS2PAPI extends stream.Transform { this.upnpAPI.stopRegular(); } try { - this.upnpAPI = await new WS2PUpnp(this.logger) - const { host, port } = await this.upnpAPI.startRegular() - await this.cluster.listen(host, port) - await this.server.PeeringService.generateSelfPeer(this.server.conf) + this.upnpAPI = new WS2PUpnp(this.logger) + const { host, port, available } = await this.upnpAPI.startRegular() + if (available) { + await this.cluster.listen(host, port) + await this.server.PeeringService.generateSelfPeer(this.server.conf) + } } catch (e) { this.logger.warn(e); } diff --git a/app/modules/ws2p/lib/WS2PConnection.ts b/app/modules/ws2p/lib/WS2PConnection.ts index 9835cd84d..4e698aa41 100644 --- a/app/modules/ws2p/lib/WS2PConnection.ts +++ b/app/modules/ws2p/lib/WS2PConnection.ts @@ -6,12 +6,13 @@ import {CertificationDTO} from "../../../lib/dto/CertificationDTO" import {MembershipDTO} from "../../../lib/dto/MembershipDTO" import {TransactionDTO} from "../../../lib/dto/TransactionDTO" import {PeerDTO} from "../../../lib/dto/PeerDTO" +import {WS2PConstants} from "./constants" const ws = require('ws') const nuuid = require('node-uuid'); const logger = require('../../../lib/logger').NewLogger('ws2p') const MAXIMUM_ERRORS_COUNT = 5 -const REQUEST_TIMEOUT_VALUE = 1000 * 5 // 10 seconds +const REQUEST_TIMEOUT_VALUE = WS2PConstants.REQUEST_TIMEOUT enum WS2P_ERR { REJECTED_PUBKEY_OR_INCORRECT_ASK_SIGNATURE_FROM_REMOTE, diff --git a/app/modules/ws2p/lib/ws2p-upnp.ts b/app/modules/ws2p/lib/ws2p-upnp.ts index 8f16146d7..81b0dae12 100644 --- a/app/modules/ws2p/lib/ws2p-upnp.ts +++ b/app/modules/ws2p/lib/ws2p-upnp.ts @@ -21,10 +21,10 @@ export class WS2PUpnp { try { await new Promise((resolve, reject) => { this.client.externalIp((err:any, res:any) => { - if (err) { - resolve(true) + if (err || !res) { + reject() } else { - resolve(false) + resolve() } }) }) @@ -67,11 +67,14 @@ export class WS2PUpnp { async startRegular() { this.stopRegular(); - if (await this.checkUPnPisAvailable()) { + const available = await this.checkUPnPisAvailable() + if (available) { // Update UPnP IGD every INTERVAL seconds this.interval = setInterval(() => this.openPort(), 1000 * WS2PConstants.WS2P_UPNP_INTERVAL) + const { host, port } = await this.openPort() + return { host, port, available } } - return this.openPort() + return { host: '', port: 0, available: false } } stopRegular() { -- GitLab