Skip to content

Commit

Permalink
fix wrong event listerner add
Browse files Browse the repository at this point in the history
  • Loading branch information
skadefro committed May 9, 2021
1 parent 391677e commit 70be2c1
Showing 1 changed file with 68 additions and 49 deletions.
117 changes: 68 additions & 49 deletions OpenRPA/RobotInstance.cs
Expand Up @@ -679,7 +679,7 @@ public async Task init()
global.webSocketClient.OnOpen += RobotInstance_WebSocketClient_OnOpen;
global.webSocketClient.OnClose += WebSocketClient_OnClose;
global.webSocketClient.OnQueueMessage += WebSocketClient_OnQueueMessage;
global.webSocketClient.OnQueueClosed -= WebSocketClient_OnQueueClosed;
global.webSocketClient.OnQueueClosed += WebSocketClient_OnQueueClosed;
SetStatus("Connecting to " + Config.local.wsurl);
_ = global.webSocketClient.Connect();
}
Expand Down Expand Up @@ -1030,65 +1030,84 @@ private async void RobotInstance_WebSocketClient_OnOpen()
}
async private Task RegisterQueues()
{
bool registerqueues = true;
Interfaces.entity.TokenUser user = global.webSocketClient.user;
if (Interfaces.win32.ChildSession.IsChildSessionsEnabled())
if (!global.isConnected)
{
var CurrentP = System.Diagnostics.Process.GetCurrentProcess();
var myusername = UserLogins.QuerySessionInformation(CurrentP.SessionId, UserLogins.WTS_INFO_CLASS.WTSUserName);
var mydomain = UserLogins.QuerySessionInformation(CurrentP.SessionId, UserLogins.WTS_INFO_CLASS.WTSDomainName);
var mywinstation = UserLogins.QuerySessionInformation(CurrentP.SessionId, UserLogins.WTS_INFO_CLASS.WTSWinStationName);

if (string.IsNullOrEmpty(mywinstation)) mywinstation = "";
mywinstation = mywinstation.ToLower();
if (!mywinstation.Contains("rdp") && mywinstation != "console")
_ = Task.Run(async () =>
{
Log.Debug("my WTSUserName: " + myusername);
Log.Debug("my WTSDomainName: " + mydomain);
Log.Debug("my WTSWinStationName: " + mywinstation);
registerqueues = false;
Log.Warning("mywinstation is empty or does not contain RDP, skip registering queues");
}
else
await Task.Delay(5000);
await RegisterQueues();
});
return;
}
try
{
bool registerqueues = true;
Interfaces.entity.TokenUser user = global.webSocketClient.user;
if (Interfaces.win32.ChildSession.IsChildSessionsEnabled())
{
var processes = System.Diagnostics.Process.GetProcessesByName("explorer");
foreach (var ps in processes)
var CurrentP = System.Diagnostics.Process.GetCurrentProcess();
var myusername = UserLogins.QuerySessionInformation(CurrentP.SessionId, UserLogins.WTS_INFO_CLASS.WTSUserName);
var mydomain = UserLogins.QuerySessionInformation(CurrentP.SessionId, UserLogins.WTS_INFO_CLASS.WTSDomainName);
var mywinstation = UserLogins.QuerySessionInformation(CurrentP.SessionId, UserLogins.WTS_INFO_CLASS.WTSWinStationName);

if (string.IsNullOrEmpty(mywinstation)) mywinstation = "";
mywinstation = mywinstation.ToLower();
if (!mywinstation.Contains("rdp") && mywinstation != "console")
{
var username = UserLogins.QuerySessionInformation(ps.SessionId, UserLogins.WTS_INFO_CLASS.WTSUserName);
var domain = UserLogins.QuerySessionInformation(ps.SessionId, UserLogins.WTS_INFO_CLASS.WTSDomainName);
var winstation = UserLogins.QuerySessionInformation(ps.SessionId, UserLogins.WTS_INFO_CLASS.WTSWinStationName);
Log.Debug("WTSUserName: " + username);
Log.Debug("WTSDomainName: " + domain);
Log.Debug("WTSWinStationName: " + winstation);
Log.Debug("my WTSUserName: " + myusername);
Log.Debug("my WTSDomainName: " + mydomain);
Log.Debug("my WTSWinStationName: " + mywinstation);
registerqueues = false;
Log.Warning("mywinstation is empty or does not contain RDP, skip registering queues");
}
else
{
var processes = System.Diagnostics.Process.GetProcessesByName("explorer");
foreach (var ps in processes)
{
var username = UserLogins.QuerySessionInformation(ps.SessionId, UserLogins.WTS_INFO_CLASS.WTSUserName);
var domain = UserLogins.QuerySessionInformation(ps.SessionId, UserLogins.WTS_INFO_CLASS.WTSDomainName);
var winstation = UserLogins.QuerySessionInformation(ps.SessionId, UserLogins.WTS_INFO_CLASS.WTSWinStationName);
Log.Debug("WTSUserName: " + username);
Log.Debug("WTSDomainName: " + domain);
Log.Debug("WTSWinStationName: " + winstation);
}
}
//int ConsoleSession = NativeMethods.WTSGetActiveConsoleSessionId();
////uint SessionId = Interfaces.win32.ChildSession.GetChildSessionId();
//var p = System.Diagnostics.Process.GetCurrentProcess();
//if (ConsoleSession != p.SessionId)
//{
// Log.Warning("Child sessions enabled and not running as console, skip registering queues");
// registerqueues = false;
//}
}
//int ConsoleSession = NativeMethods.WTSGetActiveConsoleSessionId();
////uint SessionId = Interfaces.win32.ChildSession.GetChildSessionId();
//var p = System.Diagnostics.Process.GetCurrentProcess();
//if (ConsoleSession != p.SessionId)
//{
// Log.Warning("Child sessions enabled and not running as console, skip registering queues");
// registerqueues = false;
//}
}
if (registerqueues)
{
SetStatus("Registering queues");
Log.Debug("Registering queue for robot " + user._id);
robotqueue = await global.webSocketClient.RegisterQueue(user._id);

foreach (var role in global.webSocketClient.user.roles)
if (registerqueues)
{
var roles = await global.webSocketClient.Query<Interfaces.entity.apirole>("users", "{_id: '" + role._id + "'}", top: 5000);
if (roles.Length == 1 && roles[0].rparole)
SetStatus("Registering queues");
Log.Debug("Registering queue for robot " + user._id);
robotqueue = await global.webSocketClient.RegisterQueue(user._id);

foreach (var role in global.webSocketClient.user.roles)
{
SetStatus("Add queue " + role.name);
Log.Debug("Registering queue for role " + role.name + " " + role._id + " ");
await global.webSocketClient.RegisterQueue(role._id);
var roles = await global.webSocketClient.Query<Interfaces.entity.apirole>("users", "{_id: '" + role._id + "'}", top: 5000);
if (roles.Length == 1 && roles[0].rparole)
{
SetStatus("Add queue " + role.name);
Log.Debug("Registering queue for role " + role.name + " " + role._id + " ");
await global.webSocketClient.RegisterQueue(role._id);
}
}
}
}

catch (Exception)
{
_ = Task.Run(async () =>
{
await Task.Delay(5000);
await RegisterQueues();
});
}
}

private async void WebSocketClient_OnClose(string reason)
Expand Down

0 comments on commit 70be2c1

Please sign in to comment.