mirror of
https://github.com/fluencelabs/js-libp2p-tcp
synced 2025-07-02 04:41:45 +00:00
Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
541ec1fdda | |||
c3b5c132c0 | |||
4499bba514 | |||
9d778b56dd | |||
4769958b8f | |||
4d34757552 | |||
77600d3a8b |
@ -1,11 +1,16 @@
|
|||||||
js-libp2p-tcp
|
js-libp2p-tcp
|
||||||
===============
|
===============
|
||||||
|
|
||||||
[](http://ipn.io) [[](http://webchat.freenode.net/?channels=%23ipfs) ](https://travis-ci.org/diasdavid/js-libp2p-tcp)  [](https://david-dm.org/diasdavid/js-libp2p-tcp) [](https://github.com/feross/standard)
|
[](http://ipn.io)
|
||||||
|
[](http://webchat.freenode.net/?channels=%23ipfs)
|
||||||
|
[](https://travis-ci.org/diasdavid/js-libp2p-tcp)
|
||||||
|

|
||||||
|
[](https://david-dm.org/diasdavid/js-libp2p-tcp)
|
||||||
|
[](https://github.com/feross/standard)
|
||||||
|
|
||||||

|

|
||||||

|

|
||||||
|
|
||||||
> Node.js implementation of the TCP module that libp2p uses, which implements the [abstract-connection]() interface for dial/listen.
|
> Node.js implementation of the TCP module that libp2p uses, which implements the [interface-connection]() interface for dial/listen.
|
||||||
|
|
||||||
note: libp2p-tcp in Node.js is a very thin shim that adds the support to dial to a `multiaddr`. This small shim will enable libp2p to use other different transports.
|
note: libp2p-tcp in Node.js is a very thin shim that adds the support to dial to a `multiaddr`. This small shim will enable libp2p to use other different transports.
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "libp2p-tcp",
|
"name": "libp2p-tcp",
|
||||||
"version": "0.2.1",
|
"version": "0.4.0",
|
||||||
"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",
|
||||||
"main": "src/index.js",
|
"main": "src/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@ -34,12 +34,13 @@
|
|||||||
"interface-transport": "^0.1.1",
|
"interface-transport": "^0.1.1",
|
||||||
"istanbul": "^0.4.2",
|
"istanbul": "^0.4.2",
|
||||||
"mocha": "^2.4.5",
|
"mocha": "^2.4.5",
|
||||||
"multiaddr": "^1.1.1",
|
|
||||||
"pre-commit": "^1.1.2",
|
"pre-commit": "^1.1.2",
|
||||||
"standard": "^6.0.7",
|
"standard": "^6.0.7",
|
||||||
"tape": "^4.2.0"
|
"tape": "^4.2.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"multiaddr": "^1.0.0"
|
"ip-address": "^5.8.0",
|
||||||
|
"mafmt": "^1.0.1",
|
||||||
|
"multiaddr": "^1.1.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
41
src/index.js
41
src/index.js
@ -2,6 +2,8 @@
|
|||||||
// const log = debug('libp2p:tcp')
|
// const log = debug('libp2p:tcp')
|
||||||
const tcp = require('net')
|
const tcp = require('net')
|
||||||
const multiaddr = require('multiaddr')
|
const multiaddr = require('multiaddr')
|
||||||
|
const Address6 = require('ip-address').Address6
|
||||||
|
const mafmt = require('mafmt')
|
||||||
|
|
||||||
exports = module.exports = TCP
|
exports = module.exports = TCP
|
||||||
|
|
||||||
@ -17,7 +19,11 @@ function TCP () {
|
|||||||
options = {}
|
options = {}
|
||||||
}
|
}
|
||||||
options.ready = options.ready || function noop () {}
|
options.ready = options.ready || function noop () {}
|
||||||
return tcp.connect(multiaddr.toOptions(), options.ready)
|
const conn = tcp.connect(multiaddr.toOptions(), options.ready)
|
||||||
|
conn.getObservedAddrs = () => {
|
||||||
|
return [multiaddr]
|
||||||
|
}
|
||||||
|
return conn
|
||||||
}
|
}
|
||||||
|
|
||||||
this.createListener = (multiaddrs, options, handler, callback) => {
|
this.createListener = (multiaddrs, options, handler, callback) => {
|
||||||
@ -35,7 +41,12 @@ function TCP () {
|
|||||||
const freshMultiaddrs = []
|
const freshMultiaddrs = []
|
||||||
|
|
||||||
multiaddrs.forEach((m) => {
|
multiaddrs.forEach((m) => {
|
||||||
const listener = tcp.createServer(handler)
|
const listener = tcp.createServer((conn) => {
|
||||||
|
conn.getObservedAddrs = () => {
|
||||||
|
return [getMultiaddr(conn)]
|
||||||
|
}
|
||||||
|
handler(conn)
|
||||||
|
})
|
||||||
listener.listen(m.toOptions(), () => {
|
listener.listen(m.toOptions(), () => {
|
||||||
// Node.js likes to convert addr to IPv6 (when 0.0.0.0 for e.g)
|
// Node.js likes to convert addr to IPv6 (when 0.0.0.0 for e.g)
|
||||||
const address = listener.address()
|
const address = listener.address()
|
||||||
@ -69,5 +80,31 @@ function TCP () {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.filter = (multiaddrs) => {
|
||||||
|
if (!Array.isArray(multiaddrs)) {
|
||||||
|
multiaddrs = [multiaddrs]
|
||||||
|
}
|
||||||
|
return multiaddrs.filter((ma) => {
|
||||||
|
return mafmt.TCP.matches(ma)
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getMultiaddr (conn) {
|
||||||
|
var mh
|
||||||
|
|
||||||
|
if (conn.remoteFamily === 'IPv6') {
|
||||||
|
var addr = new Address6(conn.remoteAddress)
|
||||||
|
if (addr.v4) {
|
||||||
|
var ip4 = addr.to4().correctForm()
|
||||||
|
mh = multiaddr('/ip4/' + ip4 + '/tcp/' + conn.remotePort)
|
||||||
|
} else {
|
||||||
|
mh = multiaddr('/ip6/' + conn.remoteAddress + '/tcp/' + conn.remotePort)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
mh = multiaddr('/ip4/' + conn.remoteAddress + '/tcp/' + conn.remotePort)
|
||||||
|
}
|
||||||
|
|
||||||
|
return mh
|
||||||
|
}
|
||||||
|
@ -53,5 +53,40 @@ describe('libp2p-tcp', function () {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('get observed addrs', (done) => {
|
||||||
|
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
||||||
|
var dialerObsAddrs
|
||||||
|
var listenerObsAddrs
|
||||||
|
|
||||||
|
tcp.createListener(mh, (conn) => {
|
||||||
|
expect(conn).to.exist
|
||||||
|
dialerObsAddrs = conn.getObservedAddrs()
|
||||||
|
conn.end()
|
||||||
|
}, () => {
|
||||||
|
const conn = tcp.dial(mh)
|
||||||
|
conn.on('end', () => {
|
||||||
|
listenerObsAddrs = conn.getObservedAddrs()
|
||||||
|
conn.end()
|
||||||
|
|
||||||
|
tcp.close(() => {
|
||||||
|
expect(listenerObsAddrs[0]).to.deep.equal(mh)
|
||||||
|
expect(dialerObsAddrs.length).to.equal(1)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('filter valid addrs for this transport', (done) => {
|
||||||
|
const mh1 = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
||||||
|
const mh2 = multiaddr('/ip4/127.0.0.1/udp/9090')
|
||||||
|
const mh3 = multiaddr('/ip4/127.0.0.1/tcp/9090/http')
|
||||||
|
|
||||||
|
const valid = tcp.filter([mh1, mh2, mh3])
|
||||||
|
expect(valid.length).to.equal(1)
|
||||||
|
expect(valid[0]).to.deep.equal(mh1)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
|
||||||
it.skip('listen on IPv6', (done) => {})
|
it.skip('listen on IPv6', (done) => {})
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user