protected void setEncryption(SecretKey keystr, packet reader) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IOException { IvParameterSpec ivr = new IvParameterSpec(keystr.getEncoded()); Cipher cipher = Cipher.getInstance("AES/CFB8/NoPadding"); cipher.init(Cipher.DECRYPT_MODE, keystr, ivr); cis = new CipherInputStream(sock.getInputStream(), cipher); Cipher cipher2 = Cipher.getInstance("AES/CFB8/NoPadding"); cipher2.init(Cipher.ENCRYPT_MODE, keystr, ivr); cos = new CipherOutputStream(sock.getOutputStream(), cipher2); reader.setEncryptedStreams(cis, cos); cin = cis; cout = cos; reader.setEncrypted(); sendmsg("�ncryption activated"); }
private void processpreloginpacket(int pid, int len, ByteBuffer buf) throws Exception { Event e = null; switch (pid) { case ConnectionResetPacket.ID2: { ConnectionResetEvent dcevent = new ConnectionResetPacket(buf, reader).Read(); sendmsg("�erver closed connection. Reason:\n" + dcevent.getReason()); communicationavailable = false; } break; case SetCompressionPacket.ID: SetCompressionPacket compack = new SetCompressionPacket(buf, reader); compack.Read(); sendmsg("�ompression activated"); reader.compression = true; break; case LoginSuccessPacket.ID: LoginSuccessEvent logse = new LoginSuccessPacket(buf, reader).Read(); String uuid = logse.getUUID(); String username = logse.getUsername(); sendmsg("�eceived UUID: " + uuid); sendmsg("�eceived Nick: " + username); nowConnected(); e = logse; encryptiondecided = true; break; case EncryptionRequestPacket.ID: // Maybe the server wants some encryption EncryptionRequestPacket encr = new EncryptionRequestPacket(buf, reader); try { encr.Read(); encr.Write(bot); setEncryption(encr.getSecret(), reader); } catch (BufferUnderflowException e1) { } break; } if (e != null) { storage.pluginManager.invokeEvent(e, bot.getAllowedPlugins()); } }
private void processpacket(int pid, int len, ByteBuffer buf, packetStruct packet) throws Exception { Event e = null; switch (pid) { // default: // sendmsg("�Unhandled packet " + pid); // new Ignored_Packet(len, reader).Read(); // break; /* * case EntityStatusPacket.ID: e = new EntityStatusPacket(reader, * buf).Read(); break; */ case SignUpdatePacket.ID: e = new SignUpdatePacket(buf, reader).Read(); break; case TimeUpdatePacket.ID: new TimeUpdatePacket(buf, reader).Read(); break; case UpdateHealthPacket.ID: UpdateHealthEvent upheal = new UpdateHealthPacket(reader, buf).Read(); this.Health = upheal.getHealth(); this.Food = upheal.getFood(); this.Satur = upheal.getSaturation(); bot.updateHealth(this.Health, this.Food, this.Satur); break; case PlayerPositionAndLookPacket.ID: PlayerPositionAndLookEvent ppal = new PlayerPositionAndLookPacket(reader, buf).Read(); this.pos_x = (int) ppal.getX(); this.pos_y = (int) ppal.getY(); this.pos_z = (int) ppal.getZ(); bot.updateposition(this.pos_x, this.pos_y, this.pos_z); break; case TabCompletePacket.ID: TabCompletePacket tabpack = new TabCompletePacket(reader, buf); TabCompleteEvent tabev = tabpack.Read(); tabcomp.setNames(tabev.getNames()); tabcomp.getNext(); e = tabev; break; case KeepAlivePacket.ID: // Keep us alive KeepAlivePacket keepalivepack = new KeepAlivePacket(reader, buf); keepalivepack.Read(); keepalivepack.Write(); break; case JoinGamePacket.ID: // join game JoinGameEvent joingameevent = new JoinGamePacket(buf, reader).Read(); if (joingameevent.getMaxPlayers() > 25 && joingameevent.getMaxPlayers() < 50) { // 2 Columns 20 rows settablesize(2, 20); } else if (joingameevent.getMaxPlayers() >= 50 && joingameevent.getMaxPlayers() < 70) { // 3 Columns 20 rows settablesize(3, 20); } else if (joingameevent.getMaxPlayers() >= 70) { // 4 Columns 20 rows settablesize(4, 20); } else { // 1 Columns 20 rows settablesize(1, 20); } e = joingameevent; break; case ChatPacket.ID: // Chat ChatEvent event = new ChatPacket(buf, reader).Read(); if (!event.isIgnored()) { String msg = event.getFormatedMessage(); sendChatMsg(msg); tryandsendlogin(); } e = event; break; case SpawnPositionPacket.ID: new SpawnPositionPacket(buf, reader).Read(); break; case RespawnPacket.ID: new RespawnPacket(buf, reader).Read(); break; case PlayerListItemPacket.ID: PlayerListItemPacket playerlistitem = new PlayerListItemPacket(buf, reader); PlayerListEvent plevent = playerlistitem.Read(Tablist, Tablist_nicks); if (servePlayerList(plevent, Tablist, Tablist_nicks)) { this.refreshTablist(); } e = plevent; break; case DisplayScoreBoardPacket.ID: // Scoreboard display new DisplayScoreBoardPacket(buf, reader).Read(); break; case SetCompressionPacket.ID2: new SetCompressionPacket(buf, reader).Read(); sendmsg("Compression activated"); // compack.Write(); reader.compression = true; break; case TeamPacket.ID: // Teams this.handleteam(new TeamPacket(buf, reader).Read()); break; case PluginMessagePacket.ID: // Plugin message // PluginMessageEvent plmsge = new PluginMessagePacket(buf, // reader).Read(); // e = plmsge; // sendmsg("Channel: " + plmsge.getChannel() + " Message: " + // plmsge.getMessageAsString()); break; case ConnectionResetPacket.ID: ConnectionResetEvent dcevent = new ConnectionResetPacket(buf, reader).Read(); sendmsg("�erver closed connection. (" + dcevent.getReason() + ")"); break; } if (e != null) { storage.pluginManager.invokeEvent(e, bot.getAllowedPlugins()); } }
private void mainloop() { // Main loop try { sendmsg("�onnecting"); if (protocolversion == 4 || protocolversion == 5) { this.maxpacketid = 0x40; } else if (protocolversion == 47) { this.maxpacketid = 0x49; } BotInitEvent botinitevent = new BotInitEvent(bot); storage.pluginManager.invokeEvent(botinitevent, bot.getAllowedPlugins()); Tablist = new ArrayList<String>(); Tablist_nicks = new HashMap<String, String>(); playerTeams = new HashMap<String, String>(); TeamsByNames = new HashMap<String, team_struct>(); haslogged = false; encryptiondecided = false; sock = null; bot.seticon(ICONSTATE.CONNECTING); if (bot.useProxy()) { Proxy proxy = bot.getProxyAddress(); if (proxy != null) { sock = new Socket(proxy); sock.connect(bot.getServerAddress()); } else { bot.logmsg("�nvalid proxy"); this.reconnect = false; return; } } else { sock = new Socket(bot.serverip, bot.serverport); } cin = sock.getInputStream(); cout = sock.getOutputStream(); reader = new packet(bot, cin, cout); reader.ProtocolVersion = protocolversion; // definepackets(reader); storage.changemenuitems(); // First, we must send HandShake and hope for good response new HandShakePacket(reader).Write(bot.serverip, bot.serverport); new LoginStartPacket(reader).Write(bot.username); connecttime = System.currentTimeMillis() / 1000; // Init routine communicationavailable = true; int pid; int len = 0; boolean connectedicon = true; // Connection established, time to create AntiAFK this.afkter = new AntiAFK(this); this.afkter.start(); // The loop while (communicationavailable) { packetStruct packet = reader.readNexter(); if (packet == null) { continue; } len = packet.packetLength; pid = packet.packetID; int datalen = packet.dataLength; // TODO: Debug part // System.out.println("PID: " + Integer.toHexString(pid) + // " LEN: " + len); if (pid > maxpacketid) { sendmsg("Received packet id " + pid + " (Length: " + len + ",Compression: " + reader.compression + ", Encryption: " + reader.isEncrypted() + ")"); sendmsg("�alformed communication"); break; } if (connectedicon) { bot.seticon(ICONSTATE.CONNECTED); } PacketReceiveEvent packevent = new PacketReceiveEvent(bot, packet, !encryptiondecided); storage.pluginManager.invokeEvent(packevent, bot.getAllowedPlugins()); if (packevent.isCancelled()) { continue; } if (datalen > 0) { ByteBuffer buf = packet.generateBuffer(); if (encryptiondecided) { processpacket(pid, datalen, buf, packet); } else { processpreloginpacket(pid, datalen, buf); } } } } catch (UnknownHostException e) { sendmsg("�o such host is known."); } catch (SerialException e) { } catch (IllegalArgumentException e) { if (!storage.reportthis(e)) { sendmsg("�ata stream error happened. Error log written into main tab. Please report this."); e.printStackTrace(); } } catch (NullPointerException e) { if (!storage.reportthis(e)) { sendmsg("�trange error happened. Please report this."); e.printStackTrace(); } } catch (SocketException e) { } catch (IOException e) { if (!storage.reportthis(e)) { e.printStackTrace(); } } catch (RuntimeException e) { if (!storage.reportthis(e)) { sendmsg("�rror happened. Error log written into main tab. Please report this."); e.printStackTrace(); } } catch (Exception e) { if (!storage.reportthis(e)) { sendmsg("�rror happened. Error log written into main tab. Please report this."); e.printStackTrace(); } } }
/** * Sends packet to server, if available * @param packet * @throws IOException */ public void sendIfAvailable(packetStruct packet) throws IOException { reader.Send(packet); }