Update static data, and don't hard code it

This commit is contained in:
Mikhail
2024-07-12 14:01:38 -04:00
parent 618619e36d
commit 3865b403b4
13 changed files with 115 additions and 44 deletions

View File

@@ -19,6 +19,7 @@ namespace nksrv.IntlServer
protected override async Task HandleAsync()
{
if (ctx == null) throw new Exception("ctx cannot be null");
Console.WriteLine("li-sg redirect in: " + Content);
HttpClientHandler handler = new()
{

View File

@@ -19,18 +19,15 @@ namespace nksrv.IntlServer
protected override async Task HandleAsync()
{
RegisterEPReq? ep = JsonConvert.DeserializeObject<RegisterEPReq>(Content);
if (ep != null)
{
string? seg = ctx.GetRequestQueryData().Get("seq");
// check if the account already exists
foreach (var item in JsonDb.Instance.Users)
{
if (item.Username == ep.account)
{
await WriteJsonStringAsync("{\"msg\":\"send code failed; invalid account\",\"ret\":2112,\"seq\":\"" + seg + "\"}");
await WriteJsonStringAsync("{\"msg\":\"send code failed; invalid account\",\"ret\":2112,\"seq\":\"" + Seq + "\"}");
return;
}
}
@@ -51,7 +48,7 @@ namespace nksrv.IntlServer
JsonDb.Instance.Users.Add(user);
var tok = IntlHandler.CreateLauncherTokenForUser(user);
await WriteJsonStringAsync("{\"expire\":" + tok.ExpirationTime + ",\"is_login\":false,\"msg\":\"Success\",\"register_time\":" + user.RegisterTime + ",\"ret\":0,\"seq\":\"" + seg + "\",\"token\":\"" + tok.Token + "\",\"uid\":\"" + user.ID + "\"}");
await WriteJsonStringAsync("{\"expire\":" + tok.ExpirationTime + ",\"is_login\":false,\"msg\":\"Success\",\"register_time\":" + user.RegisterTime + ",\"ret\":0,\"seq\":\"" + Seq + "\",\"token\":\"" + tok.Token + "\",\"uid\":\"" + user.ID + "\"}");
}
else
{

View File

@@ -33,9 +33,7 @@ namespace nksrv.IntlServer
}
}
string? seg = ctx.GetRequestQueryData().Get("seq");
await WriteJsonStringAsync("{\"msg\":\"the account does not exists!\",\"ret\":2001,\"seq\":\"" + seg + "\"}");
await WriteJsonStringAsync("{\"msg\":\"the account does not exists!\",\"ret\":2001,\"seq\":\"" + Seq + "\"}");
}
else
{

View File

@@ -12,17 +12,17 @@ namespace nksrv.IntlServer
{
public abstract class IntlMsgHandler
{
protected IHttpContext ctx;
protected IHttpContext? ctx;
protected string Content = "";
protected User? User;
protected string? Seq;
protected string Seq = "";
protected AccessToken? UsedToken;
public abstract bool RequiresAuth { get; }
public async Task HandleAsync(IHttpContext ctx)
{
this.ctx = ctx;
Content = await ctx.GetRequestBodyAsStringAsync();
Seq = ctx.GetRequestQueryData().Get("seq");
Seq = ctx.GetRequestQueryData().Get("seq") ?? "";
if (RequiresAuth)
{
var x = JsonConvert.DeserializeObject<AuthPkt>(Content);
@@ -74,7 +74,6 @@ namespace nksrv.IntlServer
await HandleAsync();
}
protected abstract Task HandleAsync();
protected async Task WriteJsonStringAsync(string data)
{
if (ctx != null)
@@ -83,10 +82,11 @@ namespace nksrv.IntlServer
ctx.Response.ContentEncoding = null;
ctx.Response.ContentType = "application/json";
ctx.Response.ContentLength64 = bt.Length;
await ctx.Response.OutputStream.WriteAsync(bt, 0, bt.Length, ctx.CancellationToken);
await ctx.Response.OutputStream.WriteAsync(bt, ctx.CancellationToken);
await ctx.Response.OutputStream.FlushAsync();
}
}
public class ChannelInfo
{
public string openid { get; set; } = "";

View File

@@ -20,10 +20,7 @@ namespace nksrv.IntlServer
protected override async Task HandleAsync()
{
var str = await ctx.GetRequestBodyAsStringAsync();
string? seg = ctx.GetRequestQueryData().Get("seq");
await WriteJsonStringAsync(JsonToReturn.Replace("((SEGID))", seg));
await WriteJsonStringAsync(JsonToReturn.Replace("((SEGID))", Seq));
}
}
}

View File

@@ -22,21 +22,19 @@ namespace nksrv.IntlServer
SendCodeRequest? ep = JsonConvert.DeserializeObject<SendCodeRequest>(Content);
if (ep != null)
{
string? seg = ctx.GetRequestQueryData().Get("seq");
// check if the account already exists
foreach (var item in JsonDb.Instance.Users)
{
if (item.Username == ep.account)
{
await WriteJsonStringAsync("{\"msg\":\"send code failed; invalid account\",\"ret\":2112,\"seq\":\"" + seg + "\"}");
await WriteJsonStringAsync("{\"msg\":\"send code failed; invalid account\",\"ret\":2112,\"seq\":\"" + Seq + "\"}");
return;
}
}
// pretend that we sent the code
await WriteJsonStringAsync("{\"expire_time\":898,\"msg\":\"Success\",\"ret\":0,\"seq\":\"" + seg + "\"}");
await WriteJsonStringAsync("{\"expire_time\":898,\"msg\":\"Success\",\"ret\":0,\"seq\":\"" + Seq + "\"}");
}
else
{

View File

@@ -15,9 +15,9 @@ namespace nksrv.LobbyServer.Msgs.Misc
var req = await ReadData<ResourceHostRequest>();
var r = new ResourceHostResponse();
r.BaseUrl = "https://cloud.nikke-kr.com/prdenv/122-b0255105e0/{Platform}";
await WriteDataAsync(r);
r.BaseUrl = GameConfig.Root.ResourceBaseURL;
await WriteDataAsync(r);
}
}
}

View File

@@ -12,16 +12,14 @@ namespace nksrv.LobbyServer.Msgs.Misc
var req = await ReadData<StaticDataPackRequest>();
var r = new StaticDataPackResponse();
r.Url = StaticDataParser.StaticDataUrl;
r.Version = StaticDataParser.Version;
r.Size = StaticDataParser.Size;
r.Url = GameConfig.Root.StaticData.Url;
r.Version = GameConfig.Root.StaticData.Version;
r.Size = StaticDataParser.Instance.Size;
r.Sha256Sum = ByteString.CopyFrom(StaticDataParser.Instance.Sha256Hash);
r.Salt1 = ByteString.CopyFrom(Convert.FromBase64String(GameConfig.Root.StaticData.Salt1));
r.Salt2 = ByteString.CopyFrom(Convert.FromBase64String(GameConfig.Root.StaticData.Salt2));
// TODO: Read the file and compute these values
r.Sha256Sum = ByteString.CopyFrom(StaticDataParser.Sha256Sum);
r.Salt1 = ByteString.CopyFrom(StaticDataParser.Salt1);
r.Salt2 = ByteString.CopyFrom(StaticDataParser.Salt2);
await WriteDataAsync(r);
await WriteDataAsync(r);
}
}
}

