GateSession.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Threading.Tasks;
  3. using BossBase;
  4. using Helper;
  5. using Logger;
  6. namespace BossClient
  7. {
  8. public class GateSession: IDisposable
  9. {
  10. public int ID { get; set; }
  11. public IMessageChannel IMessageChannel { get; set; }
  12. public GateSession(int id, IMessageChannel eNetChannel)
  13. {
  14. this.ID = id;
  15. this.IMessageChannel = eNetChannel;
  16. }
  17. public void Dispose()
  18. {
  19. this.IMessageChannel.Dispose();
  20. }
  21. public void SendMessage<T>(ushort opcode, T message, byte channelID = 0)
  22. {
  23. this.IMessageChannel.SendMessage(opcode, message, channelID);
  24. }
  25. public async Task Login(SRP6Client srp6Client)
  26. {
  27. var smsgAuthChallenge = await this.Handle_SMSG_Auth_Challenge();
  28. var clientSeed = (uint) TimeHelper.EpochTimeSecond();
  29. byte[] digest = srp6Client.CalculateGateDigest(clientSeed, smsgAuthChallenge.Seed);
  30. var cmsgAuthSession = new CMSG_Auth_Session
  31. {
  32. ClientBuild = 11723,
  33. ClientSeed = clientSeed,
  34. Digest = digest,
  35. Hd = new byte[0],
  36. Mac = new byte[0],
  37. Unk2 = 0,
  38. Unk3 = 0,
  39. Unk4 = 0,
  40. Username = srp6Client.Account
  41. };
  42. this.IMessageChannel.SendMessage(MessageOpcode.CMSG_AUTH_SESSION, cmsgAuthSession);
  43. var smsgAuthResponse = await this.Handle_SMSG_Auth_Response();
  44. if (smsgAuthResponse.ErrorCode != ErrorCode.AUTH_OK)
  45. {
  46. throw new BossException(string.Format("session: {0}, SMSG_Auth_Response: {1}",
  47. this.ID, MongoHelper.ToJson(smsgAuthResponse)));
  48. }
  49. Log.Trace("session: {0}, login gate OK!", this.ID);
  50. }
  51. public async Task<SMSG_Auth_Challenge> Handle_SMSG_Auth_Challenge()
  52. {
  53. var result = await this.IMessageChannel.RecvMessage();
  54. ushort opcode = result.Item1;
  55. byte[] message = result.Item2;
  56. Log.Debug("message: {0}", message.ToHex());
  57. if (opcode != MessageOpcode.SMSG_AUTH_CHALLENGE)
  58. {
  59. throw new BossException(string.Format("session: {0}, opcode: {1}", this.ID, opcode));
  60. }
  61. var smsgAuthChallenge = ProtobufHelper.FromBytes<SMSG_Auth_Challenge>(message);
  62. return smsgAuthChallenge;
  63. }
  64. public async Task<SMSG_Auth_Response> Handle_SMSG_Auth_Response()
  65. {
  66. var result = await this.IMessageChannel.RecvMessage();
  67. ushort opcode = result.Item1;
  68. byte[] message = result.Item2;
  69. if (opcode != MessageOpcode.SMSG_AUTH_RESPONSE)
  70. {
  71. throw new BossException(string.Format("session: {0}, opcode: {1}", this.ID, opcode));
  72. }
  73. var smsgAuthResponse = ProtobufHelper.FromBytes<SMSG_Auth_Response>(message);
  74. return smsgAuthResponse;
  75. }
  76. }
  77. }