-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
45 lines (38 loc) · 1.64 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System.Net;
using System.Net.Sockets;
using QRCoder;
var builder = WebApplication.CreateBuilder(args);
builder.Logging.AddSimpleConsole(c => c.SingleLine = true);
var app = builder.Build();
app.Logger.LogInformation("Starting...");
var addresses = Dns.GetHostEntry("").AddressList
.Where(ip => ip.AddressFamily == AddressFamily.InterNetwork).Distinct().ToList();
if (addresses.Count == 0) throw new PlatformNotSupportedException("Can't find any local IP");
if (addresses.Count > 1) app.Logger.LogWarning("More than one local IP available: {IPs}", addresses);
var host = $"http://{addresses[0]}:7384/";
app.Urls.Add(host);
app.UseWebSockets(new WebSocketOptions
{
KeepAliveInterval = TimeSpan.FromSeconds(1)
});
using var controller = new Controller();
app.Use(async (context, next) =>
{
var logger = context.RequestServices.GetService<ILogger<Controller>>()!;
if (context.Request.Path == "/ws" && context.WebSockets.IsWebSocketRequest)
{
logger.LogInformation($"New connection from {context.Connection.RemoteIpAddress}");
using var webSocket = await context.WebSockets.AcceptWebSocketAsync("tmphonepad");
await controller.Connect(webSocket, logger, context.RequestAborted);
}
else await next(context);
});
app.UseFileServer(new FileServerOptions
{
StaticFileOptions = { OnPrepareResponse = (ctx) =>
ctx.Context.Response.GetTypedHeaders().CacheControl = new() { NoStore = true } }
});
app.Start();
var qr = AsciiQRCodeHelper.GetQRCode(host, 1, " ", "██", QRCodeGenerator.ECCLevel.L);
app.Logger.LogInformation($"Started! Open {host} on your touchscreen device: \n{qr}");
app.WaitForShutdown();