improve logging, various qol fixes

This commit is contained in:
Mikhail
2024-08-31 12:39:47 -04:00
parent f199ca63e0
commit a68a201c13
10 changed files with 210 additions and 9 deletions

View File

@@ -1,6 +1,7 @@
using EpinelPS.Database;
using EpinelPS.Utils;
using Microsoft.AspNetCore.Mvc;
using Org.BouncyCastle.Ocsp;
namespace EpinelPS.Controllers
{
@@ -44,8 +45,12 @@ namespace EpinelPS.Controllers
[HttpPost]
[Route("auth/auto_login")]
public string AutoLogin(string seq)
public string AutoLogin(string seq, [FromBody] AuthPkt2 req)
{
User? user;
if ((user = NetUtils.GetUser(req.token).Item1) == null) return BadAuthToken;
return "{\"del_account_info\":\"{\\\"ret\\\":0,\\\"msg\\\":\\\"\\\",\\\"status\\\":0,\\\"created_at\\\":\\\"0\\\",\\\"target_destroy_at\\\":\\\"0\\\",\\\"destroyed_at\\\":\\\"0\\\",\\\"err_code\\\":0,\\\"seq\\\":\\\"" + seq + "\\\"}\",\"del_account_status\":0,\"del_li_account_status\":0,\"extra_json\":{\"del_li_account_info\":\"{\\\"ret\\\":0,\\\"msg\\\":\\\"\\\",\\\"status\\\":0,\\\"created_at\\\":\\\"0\\\",\\\"target_destroy_at\\\":\\\"0\\\",\\\"destroyed_at\\\":\\\"0\\\",\\\"err_code\\\":0,\\\"seq\\\":\\\"" + seq + "\\\"}\",\"get_status_msg\":\"success\",\"get_status_ret\":0,\"get_status_rsp\":{\"adult_age\":14,\"adult_age_map\":{},\"adult_check_status\":1,\"adult_check_status_expiration\":\"0\",\"adult_status_map\":{},\"certificate_type\":3,\"email\":\"\",\"eu_user_agree_status\":0,\"game_grade\":0,\"game_grade_map\":{},\"is_dma\":true,\"is_eea\":false,\"is_need_li_cert\":false,\"msg\":\"success\",\"need_parent_control\":0,\"need_realname_auth\":0,\"parent_certificate_status\":0,\"parent_certificate_status_expiration\":\"0\",\"parent_control_map\":{},\"qr_code_ret\":0,\"realname_auth_status\":0,\"region\":\"724\",\"ret\":0,\"ts\":\"" + DateTimeOffset.UtcNow.ToUnixTimeSeconds()
+ "\"},\"need_notify_msg\":\"success\",\"need_notify_ret\":0,\"need_notify_rsp\":{\"has_bind_li\":true,\"is_receive_email\":1,\"is_receive_email_in_night\":0,\"user_agreed_game_dma\":\"2\",\"user_agreed_game_pp\":\"1\",\"user_agreed_game_tos\":\"1\",\"user_agreed_li_dt\":\"\",\"user_agreed_li_pp\":\"1\",\"user_agreed_li_tos\":\"\"}},\"msg\":\"success\",\"ret\":0,\"seq\":\"" + seq + "\"}";
}
@@ -118,12 +123,24 @@ namespace EpinelPS.Controllers
[HttpPost]
[Route("profile/get_bind_info")]
public string GetProfileBindInfo(string seq, [FromBody] AuthPkt req)
public string GetProfileBindInfo(string seq, [FromBody] AuthPkt2 req)
{
User? user;
if ((user = NetUtils.GetUser(req.channel_info.token).Item1) == null) return BadAuthToken;
if ((user = NetUtils.GetUser(req.token).Item1) == null) return BadAuthToken;
return "{\"bind_list\":[{\"bind_ts\":1717783095,\"channel_info\":{\"birthday\":\"1970-01\",\"email\":\"" + user.Username + "\",\"is_receive_email\":1,\"lang_type\":\"en\",\"last_login_time\":171000000,\"nick_name\":\"\",\"phone\":\"\",\"phone_area_code\":\"\",\"region\":\"724\",\"register_account\":\"" + user.Username + "\",\"register_account_type\":1,\"register_time\":" + user.RegisterTime + ",\"seq\":\"" + seq + "\",\"uid\":\"2752409592679849\",\"user_name\":\"" + user.PlayerName + "\",\"username_pass_verify\":0},\"channelid\":131,\"email\":\"" + user.Username + "\",\"history_scopes\":[],\"is_primary\":1,\"picture_url\":\"\",\"user_name\":\"" + user.PlayerName + "\"}],\"create_ts\":" + user.RegisterTime + ",\"last_login_ts\":171000000,\"msg\":\"success\",\"ret\":0,\"seq\":\"" + seq + "\"}";
}
[HttpPost]
[Route("auth/refresh_sacc_token")]
public string RefreshAuthToken(string seq, [FromBody] AuthPkt2 req)
{
// TODO redo auth token system
AccessToken? user;
if ((user = NetUtils.GetUser(req.token).Item2) == null) return BadAuthToken;
user.ExpirationTime = DateTimeOffset.UtcNow.AddYears(1).ToUnixTimeSeconds();
return "{\"msg\":\"success\",\"ret\":0,\"seq\":\"" + seq + "\"}";
}
}
}

View File

@@ -1,5 +1,6 @@
using EpinelPS.LobbyServer;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
namespace EpinelPS.Controllers
{
@@ -12,7 +13,16 @@ namespace EpinelPS.Controllers
[Consumes("application/octet-stream+protobuf")]
public async Task CatchAll(string all)
{
Stopwatch st = Stopwatch.StartNew();
await LobbyHandler.DispatchSingle(HttpContext);
st.Stop();
var fg = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("POST " + HttpContext.Request.Path.Value + " completed in " + st.Elapsed);
Console.ForegroundColor = fg;
}
}
}

View File

@@ -237,11 +237,12 @@ namespace EpinelPS.StaticInfo
if (records == null) throw new Exception(entry + " is missing records element");
currentFile++;
bar.Report((double)currentFile / totalFiles);
return records;
}
int totalFiles = 14;
int totalFiles = 78;
int currentFile = 0;
public async Task Parse()

View File

@@ -0,0 +1,20 @@
using EpinelPS.Utils;
namespace EpinelPS.LobbyServer.Msgs.User
{
[PacketPath("/lobby/usertitle/acquire")]
public class AquireUserTitle : LobbyMsgHandler
{
protected override async Task HandleAsync()
{
var req = await ReadData<ReqAcquireUserTitle>();
var user = GetUser();
var response = new ResAcquireUserTitle();
// TODO
await WriteDataAsync(response);
}
}
}

View File

@@ -0,0 +1,20 @@
using EpinelPS.Utils;
namespace EpinelPS.LobbyServer.Msgs.User
{
[PacketPath("/lobby/usertitle/unmark-badge")]
public class UnmarkUserTitleBase : LobbyMsgHandler
{
protected override async Task HandleAsync()
{
var req = await ReadData<ReqUnMarkUserTitleBadge>();
var user = GetUser();
var response = new ResUnMarkUserTitleBadge();
// TODO
await WriteDataAsync(response);
}
}
}

View File

@@ -1,9 +1,13 @@
using EpinelPS.Database;
using DnsClient;
using EpinelPS.Database;
using EpinelPS.LobbyServer;
using EpinelPS.LobbyServer.Msgs.Stage;
using EpinelPS.StaticInfo;
using EpinelPS.Utils;
using Google.Api;
using Microsoft.AspNetCore.Server.Kestrel.Https;
using Microsoft.Extensions.Logging.EventLog;
using Microsoft.VisualBasic;
using System.Net;
using System.Net.Http.Headers;
using System.Security.Cryptography.X509Certificates;
@@ -50,6 +54,7 @@ namespace EpinelPS
serverOptions.AllowSynchronousIO = true;
});
// Add services to the container.
builder.Services.AddControllersWithViews();
@@ -57,6 +62,16 @@ namespace EpinelPS
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddRouting();
builder.Logging.ClearProviders();
builder.Logging.AddColorConsoleLogger(configuration =>
{
// Replace warning value from appsettings.json of "Cyan"
configuration.LogLevelToColorMap[LogLevel.Warning] = ConsoleColor.Yellow;
// Replace warning value from appsettings.json of "Red"
configuration.LogLevelToColorMap[LogLevel.Error] = ConsoleColor.DarkRed;
});
var app = builder.Build();
app.UseDefaultFiles();
@@ -87,7 +102,7 @@ namespace EpinelPS
// NOTE: pub prefixes shows public (production server), local is local server (does not have any effect), dev is development server, etc.
// It does not have any effect, except for the publisher server, which adds a watermark?
app.MapGet("/route/*/route_config.json", () => @"{
app.MapGet("/route/{**all}", () => @"{
""Config"": [
{
""VersionRange"": {
@@ -499,7 +514,11 @@ namespace EpinelPS
return item.Value.ReturnBytes;
}
}
var fg = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("HANDLER NOT FOUND: " + url);
Console.ForegroundColor = fg;
return null;
}
}

View File

@@ -2,11 +2,11 @@
"profiles": {
"EpinelPS": {
"commandName": "Project",
"launchBrowser": true,
"launchBrowser": false,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:64668;http://localhost:64669"
"applicationUrl": ""
}
}
}

