RealmSession.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net.Sockets;
  4. using System.Numerics;
  5. using System.Security.Cryptography;
  6. using System.Threading.Tasks;
  7. using Helper;
  8. using Log;
  9. namespace LoginClient
  10. {
  11. public class RealmSession: IDisposable
  12. {
  13. public int ID { get; set; }
  14. public IMessageChannel MessageChannel { get; set; }
  15. public RealmSession(int id, IMessageChannel messageChannel)
  16. {
  17. this.ID = id;
  18. this.MessageChannel = messageChannel;
  19. }
  20. public void Dispose()
  21. {
  22. this.MessageChannel.Dispose();
  23. }
  24. public async Task<SMSG_Password_Protect_Type> Handle_CMSG_AuthLogonPermit_Response()
  25. {
  26. var result = await this.MessageChannel.RecvMessage();
  27. ushort opcode = result.Item1;
  28. byte[] message = result.Item2;
  29. if (opcode != MessageOpcode.SMSG_PASSWORD_PROTECT_TYPE)
  30. {
  31. throw new LoginException(string.Format(
  32. "session: {0}, opcode: {1}", this.ID, opcode));
  33. }
  34. var smsgPasswordProtectType =
  35. ProtobufHelper.FromBytes<SMSG_Password_Protect_Type>(message);
  36. Logger.Trace("message: {0}", JsonHelper.ToString(smsgPasswordProtectType));
  37. if (smsgPasswordProtectType.Code != 200)
  38. {
  39. throw new LoginException(string.Format(
  40. "session: {0}, SMSG_Lock_For_Safe_Time: {1}",
  41. this.ID, JsonHelper.ToString(smsgPasswordProtectType)));
  42. }
  43. return smsgPasswordProtectType;
  44. }
  45. public async Task<SMSG_Auth_Logon_Challenge_Response>
  46. Handle_SMSG_Auth_Logon_Challenge_Response()
  47. {
  48. var result = await this.MessageChannel.RecvMessage();
  49. ushort opcode = result.Item1;
  50. byte[] message = result.Item2;
  51. if (opcode != MessageOpcode.SMSG_AUTH_LOGON_CHALLENGE_RESPONSE)
  52. {
  53. Logger.Trace("opcode: {0}", opcode);
  54. throw new LoginException(string.Format(
  55. "session: {0}, opcode: {1}", this.ID, opcode));
  56. }
  57. var smsgAuthLogonChallengeResponse =
  58. ProtobufHelper.FromBytes<SMSG_Auth_Logon_Challenge_Response>(message);
  59. if (smsgAuthLogonChallengeResponse.ErrorCode != ErrorCode.REALM_AUTH_SUCCESS)
  60. {
  61. Logger.Trace("error code: {0}", smsgAuthLogonChallengeResponse.ErrorCode);
  62. throw new LoginException(
  63. string.Format("session: {0}, SMSG_Auth_Logon_Challenge_Response: {1}",
  64. this.ID, JsonHelper.ToString(smsgAuthLogonChallengeResponse)));
  65. }
  66. Logger.Debug("SMSG_Auth_Logon_Challenge_Response: \n{0}",
  67. JsonHelper.ToString(smsgAuthLogonChallengeResponse));
  68. return smsgAuthLogonChallengeResponse;
  69. }
  70. public async Task<SMSG_Auth_Logon_Proof_M2> Handle_SMSG_Auth_Logon_Proof_M2()
  71. {
  72. var result = await this.MessageChannel.RecvMessage();
  73. ushort opcode = result.Item1;
  74. byte[] message = result.Item2;
  75. if (opcode != MessageOpcode.SMSG_AUTH_LOGON_PROOF_M2)
  76. {
  77. throw new LoginException(string.Format(
  78. "session: {0}, error opcode: {1}", this.ID, opcode));
  79. }
  80. var smsgAuthLogonProofM2 = ProtobufHelper.FromBytes<SMSG_Auth_Logon_Proof_M2>(message);
  81. if (smsgAuthLogonProofM2.ErrorCode != ErrorCode.REALM_AUTH_SUCCESS)
  82. {
  83. throw new LoginException(string.Format(
  84. "session: {0}, SMSG_Auth_Logon_Proof_M2: {1}",
  85. this.ID, JsonHelper.ToString(smsgAuthLogonProofM2)));
  86. }
  87. return smsgAuthLogonProofM2;
  88. }
  89. public async Task<SMSG_Realm_List> Handle_SMSG_Realm_List()
  90. {
  91. var result = await this.MessageChannel.RecvMessage();
  92. ushort opcode = result.Item1;
  93. byte[] message = result.Item2;
  94. if (opcode != MessageOpcode.SMSG_REALM_LIST)
  95. {
  96. throw new LoginException(string.Format(
  97. "session: {0}, error opcode: {1}", this.ID, opcode));
  98. }
  99. var smsgRealmList = ProtobufHelper.FromBytes<SMSG_Realm_List>(message);
  100. return smsgRealmList;
  101. }
  102. public async Task<Tuple<string, ushort, SRP6Client>> Login(string account, string password)
  103. {
  104. byte[] passwordBytes = password.ToByteArray();
  105. MD5 md5 = MD5.Create();
  106. byte[] passwordMd5 = md5.ComputeHash(passwordBytes);
  107. byte[] passwordMd5Hex = passwordMd5.ToHex().ToLower().ToByteArray();
  108. // 发送帐号和密码MD5
  109. var cmsgAuthLogonPermit = new CMSG_Auth_Logon_Permit
  110. {
  111. Account = account.ToByteArray(),
  112. PasswordMd5 = passwordMd5Hex
  113. };
  114. Logger.Trace("account: {0}, password: {1}",
  115. cmsgAuthLogonPermit.Account, cmsgAuthLogonPermit.PasswordMd5.ToHex());
  116. this.MessageChannel.SendMessage(MessageOpcode.CMSG_AUTH_LOGON_PERMIT, cmsgAuthLogonPermit);
  117. await this.Handle_CMSG_AuthLogonPermit_Response();
  118. // 这个消息已经没有作用,只用来保持原有的代码流程
  119. var cmsgAuthLogonChallenge = new CMSG_Auth_Logon_Challenge();
  120. this.MessageChannel.SendMessage(
  121. MessageOpcode.CMSG_AUTH_LOGON_CHALLENGE, cmsgAuthLogonChallenge);
  122. var smsgAuthLogonChallengeResponse =
  123. await this.Handle_SMSG_Auth_Logon_Challenge_Response();
  124. // 以下是SRP6处理过程
  125. var n = smsgAuthLogonChallengeResponse.N.ToUBigInteger();
  126. var g = smsgAuthLogonChallengeResponse.G.ToUBigInteger();
  127. var b = smsgAuthLogonChallengeResponse.B.ToUBigInteger();
  128. var salt = smsgAuthLogonChallengeResponse.S.ToUBigInteger();
  129. var srp6Client = new SRP6Client(
  130. new SHA1Managed(), n, g, b, salt, account.ToByteArray(), passwordMd5Hex);
  131. Logger.Debug("s: {0}\nN: {1}\nG: {2}\nB: {3}\nA: {4}\nS: {5}\nK: {6}\nm: {7}\na: {8}",
  132. srp6Client.Salt.ToUBigIntegerArray().ToHex(),
  133. srp6Client.N.ToUBigIntegerArray().ToHex(),
  134. srp6Client.G.ToUBigIntegerArray().ToHex(),
  135. srp6Client.B.ToUBigIntegerArray().ToHex(),
  136. srp6Client.A.ToUBigIntegerArray().ToHex(),
  137. srp6Client.S.ToUBigIntegerArray().ToHex(),
  138. srp6Client.K.ToUBigIntegerArray().ToHex(),
  139. srp6Client.M.ToUBigIntegerArray().ToHex(),
  140. srp6Client.SmallA.ToUBigIntegerArray().ToHex());
  141. var cmsgAuthLogonProof = new CMSG_Auth_Logon_Proof
  142. {
  143. A = srp6Client.A.ToUBigIntegerArray(),
  144. M = srp6Client.M.ToUBigIntegerArray()
  145. };
  146. this.MessageChannel.SendMessage(MessageOpcode.CMSG_AUTH_LOGON_PROOF, cmsgAuthLogonProof);
  147. await this.Handle_SMSG_Auth_Logon_Proof_M2();
  148. // 请求realm list
  149. var cmsgRealmList = new CMSG_Realm_List();
  150. this.MessageChannel.SendMessage(MessageOpcode.CMSG_REALM_LIST, cmsgRealmList);
  151. var smsgRealmList = await this.Handle_SMSG_Realm_List();
  152. string address = smsgRealmList.GateList[0].Address.ToStr();
  153. string[] split = address.Split(new[] { ':' });
  154. string gateIP = split[0];
  155. ushort gatePort = UInt16.Parse(split[1]);
  156. return Tuple.Create(gateIP, gatePort, srp6Client);
  157. }
  158. }
  159. }