├── .gitignore ├── LICENSE ├── README.md ├── leecher.js ├── message ├── package.json └── seeder.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.torrent 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Two Bucks Ltd 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Bittorrent example 2 | 3 | A simple example of file transfer using Bittorrent protocol. 4 | 5 | It uses [bittorrent-tracker](https://github.com/feross/bittorrent-tracker) and [webtorrent](https://github.com/feross/webtorrent) under the hood. 6 | 7 | ## Steps 8 | 9 | 1. Install dependencies: `npm install` 10 | 2. Start a Bittorrent tracker server on port 8000: `bittorrent-tracker` 11 | 3. Open another terminal and start seeding *hello world* with: `node seeder.js` 12 | 4. Run this in another terminal: `node leecher.js` 13 | 14 | ## FAQ 15 | 16 | #### How did you generate the magnet hash? 17 | 18 | Use `create-torrent` and `magnet-link`. 19 | 20 | ``` 21 | npm install -g create-torrent magnet-link 22 | ``` 23 | 24 | Magnet hash is generated with the following command 25 | 26 | ``` 27 | create-torrent message | magnet-link 28 | ``` 29 | 30 | and then appending the other params like download name and tracker to it. So the above command gives us `magnet:?xt=urn:btih:b8e8adbee320fc312e5fd71479329c3d53c40aea` and we append more params to it so it uses our tracker on port 8000: 31 | ``` 32 | magnet:?xt=urn:btih:b8e8adbee320fc312e5fd71479329c3d53c40aea&dn=message&tr=http%3A%2F%2Flocalhost%3A8000%2Fannounce 33 | ``` 34 | 35 | You also get the same magnet link printed to the console if you run the seeder first with `node seeder.js`. 36 | 37 | ## License 38 | 39 | MIT 40 | 41 | ## Sponsors 42 | 43 | Two Bucks Ltd © 2017 44 | 45 | ![https://twobucks.co](https://twobucks.co/assets/images/logo-small.png) 46 | -------------------------------------------------------------------------------- /leecher.js: -------------------------------------------------------------------------------- 1 | var WebTorrent = require('webtorrent') 2 | 3 | var client = new WebTorrent() 4 | var magnetUri = "magnet:?xt=urn:btih:b8e8adbee320fc312e5fd71479329c3d53c40aea&dn=message&tr=http%3A%2F%2Flocalhost%3A8000%2Fannounce" 5 | 6 | client.add(magnetUri, function (torrent) { 7 | // Got torrent metadata! 8 | console.log('Client is downloading:', torrent.infoHash) 9 | 10 | torrent.files.forEach(function (file) { 11 | file.getBuffer(function (er, buf) { 12 | console.log(buf.toString()) 13 | }) 14 | }) 15 | }) 16 | -------------------------------------------------------------------------------- /message: -------------------------------------------------------------------------------- 1 | hello world 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bittorrent-test", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "leecher.js", 6 | "dependencies": { 7 | "bittorrent-tracker": "^6.0.7", 8 | "parse-torrent": "^5.4.0", 9 | "webtorrent": "^0.63.4" 10 | }, 11 | "devDependencies": {}, 12 | "scripts": { 13 | "test": "echo \"Error: no test specified\" && exit 1" 14 | }, 15 | "keywords": [], 16 | "author": "", 17 | "license": "MIT" 18 | } 19 | -------------------------------------------------------------------------------- /seeder.js: -------------------------------------------------------------------------------- 1 | var WebTorrent = require('webtorrent') 2 | var client = new WebTorrent() 3 | client.seed('message', {announceList: [["http://localhost:8000/announce"]]}, function(torrent){ 4 | console.log('Client is seeding:', torrent.magnetURI) 5 | }) 6 | --------------------------------------------------------------------------------