mirror of
https://github.com/sirjonasxx/G-Earth.git
synced 2024-11-23 17:00:52 +01:00
replace InputStream with DataInputStream
This commit is contained in:
parent
1dce738a12
commit
2eef54600a
@ -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,90 +51,106 @@ 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();
|
||||||
|
|
||||||
PayloadBuffer buffer = new PayloadBuffer();
|
while (!gEarthExtensionServer.isClosed()) {
|
||||||
while (!GEarthExtensionServer.isClosed()) {
|
|
||||||
if (in.available() > 0) {
|
|
||||||
byte[] data = new byte[in.available()];
|
|
||||||
in.read(data);
|
|
||||||
buffer.push(data);
|
|
||||||
|
|
||||||
HPacket[] packets = buffer.receive();
|
int length = dIn.readInt();
|
||||||
for (HPacket packet : packets) {
|
byte[] headerandbody = new byte[length + 4];
|
||||||
if (packet.headerId() == Extensions.OUTGOING_MESSAGES_IDS.INFOREQUEST) {
|
int amountRead = dIn.read(headerandbody, 4, length);
|
||||||
HPacket response = new HPacket(Extensions.INCOMING_MESSAGES_IDS.EXTENSIONINFO);
|
|
||||||
response.appendString(getTitle())
|
|
||||||
.appendString(getAuthor())
|
|
||||||
.appendString(getVersion())
|
|
||||||
.appendString(getDescription());
|
|
||||||
writeToStream(response.toBytes());
|
|
||||||
}
|
|
||||||
else if (packet.headerId() == Extensions.OUTGOING_MESSAGES_IDS.CONNECTIONSTART) {
|
|
||||||
onStartConnection();
|
|
||||||
}
|
|
||||||
else if (packet.headerId() == Extensions.OUTGOING_MESSAGES_IDS.CONNECTIONEND) {
|
|
||||||
onEndConnection();
|
|
||||||
}
|
|
||||||
else if (packet.headerId() == Extensions.OUTGOING_MESSAGES_IDS.FLAGSCHECK) {
|
|
||||||
// body = an array of G-Earths main flags
|
|
||||||
if (flagRequestCallback != null) {
|
|
||||||
int arraysize = packet.readInteger();
|
|
||||||
String[] gEarthArgs = new String[arraysize];
|
|
||||||
for (int i = 0; i < gEarthArgs.length; i++) {
|
|
||||||
gEarthArgs[i] = packet.readString();
|
|
||||||
}
|
|
||||||
flagRequestCallback.act(gEarthArgs);
|
|
||||||
}
|
|
||||||
flagRequestCallback = null;
|
|
||||||
}
|
|
||||||
else if (packet.headerId() == Extensions.OUTGOING_MESSAGES_IDS.INIT) {
|
|
||||||
init();
|
|
||||||
}
|
|
||||||
else if (packet.headerId() == Extensions.OUTGOING_MESSAGES_IDS.FREEFLOW) {
|
|
||||||
// nothing to be done yet
|
|
||||||
}
|
|
||||||
else if (packet.headerId() == Extensions.OUTGOING_MESSAGES_IDS.ONDOUBLECLICK) {
|
|
||||||
onDoubleClick();
|
|
||||||
}
|
|
||||||
else if (packet.headerId() == Extensions.OUTGOING_MESSAGES_IDS.PACKETINTERCEPT) {
|
|
||||||
HMessage habboMessage = new HMessage(packet.readString());
|
|
||||||
HPacket habboPacket = habboMessage.getPacket();
|
|
||||||
|
|
||||||
Map<Integer, List<MessageListener>> listeners =
|
if (amountRead != length) break;
|
||||||
habboMessage.getDestination() == HMessage.Side.TOCLIENT ?
|
|
||||||
incomingMessageListeners :
|
|
||||||
outgoingMessageListeners;
|
|
||||||
|
|
||||||
if (listeners.containsKey(-1)) { // registered on all packets
|
HPacket packet = new HPacket(headerandbody);
|
||||||
for (int i = listeners.get(-1).size() - 1; i >= 0; i--) {
|
packet.fixLength();
|
||||||
listeners.get(-1).get(i).act(habboMessage);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (listeners.containsKey(habboPacket.headerId())) {
|
|
||||||
for (int i = listeners.get(habboPacket.headerId()).size() - 1; i >= 0; i--) {
|
|
||||||
listeners.get(habboPacket.headerId()).get(i).act(habboMessage);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
HPacket response = new HPacket(Extensions.INCOMING_MESSAGES_IDS.MANIPULATEDPACKET);
|
|
||||||
response.appendString(habboMessage.stringify());
|
|
||||||
|
|
||||||
writeToStream(response.toBytes());
|
if (packet.headerId() == Extensions.OUTGOING_MESSAGES_IDS.INFOREQUEST) {
|
||||||
|
HPacket response = new HPacket(Extensions.INCOMING_MESSAGES_IDS.EXTENSIONINFO);
|
||||||
|
response.appendString(getTitle())
|
||||||
|
.appendString(getAuthor())
|
||||||
|
.appendString(getVersion())
|
||||||
|
.appendString(getDescription());
|
||||||
|
writeToStream(response.toBytes());
|
||||||
|
}
|
||||||
|
else if (packet.headerId() == Extensions.OUTGOING_MESSAGES_IDS.CONNECTIONSTART) {
|
||||||
|
onStartConnection();
|
||||||
|
}
|
||||||
|
else if (packet.headerId() == Extensions.OUTGOING_MESSAGES_IDS.CONNECTIONEND) {
|
||||||
|
onEndConnection();
|
||||||
|
}
|
||||||
|
else if (packet.headerId() == Extensions.OUTGOING_MESSAGES_IDS.FLAGSCHECK) {
|
||||||
|
// body = an array of G-Earths main flags
|
||||||
|
if (flagRequestCallback != null) {
|
||||||
|
int arraysize = packet.readInteger();
|
||||||
|
String[] gEarthArgs = new String[arraysize];
|
||||||
|
for (int i = 0; i < gEarthArgs.length; i++) {
|
||||||
|
gEarthArgs[i] = packet.readString();
|
||||||
|
}
|
||||||
|
flagRequestCallback.act(gEarthArgs);
|
||||||
|
}
|
||||||
|
flagRequestCallback = null;
|
||||||
|
}
|
||||||
|
else if (packet.headerId() == Extensions.OUTGOING_MESSAGES_IDS.INIT) {
|
||||||
|
init();
|
||||||
|
}
|
||||||
|
else if (packet.headerId() == Extensions.OUTGOING_MESSAGES_IDS.FREEFLOW) {
|
||||||
|
// nothing to be done yet
|
||||||
|
}
|
||||||
|
else if (packet.headerId() == Extensions.OUTGOING_MESSAGES_IDS.ONDOUBLECLICK) {
|
||||||
|
onDoubleClick();
|
||||||
|
}
|
||||||
|
else if (packet.headerId() == Extensions.OUTGOING_MESSAGES_IDS.PACKETINTERCEPT) {
|
||||||
|
String stringifiedMessage = packet.readString();
|
||||||
|
HMessage habboMessage = new HMessage(stringifiedMessage);
|
||||||
|
HPacket habboPacket = habboMessage.getPacket();
|
||||||
|
//
|
||||||
|
// System.out.println("----------");
|
||||||
|
// System.out.println(stringifiedMessage);
|
||||||
|
// System.out.println(habboPacket);
|
||||||
|
|
||||||
|
Map<Integer, List<MessageListener>> listeners =
|
||||||
|
habboMessage.getDestination() == HMessage.Side.TOCLIENT ?
|
||||||
|
incomingMessageListeners :
|
||||||
|
outgoingMessageListeners;
|
||||||
|
|
||||||
|
if (listeners.containsKey(-1)) { // registered on all packets
|
||||||
|
for (int i = listeners.get(-1).size() - 1; i >= 0; i--) {
|
||||||
|
listeners.get(-1).get(i).act(habboMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (listeners.containsKey(habboPacket.headerId())) {
|
||||||
|
for (int i = listeners.get(habboPacket.headerId()).size() - 1; i >= 0; i--) {
|
||||||
|
listeners.get(habboPacket.headerId()).get(i).act(habboMessage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
HPacket response = new HPacket(Extensions.INCOMING_MESSAGES_IDS.MANIPULATEDPACKET);
|
||||||
|
response.appendString(habboMessage.stringify());
|
||||||
|
|
||||||
|
writeToStream(response.toBytes());
|
||||||
}
|
}
|
||||||
Thread.sleep(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
} catch (IOException | InterruptedException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
// e.printStackTrace();
|
||||||
|
System.out.println("ERROR");
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
if (gEarthExtensionServer != null && !gEarthExtensionServer.isClosed()) {
|
||||||
|
try {
|
||||||
|
gEarthExtensionServer.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
55
src/main/extensions/SimpleTestExtension.java
Normal file
55
src/main/extensions/SimpleTestExtension.java
Normal 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";
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
@ -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++;
|
||||||
}
|
}
|
||||||
|
@ -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 ++;
|
||||||
}
|
}
|
||||||
|
@ -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,39 +31,34 @@ 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()];
|
int length = dIn.readInt();
|
||||||
inputStream.read(incoming);
|
byte[] headerandbody = new byte[length + 4];
|
||||||
payloadBuffer.push(incoming);
|
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) {
|
||||||
|
GEarthExtension gEarthExtension = new GEarthExtension(
|
||||||
|
packet.readString(),
|
||||||
|
packet.readString(),
|
||||||
|
packet.readString(),
|
||||||
|
packet.readString(),
|
||||||
|
connection,
|
||||||
|
onDisconnectedCallback
|
||||||
|
);
|
||||||
|
callback.act(gEarthExtension);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
HPacket[] hPackets = payloadBuffer.receive();
|
|
||||||
for (HPacket packet : hPackets) { // it should be only one packet
|
|
||||||
if (packet.headerId() == Extensions.INCOMING_MESSAGES_IDS.EXTENSIONINFO) {
|
|
||||||
|
|
||||||
GEarthExtension gEarthExtension = new GEarthExtension(
|
|
||||||
packet.readString(),
|
|
||||||
packet.readString(),
|
|
||||||
packet.readString(),
|
|
||||||
packet.readString(),
|
|
||||||
connection,
|
|
||||||
onDisconnectedCallback
|
|
||||||
);
|
|
||||||
callback.act(gEarthExtension);
|
|
||||||
|
|
||||||
break outerloop;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Thread.sleep(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (IOException | InterruptedException ignored) {}
|
} catch (IOException 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();
|
||||||
|
|
||||||
|
for (int i = receiveMessageListeners.size() - 1; i >= 0; i--) {
|
||||||
|
receiveMessageListeners.get(i).act(packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
HPacket[] hPackets = payloadBuffer.receive();
|
|
||||||
for (HPacket packet : hPackets) {
|
|
||||||
for (int i = receiveMessageListeners.size() - 1; i >= 0; i--) {
|
|
||||||
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();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user