|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const Session = require('../../').Session |
| 4 | +const TorrentInfo = require('../../').TorrentInfo |
| 5 | +const path = require('path') |
| 6 | +const areTermsMatching = require('../../lib/utils').areTermsMatching |
| 7 | +const ConnectionInnerState = require('../../').ConnectionInnerState |
| 8 | +const TorrentState = require('../../').TorrentState |
| 9 | +const LibtorrentInteraction = require('../../').LibtorrentInteraction |
| 10 | + |
| 11 | +const torrentPath = path.join(__dirname, '/../../test/sfc.torrent') |
| 12 | +const sintelTorrentPath = path.join(__dirname, '/../../test/sintel.torrent') |
| 13 | + |
| 14 | +var buyerSession = new Session({ |
| 15 | + port: 6881 |
| 16 | +}) |
| 17 | + |
| 18 | +let addTorrentParamsBuyer = { |
| 19 | + ti: new TorrentInfo(torrentPath), |
| 20 | + savePath: path.join(__dirname, '/downloads/') |
| 21 | +} |
| 22 | + |
| 23 | +function letsBuy (torrent) { |
| 24 | + console.log('torrent in downloading state, going to buy mode') |
| 25 | + // 100, 5, 1, 20000 |
| 26 | + const buyerTerms = { |
| 27 | + maxPrice: 100, |
| 28 | + maxLock: 5, |
| 29 | + minNumberOfSellers: 1, |
| 30 | + maxContractFeePerKb: 20000 |
| 31 | + } |
| 32 | + |
| 33 | + let lookingForSeller = false |
| 34 | + |
| 35 | + torrent.toBuyMode(buyerTerms, (err) => { |
| 36 | + if (err) { |
| 37 | + return console.log(err) |
| 38 | + } |
| 39 | + |
| 40 | + torrent.startPlugin(function (err) { |
| 41 | + if(err) { |
| 42 | + return console.log(err) |
| 43 | + } |
| 44 | + |
| 45 | + console.log('We are in buying mode') |
| 46 | + lookingForSeller = true |
| 47 | + }) |
| 48 | + }) |
| 49 | + |
| 50 | + // Wait for one suitable seller and start downloading |
| 51 | + torrent.on('peerPluginStatusUpdates', function (peerStatuses) { |
| 52 | + if (!lookingForSeller) return |
| 53 | + |
| 54 | + console.log('looking for seller') |
| 55 | + |
| 56 | + const connection = pickSuitableSeller(peerStatuses, buyerTerms) |
| 57 | + |
| 58 | + if (!connection) return |
| 59 | + |
| 60 | + console.log('found a suitable seller', connection) |
| 61 | + |
| 62 | + // Stop looking for seller |
| 63 | + lookingForSeller = false |
| 64 | + |
| 65 | + const pid = connection.pid |
| 66 | + const sellerTerms = connection.announcedModeAndTermsFromPeer.seller.terms |
| 67 | + |
| 68 | + let setup = makeContractAndDownloadInfoMap(pid, sellerTerms) |
| 69 | + |
| 70 | + torrent.startDownloading(setup.contract, setup.map, function (err) { |
| 71 | + if (err) { |
| 72 | + console.log(err) |
| 73 | + lookingForSeller = true |
| 74 | + return |
| 75 | + } |
| 76 | + console.log('Started Downloading From Seller') |
| 77 | + }) |
| 78 | + }) |
| 79 | +} |
| 80 | + |
| 81 | +function makeContractAndDownloadInfoMap (pid, sellerTerms) { |
| 82 | + // Fake contract |
| 83 | + const contract = Buffer.from('01000000017b1eabe0209b1fe794124575ef807057c77ada2138ae4fa8d6c4de0398a14f3f00000000494830450221008949f0cb400094ad2b5eb399d59d01c14d73d8fe6e96df1a7150deb388ab8935022079656090d7f6bac4c9a94e0aad311a4268e082a725f8aeae0573fb12ff866a5f01ffffffff01f0ca052a010000001976a914cbc20a7664f2f69e5355aa427045bc15e7c6c77288ac00000000', 'hex') |
| 84 | + |
| 85 | + // Download info map for one seller |
| 86 | + const map = new Map() |
| 87 | + |
| 88 | + map.set(pid, { |
| 89 | + index: 0, |
| 90 | + value: 100000, |
| 91 | + sellerTerms: sellerTerms, |
| 92 | + buyerContractSk: Buffer.from('0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20', 'hex'), |
| 93 | + buyerFinalPkHash: new Buffer(20) |
| 94 | + }) |
| 95 | + |
| 96 | + return {contract, map} |
| 97 | +} |
| 98 | + |
| 99 | +function pickSuitableSeller (peerStatuses, buyerTerms) { |
| 100 | + for (var i in peerStatuses) { |
| 101 | + const status = peerStatuses[i] |
| 102 | + |
| 103 | + if (!status.connection) continue |
| 104 | + |
| 105 | + //console.log(status.connection.announcedModeAndTermsFromPeer) |
| 106 | + |
| 107 | + // connection must be in PerparingContract state |
| 108 | + if (status.connection.innerState !== ConnectionInnerState.PreparingContract) { |
| 109 | + console.log('not in preparing contract status') |
| 110 | + continue |
| 111 | + } |
| 112 | + |
| 113 | + try { |
| 114 | + // lazy checking for seller, if peer is not a seller this will throw.. |
| 115 | + const sellerTerms = status.connection.announcedModeAndTermsFromPeer.seller.terms |
| 116 | + console.log(sellerTerms) |
| 117 | + if (areTermsMatching(buyerTerms, sellerTerms)) { |
| 118 | + return status.connection |
| 119 | + } |
| 120 | + |
| 121 | + } catch (e) { console.log(e) } |
| 122 | + } |
| 123 | + |
| 124 | + return null |
| 125 | +} |
| 126 | + |
| 127 | +buyerSession.addTorrent(addTorrentParamsBuyer, (err, torrent) => { |
| 128 | + if (err) { |
| 129 | + return console.log(err) |
| 130 | + } |
| 131 | + |
| 132 | + console.log(torrent) |
| 133 | + |
| 134 | + torrent.setLibtorrentInteraction(LibtorrentInteraction.BlockUploadingAndDownloading) |
| 135 | + |
| 136 | + // Wait for libtorrent state to be downloading |
| 137 | + waitForState(torrent, TorrentState.downloading, function () { |
| 138 | + letsBuy(torrent) |
| 139 | + }) |
| 140 | + |
| 141 | + // Wait for libtorrent state to be seeding which means we already |
| 142 | + // have it and it doesn't make sense to try to buy it, or we completed downloading it |
| 143 | + waitForState(torrent, TorrentState.seeding, function () { |
| 144 | + console.log('Torrent downloaded, exiting') |
| 145 | + process.exit() |
| 146 | + }) |
| 147 | +}) |
| 148 | + |
| 149 | +function waitForState (torrent, targetState, callback) { |
| 150 | + function checkState () { |
| 151 | + if (torrent.status().state === targetState) { |
| 152 | + torrent.removeListener('state_changed', checkState) |
| 153 | + callback() |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + if (torrent.status().state === targetState) { |
| 158 | + callback() |
| 159 | + } else { |
| 160 | + torrent.on('state_changed', checkState) |
| 161 | + } |
| 162 | +} |
0 commit comments