IndexService.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. using IES.ExamServer.Helpers;
  2. using Microsoft.Extensions.Caching.Memory;
  3. using System.Diagnostics;
  4. using System.Net.NetworkInformation;
  5. using System.Net;
  6. using System.Runtime.InteropServices;
  7. using System.Text.Json;
  8. using System.Text.Json.Nodes;
  9. using System.Text.RegularExpressions;
  10. using System.Management;
  11. using IES.ExamServer.Models;
  12. using System.CodeDom.Compiler;
  13. using System.Text;
  14. namespace IES.ExamServer.Services
  15. {
  16. public static class IndexService
  17. {
  18. public static ServerDevice GetServerDevice( string remote,string region)
  19. {
  20. string hostName = $"{Environment.UserName}-{Dns.GetHostName()}";
  21. string os = RuntimeInformation.OSDescription;
  22. //获取当前客户端的服务端口
  23. string currentUserName = Environment.UserName;
  24. string MachineName =Environment.MachineName;
  25. //var s = Environment;
  26. ServerDevice device = new ServerDevice { name =hostName, userName=currentUserName, os= os,region=region,remote=remote };
  27. int CpuCoreCount = 0;
  28. long MenemorySize = 0;
  29. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  30. {
  31. CpuCoreCount = Environment.ProcessorCount;
  32. MenemorySize = MemoryInfo.GetTotalPhysicalMemory();
  33. //using (PerformanceCounter pc = new PerformanceCounter("Memory", "Available Bytes"))
  34. //{
  35. // // 获取可用内存
  36. // AvailableMemory = (long)pc.NextValue();
  37. //}
  38. //using (PerformanceCounter pc = new PerformanceCounter("Memory", "Committed Bytes"))
  39. //{
  40. // // 获取可用内存
  41. // CommittedMemory= (long)pc.NextValue();
  42. //}
  43. //MenemorySize=AvailableMemory+CommittedMemory;
  44. //
  45. // 获取CPU核心数
  46. //int processorCount = Environment.ProcessorCount;
  47. //Console.WriteLine("CPU 核心数: " + processorCount);
  48. //using (ManagementClass managementClass = new ManagementClass("Win32_Processor"))
  49. //{
  50. // using (ManagementObjectCollection managementObjectCollection = managementClass.GetInstances())
  51. // {
  52. // foreach (ManagementObject managementObject in managementObjectCollection)
  53. // {
  54. // CpuCoreCount += Convert.ToInt32(managementObject.Properties["NumberOfLogicalProcessors"].Value);
  55. // }
  56. // }
  57. //}
  58. //using (ManagementObjectSearcher searcher1 = new ManagementObjectSearcher("SELECT TotalPhysicalMemory FROM Win32_ComputerSystem"))
  59. //{
  60. // foreach (ManagementObject obj in searcher1.Get())
  61. // {
  62. // long MenemorySizes1 = Convert.ToInt64(obj["TotalPhysicalMemory"]);
  63. // break; // 只需要第一个结果
  64. // }
  65. //}
  66. //using (ManagementClass mc = new ManagementClass("Win32_ComputerSystem"))
  67. //{
  68. // using (ManagementObjectCollection moc = mc.GetInstances())
  69. // {
  70. // foreach (ManagementObject mo in moc)
  71. // {
  72. // if (mo["TotalPhysicalMemory"]!= null)
  73. // {
  74. // MenemorySize = Convert.ToInt64(mo["TotalPhysicalMemory"]);
  75. // }
  76. // }
  77. // }
  78. //}
  79. if (Environment.Is64BitOperatingSystem)
  80. {
  81. device.bit="64";
  82. }
  83. else
  84. {
  85. device.bit="32";
  86. }
  87. //ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT Name, MaxClockSpeed FROM Win32_Processor");
  88. //foreach (ManagementObject mo in searcher.Get())
  89. //{
  90. // string? cpuName = mo["Name"].ToString();
  91. // string? clockSpeed = mo["MaxClockSpeed"].ToString();
  92. // //Console.WriteLine($"CPU 名称: {cpuName}");
  93. // //Console.WriteLine($"CPU 主频: {clockSpeed} MHz");
  94. // device.cpuInfos.Add(new CPUInfo { name = cpuName, hz = clockSpeed });
  95. //}
  96. string cpuName = MemoryInfo.GetCpuName();
  97. var clockSpeed = MemoryInfo.GetCpuSpeed();
  98. device.cpuInfos.Add(new CPUInfo { name = cpuName, hz = $"{clockSpeed}" });
  99. }
  100. else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
  101. {
  102. //int processorCount = Environment.ProcessorCount;
  103. // Console.WriteLine("CPU 核心数: " + processorCount);
  104. try {
  105. string cpuInfo = File.ReadAllText("/proc/cpuinfo");
  106. string[] cpu_lines = cpuInfo.Split('\n');
  107. CpuCoreCount= cpu_lines.Count(line => line.StartsWith("processor", StringComparison.OrdinalIgnoreCase));
  108. string? cpuNameLine = cpuInfo.Split('\n').FirstOrDefault(line => line.StartsWith("model name"));
  109. string? clockSpeedLine = cpuInfo.Split('\n').FirstOrDefault(line => line.StartsWith("cpu MHz"));
  110. string cpuName = string.Empty;
  111. string clockSpeed = string.Empty;
  112. if (cpuNameLine!= null)
  113. {
  114. cpuName = cpuNameLine.Split(':').Last().Trim();
  115. }
  116. if (clockSpeedLine!= null)
  117. {
  118. clockSpeed = clockSpeedLine.Split(':').Last().Trim();
  119. }
  120. device.cpuInfos.Add(new CPUInfo { name = cpuName, hz = clockSpeed });
  121. } catch (Exception ex)
  122. {
  123. }
  124. string[] mem_lines = File.ReadAllLines("/proc/meminfo");
  125. var match = mem_lines.FirstOrDefault(line => line.StartsWith("MemTotal:"));
  126. if (match != null)
  127. {
  128. var matchResult = Regex.Match(match, @"\d+");
  129. if (matchResult.Success)
  130. {
  131. MenemorySize= long.Parse(matchResult.Value);
  132. }
  133. }
  134. }
  135. else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  136. {
  137. try {
  138. using (var process = new Process())
  139. {
  140. process.StartInfo.FileName = "/usr/sbin/sysctl";
  141. process.StartInfo.Arguments = "-n hw.ncpu";
  142. process.StartInfo.RedirectStandardOutput = true;
  143. process.StartInfo.UseShellExecute = false;
  144. process.Start();
  145. string output = process.StandardOutput.ReadToEnd().Trim();
  146. int coreCount;
  147. if (int.TryParse(output, out coreCount))
  148. {
  149. CpuCoreCount= coreCount;
  150. }
  151. }
  152. }
  153. catch (Exception ex) { }
  154. try
  155. {
  156. using (var process = new Process())
  157. {
  158. process.StartInfo.FileName = "/usr/sbin/sysctl";
  159. process.StartInfo.Arguments = "-n hw.memsize";
  160. process.StartInfo.RedirectStandardOutput = true;
  161. process.StartInfo.UseShellExecute = false;
  162. process.Start();
  163. string output = process.StandardOutput.ReadToEnd().Trim();
  164. long memorySize;
  165. if (long.TryParse(output, out memorySize))
  166. {
  167. MenemorySize= memorySize;
  168. }
  169. }
  170. }
  171. catch (Exception ex) { }
  172. try
  173. {
  174. using (var process = new Process())
  175. {
  176. process.StartInfo.FileName = "/usr/sbin/sysctl";
  177. process.StartInfo.Arguments = "-n machdep.cpu.brand_string";
  178. process.StartInfo.RedirectStandardOutput = true;
  179. process.StartInfo.UseShellExecute = false;
  180. process.Start();
  181. string cpuName = process.StandardOutput.ReadToEnd().Trim();
  182. process.StartInfo.FileName = "/usr/sbin/sysctl";
  183. process.StartInfo.Arguments = "-n hw.cpu.frequency";
  184. process.StartInfo.RedirectStandardOutput = true;
  185. process.StartInfo.UseShellExecute = false;
  186. process.Start();
  187. string clockSpeed = process.StandardOutput.ReadToEnd().Trim();
  188. //Console.WriteLine($"CPU 名称: {cpuName}");
  189. //Console.WriteLine($"CPU 主频: {clockSpeed} Hz");
  190. device.cpuInfos.Add(new CPUInfo { name = cpuName, hz = clockSpeed });
  191. }
  192. }
  193. catch (Exception ex)
  194. {
  195. Console.WriteLine($"出现错误: {ex.Message}");
  196. }
  197. if (Environment.Is64BitOperatingSystem)
  198. {
  199. device.bit="64";
  200. }
  201. else
  202. {
  203. device.bit="32";
  204. }
  205. }
  206. if (RuntimeInformation.ProcessArchitecture == Architecture.Arm64)
  207. {
  208. device.arch="ARM64";
  209. }
  210. else if (RuntimeInformation.ProcessArchitecture == Architecture.Arm)
  211. {
  212. device.arch="ARM32";
  213. }
  214. else if (RuntimeInformation.ProcessArchitecture == Architecture.X64)
  215. {
  216. device.arch="X64";
  217. }
  218. else if (RuntimeInformation.ProcessArchitecture == Architecture.X86)
  219. {
  220. device.arch="X86";
  221. }
  222. else
  223. {
  224. device.arch=$"未知({device.arch})";
  225. }
  226. //Console.WriteLine("CPU 核心数: " + CpuCoreCount+",RAM 大小:"+MenemorySize);
  227. device.cpu=CpuCoreCount;
  228. device.ram=MenemorySize;
  229. var nics = NetworkInterface.GetAllNetworkInterfaces();
  230. foreach (var nic in nics)
  231. {
  232. int physical = 0;
  233. if (nic.OperationalStatus == OperationalStatus.Up)
  234. {
  235. //该方法并不能判断是否是物理网卡
  236. //if (nic.NetworkInterfaceType== NetworkInterfaceType.Ethernet//- 以太网接口,通常用于有线网络连接。
  237. // ||nic.NetworkInterfaceType== NetworkInterfaceType.TokenRing//- 令牌环网接口,一种较早的局域网技术。
  238. // ||nic.NetworkInterfaceType== NetworkInterfaceType.Fddi//- 光纤分布式数据接口,用于光纤网络。
  239. // ||nic.NetworkInterfaceType== NetworkInterfaceType.Ethernet3Megabit//- 3兆比特以太网接口,一种较早的以太网标准。
  240. // ||nic.NetworkInterfaceType== NetworkInterfaceType.FastEthernetT// 快速以太网接口(100Base-T),用于双绞线网络。
  241. // ||nic.NetworkInterfaceType== NetworkInterfaceType.FastEthernetFx//快速以太网接口(100Base-FX),用于光纤网络。
  242. // ||nic.NetworkInterfaceType== NetworkInterfaceType.Wireless80211//无线局域网接口(Wi-Fi)。
  243. // ||nic.NetworkInterfaceType== NetworkInterfaceType.GigabitEthernet //千兆以太网接口,提供1 Gbps的数据速率。
  244. // ||nic.NetworkInterfaceType== NetworkInterfaceType.Wman// 移动宽带接口,用于WiMax设备。
  245. // ||nic.NetworkInterfaceType== NetworkInterfaceType.Wwanpp//移动宽带接口,用于GSM设备。
  246. // ||nic.NetworkInterfaceType== NetworkInterfaceType.Wwanpp2//移动宽带接口,用于CDMA设备。
  247. // )
  248. //{
  249. // physical=1;
  250. //}
  251. if (IsPhysicalNetworkInterface(nic))
  252. {
  253. physical = 1;
  254. }
  255. var name = $"{nic.Name}-{nic.Description}";
  256. var mac = nic.GetPhysicalAddress().ToString();
  257. var properties = nic.GetIPProperties();
  258. var unicastAddresses = properties.UnicastAddresses;
  259. foreach (var unicast in unicastAddresses)
  260. {
  261. if (unicast.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
  262. {
  263. var ip = unicast.Address.ToString();
  264. // 绑定域名 优先绑定
  265. Network network = new Network() { mac=mac, ip=ip, name= name, physical=physical };
  266. if (!string.IsNullOrWhiteSpace(mac.ToString()) && !mac.Equals("000000000000"))
  267. {
  268. device.networks.Add(network);
  269. }
  270. }
  271. }
  272. }
  273. }
  274. //if (_url!.IsNotEmpty())
  275. //{
  276. // List<UriInfo> ports = new List<UriInfo>();
  277. // foreach (var url in _url!)
  278. // {
  279. // Uri uri = new Uri(url);
  280. // device.uris.Add(new UriInfo { port= uri.Port, protocol= uri.Scheme });
  281. // }
  282. //}
  283. //else
  284. //{
  285. // throw new Exception("未获取到端口信息!");
  286. //}
  287. var networks = device.networks;
  288. if (device.networks.IsNotEmpty())
  289. {
  290. var order= device.networks.OrderByDescending(x => x.physical).ToList();
  291. for (int i=0; i<order.Count();i++)
  292. {
  293. if (i==0)
  294. {
  295. order[i].domain="exam.habook.local";
  296. }
  297. else {
  298. order[i].domain=$"exam{i}.habook.local";
  299. }
  300. }
  301. //优先以物理网卡来生成hash,如果没有则以所有网卡生成hash
  302. var physical = order.FindAll(x => x.physical==1);
  303. if (physical.IsNotEmpty())
  304. {
  305. networks.AddRange(physical);
  306. }
  307. }
  308. StringBuilder sb= new StringBuilder();
  309. sb.Append(device.name);//设备名称
  310. sb.Append(device.os);//系统名称
  311. sb.Append(device.bit);//系统位
  312. sb.Append(device.arch);//指令架构
  313. sb.Append(device.cpu);//cpu核心树
  314. sb.Append(string.Join(",", device.cpuInfos.Select(x=>$"{x.name}{x.hz}")));//cpu名称和频率
  315. sb.Append(device.ram);//内存
  316. sb.Append(string.Join(",", networks.Select(x => $"{x.mac}-{x.name}")));//网卡地址和名称
  317. //暂不使用远程ip和局域网内ip作为hash,可能发生变化
  318. string hashData = ShaHashHelper.GetSHA256(sb.ToString());
  319. device.deviceId=hashData;
  320. device.id= hashData;
  321. return device;
  322. }
  323. public static bool IsPhysicalNetworkInterface(NetworkInterface nic)
  324. {
  325. // 排除虚拟网卡的关键词
  326. string[] virtualKeywords = { "virtual", "hyper-v", "virtualbox", "vmware", "veth","virbr", "tunnel", "docker", "loopback", "vpn","ppp", "slip", "bridge",
  327. "vnic", "vif", "tap", "vlan", "vswitch", "vxlan", "gre", "ipsec", "vrf", "vport", "vnet", "vmac", "vxnet"};
  328. // 检查网卡描述或名称中是否包含虚拟关键词
  329. string description = nic.Description.ToLower();
  330. string name = nic.Name.ToLower();
  331. foreach (var keyword in virtualKeywords)
  332. {
  333. if (description.Contains(keyword,StringComparison.OrdinalIgnoreCase) || name.Contains(keyword, StringComparison.OrdinalIgnoreCase))
  334. {
  335. return false; // 是虚拟网卡
  336. }
  337. }
  338. // 排除一些常见的虚拟网卡类型
  339. if (nic.NetworkInterfaceType == NetworkInterfaceType.Loopback ||
  340. nic.NetworkInterfaceType == NetworkInterfaceType.Tunnel ||
  341. nic.NetworkInterfaceType == NetworkInterfaceType.Ppp ||
  342. nic.NetworkInterfaceType == NetworkInterfaceType.Slip||
  343. nic.NetworkInterfaceType == NetworkInterfaceType.Unknown
  344. )
  345. {
  346. return false; // 是虚拟网卡
  347. }
  348. // 默认认为是物理网卡
  349. return true;
  350. }
  351. /// <summary>
  352. /// 直接获取设备。
  353. /// </summary>
  354. /// <param name="httpContext"></param>
  355. /// <param name="IP"></param>
  356. /// <param name="device_timeSpan"></param>
  357. /// <param name="_azureRedis"></param>
  358. /// <returns></returns>
  359. public static string GetDevice(HttpContext httpContext, IMemoryCache _memoryCache)
  360. {
  361. string IP= GetIP(httpContext);
  362. var cookie = httpContext.Request.Cookies;
  363. string device =string.Empty;
  364. if (cookie != null)
  365. {
  366. ///设备是否存在
  367. foreach (var ck in cookie)
  368. {
  369. if (ck.Key.Equals("device"))
  370. {
  371. //redis如果存在则
  372. var fingerprint = ck.Value.Split("-");
  373. if (fingerprint.Length == 2 && IP.Equals(fingerprint[1]))
  374. {
  375. if (!_memoryCache.TryGetValue<string>($"device:{fingerprint[0]}:{IP}", out device))
  376. {
  377. _memoryCache.Set($"device:{fingerprint[0]}:{IP}", $"{fingerprint[0]}-{IP}");
  378. device = $"{fingerprint[0]}-{IP}";
  379. }
  380. }
  381. }
  382. }
  383. }
  384. return device;
  385. }
  386. public static string GetIP(HttpContext httpContext)
  387. {
  388. var IpPort = httpContext.Request.Headers["X-Forwarded-For"].FirstOrDefault();
  389. if (string.IsNullOrEmpty(IpPort))
  390. {
  391. IpPort = $"{httpContext.Connection.RemoteIpAddress}";
  392. }
  393. if (IpPort.Contains("::"))
  394. {
  395. IpPort = "127.0.0.1";
  396. }
  397. return IpPort;
  398. }
  399. /// <summary>
  400. /// 初始化设备
  401. /// </summary>
  402. /// <param name="httpContext"></param>
  403. /// <param name="fingerprint">浏览器指纹</param>
  404. /// <param name="IP"></param>
  405. /// <param name="device_timeSpan"></param>
  406. /// <param name="_azureRedis"></param>
  407. /// <returns></returns>
  408. public static string GetDeviceInit(this HttpContext httpContext, string fingerprint, string IP, IMemoryCache _memoryCache)
  409. {
  410. string device = $"{fingerprint}-{IP}";
  411. List<string> cookieString = new List<string>();
  412. var cookie = httpContext.Request.Cookies;
  413. int status = 1;
  414. if (cookie != null)
  415. {
  416. ///设备是否存在
  417. foreach (var ck in cookie)
  418. {
  419. if (ck.Key.Equals("device"))
  420. {
  421. if (device.Contains("-") && device.Contains("."))
  422. {
  423. //如果匹配的是fingerprint-IP 则是已经存在的。
  424. if (ck.Value.Equals(device))
  425. {
  426. //redis如果存在则
  427. _memoryCache.TryGetValue<string>($"device:{fingerprint}:{IP}", out string device_exist);
  428. // 返回的则应该是ck.Value=exist_device_exist的数据
  429. if (device_exist!=null)
  430. {
  431. if (!string.IsNullOrWhiteSpace($"{device_exist}"))
  432. {
  433. //0是代表指纹和IP匹配,正常返回的
  434. status = 1;
  435. }
  436. else
  437. {
  438. //需要新建 fingerprint-IP
  439. status = 1;
  440. }
  441. }
  442. else
  443. {
  444. status = 1;
  445. }
  446. }
  447. else
  448. {
  449. string ck_ip = ck.Value.Split("-")[1];
  450. if (ck_ip.Equals(IP))
  451. {
  452. //传入的指纹和cookie的不一致,仍然以cookie的为准。
  453. status = 1;
  454. fingerprint = ck.Value.Split("-").First();
  455. device = ck.Value;
  456. }
  457. }
  458. }
  459. else
  460. {
  461. //如果匹配的是fingerprint则是一个新的设备。
  462. if (ck.Value.Equals(fingerprint))
  463. {
  464. //检查设备是否被占用
  465. //var device_exist = _azureRedis.GetRedisClient(8).HashExists($"device:{fingerprint}", IP);
  466. _memoryCache.TryGetValue<JsonNode>($"device:{fingerprint}:{IP}", out JsonNode device_exist);
  467. if (device_exist!=null)
  468. {
  469. //需要新建 sha1(fingerprint+uuid)-IP
  470. status = 2;
  471. }
  472. else
  473. {
  474. //0是代表指纹和IP匹配,正常返回的
  475. status = 1;
  476. }
  477. }
  478. else
  479. {
  480. //匹配的都不是,新设备。
  481. status = 1;
  482. }
  483. }
  484. }
  485. else
  486. {
  487. cookieString.Add($"{ck.Key}{ck.Value}");
  488. }
  489. }
  490. }
  491. /*
  492. httpContext.Request.Headers.TryGetValue("accept-language", out var accept_language);
  493. httpContext.Request.Headers.TryGetValue("sec-ch-ua", out var chua);
  494. httpContext.Request.Headers.TryGetValue("sec-ch-ua-platform", out var platform);
  495. httpContext.Request.Headers.TryGetValue("user-agent", out var useragent);
  496. httpContext.Request.Headers.TryGetValue("accept", out var accept);
  497. httpContext.Request.Headers.TryGetValue("accept-encoding", out var accept_encoding);
  498. device = ShaHashHelper.GetSHA1($"{IP}{accept_language}{chua}{platform}{useragent}{accept}{accept_encoding}{string.Join("", cookieString)}");
  499. */
  500. if (status == 2)
  501. {
  502. fingerprint = ShaHashHelper.GetSHA1(fingerprint + Guid.NewGuid().ToString());
  503. device = $"{fingerprint}-{IP}";
  504. }
  505. //else if (status == 1)
  506. //{
  507. // device = $"{fingerprint}-{IP}";
  508. //}
  509. //await _azureRedis.GetRedisClient(8).HashSetAsync($"device:{fingerprint}", IP, new { device }.ToJsonString());
  510. //await _azureRedis.GetRedisClient(8).KeyExpireAsync($"device:{fingerprint}", device_timeSpan);
  511. _memoryCache.Set($"device:{fingerprint}:{IP}", device);
  512. httpContext.Response.Cookies.Append("device", device, new CookieOptions { HttpOnly = true, MaxAge = new TimeSpan(24 * 7, 0, 0) });
  513. return device;
  514. }
  515. }
  516. public class UriInfo
  517. {
  518. public string? protocol { get; set; }
  519. public int port { get; set; }
  520. }
  521. }