diff --git a/src/main/java/core/packetproxy/Duplex.java b/src/main/java/core/packetproxy/Duplex.java index 361f3c6..7f06186 100644 --- a/src/main/java/core/packetproxy/Duplex.java +++ b/src/main/java/core/packetproxy/Duplex.java @@ -257,7 +257,10 @@ protected void sendToClientImpl(byte[] data) throws Exception {} protected void sendToServerImpl(byte[] data) throws Exception {} public void close() throws Exception {} - public Duplex crateSameConnectionDuplex() throws Exception { return null; } + public Duplex createSameConnectionDuplex() throws Exception { return null; } public byte[] prepareFastSend(byte[] data) throws Exception { return null; } public void execFastSend(byte[] data) throws Exception {} + + public boolean isListenPort(int listenPort) { return false; } + } diff --git a/src/main/java/core/packetproxy/DuplexAsync.java b/src/main/java/core/packetproxy/DuplexAsync.java index 444518b..bc3e770 100644 --- a/src/main/java/core/packetproxy/DuplexAsync.java +++ b/src/main/java/core/packetproxy/DuplexAsync.java @@ -63,9 +63,12 @@ public DuplexAsync(Endpoint client_endpoint, Endpoint server_endpoint) throws Ex disableDuplexEventListener(); } - @Override - public Duplex crateSameConnectionDuplex() throws Exception { + public boolean isListenPort(int listenPort) { + return this.client.getLocalPort() == listenPort; + } + @Override + public Duplex createSameConnectionDuplex() throws Exception { return new DuplexAsync(this.client, this.server); } public byte[] prepareFastSend(byte[] data) throws Exception { @@ -166,6 +169,10 @@ public void run() { public void close() throws Exception { client_to_server.close(); server_to_client.close(); + client_input.close(); + server_output.close(); + server_input.close(); + client_output.close(); } private Simplex createClientToServerSimplex(final InputStream in, final OutputStream out) throws Exception { diff --git a/src/main/java/core/packetproxy/DuplexFactory.java b/src/main/java/core/packetproxy/DuplexFactory.java index 98ebfeb..2a7984b 100644 --- a/src/main/java/core/packetproxy/DuplexFactory.java +++ b/src/main/java/core/packetproxy/DuplexFactory.java @@ -377,7 +377,7 @@ public InputStream getServerChunkFlowControlSink() throws Exception { // original_duplexと接続を共有しているが、イベントリスナーは再送用のものに差し替えたDuplexを返す public static Duplex createDuplexFromOriginalDuplex(Duplex original_duplex, OneShotPacket oneshot) throws Exception { - Duplex duplex = original_duplex.crateSameConnectionDuplex(); + Duplex duplex = original_duplex.createSameConnectionDuplex(); duplex.addDuplexEventListener(new Duplex.DuplexEventListener() { private Packets packets = Packets.getInstance(); private Encoder encoder = EncoderManager.getInstance().createInstance(oneshot.getEncoder(), oneshot.getAlpn()); diff --git a/src/main/java/core/packetproxy/DuplexManager.java b/src/main/java/core/packetproxy/DuplexManager.java index 1311a4d..a81e24e 100644 --- a/src/main/java/core/packetproxy/DuplexManager.java +++ b/src/main/java/core/packetproxy/DuplexManager.java @@ -35,6 +35,16 @@ public DuplexManager() { duplex_list = new HashMap(); } + public void closeAndClearDuplex(int listenPort) throws Exception { + for (int key : duplex_list.keySet()) { + Duplex d = duplex_list.get(key); + if (d.isListenPort(listenPort)) { + d.close(); + duplex_list.remove(key); + } + } + } + public int registerDuplex(Duplex duplex) { duplex_list.put(duplex.hashCode(), duplex); return duplex.hashCode(); diff --git a/src/main/java/core/packetproxy/DuplexSync.java b/src/main/java/core/packetproxy/DuplexSync.java index 801d61c..87992ee 100644 --- a/src/main/java/core/packetproxy/DuplexSync.java +++ b/src/main/java/core/packetproxy/DuplexSync.java @@ -41,7 +41,12 @@ public static OneShotPacket encodePacket(OneShotPacket one_shot) { } @Override - public Duplex crateSameConnectionDuplex() throws Exception { + public boolean isListenPort(int listenPort) { + return false; + } + + @Override + public Duplex createSameConnectionDuplex() throws Exception { return new DuplexSync(this.server); } public byte[] prepareFastSend(byte[] data) throws Exception { diff --git a/src/main/java/core/packetproxy/Listen.java b/src/main/java/core/packetproxy/Listen.java index 985baf5..36bab61 100644 --- a/src/main/java/core/packetproxy/Listen.java +++ b/src/main/java/core/packetproxy/Listen.java @@ -26,8 +26,10 @@ public ListenPort getListenInfo() { return listen_info; } public void close() throws Exception { - if (proxy != null) + if (proxy != null) { proxy.close(); + DuplexManager.getInstance().closeAndClearDuplex(listen_info.getPort()); + } } public Listen(ListenPort listen_info) throws Exception { this.listen_info = listen_info; diff --git a/src/main/java/core/packetproxy/common/Endpoint.java b/src/main/java/core/packetproxy/common/Endpoint.java index 71643ef..e1fb51a 100644 --- a/src/main/java/core/packetproxy/common/Endpoint.java +++ b/src/main/java/core/packetproxy/common/Endpoint.java @@ -23,5 +23,6 @@ public interface Endpoint { public InputStream getInputStream() throws Exception; public OutputStream getOutputStream() throws Exception; public InetSocketAddress getAddress(); + public int getLocalPort(); public String getName(); } diff --git a/src/main/java/core/packetproxy/common/RawEndpoint.java b/src/main/java/core/packetproxy/common/RawEndpoint.java index f889895..768bb9e 100644 --- a/src/main/java/core/packetproxy/common/RawEndpoint.java +++ b/src/main/java/core/packetproxy/common/RawEndpoint.java @@ -45,6 +45,11 @@ public InputStream getInputStream() throws Exception { public OutputStream getOutputStream() throws Exception { return output; } + + @Override + public int getLocalPort() { + return 0; + } @Override public String getName() { diff --git a/src/main/java/core/packetproxy/common/SSLSocketEndpoint.java b/src/main/java/core/packetproxy/common/SSLSocketEndpoint.java index afb3a18..4cfedbc 100644 --- a/src/main/java/core/packetproxy/common/SSLSocketEndpoint.java +++ b/src/main/java/core/packetproxy/common/SSLSocketEndpoint.java @@ -61,6 +61,11 @@ public OutputStream getOutputStream() throws Exception { public InetSocketAddress getAddress() { return new InetSocketAddress(socket.getInetAddress(), socket.getPort()); } + + @Override + public int getLocalPort() { + return socket.getLocalPort(); + } @Override public String getName() { diff --git a/src/main/java/core/packetproxy/common/SocketEndpoint.java b/src/main/java/core/packetproxy/common/SocketEndpoint.java index db0a75d..57308df 100644 --- a/src/main/java/core/packetproxy/common/SocketEndpoint.java +++ b/src/main/java/core/packetproxy/common/SocketEndpoint.java @@ -62,6 +62,11 @@ public InputStream getInputStream() throws Exception { public OutputStream getOutputStream() throws Exception { return socket.getOutputStream(); } + + @Override + public int getLocalPort() { + return socket.getLocalPort(); + } @Override public String getName() { diff --git a/src/main/java/core/packetproxy/common/UDPSocketEndpoint.java b/src/main/java/core/packetproxy/common/UDPSocketEndpoint.java index 29657a5..8c1c99c 100644 --- a/src/main/java/core/packetproxy/common/UDPSocketEndpoint.java +++ b/src/main/java/core/packetproxy/common/UDPSocketEndpoint.java @@ -80,6 +80,11 @@ public Void call() throws Exception { executor.submit(sendTask); executor.submit(recvTask); } + + @Override + public int getLocalPort() { + return socket.getLocalPort(); + } @Override public String getName() {