From 877477a96175db50b3d202fbb965a4a88b427bcd Mon Sep 17 00:00:00 2001 From: Gavin Camp Date: Mon, 11 Jun 2018 13:26:44 +0100 Subject: [PATCH 1/2] Fixes to sshd to fix the socks connection timing issue --- pom.xml | 29 +- sshd-checkstyle.xml | 289 ------------------ .../sshd/common/forward/SocksProxy.java | 57 +++- 3 files changed, 47 insertions(+), 328 deletions(-) diff --git a/pom.xml b/pom.xml index 31b7d7252..4ff459577 100644 --- a/pom.xml +++ b/pom.xml @@ -426,7 +426,7 @@ maven-checkstyle-plugin [1.0,) - check + none @@ -767,29 +767,6 @@ - - org.apache.maven.plugins - maven-enforcer-plugin - 1.4.1 - - - enforce-versions - - enforce - - - - - - - [${min.required.maven.version},) - - - [${javac.target},) - - - - org.apache.maven.plugins maven-compiler-plugin @@ -844,7 +821,7 @@ - + org.apache.maven.plugins maven-assembly-plugin diff --git a/sshd-checkstyle.xml b/sshd-checkstyle.xml index 0a5aa7764..b09909582 100644 --- a/sshd-checkstyle.xml +++ b/sshd-checkstyle.xml @@ -12,294 +12,5 @@ --> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sshd-core/src/main/java/org/apache/sshd/common/forward/SocksProxy.java b/sshd-core/src/main/java/org/apache/sshd/common/forward/SocksProxy.java index 45e157e87..3871b41cd 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/forward/SocksProxy.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/forward/SocksProxy.java @@ -20,6 +20,7 @@ import java.io.Closeable; import java.io.IOException; +import java.util.ArrayList; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -199,6 +200,8 @@ private String getNTString(Buffer buffer) { * @see SOCKS5 */ public class Socks5 extends Proxy { + private ArrayList delayedBuffers = null; + private byte[] authMethods; private Buffer response; @@ -266,29 +269,33 @@ protected void onMessage(Buffer buffer) throws IOException { if (log.isDebugEnabled()) { log.debug("Received socks5 connection request to {}:{}", host, port); } + + delayedBuffers = new ArrayList<>(); + sendChannelSuccess(); + SshdSocketAddress remote = new SshdSocketAddress(host, port); channel = new TcpipClientChannel(TcpipClientChannel.Type.Direct, session, remote); service.registerChannel(channel); channel.open().addListener(this::onChannelOpened); } else { log.debug("Received socks5 connection message"); - super.onMessage(buffer); + + synchronized (this) { + if (delayedBuffers != null) { + log.debug("Delaying socks5 connection message"); + delayedBuffers.add(buffer); + } else { + super.onMessage(buffer); + } + } } } - - @SuppressWarnings("synthetic-access") - protected void onChannelOpened(OpenFuture future) { - int wpos = response.wpos(); + + private void sendChannelSuccess() { + int wpos = response.wpos(); response.rpos(0); response.wpos(1); - Throwable t = future.getException(); - if (t != null) { - service.unregisterChannel(channel); - channel.close(false); - response.putByte((byte) 0x01); - } else { - response.putByte((byte) 0x00); - } + response.putByte((byte) 0x00); response.wpos(wpos); try { session.writePacket(response); @@ -299,6 +306,30 @@ protected void onChannelOpened(OpenFuture future) { } } + @SuppressWarnings("synthetic-access") + protected void onChannelOpened(OpenFuture future) { + log.debug("socks5 channel open"); + + Throwable t = future.getException(); + if (t != null) { + service.unregisterChannel(channel); + channel.close(false); + } + + synchronized (this) { + if (delayedBuffers != null) { + for (Buffer buffer : delayedBuffers) { + try { + super.onMessage(buffer); + } catch (IOException e) { + log.error("Failed ({}) to send delayed response for {}: {}", e.getClass().getSimpleName(), channel, e.getMessage()); + } + } + delayedBuffers = null; + } + } + } + private String getBLString(Buffer buffer) { int length = getUByte(buffer); StringBuilder sb = new StringBuilder(); From b01d69258240a6c452eb36bb2899fe19a2a15d97 Mon Sep 17 00:00:00 2001 From: Gavin Camp Date: Wed, 13 Jun 2018 10:51:48 +0100 Subject: [PATCH 2/2] Reverted changes to poms --- pom.xml | 29 ++++- sshd-checkstyle.xml | 289 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 315 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 4ff459577..31b7d7252 100644 --- a/pom.xml +++ b/pom.xml @@ -426,7 +426,7 @@ maven-checkstyle-plugin [1.0,) - none + check @@ -767,6 +767,29 @@ + + org.apache.maven.plugins + maven-enforcer-plugin + 1.4.1 + + + enforce-versions + + enforce + + + + + + + [${min.required.maven.version},) + + + [${javac.target},) + + + + org.apache.maven.plugins maven-compiler-plugin @@ -821,7 +844,7 @@ - + org.apache.maven.plugins maven-assembly-plugin diff --git a/sshd-checkstyle.xml b/sshd-checkstyle.xml index b09909582..0a5aa7764 100644 --- a/sshd-checkstyle.xml +++ b/sshd-checkstyle.xml @@ -12,5 +12,294 @@ --> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +