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

Commit d70d320

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

File tree

4 files changed

+428
-256
lines changed

4 files changed

+428
-256
lines changed

README.md

+10-25
Original file line numberDiff line numberDiff line change
@@ -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

src/index.js

+104-49
Original file line numberDiff line numberDiff line change
@@ -6,68 +6,144 @@ 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')
1112

1213
exports = module.exports = TCP
1314

1415
const IPFS_CODE = 421
15-
const CLOSE_TIMEOUT = 300
16+
const CLOSE_TIMEOUT = 2000
1617

1718
function TCP () {
1819
if (!(this instanceof TCP)) {
1920
return new TCP()
2021
}
2122

22-
const listeners = []
23-
24-
this.dial = function (multiaddr, options) {
23+
/*
24+
this.dial = function (ma, options) {
2525
if (!options) {
2626
options = {}
2727
}
2828
options.ready = options.ready || function noop () {}
29-
const conn = tcp.connect(multiaddr.toOptions(), options.ready)
29+
const conn = tcp.connect(ma.toOptions(), options.ready)
3030
conn.getObservedAddrs = () => {
3131
return [multiaddr]
3232
}
3333
return conn
3434
}
35+
*/
3536

36-
this.createListener = (multiaddrs, handler, callback) => {
37-
if (!Array.isArray(multiaddrs)) {
38-
multiaddrs = [multiaddrs]
37+
this.createListener = (options, handler) => {
38+
if (typeof options === 'function') {
39+
handler = options
40+
options = {}
3941
}
4042

41-
const freshMultiaddrs = []
43+
const listener = tcp.createServer((socket) => {
44+
// TODO update to Connection
45+
socket.getObservedAddrs = () => {
46+
return [getMultiaddr(socket)]
47+
}
48+
handler(socket)
49+
})
4250

43-
parallel(multiaddrs.map((m) => (cb) => {
44-
let ipfsHashId
45-
if (contains(m.protoNames(), 'ipfs')) {
46-
ipfsHashId = m.stringTuples().filter((tuple) => {
51+
let ipfsId
52+
let listeningMultiaddr
53+
54+
listener._listen = listener.listen
55+
listener.listen = (ma, callback) => {
56+
listeningMultiaddr = ma
57+
if (contains(ma.protoNames(), 'ipfs')) {
58+
ipfsId = ma.stringTuples().filter((tuple) => {
4759
if (tuple[0] === IPFS_CODE) {
4860
return true
4961
}
5062
})[0][1]
51-
m = m.decapsulate('ipfs')
63+
listeningMultiaddr = ma.decapsulate('ipfs')
5264
}
5365

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

61-
listener.__connections = {}
62-
listener.on('connection', (conn) => {
63-
const key = `${conn.remoteAddress}:${conn.remotePort}`
64-
listener.__connections[key] = conn
69+
listener._close = listener.close
70+
listener.close = (options, callback) => {
71+
if (typeof options === 'function') {
72+
callback = options
73+
options = {}
74+
}
75+
if (!callback) { callback = function noop () {} }
76+
if (!options) { options = {} }
6577

66-
conn.on('close', () => {
67-
delete listener.__connections[key]
78+
let closed = false
79+
listener._close(callback)
80+
listener.once('close', () => {
81+
closed = true
82+
})
83+
setTimeout(() => {
84+
if (closed) {
85+
return
86+
}
87+
log('unable to close graciously, destroying conns')
88+
Object.keys(listener.__connections).forEach((key) => {
89+
log('destroying %s', key)
90+
listener.__connections[key].destroy()
6891
})
92+
}, options.timeout || CLOSE_TIMEOUT)
93+
}
94+
95+
// Keep track of open connections to destroy in case of timeout
96+
listener.__connections = {}
97+
listener.on('connection', (socket) => {
98+
const key = `${socket.remoteAddress}:${socket.remotePort}`
99+
listener.__connections[key] = socket
100+
101+
socket.on('close', () => {
102+
delete listener.__connections[key]
69103
})
104+
})
70105

106+
listener.getAddrs = (callback) => {
107+
const multiaddrs = []
108+
const address = listener.address()
109+
110+
// Because TCP will only return the IPv6 version
111+
// we need to capture from the passed multiaddr
112+
if (listeningMultiaddr.toString().indexOf('ip4') !== -1) {
113+
let m = listeningMultiaddr.decapsulate('tcp')
114+
m = m.encapsulate('/tcp/' + address.port)
115+
if (ipfsId) {
116+
m = m.encapsulate('/ipfs/' + ipfsId)
117+
}
118+
119+
if (m.toString().indexOf('0.0.0.0') !== -1) {
120+
const netInterfaces = os.networkInterfaces()
121+
Object.keys(netInterfaces).forEach((niKey) => {
122+
netInterfaces[niKey].forEach((ni) => {
123+
if (ni.family === 'IPv4') {
124+
multiaddrs.push(multiaddr(m.toString().replace('0.0.0.0', ni.address)))
125+
}
126+
})
127+
})
128+
} else {
129+
multiaddrs.push(m)
130+
}
131+
}
132+
133+
if (address.family === 'IPv6') {
134+
let ma = multiaddr('/ip6/' + address.address + '/tcp/' + address.port)
135+
if (ipfsId) {
136+
ma = ma.encapsulate('/ipfs/' + ipfsId)
137+
}
138+
139+
multiaddrs.push(ma)
140+
}
141+
142+
callback(null, multiaddrs)
143+
}
144+
145+
return listener
146+
/*
71147
listener.listen(m.toOptions(), () => {
72148
// Node.js likes to convert addr to IPv6 (when 0.0.0.0 for e.g)
73149
const address = listener.address()
@@ -92,28 +168,7 @@ function TCP () {
92168
cb()
93169
})
94170
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)
171+
*/
117172
}
118173

119174
this.filter = (multiaddrs) => {

0 commit comments

Comments
 (0)