View File

@@ -12,6 +12,7 @@ using ICSharpCode.SharpZipLib.Zip;
using Newtonsoft.Json.Linq;
using Swan.Parsers;
using Newtonsoft.Json;
using System.Drawing;
namespace nksrv.StaticInfo
{
@@ -20,14 +21,6 @@ namespace nksrv.StaticInfo
/// </summary>
public class StaticDataParser
{
// Extracted from staticinfo api call
public const string StaticDataUrl = "https://cloud.nikke-kr.com/prdenv/122-c8cee37754/staticdata/data/qa-240704-07b/312528/StaticData.pack";
public const string Version = "data/qa-240704-07b/312528";
public const int Size = 11799792;
public static byte[] Sha256Sum = Convert.FromBase64String("Wzy+AcGutLR6z1yM7lp+UpFkNuErf56Aj6e9taGH8j4=");
public static byte[] Salt1 = Convert.FromBase64String("vZ3Nv6JwfaZJpHwmUc0kyV7Q3Yzm8ysPhyVE0R0GVTc=");
public static byte[] Salt2 = Convert.FromBase64String("L29mjnvnlktQ1vLq+E56FkRECojiaHx9UmWzsurBfIU=");
// These fields were extracted from the game.
public static byte[] PresharedKey = [0xCB, 0xC2, 0x1C, 0x6F, 0xF3, 0xF5, 0x07, 0xF5, 0x05, 0xBA, 0xCA, 0xD4, 0x98, 0x28, 0x84, 0x1F, 0xF0, 0xD1, 0x38, 0xC7, 0x61, 0xDF, 0xD6, 0xE6, 0x64, 0x9A, 0x85, 0x13, 0x3E, 0x1A, 0x6A, 0x0C, 0x68, 0x0E, 0x2B, 0xC4, 0xDF, 0x72, 0xF8, 0xC6, 0x55, 0xE4, 0x7B, 0x14, 0x36, 0x18, 0x3B, 0xA7, 0xD1, 0x20, 0x81, 0x22, 0xD1, 0xA9, 0x18, 0x84, 0x65, 0x13, 0x0B, 0xED, 0xA3, 0x00, 0xE5, 0xD9];
public static RSAParameters RSAParameters = new RSAParameters()
@@ -62,6 +55,9 @@ namespace nksrv.StaticInfo
private JArray characterTable;
private JArray tutorialTable;
public byte[] Sha256Hash;
public int Size;
static async Task<StaticDataParser> BuildAsync()
{
Logger.Info("Loading static data");
@@ -69,6 +65,7 @@ namespace nksrv.StaticInfo
Logger.Info("Parsing static data");
await Instance.Parse();
return Instance;
}
@@ -87,6 +84,11 @@ namespace nksrv.StaticInfo
ZipStream = new();
tutorialTable = new();
var rawBytes = File.ReadAllBytes(filePath);
Sha256Hash = SHA256.HashData(rawBytes);
Size = rawBytes.Length;
DecryptStaticDataAndLoadZip(filePath);
if (MainZip == null) throw new Exception("failed to read zip file");
}
@@ -95,7 +97,7 @@ namespace nksrv.StaticInfo
{
using var fileStream = File.Open(file, FileMode.Open, FileAccess.Read);
var keyDecryptor = new Rfc2898DeriveBytes(PresharedKey, Salt2, 10000, HashAlgorithmName.SHA256);
var keyDecryptor = new Rfc2898DeriveBytes(PresharedKey, GameConfig.Root.StaticData.GetSalt2Bytes(), 10000, HashAlgorithmName.SHA256);
var key2 = keyDecryptor.GetBytes(32);
byte[] decryptionKey = key2[0..16];
@@ -141,7 +143,7 @@ namespace nksrv.StaticInfo
dataMs.Position = 0;
// Decryption of layer 3
var keyDecryptor2 = new Rfc2898DeriveBytes(PresharedKey, Salt1, 10000, HashAlgorithmName.SHA256);
var keyDecryptor2 = new Rfc2898DeriveBytes(PresharedKey, GameConfig.Root.StaticData.GetSalt1Bytes(), 10000, HashAlgorithmName.SHA256);
var key3 = keyDecryptor2.GetBytes(32);
byte[] decryptionKey2 = key3[0..16];
@@ -208,7 +210,7 @@ namespace nksrv.StaticInfo
}
public static async Task Load()
{
var targetFile = await AssetDownloadUtil.DownloadOrGetFileAsync(StaticDataUrl, CancellationToken.None);
var targetFile = await AssetDownloadUtil.DownloadOrGetFileAsync(GameConfig.Root.StaticData.Url, CancellationToken.None);
if (targetFile == null) throw new Exception("static data download fail");
_instance = new(targetFile);

View File

@@ -45,6 +45,7 @@ namespace nksrv.Utils
}
else
{
Logger.Error("Failed to download " + url + " with status code " + response.StatusCode);
return null;
}
}

