Skip to content
This repository has been archived by the owner on Jul 19, 2022. It is now read-only.

Commit

Permalink
Added WithProxiesFromUrl to CheckerBuilder
Browse files Browse the repository at this point in the history
Enjoy easily loading proxies from an URL using Milky. Supports CR&LF line breaks
  • Loading branch information
Laiteux committed Jan 6, 2021
1 parent 0124ce1 commit 99b3f72
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/Milky/CheckerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -17,6 +18,7 @@ public class CheckerBuilder
private OutputSettings _outputSettings = new OutputSettings();
private readonly List<Combo> _combos = new List<Combo>();
private readonly Library<HttpClient> _httpClientLibrary = new Library<HttpClient>();
private readonly Lazy<WebClient> _lazyWebClient = new Lazy<WebClient>();
private readonly Dictionary<string, string> _defaultRequestHeaders = new Dictionary<string, string>();

public CheckerBuilder(CheckerSettings checkerSettings, Func<Combo, HttpClient, int, Task<CheckResult>> checkProcess)
Expand Down Expand Up @@ -66,6 +68,17 @@ public CheckerBuilder WithProxies(IEnumerable<string> proxies, ProxySettings set
return this;
}

public CheckerBuilder WithProxiesFromUrl(string url, ProxySettings settings)
{
var responseString = _lazyWebClient.Value.DownloadString(url);

string[] proxies = responseString.Split(new[] { @"\n", @"\r" }, StringSplitOptions.RemoveEmptyEntries);

WithProxies(proxies, settings);

return this;
}

public CheckerBuilder WithDefaultRequestHeader(string name, string value)
{
_defaultRequestHeaders.Add(name, value);
Expand All @@ -91,13 +104,6 @@ public Checker Build()
return new Checker(_checkerSettings, _outputSettings, _checkProcess, _combos, _httpClientLibrary);
}

private void SetUpMiscellaneous(int extraThreads = 10)
{
ThreadPool.SetMinThreads(_checkerSettings.MaxThreads + extraThreads, _checkerSettings.MaxThreads + extraThreads);

Directory.CreateDirectory(_outputSettings.OutputDirectory);
}

private void SetUpHttpClientLibrary()
{
if (!_checkerSettings.UseProxies)
Expand All @@ -115,7 +121,7 @@ private void SetUpHttpClientLibrary()
}
else
{
_httpClientLibrary.Fill(_checkerSettings.MaxThreads * 2); // Lazy to explain, use your brain
_httpClientLibrary.Fill(_checkerSettings.MaxThreads * 2); // * 2 so we basically always have a different or same HttpClient available to one already in use
}

foreach (var header in _defaultRequestHeaders)
Expand All @@ -126,5 +132,12 @@ private void SetUpHttpClientLibrary()
}
}
}

private void SetUpMiscellaneous(int extraThreads = 10)
{
ThreadPool.SetMinThreads(_checkerSettings.MaxThreads + extraThreads, _checkerSettings.MaxThreads + extraThreads);

Directory.CreateDirectory(_outputSettings.OutputDirectory);
}
}
}

0 comments on commit 99b3f72

Please sign in to comment.