Skip to content

Commit

Permalink
wip: more friendly startup process
Browse files Browse the repository at this point in the history
  • Loading branch information
GZTimeWalker committed Nov 6, 2022
1 parent ab9519a commit b15ba92
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 17 deletions.
4 changes: 2 additions & 2 deletions GZCTF.Test/CTFServer.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PackageReference Include="coverlet.collector" Version="3.2.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.msbuild" Version="3.1.2">
<PackageReference Include="coverlet.msbuild" Version="3.2.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
71 changes: 59 additions & 12 deletions GZCTF/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

Banner();

#region Directory

var uploadPath = Path.Combine(builder.Configuration.GetSection("UploadFolder").Value ?? "uploads");
Expand All @@ -39,6 +41,10 @@
builder.Logging.ClearProviders();
builder.Logging.SetMinimumLevel(LogLevel.Trace);
builder.Host.UseSerilog(dispose: true);
builder.Configuration.AddEnvironmentVariables("GZCTF_");
Log.Logger = LogHelper.GetInitLogger();

Log.Logger.Debug("GZCTF 正在启动中……");

#endregion Logging

Expand All @@ -51,6 +57,13 @@
}
else
{
if (!builder.Configuration.GetSection("ConnectionStrings").GetSection("Database").Exists())
{
Log.Logger.Fatal("未找到数据库连接字符串字段 ConnectionStrings,请检查 appsettings.json 是否正常挂载及配置");
Thread.Sleep(30000);
Environment.Exit(1);
}

builder.Services.AddDbContext<AppDbContext>(
options =>
{
Expand All @@ -72,19 +85,27 @@
builder.Host.ConfigureAppConfiguration((host, config) =>
{
config.AddJsonFile("ratelimit.json", optional: true, reloadOnChange: true);
config.AddEntityConfiguration(options =>
try
{
if (builder.Configuration.GetSection("ConnectionStrings").Exists())
options.UseNpgsql(builder.Configuration.GetConnectionString("Database"));
else
options.UseInMemoryDatabase("TestDb");
});
config.AddEntityConfiguration(options =>
{
if (builder.Configuration.GetSection("ConnectionStrings").Exists())
options.UseNpgsql(builder.Configuration.GetConnectionString("Database"));
else
options.UseInMemoryDatabase("TestDb");
});
}
catch
{
Log.Logger.Fatal("数据库连接失败,请检查 Database 连接字符串配置");
Thread.Sleep(30000);
Environment.Exit(1);
}
});
}
#endregion Configuration

#region OpenApiDocument

builder.Services.AddRouting(options => options.LowercaseUrls = true);
builder.Services.AddOpenApiDocument(settings =>
{
Expand All @@ -99,7 +120,6 @@
});
settings.DefaultReferenceTypeNullHandling = ReferenceTypeNullHandling.NotNull;
});

#endregion OpenApiDocument

#region SignalR
Expand Down Expand Up @@ -265,13 +285,16 @@ await context.Posts.AddAsync(new()
await context.SaveChangesAsync();
}

