15min TTL and re-punch NAT periodically

pull/317/head
sachaaaaa 6 years ago
parent 57b7aaf195
commit abdb232151

@ -107,6 +107,7 @@ class LocalLokiServer extends EventEmitter {
const privatePort = this.server.address().port; const privatePort = this.server.address().port;
const portStart = 22100; const portStart = 22100;
const portEnd = 22200; const portEnd = 22200;
const ttl = 60 * 15; // renew upnp every 15 minutes
const publicPortsInUse = await new Promise((resolve, reject) => { const publicPortsInUse = await new Promise((resolve, reject) => {
this.upnpClient.getMappings({ local: true }, (err, results) => { this.upnpClient.getMappings({ local: true }, (err, results) => {
if (err) { if (err) {
@ -114,7 +115,11 @@ class LocalLokiServer extends EventEmitter {
reject(new textsecure.HolePunchingError('Could not get mapping from upnp. Upnp not available?', err)); reject(new textsecure.HolePunchingError('Could not get mapping from upnp. Upnp not available?', err));
} }
else { else {
resolve(results.map(entry => entry.public.port)); // remove the current private port from the current mapping
// to allow reusing that port.
resolve(results
.filter(entry => entry.private.port !== privatePort)
.map(entry => entry.public.port));
} }
}); });
}); });
@ -127,7 +132,7 @@ class LocalLokiServer extends EventEmitter {
this.upnpClient.portMapping({ this.upnpClient.portMapping({
public: publicPort, public: publicPort,
private: privatePort, private: privatePort,
ttl: 100000, ttl,
}, (err) => { }, (err) => {
if (err) if (err)
reject(err); reject(err);
@ -139,6 +144,13 @@ class LocalLokiServer extends EventEmitter {
// eslint-disable-next-line no-await-in-loop // eslint-disable-next-line no-await-in-loop
await p; await p;
this.publicPort = publicPort; this.publicPort = publicPort;
setTimeout(() => {
try {
this.publicPort = this.punchHole();
} catch (e) {
this.close();
}
}, ttl);
return publicPort; return publicPort;
} catch (e) { } catch (e) {
throw new textsecure.HolePunchingError('Could not punch hole. Disabled upnp?', e); throw new textsecure.HolePunchingError('Could not punch hole. Disabled upnp?', e);

Loading…
Cancel
Save