Skip to content
Snippets Groups Projects
Commit cd519708 authored by Cédric Moreau's avatar Cédric Moreau
Browse files

[fix] #1045 Detect the best UPnP available port

parent a5ce9138
Branches
Tags
No related merge requests found
...@@ -147,15 +147,7 @@ export const BmaDependency = { ...@@ -147,15 +147,7 @@ export const BmaDependency = {
methods: { methods: {
noLimit: () => BMALimitation.noLimit(), noLimit: () => BMALimitation.noLimit(),
bma, sanitize, dtos, bma, sanitize, dtos,
upnpConf: Network.upnpConf, getMainEndpoint: (conf:NetworkConfDTO) => Promise.resolve(getEndpoint(conf))
getRandomPort: Network.getRandomPort,
listInterfaces: Network.listInterfaces,
getEndpoint: getEndpoint,
getMainEndpoint: (conf:NetworkConfDTO) => Promise.resolve(getEndpoint(conf)),
getBestLocalIPv6: Network.getBestLocalIPv6,
getBestLocalIPv4: Network.getBestLocalIPv4,
createServersAndListen: Network.createServersAndListen,
http2raw
} }
} }
} }
......
export const BMAConstants = { export const BMAConstants = {
BMA_PORTS_START: 10901,
BMA_PORTS_END: 10999,
ENTITY_BLOCK: 'block', ENTITY_BLOCK: 'block',
ENTITY_IDENTITY: 'identity', ENTITY_IDENTITY: 'identity',
ENTITY_CERTIFICATION: 'certification', ENTITY_CERTIFICATION: 'certification',
......
...@@ -29,7 +29,7 @@ export const Network = { ...@@ -29,7 +29,7 @@ export const Network = {
listInterfaces: listInterfaces, listInterfaces: listInterfaces,
upnpConf: (noupnp:boolean, logger:any) => upnpConf(noupnp, logger), upnpConf,
getRandomPort: getRandomPort, getRandomPort: getRandomPort,
...@@ -335,22 +335,22 @@ function listInterfaces() { ...@@ -335,22 +335,22 @@ function listInterfaces() {
} }
async function upnpConf (noupnp:boolean, logger:any) { async function upnpConf (noupnp:boolean, logger:any) {
const client = require('nnupnp').createClient();
// Look for 2 random ports
const publicPort = await getAvailablePort(client)
const privatePort = publicPort
const conf:NetworkConfDTO = { const conf:NetworkConfDTO = {
port: 10901, port: privatePort,
ipv4: '127.0.0.1', ipv4: '127.0.0.1',
ipv6: '::1', ipv6: '::1',
dos: null, dos: null,
upnp: false, upnp: false,
httplogs: false, httplogs: false,
remoteport: 10901, remoteport: publicPort,
remotehost: null, remotehost: null,
remoteipv4: null, remoteipv4: null,
remoteipv6: null remoteipv6: null
} }
const client = require('nnupnp').createClient();
// Look for 2 random ports
const privatePort = getRandomPort(conf);
const publicPort = privatePort;
logger && logger.info('Checking UPnP features...'); logger && logger.info('Checking UPnP features...');
if (noupnp) { if (noupnp) {
throw Error('No UPnP'); throw Error('No UPnP');
...@@ -374,6 +374,19 @@ async function upnpConf (noupnp:boolean, logger:any) { ...@@ -374,6 +374,19 @@ async function upnpConf (noupnp:boolean, logger:any) {
return conf; return conf;
} }
async function getAvailablePort(client:any) {
const mappings:{ public: { port:number }}[] = await Q.nbind(client.getMappings, client)();
const externalPortsUsed = mappings.map(m => m.public.port)
let availablePort = BMAConstants.BMA_PORTS_START
while (externalPortsUsed.indexOf(availablePort) !== -1 && availablePort <= BMAConstants.BMA_PORTS_END) {
availablePort++
}
if (availablePort > BMAConstants.BMA_PORTS_END) {
throw "No port available for UPnP"
}
return availablePort
}
function getRandomPort(conf:NetworkConfDTO) { function getRandomPort(conf:NetworkConfDTO) {
if (conf && conf.remoteport) { if (conf && conf.remoteport) {
return conf.remoteport; return conf.remoteport;
......
...@@ -6,6 +6,7 @@ import * as stream from "stream" ...@@ -6,6 +6,7 @@ import * as stream from "stream"
import {RevocationDTO} from "../../../app/lib/dto/RevocationDTO" import {RevocationDTO} from "../../../app/lib/dto/RevocationDTO"
import {IdentityDTO} from "../../../app/lib/dto/IdentityDTO" import {IdentityDTO} from "../../../app/lib/dto/IdentityDTO"
import {PeerDTO} from "../../../app/lib/dto/PeerDTO" import {PeerDTO} from "../../../app/lib/dto/PeerDTO"
import {Network} from "../../../app/modules/bma/lib/network";
const _ = require('underscore'); const _ = require('underscore');
const rp = require('request-promise'); const rp = require('request-promise');
...@@ -135,7 +136,7 @@ export const fakeSyncServer = async (readBlocksMethod:any, readParticularBlockMe ...@@ -135,7 +136,7 @@ export const fakeSyncServer = async (readBlocksMethod:any, readParticularBlockMe
processRequest: () => { /* Does nothing */ } processRequest: () => { /* Does nothing */ }
}; };
const fakeServer = await require('../../../app/modules/bma').BmaDependency.duniter.methods.createServersAndListen("Fake Duniter Server", { conf: {} }, [{ const fakeServer = await Network.createServersAndListen("Fake Duniter Server", new Server("", true, {}), [{
ip: host, ip: host,
port: port port: port
}], NO_HTTP_LOGS, logger, NO_STATIC_PATH, (app:any, httpMethods:any) => { }], NO_HTTP_LOGS, logger, NO_STATIC_PATH, (app:any, httpMethods:any) => {
...@@ -170,7 +171,7 @@ export const fakeSyncServer = async (readBlocksMethod:any, readParticularBlockMe ...@@ -170,7 +171,7 @@ export const fakeSyncServer = async (readBlocksMethod:any, readParticularBlockMe
return readParticularBlockMethod(number); return readParticularBlockMethod(number);
}, dtos.Block, noLimit); }, dtos.Block, noLimit);
}); }, null)
await fakeServer.openConnections(); await fakeServer.openConnections();
return { return {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment