分开检测不同域名

This commit is contained in:
xljiulang 2021-06-11 23:00:24 +08:00
parent 99ec5d4d9d
commit bca67653a3
8 changed files with 47 additions and 34 deletions

View File

@ -6,9 +6,17 @@ namespace FastGithub
{ {
class GithubContext class GithubContext
{ {
[AllowNull]
public string Domain { get; set; }
[AllowNull] [AllowNull]
public IPAddress Address { get; set; } public IPAddress Address { get; set; }
public TimeSpan? HttpElapsed { get; set; } public TimeSpan? HttpElapsed { get; set; }
public override string ToString()
{
return $"{Address}\t{Domain}\t#{HttpElapsed}";
}
} }
} }

View File

@ -5,9 +5,7 @@ namespace FastGithub
class GithubOptions class GithubOptions
{ {
public Uri MetaUri { get; set; } = new Uri("https://gitee.com/jiulang/fast-github/raw/master/FastGithub/meta.json"); public Uri MetaUri { get; set; } = new Uri("https://gitee.com/jiulang/fast-github/raw/master/FastGithub/meta.json");
public int Concurrent { get; set; } = 50;
public TimeSpan PortScanTimeout { get; set; } = TimeSpan.FromSeconds(1d); public TimeSpan PortScanTimeout { get; set; } = TimeSpan.FromSeconds(1d);
public TimeSpan HttpTestTimeout { get; set; } = TimeSpan.FromSeconds(5d); public TimeSpan HttpTestTimeout { get; set; } = TimeSpan.FromSeconds(5d);

View File

@ -41,9 +41,9 @@ namespace FastGithub
foreach (var context in sortedContexts) foreach (var context in sortedContexts)
{ {
var content = $"{context.Address}\t{context.HttpElapsed}"; var message = context.ToString();
this.logger.LogInformation(content); this.logger.LogInformation(message);
await fileWriter.WriteLineAsync(content); await fileWriter.WriteLineAsync(message);
} }
} }
@ -53,11 +53,12 @@ namespace FastGithub
private IEnumerable<Task> GetMetaScanTasks(Meta meta, IList<GithubContext> contexts) private IEnumerable<Task> GetMetaScanTasks(Meta meta, IList<GithubContext> contexts)
{ {
foreach (var address in meta.ToIPv4Address()) foreach (var item in meta.ToDomainAddress())
{ {
var context = new GithubContext var context = new GithubContext
{ {
Address = address, Domain = item.Domain,
Address = item.Address,
}; };
contexts.Add(context); contexts.Add(context);
yield return this.githubDelegate(context); yield return this.githubDelegate(context);

View File

@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Net; using System.Net;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
@ -36,19 +35,31 @@ namespace FastGithub
public string[] Dependabot { get; set; } = Array.Empty<string>(); public string[] Dependabot { get; set; } = Array.Empty<string>();
public IEnumerable<IPAddress> ToIPv4Address() public IEnumerable<DomainAddress> ToDomainAddress()
{ {
var cidrs = this.Web.Concat(this.Api); foreach (var item in this.Web)
foreach (var cidr in cidrs)
{ {
if (IPv4CIDR.TryParse(cidr, out var value)) if (IPv4CIDR.TryParse(item, out var cidr))
{ {
foreach (var ip in value.GetAllIPAddress()) foreach (var address in cidr.GetAllIPAddress())
{ {
yield return ip; yield return new DomainAddress("github.com", address);
}
}
}
foreach (var item in this.Api)
{
if (IPv4CIDR.TryParse(item, out var cidr))
{
foreach (var address in cidr.GetAllIPAddress())
{
yield return new DomainAddress("api.github.com", address);
} }
} }
} }
} }
public record DomainAddress(string Domain, IPAddress Address);
} }
} }

View File

@ -1,5 +1,4 @@
using Microsoft.Extensions.Options; using System;
using System;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -7,12 +6,7 @@ namespace FastGithub.Middlewares
{ {
sealed class ConcurrentMiddleware : IGithubMiddleware sealed class ConcurrentMiddleware : IGithubMiddleware
{ {
private readonly SemaphoreSlim semaphoreSlim; private readonly SemaphoreSlim semaphoreSlim = new(Environment.ProcessorCount * 4);
public ConcurrentMiddleware(IOptions<GithubOptions> options)
{
this.semaphoreSlim = new SemaphoreSlim(options.Value.Concurrent);
}
public async Task InvokeAsync(GithubContext context, Func<Task> next) public async Task InvokeAsync(GithubContext context, Func<Task> next)
{ {

View File

@ -1,6 +1,7 @@
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using System; using System;
using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -27,9 +28,9 @@ namespace FastGithub.Middlewares
var request = new HttpRequestMessage var request = new HttpRequestMessage
{ {
Method = HttpMethod.Get, Method = HttpMethod.Get,
RequestUri = new Uri($"https://{context.Address}/manifest.json"), RequestUri = new Uri($"https://{context.Address}/"),
}; };
request.Headers.Host = "github.com"; request.Headers.Host = context.Domain;
using var httpClient = new HttpClient(new HttpClientHandler using var httpClient = new HttpClient(new HttpClientHandler
{ {
@ -38,18 +39,19 @@ namespace FastGithub.Middlewares
var startTime = DateTime.Now; var startTime = DateTime.Now;
using var cancellationTokenSource = new CancellationTokenSource(this.options.CurrentValue.HttpTestTimeout); using var cancellationTokenSource = new CancellationTokenSource(this.options.CurrentValue.HttpTestTimeout);
var response = await httpClient.SendAsync(request, cancellationTokenSource.Token); var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationTokenSource.Token);
var media = response.EnsureSuccessStatusCode().Content.Headers.ContentType?.MediaType; var server = response.EnsureSuccessStatusCode().Headers.Server;
if (server.Any(s => string.Equals("GitHub.com", s.Product?.Name, StringComparison.OrdinalIgnoreCase)))
if (string.Equals(media, "application/manifest+json", StringComparison.OrdinalIgnoreCase))
{ {
context.HttpElapsed = DateTime.Now.Subtract(startTime); context.HttpElapsed = DateTime.Now.Subtract(startTime);
this.logger.LogWarning(context.ToString());
await next(); await next();
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
this.logger.LogInformation($"{context.Address} {ex.Message}"); this.logger.LogInformation($"{context.Domain} {context.Address} {ex.Message}");
} }
} }
} }

View File

@ -33,7 +33,7 @@ namespace FastGithub.Middlewares
} }
catch (Exception) catch (Exception)
{ {
this.logger.LogInformation($"{context.Address}的{PORT}端口未开放"); this.logger.LogInformation($"{context.Domain} {context.Address}的{PORT}端口未开放");
} }
} }
} }

View File

@ -1,8 +1,7 @@
{ {
"Github": { "Github": {
"MetaUri": "https://gitee.com/jiulang/fast-github/raw/master/FastGithub/meta.json", // ipuri "MetaUri": "https://gitee.com/jiulang/fast-github/raw/master/FastGithub/meta.json", // ipuri
"Concurrent": 50, // "PortScanTimeout": "00:00:01", //
"PortScanTimeout": "00:00:00.500", //
"HttpTestTimeout": "00:00:05" // http "HttpTestTimeout": "00:00:05" // http
}, },
"Logging": { "Logging": {