Skip to content

Commit

Permalink
Feature Request seansfkelley#137
Browse files Browse the repository at this point in the history
adding public tracker list for each download task.
  • Loading branch information
boin committed Dec 21, 2020
1 parent 04ef4d0 commit dca9286
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 31 deletions.
66 changes: 43 additions & 23 deletions src/background/actions/torrentTracker.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,72 @@
import Axios from "axios";
import bencodec from 'bencodec';
import { startsWithAnyProtocol, MAGNET_PROTOCOL } from "../../common/apis/protocols";
import type { State } from "../../common/state";

let cachedTrackers: string[] = [];
let lastPublicTrackerURL = "";

export async function updateAndGetTorrentTrackers(storedState: State): Promise<string[]> {
async function updateRemoteTrackers(url: string) {

let response;

const flag = storedState.settings.torrentTrackers.enablePublicTrackers;
const url = storedState.settings.torrentTrackers.publicTrackerURL;
try {
response = await Axios.get(url, { timeout: 10000 });
lastPublicTrackerURL = url;
} catch (e) {
console.log("Axios Error caught when updating public trackers:", e);
cachedTrackers = [];
}

if (flag && url !== lastPublicTrackerURL) {
const trackerText: string = response?.data?.toString();

try {
response = await Axios.get(url, { timeout: 10000 });
lastPublicTrackerURL = url;
} catch (e) {
cachedTrackers = [];
if (trackerText !== "") {
if (trackerText.includes(',')) {
cachedTrackers = trackerText.split(',')
} else if (trackerText.includes("\n\n")) {
cachedTrackers = trackerText.split("\n\n")
} else {
cachedTrackers = trackerText.split("\n")
}
console.log("successfully updated public trackers:", cachedTrackers.length)
}
}

const trackerText: string = response?.data?.toString();
export function updateAndGetTorrentTrackers(storedState: State): string[] {

if (trackerText !== "") {
if (trackerText.includes(',')) {
cachedTrackers = trackerText.split(',')
} else if (trackerText.includes("\n\n")) {
cachedTrackers = trackerText.split("\n\n")
} else {
cachedTrackers = trackerText.split("\n")
}
}
console.debug('updateAndGetTorrentTrackers was called', new Error().stack);
console.debug('cached trackers:', cachedTrackers.length);

const flag = storedState.settings.torrentTrackers.enablePublicTrackers;
const url = storedState.settings.torrentTrackers.publicTrackerURL;

if (flag && url !== lastPublicTrackerURL) {
updateRemoteTrackers(url);
}

return cachedTrackers;
}

export function setTrackers(trackers: string[]) {
cachedTrackers = trackers;
}

export function addTrackersToURL(url: string): string {
if (startsWithAnyProtocol(url, MAGNET_PROTOCOL)) {
url += url.includes("?") ? "" : "?";
cachedTrackers.forEach(t => {
cachedTrackers.some((t, i) => {
if (i >= 50) return true; // make sure uri is not too large
url += "&tr=" + encodeURIComponent(t)
})
return false;
});
}
return url;
}

export function addTrackersToMetaData(metaData: Blob): Blob {
return metaData;
export function addTrackersToMetaData(metaData: Buffer) {
const torrent: any = bencodec.decode(metaData);
cachedTrackers.forEach(t => {
torrent['announce-list'].push([Buffer.from(t, 'utf8')])
});
return bencodec.encode(torrent);
}
6 changes: 6 additions & 0 deletions src/background/actions/urls.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Axios from "axios";
import { addTrackersToURL, addTrackersToMetaData } from "./torrentTracker"
import { parse as parseQueryString } from "query-string";
import {
ALL_DOWNLOADABLE_PROTOCOLS,
Expand Down Expand Up @@ -179,6 +180,9 @@ export async function resolveUrl(url: string): Promise<ResolvedUrl> {
return createUnexpectedError(e, "error while trying to fetch metadata file");
}

if (metadataFileType.mediaType === METADATA_FILE_TYPES[0].mediaType)
response.data = addTrackersToMetaData(response.data)

return {
type: "metadata-file",
url,
Expand All @@ -192,6 +196,8 @@ export async function resolveUrl(url: string): Promise<ResolvedUrl> {
};
}
} else if (startsWithAnyProtocol(url, ALL_DOWNLOADABLE_PROTOCOLS)) {
if (startsWithAnyProtocol(url, MAGNET_PROTOCOL))
url = addTrackersToURL(url);
return {
type: "direct-download",
url,
Expand Down
4 changes: 2 additions & 2 deletions src/background/onStateChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { filterTasks } from "../common/filtering";

const START_TIME = Date.now();

export async function onStoredStateChange(storedState: State) {
export function onStoredStateChange(storedState: State) {
const backgroundState = getMutableStateSingleton();

const didUpdateSettings = backgroundState.api.updateSettings({
Expand Down Expand Up @@ -51,7 +51,7 @@ export async function onStoredStateChange(storedState: State) {
backgroundState.showNonErrorNotifications =
storedState.settings.notifications.enableFeedbackNotifications;

backgroundState.torrentTrackers = await updateAndGetTorrentTrackers(storedState);
backgroundState.torrentTrackers = updateAndGetTorrentTrackers(storedState);

if (storedState.taskFetchFailureReason) {
browser.browserAction.setIcon({
Expand Down
12 changes: 6 additions & 6 deletions test/test-torrent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import bencodec from 'bencodec';
import { expect } from 'chai';
import * as fs from "fs";
import * as path from "path"
import {setTrackers, addTrackersToMetaData} from "../src/background/actions/torrentTracker"

const tmpFile = path.join(__dirname, "./tmp.torrent")

Expand All @@ -11,7 +12,10 @@ const NEW_TRACKERS = [
'udp://4.3.2.1:2710/announce',
];

const torrent: any = bencodec.decode(fs.readFileSync(path.join(__dirname, "./test.torrent")));
setTrackers(NEW_TRACKERS);

const torrent_raw = fs.readFileSync(path.join(__dirname, "./test.torrent"));
const torrent: any = bencodec.decode(torrent_raw);
let oldTrackers = torrent['announce-list'].toString("utf8");
//console.log(oldTrackers);

Expand All @@ -29,11 +33,7 @@ describe("other locale messages", () => {
*/

//console.log('------------------------------------------')

NEW_TRACKERS.forEach(t => torrent['announce-list'].push([Buffer.from(t, 'utf8')]));
//console.log(torrent['announce-list'].toString("utf8"));

fs.writeFileSync(tmpFile, bencodec.encode(torrent));
fs.writeFileSync(tmpFile, addTrackersToMetaData(torrent_raw));


describe("other locale messages", () => {
Expand Down

0 comments on commit dca9286

Please sign in to comment.