From a74711363bbeb045147cff509f64d8e170a35edd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E8=80=81=E4=B9=9D?= <366193849@qq.com>
Date: Tue, 14 Sep 2021 22:02:53 +0800
Subject: [PATCH] =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E4=BB=A3=E7=90=86=E8=AE=BE?=
=?UTF-8?q?=E7=BD=AE=E6=A3=80=E6=B5=8B?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../KestrelServerOptionsExtensions.cs | 41 +++++++++++++++++--
1 file changed, 38 insertions(+), 3 deletions(-)
diff --git a/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs b/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs
index cc0247f..408ab46 100644
--- a/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs
+++ b/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs
@@ -7,8 +7,9 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
+using System.Linq;
using System.Net;
-using System.Runtime.CompilerServices;
+using System.Net.Http;
namespace FastGithub
{
@@ -32,14 +33,48 @@ namespace FastGithub
///
public static void ListenHttpProxy(this KestrelServerOptions kestrel)
{
- var httpProxyPort = kestrel.ApplicationServices.GetRequiredService>().Value.HttpProxyPort;
+ var options = kestrel.ApplicationServices.GetRequiredService>().Value;
+ var httpProxyPort = options.HttpProxyPort;
+
if (LocalMachine.CanListenTcp(httpProxyPort) == false)
{
throw new FastGithubException($"tcp端口{httpProxyPort}已经被其它进程占用,请在配置文件更换{nameof(FastGithubOptions.HttpProxyPort)}为其它端口");
}
+ var logger = kestrel.GetLogger();
kestrel.Listen(IPAddress.Loopback, httpProxyPort);
- kestrel.GetLogger().LogInformation($"已监听http://127.0.0.1:{httpProxyPort},http代理启动完成");
+ logger.LogInformation($"已监听http://127.0.0.1:{httpProxyPort},http代理启动完成");
+
+ if (SystemHasSetHttpProxy() == false)
+ {
+ logger.LogWarning($"请设置系统或浏览器代理为:http://127.0.0.1:{httpProxyPort},或自动代理为http://127.0.0.1:{httpProxyPort}/proxy.pac");
+ }
+
+ bool SystemHasSetHttpProxy()
+ {
+ var systemProxy = HttpClient.DefaultProxy;
+ if (systemProxy == null)
+ {
+ return false;
+ }
+
+ var domainPattern = options.DomainConfigs.Keys.FirstOrDefault();
+ if (domainPattern == null)
+ {
+ return true;
+ }
+
+ var destination = new Uri($"https://{domainPattern.Replace('*', 'a')}");
+ var proxyServer = systemProxy.GetProxy(destination);
+ if (proxyServer == null)
+ {
+ return false;
+ }
+
+ var loopbackProxyUri = new Uri($"http://127.0.0.1:{httpProxyPort}");
+ var localhostProxyUri = new Uri($"http://localhost:{httpProxyPort}");
+ return proxyServer == loopbackProxyUri || proxyServer == localhostProxyUri;
+ }
}
///