mirror of
https://github.com/tgstation/tgstation-server.git
synced 2026-07-21 21:13:05 +01:00
Update setup wizard, don't inject CORS if not necessary, add additional logging
This commit is contained in:
@@ -114,6 +114,7 @@ namespace Tgstation.Server.Host.Core
|
||||
GeneralConfiguration generalConfiguration;
|
||||
DatabaseConfiguration databaseConfiguration;
|
||||
FileLoggingConfiguration fileLoggingConfiguration;
|
||||
ControlPanelConfiguration controlPanelConfiguration;
|
||||
IIOManager ioManager;
|
||||
IPlatformIdentifier platformIdentifier;
|
||||
|
||||
@@ -142,6 +143,9 @@ namespace Tgstation.Server.Host.Core
|
||||
var loggingOptions = provider.GetRequiredService<IOptions<FileLoggingConfiguration>>();
|
||||
fileLoggingConfiguration = loggingOptions.Value;
|
||||
|
||||
var controlPanelOptions = provider.GetRequiredService<IOptions<ControlPanelConfiguration>>();
|
||||
controlPanelConfiguration = controlPanelOptions.Value;
|
||||
|
||||
ioManager = provider.GetRequiredService<IIOManager>();
|
||||
platformIdentifier = provider.GetRequiredService<IPlatformIdentifier>();
|
||||
}
|
||||
@@ -223,8 +227,9 @@ namespace Tgstation.Server.Host.Core
|
||||
options.SerializerSettings.Converters = new[] { new VersionConverter() };
|
||||
});
|
||||
|
||||
//enable CORS
|
||||
services.AddCors();
|
||||
//enable CORS if necessary
|
||||
if (controlPanelConfiguration.AllowAnyOrigin || controlPanelConfiguration.AllowedOrigins?.Count > 0)
|
||||
services.AddCors();
|
||||
|
||||
void AddTypedContext<TContext>() where TContext : DatabaseContext<TContext>
|
||||
{
|
||||
@@ -346,23 +351,21 @@ namespace Tgstation.Server.Host.Core
|
||||
|
||||
//Set up CORS based on configuration if necessary
|
||||
Action<CorsPolicyBuilder> corsBuilder = null;
|
||||
|
||||
void AllowAnyOrginCors(CorsPolicyBuilder builder) => builder.AllowAnyOrigin();
|
||||
void AllowConfiguredOriginsCors(CorsPolicyBuilder builder) => builder.WithOrigins();
|
||||
|
||||
if (controlPanelConfiguration.AllowAnyOrigin)
|
||||
corsBuilder = AllowAnyOrginCors;
|
||||
{
|
||||
logger.LogTrace("Access-Control-Allow-Origin: *");
|
||||
corsBuilder = builder => builder.AllowAnyOrigin();
|
||||
}
|
||||
else if (controlPanelConfiguration.AllowedOrigins?.Count > 0)
|
||||
corsBuilder = AllowConfiguredOriginsCors;
|
||||
{
|
||||
logger.LogTrace("Access-Control-Allow-Origin: ", String.Join(',', controlPanelConfiguration.AllowedOrigins));
|
||||
corsBuilder = builder => builder.WithOrigins(controlPanelConfiguration.AllowedOrigins.ToArray());
|
||||
}
|
||||
|
||||
if (corsBuilder != null)
|
||||
{
|
||||
var originalBuilder = corsBuilder;
|
||||
corsBuilder = builder =>
|
||||
{
|
||||
originalBuilder(builder);
|
||||
builder.AllowAnyHeader().AllowAnyMethod();
|
||||
};
|
||||
corsBuilder = builder => originalBuilder(builder.AllowAnyHeader().AllowAnyMethod());
|
||||
applicationBuilder.UseCors(corsBuilder);
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.Data.SqlClient;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@@ -327,8 +328,6 @@ namespace Tgstation.Server.Host.Core
|
||||
SetupWizardMode = SetupWizardMode.Never
|
||||
};
|
||||
|
||||
newGeneralConfiguration.UseWebControlPanel = await PromptYesNo("Enable the web control panel? (y/n): ", cancellationToken).ConfigureAwait(false);
|
||||
|
||||
do
|
||||
{
|
||||
await console.WriteAsync(null, true, cancellationToken).ConfigureAwait(false);
|
||||
@@ -434,7 +433,7 @@ namespace Tgstation.Server.Host.Core
|
||||
{
|
||||
await console.WriteAsync(null, true, cancellationToken).ConfigureAwait(false);
|
||||
await console.WriteAsync(question, true, cancellationToken).ConfigureAwait(false);
|
||||
await console.WriteAsync(String.Format(CultureInfo.InvariantCulture, "Enter one of {0}/{1}/{2}/{3}/{4}/{5} (leave blank for default): ",nameof(LogLevel.Trace), nameof(LogLevel.Debug), nameof(LogLevel.Information), nameof(LogLevel.Warning), nameof(LogLevel.Error), nameof(LogLevel.Critical)), false, cancellationToken).ConfigureAwait(false);
|
||||
await console.WriteAsync(String.Format(CultureInfo.InvariantCulture, "Enter one of {0}/{1}/{2}/{3}/{4}/{5} (leave blank for default): ", nameof(LogLevel.Trace), nameof(LogLevel.Debug), nameof(LogLevel.Information), nameof(LogLevel.Warning), nameof(LogLevel.Error), nameof(LogLevel.Critical)), false, cancellationToken).ConfigureAwait(false);
|
||||
var responseString = await console.ReadLineAsync(false, cancellationToken).ConfigureAwait(false);
|
||||
if (String.IsNullOrWhiteSpace(responseString))
|
||||
return null;
|
||||
@@ -450,6 +449,33 @@ namespace Tgstation.Server.Host.Core
|
||||
return fileLoggingConfiguration;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Prompts the user to create a <see cref="ControlPanelConfiguration"/>
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation</param>
|
||||
/// <returns>A <see cref="Task{TResult}"/> resulting in the new <see cref="ControlPanelConfiguration"/></returns>
|
||||
async Task<ControlPanelConfiguration> ConfigureControlPanel(CancellationToken cancellationToken)
|
||||
{
|
||||
var config = new ControlPanelConfiguration
|
||||
{
|
||||
Enable = await PromptYesNo("Enable the web control panel? (y/n): ", cancellationToken).ConfigureAwait(false),
|
||||
AllowAnyOrigin = await PromptYesNo("Allow web control panels hosted elsewhere to access the server? (Access-Control-Allow-Origin: *) (y/n): ", cancellationToken).ConfigureAwait(false)
|
||||
};
|
||||
|
||||
if (!config.AllowAnyOrigin)
|
||||
{
|
||||
await console.WriteAsync("Enter a comma seperated list of CORS allowed origins (optional): ", false, cancellationToken).ConfigureAwait(false);
|
||||
var commaSeperatedOrigins = await console.ReadLineAsync(false, cancellationToken).ConfigureAwait(false);
|
||||
if (!String.IsNullOrWhiteSpace(commaSeperatedOrigins))
|
||||
{
|
||||
var splits = commaSeperatedOrigins.Split(',');
|
||||
config.AllowedOrigins = new List<string>(splits.Select(x => x.Trim()));
|
||||
}
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves a given <see cref="Configuration"/> set to <paramref name="userConfigFileName"/>
|
||||
/// </summary>
|
||||
@@ -458,9 +484,10 @@ namespace Tgstation.Server.Host.Core
|
||||
/// <param name="databaseConfiguration">The <see cref="DatabaseConfiguration"/> to save</param>
|
||||
/// <param name="newGeneralConfiguration">The <see cref="GeneralConfiguration"/> to save</param>
|
||||
/// <param name="fileLoggingConfiguration">The <see cref="FileLoggingConfiguration"/> to save</param>
|
||||
/// <param name="controlPanelConfiguration">The <see cref="ControlPanelConfiguration"/> to save</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation</param>
|
||||
/// <returns>A <see cref="Task"/> representing the running operation</returns>
|
||||
async Task SaveConfiguration(string userConfigFileName, ushort? hostingPort, DatabaseConfiguration databaseConfiguration, GeneralConfiguration newGeneralConfiguration, FileLoggingConfiguration fileLoggingConfiguration, CancellationToken cancellationToken)
|
||||
async Task SaveConfiguration(string userConfigFileName, ushort? hostingPort, DatabaseConfiguration databaseConfiguration, GeneralConfiguration newGeneralConfiguration, FileLoggingConfiguration fileLoggingConfiguration, ControlPanelConfiguration controlPanelConfiguration, CancellationToken cancellationToken)
|
||||
{
|
||||
await console.WriteAsync(String.Format(CultureInfo.InvariantCulture, "Configuration complete! Saving to {0}", userConfigFileName), true, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
@@ -468,7 +495,8 @@ namespace Tgstation.Server.Host.Core
|
||||
{
|
||||
{ DatabaseConfiguration.Section, databaseConfiguration },
|
||||
{ GeneralConfiguration.Section, newGeneralConfiguration },
|
||||
{ FileLoggingConfiguration.Section, fileLoggingConfiguration }
|
||||
{ FileLoggingConfiguration.Section, fileLoggingConfiguration },
|
||||
{ ControlPanelConfiguration.Section, controlPanelConfiguration }
|
||||
};
|
||||
|
||||
if (hostingPort.HasValue)
|
||||
@@ -534,9 +562,11 @@ namespace Tgstation.Server.Host.Core
|
||||
|
||||
var fileLoggingConfiguration = await ConfigureLogging(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
var controlPanelConfiguration = await ConfigureControlPanel(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
await console.WriteAsync(null, true, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
await SaveConfiguration(userConfigFileName, hostingPort, databaseConfiguration, newGeneralConfiguration, fileLoggingConfiguration, cancellationToken).ConfigureAwait(false);
|
||||
await SaveConfiguration(userConfigFileName, hostingPort, databaseConfiguration, newGeneralConfiguration, fileLoggingConfiguration, controlPanelConfiguration, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
||||
Reference in New Issue
Block a user