2024-07-31 09:14:07 +00:00
|
|
|
using System.Net.NetworkInformation;
|
2024-07-30 07:36:42 +00:00
|
|
|
using System.Text;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
namespace LocalServer.Controllers
|
|
|
|
{
|
|
|
|
public class LocalServerController(LocalSyncServerFactory factory) : ControllerBase
|
|
|
|
{
|
|
|
|
private readonly LocalSyncServerFactory Factory = factory;
|
|
|
|
|
2024-09-05 01:59:57 +00:00
|
|
|
[Route("/websoc")]
|
2024-07-30 07:36:42 +00:00
|
|
|
public async Task WebsocketConnection(string Name)
|
|
|
|
{
|
|
|
|
if (HttpContext.WebSockets.IsWebSocketRequest)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync();
|
|
|
|
Factory.CreateLocalSyncServer(webSocket, Name);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
HttpContext.Response.Body = new MemoryStream(Encoding.UTF8.GetBytes(e.Message));
|
|
|
|
HttpContext.Response.StatusCode = StatusCodes.Status406NotAcceptable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
|
|
|
|
}
|
|
|
|
}
|
2024-07-31 09:14:07 +00:00
|
|
|
|
|
|
|
[Route("/macaddr")]
|
|
|
|
public string GetMacAddress()
|
|
|
|
{
|
|
|
|
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
|
|
|
|
string macaddrs = "";
|
|
|
|
foreach (NetworkInterface nic in nics)
|
|
|
|
{
|
|
|
|
PhysicalAddress physicalAddress = nic.GetPhysicalAddress();
|
|
|
|
macaddrs += physicalAddress.ToString() + ";";
|
|
|
|
}
|
|
|
|
return macaddrs;
|
|
|
|
}
|
2024-07-30 07:36:42 +00:00
|
|
|
//TODO 是否在本地记载同步日志?
|
|
|
|
}
|
|
|
|
}
|