-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
208 additions
and
8 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
ResourceMonitor/.idea/.idea.ResourceMonitor/.idea/contentModel.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
101 changes: 101 additions & 0 deletions
101
ResourceMonitor/ResourceMonitor.Test/TorrentServiceTests.cs
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 |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Refit; | ||
using ResourceMonitor.Services.Declaration; | ||
using ResourceMonitor.Services.Implementation; | ||
|
||
namespace ResourceMonitor.Test | ||
{ | ||
[TestClass] | ||
public class TorrentServiceTests | ||
{ | ||
[TestMethod] | ||
public void IsTorrentFileValid_Pass() | ||
{ | ||
var service = new TorrentService(null, new NullLogger<TorrentService>()); | ||
var torrentBytes = File.ReadAllBytes("valid.torrent"); | ||
Assert.IsTrue(service.IsTorrentFileValid(torrentBytes)); | ||
} | ||
|
||
[TestMethod] | ||
public void IsTorrentFileValid_NotPass() | ||
{ | ||
var service = new TorrentService(null, new NullLogger<TorrentService>()); | ||
var torrentBytes = File.ReadAllBytes("valid.torrent"); | ||
//修改一些文件内容 | ||
for (int i = 100; i < 200; i++) | ||
{ | ||
torrentBytes[i] = 0; | ||
} | ||
Assert.IsFalse(service.IsTorrentFileValid(torrentBytes)); | ||
} | ||
|
||
[TestMethod] | ||
public void NormalizeMagnetUrl_Pass_40() | ||
{ | ||
var service = new TorrentService(null, new NullLogger<TorrentService>()); | ||
var magnet = "magnet:?xt=urn:btih:08c2c30ca85cf78ed147488a431d9aee50824a7b"; | ||
var after = service.NormalizeMagnetUrl(magnet); | ||
Assert.AreEqual(magnet, after); | ||
} | ||
|
||
[TestMethod] | ||
public void NormalizeMagnetUrl_Pass_32() | ||
{ | ||
var service = new TorrentService(null, new NullLogger<TorrentService>()); | ||
var magnet = "magnet:?xt=urn:btih:BDBMGDFILT3Y5UKHJCFEGHM25ZIIEST3"; | ||
var after = service.NormalizeMagnetUrl(magnet); | ||
Assert.AreEqual("magnet:?xt=urn:btih:08c2c30ca85cf78ed147488a431d9aee50824a7b", after); | ||
} | ||
|
||
[TestMethod] | ||
public void NormalizeMagnetUrl_Pass_Tracker() | ||
{ | ||
var service = new TorrentService(null, new NullLogger<TorrentService>()); | ||
var magnet = | ||
"magnet:?xt=urn:btih:4cda49aa1c28db946e89ecb6e18482c8d347b41d&tr=udp://9.rarbg.to:2710/announce&tr=udp://9.rarbg.me:2710/announce&tr=http://tr.cili001.com:8070/announce&tr=http://tracker.trackerfix.com:80/announce&tr=udp://open.demonii.com:1337&tr=udp://tracker.opentrackr.org:1337/announce&tr=udp://p4p.arenabg.com:1337&tr=wss://tracker.openwebtorrent.com&tr=wss://tracker.btorrent.xyz&tr=wss://tracker.fastcast.nz"; | ||
var after = service.NormalizeMagnetUrl(magnet); | ||
Assert.AreEqual("magnet:?xt=urn:btih:4cda49aa1c28db946e89ecb6e18482c8d347b41d", after); | ||
} | ||
|
||
[TestMethod] | ||
public void GetHash_Pass_32() | ||
{ | ||
var service = new TorrentService(null, new NullLogger<TorrentService>()); | ||
var magnet = "magnet:?xt=urn:btih:BDBMGDFILT3Y5UKHJCFEGHM25ZIIEST3"; | ||
var hash = service.GetHash(magnet); | ||
Assert.AreEqual("08c2c30ca85cf78ed147488a431d9aee50824a7b", hash); | ||
} | ||
|
||
[TestMethod] | ||
public void GetHash_Pass_40() | ||
{ | ||
var service = new TorrentService(null, new NullLogger<TorrentService>()); | ||
var magnet = "magnet:?xt=urn:btih:08c2c30ca85cf78ed147488a431d9aee50824a7b"; | ||
var hash = service.GetHash(magnet); | ||
Assert.AreEqual("08c2c30ca85cf78ed147488a431d9aee50824a7b", hash); | ||
} | ||
|
||
[TestMethod] | ||
public void GetHash_NotPass_UpperCase() | ||
{ | ||
var service = new TorrentService(null, new NullLogger<TorrentService>()); | ||
var magnet = "magnet:?xt=urn:btih:08c2c30ca85cf78ed147488a431d9aee50824a7b"; | ||
var hash = service.GetHash(magnet); | ||
Assert.AreNotEqual("08C2C30CA85CF78ED147488A431D9AEE50824A7B", hash); | ||
} | ||
|
||
//[TestMethod] | ||
public async Task DownloadTorrent_Pass_Online() | ||
{ | ||
var api = RestService.For<IMagnetApi>("https://m2t.chinacloudsites.cn"); | ||
var service = new TorrentService(api, new NullLogger<TorrentService>()); | ||
var magnet = "magnet:?xt=urn:btih:08c2c30ca85cf78ed147488a431d9aee50824a7b"; | ||
var torrentBytes = await service.DownloadTorrent(magnet); | ||
Assert.IsTrue(service.IsTorrentFileValid(torrentBytes)); | ||
} | ||
} | ||
} |
Binary file not shown.
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
12 changes: 12 additions & 0 deletions
12
ResourceMonitor/ResourceMonitor/Services/Declaration/IMagnetApi.cs
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Refit; | ||
|
||
namespace ResourceMonitor.Services.Declaration | ||
{ | ||
public interface IMagnetApi | ||
{ | ||
[Get("/Magnet/Parse")] | ||
Task<HttpContent> ParseMagnet(string magnet); | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
ResourceMonitor/ResourceMonitor/Services/Implementation/TorrentService.cs
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using OctoTorrent; | ||
using OctoTorrent.Common; | ||
using ResourceMonitor.Services.Declaration; | ||
|
||
namespace ResourceMonitor.Services.Implementation | ||
{ | ||
public class TorrentService : ITorrentService | ||
{ | ||
private readonly IMagnetApi _magnetApi; | ||
private readonly ILogger<TorrentService> _logger; | ||
|
||
public TorrentService(IMagnetApi magnetApi, ILogger<TorrentService> logger) | ||
{ | ||
_magnetApi = magnetApi; | ||
_logger = logger; | ||
} | ||
|
||
public bool IsTorrentFileValid(byte[] torrentBytes) | ||
{ | ||
return Torrent.TryLoad(torrentBytes, out var _); | ||
} | ||
|
||
public string NormalizeMagnetUrl(string magnet) | ||
{ | ||
return "magnet:?xt=urn:btih:" + GetHash(magnet); | ||
} | ||
|
||
public string GetHash(string magnet) | ||
{ | ||
return InfoHash.FromMagnetLink(magnet).ToHex().ToLowerInvariant(); | ||
} | ||
|
||
public async Task<byte[]> DownloadTorrent(string magnet) | ||
{ | ||
try | ||
{ | ||
var content = await _magnetApi.ParseMagnet(magnet); | ||
var torrent = await content.ReadAsByteArrayAsync(); | ||
return torrent; | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogDebug(ex, "下载种子文件时出错"); | ||
return null; | ||
} | ||
} | ||
} | ||
} |
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