|
|
@ -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)
|
|
|
|
public SabnzbdInfoModel AutoConfigureSab()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_httpProvider = httpProvider;
|
|
|
|
var info = GetConnectionList();
|
|
|
|
_configProvider = configProvider;
|
|
|
|
return FindApiKey(info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public SabnzbdInfoModel AutoConfigureSab(string username, string password)
|
|
|
|
private List<ConnectionInfoModel> GetConnectionList()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
//Get Output from Netstat
|
|
|
|
IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();
|
|
|
|
var netStatOutput = String.Empty;
|
|
|
|
var info =
|
|
|
|
//var port = GetSabnzbdPort(netStatOutput);
|
|
|
|
ipProperties.GetActiveTcpListeners().Select(
|
|
|
|
var port = 2222;
|
|
|
|
p =>
|
|
|
|
var apiKey = GetSabnzbdApiKey(port);
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (port > 0 && !String.IsNullOrEmpty(apiKey))
|
|
|
|
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
|
|
|
|
var request = String.Format("http://{0}:{1}/config/general/", ipAddress, port);
|
|
|
|
| RegexOptions.Compiled);
|
|
|
|
var result = DownloadString(request);
|
|
|
|
var match = regex.Match(netstatOutput);
|
|
|
|
|
|
|
|
var port = 0;
|
|
|
|
Regex regex =
|
|
|
|
Int32.TryParse(match.Groups["port"].Value, out port);
|
|
|
|
new Regex("\\<input\\Wtype\\=\\\"text\\\"\\Wid\\=\\\"apikey\\\"\\Wvalue\\=\\\"(?<apikey>\\w+)\\W",
|
|
|
|
|
|
|
|
RegexOptions.IgnoreCase
|
|
|
|
|
|
|
|
| RegexOptions.Compiled);
|
|
|
|
|
|
|
|
var match = regex.Match(result);
|
|
|
|
|
|
|
|
|
|
|
|
return port;
|
|
|
|
if (match.Success)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return match.Groups["apikey"].Value;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return String.Empty;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private string GetSabnzbdApiKey(int port, string ipAddress = "127.0.0.1")
|
|
|
|
private string DownloadString(string url)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var request = String.Format("http://{0}:{1}/config/general/", ipAddress, port);
|
|
|
|
try
|
|
|
|
var result = _httpProvider.DownloadString(request);
|
|
|
|
{
|
|
|
|
|
|
|
|
var request = WebRequest.Create(url);
|
|
|
|
|
|
|
|
request.Timeout = 2000;
|
|
|
|
|
|
|
|
|
|
|
|
Regex regex = new Regex("\\<input\\Wtype\\=\\\"text\\\"\\Wid\\=\\\"apikey\\\"\\Wvalue\\=\\\"(?<apikey>\\w+)\\W", RegexOptions.IgnoreCase
|
|
|
|
var response = request.GetResponse();
|
|
|
|
| RegexOptions.Compiled);
|
|
|
|
|
|
|
|
var match = regex.Match(result);
|
|
|
|
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 match.Groups["apikey"].Value;
|
|
|
|
return String.Empty;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|