1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-04 10:02:40 +01:00

AutoConfigure for SAB is setup, it works for systems with NzbDrone and SABnzbd on the same server only.

This commit is contained in:
Mark McDowall 2011-05-10 23:46:26 -07:00
parent 70bfc49b4e
commit 49a059bdea
7 changed files with 97 additions and 59 deletions

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NzbDrone.Core.Model
{
public class ConnectionInfoModel
{
public string Address { get; set; }
public int Port { get; set; }
}
}

View File

@ -7,9 +7,8 @@ namespace NzbDrone.Core.Model
{ {
public class SabnzbdInfoModel public class SabnzbdInfoModel
{ {
public string ApiKey { get; set; } public string Host { get; set; }
public int Port { get; set; } public int Port { get; set; }
public string Username { get; set; } public string ApiKey { get; set; }
public string Password { get; set; }
} }
} }

View File

@ -166,6 +166,7 @@
<Compile Include="Instrumentation\SubsonicTarget.cs" /> <Compile Include="Instrumentation\SubsonicTarget.cs" />
<Compile Include="Instrumentation\ExceptioneerTarget.cs" /> <Compile Include="Instrumentation\ExceptioneerTarget.cs" />
<Compile Include="Instrumentation\NlogWriter.cs" /> <Compile Include="Instrumentation\NlogWriter.cs" />
<Compile Include="Model\ConnectionInfoModel.cs" />
<Compile Include="Model\ExternalNotificationType.cs" /> <Compile Include="Model\ExternalNotificationType.cs" />
<Compile Include="Model\IndexerType.cs" /> <Compile Include="Model\IndexerType.cs" />
<Compile Include="Model\LanguageType.cs" /> <Compile Include="Model\LanguageType.cs" />

View File

@ -1,6 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using NzbDrone.Core.Model; using NzbDrone.Core.Model;
@ -10,58 +13,91 @@ namespace NzbDrone.Core.Providers
{ {
public class AutoConfigureProvider public class AutoConfigureProvider
{ {
private HttpProvider _httpProvider; public AutoConfigureProvider()
private ConfigProvider _configProvider;
public AutoConfigureProvider(HttpProvider httpProvider, ConfigProvider configProvider)
{ {
_httpProvider = httpProvider;
_configProvider = configProvider;
} }
public SabnzbdInfoModel AutoConfigureSab(string username, string password) public SabnzbdInfoModel AutoConfigureSab()
{ {
//Get Output from Netstat var info = GetConnectionList();
var netStatOutput = String.Empty; return FindApiKey(info);
//var port = GetSabnzbdPort(netStatOutput); }
var port = 2222;
var apiKey = GetSabnzbdApiKey(port);
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 var apiKey = GetApiKey(connection.Address, connection.Port);
{ if (!String.IsNullOrEmpty(apiKey))
ApiKey = apiKey, return new SabnzbdInfoModel {
Port = port, Host = connection.Address,
Username = username, Port = connection.Port,
Password = password ApiKey = apiKey
}; };
} }
return null; return null;
} }
private int GetSabnzbdPort(string netstatOutput) private string GetApiKey(string ipAddress, int port)
{
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")
{ {
var request = String.Format("http://{0}:{1}/config/general/", ipAddress, 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 Regex regex =
| RegexOptions.Compiled); new Regex("\\<input\\Wtype\\=\\\"text\\\"\\Wid\\=\\\"apikey\\\"\\Wvalue\\=\\\"(?<apikey>\\w+)\\W",
RegexOptions.IgnoreCase
| RegexOptions.Compiled);
var match = regex.Match(result); 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;
} }
} }
} }

View File

@ -296,26 +296,18 @@ public JsonResult DeleteQualityProfile(int profileId)
return new JsonResult { Data = "ok" }; return new JsonResult { Data = "ok" };
} }
public JsonResult AutoConfigureSab(string username, string password) public JsonResult AutoConfigureSab()
{ {
SabnzbdInfoModel info;
try try
{ {
//info = _autoConfigureProvider.AutoConfigureSab(username, password); var info = _autoConfigureProvider.AutoConfigureSab();
info = new SabnzbdInfoModel return Json(info, JsonRequestBehavior.AllowGet);
{
ApiKey = "123456",
Port = 2222
};
} }
catch (Exception) catch (Exception)
{ {
return new JsonResult { Data = "failed" }; return new JsonResult { Data = "failed" };
} }
return Json(info);
} }
[HttpPost] [HttpPost]

View File

@ -599,7 +599,6 @@
<Content Include="Content\jquery.jgrowl.css" /> <Content Include="Content\jquery.jgrowl.css" />
<Content Include="Content\notibar.css" /> <Content Include="Content\notibar.css" />
<Content Include="Content\style.css" /> <Content Include="Content\style.css" />
<Content Include="Content\thickbox.css" />
<Content Include="Content\XbmcNotification.png" /> <Content Include="Content\XbmcNotification.png" />
<Content Include="favicon.ico" /> <Content Include="favicon.ico" />
<Content Include="Global.asax" /> <Content Include="Global.asax" />
@ -643,7 +642,6 @@
<Content Include="Scripts\jquery-tgc-countdown-1.0.js" /> <Content Include="Scripts\jquery-tgc-countdown-1.0.js" />
<Content Include="Scripts\jquery.simpledropdown.js" /> <Content Include="Scripts\jquery.simpledropdown.js" />
<Content Include="Scripts\Notification.js" /> <Content Include="Scripts\Notification.js" />
<Content Include="Scripts\thickbox-compressed.js" />
<Content Include="Views\AddSeries\AddExisting.cshtml" /> <Content Include="Views\AddSeries\AddExisting.cshtml" />
<Content Include="Views\AddSeries\AddNew.cshtml" /> <Content Include="Views\AddSeries\AddNew.cshtml" />
<Content Include="Views\AddSeries\AddSeriesItem.cshtml" /> <Content Include="Views\AddSeries\AddSeriesItem.cshtml" />

View File

@ -113,7 +113,7 @@
<fieldset class="sub-field"> <fieldset class="sub-field">
<legend>SABnzbd</legend> <legend>SABnzbd</legend>
@*<button type="button" onclick="autoConfigureSab()">Auto-Configure</button>*@ <button type="button" onclick="autoConfigureSab()">Auto-Configure</button>
<div class="config-section"> <div class="config-section">
<div class="config-group"> <div class="config-group">
@ -223,9 +223,9 @@
function autoConfigureSab() { function autoConfigureSab() {
$.ajax({ $.ajax({
type: "POST", type: "GET",
url: autoConfigureSabUrl, url: autoConfigureSabUrl,
data: jQuery.param({ username: $('#SabUsername').val(), password: $('#SabPassword').val() }), //data: jQuery.param({ username: $('#SabUsername').val(), password: $('#SabPassword').val() }),
error: function (req, status, error) { error: function (req, status, error) {
alert("Sorry! We could not autoconfigure SABnzbd for you"); alert("Sorry! We could not autoconfigure SABnzbd for you");
}, },
@ -233,10 +233,9 @@
}); });
function autoConfigureSuccess(data) { function autoConfigureSuccess(data) {
$('#SabApiKey').val(data.ApiKey); $('#SabHost').val(data.Host);
$('#SabPort').val(data.Port); $('#SabPort').val(data.Port);
$('#SabUsername').val(data.Username); $('#SabApiKey').val(data.ApiKey);
$('#SabPassword').val(data.Password);
} }
} }
</script> </script>