ipv6监听

This commit is contained in:
陈国伟 2021-11-18 09:27:39 +08:00
parent 99a8596a8a
commit 6b7450b94f
5 changed files with 27 additions and 27 deletions

View File

@ -2,7 +2,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Net; using System.Net;
using System.Net.NetworkInformation; using System.Net.NetworkInformation;
using System.Net.Sockets;
namespace FastGithub.Configuration namespace FastGithub.Configuration
{ {
@ -29,20 +28,16 @@ namespace FastGithub.Configuration
/// <summary> /// <summary>
/// 获取可用的随机Tcp端口 /// 获取可用的随机Tcp端口
/// </summary> /// </summary>
/// <param name="minValue"></param> /// <param name="minValue"></param>
/// <param name="addressFamily"></param>
/// <returns></returns> /// <returns></returns>
private static int GetAvailableTcpPort(int minValue, AddressFamily addressFamily = AddressFamily.InterNetwork) private static int GetAvailableTcpPort(int minValue)
{ {
var hashSet = new HashSet<int>(); var hashSet = new HashSet<int>();
var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners(); var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
foreach (var endpoint in tcpListeners) foreach (var endpoint in tcpListeners)
{ {
if (endpoint.AddressFamily == addressFamily) hashSet.Add(endpoint.Port);
{
hashSet.Add(endpoint.Port);
}
} }
for (var port = minValue; port < IPEndPoint.MaxPort; port++) for (var port = minValue; port < IPEndPoint.MaxPort; port++)

View File

@ -20,7 +20,6 @@ namespace FastGithub.HttpServer
/// </summary> /// </summary>
sealed class HttpProxyMiddleware sealed class HttpProxyMiddleware
{ {
private const string LOOPBACK = "127.0.0.1";
private const string LOCALHOST = "localhost"; private const string LOCALHOST = "localhost";
private const int HTTP_PORT = 80; private const int HTTP_PORT = 80;
private const int HTTPS_PORT = 443; private const int HTTPS_PORT = 443;
@ -120,7 +119,17 @@ namespace FastGithub.HttpServer
/// <returns></returns> /// <returns></returns>
private bool IsFastGithubServer(HostString host) private bool IsFastGithubServer(HostString host)
{ {
return host.Port == this.fastGithubConfig.HttpProxyPort && (host.Host == LOOPBACK || host.Host == LOCALHOST); if (host.Port != this.fastGithubConfig.HttpProxyPort)
{
return false;
}
if (host.Host == LOCALHOST)
{
return true;
}
return IPAddress.TryParse(host.Host, out var address) && IPAddress.IsLoopback(address);
} }
/// <summary> /// <summary>

View File

@ -8,9 +8,7 @@ using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using System; using System;
using System.Linq; using System.Linq;
using System.Net;
using System.Net.NetworkInformation; using System.Net.NetworkInformation;
using System.Net.Sockets;
namespace FastGithub namespace FastGithub
{ {
@ -45,8 +43,8 @@ namespace FastGithub
} }
var logger = kestrel.GetLogger(); var logger = kestrel.GetLogger();
kestrel.Listen(IPAddress.Loopback, httpProxyPort); kestrel.ListenLocalhost(httpProxyPort);
logger.LogInformation($"已监听http://{IPAddress.Loopback}:{httpProxyPort}http代理服务启动完成"); logger.LogInformation($"已监听http://localhost:{httpProxyPort}http代理服务启动完成");
} }
/// <summary> /// <summary>
@ -56,7 +54,7 @@ namespace FastGithub
public static void ListenSshReverseProxy(this KestrelServerOptions kestrel) public static void ListenSshReverseProxy(this KestrelServerOptions kestrel)
{ {
var sshPort = ReverseProxyPort.Ssh; var sshPort = ReverseProxyPort.Ssh;
kestrel.Listen(IPAddress.Loopback, sshPort, listen => kestrel.ListenLocalhost(sshPort, listen =>
{ {
listen.UseFlowAnalyze(); listen.UseFlowAnalyze();
listen.UseConnectionHandler<SshReverseProxyHandler>(); listen.UseConnectionHandler<SshReverseProxyHandler>();
@ -64,7 +62,7 @@ namespace FastGithub
if (OperatingSystem.IsWindows()) if (OperatingSystem.IsWindows())
{ {
kestrel.GetLogger().LogInformation($"已监听ssh://{IPAddress.Loopback}:{sshPort}github的ssh反向代理服务启动完成"); kestrel.GetLogger().LogInformation($"已监听ssh://localhost:{sshPort}github的ssh反向代理服务启动完成");
} }
} }
@ -75,11 +73,11 @@ namespace FastGithub
public static void ListenHttpReverseProxy(this KestrelServerOptions kestrel) public static void ListenHttpReverseProxy(this KestrelServerOptions kestrel)
{ {
var httpPort = ReverseProxyPort.Http; var httpPort = ReverseProxyPort.Http;
kestrel.Listen(IPAddress.Loopback, httpPort); kestrel.ListenLocalhost(httpPort);
if (OperatingSystem.IsWindows()) if (OperatingSystem.IsWindows())
{ {
kestrel.GetLogger().LogInformation($"已监听http://{IPAddress.Loopback}:{httpPort}http反向代理服务启动完成"); kestrel.GetLogger().LogInformation($"已监听http://localhost:{httpPort}http反向代理服务启动完成");
} }
} }
@ -95,7 +93,7 @@ namespace FastGithub
certService.InstallAndTrustCaCert(); certService.InstallAndTrustCaCert();
var httpsPort = ReverseProxyPort.Https; var httpsPort = ReverseProxyPort.Https;
kestrel.Listen(IPAddress.Loopback, httpsPort, listen => kestrel.ListenLocalhost(httpsPort, listen =>
{ {
if (OperatingSystem.IsWindows()) if (OperatingSystem.IsWindows())
{ {
@ -110,7 +108,7 @@ namespace FastGithub
if (OperatingSystem.IsWindows()) if (OperatingSystem.IsWindows())
{ {
var logger = kestrel.GetLogger(); var logger = kestrel.GetLogger();
logger.LogInformation($"已监听https://{IPAddress.Loopback}:{httpsPort}https反向代理服务启动完成"); logger.LogInformation($"已监听https://localhost:{httpsPort}https反向代理服务启动完成");
} }
} }
@ -128,13 +126,12 @@ namespace FastGithub
/// <summary> /// <summary>
/// 是否可以监听指定tcp端口 /// 是否可以监听指定tcp端口
/// </summary> /// </summary>
/// <param name="port"></param> /// <param name="port"></param>
/// <param name="addressFamily"></param>
/// <returns></returns> /// <returns></returns>
private static bool CanListenTcp(int port, AddressFamily addressFamily = AddressFamily.InterNetwork) private static bool CanListenTcp(int port)
{ {
var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners(); var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
return tcpListeners.Any(item => item.AddressFamily == addressFamily && item.Port == port) == false; return tcpListeners.Any(item => item.Port == port) == false;
} }
} }
} }

View File

@ -142,7 +142,7 @@ namespace FastGithub.PacketIntercept.Dns
// dns响应数据 // dns响应数据
var response = Response.FromRequest(request); var response = Response.FromRequest(request);
var loopback = question.Type == RecordType.A ? IPAddress.Loopback : IPAddress.Loopback.MapToIPv6(); var loopback = question.Type == RecordType.A ? IPAddress.Loopback : IPAddress.IPv6Loopback;
var record = new IPAddressResourceRecord(domain, loopback, this.ttl); var record = new IPAddressResourceRecord(domain, loopback, this.ttl);
response.AnswerRecords.Add(record); response.AnswerRecords.Add(record);
var responsePayload = response.ToArray(); var responsePayload = response.ToArray();

View File

@ -2,7 +2,6 @@
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System; using System;
using System.ComponentModel; using System.ComponentModel;
using System.Net;
using System.Runtime.Versioning; using System.Runtime.Versioning;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -54,7 +53,7 @@ namespace FastGithub.PacketIntercept.Tcp
throw new Win32Exception(); throw new Win32Exception();
} }
this.logger.LogInformation($"tcp://{IPAddress.Loopback}:{this.oldServerPort} => tcp://{IPAddress.Loopback}:{this.newServerPort}"); this.logger.LogInformation($"tcp://localhost:{this.oldServerPort} => tcp://localhost:{this.newServerPort}");
cancellationToken.Register(hwnd => WinDivert.WinDivertClose((IntPtr)hwnd!), handle); cancellationToken.Register(hwnd => WinDivert.WinDivertClose((IntPtr)hwnd!), handle);
var packetLength = 0U; var packetLength = 0U;