Commit 4cf66bba authored by Cédric Moreau's avatar Cédric Moreau
Browse files

Fix UPnP issues and add it to Webmin

parent 52682055
......@@ -7,6 +7,7 @@ var _ = require('underscore');
var Q = require('q');
let co = require('co');
let ucoin = require('../../index');
var upnp = require('../lib/upnp');
let ucp = require('../lib/ucp');
let constants = require('../lib/constants');
let base58 = require('../lib/base58');
......@@ -82,6 +83,11 @@ function WebAdmin (dbConf, overConf) {
return bmapi.openConnections();
});
this.openUPnP = () => co(function *() {
yield pluggedDALP;
return upnp(server.conf.port, server.conf.remoteport);
});
this.stopHTTP = () => co(function *() {
yield pluggedDALP;
return bmapi.closeConnections();
......
......@@ -21,6 +21,7 @@ module.exports = function(dbConf, overConf, interfaces, httpLogs) {
httpMethods.httpPOST( '/webmin/key/preview', webminCtrl.previewPubkey, dtos.PreviewPubkey);
httpMethods.httpGET( '/webmin/server/http/start', webminCtrl.startHTTP, dtos.Boolean);
httpMethods.httpGET( '/webmin/server/http/stop', webminCtrl.stopHTTP, dtos.Boolean);
httpMethods.httpGET( '/webmin/server/http/upnp/open', webminCtrl.openUPnP, dtos.Boolean);
httpMethods.httpGET( '/webmin/server/preview_next', webminCtrl.previewNext, dtos.Block);
httpMethods.httpPOST( '/webmin/server/send_conf', webminCtrl.sendConf, dtos.Identity);
httpMethods.httpPOST( '/webmin/server/net_conf', webminCtrl.applyNetworkConf, dtos.Boolean);
......
......@@ -9,12 +9,12 @@ module.exports = function (localPort, remotePort) {
"use strict";
return co(function *() {
logger.info('UPnP: configuring...');
// Update UPnP IGD every INTERVAL seconds
setInterval(async.apply(openPort, localPort, remotePort), 1000 * constants.NETWORK.UPNP.INTERVAL);
return openPort(localPort, remotePort)
.catch(function(){
return co(function *() {
try {
yield openPort(localPort, remotePort);
} catch (e) {
var client = upnp.createClient();
return Q.nbind(client.externalIp, client)()
yield Q.nbind(client.externalIp, client)()
.catch(function(err){
if (err && err.message == 'timeout') {
throw 'No UPnP gateway found: your node won\'t be reachable from the Internet. Use --noupnp option to avoid this message.'
......@@ -24,7 +24,24 @@ module.exports = function (localPort, remotePort) {
.finally(function() {
client.close();
});
});
}
let interval;
return {
openPort: () => {
return openPort(localPort, remotePort);
},
startRegular: () => {
this.stopRegular();
// Update UPnP IGD every INTERVAL seconds
interval = setInterval(async.apply(openPort, localPort, remotePort), 1000 * constants.NETWORK.UPNP.INTERVAL);
},
stopRegular: () => {
if (interval) {
clearInterval(interval);
}
}
};
});
});
};
......
......@@ -53,10 +53,15 @@ module.exports.statics = {
* UPnP
**************/
if (server.conf.upnp) {
yield upnp(server.conf.port, server.conf.remoteport)
.catch(function(err){
logger.warn(err);
});
try {
if (server.upnpAPI) {
server.upnpAPI.stopRegular();
}
server.upnpAPI = yield upnp(server.conf.port, server.conf.remoteport);
server.upnpAPI.startRegular();
} catch (e) {
logger.warn(e);
}
}
/*******************
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment