/
ServerHostsFile.cs
145 lines (118 loc) · 5.27 KB
/
ServerHostsFile.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using IisManagement.Shared;
namespace IisManagement.Server
{
public class ServerHostsFile
{
private static readonly List<string> ServerHostsDefaultEntries = new List<string>(new[]
{
@"192.168.10.80 images002.bmf-application.com",
@"192.168.10.80 images001.bmf-application.com",
@"192.168.10.80 gutachten.bmf-application.com",
@"192.168.10.80 www.gutachten.bmf-application.com"
});
private static void CheckHostsFileForDefaultEntries()
{
//Alle Datensätze einlesen in Array
var hosts = File.ReadAllLines(Path.Combine(Environment.SystemDirectory, "drivers", "etc", "hosts")).ToList();
//Check Default Entries.
string marker = "Server Default";
RemoveDomains(hosts, ServerHostsDefaultEntries, marker);
AddEntries(hosts, ServerHostsDefaultEntries, marker);
File.WriteAllLines(Path.Combine(Environment.SystemDirectory, "drivers", "etc", "hosts"), hosts);
}
public static void AddSite(IisSite newSite)
{
CheckHostsFileForDefaultEntries();
var hosts = File.ReadAllLines(Path.Combine(Environment.SystemDirectory, "drivers", "etc", "hosts")).ToList();
string marker = string.Format("{0} - {1}", newSite.SiteName, newSite.Group);
var domainEntries = newSite.Domains.Where(o => !string.IsNullOrWhiteSpace(o)).Select(o => $"127.0.0.1\t{o}").ToList();
RemoveDomains(hosts, domainEntries, marker);
AddEntries(hosts, domainEntries, marker);
File.WriteAllLines(Path.Combine(Environment.SystemDirectory, "drivers", "etc", "hosts"), hosts);
}
public static void RemoveSite(IisSite newSite)
{
var hosts = File.ReadAllLines(Path.Combine(Environment.SystemDirectory, "drivers", "etc", "hosts")).ToList();
string marker = string.Format("{0} - {1}", newSite.SiteName, newSite.Group);
var domainEntries = newSite.Domains.Where(o => !string.IsNullOrWhiteSpace(o)).Select(o => $"127.0.0.1\t{o}").ToList();
RemoveDomains(hosts, domainEntries, marker);
File.WriteAllLines(Path.Combine(Environment.SystemDirectory, "drivers", "etc", "hosts"), hosts);
}
public static void RemoveDuplicateEmptyHostEntries()
{
var hosts = File.ReadAllLines(Path.Combine(Environment.SystemDirectory, "drivers", "etc", "hosts")).ToList();
bool lastWasEmpty = false;
for (int i = hosts.Count - 1; i >0 ; i--)
{
if (lastWasEmpty && string.IsNullOrWhiteSpace(hosts[i]))
{
hosts.RemoveAt(i);
continue;
}
lastWasEmpty = string.IsNullOrWhiteSpace(hosts[i]);
}
File.WriteAllLines(Path.Combine(Environment.SystemDirectory, "drivers", "etc", "hosts"), hosts);
}
private static void RemoveDomains(List<string> hosts, List<string> domainentries, string marker)
{
var start = hosts.IndexOf(GetStartMarker(marker));
var ende = hosts.IndexOf(GetEndMarker(marker));
if (start == -1)
{
RemoveFromHosts(hosts, domainentries, ende);
}
else
{
if (ende > start)
{
hosts.RemoveRange(start, ende - start + 1);
}
else
{
RemoveFromHosts(hosts, domainentries, start);
}
}
}
private static string GetStartMarker(string marker)
{
return "# " + marker + " Entries";
}
private static string GetEndMarker(string marker)
{
return "#/ " + marker + " Entries";
}
private static void AddEntries(List<string> hosts, List<string> domainEntries, string markerComment)
{
hosts.Add("");
hosts.Add(GetStartMarker(markerComment));
hosts.AddRange(domainEntries);
hosts.Add(GetEndMarker(markerComment));
hosts.Add("");
}
private static void RemoveFromHosts(List<string> hosts, List<string> domainEntries, int marker = -1)
{
if (marker >= 0)
hosts.RemoveAt(marker);
//evtl. schon vorhandene Einträge entfernen
foreach (var defaultEntry in domainEntries)
hosts.Remove(defaultEntry);
var ips = domainEntries.Select(o => o.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries))
.Where(o => o.Length > 1)
.Select(o=> o[1]);
foreach (var defaultHost in hosts.Where(o =>
{
var splits = o.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
if (o.Length < 2)
return false;
return ips.Contains(splits[1]);
}).ToList())
{
hosts.Remove(defaultHost);
}
}
}
}