refactoring

This commit is contained in:
Mikhail Thompson
2024-06-28 21:13:21 +03:00
parent c7250cdf24
commit 1229104086
10 changed files with 228 additions and 310 deletions

View File

@@ -28,7 +28,7 @@ namespace nksrv.LobbyServer
UserId = 0;
ctx = null;
}
public async Task HandleAsync(IHttpContext ctx)
{
this.ctx = ctx;
@@ -93,7 +93,7 @@ namespace nksrv.LobbyServer
{
if (ctx == null)
{
T msg2 = new T();
T msg2 = new();
msg2.MergeFrom(Contents);
return msg2;
}
@@ -102,16 +102,8 @@ namespace nksrv.LobbyServer
var bin = await PacketDecryption.DecryptOrReturnContentAsync(ctx);
// return grpc IMessage from byte array with type T
T msg = default(T);
try
{
msg = new T();
msg.MergeFrom(bin.Contents);
}
catch
{
;
}
T msg = new();
msg.MergeFrom(bin.Contents);
UserId = bin.UserId;
UsedAuthToken = bin.UsedAuthToken;
@@ -122,9 +114,7 @@ namespace nksrv.LobbyServer
public User GetUser()
{
User? user = JsonDb.GetUser(UserId);
if (user == null) throw new Exception("null user");
return user;
return JsonDb.GetUser(UserId) ?? throw new Exception("null user");
}
}
}

View File

@@ -24,7 +24,7 @@ namespace nksrv.LobbyServer.Msgs
AllowAutoRedirect = true // from gameassembly dll
};
HttpClient client = new HttpClient(new LoggingHandler(handler));
HttpClient client = new(new LoggingHttpHandler(handler));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/octet-stream+protobuf"));
client.BaseAddress = new Uri("https://global-lobby.nikke-kr.com");
client.DefaultRequestVersion = HttpVersion.Version20;
@@ -47,38 +47,21 @@ namespace nksrv.LobbyServer.Msgs
ctx.Response.OutputStream.Flush();
}
public static async Task<PacketDecryptResponse> DecryptOrReturnContentAsync(IHttpContext ctx, bool decompress = false)
public static async Task<PacketDecryptResponse> DecryptOrReturnContentAsync(IHttpContext ctx)
{
byte[] bin = Array.Empty<byte>();
using MemoryStream buffer = new MemoryStream();
var stream = ctx.Request.InputStream;
var encoding = ctx.Request.Headers[HttpHeaderNames.ContentEncoding]?.Trim();
Stream decryptedStream;
switch (encoding)
Stream decryptedStream = encoding switch
{
case CompressionMethodNames.Gzip:
decryptedStream = new GZipStream(stream, CompressionMode.Decompress);
break;
case CompressionMethodNames.Deflate:
decryptedStream = new DeflateStream(stream, CompressionMode.Decompress);
break;
case CompressionMethodNames.None:
case null:
decryptedStream = stream;
break;
case "gzip,enc":
decryptedStream = stream;
break;
default:
throw HttpException.BadRequest($"Unsupported content encoding \"{encoding}\"");
}
CompressionMethodNames.Gzip => new GZipStream(stream, CompressionMode.Decompress),
CompressionMethodNames.Deflate => new DeflateStream(stream, CompressionMode.Decompress),
CompressionMethodNames.None or null => stream,
"gzip,enc" => stream,
_ => throw HttpException.BadRequest($"Unsupported content encoding \"{encoding}\""),
};
await stream.CopyToAsync(buffer, 81920, ctx.CancellationToken).ConfigureAwait(continueOnCapturedContext: false);
return new PacketDecryptResponse() { Contents = buffer.ToArray() };
}