-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
49775d2
commit b79e0ec
Showing
4 changed files
with
293 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
This comment has been minimized.
Sorry, something went wrong. |
||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Selenium.WebDriver" Version="4.13.1" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.7.34024.191 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdsPowerManager", "AdsPowerManager.csproj", "{CDCF5DFD-637B-45FD-9291-A0030BE5F808}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{CDCF5DFD-637B-45FD-9291-A0030BE5F808}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{CDCF5DFD-637B-45FD-9291-A0030BE5F808}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{CDCF5DFD-637B-45FD-9291-A0030BE5F808}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{CDCF5DFD-637B-45FD-9291-A0030BE5F808}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {63ACBE22-D974-438D-85D0-DECCB2B58235} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,236 @@ | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using OpenQA.Selenium; | ||
using OpenQA.Selenium.Chrome; | ||
|
||
namespace AdsPowerManager | ||
{ | ||
/// <summary> | ||
/// Менеджер для управления браузером. | ||
/// </summary> | ||
public class BrowserManager | ||
{ | ||
/// <summary> | ||
/// Инициализирует веб-драйвер браузера. | ||
/// </summary> | ||
/// <param name="profileId">Идентификатор профиля.</param> | ||
/// <returns>Интерфейс веб-драйвера.</returns> | ||
public async Task<IWebDriver> InitializeDriver(string profileId) | ||
{ | ||
string launchUrl = $"http://local.adspower.com:50325/api/v1/browser/start?user_id={profileId}"; | ||
|
||
using var httpClient = new HttpClient(); | ||
var response = await httpClient.GetAsync(launchUrl); | ||
string responseString = await response.Content.ReadAsStringAsync(); | ||
|
||
JObject responseDataJson = null; | ||
try | ||
{ | ||
responseDataJson = JObject.Parse(responseString); | ||
} | ||
catch (JsonReaderException ex) | ||
{ | ||
Console.WriteLine($"Failed to parse response JSON: {ex.Message}"); | ||
return null; | ||
} | ||
|
||
string? status = string.Empty; | ||
string? remoteAddressWithSelenium = string.Empty; | ||
string? webdriverPath = string.Empty; | ||
try | ||
{ | ||
status = (string?)responseDataJson["msg"]; | ||
remoteAddressWithSelenium = (string?)responseDataJson?["data"]?["ws"]?["selenium"]; | ||
webdriverPath = (string?)responseDataJson?["data"]?["webdriver"]; | ||
} | ||
catch (Exception) | ||
{ | ||
// Обработка исключения по необходимости | ||
} | ||
|
||
if (status == "failed") | ||
{ | ||
return null; | ||
} | ||
|
||
var options = new ChromeOptions(); | ||
options.AddArguments( | ||
"start-maximized", | ||
"enable-automation", | ||
"--headless", | ||
"--no-sandbox", | ||
"--disable-infobars", | ||
"--disable-dev-shm-usage", | ||
"--disable-browser-side-navigation", | ||
"--disable-gpu", | ||
"--ignore-certificate-errors"); | ||
|
||
options.DebuggerAddress = remoteAddressWithSelenium; | ||
var service = ChromeDriverService.CreateDefaultService(); | ||
string? chromeDriverDirectory = System.IO.Path.GetDirectoryName(webdriverPath); | ||
service.DriverServicePath = chromeDriverDirectory; | ||
service.DriverServiceExecutableName = "chromedriver.exe"; | ||
var driver = new ChromeDriver(service, options, TimeSpan.FromMinutes(5)); | ||
|
||
return driver; | ||
} | ||
|
||
/// <summary> | ||
/// Закрывает браузер по идентификатору пользователя. | ||
/// </summary> | ||
/// <param name="userId">Идентификатор пользователя.</param> | ||
/// <returns>True, если браузер успешно закрыт, иначе false.</returns> | ||
public async Task<bool> CloseBrowser(string userId) | ||
{ | ||
string apiUrl = $"http://local.adspower.com:50325/api/v1/browser/stop?user_id={userId}"; | ||
|
||
using var httpClient = new HttpClient(); | ||
var response = await httpClient.GetAsync(apiUrl); | ||
|
||
if (response.IsSuccessStatusCode) | ||
{ | ||
return true; | ||
} | ||
else | ||
{ | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Менеджер для работы с группами. | ||
/// </summary> | ||
public class GroupManager | ||
{ | ||
/// <summary> | ||
/// Получает список групп. | ||
/// </summary> | ||
/// <returns>Список групп.</returns> | ||
public async Task<List<Group>> GetGroups() | ||
{ | ||
string apiUrl = "http://local.adspower.com:50325/api/v1/group/list?page_size=100"; | ||
var httpClient = new HttpClient(); | ||
|
||
var response = await httpClient.GetAsync(apiUrl); | ||
string responseString = await response.Content.ReadAsStringAsync(); | ||
|
||
JObject responseDataJson = JObject.Parse(responseString); | ||
|
||
int code = (int)responseDataJson["code"]; | ||
if (code != 0) | ||
{ | ||
string errorMsg = (string?)responseDataJson["msg"]; | ||
Console.WriteLine($"Failed to get groups: {errorMsg}"); | ||
return null; | ||
} | ||
|
||
List<Group> groups = new List<Group>(); | ||
JArray groupsJsonArray = (JArray)responseDataJson["data"]["list"]; | ||
foreach (JToken groupJson in groupsJsonArray) | ||
{ | ||
Group group = new Group | ||
{ | ||
GroupId = (string?)groupJson["group_id"], | ||
GroupName = (string?)groupJson["group_name"] | ||
}; | ||
|
||
groups.Add(group); | ||
} | ||
|
||
return groups; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Группа. | ||
/// </summary> | ||
public class Group | ||
{ | ||
public string? GroupId { get; set; } | ||
public string? GroupName { get; set; } | ||
} | ||
|
||
/// <summary> | ||
/// Менеджер для работы с профилями. | ||
/// </summary> | ||
public class ProfileManager | ||
{ | ||
/// <summary> | ||
/// Получает список профилей. | ||
/// </summary> | ||
/// <returns>Список профилей.</returns> | ||
public async Task<List<Profile>> GetProfiles() | ||
{ | ||
string apiUrl = "http://local.adspower.com:50325/api/v1/user/list?page_size=100"; | ||
var httpClient = new HttpClient(); | ||
|
||
var response = await httpClient.GetAsync(apiUrl); | ||
|
||
if (!response.IsSuccessStatusCode) | ||
{ | ||
Console.WriteLine($"Failed to get profiles. Status code: {response.StatusCode}"); | ||
return null; | ||
} | ||
|
||
var responseString = await response.Content.ReadAsStringAsync(); | ||
|
||
JObject responseDataJson = JObject.Parse(responseString); | ||
|
||
int code = (int)responseDataJson["code"]; | ||
if (code != 0) | ||
{ | ||
string errorMsg = (string)responseDataJson["msg"]; | ||
Console.WriteLine($"Failed to get profiles: {errorMsg}"); | ||
return null; | ||
} | ||
|
||
var profilesJsonArray = (JArray)responseDataJson["data"]["list"]; | ||
var profiles = new List<Profile>(); | ||
|
||
foreach (JToken profileJson in profilesJsonArray) | ||
{ | ||
Profile profile = new Profile | ||
{ | ||
SerialNumber = (string?)profileJson["serial_number"], | ||
UserId = (string?)profileJson["user_id"], | ||
Name = (string?)profileJson["name"], | ||
GroupId = (string?)profileJson["group_id"], | ||
GroupName = (string?)profileJson["group_name"], | ||
DomainName = (string?)profileJson["domain_name"], | ||
Username = (string?)profileJson["username"], | ||
Remark = (string?)profileJson["remark"], | ||
CreatedTime = DateTimeOffset.FromUnixTimeSeconds((long)profileJson["created_time"]).DateTime, | ||
IP = (string?)profileJson["ip"], | ||
IPCountry = (string?)profileJson["ip_country"], | ||
Password = (string?)profileJson["password"], | ||
LastOpenTime = DateTimeOffset.FromUnixTimeSeconds((long)profileJson["last_open_time"]).DateTime | ||
}; | ||
|
||
profiles.Add(profile); | ||
} | ||
|
||
return profiles; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Профиль пользователя. | ||
/// </summary> | ||
public class Profile | ||
{ | ||
public string? SerialNumber { get; set; } | ||
public string? UserId { get; set; } | ||
public string? Name { get; set; } | ||
public string? GroupId { get; set; } | ||
public string? GroupName { get; set; } | ||
public string? DomainName { get; set; } | ||
public string? Username { get; set; } | ||
public string? Remark { get; set; } | ||
public DateTime CreatedTime { get; set; } | ||
public string? IP { get; set; } | ||
public string? IPCountry { get; set; } | ||
public string? Password { get; set; } | ||
public DateTime LastOpenTime { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# AdsPowerManager | ||
Библиотека для работы с антидетект браузером AdsPower. | ||
-Создание браузера | ||
-Закрытие браузера (профиля) | ||
-Создание профиля | ||
-Удаление профиля | ||
-Получение профилей | ||
-Получение групп | ||
|
||
## Установка | ||
|
||
Для использования этой библиотеки, убедитесь, что у вас установлены следующие зависимости: | ||
|
||
- Newtonsoft.Json | ||
- OpenQA.Selenium | ||
- OpenQA.Selenium.Chrome | ||
|
||
Вы можете установить их с помощью NuGet Package Manager: | ||
|
Чё у вас здесь происходит!?