From 6fb855f06f61405e2b4415352ea244994af74592 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com>
Date: Thu, 15 Jul 2021 13:25:53 +0800
Subject: [PATCH] =?UTF-8?q?=E6=89=8B=E5=8A=A8=E9=85=8D=E7=BD=AEip?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
FastGithub.Dns/DnsOptions.cs | 5 ++++
FastGithub.Dns/GithubRequestResolver.cs | 33 +++++--------------------
FastGithub/README.MD | 3 ++-
FastGithub/appsettings.json | 3 ++-
4 files changed, 15 insertions(+), 29 deletions(-)
diff --git a/FastGithub.Dns/DnsOptions.cs b/FastGithub.Dns/DnsOptions.cs
index d249c89..ed4316f 100644
--- a/FastGithub.Dns/DnsOptions.cs
+++ b/FastGithub.Dns/DnsOptions.cs
@@ -28,5 +28,10 @@ namespace FastGithub.Dns
/// 是否使用反向代理访问github
///
public bool UseGithubReverseProxy { get; set; }
+
+ ///
+ /// dns响应的反向代理服务的ip
+ ///
+ public IPAddress GithubReverseProxyIPAddress { get; set; } = IPAddress.Loopback;
}
}
diff --git a/FastGithub.Dns/GithubRequestResolver.cs b/FastGithub.Dns/GithubRequestResolver.cs
index 37c54c3..abdd2dc 100644
--- a/FastGithub.Dns/GithubRequestResolver.cs
+++ b/FastGithub.Dns/GithubRequestResolver.cs
@@ -7,8 +7,6 @@ using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Linq;
-using System.Net;
-using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
@@ -46,20 +44,20 @@ namespace FastGithub.Dns
///
///
///
- public async Task Resolve(IRequest request, CancellationToken cancellationToken = default)
+ public Task Resolve(IRequest request, CancellationToken cancellationToken = default)
{
- var response = Response.FromRequest(request);
+ IResponse response = Response.FromRequest(request);
var question = request.Questions.FirstOrDefault();
if (question == null || question.Type != RecordType.A)
{
- return response;
+ return Task.FromResult(response);
}
var domain = question.Name.ToString();
if (this.githubResolver.IsSupported(domain) == false)
{
- return response;
+ return Task.FromResult(response);
}
if (this.options.CurrentValue.UseGithubReverseProxy == false)
@@ -75,7 +73,7 @@ namespace FastGithub.Dns
}
else
{
- var address = await GetLocalHostAddress();
+ var address = this.options.CurrentValue.GithubReverseProxyIPAddress;
var record = new IPAddressResourceRecord(question.Name, address, TimeSpan.FromMinutes(1));
response.AnswerRecords.Add(record);
this.logger.LogInformation($"[{domain}->{address}]");
@@ -85,26 +83,7 @@ namespace FastGithub.Dns
{
this.logger.LogWarning($"无法获得{domain}的最快ip");
}
- return response;
- }
-
- ///
- /// 获取本机ip
- ///
- ///
- private static async Task GetLocalHostAddress()
- {
- try
- {
- var localhost = System.Net.Dns.GetHostName();
- var addresses = await System.Net.Dns.GetHostAddressesAsync(localhost);
- var address = addresses.FirstOrDefault(item => item.AddressFamily == AddressFamily.InterNetwork);
- return address ?? IPAddress.Loopback;
- }
- catch (Exception)
- {
- return IPAddress.Loopback;
- }
+ return Task.FromResult(response);
}
}
}
diff --git a/FastGithub/README.MD b/FastGithub/README.MD
index 1653a84..79419fd 100644
--- a/FastGithub/README.MD
+++ b/FastGithub/README.MD
@@ -1,11 +1,12 @@
# FastGithub
github定制版的dns服务,解析github最优的ip
-### 本机使用
+### 本机使用[推荐]
* 运行FastGithub程序,本机的网络适配器的dns会自动变成127.0.0.1
* 如果网络适配器的dns没有变成127.0.0.1,请手工修改网络适配器的dns
### 局域网服务器使用
+* 修改appsettings.json文件的`Dns:GithubReverseProxyIPAddress`为局域网ip
* 在局域网服务器运行FastGithub程序
* 手工修改你电脑的网络适配器的dns,值为局域网服务器的ip
* 在你的电脑安装FastGithub.cer到`将所有的证书都放入下载存储\受信任的根证书颁发机构`
diff --git a/FastGithub/appsettings.json b/FastGithub/appsettings.json
index 9ce2715..3a04cc0 100644
--- a/FastGithub/appsettings.json
+++ b/FastGithub/appsettings.json
@@ -3,7 +3,8 @@
"UpStream": "114.114.114.114", // dns
"GithubTTL": "00:10:00", // githubĴʱ
"SetToLocalMachine": true, // Ƿñʹôdns(֧windows)
- "UseGithubReverseProxy": true // Ƿʹ÷githubԽӱظ
+ "UseGithubReverseProxy": true, // Ƿʹ÷githubԽӱظ
+ "GithubReverseProxyIPAddress": "127.0.0.1" // dnsӦķipʱҪдip
},
"Lookup": { // ip
"IPAddressComProvider": {