-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
HostsFileServiceImpl.cs
632 lines (577 loc) · 26.4 KB
/
HostsFileServiceImpl.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
using System.Application.Models.Settings;
using System.Application.Properties;
using System.Application.UI.Resx;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Security;
using System.Text;
using static System.Application.Services.IHostsFileService;
namespace System.Application.Services.Implementation
{
internal sealed class HostsFileServiceImpl : IHostsFileService
{
readonly IDesktopPlatformService s;
readonly object lockObj = new();
public HostsFileServiceImpl(IDesktopPlatformService s)
{
this.s = s;
}
#region Mark
internal const string MarkStart = "# Steam++ Start";
internal const string MarkEnd = "# Steam++ End";
internal const string BackupMarkStart = "# Steam++ Backup Start";
internal const string BackupMarkEnd = "# Steam++ Backup End";
/// <summary>
/// 根据行切割数组获取标记值
/// </summary>
/// <param name="line_split_array"></param>
/// <returns></returns>
static string? GetMarkValue(string[] line_split_array)
{
if (line_split_array.Length == 3 || line_split_array.Length == 4)
{
var value = string.Join(' ', line_split_array);
if (line_split_array.Length == 3)
{
if (string.Equals(value, MarkStart, StringComparison.OrdinalIgnoreCase))
{
return MarkStart;
}
if (string.Equals(value, MarkEnd, StringComparison.OrdinalIgnoreCase))
{
return MarkEnd;
}
}
else /*if (array.Length == 4)*/
{
if (string.Equals(value, BackupMarkStart, StringComparison.OrdinalIgnoreCase))
{
return BackupMarkStart;
}
if (string.Equals(value, BackupMarkEnd, StringComparison.OrdinalIgnoreCase))
{
return BackupMarkEnd;
}
}
}
return default;
}
#endregion
#region FileVerify
/// <summary>
/// 最大支持文件大小,50MB
/// </summary>
const long MaxFileLength = 52428800;
/// <summary>
/// 尝试开始操作前
/// </summary>
/// <param name="message"></param>
/// <param name="fileInfo"></param>
/// <param name="removeReadOnly"></param>
/// <param name="checkReadOnly"></param>
/// <param name="checkMaxLength"></param>
/// <returns></returns>
bool TryOperation([NotNullWhen(false)] out string? message,
[NotNullWhen(true)] out FileInfo? fileInfo,
out bool removeReadOnly,
bool checkReadOnly = false,
bool checkMaxLength = true)
{
try
{
message = null;
removeReadOnly = false;
fileInfo = new FileInfo(s.HostsFilePath);
if (!fileInfo.Exists)
{
try
{
fileInfo.Create().Dispose();
return true;
}
catch (Exception ex)
{
message = ex.GetAllMessage();
return false;
}
}
if (OperatingSystem2.IsMacOS)
{
var editPath = Path.Combine(IOPath.CacheDirectory, "hosts");
var editInfo = new FileInfo(editPath);
if (editInfo.Exists)
editInfo.Delete();
fileInfo.CopyTo(editPath);
fileInfo = new FileInfo(editPath);
}
if (checkMaxLength)
{
if (fileInfo.Length > MaxFileLength)
{
message = SR.FileSizeTooLarge;
return false;
}
}
if (checkReadOnly)
{
var attr = fileInfo.Attributes;
if (attr.HasFlag(FileAttributes.ReadOnly))
{
try
{
fileInfo.Attributes = attr & ~FileAttributes.ReadOnly;
}
catch (Exception)
{
message = SR.FileAttributeIsReadOnlyModifyFail;
return false;
}
removeReadOnly = true;
}
}
return true;
}
catch (Exception ex)
{
removeReadOnly = false;
fileInfo = null;
message = ex.Message;
return false;
}
}
/// <summary>
/// 设置文件只读属性
/// </summary>
/// <param name="fileInfo"></param>
static void SetReadOnly(FileInfo fileInfo)
{
try
{
var attr = fileInfo.Attributes;
if (!attr.HasFlag(FileAttributes.ReadOnly))
{
attr |= FileAttributes.ReadOnly;
fileInfo.Attributes = attr;
}
}
catch
{
}
}
#endregion
#region Handle
static string[] GetLineSplitArray(string line_value) => line_value.Split(' ', StringSplitOptions.RemoveEmptyEntries);
internal static bool IsV1Format(string[] line_split_array) => line_split_array.Length == 3 && line_split_array[2] == "#Steam++";
enum HandleLineResult
{
/// <summary>
/// 当前行格式不正确,不处理直接写入
/// </summary>
IncorrectFormatNoneHandleWrite_W_False,
/// <summary>
/// 当前行格式正确,开始处理
/// </summary>
CorrectFormatStartHandle,
/// <summary>
/// 当前行格式不正确,不处理也不写入
/// </summary>
IncorrectFormatNoneHandleNoWrite_Null,
/// <summary>
/// 重复项
/// </summary>
Duplicate,
}
static bool? Convert(HandleLineResult handleLineResult)
{
switch (handleLineResult)
{
case HandleLineResult.IncorrectFormatNoneHandleWrite_W_False:
return false;
case HandleLineResult.CorrectFormatStartHandle:
return true;
case HandleLineResult.IncorrectFormatNoneHandleNoWrite_Null:
return null;
default:
throw new ArgumentOutOfRangeException(nameof(handleLineResult), handleLineResult, null); ;
}
}
static HandleLineResult HandleLineV2(int line_num, HashSet<string> domains, string line_value, out string[] line_split_array, Func<string[], bool?>? func = null)
{
static bool Contains(HashSet<string> hs, string value) => !hs.Add(value);
return HandleLineV2(Contains, line_num, domains, line_value, out line_split_array, func);
}
/// <summary>
/// 处理一行数据
/// <para><see langword="false"/> 当前行格式不正确,不处理直接写入</para>
/// <para><see langword="true"/> 当前行格式正确,开始处理</para>
/// <para><see langword="null"/> 当前行格式不正确,不处理也不写入</para>
/// </summary>
/// <param name="line_num"></param>
/// <param name="domains"></param>
/// <param name="line_value"></param>
/// <param name="line_split_array"></param>
/// <param name="action"></param>
/// <returns></returns>
static HandleLineResult HandleLineV2<T>(Func<T, string, bool> contains, int line_num, T domains, string line_value, out string[] line_split_array, Func<string[], bool?>? func = null)
{
line_split_array = GetLineSplitArray(line_value);
if (func != null)
{
var r = func.Invoke(line_split_array);
if (!r.HasValue) return HandleLineResult.IncorrectFormatNoneHandleNoWrite_Null;
if (!r.Value) return HandleLineResult.IncorrectFormatNoneHandleWrite_W_False;
}
if (line_split_array.Length < 2) return HandleLineResult.IncorrectFormatNoneHandleWrite_W_False;
if (line_split_array[0].StartsWith('#')) return HandleLineResult.IncorrectFormatNoneHandleWrite_W_False;
if (line_split_array.Length > 2 && !line_split_array[2].StartsWith('#')) return HandleLineResult.IncorrectFormatNoneHandleWrite_W_False;
if (IsV1Format(line_split_array)) return HandleLineResult.IncorrectFormatNoneHandleNoWrite_Null; // Compat V1
if (contains != null && contains(domains, line_split_array[1])) return HandleLineResult.Duplicate;
return HandleLineResult.CorrectFormatStartHandle;
}
OperationResult HandleHosts(bool isUpdateOrRemove, IReadOnlyDictionary<string, string>? hosts = null)
{
lock (lockObj)
{
var result = new OperationResult(OperationResultType.Error, AppResources.Hosts_WirteError);
if (!TryOperation(out var errmsg, out var fileInfo, out var removeReadOnly, checkReadOnly: true))
{
result.Message = errmsg;
return result;
}
try
{
var has_hosts = hosts.Any_Nullable();
if (isUpdateOrRemove && !has_hosts) throw new InvalidOperationException();
StringBuilder stringBuilder = new();
HashSet<string> markLength = new(); // mark 标志
Dictionary<string, string> insert_mark_datas = new(); // 直接插入标记区数据
Dictionary<string, (int line_num, string line_value)> backup_insert_mark_datas = new(); // 备份插入标记区数据,项已存在的情况下
Dictionary<string, (int line_num, string line_value)> backup_datas = new(); // 备份区域数据
var encoding = GetEncoding();
using (var fileReader = fileInfo.OpenText(encoding))
{
int line_num = 0;
HashSet<string> domains = new(); // 域名唯一检查
var last_line_value = (string?)null;
while (true)
{
line_num++;
var line_value = fileReader.ReadLine();
if (line_value == null) break;
var not_append = false;
var is_effective_value_v2 = HandleLineV2(line_num, domains, line_value, out var line_split_array, line_split_array =>
{
if (markLength.Contains(BackupMarkStart) && !markLength.Contains(BackupMarkEnd))
{
if (line_split_array.Length >= 2 && line_split_array[0].StartsWith('#') && int.TryParse(line_split_array[0].TrimStart('#'), out var bak_line_num)) // #{line_num} {line_value}
{
var bak_line_split_array = line_split_array.AsSpan()[1..];
if (bak_line_split_array.Length >= 2)
{
backup_datas.TryAdd(bak_line_split_array[1], (bak_line_num, string.Join(' ', bak_line_split_array.ToArray())));
}
}
return null;
}
var mark = GetMarkValue(line_split_array);
if (mark == null) return true;
if (mark == MarkEnd && !markLength.Contains(MarkStart)) return null;
if (mark == BackupMarkEnd && !markLength.Contains(BackupMarkStart)) return null;
if ((mark == MarkStart || mark == BackupMarkStart) && last_line_value != null && string.IsNullOrWhiteSpace(last_line_value))
{
var removeLen = last_line_value.Length + Environment.NewLine.Length;
stringBuilder.Remove(stringBuilder.Length - removeLen, removeLen);
}
if (!markLength.Add(mark)) throw new Exception($"hosts file mark duplicate, value: {mark}");
return null;
});
if (is_effective_value_v2 != HandleLineResult.Duplicate)
{
var is_effective_value = Convert(is_effective_value_v2);
if (!is_effective_value.HasValue) goto skip;
if (!is_effective_value.Value) goto append; // 当前行是无效值,直接写入
}
string ip, domain;
ip = line_split_array[0];
domain = line_split_array[1];
var match_domain = has_hosts && hosts!.ContainsKey(domain); // 与要修改的项匹配
if (markLength.Contains(MarkStart) && !markLength.Contains(MarkEnd)) // 在标记区域内
{
if (match_domain)
{
if (isUpdateOrRemove) // 更新值
{
ip = hosts![domain];
}
else // 删除值
{
goto skip;
}
}
insert_mark_datas[domain] = ip;
goto skip;
}
else // 在标记区域外
{
if (match_domain)
{
if (isUpdateOrRemove) // 更新值
{
insert_mark_datas[domain] = ip;
}
backup_insert_mark_datas[domain] = (line_num, line_value);
goto skip;
}
}
if (not_append)
{
goto skip;
}
append: stringBuilder.AppendLine(line_value);
last_line_value = line_value;
continue;
skip: line_num--;
continue;
}
}
if (isUpdateOrRemove)
{
foreach (var item in hosts!)
{
insert_mark_datas[item.Key] = item.Value;
}
}
var is_restore = !has_hosts && !isUpdateOrRemove;
void Restore(IEnumerable<KeyValuePair<string, (int line_num, string line_value)>> datas)
{
if (!is_restore && datas == backup_datas) datas = new List<KeyValuePair<string, (int line_num, string line_value)>>(datas);
foreach (var item in datas)
{
var line_index = stringBuilder.GetLineIndex(item.Value.line_num - 1);
var line_value = item.Value.line_value;
if (line_index >= 0)
{
stringBuilder.Insert(line_index, $"{line_value}{Environment.NewLine}");
}
else
{
stringBuilder.AppendLine(line_value);
}
if (!is_restore) backup_datas.Remove(item.Key);
}
}
if (is_restore)
{
Restore(backup_datas);
}
else
{
var any_insert_mark_datas = insert_mark_datas.Any();
var restore_backup_datas = any_insert_mark_datas ? backup_datas.Where(x => !insert_mark_datas.ContainsKey(x.Key)) : backup_datas;
Restore(restore_backup_datas); // 恢复备份数据
if (any_insert_mark_datas) // 插入新增数据
{
stringBuilder.AppendLine();
stringBuilder.AppendLine(MarkStart);
foreach (var item in insert_mark_datas)
{
stringBuilder.AppendFormat("{0} {1}", item.Value, item.Key);
stringBuilder.AppendLine();
}
stringBuilder.AppendLine(MarkEnd);
}
var any_backup_insert_mark_datas = backup_insert_mark_datas.Any();
var insert_or_remove_backup_datas = any_insert_mark_datas ? backup_datas.Where(x => insert_mark_datas.ContainsKey(x.Key)) : backup_datas;
if (any_backup_insert_mark_datas) insert_or_remove_backup_datas = insert_or_remove_backup_datas.Where(x => !backup_insert_mark_datas.ContainsKey(x.Key));
if (any_backup_insert_mark_datas || insert_or_remove_backup_datas.Any()) // 插入备份数据
{
stringBuilder.AppendLine();
stringBuilder.AppendLine(BackupMarkStart);
if (any_backup_insert_mark_datas)
{
foreach (var item in backup_insert_mark_datas) // #{line_num} {line_value}
{
stringBuilder.Append('#');
stringBuilder.Append(item.Value.line_num);
stringBuilder.Append(' ');
stringBuilder.AppendLine(item.Value.line_value);
}
}
foreach (var item in insert_or_remove_backup_datas)
{
stringBuilder.AppendLine(item.Value.line_value);
}
stringBuilder.AppendLine(BackupMarkEnd);
}
}
var contents = stringBuilder.ToString();
File.WriteAllText(fileInfo.FullName, contents, encoding);
result.ResultType = OperationResultType.Success;
result.Message = AppResources.Hosts_UpdateSuccess;
}
catch (Exception ex)
{
Log.Error(TAG, ex, "UpdateHosts catch.");
result.ResultType = OperationResultType.Error;
result.AppendData = ex;
if (ex is UnauthorizedAccessException || ex is SecurityException)
{
result.Message = SR.FileUnauthorized;
}
else
{
result.Message = ex.GetAllMessage();
}
return result;
}
if (removeReadOnly) SetReadOnly(fileInfo);
return result;
}
}
#endregion
public void OpenFile() => s.OpenFileByTextReader(s.HostsFilePath);
/// <summary>
/// 获取 hosts 文件编码
/// </summary>
/// <returns></returns>
Encoding GetEncoding() => GeneralSettings.HostsEncodingType.Value switch
{
EncodingType.ANSICodePage => s.Default,
EncodingType.UTF8WithBOM => Encoding.UTF8,
_ => Encoding.Default,
};
static Dictionary<string, string> ReadHostsAllLines(StreamReader fileReader)
{
int index = 0;
Dictionary<string, string> dict = new();
static bool Contains(Dictionary<string, string> d, string v) => d.ContainsKey(v);
while (true)
{
index++;
var line = fileReader.ReadLine();
if (line == null) break;
if (string.IsNullOrWhiteSpace(line)) continue;
var is_effective_value_v2 = HandleLineV2(Contains, index, dict, line, out var array);
if (is_effective_value_v2 == HandleLineResult.Duplicate)
{
dict[array[1]] = array[0];
}
else
{
var is_effective_value = Convert(is_effective_value_v2);
if (!is_effective_value.HasValue || !is_effective_value.Value) continue;
dict.Add(array[1], array[0]);
}
}
return dict;
}
public OperationResult<List<(string ip, string domain)>> ReadHostsAllLines()
{
lock (lockObj)
{
static IEnumerable<(string ip, string domain)> ReadHostsAllLines_(StreamReader fileReader)
{
var value = ReadHostsAllLines(fileReader);
return value.Select(x => (x.Value, x.Key));
}
var result = new OperationResult<List<(string ip, string domain)>>(OperationResultType.Error, AppResources.Hosts_ReadError);
if (!TryOperation(out var errmsg, out var fileInfo, out var _))
{
result.Message = errmsg;
return result;
}
try
{
using var fileReader = fileInfo.OpenText(GetEncoding());
result.AppendData.AddRange(ReadHostsAllLines_(fileReader));
result.ResultType = OperationResultType.Success;
result.Message = AppResources.Hosts_ReadSuccess;
}
catch (Exception ex)
{
Log.Error(TAG, ex, "ReadHostsAllLines catch.");
result.ResultType = OperationResultType.Error;
result.Message = ex.GetAllMessage();
}
return result;
}
}
public OperationResult<Dictionary<string, string>> ReadHostsAllLinesV2()
{
lock (lockObj)
{
OperationResult<Dictionary<string, string>> result;
if (!TryOperation(out var errmsg, out var fileInfo, out var _))
{
result = new OperationResult<Dictionary<string, string>>(OperationResultType.Error, errmsg);
return result;
}
try
{
using var fileReader = fileInfo.OpenText(GetEncoding());
result = new OperationResult<Dictionary<string, string>>()
{
AppendData = ReadHostsAllLines(fileReader),
ResultType = OperationResultType.Success,
Message = AppResources.Hosts_ReadSuccess
};
}
catch (Exception ex)
{
Log.Error(TAG, ex, "ReadHostsAllLines catch.");
result = new OperationResult<Dictionary<string, string>>(OperationResultType.Error, ex.GetAllMessage());
}
return result;
}
}
public OperationResult UpdateHosts(string ip, string domain)
{
var dict = new Dictionary<string, string>
{
{ domain, ip },
};
return UpdateHosts(dict);
}
public OperationResult UpdateHosts(IEnumerable<(string ip, string domain)> hosts)
{
//var value = hosts.ToDictionary(k => k.domain, v => v.ip);
var value = new Dictionary<string, string>();
foreach (var (ip, domain) in hosts)
value.TryAdd(domain, ip);
return UpdateHosts(value);
}
public OperationResult UpdateHosts(IReadOnlyDictionary<string, string> hosts)
{
return HandleHosts(isUpdateOrRemove: true, hosts);
}
public OperationResult RemoveHosts(string ip, string domain)
{
var hosts = new Dictionary<string, string>
{
{ domain, ip },
};
return HandleHosts(isUpdateOrRemove: false, hosts);
}
public OperationResult RemoveHosts(string domain) => RemoveHosts(string.Empty, domain);
public OperationResult RemoveHostsByTag()
{
return HandleHosts(isUpdateOrRemove: false);
}
public bool ContainsHostsByTag()
{
var filePath = s.HostsFilePath;
var fileInfo = new FileInfo(filePath);
if (fileInfo.Exists && fileInfo.Length <= MaxFileLength)
{
var lines = File.ReadAllLines(filePath, GetEncoding());
if (lines.Reverse().Any(x => x.StartsWith(MarkEnd)))
{
return true;
}
}
return false;
}
}
}