Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
k-rpc
Advanced tools
Readme
Low-level implementation of the k-rpc protocol used by the BitTorrent DHT.
npm install k-rpc
Read BEP 5 and BEP 44 for more background info.
var krpc = require('k-rpc')
var rpc = krpc()
var target = new Buffer('aaaabbbbccccddddeeeeffffaaaabbbbccccdddd', 'hex')
// query the BitTorrent DHT to find nodes near the target buffer
rpc.closest(target, {q: 'get_peers', a: {info_hash: target}}, onreply, done)
function onreply (message, node) {
console.log('visited peer', message, node)
}
function done () {
console.log('(done)')
}
var rpc = krpc([options])
Create a new rpc instance. Options include
{
// per peer query timeout defaults to 2s
timeout: 2000,
// an array of bootstrap nodes. defaults to the BitTorrent bootstrap nodes
nodes: ['example.com:6881'],
// how many concurrent queries should be made. defaults to 16
concurrency: 16,
// how big should be routing buckets be. defaults to 20.
k: 20,
// the local node id. defaults to 20 random bytes
id: Buffer(...),
// Length of ID in bytes, defaults to 20 (sha1)
idLength: 20,
// optional k-rpc-socket instance
krpcSocket: krpcSocket(opts)
}
rpc.id
Buffer containing the local node id.
rpc.nodes
Routing table populated by running rpc.populate
. This is a k-bucket instance.
rpc.populate(target, query, [callback])
Populate the rpc.nodes
routing table with nodes discovered by looking for other peers close to our own local node id using the specified query. The internal routing table will be used for subsequent closest queries to take load of the bootstrap nodes.
// send a find_node query
rpc.populate(rpc.id, {q: 'find_node', a: {id: rpc.id, target: rpc.id}}, function () {
console.log('internal routing table fully populated')
})
You should call this method as soon as possible to spread out query load in the DHT.
Callback is called with (err, numberOfReplies)
.
rpc.closest(target, query, onreply, [callback])
Find peers close the specified target buffer whilst sending the specified query. onreply
will be called with (reply, node)
for every reply received and the callback is called with (err, totalNumberOfReplies)
.
// find peers sharing a torrent info_hash
rpc.closest(infoHash, {q: 'get_peers', a: {id: rpc.id: info_hash: infoHash}}, onreply, function () {
console.log('no more peers to be found')
})
function onreply (message, node) {
if (message.r && message.r.values) console.log('received peers')
}
If a closest query is being executed while a population request in being run the closest query will take priority.
You can return false
from onreply to stop the query. This is useful if you are only looking for a single peer for example.
function onreply(message, node) {
console.log('will only fire once')
return false
}
rpc.query(node, query, callback)
Query a single node. If the node has a token it is set as a.token
in the query automatically.
Callback is called with (err, reply)
.
rpc.queryAll(nodes, query, onreply, callback)
Query multiple nodes with the same query. query.a.token
will be set as the corresponding nodes token when querying.
Callback is called with (err, numberOfReplies)
and onreply
will be caleld with (reply, node)
as the nodes reply.
rpc.destroy()
Destroy the underlying rpc socket.
rpc.on('query', query, node)
Emitted when a query is received.
rpc.response(node, query, response, [nodes], [callback])
Send a response to a node for a specific query. If you pass in an array of nodes {id: nodeId, host: someHost, port: somePort}
they will be added to the response.
rpc.error(node, query, error, [callback])
Send an error response for a query.
rpc.on('ping', oldNodes, swapNew)
Emitted when the bucket is full. Try and oldNodes
and if one
of them fails call swapNew
with that node to swap if for a newer one
MIT
FAQs
Low-level implementation of the k-rpc protocol used the BitTorrent DHT.
The npm package k-rpc receives a total of 2,514 weekly downloads. As such, k-rpc popularity was classified as popular.
We found that k-rpc demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.