mirror of
https://gitlab.com/Hukuta47/cartrix.git
synced 2026-12-05 12:30:55 +00:00
59 lines
2.3 KiB
C#
59 lines
2.3 KiB
C#
using CartriX.Api.Classes;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Net;
|
|
|
|
namespace CartriX.Api.Controllers.Network
|
|
{
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class NetworkController(IConfiguration configuration) : ControllerBase
|
|
{
|
|
/// <summary>
|
|
/// Доступность
|
|
/// </summary>
|
|
/// <remarks>Данный маршрут предназначен для проверки доступности к API.</remarks>
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[HttpGet(nameof(Available))]
|
|
public async Task<ActionResult> Available() => Ok();
|
|
/// <summary>
|
|
/// Получить информацию с устройства через OID по SNMP
|
|
/// </summary>
|
|
[ProducesResponseType(typeof(string), StatusCodes.Status200OK)]
|
|
[HttpGet(nameof(GetSNMPData))]
|
|
public async Task<ActionResult> GetSNMPData([FromQuery] string IpAddressOrHostname, [FromQuery] string OID)
|
|
{
|
|
if (IPAddress.TryParse(IpAddressOrHostname, out IPAddress? iPAddress))
|
|
{
|
|
return Ok(await SnmpHelper.GetSnmpData(iPAddress.ToString(), OID));
|
|
}
|
|
else
|
|
{
|
|
var ipAddress = await NetworkHelper.GetIPFromHostnameAsync(IpAddressOrHostname);
|
|
return Ok(await SnmpHelper.GetSnmpData(ipAddress.ToString(), OID));
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Сделать ПИНГ к указанному хосту или IP Адресу
|
|
/// </summary>
|
|
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(bool), StatusCodes.Status404NotFound)]
|
|
[HttpGet(nameof(Ping))]
|
|
public async Task<ActionResult> Ping([FromQuery] string IpAddressOrHostname) => (await NetworkHelper.PingHostAsync(IpAddressOrHostname)) ? StatusCode(200, true) : StatusCode(404, false);
|
|
|
|
|
|
/// <summary>
|
|
/// Получить адрес
|
|
/// </summary>
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[HttpGet(nameof(BaseAddress))]
|
|
public async Task<ActionResult> BaseAddress()
|
|
{
|
|
var port = configuration.GetValue<int>("Server:Port");
|
|
var ip = NetworkHelper.GetLocalIp();
|
|
return Ok(new Uri($"http://{ip}:{port}"));
|
|
}
|
|
|
|
|
|
}
|
|
}
|