diff --git a/app/modules/ws2p/index.ts b/app/modules/ws2p/index.ts index 2950107b8c7b11252a51f11159a2cb459f4e70a1..5c72f9cb8f76f59f54a1713c61dbad6643d30d70 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 9835cd84d2cecf52fce6e240f71a5f0fe083f2e3..4e698aa4152ec43b7210158f4e92b13f2c56e331 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 8f16146d7b14d83d3f8464cfe4eae20a457a32f1..81b0dae12e5ceec0a78dc9090988091e46b4f8f7 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() {