2018-05-01 12:55:09 +00:00
|
|
|
|
using BencodeNET.Parsing;
|
2018-05-01 12:03:16 +00:00
|
|
|
|
using Jackett.Common.Models.Config;
|
|
|
|
|
using Jackett.Common.Services.Interfaces;
|
|
|
|
|
using Jackett.Common.Utils;
|
2018-05-12 02:44:47 +00:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2018-05-01 12:55:09 +00:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.WebUtilities;
|
2018-05-01 12:03:16 +00:00
|
|
|
|
using NLog;
|
2018-05-01 12:55:09 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2018-05-01 12:03:16 +00:00
|
|
|
|
|
2018-05-01 12:55:09 +00:00
|
|
|
|
namespace Jackett.Server.Controllers
|
2018-05-01 12:03:16 +00:00
|
|
|
|
{
|
2018-05-12 02:44:47 +00:00
|
|
|
|
[AllowAnonymous]
|
2018-05-01 12:55:09 +00:00
|
|
|
|
[ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)]
|
|
|
|
|
[Route("dl/{indexerID}")]
|
|
|
|
|
public class DownloadController : Controller
|
2018-05-01 12:03:16 +00:00
|
|
|
|
{
|
|
|
|
|
private ServerConfig config;
|
|
|
|
|
private Logger logger;
|
2018-05-01 12:55:09 +00:00
|
|
|
|
private IIndexerManagerService indexerService;
|
2018-05-01 12:03:16 +00:00
|
|
|
|
private IProtectionService protectionService;
|
|
|
|
|
|
|
|
|
|
public DownloadController(IIndexerManagerService i, Logger l, IProtectionService ps, ServerConfig serverConfig)
|
|
|
|
|
{
|
|
|
|
|
config = serverConfig;
|
|
|
|
|
logger = l;
|
|
|
|
|
indexerService = i;
|
|
|
|
|
protectionService = ps;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
2018-05-01 12:55:09 +00:00
|
|
|
|
public async Task<IActionResult> Download(string indexerID, string path, string jackett_apikey, string file)
|
2018-05-01 12:03:16 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var indexer = indexerService.GetWebIndexer(indexerID);
|
|
|
|
|
|
|
|
|
|
if (!indexer.IsConfigured)
|
|
|
|
|
{
|
|
|
|
|
logger.Warn(string.Format("Rejected a request to {0} which is unconfigured.", indexer.DisplayName));
|
2018-05-01 12:55:09 +00:00
|
|
|
|
return Forbid("This indexer is not configured.");
|
2018-05-01 12:03:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-01 12:55:09 +00:00
|
|
|
|
path = Encoding.UTF8.GetString(WebEncoders.Base64UrlDecode(path));
|
2018-05-01 12:03:16 +00:00
|
|
|
|
path = protectionService.UnProtect(path);
|
|
|
|
|
|
|
|
|
|
if (config.APIKey != jackett_apikey)
|
2018-05-01 12:55:09 +00:00
|
|
|
|
return Unauthorized();
|
2018-05-01 12:03:16 +00:00
|
|
|
|
|
|
|
|
|
var target = new Uri(path, UriKind.RelativeOrAbsolute);
|
|
|
|
|
var downloadBytes = await indexer.Download(target);
|
|
|
|
|
|
|
|
|
|
// handle magnet URLs
|
|
|
|
|
if (downloadBytes.Length >= 7
|
|
|
|
|
&& downloadBytes[0] == 0x6d // m
|
|
|
|
|
&& downloadBytes[1] == 0x61 // a
|
|
|
|
|
&& downloadBytes[2] == 0x67 // g
|
|
|
|
|
&& downloadBytes[3] == 0x6e // n
|
|
|
|
|
&& downloadBytes[4] == 0x65 // e
|
|
|
|
|
&& downloadBytes[5] == 0x74 // t
|
|
|
|
|
&& downloadBytes[6] == 0x3a // :
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
var magneturi = Encoding.UTF8.GetString(downloadBytes);
|
2018-05-01 12:55:09 +00:00
|
|
|
|
return Redirect(new Uri(magneturi).ToString());
|
2018-05-01 12:03:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This will fix torrents where the keys are not sorted, and thereby not supported by Sonarr.
|
|
|
|
|
var parser = new BencodeParser();
|
|
|
|
|
var torrentDictionary = parser.Parse(downloadBytes);
|
|
|
|
|
byte[] sortedDownloadBytes = torrentDictionary.EncodeAsBytes();
|
|
|
|
|
|
2018-05-01 12:55:09 +00:00
|
|
|
|
string fileName = StringUtil.MakeValidFileName(file, '_', false) + ".torrent"; // call MakeValidFileName again to avoid any kind of injection attack
|
|
|
|
|
|
|
|
|
|
return File(sortedDownloadBytes, "application/x-bittorrent", fileName);
|
2018-05-01 12:03:16 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
logger.Error(e, "Error downloading " + indexerID + " " + path);
|
2018-05-01 12:55:09 +00:00
|
|
|
|
return NotFound();
|
2018-05-01 12:03:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|