forked from shiftwinting/FastGithub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
489 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
4 changes: 2 additions & 2 deletions
4
FastGithub/GithubContext.cs → FastGithub.Scanner/GithubContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
FastGithub/GithubContextHashSet.cs → FastGithub.Scanner/GithubContextHashSet.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.