Skip to content

Commit

Permalink
[fix] TCP connection is not closed when listen port was closed
Browse files Browse the repository at this point in the history
  • Loading branch information
funa-tk committed Feb 12, 2020
1 parent e38fe9b commit 6f529d0
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/main/java/core/packetproxy/Duplex.java
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

}
11 changes: 9 additions & 2 deletions src/main/java/core/packetproxy/DuplexAsync.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/core/packetproxy/DuplexFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/core/packetproxy/DuplexManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ public DuplexManager() {
duplex_list = new HashMap<Integer,Duplex>();
}

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();
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/core/packetproxy/DuplexSync.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/core/packetproxy/Listen.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/core/packetproxy/common/Endpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
5 changes: 5 additions & 0 deletions src/main/java/core/packetproxy/common/RawEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/core/packetproxy/common/SSLSocketEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/core/packetproxy/common/SocketEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/core/packetproxy/common/UDPSocketEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 6f529d0

Please sign in to comment.