|
1 | 1 | using System;
|
2 | 2 | using System.Collections.Generic;
|
| 3 | +using System.Net; |
3 | 4 | using DnsClient;
|
4 | 5 | using DnsTools.Web.HealthChecks;
|
5 | 6 | using DnsTools.Web.Hubs;
|
|
18 | 19 | using Microsoft.Extensions.Diagnostics.HealthChecks;
|
19 | 20 | using Microsoft.Extensions.Hosting;
|
20 | 21 |
|
21 |
| -namespace DnsTools.Web |
| 22 | +const string CORS_PROD = "prod"; |
| 23 | +const string CORS_DEV = "dev"; |
| 24 | + |
| 25 | +var builder = WebApplication.CreateBuilder(args); |
| 26 | + |
| 27 | +builder.WebHost |
| 28 | + .UseSentry(options => |
| 29 | + { |
| 30 | + options.Release = ThisAssembly.Git.Sha; |
| 31 | + }); |
| 32 | + |
| 33 | +var config = builder.Configuration; |
| 34 | +// Config shared between client-side and server-side |
| 35 | +config.AddJsonFile("ClientApp/src/config.json"); |
| 36 | + |
| 37 | +var services = builder.Services; |
| 38 | +// Default caching DNS client, eg. for reverse DNS lookups |
| 39 | +services.AddSingleton<ILookupClient>(_ => new LookupClient(new LookupClientOptions |
| 40 | +{ |
| 41 | + UseCache = true |
| 42 | +})); |
| 43 | + |
| 44 | +services.Configure<AppConfig>(config); |
| 45 | +services.AddSentryTunneling("errors.d.sb"); |
| 46 | +services.AddSingleton<IHttp2PushManifestHandler, Http2PushManifestHandler>(); |
| 47 | +services.AddSingleton<IWorkerProvider, WorkerProvider>(); |
| 48 | +services.AddSingleton<IIpDataProvider, IpDataProvider>(); |
| 49 | +services.AddScoped<ICaptcha, Captcha>(); |
| 50 | + |
| 51 | +services.AddSingleton<IIpDataLoader, IpInfoIpDataLoader>(); |
| 52 | +services.AddSingleton<IIpDataLoader, MaxMindIpDataLoader>(); |
| 53 | + |
| 54 | +services.AddSingleton<TracerouteRunner>(); |
| 55 | +services.AddTransient<MtrRunner>(); |
| 56 | + |
| 57 | +services.AddHttpClient(); |
| 58 | +services.AddHttpContextAccessor(); |
| 59 | + |
| 60 | +services.AddCors(options => |
| 61 | +{ |
| 62 | + options.AddPolicy(CORS_PROD , builder => |
| 63 | + { |
| 64 | + builder.WithOrigins("https://dnstools.ws", "https://staging.dnstools.ws") |
| 65 | + .AllowAnyHeader() |
| 66 | + .AllowCredentials(); |
| 67 | + }); |
| 68 | + |
| 69 | + options.AddPolicy(CORS_DEV, builder => |
| 70 | + { |
| 71 | + builder.WithOrigins( |
| 72 | + // create-react-app server |
| 73 | + "http://localhost:64329", |
| 74 | + // react-snap server |
| 75 | + "http://localhost:45678" |
| 76 | + ) |
| 77 | + .AllowAnyHeader() |
| 78 | + .AllowCredentials(); |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
| 82 | +services.AddControllersWithViews(); |
| 83 | +services.AddSignalR().AddJsonProtocol(options => |
| 84 | +{ |
| 85 | + options.PayloadSerializerOptions.IgnoreNullValues = true; |
| 86 | +}); |
| 87 | + |
| 88 | +services.AddDistributedMemoryCache(); |
| 89 | +services.AddSession(options => |
| 90 | +{ |
| 91 | + options.IdleTimeout = TimeSpan.FromHours(1); |
| 92 | + options.Cookie.HttpOnly = true; |
| 93 | + options.Cookie.IsEssential = true; |
| 94 | + options.Cookie.Name = ".DnsTools.Session"; |
| 95 | + options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest; |
| 96 | +}); |
| 97 | + |
| 98 | +ConfigureHealthChecks(services.AddHealthChecks()); |
| 99 | + |
| 100 | +var app = builder.Build(); |
| 101 | + |
| 102 | +if (app.Environment.IsDevelopment()) |
| 103 | +{ |
| 104 | + app.UseDeveloperExceptionPage(); |
| 105 | + // Allow unencrypted gRPC calls in development |
| 106 | + AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); |
| 107 | +} |
| 108 | +else |
| 109 | +{ |
| 110 | + app.UseExceptionHandler("/Error/Error"); |
| 111 | + app.UseStatusCodePagesWithReExecute("/Error/Status{0}"); |
| 112 | + // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. |
| 113 | + app.UseHsts(); |
| 114 | +} |
| 115 | + |
| 116 | +app.UseHttpsRedirection(); |
| 117 | +app.UseForwardedHeaders(new ForwardedHeadersOptions |
| 118 | +{ |
| 119 | + ForwardedHeaders = ForwardedHeaders.XForwardedFor |
| 120 | +}); |
| 121 | +app.UseStaticFiles(); |
| 122 | +app.UseSession(); |
| 123 | + |
| 124 | +app.UseRouting(); |
| 125 | +app.UseCors(app.Environment.IsDevelopment() ? CORS_DEV : CORS_PROD); |
| 126 | +app.UseSentryTunneling("/error/log"); |
| 127 | + |
| 128 | +app.UseHealthChecksPrometheusExporter("/health/prom", options => |
| 129 | +{ |
| 130 | + options.ResultStatusCodes = new Dictionary<HealthStatus, int> |
| 131 | + { |
| 132 | + // Always return HTTP 200 so Prometheus sees the request as successful. |
| 133 | + {HealthStatus.Healthy, 200}, |
| 134 | + {HealthStatus.Degraded, 200}, |
| 135 | + {HealthStatus.Unhealthy, 200}, |
| 136 | + }; |
| 137 | +}); |
| 138 | + |
| 139 | +app.MapHealthChecks("/health"); |
| 140 | +app.MapHealthChecks("/health/json", new HealthCheckOptions |
| 141 | +{ |
| 142 | + ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse, |
| 143 | +}); |
| 144 | + |
| 145 | +app.MapHub<ToolsHub>("/hub"); |
| 146 | +app.MapControllerRoute( |
| 147 | + name: "default", |
| 148 | + pattern: "{controller}/{action=Index}/{id?}" |
| 149 | +); |
| 150 | + |
| 151 | +app.Run(); |
| 152 | + |
| 153 | +return; |
| 154 | + |
| 155 | +void ConfigureHealthChecks(IHealthChecksBuilder builder) |
22 | 156 | {
|
23 |
| - public class Startup |
| 157 | + var appConfig = config.Get<AppConfig>(); |
| 158 | + foreach (var worker in appConfig.Workers) |
24 | 159 | {
|
25 |
| - private const string CORS_PROD = "prod"; |
26 |
| - private const string CORS_DEV = "dev"; |
27 |
| - |
28 |
| - public Startup(IConfiguration configuration) |
29 |
| - { |
30 |
| - Configuration = configuration; |
31 |
| - } |
32 |
| - |
33 |
| - public IConfiguration Configuration { get; } |
34 |
| - |
35 |
| - // This method gets called by the runtime. Use this method to add services to the container. |
36 |
| - public void ConfigureServices(IServiceCollection services) |
37 |
| - { |
38 |
| - // Default caching DNS client, eg. for reverse DNS lookups |
39 |
| - services.AddSingleton<ILookupClient>(_ => new LookupClient(new LookupClientOptions |
40 |
| - { |
41 |
| - UseCache = true |
42 |
| - })); |
43 |
| - |
44 |
| - services.Configure<AppConfig>(Configuration); |
45 |
| - services.AddSentryTunneling("errors.d.sb"); |
46 |
| - services.AddSingleton<IHttp2PushManifestHandler, Http2PushManifestHandler>(); |
47 |
| - services.AddSingleton<IWorkerProvider, WorkerProvider>(); |
48 |
| - services.AddSingleton<IIpDataProvider, IpDataProvider>(); |
49 |
| - services.AddScoped<ICaptcha, Captcha>(); |
50 |
| - |
51 |
| - services.AddSingleton<IIpDataLoader, IpInfoIpDataLoader>(); |
52 |
| - services.AddSingleton<IIpDataLoader, MaxMindIpDataLoader>(); |
53 |
| - |
54 |
| - services.AddSingleton<TracerouteRunner>(); |
55 |
| - services.AddTransient<MtrRunner>(); |
56 |
| - |
57 |
| - services.AddHttpClient(); |
58 |
| - services.AddHttpContextAccessor(); |
59 |
| - |
60 |
| - services.AddCors(options => |
61 |
| - { |
62 |
| - options.AddPolicy(CORS_PROD , builder => |
63 |
| - { |
64 |
| - builder.WithOrigins("https://dnstools.ws") |
65 |
| - .AllowAnyHeader() |
66 |
| - .AllowCredentials(); |
67 |
| - }); |
68 |
| - |
69 |
| - options.AddPolicy(CORS_DEV, builder => |
70 |
| - { |
71 |
| - builder.WithOrigins( |
72 |
| - // create-react-app server |
73 |
| - "http://localhost:31429", |
74 |
| - // react-snap server |
75 |
| - "http://localhost:45678" |
76 |
| - ) |
77 |
| - .AllowAnyHeader() |
78 |
| - .AllowCredentials(); |
79 |
| - }); |
80 |
| - }); |
81 |
| - |
82 |
| - services.AddControllersWithViews(); |
83 |
| - services.AddSignalR().AddJsonProtocol(options => |
84 |
| - { |
85 |
| - options.PayloadSerializerOptions.IgnoreNullValues = true; |
86 |
| - }); |
87 |
| - |
88 |
| - services.AddDistributedMemoryCache(); |
89 |
| - services.AddSession(options => |
90 |
| - { |
91 |
| - options.IdleTimeout = TimeSpan.FromHours(1); |
92 |
| - options.Cookie.HttpOnly = true; |
93 |
| - options.Cookie.IsEssential = true; |
94 |
| - options.Cookie.Name = ".DnsTools.Session"; |
95 |
| - options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest; |
96 |
| - }); |
97 |
| - |
98 |
| - ConfigureHealthChecks(services.AddHealthChecks()); |
99 |
| - } |
100 |
| - |
101 |
| - private void ConfigureHealthChecks(IHealthChecksBuilder builder) |
102 |
| - { |
103 |
| - var config = Configuration.Get<AppConfig>(); |
104 |
| - foreach (var worker in config.Workers) |
105 |
| - { |
106 |
| - builder.AddTypeActivatedCheck<WorkerHealthCheck>($"worker_{worker.Id}", worker.Id); |
107 |
| - } |
108 |
| - } |
109 |
| - |
110 |
| - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. |
111 |
| - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) |
112 |
| - { |
113 |
| - if (env.IsDevelopment()) |
114 |
| - { |
115 |
| - app.UseDeveloperExceptionPage(); |
116 |
| - // Allow unencrypted gRPC calls in development |
117 |
| - AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); |
118 |
| - } |
119 |
| - else |
120 |
| - { |
121 |
| - app.UseExceptionHandler("/Error/Error"); |
122 |
| - app.UseStatusCodePagesWithReExecute("/Error/Status{0}"); |
123 |
| - // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. |
124 |
| - app.UseHsts(); |
125 |
| - } |
126 |
| - |
127 |
| - app.UseHttpsRedirection(); |
128 |
| - app.UseForwardedHeaders(new ForwardedHeadersOptions |
129 |
| - { |
130 |
| - ForwardedHeaders = ForwardedHeaders.XForwardedFor |
131 |
| - }); |
132 |
| - app.UseStaticFiles(); |
133 |
| - app.UseSession(); |
134 |
| - |
135 |
| - app.UseRouting(); |
136 |
| - app.UseCors(env.IsDevelopment() ? CORS_DEV : CORS_PROD); |
137 |
| - app.UseSentryTunneling("/error/log"); |
138 |
| - |
139 |
| - app.UseHealthChecksPrometheusExporter("/health/prom", options => |
140 |
| - { |
141 |
| - options.ResultStatusCodes = new Dictionary<HealthStatus, int> |
142 |
| - { |
143 |
| - // Always return HTTP 200 so Prometheus sees the request as successful. |
144 |
| - {HealthStatus.Healthy, 200}, |
145 |
| - {HealthStatus.Degraded, 200}, |
146 |
| - {HealthStatus.Unhealthy, 200}, |
147 |
| - }; |
148 |
| - }); |
149 |
| - app.UseEndpoints(endpoints => |
150 |
| - { |
151 |
| - endpoints.MapHealthChecks("/health"); |
152 |
| - endpoints.MapHealthChecks("/health/json", new HealthCheckOptions |
153 |
| - { |
154 |
| - ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse, |
155 |
| - }); |
156 |
| - |
157 |
| - endpoints.MapHub<ToolsHub>("/hub"); |
158 |
| - endpoints.MapControllerRoute( |
159 |
| - name: "default", |
160 |
| - pattern: "{controller}/{action=Index}/{id?}"); |
161 |
| - }); |
162 |
| - } |
| 160 | + builder.AddTypeActivatedCheck<WorkerHealthCheck>($"worker_{worker.Id}", worker.Id); |
163 | 161 | }
|
164 | 162 | }
|
0 commit comments