forked from AlexaProjects/Alexa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
277 lines (220 loc) · 10.1 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
Copyright (C) 2013 Alan Pipitone
Al'exa is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Al'exa is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Al'exa. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;
using Alexa.Utilities;
using System.Diagnostics;
using System.Threading;
using System.Drawing;
using System.Xml;
using System.Collections.Generic;
namespace Alexa
{
class Program
{
//stores the regular expression containing the name of processes to kill
static private string _processToKillRegEx = "";
//stores the elapsed time of Al'exa execution time
static private Stopwatch _globalTime = new Stopwatch();
//contains all pid of the processes started before Al'exa
static private List<UInt32> processesBeforeAlexa;
//contains all pid of the processes started after the execution of Al'exa
static private List<UInt32> processesAfterAlexa;
//contain user name and user domain of the account used to run Al'exa
private static string userName;
private static string userDomain;
private static long globalTimeout = 900000; //default global timeout is 15 minutes
//in this class I don't use try catch statement, so if the user doesn't add the argument the application will be crash
//and you can see the error from the event viewer of windows, this because I don't know yet the path of log file and I
//cannot write any error message.
static void Main(string[] args)
{
//init configuration utilities class
ConfigUtils.Init(args[0]);
//init CryptoUtils
CryptoUtils.Init();
//if the username (and other attributes) for the share is set...
if (ConfigUtils.LogFolderUserName != "" && ConfigUtils.LogFolderPassword != "" && ConfigUtils.LogFolder.IndexOf(@"\\") != -1)
{
//...call the share connect function
SystemUtils.NetworkShare.Connect(ConfigUtils.LogFolder, ConfigUtils.LogFolderUserName, ConfigUtils.LogFolderPassword);
}
//init log utilities class
LogUtils.Init();
//check if another instance of Al'exa is running
if (SystemUtils.ProcessUtils.CheckAlexaInstances() == true)
{
Console.WriteLine("UNKNOWN: another instance of Al'exa is running");
LogUtils.Write(new StackFrame(0, true), LogUtils.ErrorLevel.Error, "Another instance of Al'exa is running");
Environment.Exit(3);
}
//Hide current window calling windows API.
//NB: To do this we can also set "Windows Application" on the output type property of Visual Studio.
//But in that way we cannot print any message on the standard output
IntPtr windowHandle = Process.GetCurrentProcess().MainWindowHandle;
SystemUtils.User32.HideWindow(windowHandle);
//get user name and user domain of the account used to run Al'exa
userName = Environment.UserName;
userDomain = Environment.UserDomainName;
//call the wormup
WarmUp();
//get the regular expression containing the name of processes to kill
_processToKillRegEx = ConfigUtils.ProcessesToKill;
//get all processes (of the above user) that are running now
if (_processToKillRegEx != "") processesBeforeAlexa = SystemUtils.ProcessUtils.GetUserProcesses(userDomain, userName);
try
{
//get the global timeout attribute, it isn't mandatory.
globalTimeout = long.Parse(ConfigUtils.Global.SelectSingleNode("performance").Attributes["timeout.value"].Value);
}
catch { }
//new thread to check global timeout
Thread _timeoutThread = new Thread(new ThreadStart(CheckTimeout));
_timeoutThread.IsBackground = true;
//save the start time, this will be saved in the xml output file
//into the global node
OutputUtils.Global.startTime = DateTime.Now;
//start the stopwatch that measure the elapsed time of all Al'exa steps and computer vision time
_globalTime.Start();
//start the thread
_timeoutThread.Start();
//init Core utilities class
CoreUtils.Init();
//execute all the steps
CoreUtils.RunSteps(ConfigUtils.AlexaSteps);
//stop the stopwatch
_globalTime.Stop();
//save the output file
Finish(false);
}
/// <summary>
/// Checks the global timeout
/// </summary>
private static void CheckTimeout()
{
while (true)
{
//checks if a timeout has occurred
if (_globalTime.ElapsedMilliseconds > globalTimeout)
{
LogUtils.Write(new StackFrame(0, true), LogUtils.ErrorLevel.Error, "global timeout has occurred");
//call the method that save the output and exit from the program
Program.Finish(false);
break;
}
Thread.Sleep(500);
}
}
/// <summary>
/// Execute the method that clean the desktop
/// </summary>
public static void WarmUp()
{
//----------------------------------------------------------------------
// KILL THE PROCESSES
//----------------------------------------------------------------------
XmlNodeList processesToKill = ConfigUtils.GetProcessesToKillAtStartTime;
if (processesToKill != null)
{
foreach (XmlNode processToKill in processesToKill)
{
try
{
string processUser = userName;
string processDomain = userDomain;
try
{
if (processToKill.Attributes["user"].Value.ToLower() != "current")
{
processUser = processToKill.Attributes["user"].Value.Split('\\')[1];
processDomain = processToKill.Attributes["user"].Value.Split('\\')[0];
}
}
catch { }
List<uint> processes = SystemUtils.ProcessUtils.GetUserProcessesByRegEx(processDomain, processUser, processToKill.InnerText);
foreach (UInt32 pidToKill in processes)
{
SystemUtils.ProcessUtils.KillProcess(pidToKill);
}
}
catch(Exception ex)
{
}
}
}
//----------------------------------------------------------------------
// CLOSE THE WINDOWS
//----------------------------------------------------------------------
XmlNodeList windowsToClose = ConfigUtils.GetWindowsToCloseAtStartTime;
if (windowsToClose != null)
{
foreach (XmlNode windowToClose in windowsToClose)
{
try
{
SystemUtils.User32.CloseWindowNew(windowToClose.InnerText);
}
catch
{
}
}
}
}
/// <summary>
/// Execute the method that save the output
/// </summary>
/// <param name="exception">set it true if you call this method on an unkown error</param>
public static void Finish(Boolean exception)
{
//stop the stopwatch
_globalTime.Stop();
//save all info that will be used by OutputUtils to write global info
OutputUtils.Global.xmlNode = ConfigUtils.Global;
OutputUtils.Global.endTime = DateTime.Now;
OutputUtils.Global.duration = _globalTime.ElapsedMilliseconds;
//close window(s)
Thread.Sleep(2000);
string windowToCloseRegEx = ConfigUtils.WindowTitleToClose;
if (windowToCloseRegEx != "")
SystemUtils.User32.CloseWindow(windowToCloseRegEx);
Thread.Sleep(2000);
if (_processToKillRegEx != "")
{
processesAfterAlexa = SystemUtils.ProcessUtils.GetUserProcessesByRegEx(userDomain, userName, _processToKillRegEx);
foreach (UInt32 pidAfterAlexa in processesAfterAlexa)
{
bool pidFound = false;
foreach (UInt32 pidBeforeAlexa in processesBeforeAlexa)
{
if (pidAfterAlexa == pidBeforeAlexa) pidFound = true;
}
if (pidFound == false) SystemUtils.ProcessUtils.processesToKill.Add(pidAfterAlexa);
}
//loop through all processes to kill
foreach (UInt32 pidToKill in SystemUtils.ProcessUtils.processesToKill)
{
//kill the process
SystemUtils.ProcessUtils.KillProcess(pidToKill);
}
}
//save the output file and exit
OutputUtils.Finish(exception);
//if exit is true then exit with exitcode 3
//if (exit) Environment.Exit(3);
}
}
}