mirror of
https://github.com/hierynomus/sshj.git
synced 2025-12-06 07:10:53 +03:00
Compare commits
6 Commits
95aab0088e
...
acad163e50
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
acad163e50 | ||
|
|
8e9e644bb8 | ||
|
|
857d56a679 | ||
|
|
0e4a8f675f | ||
|
|
c3236a7405 | ||
|
|
bfa82b4e44 |
@@ -47,7 +47,7 @@ compileJava {
|
|||||||
configurations.implementation.transitive = false
|
configurations.implementation.transitive = false
|
||||||
|
|
||||||
def bouncycastleVersion = "1.80"
|
def bouncycastleVersion = "1.80"
|
||||||
def sshdVersion = "2.14.0"
|
def sshdVersion = "2.15.0"
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation "org.slf4j:slf4j-api:2.0.17"
|
implementation "org.slf4j:slf4j-api:2.0.17"
|
||||||
@@ -89,7 +89,7 @@ testing {
|
|||||||
configureEach {
|
configureEach {
|
||||||
useJUnitJupiter()
|
useJUnitJupiter()
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation "org.slf4j:slf4j-api:2.0.16"
|
implementation "org.slf4j:slf4j-api:2.0.17"
|
||||||
implementation 'org.spockframework:spock-core:2.3-groovy-3.0'
|
implementation 'org.spockframework:spock-core:2.3-groovy-3.0'
|
||||||
implementation "org.mockito:mockito-core:5.16.1"
|
implementation "org.mockito:mockito-core:5.16.1"
|
||||||
implementation "org.assertj:assertj-core:3.27.3"
|
implementation "org.assertj:assertj-core:3.27.3"
|
||||||
@@ -98,7 +98,6 @@ testing {
|
|||||||
implementation "org.apache.sshd:sshd-sftp:$sshdVersion"
|
implementation "org.apache.sshd:sshd-sftp:$sshdVersion"
|
||||||
implementation "org.apache.sshd:sshd-scp:$sshdVersion"
|
implementation "org.apache.sshd:sshd-scp:$sshdVersion"
|
||||||
implementation "ch.qos.logback:logback-classic:1.5.18"
|
implementation "ch.qos.logback:logback-classic:1.5.18"
|
||||||
implementation 'org.glassfish.grizzly:grizzly-http-server:3.0.1'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
targets {
|
targets {
|
||||||
|
|||||||
@@ -59,7 +59,8 @@ import java.util.Properties;
|
|||||||
* net.schmizz.sshj.transport.mac.HMACMD596}</li>
|
* net.schmizz.sshj.transport.mac.HMACMD596}</li>
|
||||||
* <li>{@link net.schmizz.sshj.ConfigImpl#setCompressionFactories Compression}: {@link net.schmizz.sshj.transport.compression.NoneCompression}</li>
|
* <li>{@link net.schmizz.sshj.ConfigImpl#setCompressionFactories Compression}: {@link net.schmizz.sshj.transport.compression.NoneCompression}</li>
|
||||||
* <li>{@link net.schmizz.sshj.ConfigImpl#setKeyAlgorithms KeyAlgorithm}: {@link net.schmizz.sshj.signature.SignatureRSA}, {@link net.schmizz.sshj.signature.SignatureDSA}</li>
|
* <li>{@link net.schmizz.sshj.ConfigImpl#setKeyAlgorithms KeyAlgorithm}: {@link net.schmizz.sshj.signature.SignatureRSA}, {@link net.schmizz.sshj.signature.SignatureDSA}</li>
|
||||||
* <li>{@link net.schmizz.sshj.ConfigImpl#setRandomFactory PRNG}: {@link net.schmizz.sshj.transport.random.BouncyCastleRandom}* or {@link net.schmizz.sshj.transport.random.JCERandom}</li>
|
* <li>{@link net.schmizz.sshj.ConfigImpl#setRandomFactory BC}: {@link net.schmizz.sshj.transport.random.BouncyCastleRandom}* or {@link net.schmizz.sshj.transport.random.JCERandom}</li>
|
||||||
|
* <li>{@link net.schmizz.sshj.ConfigImpl#setRandomFactory BCFIPS}: {@link net.schmizz.sshj.transport.random.BouncyCastleFipsRandom}* or {@link net.schmizz.sshj.transport.random.JCERandom}</li>
|
||||||
* <li>{@link net.schmizz.sshj.ConfigImpl#setFileKeyProviderFactories Key file support}: {@link net.schmizz.sshj.userauth.keyprovider.PKCS8KeyFile}*, {@link
|
* <li>{@link net.schmizz.sshj.ConfigImpl#setFileKeyProviderFactories Key file support}: {@link net.schmizz.sshj.userauth.keyprovider.PKCS8KeyFile}*, {@link
|
||||||
* net.schmizz.sshj.userauth.keyprovider.OpenSSHKeyFile}*</li>
|
* net.schmizz.sshj.userauth.keyprovider.OpenSSHKeyFile}*</li>
|
||||||
* <li>{@link net.schmizz.sshj.ConfigImpl#setVersion Client version}: {@code "NET_3_0"}</li>
|
* <li>{@link net.schmizz.sshj.ConfigImpl#setVersion Client version}: {@code "NET_3_0"}</li>
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C)2009 - SSHJ Contributors
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package net.schmizz.sshj.transport.random;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* BouncyCastle <code>Random</code>. This pseudo random number generator uses BouncyCastle fips.
|
||||||
|
* The JRE random will be used when creating a new generator to add some random data to the seed.
|
||||||
|
*/
|
||||||
|
public class BouncyCastleFipsRandom extends SecureRandomProvider {
|
||||||
|
|
||||||
|
/** Named factory for the BouncyCastle <code>Random</code> */
|
||||||
|
public static class Factory
|
||||||
|
implements net.schmizz.sshj.common.Factory<Random> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Random create() {
|
||||||
|
return new BouncyCastleFipsRandom();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public BouncyCastleFipsRandom() {
|
||||||
|
super("DEFAULT", "BCFIPS");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,23 +13,28 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
/*
|
||||||
|
* Copyright (C)2009 - SSHJ Contributors
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
package net.schmizz.sshj.transport.random;
|
package net.schmizz.sshj.transport.random;
|
||||||
|
|
||||||
import org.bouncycastle.crypto.prng.RandomGenerator;
|
|
||||||
import org.bouncycastle.crypto.prng.VMPCRandomGenerator;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import java.security.SecureRandom;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* BouncyCastle <code>Random</code>. This pseudo random number generator uses the a very fast PRNG from BouncyCastle.
|
* BouncyCastle <code>Random</code>. This pseudo random number generator uses BouncyCastle non fips.
|
||||||
* The JRE random will be used when creating a new generator to add some random data to the seed.
|
* The JRE random will be used when creating a new generator to add some random data to the seed.
|
||||||
*/
|
*/
|
||||||
public class BouncyCastleRandom
|
public class BouncyCastleRandom extends SecureRandomProvider {
|
||||||
implements Random {
|
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(BouncyCastleRandom.class);
|
|
||||||
|
|
||||||
/** Named factory for the BouncyCastle <code>Random</code> */
|
/** Named factory for the BouncyCastle <code>Random</code> */
|
||||||
public static class Factory
|
public static class Factory
|
||||||
@@ -42,24 +47,7 @@ public class BouncyCastleRandom
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private final RandomGenerator random = new VMPCRandomGenerator();
|
|
||||||
|
|
||||||
public BouncyCastleRandom() {
|
public BouncyCastleRandom() {
|
||||||
logger.info("Generating random seed from SecureRandom.");
|
super("DEFAULT", "BC");
|
||||||
long t = System.currentTimeMillis();
|
|
||||||
byte[] seed = new SecureRandom().generateSeed(8);
|
|
||||||
logger.debug("Creating random seed took {} ms", System.currentTimeMillis() - t);
|
|
||||||
random.addSeedMaterial(seed);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void fill(byte[] bytes, int start, int len) {
|
|
||||||
random.nextBytes(bytes, start, len);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void fill(byte[] bytes) {
|
|
||||||
random.nextBytes(bytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,16 +15,11 @@
|
|||||||
*/
|
*/
|
||||||
package net.schmizz.sshj.transport.random;
|
package net.schmizz.sshj.transport.random;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
|
|
||||||
/** A {@link Random} implementation using the built-in {@link SecureRandom} PRNG. */
|
/** A {@link Random} implementation using the built-in {@link SecureRandom} PRNG. */
|
||||||
public class JCERandom
|
public class JCERandom extends SecureRandomProvider {
|
||||||
implements Random {
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(JCERandom.class);
|
|
||||||
|
|
||||||
/** Named factory for the JCE {@link Random} */
|
/** Named factory for the JCE {@link Random} */
|
||||||
public static class Factory
|
public static class Factory
|
||||||
implements net.schmizz.sshj.common.Factory.Named<Random> {
|
implements net.schmizz.sshj.common.Factory.Named<Random> {
|
||||||
@@ -41,39 +36,7 @@ public class JCERandom
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte[] tmp = new byte[16];
|
|
||||||
private final SecureRandom random;
|
|
||||||
|
|
||||||
JCERandom() {
|
JCERandom() {
|
||||||
logger.info("Creating new SecureRandom.");
|
super();
|
||||||
long t = System.currentTimeMillis();
|
|
||||||
random = new SecureRandom();
|
|
||||||
logger.debug("Random creation took {} ms", System.currentTimeMillis() - t);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fill the given byte-array with random bytes from this PRNG.
|
|
||||||
*
|
|
||||||
* @param foo the byte-array
|
|
||||||
* @param start the offset to start at
|
|
||||||
* @param len the number of bytes to fill
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public synchronized void fill(byte[] foo, int start, int len) {
|
|
||||||
if (start == 0 && len == foo.length) {
|
|
||||||
random.nextBytes(foo);
|
|
||||||
} else {
|
|
||||||
synchronized (this) {
|
|
||||||
if (len > tmp.length)
|
|
||||||
tmp = new byte[len];
|
|
||||||
random.nextBytes(tmp);
|
|
||||||
System.arraycopy(tmp, 0, foo, start, len);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void fill(final byte[] bytes) {
|
|
||||||
random.nextBytes(bytes);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,75 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C)2009 - SSHJ Contributors
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package net.schmizz.sshj.transport.random;
|
||||||
|
|
||||||
|
import java.security.NoSuchProviderException;
|
||||||
|
import java.security.SecureRandom;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
public class SecureRandomProvider implements Random{
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(SecureRandomProvider.class);
|
||||||
|
|
||||||
|
private byte[] tmp = new byte[16];
|
||||||
|
private SecureRandom random;
|
||||||
|
|
||||||
|
protected SecureRandomProvider() {
|
||||||
|
this.random = newRandom();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected SecureRandomProvider(String algorithm, String provider) {
|
||||||
|
this.random = newRandom(algorithm, provider);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static SecureRandom newRandom() {
|
||||||
|
return new SecureRandom();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static SecureRandom newRandom(String algorithm, String provider) {
|
||||||
|
logger.info("Generating random seed from SecureRandom of {}.", provider);
|
||||||
|
long t = System.currentTimeMillis();
|
||||||
|
try {
|
||||||
|
// Use SecureRandom with the provider
|
||||||
|
return SecureRandom.getInstance(algorithm, provider);
|
||||||
|
} catch (NoSuchProviderException e) {
|
||||||
|
throw new RuntimeException(String.format("%s provider is not in the classpath", provider), e);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(String.format("Failed to initialize SecureRandom with %s provider", provider), e);
|
||||||
|
} finally {
|
||||||
|
logger.debug("Creating random seed took {} ms", System.currentTimeMillis() - t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized void fill(byte[] bytes, int start, int len) {
|
||||||
|
if (start == 0 && len == bytes.length) {
|
||||||
|
random.nextBytes(bytes);
|
||||||
|
} else {
|
||||||
|
synchronized (this) {
|
||||||
|
if (len > tmp.length) tmp = new byte[len];
|
||||||
|
random.nextBytes(tmp);
|
||||||
|
System.arraycopy(tmp, 0, bytes, start, len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void fill(byte[] bytes) {
|
||||||
|
random.nextBytes(bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -17,7 +17,6 @@ package com.hierynomus.sshj.connection.channel.forwarded;
|
|||||||
|
|
||||||
import com.hierynomus.sshj.test.HttpServer;
|
import com.hierynomus.sshj.test.HttpServer;
|
||||||
import com.hierynomus.sshj.test.SshServerExtension;
|
import com.hierynomus.sshj.test.SshServerExtension;
|
||||||
import com.hierynomus.sshj.test.util.FileUtil;
|
|
||||||
import net.schmizz.sshj.SSHClient;
|
import net.schmizz.sshj.SSHClient;
|
||||||
import net.schmizz.sshj.connection.channel.direct.LocalPortForwarder;
|
import net.schmizz.sshj.connection.channel.direct.LocalPortForwarder;
|
||||||
import net.schmizz.sshj.connection.channel.direct.Parameters;
|
import net.schmizz.sshj.connection.channel.direct.Parameters;
|
||||||
@@ -29,13 +28,10 @@ import org.junit.jupiter.api.extension.RegisterExtension;
|
|||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.nio.file.Files;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
public class LocalPortForwarderTest {
|
public class LocalPortForwarderTest {
|
||||||
private static final String LOCALHOST_URL = "http://127.0.0.1:8080";
|
|
||||||
|
|
||||||
@RegisterExtension
|
@RegisterExtension
|
||||||
public SshServerExtension fixture = new SshServerExtension();
|
public SshServerExtension fixture = new SshServerExtension();
|
||||||
|
|
||||||
@@ -43,21 +39,19 @@ public class LocalPortForwarderTest {
|
|||||||
public HttpServer httpServer = new HttpServer();
|
public HttpServer httpServer = new HttpServer();
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
public void setUp() throws IOException {
|
public void setUp() {
|
||||||
fixture.getServer().setForwardingFilter(new AcceptAllForwardingFilter());
|
fixture.getServer().setForwardingFilter(new AcceptAllForwardingFilter());
|
||||||
File file = Files.createFile(httpServer.getDocRoot().toPath().resolve("index.html")).toFile();
|
|
||||||
FileUtil.writeToFile(file, "<html><head/><body><h1>Hi!</h1></body></html>");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldHaveWorkingHttpServer() throws IOException {
|
public void shouldHaveWorkingHttpServer() throws IOException {
|
||||||
assertEquals(200, httpGet());
|
assertEquals(HttpURLConnection.HTTP_NOT_FOUND, httpGet());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldHaveHttpServerThatClosesConnectionAfterResponse() throws IOException {
|
public void shouldHaveHttpServerThatClosesConnectionAfterResponse() throws IOException {
|
||||||
// Just to check that the test server does close connections before we try through the forwarder...
|
// Just to check that the test server does close connections before we try through the forwarder...
|
||||||
httpGetAndAssertConnectionClosedByServer(8080);
|
httpGetAndAssertConnectionClosedByServer(httpServer.getServerUrl().getPort());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -68,7 +62,8 @@ public class LocalPortForwarderTest {
|
|||||||
ServerSocket serverSocket = new ServerSocket();
|
ServerSocket serverSocket = new ServerSocket();
|
||||||
serverSocket.setReuseAddress(true);
|
serverSocket.setReuseAddress(true);
|
||||||
serverSocket.bind(new InetSocketAddress("0.0.0.0", 12345));
|
serverSocket.bind(new InetSocketAddress("0.0.0.0", 12345));
|
||||||
LocalPortForwarder localPortForwarder = sshClient.newLocalPortForwarder(new Parameters("0.0.0.0", 12345, "localhost", 8080), serverSocket);
|
final int serverPort = httpServer.getServerUrl().getPort();
|
||||||
|
LocalPortForwarder localPortForwarder = sshClient.newLocalPortForwarder(new Parameters("0.0.0.0", 12345, "localhost", serverPort), serverSocket);
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
try {
|
try {
|
||||||
localPortForwarder.listen();
|
localPortForwarder.listen();
|
||||||
@@ -90,7 +85,7 @@ public class LocalPortForwarderTest {
|
|||||||
// It returns 400 Bad Request because it's missing a bunch of info, but the HTTP response doesn't matter, we just want to test the connection closing.
|
// It returns 400 Bad Request because it's missing a bunch of info, but the HTTP response doesn't matter, we just want to test the connection closing.
|
||||||
OutputStream outputStream = socket.getOutputStream();
|
OutputStream outputStream = socket.getOutputStream();
|
||||||
PrintWriter writer = new PrintWriter(outputStream);
|
PrintWriter writer = new PrintWriter(outputStream);
|
||||||
writer.println("GET / HTTP/1.1");
|
writer.println("GET / HTTP/1.1\r\n");
|
||||||
writer.println("");
|
writer.println("");
|
||||||
writer.flush();
|
writer.flush();
|
||||||
|
|
||||||
@@ -111,7 +106,7 @@ public class LocalPortForwarderTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private int httpGet() throws IOException {
|
private int httpGet() throws IOException {
|
||||||
final URL url = new URL(LOCALHOST_URL);
|
final URL url = httpServer.getServerUrl().toURL();
|
||||||
final HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
|
final HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
|
||||||
urlConnection.setConnectTimeout(3000);
|
urlConnection.setConnectTimeout(3000);
|
||||||
urlConnection.setRequestMethod("GET");
|
urlConnection.setRequestMethod("GET");
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ package com.hierynomus.sshj.connection.channel.forwarded;
|
|||||||
|
|
||||||
import com.hierynomus.sshj.test.HttpServer;
|
import com.hierynomus.sshj.test.HttpServer;
|
||||||
import com.hierynomus.sshj.test.SshServerExtension;
|
import com.hierynomus.sshj.test.SshServerExtension;
|
||||||
import com.hierynomus.sshj.test.util.FileUtil;
|
|
||||||
import net.schmizz.sshj.SSHClient;
|
import net.schmizz.sshj.SSHClient;
|
||||||
import net.schmizz.sshj.connection.ConnectionException;
|
import net.schmizz.sshj.connection.ConnectionException;
|
||||||
import net.schmizz.sshj.connection.channel.forwarded.RemotePortForwarder;
|
import net.schmizz.sshj.connection.channel.forwarded.RemotePortForwarder;
|
||||||
@@ -27,20 +26,18 @@ import org.junit.jupiter.api.BeforeEach;
|
|||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.URI;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.file.Files;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
public class RemotePortForwarderTest {
|
public class RemotePortForwarderTest {
|
||||||
private static final PortRange RANGE = new PortRange(9000, 9999);
|
private static final PortRange RANGE = new PortRange(9000, 9999);
|
||||||
private static final String LOCALHOST = "127.0.0.1";
|
private static final String LOCALHOST = "127.0.0.1";
|
||||||
private static final String LOCALHOST_URL_FORMAT = "http://127.0.0.1:%d";
|
private static final String URL_FORMAT = "http://%s:%d";
|
||||||
private static final InetSocketAddress HTTP_SERVER_SOCKET_ADDR = new InetSocketAddress(LOCALHOST, 8080);
|
|
||||||
|
|
||||||
@RegisterExtension
|
@RegisterExtension
|
||||||
public SshServerExtension fixture = new SshServerExtension();
|
public SshServerExtension fixture = new SshServerExtension();
|
||||||
@@ -49,21 +46,21 @@ public class RemotePortForwarderTest {
|
|||||||
public HttpServer httpServer = new HttpServer();
|
public HttpServer httpServer = new HttpServer();
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
public void setUp() throws IOException {
|
public void setUp() {
|
||||||
fixture.getServer().setForwardingFilter(new AcceptAllForwardingFilter());
|
fixture.getServer().setForwardingFilter(new AcceptAllForwardingFilter());
|
||||||
File file = Files.createFile(httpServer.getDocRoot().toPath().resolve("index.html")).toFile();
|
|
||||||
FileUtil.writeToFile(file, "<html><head/><body><h1>Hi!</h1></body></html>");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldHaveWorkingHttpServer() throws IOException {
|
public void shouldHaveWorkingHttpServer() throws IOException {
|
||||||
assertEquals(200, httpGet(8080));
|
final URI serverUrl = httpServer.getServerUrl();
|
||||||
|
|
||||||
|
assertEquals(HttpURLConnection.HTTP_NOT_FOUND, httpGet(serverUrl.getHost(), serverUrl.getPort()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldDynamicallyForwardPortForLocalhost() throws IOException {
|
public void shouldDynamicallyForwardPortForLocalhost() throws IOException {
|
||||||
SSHClient sshClient = getFixtureClient();
|
SSHClient sshClient = getFixtureClient();
|
||||||
RemotePortForwarder.Forward bind = forwardPort(sshClient, "127.0.0.1", new SinglePort(0));
|
RemotePortForwarder.Forward bind = forwardPort(sshClient, LOCALHOST, new SinglePort(0));
|
||||||
assertHttpGetSuccess(bind);
|
assertHttpGetSuccess(bind);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,7 +81,7 @@ public class RemotePortForwarderTest {
|
|||||||
@Test
|
@Test
|
||||||
public void shouldForwardPortForLocalhost() throws IOException {
|
public void shouldForwardPortForLocalhost() throws IOException {
|
||||||
SSHClient sshClient = getFixtureClient();
|
SSHClient sshClient = getFixtureClient();
|
||||||
RemotePortForwarder.Forward bind = forwardPort(sshClient, "127.0.0.1", RANGE);
|
RemotePortForwarder.Forward bind = forwardPort(sshClient, LOCALHOST, RANGE);
|
||||||
assertHttpGetSuccess(bind);
|
assertHttpGetSuccess(bind);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,17 +100,22 @@ public class RemotePortForwarderTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void assertHttpGetSuccess(final RemotePortForwarder.Forward bind) throws IOException {
|
private void assertHttpGetSuccess(final RemotePortForwarder.Forward bind) throws IOException {
|
||||||
assertEquals(200, httpGet(bind.getPort()));
|
final String bindAddress = bind.getAddress();
|
||||||
|
final String address = bindAddress.isEmpty() ? LOCALHOST : bindAddress;
|
||||||
|
final int port = bind.getPort();
|
||||||
|
assertEquals(HttpURLConnection.HTTP_NOT_FOUND, httpGet(address, port));
|
||||||
}
|
}
|
||||||
|
|
||||||
private RemotePortForwarder.Forward forwardPort(SSHClient sshClient, String address, PortRange portRange) throws IOException {
|
private RemotePortForwarder.Forward forwardPort(SSHClient sshClient, String address, PortRange portRange) throws IOException {
|
||||||
while (true) {
|
while (true) {
|
||||||
|
final URI serverUrl = httpServer.getServerUrl();
|
||||||
|
final InetSocketAddress serverAddress = new InetSocketAddress(serverUrl.getHost(), serverUrl.getPort());
|
||||||
try {
|
try {
|
||||||
return sshClient.getRemotePortForwarder().bind(
|
return sshClient.getRemotePortForwarder().bind(
|
||||||
// where the server should listen
|
// where the server should listen
|
||||||
new RemotePortForwarder.Forward(address, portRange.nextPort()),
|
new RemotePortForwarder.Forward(address, portRange.nextPort()),
|
||||||
// what we do with incoming connections that are forwarded to us
|
// what we do with incoming connections that are forwarded to us
|
||||||
new SocketForwardingConnectListener(HTTP_SERVER_SOCKET_ADDR));
|
new SocketForwardingConnectListener(serverAddress));
|
||||||
} catch (ConnectionException ce) {
|
} catch (ConnectionException ce) {
|
||||||
if (!portRange.hasNext()) {
|
if (!portRange.hasNext()) {
|
||||||
throw ce;
|
throw ce;
|
||||||
@@ -122,8 +124,8 @@ public class RemotePortForwarderTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private int httpGet(final int port) throws IOException {
|
private int httpGet(final String address, final int port) throws IOException {
|
||||||
final URL url = new URL(String.format(LOCALHOST_URL_FORMAT, port));
|
final URL url = new URL(String.format(URL_FORMAT, address, port));
|
||||||
final HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
|
final HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
|
||||||
urlConnection.setConnectTimeout(3000);
|
urlConnection.setConnectTimeout(3000);
|
||||||
urlConnection.setRequestMethod("GET");
|
urlConnection.setRequestMethod("GET");
|
||||||
|
|||||||
@@ -19,42 +19,36 @@ import org.junit.jupiter.api.extension.AfterEachCallback;
|
|||||||
import org.junit.jupiter.api.extension.BeforeEachCallback;
|
import org.junit.jupiter.api.extension.BeforeEachCallback;
|
||||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||||
|
|
||||||
import java.io.File;
|
import java.net.InetSocketAddress;
|
||||||
import java.nio.file.Files;
|
import java.net.URI;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Can be used to setup a test HTTP server
|
* Can be used to setup a test HTTP server
|
||||||
*/
|
*/
|
||||||
public class HttpServer implements BeforeEachCallback, AfterEachCallback {
|
public class HttpServer implements BeforeEachCallback, AfterEachCallback {
|
||||||
|
|
||||||
private org.glassfish.grizzly.http.server.HttpServer httpServer;
|
private static final String BIND_ADDRESS = "127.0.0.1";
|
||||||
|
|
||||||
|
private com.sun.net.httpserver.HttpServer httpServer;
|
||||||
private File docRoot ;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterEach(ExtensionContext context) throws Exception {
|
public void afterEach(ExtensionContext context) {
|
||||||
try {
|
try {
|
||||||
httpServer.shutdownNow();
|
httpServer.stop(0);
|
||||||
} catch (Exception e) {}
|
} catch (Exception ignored) {}
|
||||||
try {
|
|
||||||
docRoot.delete();
|
|
||||||
} catch (Exception e) {}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void beforeEach(ExtensionContext context) throws Exception {
|
public void beforeEach(ExtensionContext context) throws Exception {
|
||||||
docRoot = Files.createTempDirectory("sshj").toFile();
|
httpServer = com.sun.net.httpserver.HttpServer.create();
|
||||||
httpServer = org.glassfish.grizzly.http.server.HttpServer.createSimpleServer(docRoot.getAbsolutePath());
|
final InetSocketAddress socketAddress = new InetSocketAddress(BIND_ADDRESS, 0);
|
||||||
|
httpServer.bind(socketAddress, 10);
|
||||||
httpServer.start();
|
httpServer.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public org.glassfish.grizzly.http.server.HttpServer getHttpServer() {
|
public URI getServerUrl() {
|
||||||
return httpServer;
|
final InetSocketAddress bindAddress = httpServer.getAddress();
|
||||||
}
|
final String serverUrl = String.format("http://%s:%d", BIND_ADDRESS, bindAddress.getPort());
|
||||||
|
return URI.create(serverUrl);
|
||||||
public File getDocRoot() {
|
|
||||||
return docRoot;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user