-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
65 lines (56 loc) · 2.72 KB
/
Copy pathProgram.cs
File metadata and controls
65 lines (56 loc) · 2.72 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright © 2026 Xpl0itR
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
using System.Net;
using System.Threading.RateLimiting;
using Microsoft.AspNetCore.Connections;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Proto.Promises;
using Xport.AspNetCore.Adapters.Middleware.Logging;
using Xport.AspNetCore.Adapters.Transport.Sockets;
using Xport.Hosting;
using Xport.Middleware;
using Xport.Middleware.ConnectionLimiter;
using XportExample;
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
#if DEBUG
builder.Logging.SetMinimumLevel(LogLevel.Debug);
#endif
builder.Services.AddSocketTransport()
.AddHostedTransportServer(builder.Configuration.GetSection("Xport"))
.AddConnectionServicesMiddleware()
.AddLoggingTransportMiddleware()
.AddConnectionLimiterMiddleware()
.AddConnectionLimiterPolicy(
PartitionedRateLimiter.Create<BaseConnectionContext, string>(static _ =>
RateLimitPartition.GetConcurrencyLimiter("global", static _ =>
new ConcurrencyLimiterOptions { PermitLimit = 10000 })));
using IHost host = builder.Build();
ILogger logger = host.Services.GetRequiredService<ILogger<Program>>();
Promise.Config.UncaughtRejectionHandler = ex => logger.LogError(ex, "An unhandled exception has occurred in an unawaited promise.");
host.ConfigureTransportServer(static server =>
{
server.ListenIp(IPAddress.Loopback, 1234, static options => // programmatically configure an endpoint
options.UseSocketTransport()
.UseSingleplexedMiddleware<ConnectionLimiterMiddleware>()
#if DEBUG
.UseSingleplexedMiddleware<LoggingTransportMiddleware>()
#endif
.UseSingleplexedMiddleware<ConnectionServicesMiddleware>()
.UseSingleplexedConnectionHandler<SessionManager>());
server.ConfigureEndpoint("EchoServer", static options => // configure an endpoint from configuration (see appsettings.json)
options.UseSingleplexedMiddleware<ConnectionLimiterMiddleware>()
#if DEBUG
.UseSingleplexedMiddleware<LoggingTransportMiddleware>()
#endif
.Run(static async conn =>
{
await conn.Transport.Output.WriteAsync("Welcome to the echo test server!\n"u8.ToArray(), conn.ConnectionClosed);
await conn.Transport.Input.CopyToAsync(conn.Transport.Output, conn.ConnectionClosed);
}));
});
await host.RunAsync();