Skip to content

Commit

Permalink
分解项目
Browse files Browse the repository at this point in the history
  • Loading branch information
xljiulang committed Jun 15, 2021
1 parent 0ee36f0 commit c5c5443
Show file tree
Hide file tree
Showing 34 changed files with 489 additions and 163 deletions.
12 changes: 12 additions & 0 deletions FastGithub.Core/FastGithub.Core.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="5.0.0" />
</ItemGroup>

</Project>
29 changes: 29 additions & 0 deletions FastGithub.Core/OptionsAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;

namespace FastGithub
{
/// <summary>
/// 表示选项特性
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public sealed class OptionsAttribute : Attribute
{
public string? SessionKey { get; }

/// <summary>
/// 选项特性
/// </summary>
public OptionsAttribute()
{
}

/// <summary>
/// 选项特性
/// </summary>
/// <param name="sessionKey"></param>
public OptionsAttribute(string sessionKey)
{
this.SessionKey = sessionKey;
}
}
}
32 changes: 32 additions & 0 deletions FastGithub.Core/ServiceAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Microsoft.Extensions.DependencyInjection;
using System;

namespace FastGithub
{
/// <summary>
/// 表示服务特性
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
public sealed class ServiceAttribute : Attribute
{
/// <summary>
/// 获取服务的生命周期
/// </summary>
public ServiceLifetime Lifetime { get; }

/// <summary>
/// 获取或设置注册的服务类型
/// 为null直接使得当前类型
/// </summary>
public Type? ServiceType { get; set; }

/// <summary>
/// 将当前实现类型注册为服务的特性
/// </summary>
/// <param name="lifetime">生命周期</param>
public ServiceAttribute(ServiceLifetime lifetime)
{
Lifetime = lifetime;
}
}
}
118 changes: 118 additions & 0 deletions FastGithub.Core/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
using System.Reflection;

namespace FastGithub
{
/// <summary>
/// 服务注册扩展
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// 注册程序集下所有服务下选项
/// </summary>
/// <param name="services"></param>
/// <param name="configuration">配置</param>
/// <returns></returns>
public static IServiceCollection AddServiceAndOptions(this IServiceCollection services, Assembly assembly, IConfiguration configuration)
{
services.AddAttributeServices(assembly);
services.AddAttributeOptions(assembly, configuration);

return services;
}

/// <summary>
/// 添加程序集下ServiceAttribute标记的服务
/// </summary>
/// <param name="services"></param>
/// <param name="assembly"></param>
/// <returns></returns>
private static IServiceCollection AddAttributeServices(this IServiceCollection services, Assembly assembly)
{
var implTypes = assembly
.GetTypes()
.Where(item => item.IsClass && item.IsAbstract == false)
.ToArray();

foreach (var implType in implTypes)
{
var attributes = implType.GetCustomAttributes<ServiceAttribute>(false);
foreach (var attr in attributes)
{
var serviceType = attr.ServiceType ?? implType;
if (services.Any(item => item.ServiceType == serviceType && item.ImplementationType == implType) == false)
{
var descriptor = ServiceDescriptor.Describe(serviceType, implType, attr.Lifetime);
services.Add(descriptor);
}
}
}
return services;
}


/// <summary>
/// 添加程序集下OptionsAttribute标记的服务
/// </summary>
/// <param name="services"></param>
/// <param name="assembly"></param>
/// <param name="configuration"></param>
private static IServiceCollection AddAttributeOptions(this IServiceCollection services, Assembly assembly, IConfiguration configuration)
{
foreach (var optionsType in assembly.GetTypes())
{
var optionsAttribute = optionsType.GetCustomAttribute<OptionsAttribute>();
if (optionsAttribute != null)
{
var key = optionsAttribute.SessionKey ?? optionsType.Name;
var section = configuration.GetSection(key);
OptionsBinder.Create(services, optionsType).Bind(section);
}
}
return services;
}

/// <summary>
/// options绑定器
/// </summary>
private abstract class OptionsBinder
{
public abstract void Bind(IConfiguration configuration);

/// <summary>
/// 创建OptionsBinder实例
/// </summary>
/// <param name="services"></param>
/// <param name="optionsType"></param>
/// <returns></returns>
public static OptionsBinder Create(IServiceCollection services, Type optionsType)
{
var binderType = typeof(OptionsBinderImpl<>).MakeGenericType(optionsType);
var binder = Activator.CreateInstance(binderType, new object[] { services });

return binder is OptionsBinder optionsBinder
? optionsBinder
: throw new TypeInitializationException(binderType.FullName, null);
}

private class OptionsBinderImpl<TOptions> : OptionsBinder where TOptions : class
{
private readonly IServiceCollection services;

public OptionsBinderImpl(IServiceCollection services)
{
this.services = services;
}

public override void Bind(IConfiguration configuration)
{
this.services.AddOptions<TOptions>().Bind(configuration);
}
}
}
}
}
38 changes: 38 additions & 0 deletions FastGithub.Dns/DnsHostedService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using DNS.Server;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Threading;
using System.Threading.Tasks;

