|
@@ -8,6 +8,8 @@ using System.Text.Json;
|
|
|
using System.Text.Unicode;
|
|
|
using IES.ExamServer.Helper;
|
|
|
using IES.ExamServer.Models;
|
|
|
+using System.Security.Policy;
|
|
|
+using IES.ExamServer.Helpers;
|
|
|
|
|
|
namespace IES.ExamServer.DI
|
|
|
{
|
|
@@ -17,7 +19,7 @@ namespace IES.ExamServer.DI
|
|
|
private readonly IHttpClientFactory _clientFactory;
|
|
|
private readonly LiteDBFactory _liteDBFactory;
|
|
|
private readonly IConfiguration _configuration;
|
|
|
- private readonly DataCenterConnectionService _connectionService;
|
|
|
+ private readonly CenterServiceConnectionService _connectionService;
|
|
|
private readonly IHostApplicationLifetime _lifetime;
|
|
|
private readonly IServer _server;
|
|
|
private readonly ILogger<ServiceInitializer> _logger;
|
|
@@ -26,7 +28,7 @@ namespace IES.ExamServer.DI
|
|
|
IHttpClientFactory clientFactory,
|
|
|
LiteDBFactory liteDBFactory,
|
|
|
IConfiguration configuration,
|
|
|
- DataCenterConnectionService connectionService,
|
|
|
+ CenterServiceConnectionService connectionService,
|
|
|
IHostApplicationLifetime lifetime,
|
|
|
IServer server,
|
|
|
ILogger<ServiceInitializer> logger)
|
|
@@ -44,13 +46,13 @@ namespace IES.ExamServer.DI
|
|
|
public async Task InitializeAsync()
|
|
|
{
|
|
|
JsonNode? data = null;
|
|
|
- int hybrid = 0;
|
|
|
+ int hybrid = 0, notify=0;
|
|
|
string remote = "127.0.0.1";
|
|
|
string region = "局域网·内网";
|
|
|
-
|
|
|
+ string? centerUrl = _configuration.GetValue<string>("ExamServer:CenterUrl");
|
|
|
try
|
|
|
{
|
|
|
- string? centerUrl = _configuration.GetValue<string>("ExamServer:CenterUrl");
|
|
|
+
|
|
|
var httpClient = _clientFactory.CreateClient();
|
|
|
httpClient.Timeout = TimeSpan.FromSeconds(10);
|
|
|
HttpResponseMessage message = await httpClient.PostAsJsonAsync($"{centerUrl}/core/system-info", new { });
|
|
@@ -71,9 +73,26 @@ namespace IES.ExamServer.DI
|
|
|
// 云端服务连接失败
|
|
|
hybrid = 0;
|
|
|
}
|
|
|
+ string? notifyUrl = _configuration.GetValue<string>("ExamServer:NotifyUrl");
|
|
|
+ try
|
|
|
+ {
|
|
|
+
|
|
|
+ var httpClient = _clientFactory.CreateClient();
|
|
|
+ httpClient.Timeout = TimeSpan.FromSeconds(10);
|
|
|
+ HttpResponseMessage message = await httpClient.PostAsJsonAsync($"{notifyUrl}/index/device-init", new { fp= Guid.NewGuid().ToString() });
|
|
|
+ if (message.IsSuccessStatusCode)
|
|
|
+ {
|
|
|
+ notify = 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ // 云端服务连接失败
|
|
|
+ notify = 0;
|
|
|
+ }
|
|
|
if (hybrid==1)
|
|
|
{
|
|
|
- string? centerUrl = _configuration.GetValue<string>("ExamServer:CenterUrl");
|
|
|
+
|
|
|
var httpClient = _clientFactory.CreateClient();
|
|
|
httpClient.Timeout = TimeSpan.FromSeconds(10);
|
|
|
HttpResponseMessage message = await httpClient.GetAsync("https://teammodelos.blob.core.chinacloudapi.cn/0-public/schools.json");
|
|
@@ -93,54 +112,34 @@ namespace IES.ExamServer.DI
|
|
|
throw new Exception($"Failed to download data. Status code: {message.StatusCode}");
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+ _connectionService.notifyUrl = notify == 1 ? notifyUrl : null;
|
|
|
+ _connectionService.notifyIsConnected = notify == 1;
|
|
|
// 单例模式存储云端数据中心连接状态
|
|
|
- _connectionService.centerUrl = hybrid == 1 ? $"{data?["centerUrl"]}" : null;
|
|
|
- _connectionService.dataCenterIsConnected = hybrid == 1;
|
|
|
-
|
|
|
+ _connectionService.centerUrl = hybrid == 1 ?centerUrl : null;
|
|
|
+ _connectionService.centerIsConnected = hybrid == 1;
|
|
|
+ ServerDevice serverDevice = IndexService.GetServerDevice(remote, region);
|
|
|
+ IEnumerable<School> schools = _liteDBFactory.GetLiteDatabase().GetCollection<School>().FindAll();
|
|
|
+ School? school = schools?.FirstOrDefault();
|
|
|
+ serverDevice.school = school;
|
|
|
+ _cache.Set(Constant._KeyServerDevice, serverDevice);
|
|
|
_lifetime.ApplicationStarted.Register(() =>
|
|
|
{
|
|
|
- var addresses = _server.Features.Get<IServerAddressesFeature>()?.Addresses;
|
|
|
- ServerDevice serverDevice = IndexService.GetServerDevice(remote, region, addresses);
|
|
|
- IEnumerable<School> schools = _liteDBFactory.GetLiteDatabase().GetCollection<School>().FindAll();
|
|
|
- School? school = schools?.FirstOrDefault();
|
|
|
- serverDevice.school = school;
|
|
|
- //int domainStatus =0;
|
|
|
- //string domain = builder.Configuration.GetValue<string>("ExamClient:Domain");
|
|
|
- //foreach (var network in serverDevice.networks)
|
|
|
- //{
|
|
|
- // try
|
|
|
- // {
|
|
|
- // string domain_entry = $"{network.ip} {domain}";
|
|
|
- // string hostsFilePath = @"C:\Windows\System32\drivers\etc\hosts";
|
|
|
- // string content = File.ReadAllText(hostsFilePath, Encoding.UTF8);
|
|
|
- // if (!content.Contains(domain_entry))
|
|
|
- // {
|
|
|
- // content += Environment.NewLine + domain_entry;
|
|
|
- // // 使用管理员权限运行此程序,不然会抛出UnauthorizedAccessException
|
|
|
- // File.WriteAllText(hostsFilePath, content, Encoding.UTF8);
|
|
|
- // domainStatus=1;
|
|
|
- // // Console.WriteLine("Hosts file updated successfully.");
|
|
|
- // }
|
|
|
- // else
|
|
|
- // {
|
|
|
- // domainStatus=1;
|
|
|
- // //Console.WriteLine("The entry already exists in the hosts file.");
|
|
|
- // }
|
|
|
- // }
|
|
|
- // catch (UnauthorizedAccessException)
|
|
|
- // {
|
|
|
- // domainStatus=2;
|
|
|
- // // Console.WriteLine("You need to run this program with administrative privileges to modify the hosts file.");
|
|
|
- // }
|
|
|
- // catch (Exception ex)
|
|
|
- // {
|
|
|
- // domainStatus=0;
|
|
|
- // // Console.WriteLine($"An error occurred: {ex.Message}");
|
|
|
- // }
|
|
|
- //}
|
|
|
- //serverDevice.domainStatus=domainStatus;
|
|
|
- //serverDevice.domain=domain;
|
|
|
+ var serverDevice= _cache.Get<ServerDevice>(Constant._KeyServerDevice);
|
|
|
+ var _url = _server.Features.Get<IServerAddressesFeature>()?.Addresses;
|
|
|
+ if (_url!.IsNotEmpty())
|
|
|
+ {
|
|
|
+ List<UriInfo> ports = new List<UriInfo>();
|
|
|
+ foreach (var url in _url!)
|
|
|
+ {
|
|
|
+ Uri uri = new Uri(url);
|
|
|
+ serverDevice.uris.Add(new UriInfo { port= uri.Port, protocol= uri.Scheme });
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ throw new Exception("未获取到端口信息!");
|
|
|
+ }
|
|
|
_logger.LogInformation($"服务端设备信息:{JsonSerializer.Serialize(serverDevice, options: new JsonSerializerOptions { Encoder = JavaScriptEncoder.Create(UnicodeRanges.All) })}");
|
|
|
_cache.Set(Constant._KeyServerDevice, serverDevice);
|
|
|
});
|