mirror of
https://github.com/fluencelabs/js-libp2p-tcp
synced 2025-07-04 05:31:51 +00:00
Compare commits
1 Commits
feat/use-p
...
api-docs-r
Author | SHA1 | Date | |
---|---|---|---|
3f61784be9 |
@ -27,3 +27,4 @@ build/Release
|
|||||||
node_modules
|
node_modules
|
||||||
|
|
||||||
test
|
test
|
||||||
|
docs
|
@ -51,7 +51,6 @@
|
|||||||
"mafmt": "^6.0.0",
|
"mafmt": "^6.0.0",
|
||||||
"multiaddr": "^4.0.0",
|
"multiaddr": "^4.0.0",
|
||||||
"once": "^1.4.0",
|
"once": "^1.4.0",
|
||||||
"pull-net": "github:mkg20001/pull-net",
|
|
||||||
"stream-to-pull-stream": "^1.7.2"
|
"stream-to-pull-stream": "^1.7.2"
|
||||||
},
|
},
|
||||||
"contributors": [
|
"contributors": [
|
||||||
|
@ -9,22 +9,22 @@ module.exports = (socket) => {
|
|||||||
let ma
|
let ma
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (socket.remoteAddress.family === 'IPv6') {
|
if (socket.remoteFamily === 'IPv6') {
|
||||||
const addr = new Address6(socket.remoteAddress.address)
|
const addr = new Address6(socket.remoteAddress)
|
||||||
|
|
||||||
if (addr.v4) {
|
if (addr.v4) {
|
||||||
const ip4 = addr.to4().correctForm()
|
const ip4 = addr.to4().correctForm()
|
||||||
ma = multiaddr('/ip4/' + ip4 +
|
ma = multiaddr('/ip4/' + ip4 +
|
||||||
'/tcp/' + socket.remoteAddress.port
|
'/tcp/' + socket.remotePort
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
ma = multiaddr('/ip6/' + socket.remoteAddress.address +
|
ma = multiaddr('/ip6/' + socket.remoteAddress +
|
||||||
'/tcp/' + socket.remoteAddress.port
|
'/tcp/' + socket.remotePort
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ma = multiaddr('/ip4/' + socket.remoteAddress.address +
|
ma = multiaddr('/ip4/' + socket.remoteAddress +
|
||||||
'/tcp/' + socket.remoteAddress.port)
|
'/tcp/' + socket.remotePort)
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
log(err)
|
log(err)
|
||||||
|
46
src/index.js
46
src/index.js
@ -1,6 +1,7 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const {connect} = require('pull-net')
|
const net = require('net')
|
||||||
|
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 includes = require('lodash.includes')
|
||||||
@ -14,7 +15,18 @@ const createListener = require('./listener')
|
|||||||
|
|
||||||
function noop () {}
|
function noop () {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
class TCP {
|
class TCP {
|
||||||
|
/**
|
||||||
|
* Dial to another peer.
|
||||||
|
*
|
||||||
|
* @param {Multiaddr} ma - The address of the peer we want to dial to.
|
||||||
|
* @param {Object} [options={}]
|
||||||
|
* @param {function(Error?, Array<Multiaddr>?)} [callback]
|
||||||
|
* @returns {Connection}
|
||||||
|
*/
|
||||||
dial (ma, options, callback) {
|
dial (ma, options, callback) {
|
||||||
if (isFunction(options)) {
|
if (isFunction(options)) {
|
||||||
callback = options
|
callback = options
|
||||||
@ -26,7 +38,23 @@ class TCP {
|
|||||||
const cOpts = ma.toOptions()
|
const cOpts = ma.toOptions()
|
||||||
log('Connecting to %s %s', cOpts.port, cOpts.host)
|
log('Connecting to %s %s', cOpts.port, cOpts.host)
|
||||||
|
|
||||||
const conn = new Connection(connect(cOpts.port, cOpts.host, callback))
|
const rawSocket = net.connect(cOpts)
|
||||||
|
|
||||||
|
rawSocket.once('timeout', () => {
|
||||||
|
log('timeout')
|
||||||
|
rawSocket.emit('error', new Error('Timeout'))
|
||||||
|
})
|
||||||
|
|
||||||
|
rawSocket.once('error', callback)
|
||||||
|
|
||||||
|
rawSocket.once('connect', () => {
|
||||||
|
rawSocket.removeListener('error', callback)
|
||||||
|
callback()
|
||||||
|
})
|
||||||
|
|
||||||
|
const socket = toPull.duplex(rawSocket)
|
||||||
|
|
||||||
|
const conn = new Connection(socket)
|
||||||
|
|
||||||
conn.getObservedAddrs = (callback) => {
|
conn.getObservedAddrs = (callback) => {
|
||||||
return callback(null, [ma])
|
return callback(null, [ma])
|
||||||
@ -35,6 +63,13 @@ class TCP {
|
|||||||
return conn
|
return conn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Listen for incoming `TCP` connetions.
|
||||||
|
*
|
||||||
|
* @param {Object} [options={}]
|
||||||
|
* @param {function(Connection)} [handler] - Called with newly incomin connections.
|
||||||
|
* @returns {Listener}
|
||||||
|
*/
|
||||||
createListener (options, handler) {
|
createListener (options, handler) {
|
||||||
if (isFunction(options)) {
|
if (isFunction(options)) {
|
||||||
handler = options
|
handler = options
|
||||||
@ -46,6 +81,13 @@ class TCP {
|
|||||||
return createListener(handler)
|
return createListener(handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filter a list of multiaddrs for those which contain
|
||||||
|
* valid `TCP` addresses.
|
||||||
|
*
|
||||||
|
* @param {Multiaddr|Array<Multiaddr>} multiaddrs
|
||||||
|
* @returns {Array<Multiaddr>}
|
||||||
|
*/
|
||||||
filter (multiaddrs) {
|
filter (multiaddrs) {
|
||||||
if (!Array.isArray(multiaddrs)) {
|
if (!Array.isArray(multiaddrs)) {
|
||||||
multiaddrs = [multiaddrs]
|
multiaddrs = [multiaddrs]
|
||||||
|
@ -4,23 +4,71 @@ 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 includes = require('lodash.includes')
|
||||||
const {createServer} = require('pull-net')
|
const net = require('net')
|
||||||
|
const toPull = require('stream-to-pull-stream')
|
||||||
const EventEmitter = require('events').EventEmitter
|
const EventEmitter = require('events').EventEmitter
|
||||||
const debug = require('debug')
|
const debug = require('debug')
|
||||||
const log = debug('libp2p:tcp:listen')
|
const log = debug('libp2p:tcp:listen')
|
||||||
|
|
||||||
const getMultiaddr = require('./get-multiaddr')
|
const getMultiaddr = require('./get-multiaddr')
|
||||||
|
|
||||||
const IPFS_CODE = 421
|
const IPFS_CODE = 421
|
||||||
|
const CLOSE_TIMEOUT = 2000
|
||||||
|
|
||||||
function noop () {}
|
function noop () {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Listening for incoming connections.
|
||||||
|
*
|
||||||
|
* @event listening
|
||||||
|
* @instance
|
||||||
|
* @memberof Listener
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The server closes.
|
||||||
|
*
|
||||||
|
* @event close
|
||||||
|
* @instance
|
||||||
|
* @memberof Listener
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* New connection established.
|
||||||
|
*
|
||||||
|
* @event connection
|
||||||
|
* @instance
|
||||||
|
* @type {Connection}
|
||||||
|
* @memberof Listener
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The underlying server encountered an error.
|
||||||
|
*
|
||||||
|
* @event error
|
||||||
|
* @instance
|
||||||
|
* @type {Error}
|
||||||
|
* @memberof Listener
|
||||||
|
*/
|
||||||
|
|
||||||
module.exports = (handler) => {
|
module.exports = (handler) => {
|
||||||
|
/**
|
||||||
|
* @alias Listener
|
||||||
|
* @type {Eventemitter}
|
||||||
|
* @fires Listener#listening
|
||||||
|
* @fires Listener#close
|
||||||
|
* @fires Listener#connection
|
||||||
|
* @fires Listener#error
|
||||||
|
*/
|
||||||
const listener = new EventEmitter()
|
const listener = new EventEmitter()
|
||||||
|
|
||||||
const server = createServer((stream) => {
|
const server = net.createServer((socket) => {
|
||||||
const addr = getMultiaddr(stream)
|
// Avoid uncaught errors cause by unstable connections
|
||||||
|
socket.on('error', noop)
|
||||||
|
|
||||||
|
const addr = getMultiaddr(socket)
|
||||||
if (!addr) {
|
if (!addr) {
|
||||||
if (stream.remoteAddress === undefined || stream.remoteAddress.address === 'undefined') {
|
if (socket.remoteAddress === undefined) {
|
||||||
log('connection closed before p2p connection made')
|
log('connection closed before p2p connection made')
|
||||||
} else {
|
} else {
|
||||||
log('error interpreting incoming p2p connection')
|
log('error interpreting incoming p2p connection')
|
||||||
@ -30,16 +78,25 @@ module.exports = (handler) => {
|
|||||||
|
|
||||||
log('new connection', addr.toString())
|
log('new connection', addr.toString())
|
||||||
|
|
||||||
stream.getObservedAddrs = (cb) => {
|
const s = toPull.duplex(socket)
|
||||||
|
|
||||||
|
s.getObservedAddrs = (cb) => {
|
||||||
cb(null, [addr])
|
cb(null, [addr])
|
||||||
}
|
}
|
||||||
|
|
||||||
const conn = new Connection(stream)
|
trackSocket(server, socket)
|
||||||
|
|
||||||
|
const conn = new Connection(s)
|
||||||
handler(conn)
|
handler(conn)
|
||||||
listener.emit('connection', conn)
|
listener.emit('connection', conn)
|
||||||
})
|
})
|
||||||
|
|
||||||
listener.emit('listening')
|
server.on('listening', () => listener.emit('listening'))
|
||||||
|
server.on('error', (err) => listener.emit('error', err))
|
||||||
|
server.on('close', () => listener.emit('close'))
|
||||||
|
|
||||||
|
// Keep track of open connections to destroy in case of timeout
|
||||||
|
server.__connections = {}
|
||||||
|
|
||||||
listener.close = (options, callback) => {
|
listener.close = (options, callback) => {
|
||||||
if (typeof options === 'function') {
|
if (typeof options === 'function') {
|
||||||
@ -49,9 +106,18 @@ module.exports = (handler) => {
|
|||||||
callback = callback || noop
|
callback = callback || noop
|
||||||
options = options || {}
|
options = options || {}
|
||||||
|
|
||||||
server.close((err, ...a) => {
|
const timeout = setTimeout(() => {
|
||||||
listener.emit('close')
|
log('unable to close graciously, destroying conns')
|
||||||
callback(err, ...a)
|
Object.keys(server.__connections).forEach((key) => {
|
||||||
|
log('destroying %s', key)
|
||||||
|
server.__connections[key].destroy()
|
||||||
|
})
|
||||||
|
}, options.timeout || CLOSE_TIMEOUT)
|
||||||
|
|
||||||
|
server.close(callback)
|
||||||
|
|
||||||
|
server.once('close', () => {
|
||||||
|
clearTimeout(timeout)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,3 +187,12 @@ function getIpfsId (ma) {
|
|||||||
return tuple[0] === IPFS_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