|
@@ -337,48 +337,113 @@ namespace IES.ExamServer.Controllers
|
|
|
{
|
|
|
return Ok(new { code=200});
|
|
|
}
|
|
|
-
|
|
|
+ /// <summary>
|
|
|
+ /// 更新学校
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="ip"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ [HttpGet("update-schools")]
|
|
|
+ public async Task<IActionResult> UpdateSchools()
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ var httpClient = _httpClientFactory.CreateClient();
|
|
|
+ httpClient.Timeout = TimeSpan.FromSeconds(10);
|
|
|
+ HttpResponseMessage message = await httpClient.GetAsync("https://teammodelos.blob.core.chinacloudapi.cn/0-public/schools.json");
|
|
|
+ if (message.IsSuccessStatusCode)
|
|
|
+ {
|
|
|
+ // 读取响应内容
|
|
|
+ string content = await message.Content.ReadAsStringAsync();
|
|
|
+ // 保存文件的路径
|
|
|
+ string filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "schools.json");
|
|
|
+ // 确保目录存在
|
|
|
+ Directory.CreateDirectory(Path.GetDirectoryName(filePath)!);
|
|
|
+ // 将内容写入文件
|
|
|
+ await System.IO.File.WriteAllTextAsync(filePath, content);
|
|
|
+ return Ok(new { code=200,msg="更新成功"});
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return Ok(new { code = (int)message.StatusCode, msg = "更新成功" });
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception ex) {
|
|
|
+ return Ok(new { code =500, msg = ex.Message });
|
|
|
+ }
|
|
|
+ }
|
|
|
/// <summary>
|
|
|
/// 修改Hosts文件的 局域网域名IP映射
|
|
|
/// </summary>
|
|
|
/// <param name="ip"></param>
|
|
|
/// <returns></returns>
|
|
|
[HttpGet("modify-hosts")]
|
|
|
- public async Task<IActionResult> ModifyHosts([FromQuery] string ip )
|
|
|
+ public async Task<IActionResult> ModifyHosts([FromQuery] string? ip=null )
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
+
|
|
|
+ string pathCerNew = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "certificate.cer");
|
|
|
+ string pathBatNew = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "install_certificate.bat");
|
|
|
+ if (!System.IO.File.Exists(pathCerNew)|| !System.IO.File.Exists(pathBatNew))
|
|
|
+ {
|
|
|
+ string pathCer = Path.Combine(Directory.GetCurrentDirectory(), "Configs", "cer", "certificate.cer");
|
|
|
+ System.IO.File.Copy(pathCer, pathCerNew);
|
|
|
+ string pathBat = Path.Combine(Directory.GetCurrentDirectory(), "Configs", "cer", "install_certificate.bat");
|
|
|
+ System.IO.File.Copy(pathBat, pathBatNew);
|
|
|
+ var res = ProcessHelper.ExecuteProcess(pathBatNew);
|
|
|
+ }
|
|
|
+
|
|
|
ServerDevice serverDevice = _memoryCache.Get<ServerDevice>(Constant._KeyServerDevice);
|
|
|
if (serverDevice != null && serverDevice.networks.IsNotEmpty())
|
|
|
{
|
|
|
-
|
|
|
- var network = serverDevice.networks.Find(x => ip.Equals(x.ip));
|
|
|
+ Network? network = serverDevice.networks.FirstOrDefault();
|
|
|
+ if (!string.IsNullOrWhiteSpace(ip))
|
|
|
+ {
|
|
|
+ network = serverDevice.networks.FindAll(x => ip.Equals(x.ip))?.FirstOrDefault();
|
|
|
+ }
|
|
|
+
|
|
|
if (network != null && !string.IsNullOrWhiteSpace(network.ip))
|
|
|
{
|
|
|
- string pathBat = Path.Combine(Directory.GetCurrentDirectory(), "Configs", "cer", "modify_hosts.bat");
|
|
|
- string text = await System.IO.File.ReadAllTextAsync(pathBat);
|
|
|
-
|
|
|
+ network.primary = 1;
|
|
|
+ _memoryCache.Set<ServerDevice>(Constant._KeyServerDevice,serverDevice);
|
|
|
+ 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 pathCertificateBat = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "package", "certificate.bat");
|
|
|
- await System.IO.File.WriteAllTextAsync(pathCertificateBat, result);
|
|
|
-
|
|
|
-
|
|
|
- return Ok(new { code = 200, msg = "执行成功。", bat = "package/certificate.bat" });
|
|
|
+ string pathBatHostsNew = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "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", "student_manual.bat");
|
|
|
+ await System.IO.File.WriteAllTextAsync(pathBatStudentNew, resultStudent);
|
|
|
+ var resHosts = ProcessHelper.ExecuteProcess(pathBatHostsNew);
|
|
|
+ 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"
|
|
|
+ });
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
+ code = 400;
|
|
|
msg = "未找到匹配的IP。";
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
+ code = 400;
|
|
|
msg = "服务端设备未找到,或网卡设备不存在。";
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
+ code = 500;
|
|
|
_logger.LogError($"域名IP绑定错误。{ex.Message},{ex.StackTrace}");
|
|
|
msg = $"域名IP绑定错误,{ex.Message}";
|
|
|
}
|
|
@@ -390,63 +455,15 @@ namespace IES.ExamServer.Controllers
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
+ string pathCerNew = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "batscript", "certificate.cer");
|
|
|
+ string pathBatNew = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "batscript", "install_certificate.bat");
|
|
|
+ string pathCer = Path.Combine(Directory.GetCurrentDirectory(), "Configs", "cer", "certificate.cer");
|
|
|
+ System.IO.File.Copy(pathCer, pathCerNew);
|
|
|
string pathBat = Path.Combine(Directory.GetCurrentDirectory(), "Configs", "cer", "install_certificate.bat");
|
|
|
- // 创建一个新的 ProcessStartInfo 对象
|
|
|
- ProcessStartInfo startInfo = new ProcessStartInfo
|
|
|
- {
|
|
|
- // 指定要执行的 BAT 文件的路径
|
|
|
- FileName = "cmd.exe",
|
|
|
- // 设置命令行参数,使用 /c 表示执行命令后关闭命令提示符窗口
|
|
|
- Arguments = $"/c {pathBat}",
|
|
|
- // 设置是否使用操作系统 shell 启动进程
|
|
|
- UseShellExecute = false,
|
|
|
- // 设置是否创建新的窗口
|
|
|
- CreateNoWindow = true,
|
|
|
- // 重定向标准输出和标准错误输出
|
|
|
- RedirectStandardOutput = true,
|
|
|
- RedirectStandardError = true
|
|
|
- };
|
|
|
- // 创建一个新的 Process 对象
|
|
|
- using (Process process = new Process())
|
|
|
- {
|
|
|
- // 将 ProcessStartInfo 对象分配给 Process 对象
|
|
|
- process.StartInfo = startInfo;
|
|
|
+ System.IO.File.Copy(pathBat, pathBatNew);
|
|
|
+ var res = ProcessHelper.ExecuteProcess(pathBatNew);
|
|
|
|
|
|
- // 启动进程
|
|
|
- process.Start();
|
|
|
-
|
|
|
- // 读取标准输出和标准错误输出
|
|
|
- string output = process.StandardOutput.ReadToEnd();
|
|
|
- string error = process.StandardError.ReadToEnd();
|
|
|
-
|
|
|
- // 等待进程执行完成
|
|
|
- process.WaitForExit();
|
|
|
-
|
|
|
- // 输出执行结果
|
|
|
- if (!string.IsNullOrEmpty(output))
|
|
|
- {
|
|
|
- // Console.WriteLine("标准输出:");
|
|
|
- // Console.WriteLine(output);
|
|
|
- if (output.Contains("successfully", StringComparison.OrdinalIgnoreCase))
|
|
|
- {
|
|
|
- msg = $"证书安装成功!";
|
|
|
- code = 200;
|
|
|
- }
|
|
|
- else {
|
|
|
- msg = $"证书安装异常:{output}";
|
|
|
- code = 1;
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- if (!string.IsNullOrEmpty(error))
|
|
|
- {
|
|
|
- // Console.WriteLine("错误输出:");
|
|
|
- // Console.WriteLine(error);
|
|
|
- msg = $"执行失败:{error}";
|
|
|
- code = 2;
|
|
|
- }
|
|
|
- }
|
|
|
+ return Ok(new {code= res.code, msg= res.msg });
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
@@ -456,8 +473,13 @@ namespace IES.ExamServer.Controllers
|
|
|
}
|
|
|
return Ok(new { code = code, msg = msg });
|
|
|
}
|
|
|
- [HttpPost("generate-certificate")]
|
|
|
- public async Task<IActionResult> GenerateCertificate(JsonNode json)
|
|
|
+ /// <summary>
|
|
|
+ ///
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="json"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ [HttpPost("download-primary")]
|
|
|
+ public async Task<IActionResult> DownloadPrimary(JsonNode json)
|
|
|
{
|
|
|
try {
|
|
|
ServerDevice serverDevice = _memoryCache.Get<ServerDevice>(Constant._KeyServerDevice);
|
|
@@ -496,7 +518,7 @@ namespace IES.ExamServer.Controllers
|
|
|
[HttpPost("list-schools")]
|
|
|
public async Task<IActionResult> ListSchool(JsonNode json)
|
|
|
{
|
|
|
- string filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "package", "schools.json");
|
|
|
+ string filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "schools.json");
|
|
|
string schoolText = await System.IO.File.ReadAllTextAsync(filePath);
|
|
|
JsonNode? node = schoolText.ToObject<JsonNode>();
|
|
|
return Ok(new {code =200, schools= node?["schools"] });
|
|
@@ -509,7 +531,7 @@ namespace IES.ExamServer.Controllers
|
|
|
string fp = $"{json["fp"]}";
|
|
|
if (!string.IsNullOrWhiteSpace(id) && !string.IsNullOrWhiteSpace(name) && !string.IsNullOrWhiteSpace(fp))
|
|
|
{
|
|
|
- string filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "package", "schools.json");
|
|
|
+ string filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "schools.json");
|
|
|
string schoolText = await System.IO.File.ReadAllTextAsync(filePath);
|
|
|
List<School>? schools = schoolText.ToObject<JsonNode>()?["schools"]?.ToObject<List<School>>();
|
|
|
School? school = schools?.Find(x => id.Equals(x.id) && name.Equals(x.name));
|