-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
72 lines (56 loc) · 1.49 KB
/
Program.cs
File metadata and controls
72 lines (56 loc) · 1.49 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
66
67
68
69
70
71
72
using net;
var version = "v1.3";
var options = CommandLineOptions.Parse(args, version);
if (options.ShouldExit)
{
return;
}
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(opt =>
{
opt.ListenAnyIP(options.Port);
});
var app = builder.Build();
// 错误处理
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/error");
}
app.MapGet("/error", () =>
Results.Text("Internal Server Error", "text/plain"));
var netstat = new NetworkStats();
app.MapGet("/api/status", (HttpContext ctx) =>
{
ctx.Response.Headers["Cache-Control"] = "no-store";
ctx.Response.Headers["X-Content-Type-Options"] = "nosniff";
string clientIp = "unknown";
if (ctx.Request.Headers.TryGetValue("X-Forwarded-For", out var xff))
{
clientIp = xff.ToString().Split(',')[0].Trim();
}
else
{
clientIp = ctx.Connection.RemoteIpAddress?.ToString() ?? "unknown";
}
var response = new
{
version,
server_time = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff 'UTC'"),
client_ip = clientIp,
network_stats = netstat.GetJsonData()
};
return Results.Json(response);
});
app.MapGet("/", (HttpContext ctx) =>
{
ctx.Response.Headers["Cache-Control"] = "no-store";
ctx.Response.Headers["X-Content-Type-Options"] = "nosniff";
var html = StatusPageBuilder.Build(
ctx,
version,
netstat,
options.FooterHtml
);
return Results.Text(html, "text/html");
});
app.Run();