-
Notifications
You must be signed in to change notification settings - Fork 14
/
HTTP.cs
644 lines (448 loc) · 35.5 KB
/
HTTP.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using NHttp;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Data.SQLite;
using System.Data;
using System.Collections.Generic;
namespace NoDiskSystem
{
class HTTP
{
public void start()
{
string server_ip = "";
string client_disk_path = "";
bool ipIsAvalible = false;
string tempIp = "";
using (SQLiteConnection conn = new SQLiteConnection("data source=nodisk.db"))
{
using (SQLiteCommand cmd = new SQLiteCommand())
{
cmd.Connection = conn;
conn.Open();
SQLiteHelper sh = new SQLiteHelper(cmd);
DataTable dt = sh.Select("select * from SYSTEM_SETTING;");
foreach (DataRow row in dt.Rows)
{
server_ip = row["server_ip"].ToString();
client_disk_path = row["client_disk_path"].ToString();
}
conn.Close();
string HostName = Dns.GetHostName();
IPHostEntry IpEntry = Dns.GetHostEntry(HostName);
for (int i = 0; i < IpEntry.AddressList.Length; i++)
{
//从IP地址列表中筛选出IPv4类型的IP地址
//AddressFamily.InterNetwork表示此IP为IPv4,
//AddressFamily.InterNetworkV6表示此地址为IPv6类型
if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
{
if (IpEntry.AddressList[i].ToString() == server_ip)
{
ipIsAvalible = true;
}
else
{
tempIp = IpEntry.AddressList[i].ToString();
}
}
}
}
}
using(var server = new HttpServer())
{
server.RequestReceived += (s, e) =>
{
using(var writer = new StreamWriter(e.Response.OutputStream))
{
//char[] TrimChar = {':'};
string mac = e.Request.Params.Get(0);
mac = mac.Replace(":","").ToUpper();
using (SQLiteConnection conn = new SQLiteConnection("data source=nodisk.db"))
{
using (SQLiteCommand cmd = new SQLiteCommand())
{
cmd.Connection = conn;
conn.Open();
SQLiteHelper sh = new SQLiteHelper(cmd);
DataTable dt = sh.Select("select * from CLIENT where client_mac = '" + mac + "';");
if (dt.Rows.Count == 0)
{
string client_id = System.Guid.NewGuid().ToString("N");
string client_name = "HSTECS-" + mac;
string client_mac = mac;
string client_work_path = client_disk_path;
string client_description = mac;
string client_group_id = "未分组";
string client_enable = "0";
//1、磁盘第一次登录自动登记并设置为未启用
string addClientString = "insert into CLIENT (client_id,client_name,client_mac,client_work_path,client_description,client_group_id,client_enable,client_super_enable) values ('" + client_id + "','" + client_name + "','" + client_mac + "','" + client_work_path + "','" + client_description + "','" + client_group_id + "','" + client_enable + "','0');";
sh.ExecuteScalar(addClientString);
writer.Write("#!ipxe");
writer.Write(System.Environment.NewLine);
writer.Write("echo Computer HSTECS-" + mac + " add success!");
}
else {
//2、已注册工作站登录
foreach (DataRow row in dt.Rows)
{
string checkSuperString = "select count(*) from CLIENT where client_super_enable;";
int isSuperEnable = int.Parse(sh.ExecuteScalar(checkSuperString).ToString());
if (isSuperEnable > 0 && row["client_super_enable"].ToString() != "1")
{
writer.Write("#!ipxe");
writer.Write(System.Environment.NewLine);
writer.Write("echo Super Computer have been enable!Please disable super computer in system or power on the right computer!");
return;
}
if (row["client_enable"].ToString() == "0")
{
writer.Write("#!ipxe");
writer.Write(System.Environment.NewLine);
writer.Write("echo Computer HSTECS-" + mac + " have been disable!");
}
else {
if (row["client_super_enable"].ToString() == "1")
{
//超级工作站模式
writer.Write("#!ipxe");
writer.Write(System.Environment.NewLine);
writer.Write("set menu-timeout 5000");
writer.Write(System.Environment.NewLine);
writer.Write("set submenu-timeout ${menu-timeout}");
//系统启动菜单
string getDiskList = "select * from CLIENT_DISK_LIST where client_disk_list_id = '" + row["client_disk_list_id"] + "';";
DataTable dtDiskList = sh.Select(getDiskList);
Dictionary<String,String> tempDiskNameList = new Dictionary<String,String>();
foreach (DataRow rowDiskList in dtDiskList.Rows)
{
string disk_id = rowDiskList["disk_id"].ToString();
string getDiskName = "select * from VDISK_TEMPLET where disk_id = '" + disk_id + "';";
DataTable dtDisk = sh.Select(getDiskName);
if (dtDisk.Rows.Count > 0)
{
writer.Write(System.Environment.NewLine);
writer.Write("set menu-default " + dtDisk.Rows[0]["disk_name"].ToString());
}
}
//writer.Write(System.Environment.NewLine);
//writer.Write("isset ${menu-default} || set menu-default exit");
writer.Write(System.Environment.NewLine);
writer.Write("###################### MAIN MENU ####################################");
writer.Write(System.Environment.NewLine);
writer.Write(":start");
writer.Write(System.Environment.NewLine);
writer.Write("menu Hstecs PXE boot menu ");
writer.Write(System.Environment.NewLine);
writer.Write("item --gap -- ------------------------- Operating systems ------------------------------");
//系统启动菜单
getDiskList = "select * from CLIENT_DISK_LIST where client_disk_list_id = '" + row["client_disk_list_id"] + "';";
dtDiskList = sh.Select(getDiskList);
tempDiskNameList = new Dictionary<String, String>();
foreach (DataRow rowDiskList in dtDiskList.Rows)
{
string disk_id = rowDiskList["disk_id"].ToString();
string getDiskName = "select * from VDISK_TEMPLET where disk_id = '" + disk_id + "';";
DataTable dtDisk = sh.Select(getDiskName);
foreach (DataRow rowDisk in dtDisk.Rows)
{
List<VdiskRestorePoint> tempResotorePointList = new List<VdiskRestorePoint>();
DataTable rsDiskRestorePoint = sh.Select("select * from VDISK_RESTORE_POINT where disk_id = '" + disk_id + "';");
foreach (DataRow rows in rsDiskRestorePoint.Rows)
{
VdiskRestorePoint tempPoint = new VdiskRestorePoint();
tempPoint.VdiskResotrePointName = rows["vdisk_resotre_point_name"].ToString();
tempPoint.VdiskRestorePointSort = float.Parse(rows["vdisk_restore_point_sort"].ToString());
tempResotorePointList.Add(tempPoint);
}
//查询最新的还原点
VdiskRestorePoint tempPoints = new VdiskRestorePoint();
float tempPointSort = 0;
foreach (VdiskRestorePoint p in tempResotorePointList)
{
if (p.VdiskRestorePointSort > tempPointSort)
{
tempPointSort = p.VdiskRestorePointSort;
}
}
foreach (VdiskRestorePoint p in tempResotorePointList)
{
if (p.VdiskRestorePointSort == tempPointSort)
{
tempPoints = p;
}
}
string vdisk_restore_point_id = tempPoints.VdiskRestorePointId;
string vdisk_resotre_point_name = tempPoints.VdiskResotrePointName;
//
string disk_name = rowDisk["disk_name"].ToString();
tempDiskNameList.Add(disk_name, vdisk_resotre_point_name);
writer.Write(System.Environment.NewLine);
writer.Write("item " + disk_name + " Boot " + disk_name + " super disk from iSCSI");
}
}
writer.Write(System.Environment.NewLine);
writer.Write("item --gap -- ------------------------- Advanced options -------------------------------");
writer.Write(System.Environment.NewLine);
writer.Write("item shell Drop to iPXE shell");
writer.Write(System.Environment.NewLine);
writer.Write("item reboot Reboot computer");
writer.Write(System.Environment.NewLine);
writer.Write("item");
writer.Write(System.Environment.NewLine);
writer.Write("item --key x exit Exit iPXE and continue BIOS boot");
writer.Write(System.Environment.NewLine);
writer.Write("choose --timeout ${menu-timeout} --default ${menu-default} selected || goto cancel");
writer.Write(System.Environment.NewLine);
writer.Write("set menu-timeout 0");
writer.Write(System.Environment.NewLine);
writer.Write("goto ${selected}");
writer.Write(System.Environment.NewLine);
writer.Write(":cancel");
writer.Write(System.Environment.NewLine);
writer.Write("echo You cancelled the menu, dropping you to a shell");
writer.Write(System.Environment.NewLine);
writer.Write(":shell");
writer.Write(System.Environment.NewLine);
writer.Write("echo Type 'exit' to get the back to the menu");
writer.Write(System.Environment.NewLine);
writer.Write("shell");
writer.Write(System.Environment.NewLine);
writer.Write("set menu-timeout 0");
writer.Write(System.Environment.NewLine);
writer.Write("set submenu-timeout 0");
writer.Write(System.Environment.NewLine);
writer.Write("goto start");
writer.Write(System.Environment.NewLine);
writer.Write(":failed");
writer.Write(System.Environment.NewLine);
writer.Write("echo Booting failed, dropping to shell");
writer.Write(System.Environment.NewLine);
writer.Write("goto shell");
writer.Write(System.Environment.NewLine);
writer.Write(":reboot");
writer.Write(System.Environment.NewLine);
writer.Write("reboot");
writer.Write(System.Environment.NewLine);
writer.Write(":exit");
writer.Write(System.Environment.NewLine);
writer.Write("exit");
writer.Write(System.Environment.NewLine);
writer.Write(":back");
writer.Write(System.Environment.NewLine);
writer.Write("set submenu-timeout 0");
writer.Write(System.Environment.NewLine);
writer.Write("clear submenu-default");
writer.Write(System.Environment.NewLine);
writer.Write("goto start");
Dictionary<string, string>.ValueCollection valueCol = tempDiskNameList.Values;
foreach (KeyValuePair<string, string> kvp in tempDiskNameList)
{
if (kvp.Value != null)
{
string tempMac = mac.ToLower();
writer.Write(System.Environment.NewLine);
writer.Write(":" + kvp.Value);
writer.Write(System.Environment.NewLine);
writer.Write("echo Booting " + kvp.Value + " from iSCSI");
writer.Write(System.Environment.NewLine);
if (ipIsAvalible == true)
{
writer.Write("sanboot iscsi:" + server_ip + "::::hstecsclient." + kvp.Value + " || goto failed");
}
else
{
writer.Write("sanboot iscsi:" + tempIp + "::::hstecsclient." + kvp.Value + " || goto failed");
}
writer.Write(System.Environment.NewLine);
writer.Write("goto start");
}
else {
string tempMac = mac.ToLower();
writer.Write(System.Environment.NewLine);
writer.Write(":" + kvp.Key);
writer.Write(System.Environment.NewLine);
writer.Write("echo Booting " + kvp.Key + " from iSCSI");
writer.Write(System.Environment.NewLine);
if (ipIsAvalible == true)
{
writer.Write("sanboot iscsi:" + server_ip + "::::hstecsclient." + kvp.Key + " || goto failed");
}
else
{
writer.Write("sanboot iscsi:" + tempIp + "::::hstecsclient." + kvp.Key + " || goto failed");
}
writer.Write(System.Environment.NewLine);
writer.Write("goto start");
}
}
foreach (string name in valueCol)
{
}
}
else {
//普通工作站,创建普通工作站启动脚本
writer.Write("#!ipxe");
writer.Write(System.Environment.NewLine);
writer.Write("set menu-timeout 5000");
writer.Write(System.Environment.NewLine);
writer.Write("set submenu-timeout ${menu-timeout}");
//系统启动菜单
string getDiskList = "select * from CLIENT_DISK_LIST where client_disk_list_id = '" + row["client_disk_list_id"] + "';";
DataTable dtDiskList = sh.Select(getDiskList);
List<String> tempDiskNameList = new List<String>();
foreach (DataRow rowDiskList in dtDiskList.Rows)
{
string disk_id = rowDiskList["disk_id"].ToString();
string getDiskName = "select * from VDISK_TEMPLET where disk_id = '" + disk_id + "';";
DataTable dtDisk = sh.Select(getDiskName);
if (dtDisk.Rows.Count > 0)
{
writer.Write(System.Environment.NewLine);
writer.Write("set menu-default " + dtDisk.Rows[0]["disk_name"].ToString());
}
}
//writer.Write(System.Environment.NewLine);
//writer.Write("isset ${menu-default} || set menu-default exit");
writer.Write(System.Environment.NewLine);
writer.Write("###################### MAIN MENU ####################################");
writer.Write(System.Environment.NewLine);
writer.Write(":start");
writer.Write(System.Environment.NewLine);
writer.Write("menu Hstecs PXE boot menu ");
writer.Write(System.Environment.NewLine);
writer.Write("item --gap -- ------------------------- Operating systems ------------------------------");
//系统启动菜单
getDiskList = "select * from CLIENT_DISK_LIST where client_disk_list_id = '" + row["client_disk_list_id"] + "';";
dtDiskList = sh.Select(getDiskList);
tempDiskNameList = new List<String>();
foreach (DataRow rowDiskList in dtDiskList.Rows)
{
string disk_id = rowDiskList["disk_id"].ToString();
string getDiskName = "select * from VDISK_TEMPLET where disk_id = '" + disk_id + "';";
DataTable dtDisk = sh.Select(getDiskName);
foreach (DataRow rowDisk in dtDisk.Rows)
{
string disk_name = rowDisk["disk_name"].ToString();
tempDiskNameList.Add(disk_name);
writer.Write(System.Environment.NewLine);
writer.Write("item " + disk_name + " Boot " + disk_name + " from iSCSI");
}
}
writer.Write(System.Environment.NewLine);
writer.Write("item --gap -- ------------------------- Advanced options -------------------------------");
writer.Write(System.Environment.NewLine);
writer.Write("item shell Drop to iPXE shell");
writer.Write(System.Environment.NewLine);
writer.Write("item reboot Reboot computer");
writer.Write(System.Environment.NewLine);
writer.Write("item");
writer.Write(System.Environment.NewLine);
writer.Write("item --key x exit Exit iPXE and continue BIOS boot");
writer.Write(System.Environment.NewLine);
writer.Write("choose --timeout ${menu-timeout} --default ${menu-default} selected || goto cancel");
writer.Write(System.Environment.NewLine);
writer.Write("set menu-timeout 0");
writer.Write(System.Environment.NewLine);
writer.Write("goto ${selected}");
writer.Write(System.Environment.NewLine);
writer.Write(":cancel");
writer.Write(System.Environment.NewLine);
writer.Write("echo You cancelled the menu, dropping you to a shell");
writer.Write(System.Environment.NewLine);
writer.Write(":shell");
writer.Write(System.Environment.NewLine);
writer.Write("echo Type 'exit' to get the back to the menu");
writer.Write(System.Environment.NewLine);
writer.Write("shell");
writer.Write(System.Environment.NewLine);
writer.Write("set menu-timeout 0");
writer.Write(System.Environment.NewLine);
writer.Write("set submenu-timeout 0");
writer.Write(System.Environment.NewLine);
writer.Write("goto start");
writer.Write(System.Environment.NewLine);
writer.Write(":failed");
writer.Write(System.Environment.NewLine);
writer.Write("echo Booting failed, dropping to shell");
writer.Write(System.Environment.NewLine);
writer.Write("goto shell");
writer.Write(System.Environment.NewLine);
writer.Write(":reboot");
writer.Write(System.Environment.NewLine);
writer.Write("reboot");
writer.Write(System.Environment.NewLine);
writer.Write(":exit");
writer.Write(System.Environment.NewLine);
writer.Write("exit");
writer.Write(System.Environment.NewLine);
writer.Write(":back");
writer.Write(System.Environment.NewLine);
writer.Write("set submenu-timeout 0");
writer.Write(System.Environment.NewLine);
writer.Write("clear submenu-default");
writer.Write(System.Environment.NewLine);
writer.Write("goto start");
foreach (string name in tempDiskNameList)
{
string tempMac = mac.ToLower();
writer.Write(System.Environment.NewLine);
writer.Write(":" + name);
writer.Write(System.Environment.NewLine);
writer.Write("echo Booting "+ name +" from iSCSI");
writer.Write(System.Environment.NewLine);
if (ipIsAvalible == true)
{
writer.Write("sanboot iscsi:" + server_ip + "::::hstecsclient." + name + "-" + tempMac + " || goto failed");
}
else {
writer.Write("sanboot iscsi:" + tempIp + "::::hstecsclient." + name + "-" + tempMac + " || goto failed");
}
writer.Write(System.Environment.NewLine);
writer.Write("goto start");
}
}
}
}
/**
//3、如果工作站中有机器被设置为超级工作站,则将超级工作站磁盘连接到对应的工作站上进行软件安装
writer.Write("#!ipxe");
writer.Write(System.Environment.NewLine);
writer.Write(":loop");
writer.Write(System.Environment.NewLine);
writer.Write("echo Hello World");
writer.Write(System.Environment.NewLine);
writer.Write("goto loop");
* */
}
conn.Close();
}
}
}
};
if (ipIsAvalible == true)
{
server.EndPoint = new IPEndPoint(IPAddress.Parse(server_ip), 80);
}
else {
server.EndPoint = new IPEndPoint(IPAddress.Parse(tempIp), 80);
}
server.Start();
//Process.Start(String.Format("http://{0}/", server.EndPoint));
while (true)
{
Thread.Sleep(10000);
}
}
}
}
}