mirror of
https://github.com/fluencelabs/js-libp2p-tcp
synced 2025-07-06 14:41:43 +00:00
Compare commits
2 Commits
fix/p2p-pr
...
fix/close
Author | SHA1 | Date | |
---|---|---|---|
270ac46090 | |||
1cd0066755 |
@ -34,11 +34,10 @@
|
|||||||
"npm": ">=3.0.0"
|
"npm": ">=3.0.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"aegir": "^17.1.1",
|
"aegir": "^17.0.1",
|
||||||
"chai": "^4.2.0",
|
"chai": "^4.2.0",
|
||||||
"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",
|
|
||||||
"pull-stream": "^3.6.9"
|
"pull-stream": "^3.6.9"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
@ -46,10 +45,8 @@
|
|||||||
"debug": "^4.1.0",
|
"debug": "^4.1.0",
|
||||||
"interface-connection": "~0.3.2",
|
"interface-connection": "~0.3.2",
|
||||||
"ip-address": "^5.8.9",
|
"ip-address": "^5.8.9",
|
||||||
"lodash.includes": "^4.3.0",
|
"mafmt": "^6.0.2",
|
||||||
"lodash.isfunction": "^3.0.9",
|
"multiaddr": "^5.0.2",
|
||||||
"mafmt": "^6.0.3",
|
|
||||||
"multiaddr": "^6.0.1",
|
|
||||||
"once": "^1.4.0",
|
"once": "^1.4.0",
|
||||||
"stream-to-pull-stream": "^1.7.2"
|
"stream-to-pull-stream": "^1.7.2"
|
||||||
},
|
},
|
||||||
|
12
src/index.js
12
src/index.js
@ -4,8 +4,6 @@ const net = require('net')
|
|||||||
const toPull = require('stream-to-pull-stream')
|
const toPull = require('stream-to-pull-stream')
|
||||||
const mafmt = require('mafmt')
|
const mafmt = require('mafmt')
|
||||||
const withIs = require('class-is')
|
const withIs = require('class-is')
|
||||||
const includes = require('lodash.includes')
|
|
||||||
const isFunction = require('lodash.isfunction')
|
|
||||||
const Connection = require('interface-connection').Connection
|
const Connection = require('interface-connection').Connection
|
||||||
const once = require('once')
|
const once = require('once')
|
||||||
const debug = require('debug')
|
const debug = require('debug')
|
||||||
@ -17,7 +15,7 @@ function noop () {}
|
|||||||
|
|
||||||
class TCP {
|
class TCP {
|
||||||
dial (ma, options, callback) {
|
dial (ma, options, callback) {
|
||||||
if (isFunction(options)) {
|
if (typeof options === 'function') {
|
||||||
callback = options
|
callback = options
|
||||||
options = {}
|
options = {}
|
||||||
}
|
}
|
||||||
@ -53,7 +51,7 @@ class TCP {
|
|||||||
}
|
}
|
||||||
|
|
||||||
createListener (options, handler) {
|
createListener (options, handler) {
|
||||||
if (isFunction(options)) {
|
if (typeof options === 'function') {
|
||||||
handler = options
|
handler = options
|
||||||
options = {}
|
options = {}
|
||||||
}
|
}
|
||||||
@ -69,12 +67,12 @@ class TCP {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return multiaddrs.filter((ma) => {
|
return multiaddrs.filter((ma) => {
|
||||||
if (includes(ma.protoNames(), 'p2p-circuit')) {
|
if (ma.protoNames().includes('p2p-circuit')) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ma.protoNames().includes('p2p')) {
|
if (ma.protoNames().includes('ipfs')) {
|
||||||
ma = ma.decapsulate('p2p')
|
ma = ma.decapsulate('ipfs')
|
||||||
}
|
}
|
||||||
|
|
||||||
return mafmt.TCP.matches(ma)
|
return mafmt.TCP.matches(ma)
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
const multiaddr = require('multiaddr')
|
const multiaddr = require('multiaddr')
|
||||||
const Connection = require('interface-connection').Connection
|
const Connection = require('interface-connection').Connection
|
||||||
const os = require('os')
|
const os = require('os')
|
||||||
const includes = require('lodash.includes')
|
|
||||||
const net = require('net')
|
const net = require('net')
|
||||||
const toPull = require('stream-to-pull-stream')
|
const toPull = require('stream-to-pull-stream')
|
||||||
const EventEmitter = require('events').EventEmitter
|
const EventEmitter = require('events').EventEmitter
|
||||||
@ -12,7 +11,7 @@ const log = debug('libp2p:tcp:listen')
|
|||||||
|
|
||||||
const getMultiaddr = require('./get-multiaddr')
|
const getMultiaddr = require('./get-multiaddr')
|
||||||
|
|
||||||
const P2P_CODE = 421
|
const IPFS_CODE = 421
|
||||||
const CLOSE_TIMEOUT = 2000
|
const CLOSE_TIMEOUT = 2000
|
||||||
|
|
||||||
function noop () {}
|
function noop () {}
|
||||||
@ -42,8 +41,6 @@ module.exports = (handler) => {
|
|||||||
cb(null, [addr])
|
cb(null, [addr])
|
||||||
}
|
}
|
||||||
|
|
||||||
trackSocket(server, socket)
|
|
||||||
|
|
||||||
const conn = new Connection(s)
|
const conn = new Connection(s)
|
||||||
handler(conn)
|
handler(conn)
|
||||||
listener.emit('connection', conn)
|
listener.emit('connection', conn)
|
||||||
@ -52,9 +49,17 @@ module.exports = (handler) => {
|
|||||||
server.on('listening', () => listener.emit('listening'))
|
server.on('listening', () => listener.emit('listening'))
|
||||||
server.on('error', (err) => listener.emit('error', err))
|
server.on('error', (err) => listener.emit('error', err))
|
||||||
server.on('close', () => listener.emit('close'))
|
server.on('close', () => listener.emit('close'))
|
||||||
|
server.on('connection', (conn) => {
|
||||||
|
server.__connections.push(conn)
|
||||||
|
conn.once('close', () => {
|
||||||
|
server.__connections = server.__connections.filter((c) => {
|
||||||
|
return conn !== c
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
// Keep track of open connections to destroy in case of timeout
|
// Keep track of open connections to destroy in case of timeout
|
||||||
server.__connections = {}
|
server.__connections = []
|
||||||
|
|
||||||
listener.close = (options, callback) => {
|
listener.close = (options, callback) => {
|
||||||
if (typeof options === 'function') {
|
if (typeof options === 'function') {
|
||||||
@ -66,9 +71,9 @@ module.exports = (handler) => {
|
|||||||
|
|
||||||
const timeout = setTimeout(() => {
|
const timeout = setTimeout(() => {
|
||||||
log('unable to close graciously, destroying conns')
|
log('unable to close graciously, destroying conns')
|
||||||
Object.keys(server.__connections).forEach((key) => {
|
server.__connections.forEach((conn) => {
|
||||||
log('destroying %s', key)
|
log('destroying %s', `${conn.remoteAddress}:${conn.remotePort}`)
|
||||||
server.__connections[key].destroy()
|
conn.destroy()
|
||||||
})
|
})
|
||||||
}, options.timeout || CLOSE_TIMEOUT)
|
}, options.timeout || CLOSE_TIMEOUT)
|
||||||
|
|
||||||
@ -79,14 +84,14 @@ module.exports = (handler) => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
let p2pId
|
let ipfsId
|
||||||
let listeningAddr
|
let listeningAddr
|
||||||
|
|
||||||
listener.listen = (ma, callback) => {
|
listener.listen = (ma, callback) => {
|
||||||
listeningAddr = ma
|
listeningAddr = ma
|
||||||
if (includes(ma.protoNames(), 'p2p')) {
|
if (ma.protoNames().includes('ipfs')) {
|
||||||
p2pId = getp2pId(ma)
|
ipfsId = getIpfsId(ma)
|
||||||
listeningAddr = ma.decapsulate('p2p')
|
listeningAddr = ma.decapsulate('ipfs')
|
||||||
}
|
}
|
||||||
|
|
||||||
const lOpts = listeningAddr.toOptions()
|
const lOpts = listeningAddr.toOptions()
|
||||||
@ -107,8 +112,8 @@ module.exports = (handler) => {
|
|||||||
if (listeningAddr.toString().indexOf('ip4') !== -1) {
|
if (listeningAddr.toString().indexOf('ip4') !== -1) {
|
||||||
let m = listeningAddr.decapsulate('tcp')
|
let m = listeningAddr.decapsulate('tcp')
|
||||||
m = m.encapsulate('/tcp/' + address.port)
|
m = m.encapsulate('/tcp/' + address.port)
|
||||||
if (p2pId) {
|
if (ipfsId) {
|
||||||
m = m.encapsulate('/p2p/' + p2pId)
|
m = m.encapsulate('/ipfs/' + ipfsId)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m.toString().indexOf('0.0.0.0') !== -1) {
|
if (m.toString().indexOf('0.0.0.0') !== -1) {
|
||||||
@ -127,8 +132,8 @@ module.exports = (handler) => {
|
|||||||
|
|
||||||
if (address.family === 'IPv6') {
|
if (address.family === 'IPv6') {
|
||||||
let ma = multiaddr('/ip6/' + address.address + '/tcp/' + address.port)
|
let ma = multiaddr('/ip6/' + address.address + '/tcp/' + address.port)
|
||||||
if (p2pId) {
|
if (ipfsId) {
|
||||||
ma = ma.encapsulate('/p2p/' + p2pId)
|
ma = ma.encapsulate('/ipfs/' + ipfsId)
|
||||||
}
|
}
|
||||||
|
|
||||||
multiaddrs.push(ma)
|
multiaddrs.push(ma)
|
||||||
@ -140,17 +145,8 @@ module.exports = (handler) => {
|
|||||||
return listener
|
return listener
|
||||||
}
|
}
|
||||||
|
|
||||||
function getp2pId (ma) {
|
function getIpfsId (ma) {
|
||||||
return ma.stringTuples().filter((tuple) => {
|
return ma.stringTuples().filter((tuple) => {
|
||||||
return tuple[0] === P2P_CODE
|
return tuple[0] === IPFS_CODE
|
||||||
})[0][1]
|
})[0][1]
|
||||||
}
|
}
|
||||||
|
|
||||||
function trackSocket (server, socket) {
|
|
||||||
const key = `${socket.remoteAddress}:${socket.remotePort}`
|
|
||||||
server.__connections[key] = socket
|
|
||||||
|
|
||||||
socket.on('close', () => {
|
|
||||||
delete server.__connections[key]
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user