This commit is contained in:
meinname
2019-04-17 18:01:08 +02:00
8 changed files with 54 additions and 30 deletions
+16 -22
View File
@@ -26,10 +26,8 @@ namespace RuriLib.CaptchaServices
using (CWebClient client = new CWebClient())
{
if (Timeout > 0) client.Timeout = Timeout;
var response = client.DownloadString($"http://2captcha.com/res.php?key={ApiKey}&action=getbalance&json=1");
GenericResponse gbr = JsonConvert.DeserializeObject<GenericResponse>(response);
if (gbr.status == 0) throw new Exception(gbr.request);
return double.Parse(gbr.request, CultureInfo.InvariantCulture);
var response = client.DownloadString($"http://2captcha.com/res.php?key={ApiKey}&action=getbalance");
return double.Parse(response, CultureInfo.InvariantCulture);
}
}
@@ -40,10 +38,9 @@ namespace RuriLib.CaptchaServices
using (CWebClient client = new CWebClient())
{
if (Timeout > 0) client.Timeout = Timeout;
var response = client.DownloadString($"http://2captcha.com/in.php?key={ApiKey}&method=userrecaptcha&googlekey={siteKey}&pageurl={siteUrl}&json=1");
GenericResponse ctr = JsonConvert.DeserializeObject<GenericResponse>(response);
if (ctr.status == 0) throw new Exception(ctr.request);
TaskId = ctr.request;
var response = client.DownloadString($"http://2captcha.com/in.php?key={ApiKey}&method=userrecaptcha&googlekey={siteKey}&pageurl={siteUrl}");
if (!response.StartsWith("OK")) throw new Exception(response);
TaskId = response.Split('|')[1];
Status = CaptchaStatus.Processing;
// Check if task has been completed
@@ -51,12 +48,11 @@ namespace RuriLib.CaptchaServices
while (Status == CaptchaStatus.Processing && (DateTime.Now - start).TotalSeconds < Timeout)
{
Thread.Sleep(5000);
response = client.DownloadString($"http://2captcha.com/res.php?key={ApiKey}&action=get&id={TaskId}&json=1");
GenericResponse gtrr = JsonConvert.DeserializeObject<GenericResponse>(response);
if (gtrr.request.Contains("NOT_READY")) continue;
if (gtrr.status == 0) throw new Exception(gtrr.request);
response = client.DownloadString($"http://2captcha.com/res.php?key={ApiKey}&action=get&id={TaskId}");
if (response.Contains("NOT_READY")) continue;
if (response.Contains("ERROR")) throw new Exception(response);
Status = CaptchaStatus.Completed;
return gtrr.request;
return response.Split('|')[1];
}
throw new TimeoutException();
}
@@ -71,10 +67,9 @@ namespace RuriLib.CaptchaServices
if (Timeout > 0) client.Timeout = Timeout;
client.Headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");
var response = client.UploadString("http://2captcha.com/in.php",
$"key={ApiKey}&method=base64&regsense=1&body={GetBase64(bitmap, ImageFormat.Bmp)}&json=1");
GenericResponse ctr = JsonConvert.DeserializeObject<GenericResponse>(response);
if (ctr.status == 0) throw new Exception(ctr.request);
TaskId = ctr.request;
$"key={ApiKey}&method=base64&regsense=1&body={GetBase64(bitmap, ImageFormat.Bmp)}");
if (!response.StartsWith("OK")) throw new Exception(response);
TaskId = response.Split('|')[1];
Status = CaptchaStatus.Processing;
// Check if task has been completed
@@ -82,12 +77,11 @@ namespace RuriLib.CaptchaServices
while (Status == CaptchaStatus.Processing && (DateTime.Now - start).TotalSeconds < Timeout)
{
Thread.Sleep(5000);
response = client.DownloadString($"http://2captcha.com/res.php?key={ApiKey}&action=get&id={TaskId}&json=1");
GenericResponse gtrr = JsonConvert.DeserializeObject<GenericResponse>(response);
if (gtrr.request.Contains("NOT_READY")) continue;
if (gtrr.status == 0) throw new Exception(gtrr.request);
response = client.DownloadString($"http://2captcha.com/res.php?key={ApiKey}&action=get&id={TaskId}");
if (response.Contains("NOT_READY")) continue;
if (response.Contains("ERROR")) throw new Exception(response);
Status = CaptchaStatus.Completed;
return gtrr.request;
return response.Split('|')[1];
}
throw new TimeoutException();
}
+4 -1
View File
@@ -83,10 +83,13 @@ namespace RuriLib.Models
valid = !CheckContains(slice, rule.RuleString);
break;
}
if (!valid) return false;
}
catch { }
}
return valid;
return true;
}
/// <summary>
+25 -3
View File
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
@@ -9,6 +10,8 @@ namespace RuriLib.Models
/// </summary>
public class ProxyPool
{
private static Random rng = new Random();
/// <summary>The full list of proxies in the pool.</summary>
public List<CProxy> Proxies { get; } = new List<CProxy>();
@@ -35,9 +38,11 @@ namespace RuriLib.Models
/// </summary>
/// <param name="proxies">The collection of strings to parse the proxies from</param>
/// <param name="type">The type of the proxies</param>
public ProxyPool(IEnumerable<string> proxies, Extreme.Net.ProxyType type)
/// <param name="shuffle">Whether to shuffle the proxy list</param>
public ProxyPool(IEnumerable<string> proxies, Extreme.Net.ProxyType type, bool shuffle)
{
Proxies = proxies.Select(p => new CProxy(p, type)).ToList();
if (shuffle) Shuffle(Proxies);
}
/// <summary>
@@ -46,7 +51,8 @@ namespace RuriLib.Models
/// <para>They will also be unbanned to make sure there are no leftovers from previous checks.</para>
/// </summary>
/// <param name="proxies">The list of CProxy objects to be cloned and added to the list</param>
public ProxyPool(List<CProxy> proxies)
/// <param name="shuffle">Whether to shuffle the proxy list</param>
public ProxyPool(List<CProxy> proxies, bool shuffle = false)
{
// We clone the list, since we don't want to target to the same objects that are in the Proxy Manager
// If we don't do this, a ban on a runner instance would cause a ban on all the other instances as well!
@@ -54,6 +60,9 @@ namespace RuriLib.Models
// Now that the list is cloned, we make sure that no proxy is already banned
UnbanAll();
// Shuffle it if necessary
if (shuffle) Shuffle(Proxies);
}
/// <summary>
@@ -126,5 +135,18 @@ namespace RuriLib.Models
Locked = false;
return proxy;
}
private static void Shuffle<T>(IList<T> list)
{
int n = list.Count;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
}
}
+2 -2
View File
@@ -982,7 +982,7 @@ namespace RuriLib.Runner
ProxyPool = new ProxyPool(
GetProxiesFromAPI(Settings.Proxies.ReloadPath,
Settings.Proxies.ReloadType,
Settings.Proxies.ParseWithIPRegex));
Settings.Proxies.ParseWithIPRegex), Settings.Proxies.ShuffleOnStart);
}
catch (Exception ex) { RaiseMessageArrived(LogLevel.Error, $"Could not contact the reload API - {ex.Message}", true); }
break;
@@ -993,7 +993,7 @@ namespace RuriLib.Runner
ProxyPool = new ProxyPool(
GetProxiesFromFile(Settings.Proxies.ReloadPath,
Settings.Proxies.ReloadType,
Settings.Proxies.ParseWithIPRegex));
Settings.Proxies.ParseWithIPRegex), Settings.Proxies.ShuffleOnStart);
}
catch (Exception ex) { RaiseMessageArrived(LogLevel.Error, $"Could not read the proxies from file - {ex.Message}", true); }
break;
@@ -32,6 +32,10 @@ namespace RuriLib.ViewModels
private bool banAfterGoodStatus = false;
/// <summary>Whether to never ban the proxy after a SUCCESS, CUSTOM or NONE status (not FAIL or RETRY).</summary>
public bool BanAfterGoodStatus { get { return banAfterGoodStatus; } set { banAfterGoodStatus = value; OnPropertyChanged(); } }
private bool shuffleOnStart = false;
/// <summary>Whether proxy lists should be shuffled before being assigned to the Runner.</summary>
public bool ShuffleOnStart { get { return shuffleOnStart; } set { shuffleOnStart = value; OnPropertyChanged(); } }
#endregion
#region Reload