diff --git a/src/main/protocol/HConnection.java b/src/main/protocol/HConnection.java index 6ce083c..d690303 100644 --- a/src/main/protocol/HConnection.java +++ b/src/main/protocol/HConnection.java @@ -1,11 +1,11 @@ package main.protocol; -import main.OSValidator; +import main.protocol.hostreplacer.HostReplacer; +import main.protocol.hostreplacer.HostReplacerFactory; import main.protocol.memory.Rc4Obtainer; import main.protocol.packethandler.Handler; import main.protocol.packethandler.IncomingHandler; import main.protocol.packethandler.OutgoingHandler; -import sun.plugin2.util.SystemUtil; import java.io.*; import java.net.InetAddress; @@ -25,13 +25,9 @@ public class HConnection { CONNECTED // CONNECTED } - private static final String hostsFileLocation; - static { - if (OSValidator.isWindows()) hostsFileLocation = System.getenv("WinDir") + "\\system32\\drivers\\etc\\hosts"; - else hostsFileLocation = "/etc/hosts"; // confirmed location on linux & mac - } + private static final HostReplacer hostsReplacer = HostReplacerFactory.get(); - private volatile boolean hostFileEdited = false; + private volatile boolean hostRedirected = false; private volatile Object[] trafficListeners = {new ArrayList(), new ArrayList(), new ArrayList()}; private volatile List stateChangeListeners = new ArrayList<>(); private volatile State state = State.NOT_CONNECTED; @@ -59,8 +55,9 @@ public class HConnection { this.actual_domain = domain; this.port = port; - if (hostFileEdited) { - removeFromHostsFile(detourIP() + " " + domain); + if (hostRedirected) { + hostsReplacer.removeRedirect(domain, detourIP()); + hostRedirected = false; } try { @@ -80,8 +77,9 @@ public class HConnection { if (DEBUG) System.out.println("waiting for client on port: " + port); setState(State.WAITING_FOR_CLIENT); - if (!hostFileEdited) { - addToHostsFile(detourIP() + " " + input_domain); + if (!hostRedirected) { + hostsReplacer.addRedirect(input_domain, detourIP()); + hostRedirected = true; } proxy = new ServerSocket(port); @@ -229,8 +227,9 @@ public class HConnection { } } private void onConnect() { - if (hostFileEdited) { - removeFromHostsFile(detourIP() + " " + input_domain); + if (hostRedirected) { + hostsReplacer.removeRedirect(input_domain, detourIP()); + hostRedirected = false; } if (proxy != null && !proxy.isClosed()) { @@ -243,8 +242,9 @@ public class HConnection { } } public void abort() { - if (hostFileEdited) { - removeFromHostsFile(detourIP() + " " + input_domain); + if (hostRedirected) { + hostsReplacer.removeRedirect(input_domain, detourIP()); + hostRedirected = false; } port = -1; setState(State.NOT_CONNECTED); @@ -259,81 +259,6 @@ public class HConnection { } } - private void addToHostsFile(String text) { - if (DEBUG) System.out.println("try add hostsfile: " + text); - try - { - ArrayList lines = new ArrayList(); - File f1 = new File(hostsFileLocation); - FileReader fr = new FileReader(f1); - BufferedReader br = new BufferedReader(fr); - String line = null; - boolean containmmm = false; - while ((line = br.readLine()) != null) - { - if (line.equals(text)) - containmmm = true; - lines.add(line); - - } - fr.close(); - br.close(); - - FileWriter fw = new FileWriter(f1); - BufferedWriter out = new BufferedWriter(fw); - - if (!containmmm) { - out.write(text); - } - - for (int i = 0; i < lines.size(); i++) { - out.write("\n"+ lines.get(i)); - } - - out.flush(); - out.close(); - hostFileEdited = true; - } - catch (Exception ex) - { - ex.printStackTrace(); - } - } - private void removeFromHostsFile(String text) { - try - { - if (DEBUG) System.out.println("try remove hostsfile: " + text); - ArrayList lines = new ArrayList(); - File f1 = new File(hostsFileLocation); - FileReader fr = new FileReader(f1); - BufferedReader br = new BufferedReader(fr); - String line = null; - while ((line = br.readLine()) != null) - { - if (!line.contains(text)) - lines.add(line); - - } - fr.close(); - br.close(); - - FileWriter fw = new FileWriter(f1); - BufferedWriter out = new BufferedWriter(fw); - - for (int i = 0; i < lines.size(); i++) { - out.write(lines.get(i)); - if (i != lines.size() - 1) out.write("\n"); - } - out.flush(); - out.close(); - hostFileEdited = false; - } - catch (Exception ex) - { - ex.printStackTrace(); - } - } - private void setState(State state) { if (state != this.state) { @@ -381,7 +306,7 @@ public class HConnection { public boolean sendToClient(HPacket message) { if (inHandler == null) return false; - new Thread(() -> inHandler.sendToStream(message.toBytes())).start(); + inHandler.sendToStream(message.toBytes()); return true; } public boolean sendToServer(HPacket message) { diff --git a/src/main/protocol/hostreplacer/HostReplacer.java b/src/main/protocol/hostreplacer/HostReplacer.java new file mode 100644 index 0000000..60c1366 --- /dev/null +++ b/src/main/protocol/hostreplacer/HostReplacer.java @@ -0,0 +1,9 @@ +package main.protocol.hostreplacer; + +public interface HostReplacer { + + void addRedirect(String original, String redirect); + + void removeRedirect(String original, String redirect); + +} diff --git a/src/main/protocol/hostreplacer/HostReplacerFactory.java b/src/main/protocol/hostreplacer/HostReplacerFactory.java new file mode 100644 index 0000000..d5f8b5d --- /dev/null +++ b/src/main/protocol/hostreplacer/HostReplacerFactory.java @@ -0,0 +1,19 @@ +package main.protocol.hostreplacer; + +import main.OSValidator; + +/** + * Created by Jonas on 04/04/18. + */ +public class HostReplacerFactory { + + public static HostReplacer get() { + + if (OSValidator.isUnix()) return new LinuxHostReplacer(); + if (OSValidator.isWindows()) return new WindowsHostReplacer(); + if (OSValidator.isMac()) return new MacOSHostReplacer(); + + return new LinuxHostReplacer(); + } + +} diff --git a/src/main/protocol/hostreplacer/LinuxHostReplacer.java b/src/main/protocol/hostreplacer/LinuxHostReplacer.java new file mode 100644 index 0000000..affd9f4 --- /dev/null +++ b/src/main/protocol/hostreplacer/LinuxHostReplacer.java @@ -0,0 +1,94 @@ +package main.protocol.hostreplacer; + +import java.io.*; +import java.util.ArrayList; + +/** + * Created by Jeunez on 04/04/18. + */ +class LinuxHostReplacer implements HostReplacer { + + protected String hostsFileLocation; + + LinuxHostReplacer() { + hostsFileLocation = "/etc/hosts"; + } + + @Override + public void addRedirect(String original, String redirect) { + String text = redirect + " " + original; + + try + { + ArrayList lines = new ArrayList(); + File f1 = new File(hostsFileLocation); + FileReader fr = new FileReader(f1); + BufferedReader br = new BufferedReader(fr); + String line = null; + boolean containmmm = false; + while ((line = br.readLine()) != null) + { + if (line.equals(text)) + containmmm = true; + lines.add(line); + + } + fr.close(); + br.close(); + + FileWriter fw = new FileWriter(f1); + BufferedWriter out = new BufferedWriter(fw); + + if (!containmmm) { + out.write(text); + } + + for (int i = 0; i < lines.size(); i++) { + out.write("\n"+ lines.get(i)); + } + + out.flush(); + out.close(); + } + catch (Exception ex) + { + ex.printStackTrace(); + } + } + + @Override + public void removeRedirect(String original, String redirect) { + String text = redirect + " " + original; + + try + { + ArrayList lines = new ArrayList(); + File f1 = new File(hostsFileLocation); + FileReader fr = new FileReader(f1); + BufferedReader br = new BufferedReader(fr); + String line = null; + while ((line = br.readLine()) != null) + { + if (!line.contains(text)) + lines.add(line); + + } + fr.close(); + br.close(); + + FileWriter fw = new FileWriter(f1); + BufferedWriter out = new BufferedWriter(fw); + + for (int i = 0; i < lines.size(); i++) { + out.write(lines.get(i)); + if (i != lines.size() - 1) out.write("\n"); + } + out.flush(); + out.close(); + } + catch (Exception ex) + { + ex.printStackTrace(); + } + } +} diff --git a/src/main/protocol/hostreplacer/MacOSHostReplacer.java b/src/main/protocol/hostreplacer/MacOSHostReplacer.java new file mode 100644 index 0000000..c2fd54e --- /dev/null +++ b/src/main/protocol/hostreplacer/MacOSHostReplacer.java @@ -0,0 +1,16 @@ +package main.protocol.hostreplacer; + +/** + * Created by Jonas on 04/04/18. + */ +class MacOSHostReplacer implements HostReplacer { + @Override + public void addRedirect(String original, String redirect) { + + } + + @Override + public void removeRedirect(String original, String redirect) { + + } +} diff --git a/src/main/protocol/hostreplacer/WindowsHostReplacer.java b/src/main/protocol/hostreplacer/WindowsHostReplacer.java new file mode 100644 index 0000000..78726d3 --- /dev/null +++ b/src/main/protocol/hostreplacer/WindowsHostReplacer.java @@ -0,0 +1,13 @@ +package main.protocol.hostreplacer; + +/** + * Created by Jonas on 04/04/18. + */ +class WindowsHostReplacer extends LinuxHostReplacer { + + WindowsHostReplacer() { + super(); + hostsFileLocation = System.getenv("WinDir") + "\\system32\\drivers\\etc\\hosts"; + } + +} diff --git a/src/main/protocol/memory/Rc4Obtainer.java b/src/main/protocol/memory/Rc4Obtainer.java index fe83c96..c179f29 100644 --- a/src/main/protocol/memory/Rc4Obtainer.java +++ b/src/main/protocol/memory/Rc4Obtainer.java @@ -20,7 +20,6 @@ public class Rc4Obtainer { client = HabboClient.create(); } - private boolean hashappened1 = false; public void setOutgoingHandler(OutgoingHandler handler) { outgoingHandler = handler; @@ -32,7 +31,6 @@ public class Rc4Obtainer { } }); } - public void setIncomingHandler(IncomingHandler handler) { incomingHandler = handler; }