-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Program.cs
233 lines (208 loc) · 8.45 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
using System.Net;
using Blazored.Toast;
using Fenrus;
using Fenrus.Models;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption;
using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel;
using Microsoft.AspNetCore.HttpOverrides;
using NUglify.Helpers;
if (args?.Any() == true && args[0] == "--init-config")
{
Console.WriteLine("Resetting initial configuration flags");
new SystemSettingsService().Delete();
if (Globals.IsDocker)
Console.WriteLine("Restart the Docker container to take effect.");
else
Console.WriteLine("Restart the application to take effect.");
return;
}
Console.WriteLine("Starting Fenrus...");
StartUpHelper.Run();
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddEnvironmentVariables();
builder.Services.AddControllersWithViews();
builder.Services.AddMvc();
//Gets the reverse proxy settings from the appsettings.json file
//to check if the app is running behind a reverse proxy
ReverseProxySettings reverseProxySettings = builder.Configuration.GetSection(nameof(ReverseProxySettings)).Get<ReverseProxySettings>();
if(reverseProxySettings.UseForwardedHeaders)
{
ConfigureUsingForwardedHeaders(builder, reverseProxySettings);
}
builder.Services.AddWebOptimizer(pipeline =>
{
pipeline.MinifyJsFiles("js/**/*.js");
var jsFiles = Directory.GetFiles("wwwroot/js", "*.js", SearchOption.AllDirectories)
.OrderBy(f => f)
.Select(x =>
{
int index = x.IndexOf("wwwroot/js");
return x[(index + 7)..];
})
.ToArray();
pipeline.AddJavaScriptBundle("/js/_fenrus.js", jsFiles);
pipeline.CompileScssFiles(new () { MinifyCss = true, SourceComments = false});
pipeline.AddScssBundle("/css/_fenrus.css", "css/**/*.scss");
});
builder.Services.Configure<IISServerOptions>(options =>
{
options.MaxRequestBodySize = long.MaxValue;
});
builder.Services.AddBlazoredToast();
if(Environment.GetEnvironmentVariable("DetailedErrors") == "1")
builder.Services.AddServerSideBlazor().AddCircuitOptions(options => { options.DetailedErrors = true; });
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor(options =>
{
});
builder.Services.AddSignalR(hubOptions =>
{
hubOptions.MaximumReceiveMessageSize = 10 * 1024 * 1024; // 10MB
});
builder.Services.AddTransient<Fenrus.Middleware.InitialConfigMiddleware>();
builder.Services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(DirectoryHelper.GetDataDirectory()))
.UseCryptographicAlgorithms(new AuthenticatedEncryptorConfiguration()
{
EncryptionAlgorithm = EncryptionAlgorithm.AES_256_CBC,
ValidationAlgorithm = ValidationAlgorithm.HMACSHA256
});
bool oAuth = SystemSettingsService.InitConfigDone && SystemSettingsService.UsingOAuth;
if (oAuth)
{
var system = new SystemSettingsService().Get();
builder.Services.AddAuthentication(o =>
{
o.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
o.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(options =>
{
options.ClientId = system.OAuthStrategyClientId;
options.ClientSecret = system.OAuthStrategySecret;
options.Authority = system.OAuthStrategyIssuerBaseUrl;
options.Scope.Add("email");
options.Scope.Add("openid");
options.Scope.Add("profile");
options.GetClaimsFromUserInfoEndpoint = true;
options.DisableTelemetry = true;
options.Events.OnRedirectToIdentityProvider = context =>
{
//Added option to debug request headers for reverse proxy
//Sometimes it can be difficult to find out if X-Forwarded-X headers are set correctly
if(reverseProxySettings.DebugPrintRequestHeaders)
Logger.DLog($"Request headers: {string.Join(Environment.NewLine, context.Request.Headers)}");
return Task.FromResult(0);
};
});
}
else
{
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.ExpireTimeSpan = TimeSpan.FromMinutes(30);
options.SlidingExpiration = true;
options.AccessDeniedPath = "/Forbidden/";
});
}
var app = builder.Build();
if(reverseProxySettings.UseForwardedHeaders)
app.UseForwardedHeaders();
bool debug = Environment.GetEnvironmentVariable("DEBUG") == "1";
app.UseWhen(context =>
{
if(debug)
Logger.DLog("Requesting: " + context.Request.Path);
if (SystemSettingsService.InitConfigDone)
return false;
var path = context.Request.Path;
if (path.StartsWithSegments("/settings"))
return true;
if (path.StartsWithSegments("/dashboard"))
return true;
if (path.StartsWithSegments("/fimage"))
return false;
if (path.Value == "/")
return true;
return false;
}, appBuilder => appBuilder.UseMiddleware<Fenrus.Middleware.InitialConfigMiddleware>());
app.UseWhen(context => SystemSettingsService.InitConfigDone,
appBuild =>
{
appBuild.UseWebSockets();
});
app.UseWebOptimizer();
app.UseWhen(
context => context.Request.Path.StartsWithSegments("/apps") == false,
appBuilder => appBuilder.UseStaticFiles(new StaticFileOptions()
{
HttpsCompression = Microsoft.AspNetCore.Http.Features.HttpsCompressionMode.Compress,
OnPrepareResponse = (context) =>
{
var headers = context.Context.Response.GetTypedHeaders();
headers.CacheControl = new Microsoft.Net.Http.Headers.CacheControlHeaderValue
{
Public = true,
MaxAge = TimeSpan.FromDays(30)
};
}
}));
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(x =>
{
x.MapControllers();
});
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapBlazorHub(options =>
{
});
app.MapFallbackToPage("/_Host");
// create an uptime service to monitor the uptime status for monitor apps/links
// move this into a worker...
new UpTimeService().Start();
var workers = new Fenrus.Workers.Worker[]
{
new Fenrus.Workers.CalendarEventWorker(),
Fenrus.Workers.MailWorker.Instance
};
workers.ForEach(x => x.Start());
Logger.ILog($"Fenrus v{Fenrus.Globals.Version} started");
app.Run();
Logger.ILog($"Fenrus v{Fenrus.Globals.Version} stopped");
workers.ForEach(x => x.Stop());
// Configure the app to use forwarded headers
//If the app is running behind a reverse proxy, the app needs to be configured to use the forwarded headers
//This means that X-Forwarded-For and X-Forwarded-Proto headers are used to determine if the request goes over https,
//but is using ssl termination
void ConfigureUsingForwardedHeaders(WebApplicationBuilder webApplicationBuilder,
ReverseProxySettings reverseProxySettings1)
{
webApplicationBuilder.Services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders =
ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
foreach (var knownProxy in reverseProxySettings1.KnownProxies)
options.KnownProxies.Add(IPAddress.Parse($"{knownProxy}"));
if (reverseProxySettings1.KnownIpv4Network.Enabled)
{
if (string.IsNullOrWhiteSpace(reverseProxySettings1.KnownIpv4Network.IpAddress) ||
reverseProxySettings1.KnownIpv4Network.PrefixLength == 0)
throw new InvalidOperationException("Invalid IPv4 network configuration");
options.KnownNetworks.Add(new IPNetwork(IPAddress.Parse(reverseProxySettings1.KnownIpv4Network.IpAddress),
reverseProxySettings1.KnownIpv4Network.PrefixLength));
}
if (!reverseProxySettings1.KnownIpv6Network.Enabled) return;
if(string.IsNullOrWhiteSpace(reverseProxySettings1.KnownIpv6Network.IpAddress) || reverseProxySettings1.KnownIpv6Network.PrefixLength == 0)
throw new InvalidOperationException("Invalid IPv6 network configuration");
options.KnownNetworks.Add(new IPNetwork(IPAddress.Parse(reverseProxySettings1.KnownIpv6Network.IpAddress), reverseProxySettings1.KnownIpv6Network.PrefixLength));
});
}