From 59a0adac82f1f9d89b70bf7d9c213da3d250c890 Mon Sep 17 00:00:00 2001 From: sirjonasxx <36828922+sirjonasxx@users.noreply.github.com> Date: Fri, 1 May 2020 05:54:28 +0200 Subject: [PATCH] fix bug when clicking abort --- .../connection/proxy/NormalProxyProvider.java | 2 +- .../connection/proxy/ProxyProvider.java | 20 ++++++++++++++----- .../connection/proxy/RawIpProxyProvider.java | 2 +- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/G-Earth/src/main/java/gearth/protocol/connection/proxy/NormalProxyProvider.java b/G-Earth/src/main/java/gearth/protocol/connection/proxy/NormalProxyProvider.java index c8956bd..e319bd4 100644 --- a/G-Earth/src/main/java/gearth/protocol/connection/proxy/NormalProxyProvider.java +++ b/G-Earth/src/main/java/gearth/protocol/connection/proxy/NormalProxyProvider.java @@ -140,7 +140,7 @@ public class NormalProxyProvider extends ProxyProvider { } clearAllProxies(); - stateSetter.setState(HState.NOT_CONNECTED); + super.abort(); } @Override diff --git a/G-Earth/src/main/java/gearth/protocol/connection/proxy/ProxyProvider.java b/G-Earth/src/main/java/gearth/protocol/connection/proxy/ProxyProvider.java index 1aa5f39..a486cc4 100644 --- a/G-Earth/src/main/java/gearth/protocol/connection/proxy/ProxyProvider.java +++ b/G-Earth/src/main/java/gearth/protocol/connection/proxy/ProxyProvider.java @@ -23,13 +23,15 @@ public abstract class ProxyProvider { protected final HStateSetter stateSetter; protected final HConnection hConnection; + private Semaphore abortSemaphore = null; + public ProxyProvider(HProxySetter proxySetter, HStateSetter stateSetter, HConnection hConnection){ this.proxySetter = proxySetter; this.stateSetter = stateSetter; this.hConnection = hConnection; } - protected void startProxyThread(Socket client, Socket server, HProxy proxy) throws InterruptedException, UnknownHostException, IOException { + protected void startProxyThread(Socket client, Socket server, HProxy proxy) throws IOException, InterruptedException { final boolean[] datastream = new boolean[1]; server.setTcpNoDelay(true); client.setTcpNoDelay(true); @@ -41,22 +43,22 @@ public abstract class ProxyProvider { IncomingPacketHandler incomingHandler = new IncomingPacketHandler(client.getOutputStream(), hConnection.getTrafficObservables()); rc4Obtainer.setPacketHandlers(outgoingHandler, incomingHandler); + Semaphore abort = new Semaphore(0); + outgoingHandler.addOnDatastreamConfirmedListener(hotelVersion -> { incomingHandler.setAsDataStream(); proxy.verifyProxy(incomingHandler, outgoingHandler, hotelVersion); proxySetter.setProxy(proxy); datastream[0] = true; + abortSemaphore = abort; onConnect(); }); - Semaphore abort = new Semaphore(0); - handleInputStream(client, outgoingHandler, abort); handleInputStream(server, incomingHandler, abort); // abort can be acquired as soon as one of the sockets is closed abort.acquire(); - try { if (!server.isClosed()) server.close(); if (!client.isClosed()) client.close(); @@ -89,13 +91,21 @@ public abstract class ProxyProvider { public abstract void start() throws IOException; - public abstract void abort(); + public void abort() { + if (abortSemaphore != null) { + abortSemaphore.release(); + } + else { + stateSetter.setState(HState.NOT_CONNECTED); + } + } protected void onConnect() { stateSetter.setState(HState.CONNECTED); } protected void onConnectEnd() { proxySetter.setProxy(null); + abortSemaphore = null; stateSetter.setState(HState.NOT_CONNECTED); } diff --git a/G-Earth/src/main/java/gearth/protocol/connection/proxy/RawIpProxyProvider.java b/G-Earth/src/main/java/gearth/protocol/connection/proxy/RawIpProxyProvider.java index c00320d..7d777ee 100644 --- a/G-Earth/src/main/java/gearth/protocol/connection/proxy/RawIpProxyProvider.java +++ b/G-Earth/src/main/java/gearth/protocol/connection/proxy/RawIpProxyProvider.java @@ -123,7 +123,7 @@ public class RawIpProxyProvider extends ProxyProvider { hasMapped = false; } tryCloseProxy(); - stateSetter.setState(HState.NOT_CONNECTED); + super.abort(); } @Override