优化hosts检测
This commit is contained in:
parent
a1e4aed2dc
commit
44f23c2ed6
@ -1,7 +1,7 @@
|
|||||||
using FastGithub.Configuration;
|
using FastGithub.Configuration;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
@ -48,45 +48,73 @@ namespace FastGithub.Dns
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var lines = await File.ReadAllLinesAsync(hostsPath);
|
var localAddresses = IPGlobalProperties
|
||||||
var records = lines.Where(item => item.TrimStart().StartsWith("#") == false);
|
.GetIPGlobalProperties()
|
||||||
var localAddresses = GetLocalMachineIPAddress().ToArray();
|
.GetUnicastAddresses()
|
||||||
|
.Select(item => item.Address)
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
foreach (var record in records)
|
var lines = await File.ReadAllLinesAsync(hostsPath);
|
||||||
|
foreach (var line in lines)
|
||||||
{
|
{
|
||||||
|
if (HostsRecord.TryParse(line, out var record) == false)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (localAddresses.Contains(record.Address) == true)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (this.fastGithubConfig.IsMatch(record.Domain))
|
||||||
|
{
|
||||||
|
this.logger.LogError($"由于你的hosts文件设置了[{record.Domain}->{record.Address}],{nameof(FastGithub)}无法加速此域名");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// hosts文件记录
|
||||||
|
/// </summary>
|
||||||
|
private class HostsRecord
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 获取域名
|
||||||
|
/// </summary>
|
||||||
|
public string Domain { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取地址
|
||||||
|
/// </summary>
|
||||||
|
public IPAddress Address { get; }
|
||||||
|
|
||||||
|
private HostsRecord(string domain, IPAddress address)
|
||||||
|
{
|
||||||
|
this.Domain = domain;
|
||||||
|
this.Address = address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool TryParse(string record, [MaybeNullWhen(false)] out HostsRecord value)
|
||||||
|
{
|
||||||
|
value = null;
|
||||||
|
if (record.TrimStart().StartsWith("#"))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
var items = record.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
var items = record.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
||||||
if (items.Length < 2)
|
if (items.Length < 2)
|
||||||
{
|
{
|
||||||
continue;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IPAddress.TryParse(items[0], out var address) == false)
|
if (IPAddress.TryParse(items[0], out var address) == false)
|
||||||
{
|
{
|
||||||
continue;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (localAddresses.Contains(address))
|
value = new HostsRecord(items[1], address);
|
||||||
{
|
return true;
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var domain = items[1];
|
|
||||||
if (this.fastGithubConfig.IsMatch(domain))
|
|
||||||
{
|
|
||||||
this.logger.LogError($"由于你的hosts文件设置了[{domain}->{address}],{nameof(FastGithub)}无法加速此域名");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 获取本机所有ip
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
private static IEnumerable<IPAddress> GetLocalMachineIPAddress()
|
|
||||||
{
|
|
||||||
foreach (var item in IPGlobalProperties.GetIPGlobalProperties().GetUnicastAddresses())
|
|
||||||
{
|
|
||||||
yield return item.Address;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user