public void Announce(InfoHash infoHash, int port) { Debug.WriteLine("DhtEngine: Announce (" + infoHash.ToHex() + ":" + port.ToString() + ")"); CheckDisposed(); Check.InfoHash(infoHash); new AnnounceTask(this, infoHash, port).Execute(); }
public void GetPeers(InfoHash infoHash) { CheckDisposed(); Check.InfoHash(infoHash); Debug.WriteLine("DhtEngine: GetPeers (" + infoHash.ToHex() + ")"); new GetPeersTask(this, infoHash).Execute(); }
private void StartDHT() { if (dhtInitialised) { return; } dhtInitialised = true; engine.DhtEngine.PeersFound += delegate(object o, PeersFoundEventArgs e) { DhtPeersFound(o, e); }; Debug.WriteLine("TorrentManager: Starting DHT Lookup (" + infohash.ToHex() + ")"); // First get some peers engine.DhtEngine.GetPeers(InfoHash); // Second, get peers every 10 minutes (if we need them) ClientEngine.MainLoop.QueueTimeout(TimeSpan.FromMinutes(10), delegate { // Torrent is no longer active if (!Mode.CanAcceptConnections) { return(false); } // Only use DHT if it hasn't been (temporarily?) disabled in settings if (CanUseDht && Peers.AvailablePeers.Count < Settings.MaxConnections) { engine.DhtEngine.Announce(InfoHash, engine.Settings.ListenPort); //announce ever done a get peers task //engine.DhtEngine.GetPeers(InfoHash); } return(true); }); }
public void MagnetLink() { InfoHash hash = Create(); string magnet = string.Format("magnet:?xt=urn:btih:{0}", hash.ToHex()); MagnetLink other = new MagnetLink(magnet); Assert.Equal(hash, other.InfoHash); }
public void InvalidMagnetLink3() { InfoHash hash = Create(); string magnet = string.Format("magnet:?xt=urn:btih:", hash.ToHex()); MagnetLink other = new MagnetLink(magnet); Assert.AreEqual(hash, other.InfoHash, "#1"); }
public void InvalidMagnetLink3() { Assert.Throws <FormatException>(() => { InfoHash hash = Create(); string magnet = string.Format("magnet:?xt=urn:btih:", hash.ToHex()); MagnetLink other = new MagnetLink(magnet); Assert.Equal(hash, other.InfoHash); }); }
public void HexTest() { InfoHash hash = Create(); string hex = hash.ToHex(); Assert.AreEqual(40, hex.Length, "#1"); InfoHash other = InfoHash.FromHex(hex); Assert.AreEqual(hash, other, "#2"); }
public void InfoHashTest() { MagnetLink link = new MagnetLink("magnet:?xt.1=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C"); Assert.Equal(InfoHash.FromBase32("YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C"), link.InfoHash); //base32 InfoHash initial = new InfoHash(System.Text.Encoding.ASCII.GetBytes("foobafoobafoobafooba")); link = new MagnetLink("magnet:?xt=urn:sha1:MZXW6YTBMZXW6YTBMZXW6YTBMZXW6YTB"); Assert.Equal(initial, link.InfoHash); //base40 = hex InfoHash hash = Create(); string hex = hash.ToHex(); link = new MagnetLink("magnet:?xt=urn:btih:" + hex); Assert.Equal(hash, link.InfoHash); }
/// <summary> /// Send an announce request for this InfoHash. /// </summary> /// <param name="infoHash"></param> /// <returns></returns> public async Task Announce(InfoHash infoHash) { var message = string.Format(BaseSearchString, Settings.ListenPort, infoHash.ToHex()); var data = Encoding.ASCII.GetBytes(message); // If there's another application on the system which joined the bittorrent LPD broadcast group, we'll be unable to // join it and will have a PortInUse error. However, we can still *send* broadcast messages using any UDP client. using (var sendingClient = new UdpClient()) foreach (var nic in NetworkInterface.GetAllNetworkInterfaces()) { try { //if (!nic.SupportsMulticast) continue; sendingClient.Client.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder(nic.GetIPProperties().GetIPv4Properties().Index)); await sendingClient.SendAsync(data, data.Length, BroadcastEndPoint); } catch { // If data can't be sent, just ignore the error } } }
async void ProcessQueue() { // Ensure this doesn't run on the UI thread as the networking calls can do some (partially) blocking operations. // Specifically 'NetworkInterface.GetAllNetworkInterfaces' is synchronous and can take hundreds of milliseconds. await MainLoop.SwitchToThreadpool(); await RateLimiterTask; using var sendingClient = new UdpClient(); var nics = NetworkInterface.GetAllNetworkInterfaces(); while (true) { InfoHash infoHash = null; lock (PendingAnnounces) { if (PendingAnnounces.Count == 0) { // Enforce a minimum delay before the next announce to avoid killing CPU by iterating network interfaces. RateLimiterTask = Task.Delay(1000); ProcessingAnnounces = false; break; } infoHash = PendingAnnounces.Dequeue(); } string message = string.Format(BaseSearchString, ListenPort, infoHash.ToHex()); byte[] data = Encoding.ASCII.GetBytes(message); foreach (var nic in nics) { try { sendingClient.Client.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder(nic.GetIPProperties().GetIPv4Properties().Index)); await sendingClient.SendAsync(data, data.Length, MulticastAddressV4).ConfigureAwait(false); } catch { // If data can't be sent, just ignore the error } } } }
private string GetTorrentPath(InfoHash hash) => Path.Combine(this.CachePath, $"{hash.ToHex()}.torrent");
public void InvalidMagnetLink4() { InfoHash hash = Create(); string magnet = string.Format("magnet:?xt=urn:btih:23526246235623564234365879634581726345981", hash.ToHex()); MagnetLink other = new MagnetLink(magnet); Assert.AreEqual(hash, other.InfoHash, "#1"); }
public TorrentHandler(InfoHash H, string DownloadDir = DOWNLOAD_DIR, bool ForceHash = false) { InitBase(DownloadDir); var CacheFile = Environment.ExpandEnvironmentVariables(TORRENT_DIR + $"\\{H.ToHex()}.torrent"); //Use cached torrent file if available if (!ForceHash && File.Exists(CacheFile)) { Torrent T = Torrent.Load(File.ReadAllBytes(CacheFile)); TM = new TorrentManager(T, Environment.ExpandEnvironmentVariables(DownloadDir), TS); } else { TM = new TorrentManager(H, Environment.ExpandEnvironmentVariables(DownloadDir), TS, Environment.ExpandEnvironmentVariables(TORRENT_DIR), InitTracker()); } Assign(); }
/// <summary> /// Returns the full path to the <see cref="FastResume"/> file for the specified torrent. This is /// where data will be written to, or loaded from, when <see cref="AutoSaveLoadFastResume"/> is enabled. /// </summary> /// <param name="infoHash">The infohash of the torrent</param> /// <returns></returns> public string GetFastResumePath(InfoHash infoHash) => Path.Combine(FastResumeCacheDirectory, $"{infoHash.ToHex ()}.fresume");
public void InvalidMagnetLink4() { Assert.Throws <FormatException>(() => { InfoHash hash = Create(); string magnet = string.Format("magnet:?xt=urn:btih:23526246235623564234365879634581726345981", hash.ToHex()); MagnetLink other = new MagnetLink(magnet); Assert.Equal(hash, other.InfoHash); }); }
internal string GetMetadataPath(InfoHash infoHash) => Path.Combine(MetadataCacheDirectory, infoHash.ToHex() + ".torrent");