From 1900b77ba9fa203fbf0e2f0e31b76f13dabe6e98 Mon Sep 17 00:00:00 2001
From: xljiulang <366193849@qq.com>
Date: Thu, 22 Jul 2021 21:26:40 +0800
Subject: [PATCH] DomainMatch->DomainPattern
---
.../{DomainMatch.cs => DomainPattern.cs} | 11 +++---
FastGithub.Core/FastGithubConfig.cs | 34 +++++++------------
2 files changed, 19 insertions(+), 26 deletions(-)
rename FastGithub.Core/{DomainMatch.cs => DomainPattern.cs} (91%)
diff --git a/FastGithub.Core/DomainMatch.cs b/FastGithub.Core/DomainPattern.cs
similarity index 91%
rename from FastGithub.Core/DomainMatch.cs
rename to FastGithub.Core/DomainPattern.cs
index 70cfa2f..9cd738b 100644
--- a/FastGithub.Core/DomainMatch.cs
+++ b/FastGithub.Core/DomainPattern.cs
@@ -4,19 +4,20 @@ using System.Text.RegularExpressions;
namespace FastGithub
{
///
- /// 域名匹配
+ /// 表示域名表达式
/// *表示除.之外任意0到多个字符
///
- public class DomainMatch : IComparable
+ sealed class DomainPattern : IComparable
{
private readonly Regex regex;
private readonly string domainPattern;
///
- /// 域名匹配
+ /// 域名表达式
+ /// *表示除.之外任意0到多个字符
///
/// 域名表达式
- public DomainMatch(string domainPattern)
+ public DomainPattern(string domainPattern)
{
this.domainPattern = domainPattern;
var regexPattern = Regex.Escape(domainPattern).Replace(@"\*", @"[^\.]*");
@@ -28,7 +29,7 @@ namespace FastGithub
///
///
///
- public int CompareTo(DomainMatch? other)
+ public int CompareTo(DomainPattern? other)
{
if (other is null)
{
diff --git a/FastGithub.Core/FastGithubConfig.cs b/FastGithub.Core/FastGithubConfig.cs
index 4b2dd63..c6f5490 100644
--- a/FastGithub.Core/FastGithubConfig.cs
+++ b/FastGithub.Core/FastGithubConfig.cs
@@ -15,6 +15,7 @@ namespace FastGithub
public class FastGithubConfig
{
private readonly ILogger logger;
+ private SortedDictionary domainConfigs;
private ConcurrentDictionary domainConfigCache;
///
@@ -27,10 +28,6 @@ namespace FastGithub
///
public IPEndPoint FastDns { get; private set; }
- ///
- /// 获取域名配置
- ///
- public SortedDictionary DomainConfigs { get; private set; }
///
/// FastGithub配置
@@ -46,7 +43,7 @@ namespace FastGithub
this.PureDns = opt.PureDns.ToIPEndPoint();
this.FastDns = opt.FastDns.ToIPEndPoint();
- this.DomainConfigs = ConvertDomainConfigs(opt.DomainConfigs);
+ this.domainConfigs = ConvertDomainConfigs(opt.DomainConfigs);
this.domainConfigCache = new ConcurrentDictionary();
options.OnChange(opt => this.Update(opt));
@@ -62,7 +59,7 @@ namespace FastGithub
{
this.PureDns = options.PureDns.ToIPEndPoint();
this.FastDns = options.FastDns.ToIPEndPoint();
- this.DomainConfigs = ConvertDomainConfigs(options.DomainConfigs);
+ this.domainConfigs = ConvertDomainConfigs(options.DomainConfigs);
this.domainConfigCache = new ConcurrentDictionary();
}
catch (Exception ex)
@@ -76,12 +73,12 @@ namespace FastGithub
///
///
///
- private static SortedDictionary ConvertDomainConfigs(Dictionary domainConfigs)
+ private static SortedDictionary ConvertDomainConfigs(Dictionary domainConfigs)
{
- var result = new SortedDictionary();
+ var result = new SortedDictionary();
foreach (var kv in domainConfigs)
{
- result.Add(new DomainMatch(kv.Key), kv.Value);
+ result.Add(new DomainPattern(kv.Key), kv.Value);
}
return result;
}
@@ -93,7 +90,7 @@ namespace FastGithub
///
public bool IsMatch(string domain)
{
- return this.DomainConfigs.Keys.Any(item => item.IsMatch(domain));
+ return this.domainConfigs.Keys.Any(item => item.IsMatch(domain));
}
///
@@ -104,19 +101,14 @@ namespace FastGithub
///
public bool TryGetDomainConfig(string domain, [MaybeNullWhen(false)] out DomainConfig value)
{
- value = this.domainConfigCache.GetOrAdd(domain, this.GetDomainConfig);
+ value = this.domainConfigCache.GetOrAdd(domain, GetDomainConfig);
return value != null;
- }
- ///
- /// 获取域名配置
- ///
- ///
- ///
- private DomainConfig? GetDomainConfig(string domain)
- {
- var key = this.DomainConfigs.Keys.FirstOrDefault(item => item.IsMatch(domain));
- return key == null ? null : this.DomainConfigs[key];
+ DomainConfig? GetDomainConfig(string domain)
+ {
+ var key = this.domainConfigs.Keys.FirstOrDefault(item => item.IsMatch(domain));
+ return key == null ? null : this.domainConfigs[key];
+ }
}
}
}