Skip to content

Commit

Permalink
Merge pull request #811 from Qtoss-AI/master
Browse files Browse the repository at this point in the history
WebDriver enhancement
  • Loading branch information
Oceania2018 authored Jan 2, 2025
2 parents 7bb63bc + 9dc1f5c commit ac78e6d
Show file tree
Hide file tree
Showing 38 changed files with 328 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ public interface IWebBrowser
Task<BrowserActionResult> CloseCurrentPage(MessageInfo message);
Task<BrowserActionResult> SendHttpRequest(MessageInfo message, HttpRequestParams actionParams);
Task<BrowserActionResult> GetAttributeValue(MessageInfo message, ElementLocatingArgs location);
Task<BrowserActionResult> SetAttributeValue(MessageInfo message, ElementLocatingArgs location);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ namespace BotSharp.Abstraction.Browsing.Models;
public class BrowserActionArgs
{
public bool Headless { get; set; }
public string? UserDataDir { get; set; }
public string? RemoteHostUrl { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ public class BrowserActionResult
public string? StackTrace { get; set; }
public string? Selector { get; set; }
public string? Body { get; set; }
/// <summary>
/// Page open in new tab after button click
/// </summary>
public string? UrlAfterAction { get; set; }
public bool IsHighlighted { get; set; }

public override string ToString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public interface IBotSharpRepository : IHaveServiceProvider
List<User> GetUserByIds(List<string> ids) => throw new NotImplementedException();
List<User> GetUsersByAffiliateId(string affiliateId) => throw new NotImplementedException();
User? GetUserByUserName(string userName) => throw new NotImplementedException();
void UpdateUserName(string userId, string userName) => throw new NotImplementedException();
Dashboard? GetDashboard(string id = null) => throw new NotImplementedException();
void CreateUser(User user) => throw new NotImplementedException();
void UpdateExistUser(string userId, User user) => throw new NotImplementedException();
Expand All @@ -49,6 +50,7 @@ public interface IBotSharpRepository : IHaveServiceProvider
PagedItems<User> GetUsers(UserFilter filter) => throw new NotImplementedException();
User? GetUserDetails(string userId, bool includeAgent = false) => throw new NotImplementedException();
bool UpdateUser(User user, bool updateUserAgents = false) => throw new NotImplementedException();

#endregion

#region Agent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public interface IUserService
Task<Token?> GetAffiliateToken(string authorization);
Task<Token?> GetAdminToken(string authorization);
Task<Token?> GetToken(string authorization);
Task<Token> CreateTokenByUser(User user);
Task<User> GetMyProfile();
Task<bool> VerifyUserNameExisting(string userName);
Task<bool> VerifyEmailExisting(string email);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public class User
public bool Verified { get; set; }
public string RegionCode { get; set; } = "CN";
public string? AffiliateId { get; set; }
public string? ReferralCode { get; set; }
public string? EmployeeId { get; set; }
public bool IsDisabled { get; set; }
public IEnumerable<string> Permissions { get; set; } = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ public class UserActivationModel
{
public string UserName { get; set; }
public string VerificationCode { get; set; }
public string RegionCode { get; set; } = "CN";
}
4 changes: 2 additions & 2 deletions src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,10 @@
<ItemGroup>
<PackageReference Include="Aspects.Cache" Version="2.0.4" />
<PackageReference Include="DistributedLock.Redis" Version="1.0.3" />
<PackageReference Include="EntityFrameworkCore.BootKit" Version="8.6.0" />
<PackageReference Include="EntityFrameworkCore.BootKit" Version="8.8.0" />
<PackageReference Include="Fluid.Core" Version="2.11.1" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.1" />
<PackageReference Include="Nanoid" Version="3.1.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ public async Task BroadcastAsync(string channel, string message)

// Add a message to the stream, keeping only the latest 1 million messages
var messageId = await db.StreamAddAsync(channel,
[
new NameValueEntry("message", message),
new NameValueEntry("timestamp", DateTime.UtcNow.ToString("o"))
],
AssembleMessage(message),
maxLength: 1000 * 10000);

_logger.LogInformation($"Published message {channel} {message} ({messageId})");
Expand Down Expand Up @@ -82,6 +79,25 @@ private bool CheckMessageExists(IDatabase db, string channel, string fieldName,
return exists;
}

public static NameValueEntry[] AssembleMessage(RedisValue message)
{
return
[
new NameValueEntry("message", message),
new NameValueEntry("timestamp", DateTime.UtcNow.ToString("o"))
];
}

public static NameValueEntry[] AssembleErrorMessage(RedisValue message, string error)
{
return
[
new NameValueEntry("message", message),
new NameValueEntry("timestamp", DateTime.UtcNow.ToString("o")),
new NameValueEntry("error", error)
];
}

public async Task ReDispatchAsync(string channel, int count = 10, string order = "asc")
{
var db = _redis.GetDatabase();
Expand All @@ -93,10 +109,10 @@ public async Task ReDispatchAsync(string channel, int count = 10, string order =

try
{
var messageId = await db.StreamAddAsync(channel, [
new NameValueEntry("message", entry.Values[0].Value),
new NameValueEntry("timestamp", DateTime.UtcNow.ToString("o"))
]);
var message = entry.Values.First(x => x.Name == "message").Value;
var messageId = await db.StreamAddAsync(channel,
AssembleMessage(message),
maxLength: 1000 * 10000);

_logger.LogWarning($"ReDispatched message: {channel} {entry.Values[0].Value} ({messageId})");

Expand Down Expand Up @@ -164,20 +180,7 @@ public async Task RemoveAsync(string channel, int count = 10)
var db = _redis.GetDatabase();

var entries = await db.StreamRangeAsync(channel, "-", "+", count: count, messageOrder: Order.Ascending);
foreach (var entry in entries)
{
_logger.LogInformation($"Fetched message: {channel} {entry.Values[0].Value} ({entry.Id})");

try
{
await db.StreamDeleteAsync(channel, [entry.Id]);

_logger.LogWarning($"Deleted message: {channel} {entry.Values[0].Value} ({entry.Id})");
}
catch (Exception ex)
{
_logger.LogError($"Error processing message: {ex.Message}, event id: {channel} {entry.Id}\r\n{ex}");
}
}
var deletedCount = await db.StreamDeleteAsync(channel, entries.Select(x => x.Id).ToArray());
_logger.LogWarning($"Deleted {deletedCount} messages from Redis stream {channel}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public async Task SubscribeAsync(string channel, string group, int? port, bool p
await CreateConsumerGroup(db, channel, group);
}

await CreateConsumerGroup(db, $"{channel}-Error", group);

var consumer = Environment.MachineName;
if (port.HasValue)
{
Expand All @@ -58,28 +60,36 @@ public async Task SubscribeAsync(string channel, string group, int? port, bool p
break;
}

if (priorityEnabled)
try
{
if (await HandleGroupMessage(db, $"{channel}-{EventPriority.High}", group, consumer, received) > 0)
if (priorityEnabled)
{
continue;
}
if (await HandleGroupMessage(db, $"{channel}-{EventPriority.High}", group, consumer, received, $"{channel}-Error") > 0)
{
continue;
}

if (await HandleGroupMessage(db, $"{channel}-{EventPriority.Medium}", group, consumer, received) > 0)
if (await HandleGroupMessage(db, $"{channel}-{EventPriority.Medium}", group, consumer, received, $"{channel}-Error") > 0)
{
continue;
}

await HandleGroupMessage(db, $"{channel}-{EventPriority.Low}", group, consumer, received, $"{channel}-Error");
}
else
{
continue;
await HandleGroupMessage(db, channel, group, consumer, received, $"{channel}-Error");
}

await HandleGroupMessage(db, $"{channel}-{EventPriority.Low}", group, consumer, received);
}
else
catch (Exception ex)
{
await HandleGroupMessage(db, channel, group, consumer, received);
_logger.LogError($"Error processing message: {ex.Message}\r\n{ex}");
await Task.Delay(1000 * 60);
}
}
}

private async Task<int> HandleGroupMessage(IDatabase db, string channel, string group, string consumer, Func<string, string, Task> received)
private async Task<int> HandleGroupMessage(IDatabase db, string channel, string group, string consumer, Func<string, string, Task> received, string errorChannel)
{
var entries = await db.StreamReadGroupAsync(channel, group, consumer, count: 1);
foreach (var entry in entries)
Expand All @@ -90,13 +100,24 @@ private async Task<int> HandleGroupMessage(IDatabase db, string channel, string
try
{
await received(channel, entry.Values[0].Value);

// Optionally delete the message to save space
await db.StreamDeleteAsync(channel, [entry.Id]);
}
catch (Exception ex)
{
_logger.LogError($"Error processing message: {ex.Message}, event id: {channel} {entry.Id}\r\n{ex}");
_logger.LogError($"Error processing message: {ex.Message}, event id: {channel} {entry.Id} {entry.Values[0].Value}");

// Add a message to the Error stream, keeping only the latest 1 million messages
await db.StreamAddAsync(errorChannel,
RedisPublisher.AssembleErrorMessage(entry.Values[0].Value, ex.Message),
messageId: entry.Id,
maxLength: 1000 * 10000);

// Slow down the consumer if there are errors
await Task.Delay(1000 * 10);
}
finally
{
var deletedCount = await db.StreamDeleteAsync(channel, [entry.Id]);
_logger.LogInformation($"Handled message {entry.Id}: {deletedCount == 1}");
}
}

Expand Down
27 changes: 21 additions & 6 deletions src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ record = db.GetUserByUserName(user.UserName);

if (!string.IsNullOrWhiteSpace(user.Phone))
{
record = db.GetUserByPhone(user.Phone);
record = db.GetUserByPhone(user.Phone, regionCode: (string.IsNullOrWhiteSpace(user.RegionCode) ? "CN" : user.RegionCode));
}

if (record == null && !string.IsNullOrWhiteSpace(user.Email))
Expand Down Expand Up @@ -112,7 +112,7 @@ record = user;
db.UpdateExistUser(hasRegisterId, record);
}

_logger.LogWarning($"Created new user account: {record.Id} {record.UserName}");
_logger.LogWarning($"Created new user account: {record.Id} {record.UserName}, RegionCode: {record.RegionCode}");
Utilities.ClearCache();

var hooks = _services.GetServices<IAuthenticationHook>();
Expand All @@ -127,7 +127,8 @@ record = user;
public async Task<bool> UpdatePassword(string password, string verificationCode)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var record = db.GetUserByUserName(_user.UserName);

var record = db.GetUserById(_user.Id);

if (record == null)
{
Expand Down Expand Up @@ -473,7 +474,7 @@ public async Task<Token> ActiveUser(UserActivationModel model)
var record = id.Contains("@") ? db.GetUserByEmail(id) : db.GetUserByUserName(id);
if (record == null)
{
record = db.GetUserByPhone(id);
record = db.GetUserByPhone(id, regionCode: (string.IsNullOrWhiteSpace(model.RegionCode) ? "CN" : model.RegionCode));
}

if (record == null)
Expand Down Expand Up @@ -505,6 +506,20 @@ record = db.GetUserByPhone(id);
return token;
}

public async Task<Token> CreateTokenByUser(User user)
{
var accessToken = GenerateJwtToken(user);
var jwt = new JwtSecurityTokenHandler().ReadJwtToken(accessToken);
var token = new Token
{
AccessToken = accessToken,
ExpireTime = jwt.Payload.Exp.Value,
TokenType = "Bearer",
Scope = "api"
};
return token;
}

public async Task<bool> VerifyUserNameExisting(string userName)
{
if (string.IsNullOrEmpty(userName))
Expand Down Expand Up @@ -646,7 +661,7 @@ record = db.GetUserByEmail(user.Email);

if (!string.IsNullOrEmpty(user.Phone))
{
record = db.GetUserByPhone(user.Phone);
record = db.GetUserByPhone(user.Phone, regionCode: (string.IsNullOrWhiteSpace(user.RegionCode) ? "CN" : user.RegionCode));
}

if (record == null)
Expand Down Expand Up @@ -695,7 +710,7 @@ public async Task<bool> ModifyUserPhone(string phone, string regionCode)
var curUser = await GetMyProfile();
var db = _services.GetRequiredService<IBotSharpRepository>();
var record = db.GetUserById(curUser.Id);
var existPhone = db.GetUserByPhone(phone);
var existPhone = db.GetUserByPhone(phone, regionCode: regionCode);

if (record == null || (existPhone != null && existPhone.RegionCode == regionCode))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using BotSharp.Abstraction.Users.Enums;
using BotSharp.Abstraction.Users.Settings;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
Expand Down Expand Up @@ -244,7 +243,7 @@ public IActionResult GetUserAvatar()
var file = fileStorage.GetUserAvatar();
if (string.IsNullOrEmpty(file))
{
return NotFound();
return NoContent();
}
return BuildFileResult(file);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class UserCreationModel
public string Type { get; set; } = UserType.Client;
public string Role { get; set; } = UserRole.User;
public string RegionCode { get; set; } = "CN";
public string? ReferralCode { get; set; }
public User ToUser()
{
return new User
Expand All @@ -27,7 +26,6 @@ public User ToUser()
Role = Role,
Type = Type,
RegionCode = RegionCode,
ReferralCode = ReferralCode
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Infrastructure\BotSharp.Core\BotSharp.Core.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="3.0.0" />
<PackageReference Include="MongoDB.Driver" Version="3.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private string GetDatabaseName(string mongoDbConnectionString)
Key = x.Split("=")[0],
Value = x.Split("=")[1]
}).ToList();

var source = queries.FirstOrDefault(x => x.Key.IsEqualTo(DB_NAME_INDEX));
if (source != null)
{
Expand Down Expand Up @@ -168,4 +168,5 @@ public IMongoCollection<RoleAgentDocument> RoleAgents

public IMongoCollection<CrontabItemDocument> CrontabItems
=> Database.GetCollection<CrontabItemDocument>($"{_collectionPrefix}_CronTabItems");

}
Loading

0 comments on commit ac78e6d

Please sign in to comment.