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