diff --git a/FastGithub.Core/FastGithub.Core.csproj b/FastGithub.Core/FastGithub.Core.csproj
new file mode 100644
index 0000000..9b80a69
--- /dev/null
+++ b/FastGithub.Core/FastGithub.Core.csproj
@@ -0,0 +1,12 @@
+
+
+
+ net5.0
+ enable
+
+
+
+
+
+
+
diff --git a/FastGithub.Core/OptionsAttribute.cs b/FastGithub.Core/OptionsAttribute.cs
new file mode 100644
index 0000000..65dadb6
--- /dev/null
+++ b/FastGithub.Core/OptionsAttribute.cs
@@ -0,0 +1,29 @@
+using System;
+
+namespace FastGithub
+{
+ ///
+ /// 表示选项特性
+ ///
+ [AttributeUsage(AttributeTargets.Class)]
+ public sealed class OptionsAttribute : Attribute
+ {
+ public string? SessionKey { get; }
+
+ ///
+ /// 选项特性
+ ///
+ public OptionsAttribute()
+ {
+ }
+
+ ///
+ /// 选项特性
+ ///
+ ///
+ public OptionsAttribute(string sessionKey)
+ {
+ this.SessionKey = sessionKey;
+ }
+ }
+}
diff --git a/FastGithub.Core/ServiceAttribute.cs b/FastGithub.Core/ServiceAttribute.cs
new file mode 100644
index 0000000..be8f59d
--- /dev/null
+++ b/FastGithub.Core/ServiceAttribute.cs
@@ -0,0 +1,32 @@
+using Microsoft.Extensions.DependencyInjection;
+using System;
+
+namespace FastGithub
+{
+ ///
+ /// 表示服务特性
+ ///
+ [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
+ public sealed class ServiceAttribute : Attribute
+ {
+ ///
+ /// 获取服务的生命周期
+ ///
+ public ServiceLifetime Lifetime { get; }
+
+ ///
+ /// 获取或设置注册的服务类型
+ /// 为null直接使得当前类型
+ ///
+ public Type? ServiceType { get; set; }
+
+ ///
+ /// 将当前实现类型注册为服务的特性
+ ///
+ /// 生命周期
+ public ServiceAttribute(ServiceLifetime lifetime)
+ {
+ Lifetime = lifetime;
+ }
+ }
+}
diff --git a/FastGithub.Core/ServiceCollectionExtensions.cs b/FastGithub.Core/ServiceCollectionExtensions.cs
new file mode 100644
index 0000000..7b0e744
--- /dev/null
+++ b/FastGithub.Core/ServiceCollectionExtensions.cs
@@ -0,0 +1,118 @@
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.DependencyInjection;
+using System;
+using System.Linq;
+using System.Reflection;
+
+namespace FastGithub
+{
+ ///
+ /// 服务注册扩展
+ ///
+ public static class ServiceCollectionExtensions
+ {
+ ///
+ /// 注册程序集下所有服务下选项
+ ///
+ ///
+ /// 配置
+ ///
+ public static IServiceCollection AddServiceAndOptions(this IServiceCollection services, Assembly assembly, IConfiguration configuration)
+ {
+ services.AddAttributeServices(assembly);
+ services.AddAttributeOptions(assembly, configuration);
+
+ return services;
+ }
+
+ ///
+ /// 添加程序集下ServiceAttribute标记的服务
+ ///
+ ///
+ ///
+ ///
+ 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(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;
+ }
+
+
+ ///
+ /// 添加程序集下OptionsAttribute标记的服务
+ ///
+ ///
+ ///
+ ///
+ private static IServiceCollection AddAttributeOptions(this IServiceCollection services, Assembly assembly, IConfiguration configuration)
+ {
+ foreach (var optionsType in assembly.GetTypes())
+ {
+ var optionsAttribute = optionsType.GetCustomAttribute();
+ if (optionsAttribute != null)
+ {
+ var key = optionsAttribute.SessionKey ?? optionsType.Name;
+ var section = configuration.GetSection(key);
+ OptionsBinder.Create(services, optionsType).Bind(section);
+ }
+ }
+ return services;
+ }
+
+ ///
+ /// options绑定器
+ ///
+ private abstract class OptionsBinder
+ {
+ public abstract void Bind(IConfiguration configuration);
+
+ ///
+ /// 创建OptionsBinder实例
+ ///
+ ///
+ ///
+ ///
+ 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 : OptionsBinder where TOptions : class
+ {
+ private readonly IServiceCollection services;
+
+ public OptionsBinderImpl(IServiceCollection services)
+ {
+ this.services = services;
+ }
+
+ public override void Bind(IConfiguration configuration)
+ {
+ this.services.AddOptions().Bind(configuration);
+ }
+ }
+ }
+ }
+}
diff --git a/FastGithub.Dns/DnsHostedService.cs b/FastGithub.Dns/DnsHostedService.cs
new file mode 100644
index 0000000..1a952fb
--- /dev/null
+++ b/FastGithub.Dns/DnsHostedService.cs
@@ -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 logger;
+
+ public DnsHostedService(
+ GithubRequestResolver githubRequestResolver,
+ IOptions options,
+ ILogger 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;
+ }
+ }
+}
diff --git a/FastGithub/DnsOptions.cs b/FastGithub.Dns/DnsOptions.cs
similarity index 62%
rename from FastGithub/DnsOptions.cs
rename to FastGithub.Dns/DnsOptions.cs
index a77c824..39ef373 100644
--- a/FastGithub/DnsOptions.cs
+++ b/FastGithub.Dns/DnsOptions.cs
@@ -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");
}
diff --git a/FastGithub.Dns/DnsServiceCollectionExtensions.cs b/FastGithub.Dns/DnsServiceCollectionExtensions.cs
new file mode 100644
index 0000000..ccbf60b
--- /dev/null
+++ b/FastGithub.Dns/DnsServiceCollectionExtensions.cs
@@ -0,0 +1,27 @@
+using FastGithub.Dns;
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.DependencyInjection;
+
+namespace FastGithub
+{
+ ///
+ /// 服务注册扩展
+ ///
+ public static class DnsServiceCollectionExtensions
+ {
+ ///
+ /// 注册github的dns服务
+ ///
+ ///
+ /// 配置
+ ///
+ public static IServiceCollection AddGithubDns(this IServiceCollection services, IConfiguration configuration)
+ {
+ var assembly = typeof(DnsServiceCollectionExtensions).Assembly;
+ return services
+ .AddGithubScanner(configuration)
+ .AddServiceAndOptions(assembly, configuration)
+ .AddHostedService();
+ }
+ }
+}
diff --git a/FastGithub.Dns/FastGithub.Dns.csproj b/FastGithub.Dns/FastGithub.Dns.csproj
new file mode 100644
index 0000000..33f532d
--- /dev/null
+++ b/FastGithub.Dns/FastGithub.Dns.csproj
@@ -0,0 +1,15 @@
+
+
+
+ net5.0
+ enable
+
+
+
+
+
+
+
+
+
+
diff --git a/FastGithub.Dns/GithubRequestResolver.cs b/FastGithub.Dns/GithubRequestResolver.cs
new file mode 100644
index 0000000..98e8c66
--- /dev/null
+++ b/FastGithub.Dns/GithubRequestResolver.cs
@@ -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 logger;
+
+ public GithubRequestResolver(
+ IGithubScanService githubScanService,
+ ILogger logger)
+ {
+ this.githubScanService = githubScanService;
+ this.logger = logger;
+ }
+
+ public Task 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(response);
+ }
+ }
+}
diff --git a/FastGithub.Scanner/FastGithub.Scanner.csproj b/FastGithub.Scanner/FastGithub.Scanner.csproj
new file mode 100644
index 0000000..493d088
--- /dev/null
+++ b/FastGithub.Scanner/FastGithub.Scanner.csproj
@@ -0,0 +1,18 @@
+
+
+
+ net5.0
+ enable
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/FastGithub/GithubContext.cs b/FastGithub.Scanner/GithubContext.cs
similarity index 91%
rename from FastGithub/GithubContext.cs
rename to FastGithub.Scanner/GithubContext.cs
index ff467e0..3f233b7 100644
--- a/FastGithub/GithubContext.cs
+++ b/FastGithub.Scanner/GithubContext.cs
@@ -1,9 +1,9 @@
using System;
using System.Net;
-namespace FastGithub
+namespace FastGithub.Scanner
{
- class GithubContext : IEquatable
+ sealed class GithubContext : IEquatable
{
public string Domain { get; }
diff --git a/FastGithub/GithubContextHashSet.cs b/FastGithub.Scanner/GithubContextHashSet.cs
similarity index 84%
rename from FastGithub/GithubContextHashSet.cs
rename to FastGithub.Scanner/GithubContextHashSet.cs
index d4bd3f2..828893a 100644
--- a/FastGithub/GithubContextHashSet.cs
+++ b/FastGithub.Scanner/GithubContextHashSet.cs
@@ -1,6 +1,6 @@
using System.Collections.Generic;
-namespace FastGithub
+namespace FastGithub.Scanner
{
class GithubContextHashSet : HashSet
{
diff --git a/FastGithub/GithubScanAllHostedService.cs b/FastGithub.Scanner/GithubFullScanHostedService.cs
similarity index 71%
rename from FastGithub/GithubScanAllHostedService.cs
rename to FastGithub.Scanner/GithubFullScanHostedService.cs
index 819e4d5..6fc2061 100644
--- a/FastGithub/GithubScanAllHostedService.cs
+++ b/FastGithub.Scanner/GithubFullScanHostedService.cs
@@ -1,17 +1,18 @@
-using Microsoft.Extensions.Hosting;
+using FastGithub.Scanner;
+using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using System.Threading;
using System.Threading.Tasks;
namespace FastGithub
{
- sealed class GithubScanAllHostedService : BackgroundService
+ sealed class GithubFullScanHostedService : BackgroundService
{
- private readonly GithubScanService githubScanService;
+ private readonly IGithubScanService githubScanService;
private readonly IOptionsMonitor options;
- public GithubScanAllHostedService(
- GithubScanService githubScanService,
+ public GithubFullScanHostedService(
+ IGithubScanService githubScanService,
IOptionsMonitor options)
{
this.githubScanService = githubScanService;
diff --git a/FastGithub/GithubMetaService.cs b/FastGithub.Scanner/GithubMetaService.cs
similarity index 88%
rename from FastGithub/GithubMetaService.cs
rename to FastGithub.Scanner/GithubMetaService.cs
index fd7911c..265d556 100644
--- a/FastGithub/GithubMetaService.cs
+++ b/FastGithub.Scanner/GithubMetaService.cs
@@ -1,4 +1,5 @@
-using Microsoft.Extensions.Logging;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Net.Http;
@@ -6,8 +7,9 @@ using System.Net.Http.Json;
using System.Threading;
using System.Threading.Tasks;
-namespace FastGithub
+namespace FastGithub.Scanner
{
+ [Service(ServiceLifetime.Singleton)]
sealed class GithubMetaService
{
private readonly IHttpClientFactory httpClientFactory;
diff --git a/FastGithub/GithubOptions.cs b/FastGithub.Scanner/GithubOptions.cs
similarity index 90%
rename from FastGithub/GithubOptions.cs
rename to FastGithub.Scanner/GithubOptions.cs
index cfcae52..c5f751c 100644
--- a/FastGithub/GithubOptions.cs
+++ b/FastGithub.Scanner/GithubOptions.cs
@@ -1,7 +1,8 @@
using System;
-namespace FastGithub
+namespace FastGithub.Scanner
{
+ [Options("Github")]
class GithubOptions
{
public TimeSpan ScanAllInterval { get; set; } = TimeSpan.FromHours(12d);
diff --git a/FastGithub/GithubScanResultHostedService.cs b/FastGithub.Scanner/GithubResultScanHostedService.cs
similarity index 71%
rename from FastGithub/GithubScanResultHostedService.cs
rename to FastGithub.Scanner/GithubResultScanHostedService.cs
index 6442a81..11ab4a0 100644
--- a/FastGithub/GithubScanResultHostedService.cs
+++ b/FastGithub.Scanner/GithubResultScanHostedService.cs
@@ -1,17 +1,18 @@
-using Microsoft.Extensions.Hosting;
+using FastGithub.Scanner;
+using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using System.Threading;
using System.Threading.Tasks;
namespace FastGithub
{
- sealed class GithubScanResultHostedService : BackgroundService
+ sealed class GithubResultScanHostedService : BackgroundService
{
- private readonly GithubScanService githubScanService;
+ private readonly IGithubScanService githubScanService;
private readonly IOptionsMonitor options;
- public GithubScanResultHostedService(
- GithubScanService githubScanService,
+ public GithubResultScanHostedService(
+ IGithubScanService githubScanService,
IOptionsMonitor options)
{
this.githubScanService = githubScanService;
diff --git a/FastGithub/GithubScanBuilder.cs b/FastGithub.Scanner/GithubScanBuilder.cs
similarity index 98%
rename from FastGithub/GithubScanBuilder.cs
rename to FastGithub.Scanner/GithubScanBuilder.cs
index d346926..077e0a3 100644
--- a/FastGithub/GithubScanBuilder.cs
+++ b/FastGithub.Scanner/GithubScanBuilder.cs
@@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
-namespace FastGithub
+namespace FastGithub.Scanner
{
///
/// 表示中间件创建者
diff --git a/FastGithub/GithubScanBuilderExtensions.cs b/FastGithub.Scanner/GithubScanBuilderExtensions.cs
similarity index 97%
rename from FastGithub/GithubScanBuilderExtensions.cs
rename to FastGithub.Scanner/GithubScanBuilderExtensions.cs
index 6c99ce3..e70cca5 100644
--- a/FastGithub/GithubScanBuilderExtensions.cs
+++ b/FastGithub.Scanner/GithubScanBuilderExtensions.cs
@@ -1,7 +1,7 @@
using System;
using System.Threading.Tasks;
-namespace FastGithub
+namespace FastGithub.Scanner
{
///
/// 中间件创建者扩展
diff --git a/FastGithub/GithubScanDelegate.cs b/FastGithub.Scanner/GithubScanDelegate.cs
similarity index 90%
rename from FastGithub/GithubScanDelegate.cs
rename to FastGithub.Scanner/GithubScanDelegate.cs
index d2908d3..7ae6c83 100644
--- a/FastGithub/GithubScanDelegate.cs
+++ b/FastGithub.Scanner/GithubScanDelegate.cs
@@ -1,6 +1,6 @@
using System.Threading.Tasks;
-namespace FastGithub
+namespace FastGithub.Scanner
{
///
/// 表示所有中间件执行委托
diff --git a/FastGithub/GithubScanService.cs b/FastGithub.Scanner/GithubScanService.cs
similarity index 73%
rename from FastGithub/GithubScanService.cs
rename to FastGithub.Scanner/GithubScanService.cs
index b0a2a87..11755bf 100644
--- a/FastGithub/GithubScanService.cs
+++ b/FastGithub.Scanner/GithubScanService.cs
@@ -1,12 +1,15 @@
-using Microsoft.Extensions.Logging;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Logging;
+using System;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
-namespace FastGithub
+namespace FastGithub.Scanner
{
- sealed class GithubScanService
+ [Service(ServiceLifetime.Singleton, ServiceType = typeof(IGithubScanService))]
+ sealed class GithubScanService : IGithubScanService
{
private readonly GithubMetaService metaService;
private readonly GithubScanDelegate scanDelegate;
@@ -25,6 +28,7 @@ namespace FastGithub
public async Task ScanAllAsync(CancellationToken cancellationToken = default)
{
+ this.logger.LogInformation("完整扫描开始");
var meta = await this.metaService.GetMetaAsync(cancellationToken);
if (meta != null)
{
@@ -32,7 +36,7 @@ namespace FastGithub
await Task.WhenAll(scanTasks);
}
- this.logger.LogInformation("完全扫描完成");
+ this.logger.LogInformation("完整扫描结束");
async Task ScanAsync(GithubContext context)
{
@@ -49,6 +53,7 @@ namespace FastGithub
public async Task ScanResultAsync()
{
+ this.logger.LogInformation("结果扫描开始");
GithubContext[] contexts;
lock (this.results.SyncRoot)
{
@@ -61,18 +66,23 @@ namespace FastGithub
await this.scanDelegate(context);
}
- this.logger.LogInformation("结果扫描完成");
+ this.logger.LogInformation("结果扫描结束");
}
- public IPAddress[] FindAddress(string domain)
+ public IPAddress? FindFastAddress(string domain)
{
+ if (domain.Contains("github", StringComparison.OrdinalIgnoreCase))
+ {
+ return default;
+ }
+
lock (this.results.SyncRoot)
{
return this.results
.Where(item => item.Domain == domain && item.HttpElapsed != null)
.OrderBy(item => item.HttpElapsed)
.Select(item => item.Address)
- .ToArray();
+ .FirstOrDefault();
}
}
}
diff --git a/FastGithub/IGithubScanBuilder.cs b/FastGithub.Scanner/IGithubScanBuilder.cs
similarity index 95%
rename from FastGithub/IGithubScanBuilder.cs
rename to FastGithub.Scanner/IGithubScanBuilder.cs
index 94495c9..3ad3453 100644
--- a/FastGithub/IGithubScanBuilder.cs
+++ b/FastGithub.Scanner/IGithubScanBuilder.cs
@@ -1,6 +1,6 @@
using System;
-namespace FastGithub
+namespace FastGithub.Scanner
{
///
/// 定义中间件管道创建者的接口
diff --git a/FastGithub/IGithubScanMiddleware.cs b/FastGithub.Scanner/IGithubScanMiddleware.cs
similarity index 93%
rename from FastGithub/IGithubScanMiddleware.cs
rename to FastGithub.Scanner/IGithubScanMiddleware.cs
index 6dc1bec..d1cdf62 100644
--- a/FastGithub/IGithubScanMiddleware.cs
+++ b/FastGithub.Scanner/IGithubScanMiddleware.cs
@@ -1,7 +1,7 @@
using System;
using System.Threading.Tasks;
-namespace FastGithub
+namespace FastGithub.Scanner
{
///
/// 定义中间件的接口
diff --git a/FastGithub.Scanner/IGithubScanService.cs b/FastGithub.Scanner/IGithubScanService.cs
new file mode 100644
index 0000000..44553a3
--- /dev/null
+++ b/FastGithub.Scanner/IGithubScanService.cs
@@ -0,0 +1,13 @@
+using System.Net;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace FastGithub.Scanner
+{
+ public interface IGithubScanService
+ {
+ Task ScanAllAsync(CancellationToken cancellationToken = default);
+ Task ScanResultAsync();
+ IPAddress? FindFastAddress(string domain);
+ }
+}
\ No newline at end of file
diff --git a/FastGithub/IPRange.cs b/FastGithub.Scanner/IPRange.cs
similarity index 99%
rename from FastGithub/IPRange.cs
rename to FastGithub.Scanner/IPRange.cs
index 910a953..f59c08a 100644
--- a/FastGithub/IPRange.cs
+++ b/FastGithub.Scanner/IPRange.cs
@@ -6,7 +6,7 @@ using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Net.Sockets;
-namespace FastGithub
+namespace FastGithub.Scanner
{
sealed class IPRange : IEnumerable
{
diff --git a/FastGithub/Meta.cs b/FastGithub.Scanner/Meta.cs
similarity index 98%
rename from FastGithub/Meta.cs
rename to FastGithub.Scanner/Meta.cs
index 310e01a..cac86c0 100644
--- a/FastGithub/Meta.cs
+++ b/FastGithub.Scanner/Meta.cs
@@ -4,7 +4,7 @@ using System.Linq;
using System.Net.Sockets;
using System.Text.Json.Serialization;
-namespace FastGithub
+namespace FastGithub.Scanner
{
class Meta
{
diff --git a/FastGithub/Middlewares/ConcurrentMiddleware.cs b/FastGithub.Scanner/Middlewares/ConcurrentMiddleware.cs
similarity index 78%
rename from FastGithub/Middlewares/ConcurrentMiddleware.cs
rename to FastGithub.Scanner/Middlewares/ConcurrentMiddleware.cs
index 5a0f38b..918b2dc 100644
--- a/FastGithub/Middlewares/ConcurrentMiddleware.cs
+++ b/FastGithub.Scanner/Middlewares/ConcurrentMiddleware.cs
@@ -1,9 +1,11 @@
-using System;
+using Microsoft.Extensions.DependencyInjection;
+using System;
using System.Threading;
using System.Threading.Tasks;
-namespace FastGithub.Middlewares
+namespace FastGithub.Scanner.Middlewares
{
+ [Service(ServiceLifetime.Singleton)]
sealed class ConcurrentMiddleware : IGithubScanMiddleware
{
private readonly SemaphoreSlim semaphoreSlim = new(Environment.ProcessorCount * 4);
diff --git a/FastGithub/Middlewares/HttpsScanMiddleware.cs b/FastGithub.Scanner/Middlewares/HttpsScanMiddleware.cs
similarity index 92%
rename from FastGithub/Middlewares/HttpsScanMiddleware.cs
rename to FastGithub.Scanner/Middlewares/HttpsScanMiddleware.cs
index 2eee908..e974495 100644
--- a/FastGithub/Middlewares/HttpsScanMiddleware.cs
+++ b/FastGithub.Scanner/Middlewares/HttpsScanMiddleware.cs
@@ -1,4 +1,5 @@
-using Microsoft.Extensions.Logging;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Linq;
@@ -6,8 +7,9 @@ using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
-namespace FastGithub.Middlewares
+namespace FastGithub.Scanner.Middlewares
{
+ [Service(ServiceLifetime.Singleton)]
sealed class HttpsScanMiddleware : IGithubScanMiddleware
{
private readonly IOptionsMonitor options;
diff --git a/FastGithub/Middlewares/PortScanMiddleware.cs b/FastGithub.Scanner/Middlewares/PortScanMiddleware.cs
similarity index 88%
rename from FastGithub/Middlewares/PortScanMiddleware.cs
rename to FastGithub.Scanner/Middlewares/PortScanMiddleware.cs
index fff41a9..f3058c9 100644
--- a/FastGithub/Middlewares/PortScanMiddleware.cs
+++ b/FastGithub.Scanner/Middlewares/PortScanMiddleware.cs
@@ -1,12 +1,14 @@
-using Microsoft.Extensions.Logging;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
-namespace FastGithub.Middlewares
+namespace FastGithub.Scanner.Middlewares
{
+ [Service(ServiceLifetime.Singleton)]
sealed class PortScanMiddleware : IGithubScanMiddleware
{
private const int PORT = 443;
diff --git a/FastGithub/Middlewares/ScanOkLogMiddleware.cs b/FastGithub.Scanner/Middlewares/ScanOkLogMiddleware.cs
similarity index 77%
rename from FastGithub/Middlewares/ScanOkLogMiddleware.cs
rename to FastGithub.Scanner/Middlewares/ScanOkLogMiddleware.cs
index 0e3984d..52aafc5 100644
--- a/FastGithub/Middlewares/ScanOkLogMiddleware.cs
+++ b/FastGithub.Scanner/Middlewares/ScanOkLogMiddleware.cs
@@ -1,9 +1,11 @@
-using Microsoft.Extensions.Logging;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
-namespace FastGithub.Middlewares
+namespace FastGithub.Scanner.Middlewares
{
+ [Service(ServiceLifetime.Singleton)]
sealed class ScanOkLogMiddleware : IGithubScanMiddleware
{
private readonly ILogger logger;
diff --git a/FastGithub.Scanner/ScannerServiceCollectionExtensions.cs b/FastGithub.Scanner/ScannerServiceCollectionExtensions.cs
new file mode 100644
index 0000000..8fd9137
--- /dev/null
+++ b/FastGithub.Scanner/ScannerServiceCollectionExtensions.cs
@@ -0,0 +1,40 @@
+using FastGithub.Scanner;
+using FastGithub.Scanner.Middlewares;
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.DependencyInjection;
+using System.Threading.Tasks;
+
+namespace FastGithub
+{
+ ///
+ /// 服务注册扩展
+ ///
+ public static class ScannerServiceCollectionExtensions
+ {
+ ///
+ /// 注册程序集下所有服务下选项
+ ///
+ ///
+ /// 配置
+ ///
+ public static IServiceCollection AddGithubScanner(this IServiceCollection services, IConfiguration configuration)
+ {
+ var assembly = typeof(ScannerServiceCollectionExtensions).Assembly;
+ return services
+ .AddHttpClient()
+ .AddSingleton(serviceProvider =>
+ {
+ return new GithubScanBuilder(serviceProvider, ctx => Task.CompletedTask)
+ .Use()
+ .Use()
+ .Use()
+ .Use()
+ .Build();
+ })
+ .AddServiceAndOptions(assembly, configuration)
+ .AddHostedService()
+ .AddHostedService()
+ ;
+ }
+ }
+}
diff --git a/FastGithub.sln b/FastGithub.sln
index 6f75c34..2b0900c 100644
--- a/FastGithub.sln
+++ b/FastGithub.sln
@@ -5,6 +5,12 @@ VisualStudioVersion = 16.0.31320.298
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastGithub", "FastGithub\FastGithub.csproj", "{C1099390-6103-4917-A740-A3002B542FE0}"
EndProject
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastGithub.Core", "FastGithub.Core\FastGithub.Core.csproj", "{4E4841D2-F743-40BB-BE28-729DB53775CC}"
+EndProject
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastGithub.Dns", "FastGithub.Dns\FastGithub.Dns.csproj", "{43FF9C79-51D5-4037-AA0B-CA3006E2A7E6}"
+EndProject
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastGithub.Scanner", "FastGithub.Scanner\FastGithub.Scanner.csproj", "{7F24CD2F-07C0-4002-A534-80688DE95ECF}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -15,6 +21,18 @@ Global
{C1099390-6103-4917-A740-A3002B542FE0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C1099390-6103-4917-A740-A3002B542FE0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C1099390-6103-4917-A740-A3002B542FE0}.Release|Any CPU.Build.0 = Release|Any CPU
+ {4E4841D2-F743-40BB-BE28-729DB53775CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {4E4841D2-F743-40BB-BE28-729DB53775CC}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {4E4841D2-F743-40BB-BE28-729DB53775CC}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {4E4841D2-F743-40BB-BE28-729DB53775CC}.Release|Any CPU.Build.0 = Release|Any CPU
+ {43FF9C79-51D5-4037-AA0B-CA3006E2A7E6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {43FF9C79-51D5-4037-AA0B-CA3006E2A7E6}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {43FF9C79-51D5-4037-AA0B-CA3006E2A7E6}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {43FF9C79-51D5-4037-AA0B-CA3006E2A7E6}.Release|Any CPU.Build.0 = Release|Any CPU
+ {7F24CD2F-07C0-4002-A534-80688DE95ECF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {7F24CD2F-07C0-4002-A534-80688DE95ECF}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {7F24CD2F-07C0-4002-A534-80688DE95ECF}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {7F24CD2F-07C0-4002-A534-80688DE95ECF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
diff --git a/FastGithub/DnsHostedService.cs b/FastGithub/DnsHostedService.cs
deleted file mode 100644
index 3e4d0bc..0000000
--- a/FastGithub/DnsHostedService.cs
+++ /dev/null
@@ -1,76 +0,0 @@
-using DNS.Client.RequestResolver;
-using DNS.Protocol;
-using DNS.Protocol.ResourceRecords;
-using DNS.Server;
-using Microsoft.Extensions.Hosting;
-using Microsoft.Extensions.Logging;
-using Microsoft.Extensions.Options;
-using System;
-using System.Linq;
-using System.Threading;
-using System.Threading.Tasks;
-
-namespace FastGithub
-{
- sealed class DnsHostedService : IHostedService, IRequestResolver
- {
- private readonly DnsServer dnsServer;
- private readonly GithubScanService githubScanService;
- private readonly ILogger logger;
-
- public DnsHostedService(
- GithubScanService githubScanService,
- IOptions options,
- ILogger logger)
- {
- this.dnsServer = new DnsServer(this, options.Value.UpStream);
- this.githubScanService = githubScanService;
- this.logger = logger;
- }
-
- ///
- /// 解析dns
- ///
- ///
- ///
- ///
- Task IRequestResolver.Resolve(IRequest request, CancellationToken cancellationToken)
- {
- var response = Response.FromRequest(request);
- var question = request.Questions.FirstOrDefault();
-
- if (question != null && question.Type == RecordType.A)
- {
- var domain = question.Name.ToString();
- if (domain.Contains("github", StringComparison.OrdinalIgnoreCase))
- {
- var addressArray = this.githubScanService.FindAddress(domain);
- foreach (var address in addressArray)
- {
- var record = new IPAddressResourceRecord(question.Name, address);
- response.AnswerRecords.Add(record);
- }
-
- var addressString = string.Join(',', addressArray.Select(a => a.ToString()));
- this.logger.LogInformation($"{domain}=>{addressString}");
- }
- }
-
- return Task.FromResult(response);
- }
-
- 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;
- }
- }
-}
diff --git a/FastGithub/FastGithub.csproj b/FastGithub/FastGithub.csproj
index 0d95d9a..d10c702 100644
--- a/FastGithub/FastGithub.csproj
+++ b/FastGithub/FastGithub.csproj
@@ -1,24 +1,21 @@
-
- Exe
- enable
- net5.0
- true
- 1.0.2
-
+
+ Exe
+ enable
+ net5.0
+ true
+ 1.0.2
+
-
-
-
-
-
-
+
+
+
-
-
- PreserveNewest
-
-
+
+
+ PreserveNewest
+
+
diff --git a/FastGithub/Program.cs b/FastGithub/Program.cs
index c026562..916f2bd 100644
--- a/FastGithub/Program.cs
+++ b/FastGithub/Program.cs
@@ -1,7 +1,4 @@
-using FastGithub.Middlewares;
-using Microsoft.Extensions.DependencyInjection;
-using Microsoft.Extensions.Hosting;
-using System.Threading.Tasks;
+using Microsoft.Extensions.Hosting;
namespace FastGithub
{
@@ -27,32 +24,8 @@ namespace FastGithub
.CreateDefaultBuilder(args)
.ConfigureServices((ctx, services) =>
{
- services
- .Configure(ctx.Configuration.GetSection("Dns"))
- .Configure(ctx.Configuration.GetSection("Github"))
- .AddHttpClient()
- .AddSingleton()
- .AddSingleton()
-
- .AddSingleton()
- .AddSingleton()
- .AddSingleton()
- .AddSingleton()
- .AddSingleton(serviceProvider =>
- {
- return new GithubScanBuilder(serviceProvider, ctx => Task.CompletedTask)
- .Use()
- .Use()
- .Use()
- .Use()
- .Build();
- })
- .AddHostedService()
- .AddHostedService()
- .AddHostedService()
- ;
+ services.AddGithubDns(ctx.Configuration);
});
-
}
}
}