-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
130 lines (120 loc) · 6.26 KB
/
Program.cs
File metadata and controls
130 lines (120 loc) · 6.26 KB
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
using ProcessGuard.Core;
using ProcessGuard.Core.Models;
using ProcessGuard.Services.BlockEventArgs;
using ProcessGuard.Services;
namespace ProcessGuard
{
internal class Program
{
static void Main(string[] args)
{
if (!PasswordManager.IsPasswordSet())
{
PasswordManager.SetPassword();
}
Console.Clear();
Console.WriteLine("ProcessGuard - Родительский контроль");
Console.WriteLine("\nКоманды:");
Console.WriteLine(" block app notepad 30 - заблокировать приложение на 30 мин");
Console.WriteLine(" block site google.com - заблокировать сайт на 60 мин");
Console.WriteLine(" unblock app notepad - разблокировать приложение");
Console.WriteLine(" unblock site google.com - разблокировать сайт");
Console.WriteLine(" exit - выход\n");
BlockManager blockManager = new BlockManager();
ApplicationBlocker applicationBlocker = new ApplicationBlocker();
NetworkBlocker networkBlocker = new NetworkBlocker();
while (true)
{
Console.Write("> ");
string command = Console.ReadLine() ?? "";
string[] parts = command?.Split(' ') ?? new string[0];
switch (parts.Length > 0 ? parts[0].ToLower() : "")
{
case "block":
if (parts.Length >= 3)
{
string target = parts[1].ToLower();
if (target == "site")
{
string siteName = parts[2];
int minutes = parts.Length > 3 && int.TryParse(parts[3], out int min) ? min : 60;
DateTime startTime = DateTime.Now;
DateTime endTime = startTime.AddMinutes(minutes);
var siteTarget = new BlockTargetSite
{
Identifier = siteName,
BlockerTimer = new BlockerTimer(startTime, endTime)
}; networkBlocker.Subscribe(blockManager);
blockManager.BlockTarget(siteTarget);
Console.WriteLine($"Заблокирован сайт '{siteName}' на {minutes} мин");
}
else if (target == "app")
{
string appName = parts[2];
int minutes = parts.Length > 3 && int.TryParse(parts[3], out int min) ? min : 60;
DateTime startTime = DateTime.Now;
DateTime endTime = startTime.AddMinutes(minutes);
var appTarget = new BlockTargetProcess
{
Identifier = appName.EndsWith(".exe") ? appName : appName + ".exe",
BlockerTimer = new BlockerTimer(startTime, endTime)
};
applicationBlocker.Subscribe(blockManager);
blockManager.BlockTarget(appTarget);
Console.WriteLine($"Заблокировано приложение '{appName}' на {minutes} мин");
}
else
{
Console.WriteLine("Используйте: block app notepad 30 или block site google.com 60");
}
}
else
{
Console.WriteLine("Используйте: block app notepad 30 или block site google.com 60");
}
break;
case "unblock":
if (PasswordManager.CheckPassword())
{
if (parts.Length >= 3)
{
string target = parts[1].ToLower();
if (target == "site")
{
string siteName = parts[2];
var siteTarget = new BlockTargetSite { Identifier = siteName };
blockManager.UnblockTarget(siteTarget);
Console.WriteLine($"Разблокирован сайт '{siteName}'");
}
else if (target == "app")
{
string appName = parts[2];
var appTarget = new BlockTargetProcess
{
Identifier = appName.EndsWith(".exe") ? appName : appName + ".exe"
};
blockManager.UnblockTarget(appTarget);
Console.WriteLine($"Разблокировано приложение '{appName}'");
}
}
else
{
Console.WriteLine("Используйте: unblock app notepad или unblock site google.com");
}
}
else
{
Console.WriteLine("Неверный пароль!");
}
break;
case "exit":
return;
default:
if (!string.IsNullOrEmpty(command))
Console.WriteLine("Неизвестная команда.");
break;
}
}
}
}
}