增加域名排序
This commit is contained in:
parent
58ffa9a2e9
commit
6abc044e90
@ -1,11 +1,12 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using System;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace FastGithub
|
||||
{
|
||||
/// <summary>
|
||||
/// 域名匹配
|
||||
/// </summary>
|
||||
public class DomainMatch
|
||||
public class DomainMatch : IComparable<DomainMatch>
|
||||
{
|
||||
private readonly Regex regex;
|
||||
private readonly string domainPattern;
|
||||
@ -21,6 +22,82 @@ namespace FastGithub
|
||||
this.regex = new Regex($"^{regexPattern}$", RegexOptions.IgnoreCase);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 与目标比较
|
||||
/// </summary>
|
||||
/// <param name="other"></param>
|
||||
/// <returns></returns>
|
||||
public int CompareTo(DomainMatch? other)
|
||||
{
|
||||
if (other is null)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
var segmentsX = this.domainPattern.Split('.');
|
||||
var segmentsY = other.domainPattern.Split('.');
|
||||
if (segmentsX.Length > segmentsY.Length)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if (segmentsX.Length < segmentsY.Length)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (var i = 0; i < segmentsX.Length; i++)
|
||||
{
|
||||
var x = segmentsX[i];
|
||||
var y = segmentsY[i];
|
||||
|
||||
var value = Compare(x, y);
|
||||
if (value == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 比较两个分段
|
||||
/// </summary>
|
||||
/// <param name="x">abc</param>
|
||||
/// <param name="y">abc*</param>
|
||||
/// <returns></returns>
|
||||
private static int Compare(string x, string y)
|
||||
{
|
||||
if (x == y)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
var valueX = x.Replace("*", null);
|
||||
var valueY = y.Replace("*", null);
|
||||
|
||||
var maskX = x.Length - valueX.Length;
|
||||
var maskY = y.Length - valueY.Length;
|
||||
if (maskX == 0 && maskY > 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
if (maskY == 0 && maskX > 0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
var value = valueX.CompareTo(valueY);
|
||||
if (value == 0)
|
||||
{
|
||||
value = x.CompareTo(y);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 是否与指定域名匹配
|
||||
/// </summary>
|
||||
|
||||
@ -30,7 +30,7 @@ namespace FastGithub
|
||||
/// <summary>
|
||||
/// 获取域名配置
|
||||
/// </summary>
|
||||
public Dictionary<DomainMatch, DomainConfig> DomainConfigs { get; private set; }
|
||||
public SortedDictionary<DomainMatch, DomainConfig> DomainConfigs { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// FastGithub配置
|
||||
@ -42,12 +42,12 @@ namespace FastGithub
|
||||
ILogger<FastGithubConfig> logger)
|
||||
{
|
||||
this.logger = logger;
|
||||
|
||||
var opt = options.CurrentValue;
|
||||
this.domainConfigCache = new ConcurrentDictionary<string, DomainConfig?>();
|
||||
|
||||
this.PureDns = opt.PureDns.ToIPEndPoint();
|
||||
this.FastDns = opt.FastDns.ToIPEndPoint();
|
||||
this.DomainConfigs = opt.DomainConfigs.ToDictionary(kv => new DomainMatch(kv.Key), kv => kv.Value);
|
||||
this.DomainConfigs = ConvertDomainConfigs(opt.DomainConfigs);
|
||||
this.domainConfigCache = new ConcurrentDictionary<string, DomainConfig?>();
|
||||
|
||||
options.OnChange(opt => this.Update(opt));
|
||||
}
|
||||
@ -60,10 +60,10 @@ namespace FastGithub
|
||||
{
|
||||
try
|
||||
{
|
||||
this.domainConfigCache = new ConcurrentDictionary<string, DomainConfig?>();
|
||||
this.PureDns = options.PureDns.ToIPEndPoint();
|
||||
this.FastDns = options.FastDns.ToIPEndPoint();
|
||||
this.DomainConfigs = options.DomainConfigs.ToDictionary(kv => new DomainMatch(kv.Key), kv => kv.Value);
|
||||
this.DomainConfigs = ConvertDomainConfigs(options.DomainConfigs);
|
||||
this.domainConfigCache = new ConcurrentDictionary<string, DomainConfig?>();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -71,6 +71,21 @@ namespace FastGithub
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置转换
|
||||
/// </summary>
|
||||
/// <param name="domainConfigs"></param>
|
||||
/// <returns></returns>
|
||||
private static SortedDictionary<DomainMatch, DomainConfig> ConvertDomainConfigs(Dictionary<string, DomainConfig> domainConfigs)
|
||||
{
|
||||
var result = new SortedDictionary<DomainMatch, DomainConfig>();
|
||||
foreach (var kv in domainConfigs)
|
||||
{
|
||||
result.Add(new DomainMatch(kv.Key), kv.Value);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否匹配指定的域名
|
||||
/// </summary>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user