-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHostsItem.cs
157 lines (143 loc) · 3.94 KB
/
HostsItem.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace Hosts
{
public class HostsItem
{
protected string SourceString;
protected ulong SourceHash;
public bool Valid { get; protected set; }
public bool Enabled { get; set; }
public bool Hidden { get; set; }
public NetAddress IP { get; set; }
public HostAliases Aliases { get; set; }
public string Comment { get; set; }
public bool ResetFormat { get; set; }
public bool Deleted { get { return Valid && (Aliases == null || Aliases.Count == 0); } }
public string RawString
{
get
{
bool format = ResetFormat || String.IsNullOrEmpty(SourceString) || SourceHash != HalfMD5.ComputeHash(ToString());
return format ? ToString(false) : SourceString;
}
set
{
Parse(value);
}
}
public HostsItem(string text, bool resetFormat = false)
{
ResetFormat = resetFormat;
Parse(text);
}
public HostsItem(string ip, string hosts, string comment = "")
{
Enabled = true;
IP = new NetAddress(ip);
Aliases = new HostAliases(hosts);
Comment = comment ?? "";
Valid = true;
}
public HostsItem(string ip, string[] hosts, string comment = "")
{
Enabled = true;
IP = new NetAddress(ip);
Aliases = new HostAliases(hosts);
Comment = comment ?? "";
Valid = true;
}
public HostsItem(NetAddress ip, HostName host, string comment = "")
{
Enabled = true;
IP = ip;
Aliases = new HostAliases(host);
Comment = comment ?? "";
Valid = true;
}
public HostsItem(NetAddress ip, HostAliases hosts, string comment = "")
{
Enabled = true;
IP = ip;
Aliases = hosts;
Comment = comment ?? "";
Valid = true;
}
public HostsItem(HostsItem source)
{
this.Valid = source.Valid;
this.Enabled = source.Enabled;
this.Hidden = source.Hidden;
this.IP = source.IP;
this.Aliases = source.Aliases == null ? null : new HostAliases(source.Aliases);
this.Comment = source.Comment;
this.ResetFormat = source.ResetFormat;
this.SourceString = source.SourceString;
this.SourceHash = source.SourceHash;
}
public HostsItem Clone()
{
return new HostsItem(this);
}
private static Regex HostRowPattern = new Regex(@"^#?\s*"
+ @"(?<ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|[0-9a-f:]+)\s+"
+ @"(?<hosts>(([a-z0-9][-_a-z0-9]*\.?)+\s*)+)"
+ @"(?:#\s*(?<comment>.*?)\s*)?$",
RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled);
public bool Parse(string value)
{
SourceString = value;
try
{
var match = HostRowPattern.Match(value);
if (!match.Success) throw new FormatException();
Enabled = value[0] != '#';
IP = new NetAddress(match.Groups["ip"].Value);
Aliases = new HostAliases(match.Groups["hosts"].Value);
Comment = match.Groups["comment"].Value;
Hidden = false;
if (!String.IsNullOrEmpty(Comment))
{
Hidden = Comment[0] == '!';
if (Hidden) Comment = Comment.Substring(1).Trim();
}
Valid = true;
}
catch
{
Enabled = false;
Hidden = false;
IP = null;
Aliases = null;
Comment = null;
Valid = false;
}
SourceHash = HalfMD5.ComputeHash(ToString());
return Valid;
}
public override string ToString()
{
return ToString(true);
}
public string ToString(bool idn)
{
if (Valid)
{
string result = String.Format("{0,-18} {1,-31} ", (Enabled ? "" : "# ") + IP.ToString(), Aliases.ToString(idn));
if (!String.IsNullOrEmpty(Comment) || Hidden) result += "#";
if (Hidden) result += "!"; else result += " ";
if (!String.IsNullOrEmpty(Comment)) result += Comment;
return result.Trim();
}
else
{
string result = SourceString.Trim();
if (result.Length > 0 && result[0] != '#') result = "# " + result;
return result;
}
}
}
}