Skip to content

Commit

Permalink
fix(web): wrong d2g response
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaojiuwo1993 committed Nov 24, 2023
1 parent d7eb831 commit 1c37b78
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 32 deletions.
10 changes: 9 additions & 1 deletion src/Libraries/Core/src/Config/ConfigManager.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Newtonsoft.Json;
using System.IO;

Expand All @@ -7,9 +8,16 @@ public class ConfigManager
{
public static bool IsConfigFileExist => File.Exists(ConfigPath);
public static UniSpyConfig Config = LoadConfigFile();
public const string ConfigPath = @"UniSpyServerConfig.json";
public const string ConfigPath = "UniSpyServerConfig.json";
public const string EnvVarName = "UNISPYCONFIG";
private static UniSpyConfig LoadConfigFile()
{
var config = Environment.GetEnvironmentVariable(EnvVarName);
if (config is not null && config !="")
{
return JsonConvert.DeserializeObject<UniSpyConfig>(config);
}

if (!IsConfigFileExist)
{
throw new UniSpy.Exception("UniSpy server config file not found");
Expand Down
4 changes: 3 additions & 1 deletion src/Libraries/Core/src/Network/Http/Server/HttpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public void Send(string response)
public void Send(byte[] response)
{
Context.Response.StatusCode = (int)HttpStatusCode.OK;
Context.Response.ContentType = "application/xml?";
Context.Response.ContentType = "application/xml";
Context.Response.SendChunked = false;
Context.Response.ContentLength64 = response.Length;
Context.Response.OutputStream.Write(response);
if (Context.Request.Headers["Connection"] == "close")
{
Expand Down
16 changes: 12 additions & 4 deletions src/Libraries/Core/src/Network/Tcp/Server/TcpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,22 @@ public void OnConnected()

private void StartReceiving()
{

if (Client.Connected == false)
{
OnDisconnected();
return;
}
var stream = Client.GetStream();
byte[] buffer = new byte[2048];
int bytesRead;
while (true)
{
try
{
var stream = Client.GetStream();
byte[] buffer = new byte[2048];
int bytesRead;
if (!stream.CanRead || !stream.CanWrite)
{
break;
}
bytesRead = stream.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
{
Expand Down
10 changes: 1 addition & 9 deletions src/Servers/WebServer/src/Abstraction/SoapEnvelopBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,12 @@ public virtual void Add(string name, object value)
{
CurrentElement.Add(new XElement(_bodyNamespace + name, value));
}
// public virtual void Add(string name, List<RecordFieldObject> values)
// {
// throw new NotImplementedException();
// }
// public virtual void Add(string name, List<FieldObject> values)
// {
// throw new NotImplementedException();
// }

public override string ToString()
{
using (var writer = new MyStringWriter())
{
Content.Save(writer);
Content.Save(writer, SaveOptions.DisableFormatting);
return writer.ToString();
}
}
Expand Down
13 changes: 9 additions & 4 deletions src/Servers/WebServer/src/Application/ClientInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using UniSpy.Server.Core.Abstraction.BaseClass;
using UniSpy.Server.Core.Extension;

namespace UniSpy.Server.WebServer.Application
{
Expand All @@ -13,24 +14,28 @@ namespace UniSpy.Server.WebServer.Application
/// </summary>
public sealed class ClientInfo : ClientInfoBase
{
public const string PeerKeyExponent = "000001";
public const string PeerKeyExponent = "010001";
public const string PeerKeyModulus = "aefb5064bbd1eb632fa8d57aab1c49366ce0ee3161cbef19f2b7971b63b811790ecbf6a47b34c55f65a0766b40c261c5d69c394cd320842dd2bccba883d30eae8fdba5d03b21b09bfc600dcb30b1b2f3fbe8077630b006dcb54c4254f14891762f72e7bbfe743eb8baf65f9e8c8d11ebe46f6b59e986b4c394cfbc2c8606e29f";
/// <summary>
/// The exponent generated for user
/// exponent is 000001
/// </summary>
public static readonly byte PeerKeyExponentByte = 1;
// public static byte[] PeerKeyExponentByte => BitConverter.GetBytes(int.Parse(PeerKeyExponent));
/// <summary>
/// The modulus generated for user aefb5064bbd1eb632fa8d57aab1c49366ce0ee3161cbef19f2b7971b63b811790ecbf6a47b34c55f65a0766b40c261c5d69c394cd320842dd2bccba883d30eae8fdba5d03b21b09bfc600dcb30b1b2f3fbe8077630b006dcb54c4254f14891762f72e7bbfe743eb8baf65f9e8c8d11ebe46f6b59e986b4c394cfbc2c8606e29f
/// </summary>
public static readonly byte[] PeerKeyModulusBytes = { 0xAE, 0xFB, 0x50, 0x64, 0xBB, 0xD1, 0xEB, 0x63, 0x2F, 0xA8, 0xD5, 0x7A, 0xAB, 0x1C, 0x49, 0x36, 0x6C, 0xE0, 0xEE, 0x31, 0x61, 0xCB, 0xEF, 0x19, 0xF2, 0xB7, 0x97, 0x1B, 0x63, 0xB8, 0x11, 0x79, 0x0E, 0xCB, 0xF6, 0xA4, 0x7B, 0x34, 0xC5, 0x5F, 0x65, 0xA0, 0x76, 0x6B, 0x40, 0xC2, 0x61, 0xC5, 0xD6, 0x9C, 0x39, 0x4C, 0xD3, 0x20, 0x84, 0x2D, 0xD2, 0xBC, 0xCB, 0xA8, 0x83, 0xD3, 0x0E, 0xAE, 0x8F, 0xDB, 0xA5, 0xD0, 0x3B, 0x21, 0xB0, 0x9B, 0xFC, 0x60, 0x0D, 0xCB, 0x30, 0xB1, 0xB2, 0xF3, 0xFB, 0xE8, 0x07, 0x76, 0x30, 0xB0, 0x06, 0xDC, 0xB5, 0x4C, 0x42, 0x54, 0xF1, 0x48, 0x91, 0x76, 0x2F, 0x72, 0xE7, 0xBB, 0xFE, 0x74, 0x3E, 0xB8, 0xBA, 0xF6, 0x5F, 0x9E, 0x8C, 0x8D, 0x11, 0xEB, 0xE4, 0x6F, 0x6B, 0x59, 0xE9, 0x86, 0xB4, 0xC3, 0x94, 0xCF, 0xBC, 0x2C, 0x86, 0x06, 0xE2, 0x9F };
// public static byte[] PeerKeyModulusBytes => PeerKeyModulus.FromHexStringToBytes();
/// <summary>
/// should be 256 characters
/// </summary>
public const string ServerData =
"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
"95980bf5011ce73f2866b995a272420c36f1e8b4ac946f0b5bfe87c9fef0811036da00cfa85e77e00af11c924d425ec06b1dd052feab1250376155272904cbf9da831b0ce3d52964424c0a426b869e2c0ad11ffa3e70496e27ea250adb707a96b3496bff190eafc0b6b9c99db75b02c2a822bb1b5b3d954e7b2c0f9b1487e3e1";
public static int ExpireTime => (int)((DateTimeOffset)DateTime.UtcNow + TimeSpan.FromMinutes(5)).ToUnixTimeSeconds();
public const string SignaturePreFix = "0001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF003020300C06082A864886F70D020505000410";

public readonly byte[] PrivateKeyD = new byte[128] { 0x96, 0xE2, 0xF4, 0xF9, 0x7C, 0x78, 0x81, 0x0C, 0x13, 0xE2, 0xA4, 0xCA, 0xC1, 0x12, 0x44, 0xBB, 0x5B, 0x37, 0xC5, 0x4E, 0x98, 0x20, 0xF1, 0x3F, 0xE9, 0xB5, 0x53, 0xDA, 0x10, 0xB1, 0xE8, 0xEF, 0x8E, 0xB0, 0x3F, 0x87, 0x68, 0x1B, 0x0E, 0x62, 0x4D, 0x1A, 0x8D, 0xE9, 0x17, 0x4C, 0xBE, 0xE5, 0xB8, 0xED, 0x92, 0xE4, 0xBE, 0x74, 0xF8, 0x6C, 0x30, 0x38, 0xCD, 0x7C, 0x1A, 0x20, 0xB9, 0xA3, 0xDB, 0x1D, 0x49, 0x22, 0x62, 0x87, 0x38, 0x68, 0xFB, 0xA0, 0x8E, 0x1E, 0xAB, 0x5C, 0xBA, 0x86, 0x3F, 0x8F, 0xDB, 0xF4, 0x5E, 0xEA, 0x61, 0x4B, 0xBF, 0x6C, 0xFC, 0x47, 0x00, 0x81, 0x44, 0x2A, 0x97, 0x78, 0x7E, 0xB6, 0xEC, 0xA7, 0x1C, 0x48, 0x96, 0x81, 0x6C, 0x2A, 0x62, 0x72, 0x4C, 0x0E, 0x8C, 0xAA, 0xEE, 0xAB, 0x72, 0x78, 0xC2, 0x55, 0x4A, 0x13, 0x80, 0x94, 0x6E, 0xED, 0x21, 0x29 };
public readonly byte[] Modulus = new byte[128] { 0xA9, 0x3E, 0x00, 0x19, 0x2C, 0x4A, 0x98, 0x69, 0xD7, 0x41, 0x9A, 0xFF, 0x66, 0x2E, 0xCA, 0xD6, 0xC8, 0xB9, 0x99, 0x09, 0xFD, 0xD0, 0xE7, 0xF8, 0xCA, 0xDD, 0x15, 0x32, 0xE8, 0xE3, 0x59, 0x37, 0x40, 0x83, 0xDA, 0xB8, 0xBE, 0x71, 0x7F, 0x60, 0x91, 0x60, 0xCD, 0x6A, 0x54, 0x11, 0xBE, 0xD7, 0x92, 0x7A, 0xD3, 0xB5, 0xC0, 0x0C, 0x4C, 0x4B, 0x34, 0x76, 0x71, 0xF2, 0x3F, 0xE0, 0x1E, 0xBB, 0x2F, 0x83, 0x4B, 0xD8, 0xCA, 0x27, 0xC3, 0x55, 0x3E, 0x1E, 0x6B, 0xC2, 0x85, 0xAF, 0xC6, 0x3E, 0xC0, 0xE1, 0x1F, 0x59, 0xCA, 0xF6, 0xAC, 0x37, 0x5F, 0x4B, 0x0E, 0xB8, 0x2A, 0x4D, 0xA2, 0x2C, 0x0C, 0x10, 0x1D, 0x09, 0x13, 0x1A, 0x7C, 0x42, 0x0D, 0x4C, 0xC4, 0xD0, 0x95, 0x62, 0x4C, 0x42, 0xBC, 0xD8, 0xD7, 0x19, 0x16, 0xC8, 0xDC, 0x00, 0x48, 0x08, 0x6D, 0x74, 0x6A, 0x31, 0x55, 0xC7 };
public readonly byte[] Exponent = new byte[3] { 0x01, 0x00, 0x01 };
public ClientInfo()
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected LoginResponseBase(RequestBase request, ResultBase result) : base(reque

protected void BuildContext()
{
_content.Add("responseCode", _result.ResponseCode);
_content.Add("responseCode", "h");
_content.Add("certificate");
_content.Add("length", _result.Length);
_content.Add("version", _request.Version);
Expand Down Expand Up @@ -49,9 +49,12 @@ protected void BuildContext()
dataToHash.AddRange(Encoding.ASCII.GetBytes(_result.UniqueNick));
dataToHash.AddRange(Encoding.ASCII.GetBytes(_result.CdKeyHash));

dataToHash.AddRange(ClientInfo.PeerKeyModulusBytes);
dataToHash.Add(ClientInfo.PeerKeyExponentByte);
dataToHash.AddRange(ClientInfo.PeerKeyModulus.FromHexStringToBytes());
dataToHash.AddRange(ClientInfo.PeerKeyExponent.FromHexStringToBytes());
// dataToHash.AddRange(new byte[] { 0x01, 0x00, 0x01 });

// var exp = System.Numerics.BigInteger.Parse(ClientInfo.PeerKeyExponent,System.Globalization.NumberStyles.HexNumber);
// exp.ToByteArray()
// server data should be convert to bytes[128] then added to list
dataToHash.AddRange(ClientInfo.ServerData.FromHexStringToBytes());
var hash = md5.ComputeHash(dataToHash.ToArray());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ join sp in db.Subprofiles on p.Profileid equals sp.Profileid
_result.UserId = data.u.Userid;
_result.ProfileId = data.p.Profileid;
// _result.CdKeyHash = data.sp.Cdkeyenc;
_result.CdKeyHash = "xxxxxxxxxxx";
_result.CdKeyHash = "D41D8CD98F00B204E9800998ECF8427E";
// currently we set this to uniquenick
_result.ProfileNick = data.p.Nick;
_result.UniqueNick = data.sp.Uniquenick;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ join sp in db.Subprofiles on p.Profileid equals sp.Profileid
_result.UserId = data.u.Userid;
_result.ProfileId = data.p.Profileid;
// _result.CdKeyHash = data.sp.Cdkeyenc;
_result.CdKeyHash = "xxxxxxxxxxx";
_result.CdKeyHash = "D41D8CD98F00B204E9800998ECF8427E";
// currently we set this to uniquenick
_result.ProfileNick = data.p.Nick;
_result.UniqueNick = data.sp.Uniquenick;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace UniSpy.Server.WebServer.Module.Direct2Game.Contract
{
public sealed class Direct2GameSoapEnvelope : SoapEnvelopBase
{
public static XNamespace Direct2GameNamespace = "http://gamespy.net/commerce/2009/02";
public Direct2GameSoapEnvelope() : base("gsc", Direct2GameNamespace)
public static XNamespace Direct2GameNamespace = "http://gamespy.net/commerce/";
public Direct2GameSoapEnvelope() : base("ns1", Direct2GameNamespace)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ public GetPurchaseHistoryResponse(RequestBase request, ResultBase result) : base

public override void Build()
{
_content.Add("GetPurchaseHistoryResponse");
_content.Add("GetPurchaseHistoryResult");
_content.Add("status");
_content.Add("code", _result.Code);
_content.ChangeToElement("GetPurchaseHistoryResult");
_content.Add("orderpurchases");
// we do not know the purchace content for each game, so currently we just make it 0
_content.Add("count", 0);
// _content.ChangeToElement("GetPurchaseHistoryResult");
// _content.BackToParentElement();
base.Build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ public GetStoreAvailabilityResponse(RequestBase request, ResultBase result) : ba

public override void Build()
{
_content.Add("GetStoreAvailabilityResponse");
_content.Add("GetStoreAvailabilityResult");
_content.Add("status");
_content.Add("code", _result.Code);
_content.ChangeToElement("status");
_content.Add("storestatusid", _result.StoreStatusId);
_content.ChangeToElement("GetStoreAvailabilityResult");
_content.Add("storestatusid", (int)_result.StoreStatusId);
base.Build();
}
}
Expand Down
25 changes: 23 additions & 2 deletions src/Servers/WebServer/test/Auth/RSATest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using System.Security.Cryptography;
using UniSpy.Server.Core.Extension;
using UniSpy.Server.Core.Logging;
using Xunit;

namespace UniSpy.Server.WebServer.Test.Auth
Expand All @@ -11,9 +14,27 @@ public void KeyGen()
using (var rsa = new RSACryptoServiceProvider(1024))
{
var privateKey = rsa.ExportParameters(true);

// The public key is used for verifying the signature
var publicKey = rsa.ExportParameters(false);
var d = privateKey.D;
var modulus = privateKey.Modulus;
var exponent = privateKey.Exponent;

Console.WriteLine(StringExtensions.ConvertByteToHexString(d));
Console.WriteLine(StringExtensions.ConvertByteToHexString(modulus));
Console.WriteLine(StringExtensions.ConvertByteToHexString(exponent));


var newPublicParam = new RSAParameters
{
Modulus = modulus,
Exponent = exponent
};
var newPrivateParam = new RSAParameters
{
Modulus = modulus,
Exponent = exponent,
D = d,
};
}
}
}
Expand Down

0 comments on commit 1c37b78

Please sign in to comment.