Program.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using HTEX.ScreenClient.Services;
  2. using System.Diagnostics;
  3. using System.Management;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Runtime.InteropServices;
  7. using System.Text.RegularExpressions;
  8. namespace HTEX.ScreenClient
  9. {
  10. public class Program
  11. {
  12. public static void Main(string[] args)
  13. {
  14. long CpuCoreCount = 0;
  15. long MenemorySize = 0;
  16. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ) {
  17. // 获取CPU核心数
  18. int processorCount = Environment.ProcessorCount;
  19. Console.WriteLine("CPU 核心数: " + processorCount);
  20. using (ManagementClass managementClass = new ManagementClass("Win32_Processor"))
  21. {
  22. using (ManagementObjectCollection managementObjectCollection = managementClass.GetInstances())
  23. {
  24. foreach (ManagementObject managementObject in managementObjectCollection)
  25. {
  26. CpuCoreCount += Convert.ToInt32(managementObject.Properties["NumberOfLogicalProcessors"].Value);
  27. }
  28. }
  29. }
  30. using (ManagementClass mc = new ManagementClass("Win32_ComputerSystem"))
  31. {
  32. using (ManagementObjectCollection moc = mc.GetInstances())
  33. {
  34. foreach (ManagementObject mo in moc)
  35. {
  36. if (mo["TotalPhysicalMemory"]!= null)
  37. {
  38. MenemorySize = Convert.ToInt64(mo["TotalPhysicalMemory"]);
  39. }
  40. }
  41. }
  42. }
  43. }
  44. else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) {
  45. int processorCount = Environment.ProcessorCount;
  46. Console.WriteLine("CPU 核心数: " + processorCount);
  47. string[] cpu_lines = File.ReadAllLines("/proc/cpuinfo");
  48. CpuCoreCount= cpu_lines.Count(line => line.StartsWith("processor", StringComparison.OrdinalIgnoreCase));
  49. string[] mem_lines = File.ReadAllLines("/proc/meminfo");
  50. var match = mem_lines.FirstOrDefault(line => line.StartsWith("MemTotal:"));
  51. if (match != null)
  52. {
  53. var matchResult = Regex.Match(match, @"\d+");
  54. if (matchResult.Success)
  55. {
  56. MenemorySize= long.Parse(matchResult.Value);
  57. }
  58. }
  59. } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) {
  60. using (var process = new Process())
  61. {
  62. process.StartInfo.FileName = "/usr/sbin/sysctl";
  63. process.StartInfo.Arguments = "-n hw.ncpu";
  64. process.StartInfo.RedirectStandardOutput = true;
  65. process.StartInfo.UseShellExecute = false;
  66. process.Start();
  67. string output = process.StandardOutput.ReadToEnd().Trim();
  68. int coreCount;
  69. if (int.TryParse(output, out coreCount))
  70. {
  71. CpuCoreCount= coreCount;
  72. }
  73. }
  74. using (var process = new Process())
  75. {
  76. process.StartInfo.FileName = "/usr/sbin/sysctl";
  77. process.StartInfo.Arguments = "-n hw.memsize";
  78. process.StartInfo.RedirectStandardOutput = true;
  79. process.StartInfo.UseShellExecute = false;
  80. process.Start();
  81. string output = process.StandardOutput.ReadToEnd().Trim();
  82. long memorySize;
  83. if (long.TryParse(output, out memorySize))
  84. {
  85. MenemorySize= memorySize;
  86. }
  87. }
  88. }
  89. Console.WriteLine("CPU 核心数: " + CpuCoreCount+",RAM 大小:"+MenemorySize);
  90. var builder = WebApplication.CreateBuilder(args);
  91. builder.Services.AddControllers();
  92. builder.Services.AddHttpClient();
  93. builder.Services.AddHttpContextAccessor();
  94. //CheckOrNewPort(1883)
  95. //CheckOrNewPort(5000)
  96. builder.WebHost.UseKestrel(options => {
  97. //options.ListenAnyIP(4001, options => {
  98. // // options.UseHttps("Crt/iteden.pfx", "iteden");
  99. //});
  100. options.ListenAnyIP(1883, options => {/*options.UseHttps("Crt/iteden.pfx", "iteden");*/ });
  101. options.ListenAnyIP(5000, options => {/* options.UseHttps("Configs/Crt/iteden.pfx", "iteden"); */}); // Default HTTP pipeline
  102. });
  103. builder.Services.AddHostedService<SignalRScreenClientHub>();
  104. var app = builder.Build();
  105. // Configure the HTTP request pipeline.
  106. app.UseHttpsRedirection();
  107. app.UseAuthorization();
  108. app.MapControllers();
  109. app.Run();
  110. }
  111. /// <summary>
  112. /// 检测端口是否可用,如果不可用,则递归调用本方法,直到可用为止
  113. /// </summary>
  114. /// <param name="port"></param>
  115. /// <returns></returns>
  116. public static int CheckOrNewPort(int port)
  117. {
  118. if (IsPortAvailable(port))
  119. {
  120. return port;
  121. }
  122. else
  123. {
  124. return CheckOrNewPort(port + 1);
  125. }
  126. }
  127. /// <summary>
  128. /// 探测端口是否可用
  129. /// </summary>
  130. /// <param name="port"></param>
  131. /// <returns></returns>
  132. public static bool IsPortAvailable(int port)
  133. {
  134. bool isPortAvailable = false;
  135. IPAddress ipAddress = IPAddress.Parse("127.0.0.1"); // 本地地址
  136. try
  137. {
  138. using (Socket socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
  139. {
  140. // 尝试连接到指定的IP地址和端口
  141. IAsyncResult result = socket.BeginConnect(ipAddress, port, null, null);
  142. // 等待连接尝试完成(这里简单地使用Socket的ConnectTimeout,但更常见的做法是使用超时等待或异步回调)
  143. bool success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(1), true);
  144. if (success)
  145. {
  146. // 如果连接成功,则端口被占用
  147. socket.EndConnect(result);
  148. isPortAvailable = false;
  149. // 可选:如果需要,可以在这里关闭连接
  150. socket.Shutdown(SocketShutdown.Both);
  151. socket.Close();
  152. }
  153. else
  154. {
  155. isPortAvailable = true;
  156. }
  157. }
  158. }
  159. catch (SocketException ex)
  160. {
  161. Console.WriteLine(ex.Message);
  162. // 如果错误代码为10048,则表示地址已被使用
  163. if (ex.SocketErrorCode == SocketError.AddressAlreadyInUse)
  164. {
  165. isPortAvailable = false;
  166. }
  167. }
  168. catch (Exception ex)
  169. {
  170. isPortAvailable = false;
  171. Console.WriteLine(ex.Message);
  172. }
  173. return isPortAvailable;
  174. }
  175. }
  176. }