replace InputStream with DataInputStream

This commit is contained in:
sirjonasxx 2018-06-25 16:44:21 +02:00
parent 1dce738a12
commit 2eef54600a
6 changed files with 188 additions and 112 deletions

View File

@ -5,6 +5,7 @@ import main.protocol.HPacket;
import main.protocol.packethandler.PayloadBuffer; import main.protocol.packethandler.PayloadBuffer;
import main.ui.extensions.Extensions; import main.ui.extensions.Extensions;
import java.io.DataInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
@ -50,22 +51,26 @@ public abstract class Extension {
} }
} }
Socket gEarthExtensionServer = null;
try { try {
Socket GEarthExtensionServer = new Socket("localhost", port); gEarthExtensionServer = new Socket("localhost", port);
InputStream in = GEarthExtensionServer.getInputStream(); InputStream in = gEarthExtensionServer.getInputStream();
out = GEarthExtensionServer.getOutputStream(); DataInputStream dIn = new DataInputStream(in);
out = gEarthExtensionServer.getOutputStream();
while (!gEarthExtensionServer.isClosed()) {
int length = dIn.readInt();
byte[] headerandbody = new byte[length + 4];
int amountRead = dIn.read(headerandbody, 4, length);
if (amountRead != length) break;
HPacket packet = new HPacket(headerandbody);
packet.fixLength();
PayloadBuffer buffer = new PayloadBuffer();
while (!GEarthExtensionServer.isClosed()) {
if (in.available() > 0) {
byte[] data = new byte[in.available()];
in.read(data);
buffer.push(data);
HPacket[] packets = buffer.receive();
for (HPacket packet : packets) {
if (packet.headerId() == Extensions.OUTGOING_MESSAGES_IDS.INFOREQUEST) { if (packet.headerId() == Extensions.OUTGOING_MESSAGES_IDS.INFOREQUEST) {
HPacket response = new HPacket(Extensions.INCOMING_MESSAGES_IDS.EXTENSIONINFO); HPacket response = new HPacket(Extensions.INCOMING_MESSAGES_IDS.EXTENSIONINFO);
response.appendString(getTitle()) response.appendString(getTitle())
@ -102,8 +107,13 @@ public abstract class Extension {
onDoubleClick(); onDoubleClick();
} }
else if (packet.headerId() == Extensions.OUTGOING_MESSAGES_IDS.PACKETINTERCEPT) { else if (packet.headerId() == Extensions.OUTGOING_MESSAGES_IDS.PACKETINTERCEPT) {
HMessage habboMessage = new HMessage(packet.readString()); String stringifiedMessage = packet.readString();
HMessage habboMessage = new HMessage(stringifiedMessage);
HPacket habboPacket = habboMessage.getPacket(); HPacket habboPacket = habboMessage.getPacket();
//
// System.out.println("----------");
// System.out.println(stringifiedMessage);
// System.out.println(habboPacket);
Map<Integer, List<MessageListener>> listeners = Map<Integer, List<MessageListener>> listeners =
habboMessage.getDestination() == HMessage.Side.TOCLIENT ? habboMessage.getDestination() == HMessage.Side.TOCLIENT ?
@ -127,15 +137,22 @@ public abstract class Extension {
writeToStream(response.toBytes()); writeToStream(response.toBytes());
} }
} }
}
Thread.sleep(1);
}
} catch (IOException | InterruptedException e) { } catch (IOException e) {
// e.printStackTrace();
System.out.println("ERROR");
}
finally {
if (gEarthExtensionServer != null && !gEarthExtensionServer.isClosed()) {
try {
gEarthExtensionServer.close();
} catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
}
}
private void writeToStream(byte[] bytes) throws IOException { private void writeToStream(byte[] bytes) throws IOException {
synchronized (out) { synchronized (out) {

View File

@ -0,0 +1,55 @@
package main.extensions;
/**
* Created by Jonas on 24/06/18.
*/
public class SimpleTestExtension extends Extension {
public static void main(String[] args) {
new SimpleTestExtension(args);
}
private SimpleTestExtension(String[] args) {
super(args);
}
@Override
protected void init() {
System.out.println("init");
}
@Override
protected void onDoubleClick() {
System.out.println("doubleclick");
}
@Override
protected void onStartConnection() {
System.out.println("connection started");
}
@Override
protected void onEndConnection() {
System.out.println("connection ended");
}
@Override
protected String getTitle() {
return "Simple Test!";
}
@Override
protected String getDescription() {
return "But just for testing purpose";
}
@Override
protected String getVersion() {
return "0.1";
}
@Override
protected String getAuthor() {
return "sirjonasxx";
}
}

View File

@ -484,7 +484,7 @@ public class HPacket implements StringifyAble {
return isEdited; return isEdited;
} }
private void fixLength() { public void fixLength() {
boolean remember = isEdited; boolean remember = isEdited;
replaceInt(0, packetInBytes.length - 4); replaceInt(0, packetInBytes.length - 4);
isEdited = remember; isEdited = remember;

View File

@ -38,6 +38,7 @@ public class IncomingHandler extends Handler {
if (!hMessage.isBlocked()) { if (!hMessage.isBlocked()) {
out.write(hMessage.getPacket().toBytes()); out.write(hMessage.getPacket().toBytes());
out.flush();
} }
currentIndex++; currentIndex++;
} }

View File

@ -104,6 +104,7 @@ public class OutgoingHandler extends Handler {
currentIndex < encryptOffset ? hMessage.getPacket().toBytes() : currentIndex < encryptOffset ? hMessage.getPacket().toBytes() :
servercipher.rc4(hMessage.getPacket().toBytes()) servercipher.rc4(hMessage.getPacket().toBytes())
); );
out.flush();
} }
currentIndex ++; currentIndex ++;
} }

View File

@ -4,6 +4,7 @@ import main.protocol.HMessage;
import main.protocol.HPacket; import main.protocol.HPacket;
import main.protocol.packethandler.PayloadBuffer; import main.protocol.packethandler.PayloadBuffer;
import java.io.DataInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.Socket; import java.net.Socket;
@ -30,21 +31,20 @@ public class GEarthExtension {
connection.getOutputStream().write((new HPacket(Extensions.OUTGOING_MESSAGES_IDS.INFOREQUEST)).toBytes()); connection.getOutputStream().write((new HPacket(Extensions.OUTGOING_MESSAGES_IDS.INFOREQUEST)).toBytes());
connection.getOutputStream().flush(); connection.getOutputStream().flush();
PayloadBuffer payloadBuffer = new PayloadBuffer();
InputStream inputStream = connection.getInputStream(); InputStream inputStream = connection.getInputStream();
DataInputStream dIn = new DataInputStream(inputStream);
outerloop:
while (!connection.isClosed()) { while (!connection.isClosed()) {
if (inputStream.available() > 0) {
byte[] incoming = new byte[inputStream.available()];
inputStream.read(incoming);
payloadBuffer.push(incoming);
}
HPacket[] hPackets = payloadBuffer.receive(); int length = dIn.readInt();
for (HPacket packet : hPackets) { // it should be only one packet byte[] headerandbody = new byte[length + 4];
int amountRead = dIn.read(headerandbody, 4, length);
if (amountRead != length) break;
HPacket packet = new HPacket(headerandbody);
packet.fixLength();
if (packet.headerId() == Extensions.INCOMING_MESSAGES_IDS.EXTENSIONINFO) { if (packet.headerId() == Extensions.INCOMING_MESSAGES_IDS.EXTENSIONINFO) {
GEarthExtension gEarthExtension = new GEarthExtension( GEarthExtension gEarthExtension = new GEarthExtension(
packet.readString(), packet.readString(),
packet.readString(), packet.readString(),
@ -54,15 +54,11 @@ public class GEarthExtension {
onDisconnectedCallback onDisconnectedCallback
); );
callback.act(gEarthExtension); callback.act(gEarthExtension);
break;
break outerloop;
} }
} }
Thread.sleep(1); } catch (IOException ignored) {}
}
} catch (IOException | InterruptedException ignored) {}
}).start(); }).start();
} }
@ -77,29 +73,35 @@ public class GEarthExtension {
GEarthExtension selff = this; GEarthExtension selff = this;
new Thread(() -> { new Thread(() -> {
try { try {
PayloadBuffer payloadBuffer = new PayloadBuffer();
InputStream inputStream = connection.getInputStream(); InputStream inputStream = connection.getInputStream();
DataInputStream dIn = new DataInputStream(inputStream);
while (!connection.isClosed()) { while (!connection.isClosed()) {
if (inputStream.available() > 0) { int length = dIn.readInt();
byte[] incoming = new byte[inputStream.available()]; byte[] headerandbody = new byte[length + 4];
inputStream.read(incoming); int amountRead = dIn.read(headerandbody, 4, length);
payloadBuffer.push(incoming);
} if (amountRead != length) break;
HPacket packet = new HPacket(headerandbody);
packet.fixLength();
HPacket[] hPackets = payloadBuffer.receive();
for (HPacket packet : hPackets) {
for (int i = receiveMessageListeners.size() - 1; i >= 0; i--) { for (int i = receiveMessageListeners.size() - 1; i >= 0; i--) {
receiveMessageListeners.get(i).act(packet); receiveMessageListeners.get(i).act(packet);
} }
}
Thread.sleep(1);
} }
onDisconnectedCallback.act(selff); onDisconnectedCallback.act(selff);
} catch (IOException | InterruptedException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
if (!connection.isClosed()) {
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} }
}).start(); }).start();