structural changes for host replacing

This commit is contained in:
sirjonasxx 2018-04-04 17:34:32 +02:00
parent 47e6d8596e
commit b01e0a9b07
7 changed files with 168 additions and 94 deletions

View File

@ -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<TrafficListener>(), new ArrayList<TrafficListener>(), new ArrayList<TrafficListener>()};
private volatile List<StateChangeListener> 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<String> lines = new ArrayList<String>();
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<String> lines = new ArrayList<String>();
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) {

View File

@ -0,0 +1,9 @@
package main.protocol.hostreplacer;
public interface HostReplacer {
void addRedirect(String original, String redirect);
void removeRedirect(String original, String redirect);
}

View File

@ -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();
}
}

View File

@ -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<String> lines = new ArrayList<String>();
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<String> lines = new ArrayList<String>();
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();
}
}
}

View File

@ -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) {
}
}

View File

@ -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";
}
}

View File

@ -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;
}