-
Notifications
You must be signed in to change notification settings - Fork 14
/
WebServer.cs
233 lines (206 loc) · 7.75 KB
/
WebServer.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.IO.Compression;
using System.Windows.Forms;
using System.Diagnostics;
using System.Net.Sockets;
namespace Coffeed
{
public partial class WebServer : Form
{
public string statusTxt
{
get
{
return this.label1.Text;
}
set
{
this.label1.Text = value;
}
}
public WebServer()
{
InitializeComponent();
}
ProcessStartInfo startInfo = new ProcessStartInfo();
Process processTemp = new Process();
private void scanMySQL()
{
IPAddress parseip = IPAddress.Parse("127.0.0.1");
IPEndPoint endpoint = new IPEndPoint(parseip, 3306);
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IAsyncResult result = sock.BeginConnect(endpoint, null, null);
bool success = result.AsyncWaitHandle.WaitOne(5, true);
if (sock.Connected)
{
sock.EndConnect(result);
label4.ForeColor = Color.LimeGreen;
label4.Text = "Online";
}
else
{
label4.ForeColor = Color.Tomato;
label4.Text = "Offline";
}
sock.Close();
}
private void button2_Click(object sender, EventArgs e)
{
button4.Enabled = true;
button2.Enabled = false;
startInfo.FileName = Application.StartupPath + "/MySQL/bin/mysqld.exe";
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
processTemp.StartInfo = startInfo;
processTemp.Start();
Thread.Sleep(1000);
scanMySQL();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
Task.Run(() =>
{
this.Invoke((MethodInvoker)delegate
{
DownloadFile("https://archive.mariadb.org//mariadb-10.4.12/winx64-packages/mariadb-10.4.12-winx64.zip", Application.StartupPath + "/MySQL.zip");
});
});
}
private volatile bool _completed;
public async void DownloadFile(string address, string location)
{
WebClient client = new WebClient();
Uri Uri = new Uri(address);
_completed = false;
client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgress);
client.DownloadFileAsync(Uri, location);
}
public bool DownloadCompleted { get { return _completed; } }
private async void DownloadProgress(object sender, DownloadProgressChangedEventArgs e)
{
// Displays the operation identifier, and the transfer progress.
this.Invoke((MethodInvoker)delegate
{
label1.Text = e.ProgressPercentage + "% Downloaded";
if(e.ProgressPercentage == 100)
{
label1.Text = "Completed!";
Thread.Sleep(2000);
button1.Enabled = true;
//ZipFile.ExtractToDirectory(Application.StartupPath + "/MySQL.zip", Application.StartupPath + "/", Encoding.UTF8);
using (ZipArchive source = ZipFile.Open(Application.StartupPath + "/MySQL.zip", ZipArchiveMode.Read, null))
{
foreach (ZipArchiveEntry entry in source.Entries)
{
string fullPath = Path.GetFullPath(Path.Combine(Application.StartupPath + "/", entry.FullName));
if (Path.GetFileName(fullPath).Length != 0)
{
Directory.CreateDirectory(Path.GetDirectoryName(fullPath));
entry.ExtractToFile(fullPath, true);
}
}
}
}
});
}
private void Completed(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled == true)
{
Console.WriteLine("Download has been canceled.");
}
else
{
Console.WriteLine("Download completed!");
}
_completed = true;
}
private void button1_Click(object sender, EventArgs e)
{
Directory.Move("mariadb-10.4.12-winx64", "MySQL");
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = Application.StartupPath + "/MySQL/bin/mysql_install_db.exe";
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process processTemp = new Process();
processTemp.StartInfo = startInfo;
processTemp.Start();
processTemp.WaitForExit();
File.Delete(Application.StartupPath + "/MySQL.zip");
MessageBox.Show("Succesfully installed!", "MySQL Installed!");
button1.Visible = false;
button1.Enabled = false;
button3.Enabled = false;
button3.Visible = false;
label2.Visible = false;
button2.Enabled = true;
button2.Visible = true;
}
private void button3_Click(object sender, EventArgs e)
{
button1.Enabled = true;
button1.Visible = true;
button3.Enabled = false;
if (File.Exists(Application.StartupPath + "/MySQL/bin/mysqld.exe"))
{
MessageBox.Show("Already configured", "MySQL Is already configured!");
File.Delete(Application.StartupPath + "/MySQL.zip");
}
else
{
backgroundWorker1.RunWorkerAsync();
}
}
private void WebServer_Load(object sender, EventArgs e)
{
if (File.Exists(Application.StartupPath + "/MySQL/bin/mysqld.exe"))
{
button3.Visible = false;
button3.Enabled = false;
button1.Enabled = false;
button1.Visible = false;
button2.Visible = true;
button2.Enabled = true;
label2.Visible = false;
}
else
{
button3.Visible = true;
button3.Enabled = true;
button1.Enabled = false;
button1.Visible = true;
button2.Visible = false;
button2.Enabled = false;
label2.Visible = true;
}
}
private void button4_Click(object sender, EventArgs e)
{
button4.Enabled = false;
button2.Enabled = true;
foreach (Process process in Process.GetProcessesByName("mysqld"))
{
process.Kill();
}
Thread.Sleep(1000);
scanMySQL();
}
}
}