mirror of
https://github.com/sirjonasxx/G-Earth.git
synced 2025-01-19 00:26:27 +01:00
rewrite hconnection
This commit is contained in:
parent
7839fb4f69
commit
50a76155f8
@ -39,7 +39,6 @@ public class HConnection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).start();
|
}).start();
|
||||||
|
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
while (true) {
|
while (true) {
|
||||||
HPacket packet;
|
HPacket packet;
|
||||||
@ -98,19 +97,72 @@ public class HConnection {
|
|||||||
private volatile List<StateChangeListener> stateChangeListeners = new ArrayList<>();
|
private volatile List<StateChangeListener> stateChangeListeners = new ArrayList<>();
|
||||||
private volatile State state = State.NOT_CONNECTED;
|
private volatile State state = State.NOT_CONNECTED;
|
||||||
|
|
||||||
private volatile List<String> input_domain = new ArrayList<>(); // given string representation
|
private class Proxy {
|
||||||
private volatile List<String> actual_domain = new ArrayList<>(); // actual ip representation
|
private volatile String input_domain; //string representation of the domain to intercept
|
||||||
private volatile List<Integer> port = new ArrayList<>();
|
private volatile String actual_domain; //dns resolved domain (ignoring hosts file)
|
||||||
|
private volatile int actual_port; //port of the server
|
||||||
|
|
||||||
private volatile List<ServerSocket> proxy = new ArrayList<>();
|
private volatile int intercept_port; //port used to intercept connection (with the current implementation, must equal actual_port)
|
||||||
private volatile int realProxyIndex = -1;
|
private volatile String intercept_host; //local ip used to intercept host, example 127.0.0.1
|
||||||
|
|
||||||
private volatile Handler inHandler = null;
|
private volatile ServerSocket proxy_server = null; //listener for the client
|
||||||
private volatile Handler outHandler = null;
|
|
||||||
|
|
||||||
private volatile boolean autoDetectHost = false;
|
private volatile IncomingHandler inHandler = null; //connection with client (only initialized when verified habbo connection)
|
||||||
|
private volatile OutgoingHandler outHandler = null; //connection with server (only initialized when verified habbo connection)
|
||||||
|
|
||||||
private volatile String clientHostAndPort = "";
|
|
||||||
|
public Proxy(String input_domain, String actual_domain, int actual_port, int intercept_port, String intercept_host) {
|
||||||
|
this.input_domain = input_domain;
|
||||||
|
this.actual_domain = actual_domain;
|
||||||
|
this.actual_port = actual_port;
|
||||||
|
this.intercept_host = intercept_host;
|
||||||
|
this.intercept_port = intercept_port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void initProxy(ServerSocket socket) {
|
||||||
|
this.proxy_server = socket;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void verifyProxy(IncomingHandler incomingHandler, OutgoingHandler outgoingHandler) {
|
||||||
|
this.inHandler = incomingHandler;
|
||||||
|
this.outHandler = outgoingHandler;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getActual_port() {
|
||||||
|
return actual_port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getIntercept_port() {
|
||||||
|
return intercept_port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ServerSocket getProxy_server() {
|
||||||
|
return proxy_server;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getActual_domain() {
|
||||||
|
return actual_domain;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getInput_domain() {
|
||||||
|
return input_domain;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getIntercept_host() {
|
||||||
|
return intercept_host;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IncomingHandler getInHandler() {
|
||||||
|
return inHandler;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OutgoingHandler getOutHandler() {
|
||||||
|
return outHandler;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private volatile List<Proxy> potentialProxies = new ArrayList<>();
|
||||||
|
private volatile Proxy actual_proxy = null;
|
||||||
private volatile String hotelVersion = "";
|
private volatile String hotelVersion = "";
|
||||||
|
|
||||||
|
|
||||||
@ -137,41 +189,45 @@ public class HConnection {
|
|||||||
List<String> potentialHost = new ArrayList<>();
|
List<String> potentialHost = new ArrayList<>();
|
||||||
potentialHost.add(domain+":"+port);
|
potentialHost.add(domain+":"+port);
|
||||||
prepare(potentialHost);
|
prepare(potentialHost);
|
||||||
realProxyIndex = 0;
|
actual_proxy = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void prepare(List<String> allPotentialHosts) {
|
private void prepare(List<String> allPotentialHosts) {
|
||||||
setState(State.PREPARING);
|
setState(State.PREPARING);
|
||||||
input_domain.clear();
|
|
||||||
actual_domain.clear();
|
|
||||||
port.clear();
|
|
||||||
clearAllProxies();
|
clearAllProxies();
|
||||||
realProxyIndex = -1;
|
actual_proxy = null;
|
||||||
|
|
||||||
for (String host : allPotentialHosts) {
|
|
||||||
String[] split = host.split(":");
|
|
||||||
input_domain.add(split[0]);
|
|
||||||
port.add(Integer.parseInt(split[1]));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hostRedirected) {
|
if (hostRedirected) {
|
||||||
removeFromHosts();
|
removeFromHosts();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
List<String> willremove = new ArrayList<>();
|
List<String> willremove = new ArrayList<>();
|
||||||
|
|
||||||
|
int c = 0;
|
||||||
|
|
||||||
for (String host : allPotentialHosts) {
|
for (String host : allPotentialHosts) {
|
||||||
|
String[] split = host.split(":");
|
||||||
|
String input_dom = split[0];
|
||||||
|
int port = Integer.parseInt(split[1]);
|
||||||
|
String actual_dom;
|
||||||
|
|
||||||
InetAddress address = null;
|
InetAddress address = null;
|
||||||
try {
|
try {
|
||||||
address = InetAddress.getByName(host.split(":")[0]);
|
address = InetAddress.getByName(input_dom);
|
||||||
actual_domain.add(address.getHostAddress());
|
actual_dom = address.getHostAddress();
|
||||||
} catch (UnknownHostException e) {
|
|
||||||
// e.printStackTrace();
|
|
||||||
actual_domain.add(null);
|
|
||||||
willremove.add(host);
|
|
||||||
}
|
}
|
||||||
|
catch (UnknownHostException e) {
|
||||||
|
willremove.add(host);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
int intercept_port = port;
|
||||||
|
String intercept_host = "127.0." + (c / 254) + "." + (1 + c % 254);
|
||||||
|
potentialProxies.add(new Proxy(input_dom, actual_dom, port, intercept_port, intercept_host));
|
||||||
|
c++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
List<Object> additionalCachedHotels = Cacher.getList(HOTELS_CACHE_KEY);
|
List<Object> additionalCachedHotels = Cacher.getList(HOTELS_CACHE_KEY);
|
||||||
if (additionalCachedHotels != null) {
|
if (additionalCachedHotels != null) {
|
||||||
for (String host : willremove) {
|
for (String host : willremove) {
|
||||||
@ -192,29 +248,25 @@ public class HConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
for (int i = 0; i < actual_domain.size(); i++) {
|
for (int c = 0; c < potentialProxies.size(); c++) {
|
||||||
if (actual_domain.get(i) == null) continue;
|
Proxy potentialProxy = potentialProxies.get(c);
|
||||||
|
|
||||||
ServerSocket proxy = new ServerSocket(port.get(i), 10, InetAddress.getByName("127.0.0." + (i+1)));
|
ServerSocket proxy_server = new ServerSocket(potentialProxy.getIntercept_port(), 10, InetAddress.getByName(potentialProxy.getIntercept_host()));
|
||||||
this.proxy.add(proxy);
|
potentialProxy.initProxy(proxy_server);
|
||||||
String dom = actual_domain.get(i);
|
|
||||||
Integer port2 = port.get(i);
|
|
||||||
|
|
||||||
int[] i2 = {i};
|
|
||||||
|
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(100);
|
Thread.sleep(100);
|
||||||
while ((state == State.WAITING_FOR_CLIENT) && !proxy.isClosed()) {
|
while ((state == State.WAITING_FOR_CLIENT) && !proxy_server.isClosed()) {
|
||||||
try {
|
try {
|
||||||
Socket client = proxy.accept();
|
Socket client = proxy_server.accept();
|
||||||
realProxyIndex = i2[0];
|
actual_proxy = potentialProxy;
|
||||||
closeAllProxies(i2[0]);
|
closeAllProxies(actual_proxy);
|
||||||
if (DEBUG) System.out.println("accepted a proxy");
|
if (DEBUG) System.out.println("accepted a proxy");
|
||||||
|
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
try {
|
try {
|
||||||
startProxyThread(client, dom, port2);
|
startProxyThread(client, potentialProxy);
|
||||||
} catch (InterruptedException | IOException e) {
|
} catch (InterruptedException | IOException e) {
|
||||||
// TODO Auto-generated catch block
|
// TODO Auto-generated catch block
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -237,10 +289,10 @@ public class HConnection {
|
|||||||
if (DEBUG) System.out.println("done waiting for clients with: " + this.state );
|
if (DEBUG) System.out.println("done waiting for clients with: " + this.state );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private void startProxyThread(Socket client, String ip, int port) throws InterruptedException, UnknownHostException, IOException {
|
private void startProxyThread(Socket client, Proxy proxy) throws InterruptedException, UnknownHostException, IOException {
|
||||||
final boolean[] datastream = new boolean[1];
|
final boolean[] datastream = new boolean[1];
|
||||||
|
|
||||||
Socket habbo_server = new Socket(ip, port);
|
Socket habbo_server = new Socket(proxy.actual_domain, proxy.actual_port);
|
||||||
|
|
||||||
OutputStream client_out = client.getOutputStream();
|
OutputStream client_out = client.getOutputStream();
|
||||||
InputStream client_in = client.getInputStream();
|
InputStream client_in = client.getInputStream();
|
||||||
@ -261,13 +313,10 @@ public class HConnection {
|
|||||||
outgoingHandler.addOnDatastreamConfirmedListener(hotelVersion -> {
|
outgoingHandler.addOnDatastreamConfirmedListener(hotelVersion -> {
|
||||||
incomingHandler.setAsDataStream();
|
incomingHandler.setAsDataStream();
|
||||||
this.hotelVersion = hotelVersion;
|
this.hotelVersion = hotelVersion;
|
||||||
clientHostAndPort = client.getLocalAddress().getHostAddress() + ":" + client.getPort();
|
|
||||||
onConnect();
|
onConnect();
|
||||||
|
|
||||||
if (DEBUG) System.out.println(clientHostAndPort);
|
|
||||||
setState(State.CONNECTED);
|
setState(State.CONNECTED);
|
||||||
outHandler = outgoingHandler;
|
proxy.verifyProxy(incomingHandler, outgoingHandler);
|
||||||
inHandler = incomingHandler;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// wachten op data van client
|
// wachten op data van client
|
||||||
@ -301,8 +350,8 @@ public class HConnection {
|
|||||||
}
|
}
|
||||||
if (datastream[0]) {
|
if (datastream[0]) {
|
||||||
setState(State.NOT_CONNECTED);
|
setState(State.NOT_CONNECTED);
|
||||||
outHandler = null;
|
proxy.verifyProxy(null, null);
|
||||||
inHandler = null;
|
actual_proxy = null;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}).start();
|
}).start();
|
||||||
@ -360,26 +409,22 @@ public class HConnection {
|
|||||||
removeFromHosts();
|
removeFromHosts();
|
||||||
}
|
}
|
||||||
|
|
||||||
port.clear();
|
actual_proxy = null;
|
||||||
input_domain.clear();
|
|
||||||
actual_domain.clear();
|
|
||||||
realProxyIndex = -1;
|
|
||||||
|
|
||||||
setState(State.NOT_CONNECTED);
|
setState(State.NOT_CONNECTED);
|
||||||
clearAllProxies();
|
clearAllProxies();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void clearAllProxies() {
|
private void clearAllProxies() {
|
||||||
closeAllProxies(-1);
|
closeAllProxies(null);
|
||||||
proxy.clear();
|
potentialProxies = new ArrayList<>();
|
||||||
}
|
}
|
||||||
private void closeAllProxies(int except) {
|
private void closeAllProxies(Proxy except) {
|
||||||
for (int i = 0; i < proxy.size(); i++) {
|
for (Proxy proxy : potentialProxies) {
|
||||||
if (i != except) {
|
if (except != proxy) {
|
||||||
ServerSocket prox = proxy.get(i);
|
if (proxy.proxy_server != null && !proxy.proxy_server.isClosed()) {
|
||||||
if (prox != null && !prox.isClosed()) {
|
|
||||||
try {
|
try {
|
||||||
prox.close();
|
proxy.proxy_server.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// TODO Auto-generated catch block
|
// TODO Auto-generated catch block
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -392,10 +437,8 @@ public class HConnection {
|
|||||||
|
|
||||||
private void addToHosts() {
|
private void addToHosts() {
|
||||||
List<String> linesTemp = new ArrayList<>();
|
List<String> linesTemp = new ArrayList<>();
|
||||||
for (int i = 0; i < input_domain.size(); i++) {
|
for (Proxy proxy : potentialProxies) {
|
||||||
if (actual_domain.get(i) != null) {
|
linesTemp.add(proxy.getIntercept_host() + " " + proxy.getInput_domain());
|
||||||
linesTemp.add(("127.0.0." + (i+1)) + " " + input_domain.get(i));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String[] lines = new String[linesTemp.size()];
|
String[] lines = new String[linesTemp.size()];
|
||||||
@ -407,10 +450,8 @@ public class HConnection {
|
|||||||
}
|
}
|
||||||
private void removeFromHosts(){
|
private void removeFromHosts(){
|
||||||
List<String> linesTemp = new ArrayList<>();
|
List<String> linesTemp = new ArrayList<>();
|
||||||
for (int i = 0; i < input_domain.size(); i++) {
|
for (Proxy proxy : potentialProxies) {
|
||||||
if (actual_domain.get(i) != null) {
|
linesTemp.add(proxy.getIntercept_host() + " " + proxy.getInput_domain());
|
||||||
linesTemp.add(("127.0.0." + (i+1)) + " " + input_domain.get(i));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String[] lines = new String[linesTemp.size()];
|
String[] lines = new String[linesTemp.size()];
|
||||||
@ -428,7 +469,6 @@ public class HConnection {
|
|||||||
}
|
}
|
||||||
if (state != this.state) {
|
if (state != this.state) {
|
||||||
if (state != State.CONNECTED) {
|
if (state != State.CONNECTED) {
|
||||||
clientHostAndPort = "";
|
|
||||||
hotelVersion = "";
|
hotelVersion = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -466,27 +506,27 @@ public class HConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public int getPort() {
|
public int getPort() {
|
||||||
if (realProxyIndex == -1) return -1;
|
if (actual_proxy == null) return -1;
|
||||||
return port.get(realProxyIndex);
|
return actual_proxy.getIntercept_port();
|
||||||
}
|
}
|
||||||
public String getHost() {
|
public String getHost() {
|
||||||
if (realProxyIndex == -1) return "<auto-detect>";
|
if (actual_proxy == null) return "<auto-detect>";
|
||||||
return actual_domain.get(realProxyIndex);
|
return actual_proxy.getActual_domain();
|
||||||
}
|
}
|
||||||
public String getDomain() {
|
public String getDomain() {
|
||||||
if (realProxyIndex == -1) return "<auto-detect>";
|
if (actual_proxy == null) return "<auto-detect>";
|
||||||
return input_domain.get(realProxyIndex);
|
return actual_proxy.getInput_domain();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public boolean sendToClient(HPacket message) {
|
private boolean sendToClient(HPacket message) {
|
||||||
if (inHandler == null) return false;
|
if (actual_proxy == null || actual_proxy.getInHandler() == null) return false;
|
||||||
inHandler.sendToStream(message.toBytes());
|
actual_proxy.getInHandler().sendToStream(message.toBytes());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
public boolean sendToServer(HPacket message) {
|
private boolean sendToServer(HPacket message) {
|
||||||
if (outHandler == null) return false;
|
if (actual_proxy == null || actual_proxy.getOutHandler() == null) return false;
|
||||||
outHandler.sendToStream(message.toBytes());
|
actual_proxy.getOutHandler().sendToStream(message.toBytes());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -503,7 +543,10 @@ public class HConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String getClientHostAndPort() {
|
public String getClientHostAndPort() {
|
||||||
return clientHostAndPort;
|
if (actual_proxy == null || actual_proxy.getIntercept_host() == null || actual_proxy.getIntercept_port() == -1) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return actual_proxy.getIntercept_host() + ":" + actual_proxy.getIntercept_port();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getHotelVersion() {
|
public String getHotelVersion() {
|
||||||
|
@ -85,7 +85,7 @@ public class Scheduler extends SubForm {
|
|||||||
getHConnection().sendToServerAsync(item.getPacketProperty().get());
|
getHConnection().sendToServerAsync(item.getPacketProperty().get());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
getHConnection().sendToClient(item.getPacketProperty().get());
|
getHConnection().sendToClientAsync(item.getPacketProperty().get());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user