mirror of
https://github.com/sirjonasxx/G-Earth.git
synced 2024-11-23 00:40:51 +01:00
structural changes for host replacing
This commit is contained in:
parent
47e6d8596e
commit
b01e0a9b07
@ -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) {
|
||||
|
9
src/main/protocol/hostreplacer/HostReplacer.java
Normal file
9
src/main/protocol/hostreplacer/HostReplacer.java
Normal file
@ -0,0 +1,9 @@
|
||||
package main.protocol.hostreplacer;
|
||||
|
||||
public interface HostReplacer {
|
||||
|
||||
void addRedirect(String original, String redirect);
|
||||
|
||||
void removeRedirect(String original, String redirect);
|
||||
|
||||
}
|
19
src/main/protocol/hostreplacer/HostReplacerFactory.java
Normal file
19
src/main/protocol/hostreplacer/HostReplacerFactory.java
Normal 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();
|
||||
}
|
||||
|
||||
}
|
94
src/main/protocol/hostreplacer/LinuxHostReplacer.java
Normal file
94
src/main/protocol/hostreplacer/LinuxHostReplacer.java
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
16
src/main/protocol/hostreplacer/MacOSHostReplacer.java
Normal file
16
src/main/protocol/hostreplacer/MacOSHostReplacer.java
Normal 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) {
|
||||
|
||||
}
|
||||
}
|
13
src/main/protocol/hostreplacer/WindowsHostReplacer.java
Normal file
13
src/main/protocol/hostreplacer/WindowsHostReplacer.java
Normal 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";
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user