Compare commits

...

7 Commits

Author SHA1 Message Date
3f61784be9 docs(api): first pass 2018-07-31 15:53:27 +02:00
a0c23e49f7 chore: release version v0.12.1
License: MIT
Signed-off-by: Jacob Heun <jacobheun@gmail.com>
2018-07-31 14:13:11 +02:00
66ab208182 chore: update contributors 2018-07-31 14:13:10 +02:00
168d111158 chore: update deps and fix test runner
License: MIT
Signed-off-by: Jacob Heun <jacobheun@gmail.com>
2018-07-31 14:08:31 +02:00
4b04b17dfa fix: invalid ip address and daemon can be crashed by remote user
Per the nodeJS documentation, a Net socket.remoteAddress value may
be undefined if the socket is destroyed, as by a client disconnect.
A multiaddr cannot be created for an invalid IP address (such as
the undefined remote address of a destroyed socket). Currently
the attempt results in a crash that can be triggered remotely. This
commit catches the exception in get-multiaddr and returns an
undefined value to listener rather than throwing an exception when
trying to process defective or destroyed socket data. Listener then
terminates processing of the incoming p2p connections that generate
this error condition.

fixes: https://github.com/libp2p/js-libp2p-tcp/issues/93
fixes: https://github.com/ipfs/js-ipfs/issues/1447
2018-07-31 13:51:27 +02:00
6c36a46831 test: fixes listen-dial test "dial and destroy on listener" (#97) 2018-07-31 13:46:12 +02:00
d39ec2db40 chore: add lead maintainer (#94)
* chore: add lead maintainer

License: MIT
Signed-off-by: Jacob Heun <jacobheun@gmail.com>

* Update package.json
2018-06-26 17:58:30 +02:00
9 changed files with 175 additions and 24 deletions

View File

@ -26,4 +26,5 @@ build/Release
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
test
test
docs

View File

@ -1,3 +1,13 @@
<a name="0.12.1"></a>
## [0.12.1](https://github.com/libp2p/js-libp2p-tcp/compare/v0.12.0...v0.12.1) (2018-07-31)
### Bug Fixes
* invalid ip address and daemon can be crashed by remote user ([4b04b17](https://github.com/libp2p/js-libp2p-tcp/commit/4b04b17))
<a name="0.12.0"></a>
# [0.12.0](https://github.com/libp2p/js-libp2p-tcp/compare/v0.11.6...v0.12.0) (2018-04-05)

View File

@ -15,6 +15,10 @@
> JavaScript implementation of the TCP module for libp2p. It exposes the [interface-transport](https://github.com/libp2p/interface-connection) for dial/listen. `libp2p-tcp` is a very thin shim that adds support for dialing to a `multiaddr`. This small shim will enable libp2p to use other different transports.
## Lead Maintainer
[Jacob Heun](https://github.com/jacobheun)
## Table of Contents
- [Install](#install)

View File

@ -1,11 +1,12 @@
{
"name": "libp2p-tcp",
"version": "0.12.0",
"version": "0.12.1",
"description": "Node.js implementation of the TCP module that libp2p uses, which implements the interface-connection and interface-transport interfaces",
"leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
"main": "src/index.js",
"scripts": {
"lint": "aegir lint",
"test": "aegir test -t node",
"test": "aegir test -t node -f test/**/*.js",
"release": "aegir release -t node --no-build",
"release-minor": "aegir release -t node --type minor --no-build",
"release-major": "aegir-release -t node --type major --no-build",
@ -23,7 +24,6 @@
"keywords": [
"IPFS"
],
"author": "David Dias <daviddias@ipfs.io>",
"license": "MIT",
"bugs": {
"url": "https://github.com/libp2p/js-libp2p-tcp/issues"
@ -34,12 +34,11 @@
"npm": ">=3.0.0"
},
"devDependencies": {
"aegir": "^13.0.6",
"aegir": "^15.1.0",
"chai": "^4.1.2",
"dirty-chai": "^2.0.1",
"interface-transport": "~0.3.6",
"lodash.isfunction": "^3.0.9",
"pre-commit": "^1.2.2",
"pull-stream": "^3.6.7"
},
"dependencies": {
@ -63,10 +62,13 @@
"Evan Schwartz <evan.mark.schwartz@gmail.com>",
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
"Greenkeeper <support@greenkeeper.io>",
"Jacob Heun <jacobheun@gmail.com>",
"Jacob Heun <jake@andyet.net>",
"João Antunes <j.goncalo.antunes@gmail.com>",
"Pedro Teixeira <i@pgte.me>",
"Prashanth Chandra <coolshanth94@gmail.com>",
"Richard Littauer <richard.littauer@gmail.com>",
"Stephen Whitmore <stephen.whitmore@gmail.com>"
"Stephen Whitmore <stephen.whitmore@gmail.com>",
"TomCoded <tomtinkerer@gmail.com>"
]
}

View File

@ -2,27 +2,32 @@
const multiaddr = require('multiaddr')
const Address6 = require('ip-address').Address6
const debug = require('debug')
const log = debug('libp2p:tcp:get-multiaddr')
module.exports = (socket) => {
let ma
if (socket.remoteFamily === 'IPv6') {
const addr = new Address6(socket.remoteAddress)
try {
if (socket.remoteFamily === 'IPv6') {
const addr = new Address6(socket.remoteAddress)
if (addr.v4) {
const ip4 = addr.to4().correctForm()
ma = multiaddr('/ip4/' + ip4 +
'/tcp/' + socket.remotePort
)
if (addr.v4) {
const ip4 = addr.to4().correctForm()
ma = multiaddr('/ip4/' + ip4 +
'/tcp/' + socket.remotePort
)
} else {
ma = multiaddr('/ip6/' + socket.remoteAddress +
'/tcp/' + socket.remotePort
)
}
} else {
ma = multiaddr('/ip6/' + socket.remoteAddress +
'/tcp/' + socket.remotePort
)
ma = multiaddr('/ip4/' + socket.remoteAddress +
'/tcp/' + socket.remotePort)
}
} else {
ma = multiaddr('/ip4/' + socket.remoteAddress +
'/tcp/' + socket.remotePort)
} catch (err) {
log(err)
}
return ma
}

View File

@ -15,7 +15,18 @@ const createListener = require('./listener')
function noop () {}
/**
*
*/
class TCP {
/**
* Dial to another peer.
*
* @param {Multiaddr} ma - The address of the peer we want to dial to.
* @param {Object} [options={}]
* @param {function(Error?, Array<Multiaddr>?)} [callback]
* @returns {Connection}
*/
dial (ma, options, callback) {
if (isFunction(options)) {
callback = options
@ -52,6 +63,13 @@ class TCP {
return conn
}
/**
* Listen for incoming `TCP` connetions.
*
* @param {Object} [options={}]
* @param {function(Connection)} [handler] - Called with newly incomin connections.
* @returns {Listener}
*/
createListener (options, handler) {
if (isFunction(options)) {
handler = options
@ -63,6 +81,13 @@ class TCP {
return createListener(handler)
}
/**
* Filter a list of multiaddrs for those which contain
* valid `TCP` addresses.
*
* @param {Multiaddr|Array<Multiaddr>} multiaddrs
* @returns {Array<Multiaddr>}
*/
filter (multiaddrs) {
if (!Array.isArray(multiaddrs)) {
multiaddrs = [multiaddrs]

View File

@ -17,7 +17,49 @@ const CLOSE_TIMEOUT = 2000
function noop () {}
/**
* Listening for incoming connections.
*
* @event listening
* @instance
* @memberof Listener
*/
/**
* The server closes.
*
* @event close
* @instance
* @memberof Listener
*/
/**
* New connection established.
*
* @event connection
* @instance
* @type {Connection}
* @memberof Listener
*/
/**
* The underlying server encountered an error.
*
* @event error
* @instance
* @type {Error}
* @memberof Listener
*/
module.exports = (handler) => {
/**
* @alias Listener
* @type {Eventemitter}
* @fires Listener#listening
* @fires Listener#close
* @fires Listener#connection
* @fires Listener#error
*/
const listener = new EventEmitter()
const server = net.createServer((socket) => {
@ -25,6 +67,15 @@ module.exports = (handler) => {
socket.on('error', noop)
const addr = getMultiaddr(socket)
if (!addr) {
if (socket.remoteAddress === undefined) {
log('connection closed before p2p connection made')
} else {
log('error interpreting incoming p2p connection')
}
return
}
log('new connection', addr.toString())
const s = toPull.duplex(socket)

View File

@ -0,0 +1,54 @@
/* eslint-env mocha */
'use strict'
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const getMultiaddr = require('../src/get-multiaddr')
const goodSocket4 = {
remoteAddress: '127.0.0.1',
remotePort: '9090',
remoteFamily: 'IPv4'
}
const goodSocket6 = {
remoteAddress: '::1',
remotePort: '9090',
remoteFamily: 'IPv6'
}
const badSocket = {}
const badSocketData = {
remoteAddress: 'aewmrn4awoew',
remotePort: '234',
remoteFamily: 'Hufflepuff'
}
describe('getMultiaddr multiaddr creation', () => {
it('creates multiaddr from valid socket data', (done) => {
expect(getMultiaddr(goodSocket4))
.to.exist()
done()
})
it('creates multiaddr from valid IPv6 socket data', (done) => {
expect(getMultiaddr(goodSocket6))
.to.exist()
done()
})
it('returns undefined multiaddr from missing socket data', (done) => {
expect(getMultiaddr(badSocket))
.to.equal(undefined)
done()
})
it('returns undefined multiaddr from unparseable socket data', (done) => {
expect(getMultiaddr(badSocketData))
.to.equal(undefined)
done()
})
})

View File

@ -194,10 +194,9 @@ describe('dial', () => {
})
})
// TODO: figure out why is this failing
it.skip('dial and destroy on listener', (done) => {
it('dial and destroy on listener', (done) => {
let count = 0
const closed = ++count === 2 ? finish() : null
const closed = () => ++count === 2 ? finish() : null
const ma = multiaddr('/ip6/::/tcp/9067')