Compare commits

..

2 Commits

Author SHA1 Message Date
270ac46090 fix: close by array
License: MIT
Signed-off-by: Jacob Heun <jacobheun@gmail.com>
2018-11-08 17:03:23 +01:00
1cd0066755 chore: update dependencies and cleanup usage
License: MIT
Signed-off-by: Jacob Heun <jacobheun@gmail.com>
2018-11-08 16:32:33 +01:00
3 changed files with 31 additions and 40 deletions

View File

@ -34,11 +34,10 @@
"npm": ">=3.0.0"
},
"devDependencies": {
"aegir": "^17.1.1",
"aegir": "^17.0.1",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1",
"interface-transport": "~0.3.6",
"lodash.isfunction": "^3.0.9",
"pull-stream": "^3.6.9"
},
"dependencies": {
@ -46,10 +45,8 @@
"debug": "^4.1.0",
"interface-connection": "~0.3.2",
"ip-address": "^5.8.9",
"lodash.includes": "^4.3.0",
"lodash.isfunction": "^3.0.9",
"mafmt": "^6.0.3",
"multiaddr": "^6.0.1",
"mafmt": "^6.0.2",
"multiaddr": "^5.0.2",
"once": "^1.4.0",
"stream-to-pull-stream": "^1.7.2"
},

View File

@ -4,8 +4,6 @@ const net = require('net')
const toPull = require('stream-to-pull-stream')
const mafmt = require('mafmt')
const withIs = require('class-is')
const includes = require('lodash.includes')
const isFunction = require('lodash.isfunction')
const Connection = require('interface-connection').Connection
const once = require('once')
const debug = require('debug')
@ -17,7 +15,7 @@ function noop () {}
class TCP {
dial (ma, options, callback) {
if (isFunction(options)) {
if (typeof options === 'function') {
callback = options
options = {}
}
@ -53,7 +51,7 @@ class TCP {
}
createListener (options, handler) {
if (isFunction(options)) {
if (typeof options === 'function') {
handler = options
options = {}
}
@ -69,12 +67,12 @@ class TCP {
}
return multiaddrs.filter((ma) => {
if (includes(ma.protoNames(), 'p2p-circuit')) {
if (ma.protoNames().includes('p2p-circuit')) {
return false
}
if (ma.protoNames().includes('p2p')) {
ma = ma.decapsulate('p2p')
if (ma.protoNames().includes('ipfs')) {
ma = ma.decapsulate('ipfs')
}
return mafmt.TCP.matches(ma)

View File

@ -3,7 +3,6 @@
const multiaddr = require('multiaddr')
const Connection = require('interface-connection').Connection
const os = require('os')
const includes = require('lodash.includes')
const net = require('net')
const toPull = require('stream-to-pull-stream')
const EventEmitter = require('events').EventEmitter
@ -12,7 +11,7 @@ const log = debug('libp2p:tcp:listen')
const getMultiaddr = require('./get-multiaddr')
const P2P_CODE = 421
const IPFS_CODE = 421
const CLOSE_TIMEOUT = 2000
function noop () {}
@ -42,8 +41,6 @@ module.exports = (handler) => {
cb(null, [addr])
}
trackSocket(server, socket)
const conn = new Connection(s)
handler(conn)
listener.emit('connection', conn)
@ -52,9 +49,17 @@ module.exports = (handler) => {
server.on('listening', () => listener.emit('listening'))
server.on('error', (err) => listener.emit('error', err))
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
server.__connections = {}
server.__connections = []
listener.close = (options, callback) => {
if (typeof options === 'function') {
@ -66,9 +71,9 @@ module.exports = (handler) => {
const timeout = setTimeout(() => {
log('unable to close graciously, destroying conns')
Object.keys(server.__connections).forEach((key) => {
log('destroying %s', key)
server.__connections[key].destroy()
server.__connections.forEach((conn) => {
log('destroying %s', `${conn.remoteAddress}:${conn.remotePort}`)
conn.destroy()
})
}, options.timeout || CLOSE_TIMEOUT)
@ -79,14 +84,14 @@ module.exports = (handler) => {
})
}
let p2pId
let ipfsId
let listeningAddr
listener.listen = (ma, callback) => {
listeningAddr = ma
if (includes(ma.protoNames(), 'p2p')) {
p2pId = getp2pId(ma)
listeningAddr = ma.decapsulate('p2p')
if (ma.protoNames().includes('ipfs')) {
ipfsId = getIpfsId(ma)
listeningAddr = ma.decapsulate('ipfs')
}
const lOpts = listeningAddr.toOptions()
@ -107,8 +112,8 @@ module.exports = (handler) => {
if (listeningAddr.toString().indexOf('ip4') !== -1) {
let m = listeningAddr.decapsulate('tcp')
m = m.encapsulate('/tcp/' + address.port)
if (p2pId) {
m = m.encapsulate('/p2p/' + p2pId)
if (ipfsId) {
m = m.encapsulate('/ipfs/' + ipfsId)
}
if (m.toString().indexOf('0.0.0.0') !== -1) {
@ -127,8 +132,8 @@ module.exports = (handler) => {
if (address.family === 'IPv6') {
let ma = multiaddr('/ip6/' + address.address + '/tcp/' + address.port)
if (p2pId) {
ma = ma.encapsulate('/p2p/' + p2pId)
if (ipfsId) {
ma = ma.encapsulate('/ipfs/' + ipfsId)
}
multiaddrs.push(ma)
@ -140,17 +145,8 @@ module.exports = (handler) => {
return listener
}
function getp2pId (ma) {
function getIpfsId (ma) {
return ma.stringTuples().filter((tuple) => {
return tuple[0] === P2P_CODE
return tuple[0] === IPFS_CODE
})[0][1]
}
function trackSocket (server, socket) {
const key = `${socket.remoteAddress}:${socket.remotePort}`
server.__connections[key] = socket
socket.on('close', () => {
delete server.__connections[key]
})
}