Advertisement
Guest User

Untitled

a guest
Jun 10th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.46 KB | None | 0 0
  1. package com.rs2hd.net.codec;
  2.  
  3. //import java.io.FileNotFoundException;
  4. //import java.io.IOException;
  5. //import java.util.LinkedList;
  6. //import java.util.Queue;
  7.  
  8. import org.apache.mina.common.ByteBuffer;
  9. import org.apache.mina.common.IdleStatus;
  10. import org.apache.mina.common.IoFuture;
  11. import org.apache.mina.common.IoFutureListener;
  12. import org.apache.mina.common.IoSession;
  13. import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
  14. import org.apache.mina.filter.codec.ProtocolCodecFilter;
  15. import org.apache.mina.filter.codec.ProtocolDecoderOutput;
  16.  
  17. //import com.grahamedgecombe.rs2.cache.Cache;
  18. import com.rs2hd.Constants;
  19. import com.rs2hd.WorkerThread;
  20. import com.rs2hd.model.PlayerDetails;
  21. import com.rs2hd.net.Packet;
  22. import com.rs2hd.packetbuilder.StaticPacketBuilder;
  23. import com.rs2hd.util.Misc;
  24. import com.rs2hd.util.log.Logger;
  25.  
  26. /**
  27. * Login protocol decoder.
  28. * @author Graham
  29. *
  30. */
  31. public class RS2LoginProtocolDecoder extends CumulativeProtocolDecoder {
  32.  
  33. /**
  34. * Logger instance.
  35. */
  36. private Logger logger = Logger.getInstance();
  37.  
  38. private WorkerThread workerThread;
  39.  
  40. public RS2LoginProtocolDecoder(WorkerThread workerThread) {
  41. this.workerThread = workerThread;
  42. }
  43.  
  44. // public static Cache CACHE;
  45. // static {
  46. // try {
  47. // CACHE = new Cache("data/cache/");
  48. // } catch (FileNotFoundException e) {
  49. // CACHE = null;
  50. // }
  51. // }
  52.  
  53. // private Queue<int[]> requests = new LinkedList<int[]>();
  54.  
  55. /**
  56. * Parses the data in the provided byte buffer and writes it to
  57. * <code>out</code> as a <code>Packet</code>.
  58. *
  59. * @param session The IoSession the data was read from
  60. * @param in The buffer
  61. * @param out The decoder output stream to which to write the <code>Packet</code>
  62. * @return Whether enough data was available to create a packet
  63. */
  64. @Override
  65. public boolean doDecode(IoSession session, ByteBuffer in, ProtocolDecoderOutput out) {
  66. try {
  67. Object loginStageObj = session.getAttribute("LOGIN_STAGE");
  68. int loginStage = 0;
  69. if(loginStageObj != null) {
  70. loginStage = (Integer)loginStageObj;
  71. }
  72. //Logger.log("recv login packet, stage: "+loginStage);
  73. switch(loginStage) {
  74. case -3:
  75. StaticPacketBuilder worldList = new StaticPacketBuilder();
  76. worldList.setBare(true);
  77. for(int data : Constants.WORLD_LIST_DATA) {
  78. worldList.addByte((byte) data);
  79. }
  80. session.write(worldList.toPacket());
  81. session.close();
  82. return true;
  83. case -2:
  84. if(8 <= in.remaining()) {
  85. for(int i = 0; i < 8; i++) {
  86. in.get();
  87. }
  88. StaticPacketBuilder ukeys = new StaticPacketBuilder();
  89. ukeys.setBare(true);
  90. for(int key : Constants.UPDATE_KEYS) {
  91. ukeys.addByte((byte) key);
  92. }
  93. session.write(ukeys.toPacket()).addListener(new IoFutureListener() {
  94. @Override
  95. public void operationComplete(IoFuture arg0) {
  96. arg0.getSession().close();
  97. }
  98. });
  99. return true;
  100. }
  101. in.rewind();
  102. return false;
  103. // }
  104. case -1:
  105. if(3 <= in.remaining()) {
  106. in.get();
  107. int clientVersion = in.getShort();
  108. if(clientVersion == 554) {
  109. StaticPacketBuilder u1Response = new StaticPacketBuilder();
  110. u1Response.setBare(true).addByte((byte) 0);
  111. session.write(u1Response.toPacket());
  112. session.setAttribute("LOGIN_STAGE", -2);
  113. } else {
  114. StaticPacketBuilder u1Response = new StaticPacketBuilder();
  115. u1Response.setBare(true).addByte((byte) 6);
  116. session.write(u1Response.toPacket()).addListener(new IoFutureListener() {
  117. @Override
  118. public void operationComplete(IoFuture arg0) {
  119. arg0.getSession().close();
  120. }
  121. });
  122. session.removeAttribute("LOGIN_STAGE");
  123. }
  124. return true;
  125. }
  126. in.rewind();
  127. return false;
  128. case 0: //first login packets
  129. if(2 <= in.remaining()) {
  130. int protocolId = in.get() & 0xff;
  131. int nameHash = in.get() & 0xff;
  132. System.out.println(protocolId);
  133. if(protocolId == 23) {
  134. session.setAttribute("LOGIN_STAGE", -3);
  135. } else if(protocolId == 15) {
  136. session.setAttribute("LOGIN_STAGE", -1);
  137. } else {
  138. long serverSessionKey = ((long) (java.lang.Math.random() * 99999999D) << 32) + (long) (java.lang.Math.random() * 99999999D);
  139. StaticPacketBuilder s1Response = new StaticPacketBuilder();
  140. s1Response.setBare(true).addByte((byte) 0).addLong(serverSessionKey);
  141. session.setAttribute("SERVER_SESSION_KEY", serverSessionKey);
  142. session.write(s1Response.toPacket());
  143. session.setAttribute("LOGIN_STAGE", 1);
  144. session.setAttribute("NAME_HASH", nameHash);
  145. //Logger.log("protocolId="+protocolId+"; namePart="+namePart);
  146. }
  147. return true;
  148. } else {
  149. in.rewind();
  150. return false;
  151. }
  152. case 1: //here's where we get the username and password
  153. @SuppressWarnings("unused")
  154. int loginType = -1, loginPacketSize = -1;
  155. if(3 <= in.remaining()) {
  156. loginType = in.get() & 0xff; //should be 16 or 18
  157. loginPacketSize = in.getUnsignedShort();
  158. //Logger.log("loginType="+loginType);
  159. } else {
  160. in.rewind();
  161. return false;
  162. }
  163. if(loginPacketSize <= in.remaining()) {
  164. byte[] payload = new byte[loginPacketSize];
  165. in.get(payload);
  166. Packet p = new Packet(session, -1, payload);
  167. @SuppressWarnings("unused")
  168. int loginEncryptPacketSize = loginPacketSize - (36 + 1 + 1 + 2); // can't be negative
  169. int clientVersion = p.readInt();
  170. if(clientVersion != 554) {
  171. Logger.getInstance().error("Invalid ver : " + clientVersion);
  172. session.close();
  173. return true;
  174. }
  175. p.readByte();
  176. p.readByte();
  177. p.readByte();
  178. p.readByte();
  179. p.readShort();
  180. p.readShort();
  181. p.readByte();
  182. for(int n=0; n<24; n++) {
  183. int cacheIDX = p.readByte(); //i don't care personally
  184. if(cacheIDX == 0) {
  185. // possibly a bot
  186. session.close();
  187. return true;
  188. }
  189. }
  190. p.readRS2String(); // settings string?
  191. p.readInt();
  192. p.readInt();
  193. p.readShort();
  194. for(int n=0; n<29; n++) {
  195. int junk = p.readInt();
  196. if(junk == 0 && n != 0) {
  197. // possibly a bot
  198. session.close();
  199. return true;
  200. }
  201. }
  202. int tmpEncryptPacketSize = p.readByte() & 0xff; //hopefully same as (--loginEncryptPacketSize)
  203. boolean hd = true;
  204. if(tmpEncryptPacketSize != 10) {
  205. @SuppressWarnings("unused")
  206. int encryptPacketId = p.readByte() & 0xff; //hopefully 10
  207. hd = false;
  208. }
  209. long clientSessionKey = p.readLong();
  210. long serverSessionKey = p.readLong();
  211. //int uid = p.readInt(); //unique identifier for this session i think ?
  212. long l = p.readLong();
  213. int hash = (int) (31 & l >> 16);
  214. if(hash != (Integer) session.getAttribute("NAME_HASH")) {
  215. // invalid name hash (possibly a bot attacking)
  216. session.close();
  217. return true;
  218. }
  219. String user = Misc.longToPlayerName(l), //given username
  220. pass = p.readRS2String(); //given password
  221.  
  222. int sessionKey[] = new int[4];
  223. sessionKey[0] = (int)(clientSessionKey >> 32);
  224. sessionKey[1] = (int)clientSessionKey;
  225. sessionKey[2] = (int)(serverSessionKey >> 32);
  226. sessionKey[3] = (int)serverSessionKey;
  227.  
  228. // set in ISAAC
  229. for(int i = 0; i < 4; i++) sessionKey[i] += 50;
  230. // set out ISAAC
  231.  
  232. session.removeAttribute("LOGIN_STAGE");
  233. session.removeAttribute("NAME_HASH");
  234.  
  235. /**
  236. * Here's where we add the user to the login queue, and if the login is
  237. * accepted, we change their session filter to a standard RS2ProtocolCodec.
  238. */
  239. logger.debug("Login request: [username="+user+",password="+pass+"].");
  240.  
  241. PlayerDetails d = new PlayerDetails(user, pass, session, hd);
  242. workerThread.loadPlayer(d);
  243.  
  244. session.setIdleTime(IdleStatus.BOTH_IDLE, Constants.SESSION_IDLE_TIME);
  245.  
  246. session.getFilterChain().remove("protocolFilter");
  247. session.getFilterChain().addLast("protocolFilter", new ProtocolCodecFilter(new CodecFactory()));
  248.  
  249. return true;
  250. } else {
  251. in.rewind();
  252. return false;
  253. }
  254. }
  255. } catch(Exception e) {
  256. //logger.stackTrace(e);
  257. }
  258. return false;
  259. }
  260.  
  261.  
  262.  
  263. // private ByteBuffer getFile(int requestCache, int requestId) throws IOException {
  264. // ByteBuffer buffer = ByteBuffer.allocate(520);
  265. // buffer.put((byte) requestCache);
  266. // buffer.putShort((short) requestId);
  267. // byte[] cache = CACHE.read(CACHE.index(requestCache, requestId));
  268. // int len = (((cache[1] & 0xff) << 24)+((cache[2] & 0xff) << 16)+((cache[3] & 0xff) << 8)+(cache[4] & 0xff)) + 9;
  269. // if(cache[0] == 0) {
  270. // len -= 4;
  271. // }
  272. // int c = 3;
  273. // for(int i = 0; i < len; i++) {
  274. // if(c == 512) {
  275. // buffer.put((byte) 0xFF);
  276. // c = 1;
  277. // }
  278. // buffer.put(cache[i]);
  279. // }
  280. // return buffer.flip();
  281. // }
  282.  
  283.  
  284.  
  285. /**
  286. * Releases the buffer used by the given session.
  287. *
  288. * @param session The session for which to release the buffer
  289. * @throws Exception if failed to dispose all resources
  290. */
  291. @Override
  292. public void dispose(IoSession session) throws Exception {
  293. super.dispose(session);
  294. }
  295.  
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement