server: also modify torrent file on "Set Tracker"

This commit is contained in:
Jesse Chan
2020-09-06 00:36:44 +08:00
parent 9f87b08d26
commit 1a2ea34bb4
5 changed files with 47 additions and 0 deletions

8
package-lock.json generated
View File

@@ -3298,6 +3298,14 @@
"tweetnacl": "^0.14.3"
}
},
"bencode": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/bencode/-/bencode-2.0.1.tgz",
"integrity": "sha512-2uhEl8FdjSBUyb69qDTgOEeeqDTa+n3yMQzLW0cOzNf1Ow5bwcg3idf+qsWisIKRH8Bk8oC7UXL8irRcPA8ZEQ==",
"requires": {
"safe-buffer": "^5.1.1"
}
},
"big.js": {
"version": "5.2.2",
"resolved": "https://registry.npmjs.org/big.js/-/big.js-5.2.2.tgz",

View File

@@ -49,6 +49,7 @@
"axios": "^0.20.0",
"babel-eslint": "^10.1.0",
"babel-loader": "^8.0.6",
"bencode": "^2.0.1",
"body-parser": "^1.18.3",
"case-sensitive-paths-webpack-plugin": "2.3.0",
"chalk": "^4.1.0",

View File

@@ -249,4 +249,9 @@ torrentListPropMap.set('peersTotal', {
transformValue: (value) => Number(value.substr(0, value.indexOf('|||'))),
});
torrentListPropMap.set('session', {
methodCall: 'cat=(session.path), (d.hash)',
transformValue: defaultTransformer,
});
module.exports = torrentListPropMap;

View File

@@ -10,6 +10,7 @@ const fileUtil = require('../util/fileUtil');
const settings = require('./settings');
const torrentFilePropsMap = require('../../shared/constants/torrentFilePropsMap');
const torrentPeerPropsMap = require('../../shared/constants/torrentPeerPropsMap');
const torrentFileUtil = require('../util/torrentFileUtil');
const torrentStatusMap = require('../../shared/constants/torrentStatusMap');
const torrentTrackerPropsMap = require('../../shared/constants/torrentTrackerPropsMap');
@@ -385,6 +386,9 @@ const client = {
request.setTracker(data);
request.onComplete((response, error) => {
// Modify tracker URL in torrent files
torrentFileUtil.setTracker(services, data);
// Fetch the latest torrent list to re-index trackerURI.
services.torrentService.fetchTorrentList();
callback(response, error);

View File

@@ -0,0 +1,29 @@
const bencode = require('bencode');
const fs = require('fs');
const setTracker = (services, options) => {
const {tracker, hashes} = options;
[...new Set(hashes)].forEach((hash) => {
const torrent = services.torrentService.getTorrent(hash).session.concat('.torrent');
fs.readFile(torrent, (err, data) => {
if (err) {
return;
}
const torrentData = bencode.decode(data);
if (torrentData.announce != null) {
torrentData.announce = Buffer.from(tracker);
fs.writeFileSync(torrent, bencode.encode(torrentData));
}
});
});
};
const torrentFileUtil = {
setTracker,
};
module.exports = torrentFileUtil;