namespace FastGithub.Dns
{
sealed class DnsHostedService : IHostedService
{
private readonly DnsServer dnsServer;
private readonly ILogger<DnsHostedService> logger;

public DnsHostedService(
GithubRequestResolver githubRequestResolver,
IOptions<DnsOptions> options,
ILogger<DnsHostedService> logger)
{
this.dnsServer = new DnsServer(githubRequestResolver, options.Value.UpStream);
this.logger = logger;
}

public Task StartAsync(CancellationToken cancellationToken)
{
this.dnsServer.Listen();
this.logger.LogInformation("dns服务启用成功");
return Task.CompletedTask;
}

public Task StopAsync(CancellationToken cancellationToken)
{
this.dnsServer.Dispose();
this.logger.LogInformation("dns服务已终止");
return Task.CompletedTask;
}
}
}
5 changes: 3 additions & 2 deletions FastGithub/DnsOptions.cs → FastGithub.Dns/DnsOptions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System.Net;

namespace FastGithub
namespace FastGithub.Dns
{
class DnsOptions
[Options("Dns")]
sealed class DnsOptions
{
public IPAddress UpStream { get; set; } = IPAddress.Parse("114.114.114.114");
}
Expand Down
27 changes: 27 additions & 0 deletions FastGithub.Dns/DnsServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using FastGithub.Dns;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace FastGithub
{
/// <summary>
/// 服务注册扩展
/// </summary>
public static class DnsServiceCollectionExtensions
{
/// <summary>
/// 注册github的dns服务
/// </summary>
/// <param name="services"></param>
/// <param name="configuration">配置</param>
/// <returns></returns>
public static IServiceCollection AddGithubDns(this IServiceCollection services, IConfiguration configuration)
{
var assembly = typeof(DnsServiceCollectionExtensions).Assembly;
return services
.AddGithubScanner(configuration)
.AddServiceAndOptions(assembly, configuration)
.AddHostedService<DnsHostedService>();
}
}
}
15 changes: 15 additions & 0 deletions FastGithub.Dns/FastGithub.Dns.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="DNS" Version="6.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FastGithub.Scanner\FastGithub.Scanner.csproj" />
</ItemGroup>
</Project>
48 changes: 48 additions & 0 deletions FastGithub.Dns/GithubRequestResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using DNS.Client.RequestResolver;
using DNS.Protocol;
using DNS.Protocol.ResourceRecords;
using FastGithub.Scanner;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace FastGithub.Dns
{
[Service(ServiceLifetime.Singleton)]
sealed class GithubRequestResolver : IRequestResolver
{
private readonly IGithubScanService githubScanService;
private readonly ILogger<GithubRequestResolver> logger;

public GithubRequestResolver(
IGithubScanService githubScanService,
ILogger<GithubRequestResolver> logger)
{
this.githubScanService = githubScanService;
this.logger = logger;
}

public Task<IResponse> Resolve(IRequest request, CancellationToken cancellationToken = default)
{
var response = Response.FromRequest(request);
var question = request.Questions.FirstOrDefault();

if (question != null && question.Type == RecordType.A)
{
var domain = question.Name.ToString();
var fastAddress = this.githubScanService.FindFastAddress(domain);

if (fastAddress != null)
{
var record = new IPAddressResourceRecord(question.Name, fastAddress);
response.AnswerRecords.Add(record);
this.logger.LogInformation(record.ToString());
}
}

return Task.FromResult<IResponse>(response);
}
}
}
18 changes: 18 additions & 0 deletions FastGithub.Scanner/FastGithub.Scanner.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="IPNetwork2" Version="2.5.320" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FastGithub.Core\FastGithub.Core.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Net;

namespace FastGithub
namespace FastGithub.Scanner
{
class GithubContext : IEquatable<GithubContext>
sealed class GithubContext : IEquatable<GithubContext>
{
public string Domain { get; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Generic;

namespace FastGithub
namespace FastGithub.Scanner
{
class GithubContextHashSet : HashSet<GithubContext>
{
Expand Down
Loading

0 comments on commit c5c5443

Please sign in to comment.