|
@@ -11,19 +11,30 @@ using System.Text;
|
|
using IES.ExamServer.DI;
|
|
using IES.ExamServer.DI;
|
|
using IES.ExamServer.Helper;
|
|
using IES.ExamServer.Helper;
|
|
using System;
|
|
using System;
|
|
|
|
+using System.Security.Principal;
|
|
|
|
|
|
namespace IES.ExamServer.Services
|
|
namespace IES.ExamServer.Services
|
|
{
|
|
{
|
|
public static class IndexService
|
|
public static class IndexService
|
|
{
|
|
{
|
|
- public static async Task<(int code, string msg)> ModifyHosts(string ip,IMemoryCache _memoryCache,LiteDBFactory _liteDBFactory)
|
|
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 修改IP域名映射,以及处理证书是否安装的问题。
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="ip"></param>
|
|
|
|
+ /// <param name="_memoryCache"></param>
|
|
|
|
+ /// <param name="_liteDBFactory"></param>
|
|
|
|
+ /// <param name="connectionService"></param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ public static async Task<(int code, int code_cer ,int code_hosts,int code_zip, string msg)> ModifyHosts(string? ip,IMemoryCache _memoryCache,LiteDBFactory _liteDBFactory,CenterServiceConnectionService connectionService)
|
|
{
|
|
{
|
|
- int code = 0;
|
|
|
|
- string msg = string.Empty;
|
|
|
|
|
|
+ (string? hostsIp,string hostsMsg) = SystemScriptHelper.FindIpAddressForDomain("exam.habook.local");
|
|
|
|
+ int code = 0, code_cer = 0,code_hosts=0,code_zip=0 ;
|
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
try
|
|
try
|
|
{
|
|
{
|
|
string batscriptPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "batscript");
|
|
string batscriptPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "batscript");
|
|
- if (!Directory.Exists(batscriptPath))
|
|
|
|
|
|
+ if (!Directory.Exists(batscriptPath))
|
|
{
|
|
{
|
|
Directory.CreateDirectory(batscriptPath);
|
|
Directory.CreateDirectory(batscriptPath);
|
|
}
|
|
}
|
|
@@ -35,18 +46,36 @@ namespace IES.ExamServer.Services
|
|
System.IO.File.Copy(pathCer, pathCerNew);
|
|
System.IO.File.Copy(pathCer, pathCerNew);
|
|
string pathBat = Path.Combine(Directory.GetCurrentDirectory(), "Configs", "cer", "install_certificate.bat");
|
|
string pathBat = Path.Combine(Directory.GetCurrentDirectory(), "Configs", "cer", "install_certificate.bat");
|
|
System.IO.File.Copy(pathBat, pathBatNew);
|
|
System.IO.File.Copy(pathBat, pathBatNew);
|
|
- var res = ProcessHelper.ExecuteProcess(pathBatNew);
|
|
|
|
|
|
+ }
|
|
|
|
+ var needInstall = SystemScriptHelper.CheckCertificate(pathCerNew);
|
|
|
|
+ if (!needInstall)
|
|
|
|
+ {
|
|
|
|
+ if (SystemScriptHelper.IsAdministrator())
|
|
|
|
+ {
|
|
|
|
+ var res = ProcessHelper.ExecuteProcess(pathBatNew);
|
|
|
|
+ sb.Append(res.msg);
|
|
|
|
+ code_cer = res.code;
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ code_cer = 401;
|
|
|
|
+ sb.Append("请使用管理员身份运行本程序,如果已经安装过脚本请忽略!");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ code_cer = 200;
|
|
}
|
|
}
|
|
//获取主站配置信息。
|
|
//获取主站配置信息。
|
|
ServerDevice serverDevice = _memoryCache.Get<ServerDevice>(Constant._KeyServerDevice);
|
|
ServerDevice serverDevice = _memoryCache.Get<ServerDevice>(Constant._KeyServerDevice);
|
|
var primaryNetworks= _liteDBFactory.GetLiteDatabase().GetCollection<Network>().FindAll().ToList();
|
|
var primaryNetworks= _liteDBFactory.GetLiteDatabase().GetCollection<Network>().FindAll().ToList();
|
|
Network? primaryNetwork = null;
|
|
Network? primaryNetwork = null;
|
|
- //传入的ip为不为空
|
|
|
|
|
|
+ //传入的ip为不为空,切换
|
|
if (!string.IsNullOrWhiteSpace(ip) )
|
|
if (!string.IsNullOrWhiteSpace(ip) )
|
|
{
|
|
{
|
|
if (serverDevice != null && serverDevice.networks.IsNotEmpty())
|
|
if (serverDevice != null && serverDevice.networks.IsNotEmpty())
|
|
{
|
|
{
|
|
-
|
|
|
|
|
|
+ primaryNetwork = serverDevice.networks.FindAll(x => ip.Equals(x.ip))?.FirstOrDefault();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
else
|
|
@@ -65,14 +94,6 @@ namespace IES.ExamServer.Services
|
|
{
|
|
{
|
|
primaryNetwork = network;
|
|
primaryNetwork = network;
|
|
}
|
|
}
|
|
- else
|
|
|
|
- {
|
|
|
|
- _liteDBFactory.GetLiteDatabase().GetCollection<Network>().Delete(network.id);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- _liteDBFactory.GetLiteDatabase().GetCollection<Network>().Delete(network.id);
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -82,79 +103,85 @@ namespace IES.ExamServer.Services
|
|
primaryNetwork = serverDevice.networks.FirstOrDefault();//第一个是物理网卡
|
|
primaryNetwork = serverDevice.networks.FirstOrDefault();//第一个是物理网卡
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
}
|
|
}
|
|
-
|
|
|
|
-
|
|
|
|
- if (serverDevice != null && serverDevice.networks.IsNotEmpty())
|
|
|
|
|
|
+ if (primaryNetwork != null)
|
|
{
|
|
{
|
|
- Network? network = serverDevice.networks.FirstOrDefault();
|
|
|
|
- if (!string.IsNullOrWhiteSpace(ip))
|
|
|
|
|
|
+ string pathBatHosts = Path.Combine(Directory.GetCurrentDirectory(), "Configs", "cer", "modify_hosts.bat");
|
|
|
|
+ string text = await System.IO.File.ReadAllTextAsync(pathBatHosts);
|
|
|
|
+ // 使用正则表达式替换 IP 地址
|
|
|
|
+ string pattern = @"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b";
|
|
|
|
+ string result = Regex.Replace(text, pattern, primaryNetwork.ip!);
|
|
|
|
+ string pathBatHostsNew = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "batscript", "modify_hosts.bat");
|
|
|
|
+ await System.IO.File.WriteAllTextAsync(pathBatHostsNew, result);
|
|
|
|
+ string pathBatStudent = Path.Combine(Directory.GetCurrentDirectory(), "Configs", "cer", "student_manual.bat");
|
|
|
|
+ string textStudent = await System.IO.File.ReadAllTextAsync(pathBatStudent);
|
|
|
|
+ // 使用正则表达式替换 IP 地址
|
|
|
|
+ string resultStudent = Regex.Replace(textStudent, pattern, primaryNetwork.ip!);
|
|
|
|
+ string pathBatStudentNew = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "batscript", "student_manual.bat");
|
|
|
|
+ await System.IO.File.WriteAllTextAsync(pathBatStudentNew, resultStudent);
|
|
|
|
+ if (string.IsNullOrWhiteSpace(hostsIp) || !hostsIp.Equals(primaryNetwork.ip))
|
|
{
|
|
{
|
|
- network = serverDevice.networks.FindAll(x => ip.Equals(x.ip))?.FirstOrDefault();
|
|
|
|
|
|
+ if (SystemScriptHelper.IsAdministrator())
|
|
|
|
+ {
|
|
|
|
+ var resHosts = ProcessHelper.ExecuteProcess(pathBatHostsNew);
|
|
|
|
+ sb.Append(resHosts.msg);
|
|
|
|
+ code_hosts = resHosts.code;
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ code_hosts = 401;
|
|
|
|
+ sb.Append("请使用管理员身份执行本程序!");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ code_hosts = 200;
|
|
|
|
+ sb.Append("IP域名映射已存在,无需再次映射!");
|
|
}
|
|
}
|
|
|
|
|
|
- if (network != null && !string.IsNullOrWhiteSpace(network.ip))
|
|
|
|
|
|
+ string batscriptZipPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot","student_script.zip");
|
|
|
|
+
|
|
|
|
+ var res = ZipHelper.CreateZip(batscriptPath, batscriptZipPath);
|
|
|
|
+ if (res.res)
|
|
{
|
|
{
|
|
-
|
|
|
|
- string pathBatHosts = Path.Combine(Directory.GetCurrentDirectory(), "Configs", "cer", "modify_hosts.bat");
|
|
|
|
- string text = await System.IO.File.ReadAllTextAsync(pathBatHosts);
|
|
|
|
- // 使用正则表达式替换 IP 地址
|
|
|
|
- string pattern = @"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b";
|
|
|
|
- string result = Regex.Replace(text, pattern, network.ip);
|
|
|
|
- string pathBatHostsNew = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "batscript", "modify_hosts.bat");
|
|
|
|
- await System.IO.File.WriteAllTextAsync(pathBatHostsNew, result);
|
|
|
|
- string pathBatStudent = Path.Combine(Directory.GetCurrentDirectory(), "Configs", "cer", "student_manual.bat");
|
|
|
|
- string textStudent = await System.IO.File.ReadAllTextAsync(pathBatStudent);
|
|
|
|
- // 使用正则表达式替换 IP 地址
|
|
|
|
- string resultStudent = Regex.Replace(textStudent, pattern, network.ip);
|
|
|
|
- string pathBatStudentNew = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "batscript", "student_manual.bat");
|
|
|
|
- await System.IO.File.WriteAllTextAsync(pathBatStudentNew, resultStudent);
|
|
|
|
- var resHosts = ProcessHelper.ExecuteProcess(pathBatHostsNew);
|
|
|
|
- string batscriptZipPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "student_script.zip");
|
|
|
|
-
|
|
|
|
- var res= ZipHelper.CreateZip(batscriptPath, batscriptZipPath);
|
|
|
|
- if (res.res)
|
|
|
|
- {
|
|
|
|
- serverDevice.networks.ForEach(x => {
|
|
|
|
- x.primary = 0;
|
|
|
|
- x.batscriptZip = null;
|
|
|
|
- });
|
|
|
|
- network.primary = 1;
|
|
|
|
- network.batscriptZip = batscriptZipPath;
|
|
|
|
- _memoryCache.Set<ServerDevice>(Constant._KeyServerDevice, serverDevice);
|
|
|
|
- // _liteDBFactory.GetLiteDatabase().GetCollection<ServerDevice>().Upsert(serverDevice);
|
|
|
|
- }
|
|
|
|
- //return Ok(new
|
|
|
|
- //{
|
|
|
|
- // code = 200,
|
|
|
|
- // msg = "成功",
|
|
|
|
- // serverDevice,
|
|
|
|
- // cer = "certificate.cer",
|
|
|
|
- // install_certificate = "install_certificate.bat",
|
|
|
|
- // modify_hosts = "modify_hosts.bat",
|
|
|
|
- // student_manual = "student_manual.bat"
|
|
|
|
- //});
|
|
|
|
|
|
+ code_zip = 200;
|
|
|
|
+ sb.Append(res.msg);
|
|
|
|
+
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
- code = 400;
|
|
|
|
- msg = "未找到匹配的IP。";
|
|
|
|
|
|
+ code_zip = 400;
|
|
|
|
+ sb.Append("脚本文件创建异常!");
|
|
}
|
|
}
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- code = 400;
|
|
|
|
- msg = "服务端设备未找到,或网卡设备不存在。";
|
|
|
|
|
|
+ serverDevice!.networks.ForEach(x =>
|
|
|
|
+ {
|
|
|
|
+ x.primary = 0;
|
|
|
|
+ x.batscriptZip = null;
|
|
|
|
+ if (x.id!.Equals(primaryNetwork.id))
|
|
|
|
+ {
|
|
|
|
+ x.primary = code_hosts ==200? 1:0;
|
|
|
|
+ x.batscriptZip = res.res? "student_script.zip":null;
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ //更新设备的主站设备信息
|
|
|
|
+ connectionService.serverDevice = serverDevice;
|
|
|
|
+ _memoryCache.Set<ServerDevice>(Constant._KeyServerDevice, serverDevice);
|
|
|
|
+ _liteDBFactory.GetLiteDatabase().GetCollection<ServerDevice>().Upsert(serverDevice);
|
|
|
|
+ //清理后再保存,保证只有一条主站数据。
|
|
|
|
+ _liteDBFactory.GetLiteDatabase().GetCollection<Network>().DeleteAll();
|
|
|
|
+ _liteDBFactory.GetLiteDatabase().GetCollection<Network>().Upsert(primaryNetwork);
|
|
|
|
+ //所有执行成功
|
|
|
|
+ code = 200;
|
|
|
|
+ sb.Append("证书安装成功,域名IP绑定成功,脚本文件创建成功!");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
catch (Exception ex)
|
|
{
|
|
{
|
|
code = 500;
|
|
code = 500;
|
|
//_logger.LogError($"域名IP绑定错误。{ex.Message},{ex.StackTrace}");
|
|
//_logger.LogError($"域名IP绑定错误。{ex.Message},{ex.StackTrace}");
|
|
- msg = $"域名IP绑定错误,{ex.Message}";
|
|
|
|
|
|
+ return (500,code_cer,code_hosts,code_zip, $"域名IP绑定错误,{ex.Message},{ex.StackTrace}");
|
|
}
|
|
}
|
|
- return (code, msg);
|
|
|
|
|
|
+ return (code, code_cer, code_hosts, code_zip, sb.ToString());
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -460,10 +487,10 @@ namespace IES.ExamServer.Services
|
|
if (device.networks.IsNotEmpty())
|
|
if (device.networks.IsNotEmpty())
|
|
{
|
|
{
|
|
var order= device.networks.OrderByDescending(x => x.physical).ToList();
|
|
var order= device.networks.OrderByDescending(x => x.physical).ToList();
|
|
- for (int i=0; i<order.Count();i++)
|
|
|
|
- {
|
|
|
|
- order[i].domain="exam.habook.local";
|
|
|
|
- }
|
|
|
|
|
|
+ //for (int i=0; i<order.Count();i++)
|
|
|
|
+ //{
|
|
|
|
+ // order[i].domain="exam.habook.local";
|
|
|
|
+ //}
|
|
device.networks=order;
|
|
device.networks=order;
|
|
//优先以物理网卡来生成hash,如果没有则以所有网卡生成hash
|
|
//优先以物理网卡来生成hash,如果没有则以所有网卡生成hash
|
|
var physical = order.FindAll(x => x.physical==1);
|
|
var physical = order.FindAll(x => x.physical==1);
|