-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
363 lines (329 loc) · 11.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Resources;
using System.Reflection;
using System.Windows.Forms;
using System.Threading;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
[assembly: AssemblyVersion("0.0.1.0")]
[assembly: AssemblyTitle("LSM")]
[assembly: AssemblyCompany("")]
[assembly: NeutralResourcesLanguage("en")]
[assembly: AssemblyFileVersion("0.0.1.0")]
[assembly: AssemblyProduct("LSM")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
namespace Lan_School_Monitor
{
public class setting
{
public setting() //public setting(Toggle_WIFI_ = false, Notify = true, Hide = true)
{
Toggle_WIFI = false;
Notify = true;
Hide = true;
CloseTaskManager = false;
}
public bool Toggle_WIFI { get; set; }
public bool Notify { get; set; }
public bool Hide { get; set; }
public bool CloseTaskManager { get; set;}
}
public class BSOD
{
[DllImport("ntdll.dll")]
public static extern uint RtlAdjustPrivilege(int Privilege, bool bEnablePrivilege, bool IsThreadPrivilege, out bool PreviousValue);
[DllImport("ntdll.dll")]
public static extern uint NtRaiseHardError(uint ErrorStatus, uint NumberOfParameters, uint UnicodeStringParameterMask, IntPtr Parameters, uint ValidResponseOption, out uint Response);
public static void BSOD_()
{
Boolean t1;
uint t2;
RtlAdjustPrivilege(19, true, false, out t1);
NtRaiseHardError(0xc0000022, 0, 0, IntPtr.Zero, 6, out t2);
}
}
public class Program
{
public static setting Settings = new setting();
public static List<Process> ProcessList = new List<Process>();
public static List<PerformanceCounter> instances = new List<PerformanceCounter>();
#region Form Designer
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Text = "Form1";
}
#endregion
}
#endregion
public partial class Form1 : Form
{
NotifyIcon notifyicon;
Thread Lan_School_Monitor_Thread;
public Form1()
{
InitializeComponent();
//Application.ApplicationExit += new EventHandler(this.OnApplicationExit);
// Create an event handler that will be called when the application exits via Ctrl-C or Alt-F4.
Application.ApplicationExit += new EventHandler(this.OnApplicationExit);
notifyicon = new NotifyIcon();
notifyicon.Icon = SystemIcons.Application;
notifyicon.Visible = true;
// Create the context menu items and add them to the notication tray icon.
//MenuItem programNameMenuItem = new MenuItem("Program Name");
MenuItem quitMenuItem = new MenuItem("Quit");
MenuItem BSODMenuItem = new MenuItem("");
if (!Settings.Hide) { BSODMenuItem.Text = "BSOD"; }
ContextMenu contextMenu = new ContextMenu();
contextMenu.MenuItems.Add(BSODMenuItem);
contextMenu.MenuItems.Add(quitMenuItem);
notifyicon.ContextMenu = contextMenu;
quitMenuItem.Click += quitMenuItem_Click;
BSODMenuItem.Click += BSODMenuItem_Click;
// Hide the form.
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
Lan_School_Monitor_Thread = new Thread(new ThreadStart(Monitor_Thread));
Lan_School_Monitor_Thread.Start();
}
void OnApplicationExit(object sender, EventArgs e)
{
notifyicon.Dispose();
}
void quitMenuItem_Click(object sender,EventArgs e)
{
Lan_School_Monitor_Thread.Abort();
notifyicon.Dispose();
this.Close();
}
void BSODMenuItem_Click(object sender, EventArgs e)
{
BSOD.BSOD_();
Lan_School_Monitor_Thread.Abort();
notifyicon.Dispose();
this.Close();
}
void Monitor_Thread()
{
DateTime last_notified = DateTime.Now;
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
try
{
//
Console.WriteLine("Monitoring...");
foreach (Process p in ProcessList)
{
instances.Add(new PerformanceCounter("Process", "IO Other Bytes/sec", p.ProcessName));
//https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2003/cc780836(v=ws.10)?redirectedfrom=MSDN
//instances.Add(new PerformanceCounter("Network Interface", "Bytes Total/sec", p.ProcessName));
}
//PerformanceCounter net = new PerformanceCounter("Process", "IO Read Bytes/sec", ProcessList[0].ProcessName);
while(true)
{
foreach (PerformanceCounter net in instances)
{
float value = net.NextValue();
//Console.WriteLine("Process: {1} Net: {0}", net.NextValue(), ProcessList[0].ProcessName);
if(value != 0.0)
{
notifyicon.Icon = SystemIcons.Warning;
// Notify if 8 seconds have passed since the last notification.
if(DateTime.Now.Subtract(last_notified).TotalSeconds > 8)
{
last_notified = DateTime.Now;
// Make the message as a string.
string message = String.Format("Network activity detected on \"{0}\" at {1}.", net.InstanceName, DateTime.Now.ToString());
if(Settings.Notify){ notifyicon.ShowBalloonTip(1000, "Network Usage", message, ToolTipIcon.Info); }
Console.WriteLine(message);
// If 'CloseTaskManager' is true, close task manager.
if(Settings.CloseTaskManager)
{
Process[] task_managers = Process.GetProcessesByName("taskmgr");
foreach(Process task_manager in task_managers)
{
Console.WriteLine("Closing task manager.");
task_manager.Kill();
}
}
if(Settings.Toggle_WIFI)
{
// execute 'netsh wlan disconnect' command.
Process _process = new Process();
_process.StartInfo.FileName = "cmd.exe";
_process.StartInfo.Arguments = "/c netsh wlan disconnect";
_process.StartInfo.UseShellExecute = false;
_process.StartInfo.RedirectStandardOutput = true;
_process.StartInfo.CreateNoWindow = true;
_process.Start();
// Read the output (or the error)
//string output = _process.StandardOutput.ReadToEnd();
// Wait for the process to finish.
//_process.WaitForExit();
//Console.WriteLine(output);
}
}
}
else
{ notifyicon.Icon = SystemIcons.Application; }
}
Thread.Sleep(1000); // To decrease cpu load.
}
}
catch( ThreadAbortException e )
{
foreach (PerformanceCounter net in instances)
{
net.Dispose();
}
Console.WriteLine("Thread aborted: {0}", e.Message);
}
}
}
[STAThread]
static void Main(string[] args)
{
// First argument
if(args.Length > 0)
{
if(args[0] == "--help" || args[0] == "-h" || args[0] == "/h" || args[0] == "-?" || args[0] == "-help" || args[0] == "/help")
{
Console.WriteLine("Usage: LSM.exe [--help] [--close-task-manager] [--disable-notify] [--toggle-wifi]");
Console.WriteLine("--help: Show this help message.");
Console.WriteLine("--close-task-manager: Close task manager when network activity is detected.");
Console.WriteLine("--disable-notify: Disable notifications.");
Console.WriteLine("--toggle-wifi: Toggle wifi on/off when network activity is detected.");
Console.WriteLine("--create-settings: Create a settings file.");
Console.WriteLine("--BSOD");
return;
}
// Combine all arguments together.
string arg = "";
foreach(string a in args)
{
arg += a;
}
if (arg.Contains("--close-task-manager"))
{
Settings.CloseTaskManager = true;
}
if (arg.Contains("--disable-notify"))
{
Settings.Notify = false;
}
if (arg.Contains("--toggle-wifi"))
{
Settings.Toggle_WIFI = true;
}
if (arg.Contains("--BSOD"))
{
BSOD.BSOD_();
return;
}
// Create a 'Settings.xml' file by '--create-settings' argument.
if (arg.Contains("--create-settings"))
{
// If 'Settings.xml' file already exists, tell the user.
if(File.Exists("Settings.xml"))
{
Console.WriteLine("'Settings.xml' already exists.");
return;
}
XmlTextWriter xW = new XmlTextWriter("Settings.xml", Encoding.UTF8);
xW.Formatting = Formatting.Indented;
xW.WriteStartElement("Settings");
xW.WriteStartElement("Toggle_WIFI");
xW.WriteString("false");
xW.WriteEndElement();
xW.WriteStartElement("Notify");
xW.WriteString("true");
xW.WriteEndElement();
xW.WriteStartElement("Hide");
xW.WriteString("true");
xW.WriteEndElement();
xW.WriteStartElement("CloseTaskManager");
xW.WriteString("false");
xW.WriteEndElement();
xW.WriteEndElement();
xW.Close();
Console.WriteLine("Settings.xml file created.");
return;
}
}
if(File.Exists("Settings.xml"))
{
try
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load("Settings.xml");
if (xDoc.SelectSingleNode("Settings/Toggle_WIFI").InnerText.ToLower() == "true")
{ Settings.Toggle_WIFI = true;}
if (xDoc.SelectSingleNode("Settings/Notify").InnerText.ToLower() == "false")
{ Settings.Notify = false;}
if (xDoc.SelectSingleNode("Settings/Hide").InnerText.ToLower() == "false")
{ Settings.Hide = false; }
if (xDoc.SelectSingleNode("Settings/CloseTaskManager").InnerText.ToLower() == "true")
{ Settings.CloseTaskManager = true; }
}
catch(Exception ex)
{
Console.WriteLine("There was an error.");
Console.WriteLine(ex.Message);
}
}
if (Settings.Hide == true)
{ Console.WriteLine("LSM"); }
else { Console.WriteLine("Lan School Monitor"); }
foreach (Process theprocess in Process.GetProcesses())
{
if (//theprocess.ProcessName.Contains("firefox") // <For testing only.
theprocess.ProcessName.Contains("Lsk") ||
theprocess.ProcessName.Contains("student") ||
theprocess.ProcessName.Contains("lskHlpr64") ||
theprocess.ProcessName.Contains("Isk")
)
{
ProcessList.Add(theprocess);
Console.WriteLine("Process: \"{0}\" ID: {1}", theprocess.ProcessName, theprocess.Id);
}
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//Application.ApplicationExit += new EventHandler(OnApplicationExit);
Application.Run(new Form1());
}
}
}