Skip to content

Commit

Permalink
UPNP: Two retries
Browse files Browse the repository at this point in the history
  • Loading branch information
supertypo committed Oct 6, 2023
1 parent da5e98a commit 0fffd86
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/interfaces/kaspad.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = class Kaspad extends Daemon {
this.data = [ ];

this.upnpClient = new natUpnp.Client({timeout: 4000});
this.upnpRetries = 2;
this.upnpTtl = 7200; // 2 hours
this.upnpRenewalSec = Math.floor(this.upnpTtl / 4);
this.upnpRenewalInterval = null;
Expand Down Expand Up @@ -58,7 +59,7 @@ module.exports = class Kaspad extends Daemon {
this.writeToSink(`KASPAD: Adding externalip=${externalip} to arguments\n`);
args['externalip'] = externalip;
}
const publicPort = await this.forwardPort(nodePort, true);
const publicPort = await this.forwardPort(nodePort, this.upnpRetries);
if (publicPort) {
if (publicPort !== nodePort) {
this.writeToSink(`KASPAD: Changing listener port from ${nodePort} to ${publicPort} in arguments\n`);
Expand Down Expand Up @@ -160,7 +161,7 @@ module.exports = class Kaspad extends Daemon {
});
}

forwardPort(port, retry) {
forwardPort(port, retries) {
return new Promise(resolve => setTimeout(resolve, 1000)).then(() => {
return this.upnpClient.createMapping({
public: port,
Expand All @@ -172,10 +173,10 @@ module.exports = class Kaspad extends Daemon {
this.scheduleRenewal(port);
return port;
}).catch((err) => {
if (retry) {
if (retries) {
const alternativePort = Math.floor(Math.random() * 45000) + 20000;
this.writeToSink(`UPNP: Mapping of public port ${port} failed, trying alternative port ${alternativePort}.\n`.yellow);
return this.forwardPort(alternativePort);
return this.forwardPort(alternativePort, --retries);
} else {
this.writeToSink(`UPNP: Mapping of public port ${port} failed: ${err.message}\n`.red);
}
Expand Down

0 comments on commit 0fffd86

Please sign in to comment.