-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
test/repo-tests* | ||
**/bundle.js | ||
**/.nyc_output | ||
**/.vscode/ | ||
|
||
# Logs | ||
logs | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
'use strict' | ||
const debug = require('debug') | ||
|
||
class BaseMapper { | ||
constructor (name) { | ||
this.name = name | ||
this.mappings = {} | ||
|
||
this.log = debug(`nat-puncher:${name}`) | ||
this.log.err = debug(`nat-puncher:${name}:error`) | ||
} | ||
|
||
newMapping (port) { | ||
return { | ||
routerIp: null, | ||
internalIp: null, | ||
internalPort: port, | ||
externalIp: null, // Only provided by PCP, undefined for other protocols | ||
externalPort: null, // The actual external port of the mapping, -1 on failure | ||
ttl: null, // The actual (response) lifetime of the mapping | ||
protocol: this.name, // The protocol used to make the mapping ('natPmp', 'pcp', 'upnp') | ||
nonce: null, // Only for PCP; the nonce field for deletion | ||
errInfo: null // Error message if failure; currently used only for UPnP | ||
} | ||
} | ||
|
||
addMapping (intPort, extPort, ttl, callback) { | ||
// If lifetime is zero, we want to refresh every 24 hours | ||
ttl = !ttl ? 24 * 60 * 60 : ttl | ||
|
||
this._addPortMapping(intPort, | ||
extPort, | ||
ttl, | ||
(err, mapping) => { | ||
if (err) { | ||
this.log.err(err) | ||
return callback(err) | ||
} | ||
this.mappings[`${mapping.externalIp}:${mapping.externalPort}`] = mapping | ||
callback(null, mapping) | ||
}) | ||
} | ||
|
||
_addPortMapping (intPort, extPort, lifetime, cb) { | ||
cb(new Error('Not implemented!')) | ||
} | ||
|
||
deleteMapping (mapping, callback) { | ||
this._removePortMapping(mapping.internalPort, | ||
mapping.externalPort, | ||
(err) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
|
||
// delete the mappings | ||
delete this.mappings[`${mapping.externalIp}:${mapping.externalPort}`] | ||
callback() | ||
}) | ||
} | ||
|
||
_removePortMapping (intPort, extPort, callback) { | ||
callback(new Error('Not implemented!')) | ||
} | ||
} | ||
|
||
module.exports = BaseMapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
const chai = require('chai') | ||
chai.use(require('dirty-chai')) | ||
chai.use(require('chai-checkmark')) | ||
|
||
const Manager = require('../src') | ||
const Mapper = require('../src/mappers') | ||
|
||
const expect = chai.expect | ||
|
||
describe('NAT manager', () => { | ||
it('should create mappings', (done) => { | ||
class Mapper1 extends Mapper { | ||
constructor () { | ||
super('mapper1') | ||
} | ||
|
||
_addPortMapping (intPort, extPort, lifetime, cb) { | ||
cb(null, this.newMapping(intPort)) | ||
} | ||
} | ||
|
||
const manager = new Manager([ | ||
new Mapper1() | ||
]) | ||
|
||
manager.addMapping(55555, 55555, 0, (err, mapping) => { | ||
expect(err).to.not.exist() | ||
expect(mapping).to.exist() | ||
expect(mapping.internalPort).to.eql(55555) | ||
done() | ||
}) | ||
}) | ||
|
||
it('should try mappings in order', (done) => { | ||
let fail = true | ||
|
||
class Mapper1 extends Mapper { | ||
_addPortMapping (intPort, extPort, lifetime, cb) { | ||
cb(fail ? new Error('fail') : null, this.newMapping(intPort)) | ||
fail = false | ||
} | ||
} | ||
|
||
const manager = new Manager([ | ||
new Mapper1('1'), | ||
new Mapper1('2') | ||
]) | ||
|
||
manager.addMapping(55555, 55555, 0, (err, mapping) => { | ||
expect(err).to.not.exist() | ||
expect(mapping).to.exist() | ||
expect(mapping.protocol).to.eql('2') | ||
expect(mapping.internalPort).to.eql(55555) | ||
done() | ||
}) | ||
}) | ||
}) |