using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading;
using BotCore.Dto;
using Newtonsoft.Json;
namespace BotCore
{
public class Core
{
public static string ZipDirectory = string.Empty;
public static string StreamUrl = string.Empty;
public static bool Headless;
public static int BrowserLimit;
public string PreferredQuality;
private int _refreshInterval;
public bool CanRun = true;
private bool _error;
private bool _firstPage = true;
private static readonly ConcurrentQueue<ChromeDriverService> DriverServices = new ConcurrentQueue<ChromeDriverService>();
private StreamReader _file;
public Action AllBrowsersTerminated;
public Action<string> InitializationError;
public Action<string> LogMessage;
public Action DidItsJob;
public Action IncreaseViewer;
public Action DecreaseViewer;
public Action<string> LiveViewer;
private readonly object _lockObject = new object ();
private readonly string _loginCookiesPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data", "loginCookies.json");
readonly JsonSerializerSettings _isoDateFormatSettings = new JsonSerializerSettings
{
DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
DateParseHandling = DateParseHandling.DateTime,
};
public void Start(string proxyListDirectory, string stream, bool headless, int browserLimit, int refreshInterval, string preferredQuality, ConcurrentQueue<LoginDto> loginInfos)
{
_error = false;
BrowserLimit = browserLimit;
CanRun = true;
_firstPage = true;
Headless = headless;
PreferredQuality = preferredQuality;
_refreshInterval = refreshInterval;
int i = 0;
StreamUrl = stream;
DirectoryInfo di = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "\\zipSource\\");
foreach (FileInfo res in di.GetFiles())
{
res.Delete();
}
foreach (DirectoryInfo dir in di.GetDirectories())
{
dir.Delete(true);
}
if (BrowserLimit > 0)
{
Thread thr = new Thread(LoopWithLimit);
thr.Start();
}
ZipDirectory = AppDomain.CurrentDomain.BaseDirectory + "\\zipSource\\background";
do
{
try
{
_file = new StreamReader(proxyListDirectory);
string line;
while (CanRun && (line = _file.ReadLine()) != null)
{
line = line.Replace(" ", "");
if (string.IsNullOrEmpty(line))
continue;
var array = line.ToString().Split(':');
string text = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "\\zipDirectory\\backgroundTemplate.js");
text = text.Replace("{ip}", array[0]).Replace("{port}", array[1]).Replace("{username}", array[2]).Replace("{password}", array[3]);
File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + "\\zipDirectory\\background.js", text);
ZipFile.CreateFromDirectory(AppDomain.CurrentDomain.BaseDirectory + "\\zipDirectory", AppDomain.CurrentDomain.BaseDirectory + "\\zipSource\\background" + i + ".zip");
Thread thr = new Thread(Request) { Priority = ThreadPriority.Normal };
Random r = new Random();
int rInt = r.Next(3000, 6000);
while (BrowserLimit > 0 && DriverServices.Count >= BrowserLimit)
{
Thread.Sleep(1000);
}
if (!CanRun)
continue;
loginInfos.TryDequeue(out var loginInfo);
thr.Start(new SessionConfigurationDto { Url = line, Count = i, PreferredQuality = preferredQuality, LoginInfo = loginInfo});
i++;
Thread.Sleep(BrowserLimit == 0 ? rInt : 1000);
}
_file.Close();
}
catch (Exception e)
{
InitializationError?.Invoke(e is IndexOutOfRangeException
? "Please select a valid proxy file."
: $"Uppss! {e.Message}");
_error = true;
}
if (!CanRun)
break;
} while (browserLimit > 0);
if (!_error)
DidItsJob?.Invoke();
else
{
InitializationError?.Invoke("Uppss! Something went wrong");
}
}
private void Request(object obj)
{
try
{
Random r = new Random();
SessionConfigurationDto itm = (SessionConfigurationDto)obj;
var array = itm.Url.Split(':');
var driverService = ChromeDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;
Proxy proxy = new Proxy
{
Kind = ProxyKind.Manual
};
string proxyUrl = array[0] + ":" + array[1];
proxy.SslProxy = proxyUrl;
proxy.HttpProxy = proxyUrl;
var chromeOptions = new ChromeOptions { Proxy = proxy, AcceptInsecureCertificates = true };