还是无法兼容更多版本

This commit is contained in:
Longfeng Qin 2024-12-02 11:27:14 +08:00
parent 4bd5771f5d
commit 235a4ee5fc
11 changed files with 137 additions and 39 deletions

View File

@ -14,6 +14,22 @@ namespace PSO2SERVER
{
public class Client
{
public enum PacketType : byte
{
/// NGS packet.
NGS,
/// Classic packet. (i.e. NA, JP and Vita)
Classic,
/// NA packet.
NA,
/// JP packet.
JP,
/// Vita packet.
Vita,
/// Raw packet. (i.e. don't parse the packet)
Raw,
}
internal static RSACryptoServiceProvider RsaCsp = null;
private readonly byte[] _readBuffer;
private readonly Server _server;
@ -36,7 +52,7 @@ namespace PSO2SERVER
InputArc4 = null;
OutputArc4 = null;
SendPacket(new ServerHelloPacket(0x03, 201, 0));
SendPacket(new ServerHelloPacket(0x03, 100, 68833280));
}
public bool IsClosed { get; private set; }

30
Server/Models/block.cs Normal file
View File

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PSO2SERVER.Models
{
public class block
{
public uint unk1 { get; set; }
public byte unk2 { get; set; }
public byte unk3 { get; set; }
public byte unk4 { get; set; }
public byte unk5 { get; set; }
public uint unk6 { get; set; }
public uint unk7 { get; set; }
public ushort unk8 { get; set; }
public ushort block_id { get; set; }
public string block_name { get; set; }
public byte[] ip { get; set; } = new byte[4];
public ushort port { get; set; }
public ushort unk10 { get; set; }
public ushort unk11 { get; set; }
public ushort[] unk12 { get; set; } = new ushort[3];
public float cur_capacity { get; set; } /// Block fullness (between 0 and 1).
}
}

View File

@ -53,15 +53,27 @@ namespace PSO2SERVER.Network
{
if (socket == _listener.Server)
{
// New connection
Logger.WriteInternal("[HI!] 新的客户端接入!");
var c = new SocketClient(this, _listener.AcceptTcpClient());
_clients.Add(c);
_socketMap.Add(c.Socket.Client, c);
NewClient(c);
// 获取接入客户端的 IP 和端口
var clientEndPoint = c.Socket.Client.RemoteEndPoint as IPEndPoint;
if (clientEndPoint != null)
{
string clientIp = clientEndPoint.Address.ToString();
int clientPort = clientEndPoint.Port;
// 输出客户端的 IP 和端口
Logger.WriteInternal($"[HI!] 新的客户端接入! IP: {clientIp}, 端口: {clientPort}");
}
else
{
// New connection
Logger.WriteInternal("[HI!] 新的客户端接入!");
}
}
else
{

View File

@ -32,15 +32,22 @@ namespace PSO2SERVER
public const int ServerShipProtNums = 10;
public const int ServerShipProt = 12000;
public const int ServerShipListProtNums = 10;
public const int ServerShipListProt = 12099;
public const int ServerShipListProtNums_JP = 10;
public const int ServerShipListProt_JP = 12099;
public const int ServerShipListProtNums_NA = 6;
public const int ServerShipListProt_NA = 13001;
public const string ServerSettingsKey = "Resources\\settings.txt";
//public const string ServerMemoryPacket = "Resources\\setMemoryPacket.bin"; TODO
// 密钥BLOB格式
public const string ServerPrivateKeyBlob = "key\\privateKey.blob";
public const string ServerPublicKeyBlob = "key\\publicKey.blob";
public const string ServerPrivateKeyBlob_JP = "key\\privateKey_jp.blob";
public const string ServerPublicKeyBlob_JP = "key\\publicKey_jp.blob";
public const string ServerPrivateKeyBlob_VITA = "key\\privateKey_vita.blob";
public const string ServerPublicKeyBlob_VITA = "key\\publicKey_vita.blob";
public const string ServerSEGAKeyBlob = "key\\SEGAKey.blob";
// 密钥PEM格式 来自Schthack
@ -174,7 +181,7 @@ namespace PSO2SERVER
public void GenerateKeys()
{
// Process private key files
KeyLoader.ProcessKeyFiles(ServerPrivatePem, ServerPrivateKeyBlob, true, File.Exists(ServerPrivatePem));
KeyLoader.ProcessKeyFiles(ServerPrivatePem, ServerPrivateKeyBlob_JP, true, File.Exists(ServerPrivatePem));
// Process SEGA public key files
KeyLoader.ProcessKeyFiles(ServerSEGAPem, ServerSEGAKeyBlob, false, File.Exists(ServerSEGAPem));
@ -183,8 +190,11 @@ namespace PSO2SERVER
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
// Process private and public RSA keys
KeyLoader.GenerateAndSaveKeyIfNotExists(rsa, ServerPrivateKeyBlob, true);
KeyLoader.GenerateAndSaveKeyIfNotExists(rsa, ServerPublicKeyBlob, false);
KeyLoader.GenerateAndSaveKeyIfNotExists(rsa, ServerPrivateKeyBlob_JP, true);
KeyLoader.GenerateAndSaveKeyIfNotExists(rsa, ServerPublicKeyBlob_JP, false);
KeyLoader.GenerateAndSaveKeyIfNotExists(rsa, ServerPrivateKeyBlob_VITA, true);
KeyLoader.GenerateAndSaveKeyIfNotExists(rsa, ServerPublicKeyBlob_VITA, false);
}
}
@ -198,9 +208,21 @@ namespace PSO2SERVER
await InitializeDatabaseAsync();
await InitializeQueryServers(QueryMode.AuthList, "认证", ServerShipProt, ServerShipProtNums);
await InitializeQueryServers(QueryMode.AuthList, "认证", 11000, 3, 1000);
await InitializeQueryServers(QueryMode.ShipList, "舰船", ServerShipListProt, ServerShipListProtNums);
await InitializeQueryServers(QueryMode.AuthList, "认证", 13099, 1, 0);
await InitializeQueryServers(QueryMode.AuthList, "认证", ServerShipProt, ServerShipProtNums, 100);
await InitializeQueryServers(QueryMode.ShipList, "舰船_JP", 12100, 10, 100);
await InitializeQueryServers(QueryMode.ShipList, "舰船_JP", 12193, 10, 100);
await InitializeQueryServers(QueryMode.ShipList, "舰船_JP", 12194, 10, 100);
await InitializeQueryServers(QueryMode.ShipList, "舰船_JP", ServerShipListProt_JP, ServerShipListProtNums_JP, 100);
await InitializeQueryServers(QueryMode.ShipList, "舰船_NA", ServerShipListProt_NA, ServerShipListProtNums_NA, 1);
Logger.WriteInternal("服务器启动完成 " + DateTime.Now);
@ -243,7 +265,7 @@ namespace PSO2SERVER
}
}
public async Task InitializeQueryServers(QueryMode queryMode, string portname, int port, int portnums)
public async Task InitializeQueryServers(QueryMode queryMode, string portname, int port, int portnums, int add)
{
await Task.Run(() =>
{
@ -254,7 +276,7 @@ namespace PSO2SERVER
{
for (var i = 0; i < portnums; i++)
{
QueryServers.Add(new QueryServer(queryMode, portname, port + (100 * i)));
QueryServers.Add(new QueryServer(queryMode, portname, port + (add * i)));
}
}
});

View File

@ -7,22 +7,22 @@ using PSO2SERVER.Zone;
namespace PSO2SERVER.Protocol.Handlers
{
//[PacketHandlerAttr(0x04, 0x13)]
//class _04_13_UNK : PacketHandler
//{
// public override void HandlePacket(Client context, byte flags, byte[] data, uint position, uint size)
// {
// //if (context.currentParty.currentQuest == null)
// // return;
[PacketHandlerAttr(0x04, 0x13)]
class _04_13_UNK : PacketHandler
{
public override void HandlePacket(Client context, byte flags, byte[] data, uint position, uint size)
{
if (context.currentParty.currentQuest == null)
return;
// //// TODO: WTF terribad hax?
// //if (context.CurrentLocation.PosZ >= 20)
// //{
// // var instanceName = String.Format("{0}-{1}", context.currentParty.currentQuest.name, context._account.Nickname);
// TODO: WTF terribad hax?
if (context.CurrentLocation.PosZ >= 20)
{
var instanceName = String.Format("{0}-{1}", context.currentParty.currentQuest.name, context._account.Nickname);
// // Map forest = ZoneManager.Instance.MapFromInstance("area1", instanceName);
// // forest.SpawnClient(context, new PSOLocation(0, 1, 0, -0, -37, 0.314f, 145.5f));
// //}
// }
//}
Map forest = ZoneManager.Instance.MapFromInstance("area1", instanceName);
forest.SpawnClient(context, new PSOLocation(0, 1, 0, -0, -37, 0.314f, 145.5f));
}
}
}
}

View File

@ -93,8 +93,8 @@ namespace PSO2SERVER.Protocol.Handlers
ReadFromStream(reader);
//var info = string.Format("[<--] 接收到的数据 (hex): {0}字节", data.Length);
//Logger.WriteHex(info, data);
var info = string.Format("[<--] 接收到的数据 (hex): {0}字节", data.Length);
Logger.WriteHex(info, data);
//Logger.Write("用户名 {0} 密码 {1} - {2}", Username, Password, BCrypt.Net.BCrypt.HashPassword(Password));

View File

@ -21,7 +21,7 @@ namespace PSO2SERVER.Protocol.Handlers
// Extract the first 0x80 bytes into a separate array
var cryptedBlob = new byte[0x80];
var rsaBlob = File.ReadAllBytes(ServerApp.ServerPrivateKeyBlob);
var rsaBlob = File.ReadAllBytes(ServerApp.ServerPrivateKeyBlob_JP);
Array.Copy(data, position, cryptedBlob, 0, 0x80);
Array.Reverse(cryptedBlob);
@ -30,10 +30,10 @@ namespace PSO2SERVER.Protocol.Handlers
//var info = string.Format("[<--] 接收到的数据 (hex): {0} 字节", data.Length);
//Logger.WriteHex(info, cryptedBlob);
//// Convert cryptedBlob to a hexadecimal string
// Convert cryptedBlob to a hexadecimal string
//var hexString = BitConverter.ToString(cryptedBlob).Replace("-", "");
//// Save the hexadecimal string to a text file
// Save the hexadecimal string to a text file
//File.WriteAllText("cryptedBlob.txt", hexString);
// FIXME
@ -59,7 +59,11 @@ namespace PSO2SERVER.Protocol.Handlers
// Also a failure.
if (decryptedBlob.Length < 0x20)
{
Logger.Write("解密RSA密钥交换时发生错误 {0} < 0x20", decryptedBlob.Length);
context.Socket.Close();
return;
}
// Extract the RC4 key
var arc4Key = new byte[16];

View File

@ -51,8 +51,8 @@ namespace PSO2SERVER.Protocol.Packets
foreach (var ch in chars)
{
writer.Write((uint)ch.CharacterID);//4
writer.Write((uint)ch.AccountID);//4
writer.Write((uint)ch.CharacterID);//4
writer.Write(ch.Unk1);//4
writer.Write(ch.VoiceType);//4

View File

@ -8,9 +8,16 @@ namespace PSO2SERVER.Protocol.Packets
{
public class BlockBalancePacket : Packet
{
public byte[] unk1 { get; set; } = new byte[0x20];
public string blockname { get; set; } = string.Empty; //0x20 wide
public byte[] ip { get; set; } = new byte[4];
public ushort port { get; set; }
public byte[] data { get; set; } = new byte[0x11A];
public BlockBalancePacket()
{
ip = ServerApp.BindAddress.GetAddressBytes();
}
#region implemented abstract members of Packet
@ -18,6 +25,11 @@ namespace PSO2SERVER.Protocol.Packets
public override byte[] Build()
{
var pkt = new PacketWriter();
pkt.Write(unk1);
pkt.WriteFixedLengthUtf16(blockname, 0x20);
pkt.Write(ip);
pkt.Write((UInt16)12205);
pkt.Write(new byte[0x90 - 0x6A]);
return pkt.ToArray();
}

View File

@ -123,10 +123,11 @@ namespace PSO2SERVER
{
var writer = new PacketWriter();
writer.WriteStruct(new PacketHeader(0x90, 0x11, 0x2C, 0x0, 0x0));
writer.Write(new byte[0x68 - 8]);
writer.Write(new byte[0x20]);
writer.Write(new byte[0x40]);
writer.Write(ServerApp.BindAddress.GetAddressBytes());
writer.Write((UInt16)12205);
writer.Write(new byte[0x90 - 0x6A]);
writer.Write(new byte[0x11A]);
var buffer = writer.ToArray();
await Task.Factory.FromAsync(

View File

@ -165,6 +165,7 @@
<Compile Include="ConsoleSystem.cs" />
<Compile Include="Crypto\KeyLoader.cs" />
<Compile Include="Logger.cs" />
<Compile Include="Models\block.cs" />
<Compile Include="Models\Flags.cs" />
<Compile Include="Models\Mission.cs" />
<Compile Include="Models\PSO2Item.cs" />