mirror of
https://github.com/lidarr/lidarr.git
synced 2025-08-21 05:53:33 -07:00
AutoConfigure for SAB is setup, it works for systems with NzbDrone and SABnzbd on the same server only.
This commit is contained in:
parent
70bfc49b4e
commit
49a059bdea
7 changed files with 97 additions and 59 deletions
|
@ -1,6 +1,9 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.NetworkInformation;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using NzbDrone.Core.Model;
|
||||
|
@ -10,58 +13,91 @@ namespace NzbDrone.Core.Providers
|
|||
{
|
||||
public class AutoConfigureProvider
|
||||
{
|
||||
private HttpProvider _httpProvider;
|
||||
private ConfigProvider _configProvider;
|
||||
|
||||
public AutoConfigureProvider(HttpProvider httpProvider, ConfigProvider configProvider)
|
||||
public AutoConfigureProvider()
|
||||
{
|
||||
_httpProvider = httpProvider;
|
||||
_configProvider = configProvider;
|
||||
}
|
||||
|
||||
public SabnzbdInfoModel AutoConfigureSab(string username, string password)
|
||||
public SabnzbdInfoModel AutoConfigureSab()
|
||||
{
|
||||
//Get Output from Netstat
|
||||
var netStatOutput = String.Empty;
|
||||
//var port = GetSabnzbdPort(netStatOutput);
|
||||
var port = 2222;
|
||||
var apiKey = GetSabnzbdApiKey(port);
|
||||
var info = GetConnectionList();
|
||||
return FindApiKey(info);
|
||||
}
|
||||
|
||||
if (port > 0 && !String.IsNullOrEmpty(apiKey))
|
||||
private List<ConnectionInfoModel> GetConnectionList()
|
||||
{
|
||||
IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();
|
||||
var info =
|
||||
ipProperties.GetActiveTcpListeners().Select(
|
||||
p =>
|
||||
new ConnectionInfoModel
|
||||
{Address = p.Address.ToString().Replace("0.0.0.0", "127.0.0.1"), Port = p.Port}).Distinct().
|
||||
ToList();
|
||||
|
||||
info.RemoveAll(i => i.Port == 135);
|
||||
info.RemoveAll(i => i.Port == 139);
|
||||
info.RemoveAll(i => i.Port == 445);
|
||||
info.RemoveAll(i => i.Port == 3389);
|
||||
info.RemoveAll(i => i.Port == 5900);
|
||||
info.RemoveAll(i => i.Address.Contains("::"));
|
||||
|
||||
info.Reverse();
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
private SabnzbdInfoModel FindApiKey(List<ConnectionInfoModel> info)
|
||||
{
|
||||
foreach (var connection in info)
|
||||
{
|
||||
return new SabnzbdInfoModel
|
||||
{
|
||||
ApiKey = apiKey,
|
||||
Port = port,
|
||||
Username = username,
|
||||
Password = password
|
||||
};
|
||||
var apiKey = GetApiKey(connection.Address, connection.Port);
|
||||
if (!String.IsNullOrEmpty(apiKey))
|
||||
return new SabnzbdInfoModel {
|
||||
Host = connection.Address,
|
||||
Port = connection.Port,
|
||||
ApiKey = apiKey
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private int GetSabnzbdPort(string netstatOutput)
|
||||
{
|
||||
Regex regex = new Regex(@"^(?:TCP\W+127.0.0.1:(?<port>\d+\W+).+?\r\n\W+\[sabnzbd.exe\])", RegexOptions.IgnoreCase
|
||||
| RegexOptions.Compiled);
|
||||
var match = regex.Match(netstatOutput);
|
||||
var port = 0;
|
||||
Int32.TryParse(match.Groups["port"].Value, out port);
|
||||
|
||||
return port;
|
||||
}
|
||||
|
||||
private string GetSabnzbdApiKey(int port, string ipAddress = "127.0.0.1")
|
||||
private string GetApiKey(string ipAddress, int port)
|
||||
{
|
||||
var request = String.Format("http://{0}:{1}/config/general/", ipAddress, port);
|
||||
var result = _httpProvider.DownloadString(request);
|
||||
var result = DownloadString(request);
|
||||
|
||||
Regex regex = new Regex("\\<input\\Wtype\\=\\\"text\\\"\\Wid\\=\\\"apikey\\\"\\Wvalue\\=\\\"(?<apikey>\\w+)\\W", RegexOptions.IgnoreCase
|
||||
| RegexOptions.Compiled);
|
||||
Regex regex =
|
||||
new Regex("\\<input\\Wtype\\=\\\"text\\\"\\Wid\\=\\\"apikey\\\"\\Wvalue\\=\\\"(?<apikey>\\w+)\\W",
|
||||
RegexOptions.IgnoreCase
|
||||
| RegexOptions.Compiled);
|
||||
var match = regex.Match(result);
|
||||
|
||||
return match.Groups["apikey"].Value;
|
||||
if (match.Success)
|
||||
{
|
||||
return match.Groups["apikey"].Value;
|
||||
}
|
||||
|
||||
return String.Empty;
|
||||
}
|
||||
|
||||
private string DownloadString(string url)
|
||||
{
|
||||
try
|
||||
{
|
||||
var request = WebRequest.Create(url);
|
||||
request.Timeout = 2000;
|
||||
|
||||
var response = request.GetResponse();
|
||||
|
||||
var reader = new StreamReader(response.GetResponseStream());
|
||||
return reader.ReadToEnd();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Failed to get response from: {0}", url);
|
||||
Console.WriteLine(ex.Message, ex);
|
||||
}
|
||||
|
||||
return String.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue