Skip to content

Commit 46e1baf

Browse files
authored
Revert "Fix reachable and IP leak test (#44)" (#46)
This reverts commit 3f5d7bb.
1 parent 3f5d7bb commit 46e1baf

File tree

5 files changed

+43
-41
lines changed

5 files changed

+43
-41
lines changed

UmlautAdaptarr/Program.cs

+7-16
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88

99
internal class Program
1010
{
11-
private static void Main(string[] args) {
12-
MainAsync(args).Wait();
13-
}
14-
15-
private static async Task MainAsync(string[] args)
11+
private static void Main(string[] args)
1612
{
13+
Helper.ShowLogo();
14+
Helper.ShowInformation();
1715
// TODO:
1816
// add option to sort by nzb age
1917
var builder = WebApplication.CreateBuilder(args);
@@ -45,9 +43,9 @@ private static async Task MainAsync(string[] args)
4543
builder.AddTitleLookupService();
4644
builder.Services.AddSingleton<SearchItemLookupService>();
4745
builder.Services.AddSingleton<TitleMatchingService>();
48-
await builder.AddSonarrSupport();
49-
await builder.AddLidarrSupport();
50-
await builder.AddReadarrSupport();
46+
builder.AddSonarrSupport();
47+
builder.AddLidarrSupport();
48+
builder.AddReadarrSupport();
5149
builder.Services.AddSingleton<CacheService>();
5250
builder.Services.AddSingleton<ProxyRequestService>();
5351
builder.Services.AddSingleton<ArrApplicationFactory>();
@@ -56,13 +54,6 @@ private static async Task MainAsync(string[] args)
5654

5755
var app = builder.Build();
5856

59-
Helper.ShowLogo();
60-
61-
if (app.Configuration.GetValue<bool>("IpLeakTest:Enabled"))
62-
{
63-
await Helper.ShowInformation();
64-
}
65-
6657
GlobalStaticLogger.Initialize(app.Services.GetService<ILoggerFactory>()!);
6758
app.UseHttpsRedirection();
6859
app.UseAuthorization();
@@ -118,4 +109,4 @@ private static void ConfigureLogger(ConfigurationManager configuration)
118109
//.Enrich.With(new ApiKeyMaskingEnricher("appsettings.json")) // TODO - Not working currently
119110
.CreateLogger();
120111
}
121-
}
112+
}

UmlautAdaptarr/Utilities/Helper.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ public static void ShowLogo()
1111
"\r\n _ _ _ _ ___ _ _ \r\n| | | | | | | | / _ \\ | | | | \r\n| | | |_ __ ___ | | __ _ _ _| |_/ /_\\ \\ __| | __ _ _ __ | |_ __ _ _ __ _ __ \r\n| | | | '_ ` _ \\| |/ _` | | | | __| _ |/ _` |/ _` | '_ \\| __/ _` | '__| '__|\r\n| |_| | | | | | | | (_| | |_| | |_| | | | (_| | (_| | |_) | || (_| | | | | \r\n \\___/|_| |_| |_|_|\\__,_|\\__,_|\\__\\_| |_/\\__,_|\\__,_| .__/ \\__\\__,_|_| |_| \r\n | | \r\n |_| \r\n");
1212
}
1313

14-
public static async Task ShowInformation()
14+
public static void ShowInformation()
1515
{
1616
Console.WriteLine("--------------------------[IP Leak Test]-----------------------------");
17-
var ipInfo = await GetPublicIpAddressInfoAsync();
17+
var ipInfo = GetPublicIpAddressInfoAsync().GetAwaiter().GetResult();
1818

1919
if (ipInfo != null)
2020
{

UmlautAdaptarr/Utilities/ServicesExtensions.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static class ServicesExtensions
2929
/// <param name="builder">The <see cref="WebApplicationBuilder" /> to configure the service collection.</param>
3030
/// <param name="sectionName">The name of the configuration section containing service options.</param>
3131
/// <returns>The configured <see cref="WebApplicationBuilder" />.</returns>
32-
private static async Task<WebApplicationBuilder> AddServicesWithOptions<TOptions, TService, TInterface>(
32+
private static WebApplicationBuilder AddServicesWithOptions<TOptions, TService, TInterface>(
3333
this WebApplicationBuilder builder, string sectionName)
3434
where TOptions : class, new()
3535
where TService : class, TInterface
@@ -57,9 +57,9 @@ private static async Task<WebApplicationBuilder> AddServicesWithOptions<TOptions
5757

5858
foreach (var option in optionsArray)
5959
{
60-
GlobalInstanceOptionsValidator validator = new();
60+
GlobalInstanceOptionsValidator validator = new GlobalInstanceOptionsValidator();
6161

62-
var results = await validator.ValidateAsync(option as GlobalInstanceOptions);
62+
var results = validator.Validate(option as GlobalInstanceOptions);
6363

6464
if (!results.IsValid)
6565
{
@@ -143,7 +143,7 @@ private static WebApplicationBuilder AddServiceWithOptions<TOptions, TService>(t
143143
/// </summary>
144144
/// <param name="builder">The <see cref="WebApplicationBuilder" /> to configure the service collection.</param>
145145
/// <returns>The configured <see cref="WebApplicationBuilder" />.</returns>
146-
public static Task<WebApplicationBuilder> AddSonarrSupport(this WebApplicationBuilder builder)
146+
public static WebApplicationBuilder AddSonarrSupport(this WebApplicationBuilder builder)
147147
{
148148
// builder.Serviceses.AddSingleton<IOptionsMonitoSonarrInstanceOptionsns>, OptionsMonitoSonarrInstanceOptionsns>>();
149149
return builder.AddServicesWithOptions<SonarrInstanceOptions, SonarrClient, IArrApplication>("Sonarr");
@@ -154,7 +154,7 @@ public static Task<WebApplicationBuilder> AddSonarrSupport(this WebApplicationBu
154154
/// </summary>
155155
/// <param name="builder">The <see cref="WebApplicationBuilder" /> to configure the service collection.</param>
156156
/// <returns>The configured <see cref="WebApplicationBuilder" />.</returns>
157-
public static Task<WebApplicationBuilder> AddLidarrSupport(this WebApplicationBuilder builder)
157+
public static WebApplicationBuilder AddLidarrSupport(this WebApplicationBuilder builder)
158158
{
159159
return builder.AddServicesWithOptions<LidarrInstanceOptions, LidarrClient, IArrApplication>("Lidarr");
160160
}
@@ -164,7 +164,7 @@ public static Task<WebApplicationBuilder> AddLidarrSupport(this WebApplicationBu
164164
/// </summary>
165165
/// <param name="builder">The <see cref="WebApplicationBuilder" /> to configure the service collection.</param>
166166
/// <returns>The configured <see cref="WebApplicationBuilder" />.</returns>
167-
public static Task<WebApplicationBuilder> AddReadarrSupport(this WebApplicationBuilder builder)
167+
public static WebApplicationBuilder AddReadarrSupport(this WebApplicationBuilder builder)
168168
{
169169
return builder.AddServicesWithOptions<ReadarrInstanceOptions, ReadarrClient, IArrApplication>("Readarr");
170170
}

UmlautAdaptarr/Validator/GlobalInstanceOptionsValidator.cs

+28-14
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ namespace UmlautAdaptarr.Validator;
66

77
public class GlobalInstanceOptionsValidator : AbstractValidator<GlobalInstanceOptions>
88
{
9-
private readonly static HttpClient httpClient = new() {
10-
Timeout = TimeSpan.FromSeconds(3)
11-
};
12-
139
public GlobalInstanceOptionsValidator()
1410
{
1511
RuleFor(x => x.Enabled).NotNull();
@@ -18,14 +14,12 @@ public GlobalInstanceOptionsValidator()
1814
{
1915
RuleFor(x => x.Host)
2016
.NotEmpty().WithMessage("Host is required when Enabled is true.")
21-
.Must(BeAValidUrl).WithMessage("Host/Url must start with http:// or https:// and be a valid address.");
17+
.Must(BeAValidUrl).WithMessage("Host/Url must start with http:// or https:// and be a valid address.")
18+
.Must(BeReachable)
19+
.WithMessage("Host/Url is not reachable. Please check your Host or your UmlautAdaptrr Settings");
2220

2321
RuleFor(x => x.ApiKey)
2422
.NotEmpty().WithMessage("ApiKey is required when Enabled is true.");
25-
26-
RuleFor(x => x)
27-
.MustAsync(BeReachable)
28-
.WithMessage("Host/Url is not reachable. Please check your Host or your UmlautAdaptrr Settings");
2923
});
3024
}
3125

@@ -35,30 +29,50 @@ private bool BeAValidUrl(string url)
3529
&& (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
3630
}
3731

38-
private static async Task<bool> BeReachable(GlobalInstanceOptions opts, CancellationToken cancellationToken)
32+
private static bool BeReachable(string url)
3933
{
4034
var endTime = DateTime.Now.AddMinutes(3);
4135
var reachable = false;
42-
var url = $"{opts.Host}/api?apikey={opts.ApiKey}";
4336

4437
while (DateTime.Now < endTime)
4538
{
4639
try
4740
{
48-
using var response = await httpClient.GetAsync(url, cancellationToken);
41+
// TODO use HttpClient here
42+
var request = (HttpWebRequest)WebRequest.Create(url);
43+
request.AllowAutoRedirect = false;
44+
request.Timeout = 3000;
45+
using var response = (HttpWebResponse)request.GetResponse();
4946
reachable = response.StatusCode == HttpStatusCode.OK;
50-
if (response.IsSuccessStatusCode)
47+
if (reachable)
5148
{
5249
break;
5350
}
51+
// If status is 301/302 (Found), follow the redirect manually
52+
else if (response.StatusCode == HttpStatusCode.MovedPermanently || response.StatusCode == HttpStatusCode.Found)
53+
{
54+
var redirectUrl = response.Headers["Location"]; // Get the redirect URL
55+
if (!string.IsNullOrEmpty(redirectUrl))
56+
{
57+
// Create a new request for the redirected URL
58+
var redirectRequest = (HttpWebRequest)WebRequest.Create(redirectUrl);
59+
redirectRequest.Timeout = 3000;
60+
using var redirectResponse = (HttpWebResponse)redirectRequest.GetResponse();
61+
reachable = redirectResponse.StatusCode == HttpStatusCode.OK;
62+
if (reachable)
63+
{
64+
break;
65+
}
66+
}
67+
}
5468
}
5569
catch
5670
{
5771

5872
}
5973

6074
// Wait for 15 seconds for next try
61-
Console.WriteLine($"The URL \"{opts.Host}\" is not reachable. Next attempt in 15 seconds...");
75+
Console.WriteLine($"The URL \"{url}\" is not reachable. Next attempt in 15 seconds...");
6276
Thread.Sleep(15000);
6377
}
6478

UmlautAdaptarr/appsettings.json

-3
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,5 @@
6464
"Enabled": false,
6565
"Host": "your_readarr_host_url",
6666
"ApiKey": "your_readarr_api_key"
67-
},
68-
"IpLeakTest": {
69-
"Enabled": false
7067
}
7168
}

0 commit comments

Comments
 (0)