123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573 |
- using IES.ExamServer.Helpers;
- using Microsoft.Extensions.Caching.Memory;
- using System.Diagnostics;
- using System.Net.NetworkInformation;
- using System.Net;
- using System.Runtime.InteropServices;
- using System.Text.Json.Nodes;
- using System.Text.RegularExpressions;
- using IES.ExamServer.Models;
- using System.Text;
- using IES.ExamServer.DI;
- using IES.ExamServer.Helper;
- namespace IES.ExamServer.Services
- {
- public static class IndexService
- {
- public static async Task<(string content_response, HttpStatusCode code)> GetMusicServer_thirdLocalCacheConfig(IHttpClientFactory httpClientFactory, CenterServiceConnectionService _connectionService , string requestBody)
- {
- string content_response = string.Empty;
- HttpStatusCode code = HttpStatusCode.OK ;
- try {
- string url = _connectionService!.musicUrl!;
- var client = httpClientFactory.CreateClient();
- //client.DefaultRequestHeaders.Add("Accept", "application/json, text/plain, */*");
- client.DefaultRequestHeaders.Add("appId", Constant._MusicAIServerAppId);
- client.DefaultRequestHeaders.Add("encrypt", "1");
- client.DefaultRequestHeaders.Add("schoolCode", _connectionService.serverDevice?.school?.id);
- var content = new StringContent(requestBody, Encoding.UTF8, "application/json");
- // 发送 PUT 请求
- HttpResponseMessage response = await client.PutAsync($"{url}/musicLocalCache/thirdLocalCacheConfig", content);
- // 处理响应
- if (response.IsSuccessStatusCode)
- {
- content_response = await response.Content.ReadAsStringAsync();
- //Console.WriteLine("Response: " + responseData);
- }
- else
- {
- // Console.WriteLine("Error: " + response.StatusCode);
- code=response.StatusCode;
- content_response = await response.Content.ReadAsStringAsync();
- }
- } catch (Exception ex) {
- code= HttpStatusCode.InternalServerError;
- content_response = ex.Message;
- }
- return (content_response, code);
- }
- public static ServerDevice GetServerDevice( string remote,string region)
- {
- string hostName = $"{Environment.UserName}-{Dns.GetHostName()}";
- string os = RuntimeInformation.OSDescription;
- //获取当前客户端的服务端口
- string currentUserName = Environment.UserName;
- string MachineName =Environment.MachineName;
- //var s = Environment;
- ServerDevice device = new ServerDevice { name =hostName, userName=currentUserName, os= os,region=region,remote=remote };
- int CpuCoreCount = 0;
- long MenemorySize = 0;
- if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
- {
- CpuCoreCount = Environment.ProcessorCount;
- MenemorySize = MemoryInfo.GetTotalPhysicalMemory();
- //using (PerformanceCounter pc = new PerformanceCounter("Memory", "Available Bytes"))
- //{
- // // 获取可用内存
- // AvailableMemory = (long)pc.NextValue();
- //}
- //using (PerformanceCounter pc = new PerformanceCounter("Memory", "Committed Bytes"))
- //{
- // // 获取可用内存
- // CommittedMemory= (long)pc.NextValue();
- //}
- //MenemorySize=AvailableMemory+CommittedMemory;
- //
- // 获取CPU核心数
- //int processorCount = Environment.ProcessorCount;
- //Console.WriteLine("CPU 核心数: " + processorCount);
- //using (ManagementClass managementClass = new ManagementClass("Win32_Processor"))
- //{
- // using (ManagementObjectCollection managementObjectCollection = managementClass.GetInstances())
- // {
- // foreach (ManagementObject managementObject in managementObjectCollection)
- // {
- // CpuCoreCount += Convert.ToInt32(managementObject.Properties["NumberOfLogicalProcessors"].Value);
- // }
- // }
- //}
- //using (ManagementObjectSearcher searcher1 = new ManagementObjectSearcher("SELECT TotalPhysicalMemory FROM Win32_ComputerSystem"))
- //{
- // foreach (ManagementObject obj in searcher1.Get())
- // {
- // long MenemorySizes1 = Convert.ToInt64(obj["TotalPhysicalMemory"]);
- // break; // 只需要第一个结果
- // }
- //}
- //using (ManagementClass mc = new ManagementClass("Win32_ComputerSystem"))
- //{
- // using (ManagementObjectCollection moc = mc.GetInstances())
- // {
- // foreach (ManagementObject mo in moc)
- // {
- // if (mo["TotalPhysicalMemory"]!= null)
- // {
- // MenemorySize = Convert.ToInt64(mo["TotalPhysicalMemory"]);
- // }
- // }
- // }
- //}
- if (Environment.Is64BitOperatingSystem)
- {
- device.bit="64";
- }
- else
- {
- device.bit="32";
- }
- //ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT Name, MaxClockSpeed FROM Win32_Processor");
- //foreach (ManagementObject mo in searcher.Get())
- //{
- // string? cpuName = mo["Name"].ToString();
- // string? clockSpeed = mo["MaxClockSpeed"].ToString();
- // //Console.WriteLine($"CPU 名称: {cpuName}");
- // //Console.WriteLine($"CPU 主频: {clockSpeed} MHz");
- // device.cpuInfos.Add(new CPUInfo { name = cpuName, hz = clockSpeed });
- //}
- string cpuName = MemoryInfo.GetCpuName();
- var clockSpeed = MemoryInfo.GetCpuSpeed();
- device.cpuInfos.Add(new CPUInfo { name = cpuName, hz = $"{clockSpeed}" });
- }
- else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
- {
- //int processorCount = Environment.ProcessorCount;
- // Console.WriteLine("CPU 核心数: " + processorCount);
- try {
- string cpuInfo = File.ReadAllText("/proc/cpuinfo");
- string[] cpu_lines = cpuInfo.Split('\n');
-
- CpuCoreCount= cpu_lines.Count(line => line.StartsWith("processor", StringComparison.OrdinalIgnoreCase));
- string? cpuNameLine = cpuInfo.Split('\n').FirstOrDefault(line => line.StartsWith("model name"));
- string? clockSpeedLine = cpuInfo.Split('\n').FirstOrDefault(line => line.StartsWith("cpu MHz"));
- string cpuName = string.Empty;
- string clockSpeed = string.Empty;
- if (cpuNameLine!= null)
- {
- cpuName = cpuNameLine.Split(':').Last().Trim();
- }
- if (clockSpeedLine!= null)
- {
- clockSpeed = clockSpeedLine.Split(':').Last().Trim();
- }
- device.cpuInfos.Add(new CPUInfo { name = cpuName, hz = clockSpeed });
- } catch (Exception ex)
- {
-
- }
- string[] mem_lines = File.ReadAllLines("/proc/meminfo");
- var match = mem_lines.FirstOrDefault(line => line.StartsWith("MemTotal:"));
- if (match != null)
- {
- var matchResult = Regex.Match(match, @"\d+");
- if (matchResult.Success)
- {
- MenemorySize= long.Parse(matchResult.Value);
- }
- }
- }
- else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
- {
- try {
- using (var process = new Process())
- {
- process.StartInfo.FileName = "/usr/sbin/sysctl";
- process.StartInfo.Arguments = "-n hw.ncpu";
- process.StartInfo.RedirectStandardOutput = true;
- process.StartInfo.UseShellExecute = false;
- process.Start();
- string output = process.StandardOutput.ReadToEnd().Trim();
- int coreCount;
- if (int.TryParse(output, out coreCount))
- {
- CpuCoreCount= coreCount;
- }
- }
- }
- catch (Exception ex) { }
- try
- {
- using (var process = new Process())
- {
- process.StartInfo.FileName = "/usr/sbin/sysctl";
- process.StartInfo.Arguments = "-n hw.memsize";
- process.StartInfo.RedirectStandardOutput = true;
- process.StartInfo.UseShellExecute = false;
- process.Start();
- string output = process.StandardOutput.ReadToEnd().Trim();
- long memorySize;
- if (long.TryParse(output, out memorySize))
- {
- MenemorySize= memorySize;
- }
- }
- }
- catch (Exception ex) { }
- try
- {
- using (var process = new Process())
- {
- process.StartInfo.FileName = "/usr/sbin/sysctl";
- process.StartInfo.Arguments = "-n machdep.cpu.brand_string";
- process.StartInfo.RedirectStandardOutput = true;
- process.StartInfo.UseShellExecute = false;
- process.Start();
- string cpuName = process.StandardOutput.ReadToEnd().Trim();
- process.StartInfo.FileName = "/usr/sbin/sysctl";
- process.StartInfo.Arguments = "-n hw.cpu.frequency";
- process.StartInfo.RedirectStandardOutput = true;
- process.StartInfo.UseShellExecute = false;
- process.Start();
- string clockSpeed = process.StandardOutput.ReadToEnd().Trim();
- //Console.WriteLine($"CPU 名称: {cpuName}");
- //Console.WriteLine($"CPU 主频: {clockSpeed} Hz");
- device.cpuInfos.Add(new CPUInfo { name = cpuName, hz = clockSpeed });
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine($"出现错误: {ex.Message}");
- }
- if (Environment.Is64BitOperatingSystem)
- {
- device.bit="64";
- }
- else
- {
- device.bit="32";
- }
- }
- if (RuntimeInformation.ProcessArchitecture == Architecture.Arm64)
- {
- device.arch="ARM64";
- }
- else if (RuntimeInformation.ProcessArchitecture == Architecture.Arm)
- {
- device.arch="ARM32";
- }
- else if (RuntimeInformation.ProcessArchitecture == Architecture.X64)
- {
- device.arch="X64";
- }
- else if (RuntimeInformation.ProcessArchitecture == Architecture.X86)
- {
- device.arch="X86";
- }
- else
- {
- device.arch=$"未知({device.arch})";
- }
- //Console.WriteLine("CPU 核心数: " + CpuCoreCount+",RAM 大小:"+MenemorySize);
- device.cpu=CpuCoreCount;
- device.ram=MenemorySize;
- var nics = NetworkInterface.GetAllNetworkInterfaces();
- foreach (var nic in nics)
- {
- int physical = 0;
- if (nic.OperationalStatus == OperationalStatus.Up)
- {
- //该方法并不能判断是否是物理网卡
- //if (nic.NetworkInterfaceType== NetworkInterfaceType.Ethernet//- 以太网接口,通常用于有线网络连接。
- // ||nic.NetworkInterfaceType== NetworkInterfaceType.TokenRing//- 令牌环网接口,一种较早的局域网技术。
- // ||nic.NetworkInterfaceType== NetworkInterfaceType.Fddi//- 光纤分布式数据接口,用于光纤网络。
- // ||nic.NetworkInterfaceType== NetworkInterfaceType.Ethernet3Megabit//- 3兆比特以太网接口,一种较早的以太网标准。
- // ||nic.NetworkInterfaceType== NetworkInterfaceType.FastEthernetT// 快速以太网接口(100Base-T),用于双绞线网络。
- // ||nic.NetworkInterfaceType== NetworkInterfaceType.FastEthernetFx//快速以太网接口(100Base-FX),用于光纤网络。
- // ||nic.NetworkInterfaceType== NetworkInterfaceType.Wireless80211//无线局域网接口(Wi-Fi)。
- // ||nic.NetworkInterfaceType== NetworkInterfaceType.GigabitEthernet //千兆以太网接口,提供1 Gbps的数据速率。
- // ||nic.NetworkInterfaceType== NetworkInterfaceType.Wman// 移动宽带接口,用于WiMax设备。
- // ||nic.NetworkInterfaceType== NetworkInterfaceType.Wwanpp//移动宽带接口,用于GSM设备。
- // ||nic.NetworkInterfaceType== NetworkInterfaceType.Wwanpp2//移动宽带接口,用于CDMA设备。
- // )
- //{
- // physical=1;
- //}
- if (IsPhysicalNetworkInterface(nic))
- {
- physical = 1;
- }
- var name = $"{nic.Name}-{nic.Description}";
- var mac = nic.GetPhysicalAddress().ToString();
- var properties = nic.GetIPProperties();
- var unicastAddresses = properties.UnicastAddresses;
- foreach (var unicast in unicastAddresses)
- {
- if (unicast.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
- {
- var ip = unicast.Address.ToString();
- // 绑定域名 优先绑定
- Network network = new Network() { mac=mac, ip=ip, name= name, physical=physical };
- if (!string.IsNullOrWhiteSpace(mac.ToString()) && !mac.Equals("000000000000"))
- {
- device.networks.Add(network);
- }
- }
- }
- }
- }
- //if (_url!.IsNotEmpty())
- //{
- // List<UriInfo> ports = new List<UriInfo>();
- // foreach (var url in _url!)
- // {
- // Uri uri = new Uri(url);
- // device.uris.Add(new UriInfo { port= uri.Port, protocol= uri.Scheme });
- // }
- //}
- //else
- //{
- // throw new Exception("未获取到端口信息!");
- //}
- var networks = device.networks.ToList();
- if (device.networks.IsNotEmpty())
- {
- var order= device.networks.OrderByDescending(x => x.physical).ToList();
- for (int i=0; i<order.Count();i++)
- {
- order[i].domain="exam.habook.local";
- }
- //优先以物理网卡来生成hash,如果没有则以所有网卡生成hash
- var physical = order.FindAll(x => x.physical==1);
- if (physical.IsNotEmpty())
- {
- networks=physical;
- }
- }
- StringBuilder sb= new StringBuilder();
- sb.Append(device.name);//设备名称
- sb.Append(device.os);//系统名称
- sb.Append(device.bit);//系统位
- sb.Append(device.arch);//指令架构
- sb.Append(device.cpu);//cpu核心树
- sb.Append(string.Join(",", device.cpuInfos.Select(x=>$"{x.name}{x.hz}")));//cpu名称和频率
- sb.Append(device.ram);//内存
- sb.Append(string.Join(",", networks.Select(x => $"{x.mac}-{x.name}")));//网卡地址和名称
- //暂不使用远程ip和局域网内ip作为hash,可能发生变化
- string hashData = ShaHashHelper.GetSHA256(sb.ToString());
- device.deviceId=hashData;
- device.id= hashData;
- return device;
- }
- public static bool IsPhysicalNetworkInterface(NetworkInterface nic)
- {
- // 排除虚拟网卡的关键词
- string[] virtualKeywords = { "virtual", "hyper-v", "virtualbox", "vmware", "veth","virbr", "tunnel", "docker", "loopback", "vpn","ppp", "slip", "bridge",
- "vnic", "vif", "tap", "vlan", "vswitch", "vxlan", "gre", "ipsec", "vrf", "vport", "vnet", "vmac", "vxnet"};
- // 检查网卡描述或名称中是否包含虚拟关键词
- string description = nic.Description.ToLower();
- string name = nic.Name.ToLower();
- foreach (var keyword in virtualKeywords)
- {
- if (description.Contains(keyword,StringComparison.OrdinalIgnoreCase) || name.Contains(keyword, StringComparison.OrdinalIgnoreCase))
- {
- return false; // 是虚拟网卡
- }
- }
- // 排除一些常见的虚拟网卡类型
- if (nic.NetworkInterfaceType == NetworkInterfaceType.Loopback ||
- nic.NetworkInterfaceType == NetworkInterfaceType.Tunnel ||
- nic.NetworkInterfaceType == NetworkInterfaceType.Ppp ||
- nic.NetworkInterfaceType == NetworkInterfaceType.Slip||
- nic.NetworkInterfaceType == NetworkInterfaceType.Unknown
- )
- {
- return false; // 是虚拟网卡
- }
- // 默认认为是物理网卡
- return true;
- }
- /// <summary>
- /// 直接获取设备。
- /// </summary>
- /// <param name="httpContext"></param>
- /// <param name="IP"></param>
- /// <param name="device_timeSpan"></param>
- /// <param name="_azureRedis"></param>
- /// <returns></returns>
- public static string GetDevice(HttpContext httpContext, IMemoryCache _memoryCache)
- {
- string IP= GetIP(httpContext);
- var cookie = httpContext.Request.Cookies;
- string device =string.Empty;
- if (cookie != null)
- {
- ///设备是否存在
- foreach (var ck in cookie)
- {
- if (ck.Key.Equals("device"))
- {
- //redis如果存在则
- var fingerprint = ck.Value.Split("-");
- if (fingerprint.Length == 2 && IP.Equals(fingerprint[1]))
- {
- if (!_memoryCache.TryGetValue<string>($"device:{fingerprint[0]}:{IP}", out device))
- {
- _memoryCache.Set($"device:{fingerprint[0]}:{IP}", $"{fingerprint[0]}-{IP}");
- device = $"{fingerprint[0]}-{IP}";
- }
-
- }
- }
- }
- }
- return device;
- }
- public static string GetIP(HttpContext httpContext)
- {
- var IpPort = httpContext.Request.Headers["X-Forwarded-For"].FirstOrDefault();
- if (string.IsNullOrEmpty(IpPort))
- {
- IpPort = $"{httpContext.Connection.RemoteIpAddress}";
- }
- if (IpPort.Contains("::"))
- {
- IpPort = "127.0.0.1";
- }
- return IpPort;
- }
- /// <summary>
- /// 初始化设备
- /// </summary>
- /// <param name="httpContext"></param>
- /// <param name="fingerprint">浏览器指纹</param>
- /// <param name="IP"></param>
- /// <param name="device_timeSpan"></param>
- /// <param name="_azureRedis"></param>
- /// <returns></returns>
- public static string GetDeviceInit(this HttpContext httpContext, string fingerprint, string IP, IMemoryCache _memoryCache)
- {
- string device = $"{fingerprint}-{IP}";
- List<string> cookieString = new List<string>();
- var cookie = httpContext.Request.Cookies;
- int status = 1;
- if (cookie != null)
- {
- ///设备是否存在
- foreach (var ck in cookie)
- {
- if (ck.Key.Equals("device"))
- {
- if (device.Contains("-") && device.Contains("."))
- {
- //如果匹配的是fingerprint-IP 则是已经存在的。
- if (ck.Value.Equals(device))
- {
- //redis如果存在则
- _memoryCache.TryGetValue<string>($"device:{fingerprint}:{IP}", out string device_exist);
- // 返回的则应该是ck.Value=exist_device_exist的数据
- if (device_exist!=null)
- {
- if (!string.IsNullOrWhiteSpace($"{device_exist}"))
- {
- //0是代表指纹和IP匹配,正常返回的
- status = 1;
- }
- else
- {
- //需要新建 fingerprint-IP
- status = 1;
- }
- }
- else
- {
- status = 1;
- }
- }
- else
- {
- string ck_ip = ck.Value.Split("-")[1];
- if (ck_ip.Equals(IP))
- {
- //传入的指纹和cookie的不一致,仍然以cookie的为准。
- status = 1;
- fingerprint = ck.Value.Split("-").First();
- device = ck.Value;
- }
- }
- }
- else
- {
- //如果匹配的是fingerprint则是一个新的设备。
- if (ck.Value.Equals(fingerprint))
- {
- //检查设备是否被占用
- //var device_exist = _azureRedis.GetRedisClient(8).HashExists($"device:{fingerprint}", IP);
- _memoryCache.TryGetValue<JsonNode>($"device:{fingerprint}:{IP}", out JsonNode device_exist);
- if (device_exist!=null)
- {
- //需要新建 sha1(fingerprint+uuid)-IP
- status = 2;
- }
- else
- {
- //0是代表指纹和IP匹配,正常返回的
- status = 1;
- }
- }
- else
- {
- //匹配的都不是,新设备。
- status = 1;
- }
- }
- }
- else
- {
- cookieString.Add($"{ck.Key}{ck.Value}");
- }
- }
- }
- /*
- httpContext.Request.Headers.TryGetValue("accept-language", out var accept_language);
- httpContext.Request.Headers.TryGetValue("sec-ch-ua", out var chua);
- httpContext.Request.Headers.TryGetValue("sec-ch-ua-platform", out var platform);
- httpContext.Request.Headers.TryGetValue("user-agent", out var useragent);
- httpContext.Request.Headers.TryGetValue("accept", out var accept);
- httpContext.Request.Headers.TryGetValue("accept-encoding", out var accept_encoding);
- device = ShaHashHelper.GetSHA1($"{IP}{accept_language}{chua}{platform}{useragent}{accept}{accept_encoding}{string.Join("", cookieString)}");
- */
- if (status == 2)
- {
- fingerprint = ShaHashHelper.GetSHA1(fingerprint + Guid.NewGuid().ToString());
- device = $"{fingerprint}-{IP}";
- }
- //else if (status == 1)
- //{
- // device = $"{fingerprint}-{IP}";
- //}
- //await _azureRedis.GetRedisClient(8).HashSetAsync($"device:{fingerprint}", IP, new { device }.ToJsonString());
- //await _azureRedis.GetRedisClient(8).KeyExpireAsync($"device:{fingerprint}", device_timeSpan);
- _memoryCache.Set($"device:{fingerprint}:{IP}", device);
- httpContext.Response.Cookies.Append("device", device, new CookieOptions { HttpOnly = true, MaxAge = new TimeSpan(24 * 7, 0, 0) });
- return device;
- }
- }
- public class UriInfo
- {
- public string? protocol { get; set; }
- public int port { get; set; }
- }
-
- }
|