65 lines
2.1 KiB
C#
65 lines
2.1 KiB
C#
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Options;
|
|
using System.Net;
|
|
using System.Net.NetworkInformation;
|
|
using System.Net.Sockets;
|
|
|
|
namespace OnlineSalesAutoCrop.WS
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
[ApiController]
|
|
[Route("globalApi")]
|
|
public class GlobalApiController(IOptions<AppSettings> appSettings) : ControllerBase
|
|
{
|
|
private readonly string _anydeskPort = appSettings.Value.AnyDeskPort;
|
|
private readonly string _ipStartBlock = appSettings.Value.StartIpBlock;
|
|
private readonly string _remoteDesktopPort = appSettings.Value.RemoteDesktopPort;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpPost("readSettings")]
|
|
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IpHostResponse))]
|
|
public IActionResult ReadSettings([FromBody] NoContentRequest request)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(request);
|
|
IpHostResponse response = new() { Status = 200, HostName = Dns.GetHostName() };
|
|
|
|
try
|
|
{
|
|
response.RemoteHost = HeplerFunctions.GetRemoteIp(adPort: _anydeskPort, rdPort: _remoteDesktopPort, out bool isAnydesk);
|
|
if (!string.IsNullOrEmpty(response.RemoteHost))
|
|
{
|
|
response.Status = isAnydesk ? 998 : 999;
|
|
return Ok(response);
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
//Nothing to do
|
|
}
|
|
|
|
foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces().Where(x => x.NetworkInterfaceType == NetworkInterfaceType.Ethernet && x.OperationalStatus == OperationalStatus.Up))
|
|
{
|
|
IPInterfaceProperties adapterProperties = item.GetIPProperties();
|
|
if (adapterProperties?.UnicastAddresses == null)
|
|
continue;
|
|
|
|
UnicastIPAddressInformation? ip = adapterProperties.UnicastAddresses.FirstOrDefault(x => x.Address.AddressFamily == AddressFamily.InterNetwork && x.Address.ToString().StartsWith(_ipStartBlock));
|
|
response.IpAddress = ip == null ? string.Empty : ip.Address.ToString();
|
|
response.MacAddress = item.GetPhysicalAddress().ToString();
|
|
if (!string.IsNullOrEmpty(response.IpAddress))
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
return Ok(response);
|
|
}
|
|
}
|
|
}
|