-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathProgram.cs
331 lines (284 loc) · 10.9 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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
using FellowOakDicom;
using FellowOakDicom.Imaging.NativeCodec;
using Serilog;
using Serilog.Events;
using DicomSCP.Configuration;
using DicomSCP.Services;
using DicomSCP.Data;
using Microsoft.OpenApi.Models;
using System.Text;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication;
using System.Runtime.InteropServices;
using Microsoft.AspNetCore.Rewrite;
var builder = WebApplication.CreateBuilder(args);
// 配置控制台(跨平台支持)
if (Environment.UserInteractive)
{
try
{
if (OperatingSystem.IsWindows())
{
// Windows平台禁用快速编辑模式
var handle = ConsoleHelper.GetStdHandle(-10);
if (handle != IntPtr.Zero && ConsoleHelper.GetConsoleMode(handle, out uint mode))
{
mode &= ~(uint)(0x0040 | 0x0010);
ConsoleHelper.SetConsoleMode(handle, mode);
}
}
else
{
// Unix/Linux/MacOS 平台设置
Console.TreatControlCAsInput = true;
}
}
catch
{
// 忽略控制台配置错误
}
}
// 注册编码提供程序
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
// 注册 DICOM 编码
DicomEncoding.RegisterEncoding("GB2312", "GB2312");
// 配置 Kestrel
builder.WebHost.ConfigureKestrel(options =>
{
options.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(2);
options.Limits.RequestHeadersTimeout = TimeSpan.FromMinutes(1);
options.Limits.MaxConcurrentConnections = 100;
options.Limits.MaxConcurrentUpgradedConnections = 100;
options.Limits.MaxRequestBodySize = 52428800; // 50MB
});
// 取配置
var settings = builder.Configuration.GetSection("DicomSettings").Get<DicomSettings>()
?? new DicomSettings();
// 获取 Swagger 配置
var swaggerSettings = builder.Configuration.GetSection("Swagger").Get<SwaggerSettings>()
?? new SwaggerSettings();
// 配置日志
var logSettings = builder.Configuration
.GetSection("Logging")
.Get<LogSettings>() ?? new LogSettings();
// 初始化DICOM日志
DicomLogger.Initialize(logSettings);
// 初始化数据库日志
BaseRepository.ConfigureLogging(logSettings);
// 初始化API日志
ApiLoggingMiddleware.ConfigureLogging(logSettings);
// 配置框架日志
var logConfig = new LoggerConfiguration()
.MinimumLevel.Warning() // 只记录警告以上的日志
.Filter.ByExcluding(e =>
e.Properties.ContainsKey("SourceContext") &&
e.Properties["SourceContext"].ToString().Contains("FellowOakDicom.Network") &&
(e.MessageTemplate.Text.Contains("No accepted presentation context found") ||
e.MessageTemplate.Text.Contains("Study Root Query/Retrieve Information Model - FIND") ||
e.MessageTemplate.Text.Contains("Patient Root Query/Retrieve Information Model - FIND") ||
e.MessageTemplate.Text.Contains("Storage Commitment Push Model SOP Class") ||
e.MessageTemplate.Text.Contains("Modality Performed Procedure Step") ||
e.MessageTemplate.Text.Contains("Basic Grayscale Print Management Meta") ||
e.MessageTemplate.Text.Contains("Basic Color Print Management Meta") ||
e.MessageTemplate.Text.Contains("Verification SOP Class") ||
e.MessageTemplate.Text.Contains("rejected association") ||
e.MessageTemplate.Text.Contains("Association received")))
.WriteTo.Logger(lc => lc
.WriteTo.Console(
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] [{SourceContext}] {Message:l}{NewLine}",
restrictedToMinimumLevel: LogEventLevel.Warning
)
);
Log.Logger = logConfig.CreateLogger();
builder.Host.UseSerilog();
// 添加日志服务
builder.Services.AddLogging(loggingBuilder =>
loggingBuilder.AddSerilog(dispose: true));
// 添加服务
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
// 配置 Swagger
if (swaggerSettings.Enabled)
{
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc(swaggerSettings.Version, new OpenApiInfo
{
Title = swaggerSettings.Title,
Version = swaggerSettings.Version,
Description = swaggerSettings.Description
});
});
}
// DICOM服务注册
builder.Services
.AddFellowOakDicom()
.AddTranscoderManager<NativeTranscoderManager>();
builder.Services.AddSingleton<DicomRepository>();
builder.Services.AddSingleton<DicomServer>();
builder.Services.AddSingleton<WorklistRepository>();
builder.Services.AddSingleton<IStoreSCU, StoreSCU>();
builder.Services.AddSingleton<IPrintSCU, PrintSCU>();
// 确保配置服务正确注册
builder.Services.Configure<DicomSettings>(builder.Configuration.GetSection("DicomSettings"));
builder.Services.Configure<QueryRetrieveConfig>(builder.Configuration.GetSection("QueryRetrieveConfig"));
builder.Services.AddScoped<IQueryRetrieveSCU, QueryRetrieveSCU>();
// 注册 Swagger 配置
builder.Services.Configure<SwaggerSettings>(builder.Configuration.GetSection("Swagger"));
builder.Services.AddAuthentication("CustomAuth")
.AddCookie("CustomAuth", options =>
{
options.Cookie.Name = "auth";
options.LoginPath = "/login.html";
// cookies过期时间
options.ExpireTimeSpan = TimeSpan.FromMinutes(30);
options.SlidingExpiration = false; // 禁用默认的滑动过期
options.Cookie.HttpOnly = true;
options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest;
options.Cookie.SameSite = SameSiteMode.Lax;
options.Cookie.Path = "/";
//启用最大有效期后下发的是请求的时间戳,不打开就是请求的时间戳+30分钟。
//如果设置的大于30分钟,则每次请求都会更新过期时间,导致过期时间不准确。
//options.Cookie.MaxAge = TimeSpan.FromHours(12);
// 添加验证票据过期处理
options.Events = new CookieAuthenticationEvents
{
OnRedirectToLogin = context =>
{
if (context.Request.Path.StartsWithSegments("/api"))
{
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
return Task.CompletedTask;
}
context.Response.Redirect(context.RedirectUri);
return Task.CompletedTask;
},
// 修改验证处理
OnValidatePrincipal = async context =>
{
// 检查是否已过期
if (context.Properties?.ExpiresUtc <= DateTimeOffset.UtcNow)
{
context.RejectPrincipal();
await context.HttpContext.SignOutAsync("CustomAuth");
return;
}
// 如果不是 status 接口,手动更新过期时间
if (!context.HttpContext.Request.Path.StartsWithSegments("/api/dicom/status") &&
context.Properties is not null)
{
context.Properties.ExpiresUtc = DateTimeOffset.UtcNow.Add(options.ExpireTimeSpan);
context.ShouldRenew = true;
}
}
};
});
// 添加授权但不设置默认策略
builder.Services.AddAuthorization();
// 配置转发头
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = Microsoft.AspNetCore.HttpOverrides.ForwardedHeaders.XForwardedFor |
Microsoft.AspNetCore.HttpOverrides.ForwardedHeaders.XForwardedProto;
// 清除默认网络,否则会因为安全检查而被忽略
options.KnownNetworks.Clear();
options.KnownProxies.Clear();
});
// 在 ConfigureServices 部分添加
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll",
builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
// 配置 URL 重写规则
var rewriteOptions = new RewriteOptions()
//将dicomviewer路径下非直接文件的访问重写到dicomviewer/index.html上,解决spa单应用路由问题
.AddRewrite(
@"^dicomviewer/(?!.*\.(js|css|png|jpe?g|gif|ico|svg|woff2?|ttf|otf|eot|map|json|mp[34]|webm|mkv|avi|mov|pdf|docx?|xlsx?|pptx?|zip|rar|tar|gz|7z|ts|sh|bat|py|xml|ya?ml|ini|wasm|aac)).*$",
"/dicomviewer/index.html",
skipRemainingRules: true
);
var app = builder.Build();
// 初始化服务提供者
DicomServiceProvider.Initialize(app.Services);
// 获取服务
var dicomRepository = app.Services.GetRequiredService<DicomRepository>();
// 配置 DICOM
DicomSetupBuilder.UseServiceProvider(app.Services);
CStoreSCP.Configure(settings, dicomRepository);
// 启动 DICOM 服务器
var dicomServer = app.Services.GetRequiredService<DicomServer>();
await dicomServer.StartAsync();
app.Lifetime.ApplicationStopping.Register(() => dicomServer.StopAsync().GetAwaiter().GetResult());
// 优化线程池 - 基于CPU核心数
int processorCount = Environment.ProcessorCount;
ThreadPool.SetMinThreads(processorCount * 4, processorCount * 2); // 最小线程数
ThreadPool.SetMaxThreads(processorCount * 8, processorCount * 4); // 最大线程数
// 1. 转发头中间件(最先)
app.UseForwardedHeaders();
// 2. API 日志中间件
app.UseMiddleware<ApiLoggingMiddleware>();
// 3. URL 重写(在静态文件之前)
app.UseRewriter(rewriteOptions);
// 4. 静态文件处理
app.UseDefaultFiles(new DefaultFilesOptions
{
DefaultFileNames = new List<string> { "index.html", "login.html" }
});
app.UseStaticFiles();
// 5. Swagger
if (swaggerSettings.Enabled)
{
app.UseSwagger();
app.UseSwaggerUI();
}
// 6. 路由和认证
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseCors("AllowAll"); // CORS 应该在这里
// 7. API 认证中间件
app.Use(async (context, next) =>
{
var path = context.Request.Path.Value?.ToLower();
// 只检查 API 路径的认证
if (path?.StartsWith("/api/") == true &&
!path.StartsWith("/api/auth/login") &&
!context.User.Identity?.IsAuthenticated == true)
{
context.Response.StatusCode = 401;
return;
}
await next();
});
// 8. 控制器
app.MapControllers();
// 9. 根路径处理
app.MapGet("/", context =>
{
if (!context.User.Identity?.IsAuthenticated == true)
{
context.Response.Redirect("/login.html");
}
else
{
context.Response.Redirect("/index.html");
}
return Task.CompletedTask;
});
app.Run();
// Windows控制台API定义
internal static class ConsoleHelper
{
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
}