|
|
@ -1,9 +1,5 @@
|
|
|
|
using System;
|
|
|
|
using System;
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
using System.Net;
|
|
|
|
using System.Net;
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
|
|
|
using com.LandonKey.SocksWebProxy;
|
|
|
|
|
|
|
|
using com.LandonKey.SocksWebProxy.Proxy;
|
|
|
|
|
|
|
|
using NzbDrone.Common.Cache;
|
|
|
|
using NzbDrone.Common.Cache;
|
|
|
|
using NzbDrone.Common.Extensions;
|
|
|
|
using NzbDrone.Common.Extensions;
|
|
|
|
|
|
|
|
|
|
|
@ -34,53 +30,36 @@ namespace NzbDrone.Common.Http.Proxy
|
|
|
|
|
|
|
|
|
|
|
|
private IWebProxy CreateWebProxy(HttpProxySettings proxySettings)
|
|
|
|
private IWebProxy CreateWebProxy(HttpProxySettings proxySettings)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
switch (proxySettings.Type)
|
|
|
|
var uri = GetProxyUri(proxySettings);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (uri == null)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
case ProxyType.Http:
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (proxySettings.Username.IsNotNullOrWhiteSpace() && proxySettings.Password.IsNotNullOrWhiteSpace())
|
|
|
|
if (proxySettings.Username.IsNotNullOrWhiteSpace() && proxySettings.Password.IsNotNullOrWhiteSpace())
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return new WebProxy(proxySettings.Host + ":" + proxySettings.Port, proxySettings.BypassLocalAddress, proxySettings.BypassListAsArray, new NetworkCredential(proxySettings.Username, proxySettings.Password));
|
|
|
|
return new WebProxy(uri, proxySettings.BypassLocalAddress, proxySettings.BypassListAsArray, new NetworkCredential(proxySettings.Username, proxySettings.Password));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return new WebProxy(proxySettings.Host + ":" + proxySettings.Port, proxySettings.BypassLocalAddress, proxySettings.BypassListAsArray);
|
|
|
|
return new WebProxy(uri, proxySettings.BypassLocalAddress, proxySettings.BypassListAsArray);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
case ProxyType.Socks4:
|
|
|
|
|
|
|
|
return new SocksWebProxy(new ProxyConfig(IPAddress.Loopback, GetNextFreePort(), GetProxyIpAddress(proxySettings.Host), proxySettings.Port, ProxyConfig.SocksVersion.Four, proxySettings.Username, proxySettings.Password), false);
|
|
|
|
|
|
|
|
case ProxyType.Socks5:
|
|
|
|
|
|
|
|
return new SocksWebProxy(new ProxyConfig(IPAddress.Loopback, GetNextFreePort(), GetProxyIpAddress(proxySettings.Host), proxySettings.Port, ProxyConfig.SocksVersion.Five, proxySettings.Username, proxySettings.Password), false);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static IPAddress GetProxyIpAddress(string host)
|
|
|
|
private Uri GetProxyUri(HttpProxySettings proxySettings)
|
|
|
|
{
|
|
|
|
|
|
|
|
IPAddress ipAddress;
|
|
|
|
|
|
|
|
if (!IPAddress.TryParse(host, out ipAddress))
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
try
|
|
|
|
switch (proxySettings.Type)
|
|
|
|
{
|
|
|
|
|
|
|
|
ipAddress = Dns.GetHostEntry(host).AddressList.OrderByDescending(a => a.AddressFamily == AddressFamily.InterNetwork).First();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException(string.Format("Unable to resolve proxy hostname '{0}' to a valid IP address.", host), e);
|
|
|
|
case ProxyType.Http:
|
|
|
|
}
|
|
|
|
return new Uri("http://" + proxySettings.Host + ":" + proxySettings.Port);
|
|
|
|
}
|
|
|
|
case ProxyType.Socks4:
|
|
|
|
|
|
|
|
return new Uri("socks4://" + proxySettings.Host + ":" + proxySettings.Port);
|
|
|
|
return ipAddress;
|
|
|
|
case ProxyType.Socks5:
|
|
|
|
|
|
|
|
return new Uri("socks5://" + proxySettings.Host + ":" + proxySettings.Port);
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static int GetNextFreePort()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var listener = new TcpListener(IPAddress.Loopback, 0);
|
|
|
|
|
|
|
|
listener.Start();
|
|
|
|
|
|
|
|
var port = ((IPEndPoint)listener.LocalEndpoint).Port;
|
|
|
|
|
|
|
|
listener.Stop();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return port;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|