-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainForm.cs
More file actions
181 lines (165 loc) · 6.51 KB
/
MainForm.cs
File metadata and controls
181 lines (165 loc) · 6.51 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
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
using System;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;
using System.Windows.Forms;
namespace PortFail2Ban
{
public partial class MainForm : Form
{
private CoreModule core = new CoreModule();
private WhiteListForm FrmWhiteList;
public MainForm()
{
InitializeComponent();
tbPort.Text = core.Port.ToString();
tbGate.Text = core.Gate.ToString();
tbBanDuration.Text = core.BanDuration.ToString();
tbClean.Text = core.CleanInterval.ToString();
bool exist = ServiceIsExisted("PortFail2Ban");
tbInstall.Enabled = !exist;
tbUninstall.Enabled = exist;
}
private void UpdateUI(bool start)
{
btnStart.Enabled = !start;
btnStop.Enabled = start;
}
private bool ServiceIsExisted(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController s in services)
{
if (s.ServiceName == serviceName)
{
return true;
}
}
return false;
}
private void btnStart_Click(object sender, EventArgs e)
{
try
{
core.Port = int.Parse(tbPort.Text.Trim());
core.BanDuration = int.Parse(tbBanDuration.Text.Trim());
core.CleanInterval = int.Parse(tbClean.Text.Trim());
core.Gate = int.Parse(tbGate.Text.Trim());
}
catch (Exception ex)
{
MessageBox.Show("参数错误: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
core.Start();
tmrUI.Enabled = true;
UpdateUI(true);
}
private void btnStop_Click(object sender, EventArgs e)
{
tmrUI.Enabled = false;
core.Stop();
UpdateUI(false);
}
private void tmrUI_Tick(object sender, EventArgs e)
{
try
{
lvData.VirtualListSize = core.BanItems.Count;
lvData.Invalidate();
}
catch (Exception)
{
}
}
private void lvData_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
{
if (e.ItemIndex > core.BanItems.Count - 1) return;
try
{
var item = core.BanItems.ElementAt(e.ItemIndex);
e.Item = new ListViewItem();
BanItem banItem = item.Value;
e.Item.ImageIndex = banItem.status;
e.Item.Text = "";
e.Item.SubItems.Add(banItem.IP);
e.Item.SubItems.Add(banItem.count.ToString());
e.Item.SubItems.Add(banItem.StartTime.ToString());
e.Item.SubItems.Add(banItem.lastActive.ToString());
e.Item.SubItems.Add(banItem.status == 1 ? banItem.BanTime.ToString() : "");
e.Item.SubItems.Add(banItem.lastPorts);
}
catch (Exception ex)
{
e.Item = new ListViewItem();
e.Item.Text = "-";
e.Item.SubItems.Add("-");
e.Item.SubItems.Add("-");
e.Item.SubItems.Add("-");
e.Item.SubItems.Add("-");
e.Item.SubItems.Add("-");
e.Item.SubItems.Add(ex.Message);
}
}
private void MainForm_SizeChanged(object sender, EventArgs e)
{
chPorts.Width = -2;
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
btnStop.PerformClick();
}
private void tbInstall_Click(object sender, EventArgs e)
{
try
{
string[] cmdline = { };
string serviceFileName = System.Reflection.Assembly.GetExecutingAssembly().Location;
TransactedInstaller transactedInstaller = new TransactedInstaller();
AssemblyInstaller assemblyInstaller = new AssemblyInstaller(serviceFileName, cmdline);
transactedInstaller.Installers.Add(assemblyInstaller);
transactedInstaller.Install(new System.Collections.Hashtable());
MessageBox.Show("Install success.\r\n\r\n please close application and start service by Windows services manager or reboot system.", "OK", MessageBoxButtons.OK, MessageBoxIcon.Information);
tbInstall.Enabled = false;
tbUninstall.Enabled = true;
}
catch (Exception ex)
{
MessageBox.Show("Install error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void tbUninstall_Click(object sender, EventArgs e)
{
try
{
core.cmd("net stop PortFail2Ban");
string[] cmdline = { };
string serviceFileName = System.Reflection.Assembly.GetExecutingAssembly().Location;
TransactedInstaller transactedInstaller = new TransactedInstaller();
AssemblyInstaller assemblyInstaller = new AssemblyInstaller(serviceFileName, cmdline);
transactedInstaller.Installers.Add(assemblyInstaller);
transactedInstaller.Uninstall(null);
MessageBox.Show("Uninstall success.", "OK", MessageBoxButtons.OK, MessageBoxIcon.Information);
tbInstall.Enabled = true;
tbUninstall.Enabled = false;
}
catch (Exception ex)
{
MessageBox.Show("Uninstall error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void tbWhiteList_Click(object sender, EventArgs e)
{
if (FrmWhiteList == null) FrmWhiteList = new WhiteListForm();
FrmWhiteList.tbIPs.Text = core.WhiteList.Replace(";", "\r\n");
if (FrmWhiteList.ShowDialog() == DialogResult.OK)
{
core.WhiteList = FrmWhiteList.tbIPs.Text.Trim().Replace("\r\n", ";");
}
}
private void tbAbout_Click(object sender, EventArgs e)
{
MessageBox.Show("Yeah, Delphi/OOP 10x better than VS/C#\r\n\r\nHappy coding~", "Fuck CCP", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}