forked from seansfkelley/nas-download-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding public tracker list for each download task.
- Loading branch information
Showing
4 changed files
with
57 additions
and
31 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
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); | ||
} |
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 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