|
@@ -0,0 +1,85 @@
|
|
|
+package de.tudarmstadt.informatik.hostage.protocol;
|
|
|
+
|
|
|
+import java.io.BufferedInputStream;
|
|
|
+import java.io.BufferedOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.net.Socket;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import de.tudarmstadt.informatik.hostage.wrapper.Packet;
|
|
|
+
|
|
|
+public class GhostProtocol implements Protocol {
|
|
|
+
|
|
|
+ private boolean isClosed = false;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int getDefaultPort() {
|
|
|
+ return 5050;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public TALK_FIRST whoTalksFirst() {
|
|
|
+ return TALK_FIRST.CLIENT;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Packet> processMessage(Packet packet) {
|
|
|
+ List<Packet> response = new ArrayList<Packet>();
|
|
|
+ try {
|
|
|
+ if(attacker == null) {
|
|
|
+ attacker = new Socket("127.0.0.1", 5050);
|
|
|
+ in = new BufferedInputStream(attacker.getInputStream());
|
|
|
+ out = new BufferedOutputStream(attacker.getOutputStream());
|
|
|
+ }
|
|
|
+ if(attacker.isInputShutdown() || attacker.isOutputShutdown()) {
|
|
|
+ in.close();
|
|
|
+ out.close();
|
|
|
+ attacker.close();
|
|
|
+ isClosed = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ out.write(packet.getMessage());
|
|
|
+ out.flush();
|
|
|
+
|
|
|
+ int availableBytes;
|
|
|
+ while((availableBytes = in.available()) <= 0) {
|
|
|
+ try {
|
|
|
+ Thread.sleep(1);
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ byte[] answer = new byte[availableBytes];
|
|
|
+ in.read(answer);
|
|
|
+ response.add(new Packet(answer));
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean isClosed() {
|
|
|
+ return isClosed;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean isSecure() {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Class<byte[]> getType() {
|
|
|
+ return byte[].class;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String toString() {
|
|
|
+ return "GhostProtocol";
|
|
|
+ }
|
|
|
+
|
|
|
+ Socket attacker;
|
|
|
+ BufferedInputStream in;
|
|
|
+ BufferedOutputStream out;
|
|
|
+}
|