Skip to content

Commit

Permalink
doc and example
Browse files Browse the repository at this point in the history
  • Loading branch information
Covertness committed Nov 30, 2018
1 parent c1b3162 commit c0f51a6
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 784 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 Covertness
Copyright (c) 2019 Covertness

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
25 changes: 0 additions & 25 deletions Makefile

This file was deleted.

63 changes: 46 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,55 @@
# node-dht-broker
A broker relay the dht messages to clients over HTTP.
# node-dht-peer-crawler
A fast and stable DHT crawler.

## Start
## Installation
```bash
$ apt-get install nodejs npm make g++
$ make init
$ make build
$ node lib/dht-broker.js
$ npm install dht-peer-crawler
```

## HTTP API
### Get Nodes
```bash
$ curl http://127.0.0.1:8080/nodes
## Usage
```js
import Crawler from 'dht-peer-crawler'

const crawler = new Crawler()

crawler.on('announce_peer', (infoHashStr, addressStr) => {
console.log(`got a peer ${addressStr} on ${infoHashStr}`)
})

crawler.start().then(() => {
console.log('start crawler success')
}, (error) => {
console.error(`start crawler failed: ${error}`)
})

const signalTraps = ['SIGTERM', 'SIGINT', 'SIGUSR2']

signalTraps.map(type => {
process.once(type, async () => {
try {
await crawler.stop()
console.log('stop crawler success')
} finally {
process.kill(process.pid, type)
}
})
})
```

### Get InfoHash
## Test
```bash
$ curl http://127.0.0.1:8080/infos
$ npm test
```

### Get Torrent
```bash
$ curl http://127.0.0.1:8080/torrent?info=torrent_infohash
```
## API
#### `crawler = new Crawler(listenPort)`

Create a new crawler instance.

#### `crawler.on('announce_peer', [infoHashStr, addressStr])`

Emitted when received an `announce_peer` message.

#### `crawler.on('new_info_hash', [infoHashStr])`

Emitted when find a new `info_hash`.
39 changes: 39 additions & 0 deletions example/watch_announce_peer.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Crawler from '..'

const crawler = new Crawler()

crawler.on('announce_peer', (infoHashStr, addressStr) => {
console.log(`got a peer ${addressStr} on ${infoHashStr}`)
})

crawler.on('_find_once', () => {
console.log(`route table length: ${crawler.routeTable.count()}`)
console.log(`info_hash table length: ${crawler.infoHashTable.size}`)
let foundInfoHashCount = 0
let queriedInfoHashCount = 0
crawler.infoHashTable.forEach(info => {
foundInfoHashCount += info.announceNodes.size
queriedInfoHashCount += info.queryNodes.size
})
console.log(`found info_hash length: ${foundInfoHashCount}`)
console.log(`queried info_hash length: ${queriedInfoHashCount}`)
})

crawler.start().then(() => {
console.log('start crawler success')
}, (error) => {
console.error(`start crawler failed: ${error}`)
})

const signalTraps = ['SIGTERM', 'SIGINT', 'SIGUSR2']

signalTraps.map(type => {
process.once(type, async () => {
try {
await crawler.stop()
console.log('stop crawler success')
} finally {
process.kill(process.pid, type)
}
})
})
17 changes: 3 additions & 14 deletions lib/crawler.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,11 @@ export default class Crawler extends Emitter {
await this.loadLastRoutes()

const findDivNum = Math.pow(this.config.maxRouteTableLen, 2) / (this.config.maxFindInterval - this.config.minFindInterval)
this.on('_findOnce', () => {
this.on('_find_once', () => {
const allNodesCount = this.routeTable.count()

const currentTimeout = this.config.maxFindInterval - Math.pow((allNodesCount - this.config.maxRouteTableLen), 2) / findDivNum
this.findInterval = setTimeout(() => this.emit('_findOnce'), currentTimeout)

console.log(`route table length: ${allNodesCount}`)
console.log(`info_hash table length: ${this.infoHashTable.size}`)
let foundInfoHashCount = 0
let queriedInfoHashCount = 0
this.infoHashTable.forEach(info => {
foundInfoHashCount += info.announceNodes.size
queriedInfoHashCount += info.queryNodes.size
})
console.log(`found info_hash length: ${foundInfoHashCount}`)
console.log(`queried info_hash length: ${queriedInfoHashCount}`)
this.findInterval = setTimeout(() => this.emit('_find_once'), currentTimeout)

if (allNodesCount > this.config.persistentPeersLen) {
this.persistentNodes()
Expand All @@ -121,7 +110,7 @@ export default class Crawler extends Emitter {
}
})

this.findInterval = setTimeout(() => this.emit('_findOnce'), this.config.minFindInterval)
this.findInterval = setTimeout(() => this.emit('_find_once'), this.config.minFindInterval)
}

async stop() {
Expand Down
19 changes: 6 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "node-dht-broker",
"version": "1.0.0",
"description": "A broker relay the dht messages to clients over tcp",
"main": "lib/dht-broker.js",
"name": "dht-peer-crawler",
"version": "2.0.0",
"description": "A fast and stable DHT crawler.",
"main": "lib/crawler.mjs",
"scripts": {
"test": "jest"
},
"keywords": [
"dht"
"dht",
"crawler"
],
"author": "covertness",
"license": "MIT",
Expand Down Expand Up @@ -37,16 +38,8 @@
},
"dependencies": {
"bencode": "^2.0.0",
"bncode": "^0.5.3",
"collections": "^3.0.0",
"cors": "^2.7.1",
"express": "^4.13.3",
"hat": "^0.0.3",
"ht": "0.0.2",
"k-bucket": "^5.0.0",
"magnet-uri": "^5.1.1",
"mixpanel": "^0.10.0",
"peer-wire-swarm": "^0.12.1",
"quick-lru": "^2.0.0"
},
"devDependencies": {
Expand Down
18 changes: 0 additions & 18 deletions src/dht-broker.coffee

This file was deleted.

Loading

0 comments on commit c0f51a6

Please sign in to comment.