// only for testing
if (app.Environment.IsDevelopment())
if (app.Environment.IsDevelopment() || app.Configuration.GetSection("ADMIN_PASSWORD").Exists())
{
var usermanager = serviceScope.ServiceProvider.GetRequiredService<UserManager<UserInfo>>();
var admin = await usermanager.FindByNameAsync("Admin");

if (admin is null)
{
var password = app.Environment.IsDevelopment() ? "Admin@2022" :
app.Configuration.GetValue<string>("ADMIN_PASSWORD");

admin = new UserInfo
{
UserName = "Admin",
Expand All @@ -280,7 +303,7 @@ await context.Posts.AddAsync(new()
EmailConfirmed = true,
RegisterTimeUTC = DateTimeOffset.UtcNow
};
await usermanager.CreateAsync(admin, "Admin@2022");
await usermanager.CreateAsync(admin, password);
}
}
}
Expand Down Expand Up @@ -340,7 +363,7 @@ await context.Posts.AddAsync(new()
}
catch (Exception exception)
{
logger.LogError(exception, "因异常,应用程序意外停止");
logger.LogError(exception, "因异常,应用程序意外终止");
throw;
}
finally
Expand All @@ -352,4 +375,28 @@ await context.Posts.AddAsync(new()
public partial class Program
{
public static bool IsTesting { get; set; } = false;

public static void Banner()
{
const string banner =
@" ___ ___ ___ ___ " + "\n" +
@" / /\ / /\ / /\ ___ / /\ " + "\n" +
@" / /:/_ / /::| / /:/ / /\ / /:/_ " + "\n" +
@" / /:/ /\ / /:/:| / /:/ / /:/ / /:/ /\" + "\n" +
@" / /:/_/::\ / /:/|:|__ / /:/ ___ / /:/ / /:/ /:/" + "\n" +
@" /__/:/__\/\:\ /__/:/ |:| /\ /__/:/ / /\ / /::\ /__/:/ /:/ " + "\n" +
@" \ \:\ /~~/:/ \__\/ |:|/:/ \ \:\ / /:/ /__/:/\:\ \ \:\/:/ " + "\n" +
@" \ \:\ /:/ | |:/:/ \ \:\ /:/ \__\/ \:\ \ \::/ " + "\n" +
@" \ \:\/:/ | |::/ \ \:\/:/ \ \:\ \ \:\ " + "\n" +
@" \ \::/ | |:/ \ \::/ \__\/ \ \:\ " + "\n" +
@" \__\/ |__|/ \__\/ \__\/ " + "\n";
Console.WriteLine(banner);

var versionStr = "";
var version = typeof(Codec).Assembly.GetName().Version;
if (version is not null)
versionStr = $"Version: {version.Major}.{version.Minor}.{version.Build}";

Console.WriteLine($"GZCTF © 2022-present GZTimeWalker {versionStr,33}\n");
}
}
16 changes: 15 additions & 1 deletion GZCTF/Utils/LogHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,20 @@ public static void Log<T>(this ILogger<T> _logger, string msg, string uname, str

private const string LogTemplate = "[{@t:yy-MM-dd HH:mm:ss.fff} {@l:u3}] {Substring(SourceContext, LastIndexOf(SourceContext, '.') + 1)}: {@m} {#if Length(Status) > 0}#{Status} <{UserName}>{#if Length(IP) > 0}@{IP}{#end}{#end}\n{@x}";

public static Logger GetInitLogger()
=> new LoggerConfiguration()
.Enrich.FromLogContext()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("AspNetCoreRateLimit", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information)
.MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Warning)
.WriteTo.Async(t => t.Console(
formatter: new ExpressionTemplate(LogTemplate, theme: TemplateTheme.Literate),
restrictedToMinimumLevel: LogEventLevel.Debug
))
.CreateLogger();

public static Logger GetLogger(IConfiguration configuration, IServiceProvider serviceProvider)
=> new LoggerConfiguration()
.Enrich.FromLogContext()
Expand Down Expand Up @@ -141,4 +155,4 @@ public TimeColumnWriter() : base(NpgsqlDbType.TimestampTz)

public override object GetValue(LogEvent logEvent, IFormatProvider? formatProvider = null)
=> logEvent.Timestamp.ToUniversalTime();
}
}
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,12 @@ docker pull ghcr.io/gztimewalker/gzctf/gzctf:latest

### 初始管理员

生产环境中默认不存在管理员权限用户,需要手动更改数据库条目。当管理员注册完成并成功登录后,进入所选数据库表后执行:
生产环境中默认不存在管理员权限用户,需要在首次启动时设置 `GZCTF_ADMIN_PASSWORD` 环境变量来设置初始管理员密码,并通过 `Admin` 账号登录。

你也可以通过手动更改数据库条目来将当前已注册的用户设置为管理员。当管理员注册完成并成功登录后,进入所选数据库表后执行:

```sql
update "AspNetUsers" set "Role"=3;
UPDATE "AspNetUsers" SET "Role"=3 WHERE "UserName"='some_user_name';
```

### 端口暴露范围设置
Expand Down
2 changes: 2 additions & 0 deletions scripts/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ services:
gzctf:
image: gztime/gzctf:latest
restart: always
# environment:
# - "GZCTF_ADMIN_PASSWORD=Your_Password_Here"
ports:
- "80:80"
networks:
Expand Down

0 comments on commit b15ba92

Please sign in to comment.