1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2025-01-10 23:29:53 +02:00

New: Bypass IP addresses ranges in proxies

This commit is contained in:
Bogdan 2024-08-23 19:55:16 +03:00 committed by Mark McDowall
parent 846333ddf0
commit 402db9128c
3 changed files with 15 additions and 2 deletions

View File

@ -5,6 +5,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="DryIoc.dll" Version="5.4.3" /> <PackageReference Include="DryIoc.dll" Version="5.4.3" />
<PackageReference Include="IPAddressRange" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.1" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="6.0.2" /> <PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="6.0.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />

View File

@ -12,7 +12,7 @@ public class HttpProxySettingsProviderFixture : TestBase<HttpProxySettingsProvid
{ {
private HttpProxySettings GetProxySettings() private HttpProxySettings GetProxySettings()
{ {
return new HttpProxySettings(ProxyType.Socks5, "localhost", 8080, "*.httpbin.org,google.com", true, null, null); return new HttpProxySettings(ProxyType.Socks5, "localhost", 8080, "*.httpbin.org,google.com,172.16.0.0/12", true, null, null);
} }
[Test] [Test]
@ -23,6 +23,7 @@ public void should_bypass_proxy()
Subject.ShouldProxyBeBypassed(settings, new HttpUri("http://eu.httpbin.org/get")).Should().BeTrue(); Subject.ShouldProxyBeBypassed(settings, new HttpUri("http://eu.httpbin.org/get")).Should().BeTrue();
Subject.ShouldProxyBeBypassed(settings, new HttpUri("http://google.com/get")).Should().BeTrue(); Subject.ShouldProxyBeBypassed(settings, new HttpUri("http://google.com/get")).Should().BeTrue();
Subject.ShouldProxyBeBypassed(settings, new HttpUri("http://localhost:8654/get")).Should().BeTrue(); Subject.ShouldProxyBeBypassed(settings, new HttpUri("http://localhost:8654/get")).Should().BeTrue();
Subject.ShouldProxyBeBypassed(settings, new HttpUri("http://172.21.0.1:8989/api/v3/indexer/schema")).Should().BeTrue();
} }
[Test] [Test]
@ -31,6 +32,7 @@ public void should_not_bypass_proxy()
var settings = GetProxySettings(); var settings = GetProxySettings();
Subject.ShouldProxyBeBypassed(settings, new HttpUri("http://bing.com/get")).Should().BeFalse(); Subject.ShouldProxyBeBypassed(settings, new HttpUri("http://bing.com/get")).Should().BeFalse();
Subject.ShouldProxyBeBypassed(settings, new HttpUri("http://172.3.0.1:8989/api/v3/indexer/schema")).Should().BeFalse();
} }
} }
} }

View File

@ -1,5 +1,7 @@
using System; using System;
using System.Linq;
using System.Net; using System.Net;
using NetTools;
using NzbDrone.Common.Http; using NzbDrone.Common.Http;
using NzbDrone.Common.Http.Proxy; using NzbDrone.Common.Http.Proxy;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
@ -52,7 +54,15 @@ public bool ShouldProxyBeBypassed(HttpProxySettings proxySettings, HttpUri url)
// We are utilising the WebProxy implementation here to save us having to reimplement it. This way we use Microsofts implementation // We are utilising the WebProxy implementation here to save us having to reimplement it. This way we use Microsofts implementation
var proxy = new WebProxy(proxySettings.Host + ":" + proxySettings.Port, proxySettings.BypassLocalAddress, proxySettings.BypassListAsArray); var proxy = new WebProxy(proxySettings.Host + ":" + proxySettings.Port, proxySettings.BypassLocalAddress, proxySettings.BypassListAsArray);
return proxy.IsBypassed((Uri)url); return proxy.IsBypassed((Uri)url) || IsBypassedByIpAddressRange(proxySettings.BypassListAsArray, url.Host);
}
private static bool IsBypassedByIpAddressRange(string[] bypassList, string host)
{
return bypassList.Any(bypass =>
IPAddressRange.TryParse(bypass, out var ipAddressRange) &&
IPAddress.TryParse(host, out var ipAddress) &&
ipAddressRange.Contains(ipAddress));
} }
} }
} }