mirror of
https://github.com/fluencelabs/js-libp2p-tcp
synced 2025-07-03 20:51:33 +00:00
Compare commits
7 Commits
v0.12.0
...
api-docs-r
Author | SHA1 | Date | |
---|---|---|---|
3f61784be9 | |||
a0c23e49f7 | |||
66ab208182 | |||
168d111158 | |||
4b04b17dfa | |||
6c36a46831 | |||
d39ec2db40 |
@ -27,3 +27,4 @@ build/Release
|
|||||||
node_modules
|
node_modules
|
||||||
|
|
||||||
test
|
test
|
||||||
|
docs
|
10
CHANGELOG.md
10
CHANGELOG.md
@ -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>
|
<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)
|
# [0.12.0](https://github.com/libp2p/js-libp2p-tcp/compare/v0.11.6...v0.12.0) (2018-04-05)
|
||||||
|
|
||||||
|
@ -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.
|
> 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
|
## Table of Contents
|
||||||
|
|
||||||
- [Install](#install)
|
- [Install](#install)
|
||||||
|
14
package.json
14
package.json
@ -1,11 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "libp2p-tcp",
|
"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",
|
"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",
|
"main": "src/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"lint": "aegir lint",
|
"lint": "aegir lint",
|
||||||
"test": "aegir test -t node",
|
"test": "aegir test -t node -f test/**/*.js",
|
||||||
"release": "aegir release -t node --no-build",
|
"release": "aegir release -t node --no-build",
|
||||||
"release-minor": "aegir release -t node --type minor --no-build",
|
"release-minor": "aegir release -t node --type minor --no-build",
|
||||||
"release-major": "aegir-release -t node --type major --no-build",
|
"release-major": "aegir-release -t node --type major --no-build",
|
||||||
@ -23,7 +24,6 @@
|
|||||||
"keywords": [
|
"keywords": [
|
||||||
"IPFS"
|
"IPFS"
|
||||||
],
|
],
|
||||||
"author": "David Dias <daviddias@ipfs.io>",
|
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/libp2p/js-libp2p-tcp/issues"
|
"url": "https://github.com/libp2p/js-libp2p-tcp/issues"
|
||||||
@ -34,12 +34,11 @@
|
|||||||
"npm": ">=3.0.0"
|
"npm": ">=3.0.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"aegir": "^13.0.6",
|
"aegir": "^15.1.0",
|
||||||
"chai": "^4.1.2",
|
"chai": "^4.1.2",
|
||||||
"dirty-chai": "^2.0.1",
|
"dirty-chai": "^2.0.1",
|
||||||
"interface-transport": "~0.3.6",
|
"interface-transport": "~0.3.6",
|
||||||
"lodash.isfunction": "^3.0.9",
|
"lodash.isfunction": "^3.0.9",
|
||||||
"pre-commit": "^1.2.2",
|
|
||||||
"pull-stream": "^3.6.7"
|
"pull-stream": "^3.6.7"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
@ -63,10 +62,13 @@
|
|||||||
"Evan Schwartz <evan.mark.schwartz@gmail.com>",
|
"Evan Schwartz <evan.mark.schwartz@gmail.com>",
|
||||||
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
|
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
|
||||||
"Greenkeeper <support@greenkeeper.io>",
|
"Greenkeeper <support@greenkeeper.io>",
|
||||||
|
"Jacob Heun <jacobheun@gmail.com>",
|
||||||
|
"Jacob Heun <jake@andyet.net>",
|
||||||
"João Antunes <j.goncalo.antunes@gmail.com>",
|
"João Antunes <j.goncalo.antunes@gmail.com>",
|
||||||
"Pedro Teixeira <i@pgte.me>",
|
"Pedro Teixeira <i@pgte.me>",
|
||||||
"Prashanth Chandra <coolshanth94@gmail.com>",
|
"Prashanth Chandra <coolshanth94@gmail.com>",
|
||||||
"Richard Littauer <richard.littauer@gmail.com>",
|
"Richard Littauer <richard.littauer@gmail.com>",
|
||||||
"Stephen Whitmore <stephen.whitmore@gmail.com>"
|
"Stephen Whitmore <stephen.whitmore@gmail.com>",
|
||||||
|
"TomCoded <tomtinkerer@gmail.com>"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -2,10 +2,13 @@
|
|||||||
|
|
||||||
const multiaddr = require('multiaddr')
|
const multiaddr = require('multiaddr')
|
||||||
const Address6 = require('ip-address').Address6
|
const Address6 = require('ip-address').Address6
|
||||||
|
const debug = require('debug')
|
||||||
|
const log = debug('libp2p:tcp:get-multiaddr')
|
||||||
|
|
||||||
module.exports = (socket) => {
|
module.exports = (socket) => {
|
||||||
let ma
|
let ma
|
||||||
|
|
||||||
|
try {
|
||||||
if (socket.remoteFamily === 'IPv6') {
|
if (socket.remoteFamily === 'IPv6') {
|
||||||
const addr = new Address6(socket.remoteAddress)
|
const addr = new Address6(socket.remoteAddress)
|
||||||
|
|
||||||
@ -23,6 +26,8 @@ module.exports = (socket) => {
|
|||||||
ma = multiaddr('/ip4/' + socket.remoteAddress +
|
ma = multiaddr('/ip4/' + socket.remoteAddress +
|
||||||
'/tcp/' + socket.remotePort)
|
'/tcp/' + socket.remotePort)
|
||||||
}
|
}
|
||||||
|
} catch (err) {
|
||||||
|
log(err)
|
||||||
|
}
|
||||||
return ma
|
return ma
|
||||||
}
|
}
|
||||||
|
25
src/index.js
25
src/index.js
@ -15,7 +15,18 @@ const createListener = require('./listener')
|
|||||||
|
|
||||||
function noop () {}
|
function noop () {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
class TCP {
|
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) {
|
dial (ma, options, callback) {
|
||||||
if (isFunction(options)) {
|
if (isFunction(options)) {
|
||||||
callback = options
|
callback = options
|
||||||
@ -52,6 +63,13 @@ class TCP {
|
|||||||
return conn
|
return conn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Listen for incoming `TCP` connetions.
|
||||||
|
*
|
||||||
|
* @param {Object} [options={}]
|
||||||
|
* @param {function(Connection)} [handler] - Called with newly incomin connections.
|
||||||
|
* @returns {Listener}
|
||||||
|
*/
|
||||||
createListener (options, handler) {
|
createListener (options, handler) {
|
||||||
if (isFunction(options)) {
|
if (isFunction(options)) {
|
||||||
handler = options
|
handler = options
|
||||||
@ -63,6 +81,13 @@ class TCP {
|
|||||||
return createListener(handler)
|
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) {
|
filter (multiaddrs) {
|
||||||
if (!Array.isArray(multiaddrs)) {
|
if (!Array.isArray(multiaddrs)) {
|
||||||
multiaddrs = [multiaddrs]
|
multiaddrs = [multiaddrs]
|
||||||
|
@ -17,7 +17,49 @@ const CLOSE_TIMEOUT = 2000
|
|||||||
|
|
||||||
function noop () {}
|
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) => {
|
module.exports = (handler) => {
|
||||||
|
/**
|
||||||
|
* @alias Listener
|
||||||
|
* @type {Eventemitter}
|
||||||
|
* @fires Listener#listening
|
||||||
|
* @fires Listener#close
|
||||||
|
* @fires Listener#connection
|
||||||
|
* @fires Listener#error
|
||||||
|
*/
|
||||||
const listener = new EventEmitter()
|
const listener = new EventEmitter()
|
||||||
|
|
||||||
const server = net.createServer((socket) => {
|
const server = net.createServer((socket) => {
|
||||||
@ -25,6 +67,15 @@ module.exports = (handler) => {
|
|||||||
socket.on('error', noop)
|
socket.on('error', noop)
|
||||||
|
|
||||||
const addr = getMultiaddr(socket)
|
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())
|
log('new connection', addr.toString())
|
||||||
|
|
||||||
const s = toPull.duplex(socket)
|
const s = toPull.duplex(socket)
|
||||||
|
54
test/get-multiaddr.spec.js
Normal file
54
test/get-multiaddr.spec.js
Normal 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()
|
||||||
|
})
|
||||||
|
})
|
@ -194,10 +194,9 @@ describe('dial', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO: figure out why is this failing
|
it('dial and destroy on listener', (done) => {
|
||||||
it.skip('dial and destroy on listener', (done) => {
|
|
||||||
let count = 0
|
let count = 0
|
||||||
const closed = ++count === 2 ? finish() : null
|
const closed = () => ++count === 2 ? finish() : null
|
||||||
|
|
||||||
const ma = multiaddr('/ip6/::/tcp/9067')
|
const ma = multiaddr('/ip6/::/tcp/9067')
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user