/** * Check and read any incoming bytes. * @param p The Player which the frame should be created for. * @param forceRead How many bytes to read from the buffer. */ private Packet fillStream(Connection connection, int forceRead) { if (connection == null) return null; if (connection.chuckedRawPackets.Count() < forceRead) return null; PacketBuilder pckt = new PacketBuilder(); pckt.setSize(Packet.Size.Bare).addBytes(connection.chuckedRawPackets.GetRange(0, forceRead).ToArray(), 0, forceRead); connection.chuckedRawPackets.RemoveRange(0, forceRead); //delete read data. return pckt.toPacket(); }
private void updateServer(Connection connection) { if (connection == null) return; try { if (connection.loginStage == 1) { Packet fill_3 = fillStream(connection, 3); if (fill_3 == null) //really is 5, but we guess first 2 could be login server and not updateServer. return; PacketBuilder u1Response = new PacketBuilder(); u1Response.setSize(Packet.Size.Bare).addByte((byte)0); connection.SendPacket(u1Response.toPacket()); connection.loginStage = 3; updateServer(connection); } else if (connection.loginStage == 3) { Packet fill_8 = fillStream(connection, 8); if (fill_8 == null) return; PacketBuilder ukeys = new PacketBuilder(); ukeys.setSize(Packet.Size.Bare).addBytes(misc.UPDATE_KEYS); connection.SendPacket(ukeys.toPacket()); connection.loginStage = 5; updateServer(connection); } else if (connection.loginStage == 5) { Packet fill_1 = fillStream(connection, 1); if (fill_1 == null) return; //this is some unknown/not useful packet sent by client useful for quick disconnection. connection.loginStage = 255; } } catch (Exception exception) { misc.WriteError(exception.Message); } }
private void attemptPlayerLogin(Connection connection) { if (connection == null) return; if (connection.loginStage == 0) //Attempt login or update server. { Packet fill_2 = fillStream(connection, 2); if (fill_2 == null) return; int connectionType = fill_2.readByte(); if (connectionType == 15) { //it's update server connection.loginStage = 1; updateServer(connection); return; } else if (connectionType == 255) { connection.SendPacket(new PacketBuilder() .setSize(Packet.Size.Bare) .addBytes(misc.WORLD_LIST_DATA).toPacket()); connection.loginStage = 5; updateServer(connection); return; } else if (connectionType != 14) { connection.loginStage = 255; //255 is used as fail. return; } Random random = new Random(); long serverSessionKey = ((long)(random.NextDouble() * 99999999D) << 32) + (long)(random.NextDouble() * 99999999D); int longPlayerName = fill_2.readByte(); PacketBuilder s1Response = new PacketBuilder(); s1Response.setSize(Packet.Size.Bare).addByte((byte)0).addLong(serverSessionKey); connection.SendPacket(s1Response.toPacket()); connection.loginStage = 2; attemptPlayerLogin(connection); } else if (connection.loginStage == 2) { Packet fill_1 = fillStream(connection, 1); if (fill_1 == null) return; int loginType = fill_1.readByte(); if (loginType != 16 && loginType != 18 && loginType != 14) { connection.loginStage = 255; //255 is used as fail. return; } connection.loginStage = 4; attemptPlayerLogin(connection); } else if (connection.loginStage == 4) { Packet fill_2 = fillStream(connection, 2); if (fill_2 == null) return; int loginPacketSize = fill_2.readUShort(); int loginEncryptPacketSize = loginPacketSize - (36 + 1 + 1 + 2); if (loginEncryptPacketSize <= 0) { connection.loginStage = 255; return; } Packet fill_loginPacketSize = fillStream(connection, loginPacketSize); if (fill_loginPacketSize == null) return; int clientVersion = fill_loginPacketSize.readInt(); if (clientVersion != 530) { connection.loginStage = 255; return; } byte junk1 = fill_loginPacketSize.readByte(); byte lowMem = fill_loginPacketSize.readByte(); //0 is this still low mem ver? byte zero = fill_loginPacketSize.readByte(); byte b1 = fill_loginPacketSize.readByte(); ushort s1 = fill_loginPacketSize.readUShort(); ushort s2 = fill_loginPacketSize.readUShort(); byte b2 = fill_loginPacketSize.readByte(); for (int i = 0; i < 24; i++) { int cacheIDX = fill_loginPacketSize.readByte(); } string appletSettings = fill_loginPacketSize.readRS2String(); //EkKmok3kJqOeN6D3mDdihco3oPeYN2KFy6W5--vZUbNA int someInt1 = fill_loginPacketSize.readInt(); int someInt2 = fill_loginPacketSize.readInt(); ushort short1 = fill_loginPacketSize.readUShort(); for (int i = 0; i < 28; i++) { int crcOfClientClasses = fill_loginPacketSize.readInt(); } int junk2 = fill_loginPacketSize.readByte(); int encryption = fill_loginPacketSize.readByte(); if (encryption != 10 && encryption != 64) { connection.loginStage = 255; return; } long clientSessionKey = fill_loginPacketSize.readLong(); long serverSessionKey = fill_loginPacketSize.readLong(); LoginDetails loginDetails = new LoginDetails(); loginDetails.setLongName(fill_loginPacketSize.readLong()); //must start a 225. loginDetails.setUsername(misc.longToPlayerName(loginDetails.getLongName()).ToLower().Replace("_", " ").Trim()); loginDetails.setPassword(fill_loginPacketSize.readRS2String()); Console.WriteLine("Attempting to login with Username: "******" Password: " + loginDetails.getPassword()); connection.setLoginDetails(loginDetails); //start attempting to login the account. lock (playersToLoad) { playersToLoad.Enqueue(connection); } connection.loginStage = 6; } }