-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c1b3162
commit c0f51a6
Showing
13 changed files
with
95 additions
and
784 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.