Skip to content
This repository was archived by the owner on Aug 29, 2023. It is now read-only.

Commit 539a007

Browse files
committed
comply with the latest interface-transport and interface-connection spec
1 parent 730a477 commit 539a007

File tree

5 files changed

+629
-274
lines changed

5 files changed

+629
-274
lines changed

README.md

+15-30
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ js-libp2p-tcp
33

44
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
55
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
6-
[![Build Status](https://travis-ci.org/diasdavid/js-libp2p-tcp.svg?style=flat-square)](https://travis-ci.org/diasdavid/js-libp2p-tcp)
6+
[![Build Status](https://travis-ci.org/libp2p/js-libp2p-tcp.svg?style=flat-square)](https://travis-ci.org/libp2p/js-libp2p-tcp)
77
![](https://img.shields.io/badge/coverage-%3F-yellow.svg?style=flat-square)
8-
[![Dependency Status](https://david-dm.org/diasdavid/js-libp2p-tcp.svg?style=flat-square)](https://david-dm.org/diasdavid/js-libp2p-tcp)
8+
[![Dependency Status](https://david-dm.org/libp2p/js-libp2p-tcp.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-tcp)
99
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
1010

11-
![](https://raw.githubusercontent.com/diasdavid/abstract-connection/master/img/badge.png)
12-
![](https://raw.githubusercontent.com/diasdavid/abstract-transport/master/img/badge.png)
11+
![](https://raw.githubusercontent.com/libp2p/interface-connection/master/img/badge.png)
12+
![](https://raw.githubusercontent.com/libp2p/interface-transport/master/img/badge.png)
1313

1414
> Node.js implementation of the TCP module that libp2p uses, which implements
15-
> the [interface-connection](https://github.com/diasdavid/interface-connection)
15+
> the [interface-connection](https://github.com/libp2p/interface-connection)
1616
> interface for dial/listen.
1717
1818
## Description
@@ -24,18 +24,20 @@ transports.
2424
## Example
2525

2626
```js
27-
const Tcp = require('libp2p-tcp')
27+
const TCP = require('libp2p-tcp')
2828
const multiaddr = require('multiaddr')
2929

3030
const mh1 = multiaddr('/ip4/127.0.0.1/tcp/9090')
3131
const mh2 = multiaddr('/ip6/::/tcp/9092')
3232

3333
const tcp = new Tcp()
3434

35-
tcp.createListener([mh1, mh2], function handler (socket) {
35+
var listener = tcp.createListener(mh1, function handler (socket) {
3636
console.log('connection')
3737
socket.end('bye')
38-
}, function ready () {
38+
})
39+
40+
var listener.listen(function ready () {
3941
console.log('ready')
4042

4143
const client = tcp.dial(mh1)
@@ -65,31 +67,14 @@ bye
6567

6668
## API
6769

68-
```js
69-
const Tcp = require('libp2p-tcp')
70-
```
71-
72-
### var tcp = new Tcp()
73-
74-
Creates a new TCP object. This does nothing on its own, but provides access to
75-
`dial` and `createListener`.
76-
77-
### tcp.createListener(multiaddrs, handler, ready)
78-
79-
Creates TCP servers that listen on the addresses described in the array
80-
`multiaddrs`. Each connection will call `handler` with a connection stream.
81-
`ready` is called once all servers are listening.
82-
83-
### tcp.dial(multiaddr, options={})
70+
[![](https://raw.githubusercontent.com/diasdavid/interface-transport/master/img/badge.png)](https://github.com/diasdavid/interface-transport)
8471

85-
Connects to the multiaddress `multiaddr` using TCP, returning the socket stream.
86-
If `options.ready` is set to a function, it is called when a connection is
87-
established.
72+
`libp2p-tcp` accepts TCP addresses both IPFS and non IPFS encapsulated addresses, i.e:
8873

89-
### tcp.close(callback)
74+
`/ip4/127.0.0.1/tcp/4001`
75+
`/ip4/127.0.0.1/tcp/4001/ipfs/QmHash`
9076

91-
Closes all the listening TCP servers, calling `callback` once all of them have
92-
been shut down.
77+
Both for dialing and listening.
9378

9479
## License
9580

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@
3434
"devDependencies": {
3535
"aegir": "^3.0.4",
3636
"chai": "^3.5.0",
37-
"interface-connection": "0.0.3",
38-
"interface-transport": "^0.1.1",
37+
"interface-transport": "^0.2.0",
3938
"pre-commit": "^1.1.2",
4039
"tape": "^4.5.1"
4140
},
4241
"dependencies": {
42+
"duplexify": "^3.4.3",
43+
"interface-connection": "0.1.2",
4344
"ip-address": "^5.8.0",
4445
"lodash.contains": "^2.4.3",
4546
"mafmt": "^2.1.0",
@@ -53,4 +54,4 @@
5354
"Stephen Whitmore <[email protected]>",
5455
"dignifiedquire <[email protected]>"
5556
]
56-
}
57+
}

src/index.js

+136-59
Original file line numberDiff line numberDiff line change
@@ -6,68 +6,166 @@ const tcp = require('net')
66
const multiaddr = require('multiaddr')
77
const Address6 = require('ip-address').Address6
88
const mafmt = require('mafmt')
9-
const parallel = require('run-parallel')
9+
// const parallel = require('run-parallel')
1010
const contains = require('lodash.contains')
11+
const os = require('os')
12+
const Connection = require('interface-connection').Connection
1113

1214
exports = module.exports = TCP
1315

1416
const IPFS_CODE = 421
15-
const CLOSE_TIMEOUT = 300
17+
const CLOSE_TIMEOUT = 2000
1618

1719
function TCP () {
1820
if (!(this instanceof TCP)) {
1921
return new TCP()
2022
}
2123

22-
const listeners = []
23-
24-
this.dial = function (multiaddr, options) {
25-
if (!options) {
24+
this.dial = function (ma, options, callback) {
25+
if (typeof options === 'function') {
26+
callback = options
2627
options = {}
2728
}
28-
options.ready = options.ready || function noop () {}
29-
const conn = tcp.connect(multiaddr.toOptions(), options.ready)
30-
conn.getObservedAddrs = () => {
31-
return [multiaddr]
29+
30+
if (!callback) {
31+
callback = function noop () {}
32+
}
33+
34+
const socket = tcp.connect(ma.toOptions())
35+
const conn = new Connection(socket)
36+
37+
socket.on('timeout', () => {
38+
conn.emit('timeout')
39+
})
40+
41+
socket.on('error', (err) => {
42+
callback(err)
43+
conn.emit('error', err)
44+
})
45+
46+
socket.on('connect', () => {
47+
callback(null, conn)
48+
conn.emit('connect')
49+
})
50+
51+
conn.getObservedAddrs = (cb) => {
52+
return cb(null, [ma])
3253
}
54+
3355
return conn
3456
}
3557

36-
this.createListener = (multiaddrs, handler, callback) => {
37-
if (!Array.isArray(multiaddrs)) {
38-
multiaddrs = [multiaddrs]
58+
this.createListener = (options, handler) => {
59+
if (typeof options === 'function') {
60+
handler = options
61+
options = {}
3962
}
4063

41-
const freshMultiaddrs = []
64+
const listener = tcp.createServer((socket) => {
65+
const conn = new Connection(socket)
66+
67+
conn.getObservedAddrs = (cb) => {
68+
return cb(null, [getMultiaddr(socket)])
69+
}
70+
handler(conn)
71+
})
4272

43-
parallel(multiaddrs.map((m) => (cb) => {
44-
let ipfsHashId
45-
if (contains(m.protoNames(), 'ipfs')) {
46-
ipfsHashId = m.stringTuples().filter((tuple) => {
73+
let ipfsId
74+
let listeningMultiaddr
75+
76+
listener._listen = listener.listen
77+
listener.listen = (ma, callback) => {
78+
listeningMultiaddr = ma
79+
if (contains(ma.protoNames(), 'ipfs')) {
80+
ipfsId = ma.stringTuples().filter((tuple) => {
4781
if (tuple[0] === IPFS_CODE) {
4882
return true
4983
}
5084
})[0][1]
51-
m = m.decapsulate('ipfs')
85+
listeningMultiaddr = ma.decapsulate('ipfs')
5286
}
5387

54-
const listener = tcp.createServer((conn) => {
55-
conn.getObservedAddrs = () => {
56-
return [getMultiaddr(conn)]
57-
}
58-
handler(conn)
59-
})
88+
listener._listen(listeningMultiaddr.toOptions(), callback)
89+
}
6090

61-
listener.__connections = {}
62-
listener.on('connection', (conn) => {
63-
const key = `${conn.remoteAddress}:${conn.remotePort}`
64-
listener.__connections[key] = conn
91+
listener._close = listener.close
92+
listener.close = (options, callback) => {
93+
if (typeof options === 'function') {
94+
callback = options
95+
options = {}
96+
}
97+
if (!callback) { callback = function noop () {} }
98+
if (!options) { options = {} }
6599

66-
conn.on('close', () => {
67-
delete listener.__connections[key]
100+
let closed = false
101+
listener._close(callback)
102+
listener.once('close', () => {
103+
closed = true
104+
})
105+
setTimeout(() => {
106+
if (closed) {
107+
return
108+
}
109+
log('unable to close graciously, destroying conns')
110+
Object.keys(listener.__connections).forEach((key) => {
111+
log('destroying %s', key)
112+
listener.__connections[key].destroy()
68113
})
114+
}, options.timeout || CLOSE_TIMEOUT)
115+
}
116+
117+
// Keep track of open connections to destroy in case of timeout
118+
listener.__connections = {}
119+
listener.on('connection', (socket) => {
120+
const key = `${socket.remoteAddress}:${socket.remotePort}`
121+
listener.__connections[key] = socket
122+
123+
socket.on('close', () => {
124+
delete listener.__connections[key]
69125
})
126+
})
127+
128+
listener.getAddrs = (callback) => {
129+
const multiaddrs = []
130+
const address = listener.address()
131+
132+
// Because TCP will only return the IPv6 version
133+
// we need to capture from the passed multiaddr
134+
if (listeningMultiaddr.toString().indexOf('ip4') !== -1) {
135+
let m = listeningMultiaddr.decapsulate('tcp')
136+
m = m.encapsulate('/tcp/' + address.port)
137+
if (ipfsId) {
138+
m = m.encapsulate('/ipfs/' + ipfsId)
139+
}
140+
141+
if (m.toString().indexOf('0.0.0.0') !== -1) {
142+
const netInterfaces = os.networkInterfaces()
143+
Object.keys(netInterfaces).forEach((niKey) => {
144+
netInterfaces[niKey].forEach((ni) => {
145+
if (ni.family === 'IPv4') {
146+
multiaddrs.push(multiaddr(m.toString().replace('0.0.0.0', ni.address)))
147+
}
148+
})
149+
})
150+
} else {
151+
multiaddrs.push(m)
152+
}
153+
}
154+
155+
if (address.family === 'IPv6') {
156+
let ma = multiaddr('/ip6/' + address.address + '/tcp/' + address.port)
157+
if (ipfsId) {
158+
ma = ma.encapsulate('/ipfs/' + ipfsId)
159+
}
160+
161+
multiaddrs.push(ma)
162+
}
163+
164+
callback(null, multiaddrs)
165+
}
70166

167+
return listener
168+
/*
71169
listener.listen(m.toOptions(), () => {
72170
// Node.js likes to convert addr to IPv6 (when 0.0.0.0 for e.g)
73171
const address = listener.address()
@@ -92,28 +190,7 @@ function TCP () {
92190
cb()
93191
})
94192
listeners.push(listener)
95-
}), (err) => {
96-
callback(err, freshMultiaddrs)
97-
})
98-
}
99-
100-
this.close = (callback) => {
101-
log('closing')
102-
if (listeners.length === 0) {
103-
log('Called close with no active listeners')
104-
return callback()
105-
}
106-
107-
parallel(listeners.map((listener) => (cb) => {
108-
setTimeout(() => {
109-
Object.keys(listener.__connections).forEach((key) => {
110-
log('destroying %s', key)
111-
listener.__connections[key].destroy()
112-
})
113-
}, CLOSE_TIMEOUT)
114-
115-
listener.close(cb)
116-
}), callback)
193+
*/
117194
}
118195

119196
this.filter = (multiaddrs) => {
@@ -129,19 +206,19 @@ function TCP () {
129206
}
130207
}
131208

132-
function getMultiaddr (conn) {
209+
function getMultiaddr (socket) {
133210
var mh
134211

135-
if (conn.remoteFamily === 'IPv6') {
136-
var addr = new Address6(conn.remoteAddress)
212+
if (socket.remoteFamily === 'IPv6') {
213+
var addr = new Address6(socket.remoteAddress)
137214
if (addr.v4) {
138215
var ip4 = addr.to4().correctForm()
139-
mh = multiaddr('/ip4/' + ip4 + '/tcp/' + conn.remotePort)
216+
mh = multiaddr('/ip4/' + ip4 + '/tcp/' + socket.remotePort)
140217
} else {
141-
mh = multiaddr('/ip6/' + conn.remoteAddress + '/tcp/' + conn.remotePort)
218+
mh = multiaddr('/ip6/' + socket.remoteAddress + '/tcp/' + socket.remotePort)
142219
}
143220
} else {
144-
mh = multiaddr('/ip4/' + conn.remoteAddress + '/tcp/' + conn.remotePort)
221+
mh = multiaddr('/ip4/' + socket.remoteAddress + '/tcp/' + socket.remotePort)
145222
}
146223

147224
return mh

0 commit comments

Comments
 (0)