64
nksrv/Utils/GameConfig.cs Normal file
View File

@@ -0,0 +1,64 @@
using Newtonsoft.Json;
using Swan.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace nksrv.Utils
{
public class GameConfigRoot
{
public StaticData StaticData { get; set; } = new();
public string ResourceBaseURL { get; set; } = "";
}
public class StaticData
{
public string Url { get; set; } = "";
public string Version { get; set; } = "";
public string Salt1 { get; set; } = "";
public string Salt2 { get; set; } = "";
public byte[] GetSalt1Bytes()
{
return Convert.FromBase64String(Salt1);
}
public byte[] GetSalt2Bytes()
{
return Convert.FromBase64String(Salt2);
}
}
public static class GameConfig
{
private static GameConfigRoot? _root;
public static GameConfigRoot Root
{
get
{
if (_root == null)
{
if (!File.Exists(AppDomain.CurrentDomain.BaseDirectory + "/gameconfig.json"))
{
Logger.Error("Gameconfig.json is not found, the game WILL NOT work!");
_root = new GameConfigRoot();
}
Logger.Info("Loaded game config");
_root = JsonConvert.DeserializeObject<GameConfigRoot>(File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "/gameconfig.json"));
if (_root == null)
{
throw new Exception("Failed to read gameconfig.json");
}
}
return _root;
}
}
}
}

12
nksrv/gameconfig.json Normal file
View File

@@ -0,0 +1,12 @@
{
// Extracted from POST https://global-lobby.nikke-kr.com/v1/staticdatapack
"StaticData": {
"Url": "https://cloud.nikke-kr.com/prdenv/122-c8cee37754/staticdata/data/qa-240704-07b/313275/StaticData.pack",
"Version": "data/qa-240704-07b/313275",
"Salt1": "7OpvuafRK67Rf0X2VJrzIAqZ0CBPbY4IWWdtbQ3LyV8=",
"Salt2": "zR7nPjsRCPUfN9BViVkk5R/KOCkVimb8VSE+yOqey+g="
},
// Extracted from POST https://global-lobby.nikke-kr.com/v1/resourcehosts2
"ResourceBaseURL": "https://cloud.nikke-kr.com/prdenv/122-b0255105e0/{Platform}"
}

View File

@@ -29,6 +29,9 @@
</ItemGroup>
<ItemGroup>
<None Update="gameconfig.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="site.pfx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>