using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
namespace FastGithub
{
///
/// FastGithub的配置
///
public class FastGithubOptions
{
///
/// 域名
///
private DomainMatch[]? domainMatches;
private IPEndPoint? trustedDnsEndPoint;
private IPEndPoint? unTrustedDnsEndPoint;
///
/// 受信任的dns服务
///
public IPEndPointOptions TrustedDns { get; set; } = new IPEndPointOptions { IPAddress = "127.0.0.1", Port = 5533 };
///
/// 不受信任的dns服务
///
public IPEndPointOptions UntrustedDns { get; set; } = new IPEndPointOptions { IPAddress = "114.114.114.114", Port = 53 };
///
/// 代理的域名表达式
///
public HashSet DomainPatterns { get; set; } = new();
///
/// 验证选项值
///
///
public void Validate()
{
this.trustedDnsEndPoint = this.TrustedDns.ToIPEndPoint();
this.unTrustedDnsEndPoint = this.UntrustedDns.ToIPEndPoint();
this.domainMatches = this.DomainPatterns.Select(item => new DomainMatch(item)).ToArray();
}
///
/// 受信任的dns服务节点
///
public IPEndPoint GetTrustedDns()
{
return this.trustedDnsEndPoint ?? throw new InvalidOperationException();
}
///
/// 不受信任的dns服务节点
///
public IPEndPoint GetUnTrustedDns()
{
return this.unTrustedDnsEndPoint ?? throw new InvalidOperationException();
}
///
/// 是否匹配指定的域名
///
///
///
public bool IsMatch(string domain)
{
if (this.domainMatches == null)
{
throw new InvalidOperationException();
}
return this.domainMatches.Any(item => item.IsMatch(domain));
}
}
}