View File

@@ -0,0 +1,32 @@
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging.Configuration;
namespace EpinelPS.Utils
{
public static class ColorConsoleLoggerExtensions
{
public static ILoggingBuilder AddColorConsoleLogger(
this ILoggingBuilder builder)
{
builder.AddConfiguration();
builder.Services.TryAddEnumerable(
ServiceDescriptor.Singleton<ILoggerProvider, ColorConsoleLoggerProvider>());
LoggerProviderOptions.RegisterProviderOptions
<ColorConsoleLoggerConfiguration, ColorConsoleLoggerProvider>(builder.Services);
return builder;
}
public static ILoggingBuilder AddColorConsoleLogger(
this ILoggingBuilder builder,
Action<ColorConsoleLoggerConfiguration> configure)
{
builder.AddColorConsoleLogger();
builder.Services.Configure(configure);
return builder;
}
}
}

View File

@@ -0,0 +1,82 @@
using Microsoft.Extensions.Options;
using System.Collections.Concurrent;
using System.Runtime.Versioning;
namespace EpinelPS.Utils
{
[UnsupportedOSPlatform("browser")]
[ProviderAlias("ColorConsole")]
public sealed class ColorConsoleLoggerProvider : ILoggerProvider
{
private readonly IDisposable? _onChangeToken;
private ColorConsoleLoggerConfiguration _currentConfig;
private readonly ConcurrentDictionary<string, ColorConsoleLogger> _loggers =
new(StringComparer.OrdinalIgnoreCase);
public ColorConsoleLoggerProvider(
IOptionsMonitor<ColorConsoleLoggerConfiguration> config)
{
_currentConfig = config.CurrentValue;
_onChangeToken = config.OnChange(updatedConfig => _currentConfig = updatedConfig);
}
public ILogger CreateLogger(string categoryName) =>
_loggers.GetOrAdd(categoryName, name => new ColorConsoleLogger(name, GetCurrentConfig));
private ColorConsoleLoggerConfiguration GetCurrentConfig() => _currentConfig;
public void Dispose()
{
_loggers.Clear();
_onChangeToken?.Dispose();
}
}
public sealed class ColorConsoleLoggerConfiguration
{
public int EventId { get; set; }
public Dictionary<LogLevel, ConsoleColor> LogLevelToColorMap { get; set; } = new()
{
[LogLevel.Information] = ConsoleColor.Green
};
}
public sealed class ColorConsoleLogger(
string name,
Func<ColorConsoleLoggerConfiguration> getCurrentConfig) : ILogger
{
public IDisposable? BeginScope<TState>(TState state) where TState : notnull => default!;
public bool IsEnabled(LogLevel logLevel) =>
getCurrentConfig().LogLevelToColorMap.ContainsKey(logLevel);
public void Log<TState>(
LogLevel logLevel,
EventId eventId,
TState state,
Exception? exception,
Func<TState, Exception?, string> formatter)
{
if (!IsEnabled(logLevel))
{
return;
}
ColorConsoleLoggerConfiguration config = getCurrentConfig();
if (config.EventId == 0 || config.EventId == eventId.Id)
{
ConsoleColor originalColor = Console.ForegroundColor;
string msg = formatter(state, exception).Replace("Request reached the end of the middleware pipeline without being handled by application code. Request path: ", "");
if (msg.StartsWith("Executing ObjectResult") || msg.StartsWith("Executed endpoint") || msg.StartsWith("Route matched with ") || msg.Contains("CatchAll") || msg.Contains("$batch"))
return;
Console.ForegroundColor = config.LogLevelToColorMap[logLevel];
Console.Write($"{msg}");
Console.ForegroundColor = originalColor;
Console.WriteLine();
}
}
}
}

View File

@@ -15,7 +15,7 @@
<li>Updated game version to 124.6.10I</li>
<li>Improved server selector</li>
<li>Added ability to not download assets through the private server</li>
<li>ASP.NET is now used instead of EmbedIO (should provide better preformance)</li>
<li>ASP.NET is now used instead of EmbedIO (should provide better performance)</li>
<li>Rewrote admin panel</li>
</ul>
</body>