完善LocalMachine

This commit is contained in:
陈国伟 2021-08-04 09:24:53 +08:00
parent 89b66d4526
commit 19fdab2bdc
5 changed files with 33 additions and 27 deletions

View File

@ -18,7 +18,7 @@ namespace FastGithub.Configuration
public static string Name => Environment.MachineName; public static string Name => Environment.MachineName;
/// <summary> /// <summary>
/// 获取设备所有IP /// 获取本机设备所有IP
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<IPAddress> GetAllIPAddresses() public static IEnumerable<IPAddress> GetAllIPAddresses()
@ -36,7 +36,7 @@ namespace FastGithub.Configuration
} }
/// <summary> /// <summary>
/// 获取设备所有IPv4 /// 获取本机设备所有IPv4
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<IPAddress> GetAllIPv4Addresses() public static IEnumerable<IPAddress> GetAllIPv4Addresses()
@ -51,7 +51,7 @@ namespace FastGithub.Configuration
} }
/// <summary> /// <summary>
/// 返回设备是否包含指定IP /// 返回本机设备是否包含指定IP
/// </summary> /// </summary>
/// <param name="address"></param> /// <param name="address"></param>
/// <returns></returns> /// <returns></returns>
@ -61,11 +61,11 @@ namespace FastGithub.Configuration
} }
/// <summary> /// <summary>
/// 获取对应的本机地址 /// 获取与远程节点通讯的的本机IP地址
/// </summary> /// </summary>
/// <param name="remoteEndPoint">远程地址</param> /// <param name="remoteEndPoint">远程地址</param>
/// <returns></returns> /// <returns></returns>
public static IPAddress? GetLocalAddress(EndPoint remoteEndPoint) public static IPAddress? GetLocalIPAddress(EndPoint remoteEndPoint)
{ {
try try
{ {
@ -78,5 +78,27 @@ namespace FastGithub.Configuration
return default; return default;
} }
} }
/// <summary>
/// 是否可以监听指定tcp端口
/// </summary>
/// <param name="port"></param>
/// <returns></returns>
public static bool CanListenTcp(int port)
{
var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
return tcpListeners.Any(item => item.Port == port) == false;
}
/// <summary>
/// 是否可以监听指定udp端口
/// </summary>
/// <param name="port"></param>
/// <returns></returns>
public static bool CanListenUdp(int port)
{
var udpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners();
return udpListeners.Any(item => item.Port == port) == false;
}
} }
} }

View File

@ -2,9 +2,7 @@
using FastGithub.Configuration; using FastGithub.Configuration;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System; using System;
using System.Linq;
using System.Net; using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets; using System.Net.Sockets;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -47,8 +45,7 @@ namespace FastGithub.Dns
UdpTable.KillPortOwner(port); UdpTable.KillPortOwner(port);
} }
var udpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners(); if (LocalMachine.CanListenUdp(port) == false)
if (udpListeners.Any(item => item.Port == port))
{ {
throw new FastGithubException($"udp端口{port}已经被其它进程占用"); throw new FastGithubException($"udp端口{port}已经被其它进程占用");
} }

View File

@ -29,9 +29,9 @@ namespace FastGithub.Dns
/// 获取对应的本机地址 /// 获取对应的本机地址
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public IPAddress? GetLocalAddress() public IPAddress? GetLocalIPAddress()
{ {
return LocalMachine.GetLocalAddress(this.RemoteEndPoint); return LocalMachine.GetLocalIPAddress(this.RemoteEndPoint);
} }
} }
} }

View File

@ -57,7 +57,7 @@ namespace FastGithub.Dns
var domain = question.Name; var domain = question.Name;
if (this.fastGithubConfig.IsMatch(domain.ToString()) == true) if (this.fastGithubConfig.IsMatch(domain.ToString()) == true)
{ {
var localAddress = remoteEndPointRequest.GetLocalAddress() ?? IPAddress.Loopback; var localAddress = remoteEndPointRequest.GetLocalIPAddress() ?? IPAddress.Loopback;
var record = new IPAddressResourceRecord(domain, localAddress, this.ttl); var record = new IPAddressResourceRecord(domain, localAddress, this.ttl);
response.AnswerRecords.Add(record); response.AnswerRecords.Add(record);

View File

@ -5,9 +5,7 @@ using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System; using System;
using System.Linq;
using System.Net; using System.Net;
using System.Net.NetworkInformation;
using System.Security.Authentication; using System.Security.Authentication;
namespace FastGithub namespace FastGithub
@ -29,7 +27,7 @@ namespace FastGithub
TcpTable.KillPortOwner(HTTP_PORT); TcpTable.KillPortOwner(HTTP_PORT);
} }
if (CanTcpListen(HTTP_PORT) == false) if (LocalMachine.CanListenTcp(HTTP_PORT) == false)
{ {
var loggerFactory = kestrel.ApplicationServices.GetRequiredService<ILoggerFactory>(); var loggerFactory = kestrel.ApplicationServices.GetRequiredService<ILoggerFactory>();
var logger = loggerFactory.CreateLogger($"{nameof(FastGithub)}.{nameof(ReverseProxy)}"); var logger = loggerFactory.CreateLogger($"{nameof(FastGithub)}.{nameof(ReverseProxy)}");
@ -53,7 +51,7 @@ namespace FastGithub
TcpTable.KillPortOwner(HTTPS_PORT); TcpTable.KillPortOwner(HTTPS_PORT);
} }
if (CanTcpListen(HTTPS_PORT) == false) if (LocalMachine.CanListenTcp(HTTPS_PORT) == false)
{ {
throw new FastGithubException($"由于tcp端口{HTTPS_PORT}已经被其它进程占用,{nameof(FastGithub)}无法进行必须的https反向代理"); throw new FastGithubException($"由于tcp端口{HTTPS_PORT}已经被其它进程占用,{nameof(FastGithub)}无法进行必须的https反向代理");
} }
@ -71,16 +69,5 @@ namespace FastGithub
https.ServerCertificateSelector = (ctx, domain) => certService.GetOrCreateServerCert(domain); https.ServerCertificateSelector = (ctx, domain) => certService.GetOrCreateServerCert(domain);
})); }));
} }
/// <summary>
/// 是否可以监听指定端口
/// </summary>
/// <param name="port"></param>
/// <returns></returns>
private static bool CanTcpListen(int port)
{
var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
return tcpListeners.Any(item => item.Port == port) == false;
}
} }
} }