using Extreme.Net;
using RuriLib.Models;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
namespace RuriLib.ViewModels
{
///
/// The Source of proxies that gets queried when all proxies are banned in order to get fresh ones.
///
public enum ProxyReloadSource
{
/// The ProxyManager.
Manager,
/// A file on the disk.
File,
/// A remote API or website.
Remote
}
///
/// Provides proxy-related settings.
///
public class SettingsProxies : ViewModelBase
{
#region General
private bool concurrentUse = false;
/// Whether to allow two Bots to use the same proxy.
public bool ConcurrentUse { get { return concurrentUse; } set { concurrentUse = value; OnPropertyChanged(); } }
private bool neverBan = false;
/// Whether to never ban the proxies.
public bool NeverBan { get { return neverBan; } set { neverBan = value; OnPropertyChanged(); } }
private bool banAfterGoodStatus = false;
/// Whether to never ban the proxy after a SUCCESS, CUSTOM or NONE status (not FAIL or RETRY).
public bool BanAfterGoodStatus { get { return banAfterGoodStatus; } set { banAfterGoodStatus = value; OnPropertyChanged(); } }
private bool shuffleOnStart = false;
/// Whether proxy lists should be shuffled before being assigned to the Runner.
public bool ShuffleOnStart { get { return shuffleOnStart; } set { shuffleOnStart = value; OnPropertyChanged(); } }
#endregion
#region Reload
private bool reload = true;
/// Whether to reload the proxies from the proxy source after they are all banned.
public bool Reload { get { return reload; } set { reload = value; OnPropertyChanged(); } }
private ProxyReloadSource reloadSource = ProxyReloadSource.Manager;
/// The source to reload the proxies from.
public ProxyReloadSource ReloadSource { get { return reloadSource; } set { reloadSource = value; OnPropertyChanged(); } }
private string reloadPath = "";
/// The file path on disk.
public string ReloadPath { get { return reloadPath; } set { reloadPath = value; OnPropertyChanged(); } }
private ProxyType reloadType = ProxyType.Http;
/// The Type of the proxies to load.
public ProxyType ReloadType { get { return reloadType; } set { reloadType = value; OnPropertyChanged(); } }
#endregion
#region Cloudflare
private bool alwaysGetClearance = false;
/// Whether to avoid storing the clearance cookie for future use when querying the site from the same IP (proxy).
public bool AlwaysGetClearance { get { return alwaysGetClearance; } set { alwaysGetClearance = value; OnPropertyChanged(); } }
#endregion
#region Global Keys
private string[] globalBanKeys = new string[] { };
///
/// The array of possible bad replies from a proxy server.
/// The proxy will be banned when one of the keys is found.
/// These are useful to immediately blacklist proxies that need a captive portal or that only accept IPs from a given region.
///
public string[] GlobalBanKeys { get { return globalBanKeys; } set { globalBanKeys = value; OnPropertyChanged(); } }
private string[] globalRetryKeys = new string[] { };
///
/// The array of possible temporary bad replies from a proxy server.
/// The proxy will be retried on the next check.
/// These are useful when the proxy is temporarily too busy because of high traffic.
///
public string[] GlobalRetryKeys { get { return globalRetryKeys; } set { globalRetryKeys = value; OnPropertyChanged(); } }
#endregion
#region Remote Proxy Sources
/// The sources where proxies are downloaded and parsed from.
public ObservableCollection RemoteProxySources { get; set; } = new ObservableCollection();
///
/// Removes a RemoteProxySource given its id.
///
/// The id of the RemoteProxySource
public void RemoveRemoteProxySourceById(int id)
{
RemoteProxySources.Remove(GetRemoteProxySourceById(id));
}
///
/// Gets a RemoteProxySource given its id.
///
/// The id of the RemoteProxySource
/// The wanted RemoteProxySource. Null if not found.
public RemoteProxySource GetRemoteProxySourceById(int id)
{
return RemoteProxySources.FirstOrDefault(s => s.Id == id);
}
#endregion
///
/// Resets the properties to their default value.
///
public void Reset()
{
SettingsProxies def = new SettingsProxies();
IList props = new List(typeof(SettingsProxies).GetProperties());
foreach (PropertyInfo prop in props)
{
prop.SetValue(this, prop.GetValue(def, null));
}
}
}
}