You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
281 lines
9.9 KiB
281 lines
9.9 KiB
using Microsoft.AspNetCore.Mvc;
|
|
using LTEMvcApp.Models;
|
|
using LTEMvcApp.Services;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace LTEMvcApp.Controllers
|
|
{
|
|
/// <summary>
|
|
/// IP组管理控制器 - 负责IP组网络启动、停止和Key管理
|
|
/// </summary>
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class IpGroupController : ControllerBase
|
|
{
|
|
private readonly HttpClientService _httpClientService;
|
|
private readonly ILogger<IpGroupController> _logger;
|
|
|
|
// 存储IP组的Key配置
|
|
private static readonly Dictionary<string, string> _ipGroupKeys = new();
|
|
|
|
public IpGroupController(HttpClientService httpClientService, ILogger<IpGroupController> logger)
|
|
{
|
|
_httpClientService = httpClientService;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存IP组Key配置
|
|
/// </summary>
|
|
/// <param name="request">IP组Key请求</param>
|
|
/// <returns>操作结果</returns>
|
|
[HttpPost("key")]
|
|
public ActionResult SaveIpGroupKey([FromBody] IpGroupKeyRequest request)
|
|
{
|
|
if (string.IsNullOrEmpty(request.Ip))
|
|
return BadRequest("IP地址不能为空");
|
|
|
|
_ipGroupKeys[request.Ip] = request.Key ?? string.Empty;
|
|
_logger.LogInformation("保存IP组Key: {Ip} -> {Key}", request.Ip, request.Key);
|
|
|
|
return Ok(new { message = "IP组Key保存成功" });
|
|
}
|
|
|
|
/// <summary>
|
|
/// 启动IP组网络
|
|
/// </summary>
|
|
/// <param name="request">启动网络请求</param>
|
|
/// <returns>操作结果</returns>
|
|
[HttpPost("start")]
|
|
public async Task<ActionResult> StartIpGroup([FromBody] StartIpGroupRequest request)
|
|
{
|
|
if (string.IsNullOrEmpty(request.Ip))
|
|
return BadRequest("IP地址不能为空");
|
|
|
|
if (string.IsNullOrEmpty(request.Port))
|
|
return BadRequest("端口不能为空");
|
|
|
|
// 检查Key是否已配置
|
|
if (!_ipGroupKeys.TryGetValue(request.Ip, out var key) || string.IsNullOrEmpty(key))
|
|
return BadRequest("请先配置网络Key");
|
|
|
|
try
|
|
{
|
|
var apiUrl = $"http://{request.Ip}:{request.Port}/api/v1/CellularNetwork/start";
|
|
var command = new StartCellularNetworkCommand { Key = key };
|
|
|
|
_logger.LogInformation("启动IP组网络: {Ip}:{Port}, Key: {Key}", request.Ip, request.Port, key);
|
|
|
|
var response = await _httpClientService.PostJsonAsync(apiUrl, command);
|
|
|
|
_logger.LogInformation("IP组网络启动成功: {Response}", response);
|
|
return Ok(new { message = "网络启动成功", response });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "启动IP组网络失败: {Ip}:{Port}", request.Ip, request.Port);
|
|
return BadRequest($"启动网络失败: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 停止IP组网络
|
|
/// </summary>
|
|
/// <param name="request">停止网络请求</param>
|
|
/// <returns>操作结果</returns>
|
|
[HttpPost("stop")]
|
|
public async Task<ActionResult> StopIpGroup([FromBody] StopIpGroupRequest request)
|
|
{
|
|
if (string.IsNullOrEmpty(request.Ip))
|
|
return BadRequest("IP地址不能为空");
|
|
|
|
if (string.IsNullOrEmpty(request.Port))
|
|
return BadRequest("端口不能为空");
|
|
|
|
// 检查Key是否已配置
|
|
if (!_ipGroupKeys.TryGetValue(request.Ip, out var key) || string.IsNullOrEmpty(key))
|
|
return BadRequest("请先配置网络Key");
|
|
|
|
try
|
|
{
|
|
var apiUrl = $"http://{request.Ip}:{request.Port}/api/v1/CellularNetwork/stop";
|
|
var command = new StopCellularNetworkCommand { Key = key };
|
|
|
|
_logger.LogInformation("停止IP组网络: {Ip}:{Port}, Key: {Key}", request.Ip, request.Port, key);
|
|
|
|
var response = await _httpClientService.PostJsonAsync(apiUrl, command);
|
|
|
|
_logger.LogInformation("IP组网络停止成功: {Response}", response);
|
|
return Ok(new { message = "网络停止成功", response });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "停止IP组网络失败: {Ip}:{Port}", request.Ip, request.Port);
|
|
return BadRequest($"停止网络失败: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取IP组Key配置
|
|
/// </summary>
|
|
/// <param name="ip">IP地址</param>
|
|
/// <returns>Key配置</returns>
|
|
[HttpGet("key/{ip}")]
|
|
public ActionResult GetIpGroupKey(string ip)
|
|
{
|
|
if (string.IsNullOrEmpty(ip))
|
|
return BadRequest("IP地址不能为空");
|
|
|
|
var key = _ipGroupKeys.TryGetValue(ip, out var value) ? value : string.Empty;
|
|
return Ok(new { ip, key });
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取所有IP组Key配置
|
|
/// </summary>
|
|
/// <returns>所有IP组Key配置</returns>
|
|
[HttpGet("keys")]
|
|
public ActionResult GetAllIpGroupKeys()
|
|
{
|
|
return Ok(_ipGroupKeys);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除IP组Key配置
|
|
/// </summary>
|
|
/// <param name="ip">IP地址</param>
|
|
/// <returns>操作结果</returns>
|
|
[HttpDelete("key/{ip}")]
|
|
public ActionResult DeleteIpGroupKey(string ip)
|
|
{
|
|
if (string.IsNullOrEmpty(ip))
|
|
return BadRequest("IP地址不能为空");
|
|
|
|
if (_ipGroupKeys.Remove(ip))
|
|
{
|
|
_logger.LogInformation("删除IP组Key: {Ip}", ip);
|
|
return Ok(new { message = "IP组Key删除成功" });
|
|
}
|
|
else
|
|
{
|
|
return NotFound($"未找到IP地址为 {ip} 的Key配置");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取网络配置列表
|
|
/// </summary>
|
|
/// <param name="ip">IP地址</param>
|
|
/// <param name="port">端口</param>
|
|
/// <returns>网络配置列表</returns>
|
|
[HttpGet("network-config")]
|
|
public async Task<ActionResult> GetNetworkConfigs([FromQuery] string ip, [FromQuery] string port)
|
|
{
|
|
try
|
|
{
|
|
var apiUrl = $"http://{ip}:{port}/api/v1/NetworkConfig";
|
|
_logger.LogInformation("获取网络配置: {ApiUrl}", apiUrl);
|
|
|
|
var response = await _httpClientService.GetAsync<NetworkConfigResponse>(apiUrl);
|
|
|
|
_logger.LogInformation("网络配置获取成功");
|
|
return Ok(response);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "获取网络配置失败: {Ip}:{Port}", ip, port);
|
|
return BadRequest($"获取网络配置失败: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 添加网络配置(转发)
|
|
/// </summary>
|
|
[HttpPost("network-config")]
|
|
public async Task<ActionResult> AddNetworkConfig([FromQuery] string ip, [FromQuery] string port, [FromBody] object config)
|
|
{
|
|
if (string.IsNullOrEmpty(ip) || string.IsNullOrEmpty(port))
|
|
return BadRequest("ip和port不能为空");
|
|
try
|
|
{
|
|
var apiUrl = $"http://{ip}:{port}/api/v1/NetworkConfig";
|
|
var response = await _httpClientService.PostJsonAsync(apiUrl, config);
|
|
return Content(response, "application/json");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "添加网络配置失败: {Ip}:{Port}", ip, port);
|
|
return BadRequest($"添加网络配置失败: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除网络配置(转发)
|
|
/// </summary>
|
|
[HttpDelete("network-config/{configKey}")]
|
|
public async Task<ActionResult> DeleteNetworkConfig(string configKey, [FromQuery] string ip, [FromQuery] string port)
|
|
{
|
|
if (string.IsNullOrEmpty(ip) || string.IsNullOrEmpty(port))
|
|
return BadRequest("ip和port不能为空");
|
|
try
|
|
{
|
|
var apiUrl = $"http://{ip}:{port}/api/v1/NetworkConfig/{configKey}";
|
|
var response = await _httpClientService.DeleteAsync(apiUrl);
|
|
return Content(response, "application/json");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "删除网络配置失败: {Ip}:{Port}", ip, port);
|
|
return BadRequest($"删除网络配置失败: {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// IP组Key请求
|
|
/// </summary>
|
|
public class IpGroupKeyRequest
|
|
{
|
|
/// <summary>
|
|
/// IP地址
|
|
/// </summary>
|
|
public string Ip { get; set; } = string.Empty;
|
|
|
|
/// <summary>
|
|
/// Key值
|
|
/// </summary>
|
|
public string? Key { get; set; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 启动IP组请求
|
|
/// </summary>
|
|
public class StartIpGroupRequest
|
|
{
|
|
/// <summary>
|
|
/// IP地址
|
|
/// </summary>
|
|
public string Ip { get; set; } = string.Empty;
|
|
|
|
/// <summary>
|
|
/// 端口
|
|
/// </summary>
|
|
public string Port { get; set; } = string.Empty;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 停止IP组请求
|
|
/// </summary>
|
|
public class StopIpGroupRequest
|
|
{
|
|
/// <summary>
|
|
/// IP地址
|
|
/// </summary>
|
|
public string Ip { get; set; } = string.Empty;
|
|
|
|
/// <summary>
|
|
/// 端口
|
|
/// </summary>
|
|
public string Port { get; set; } = string.Empty;
|
|
}
|
|
}
|