Java源码示例:org.apache.sshd.client.channel.ChannelDirectTcpip
示例1
@Test
public void testForwardingChannel() throws Exception {
try (ClientSession session = createNativeSession()) {
SshdSocketAddress local = new SshdSocketAddress("", 0);
SshdSocketAddress remote = new SshdSocketAddress(TEST_LOCALHOST, echoPort);
try (ChannelDirectTcpip channel = session.createDirectTcpipChannel(local, remote)) {
channel.open().verify(9L, TimeUnit.SECONDS);
String expected = getCurrentTestName();
byte[] bytes = expected.getBytes(StandardCharsets.UTF_8);
try (OutputStream output = channel.getInvertedIn();
InputStream input = channel.getInvertedOut()) {
output.write(bytes);
output.flush();
byte[] buf = new byte[bytes.length + Long.SIZE];
int n = input.read(buf);
String res = new String(buf, 0, n);
assertEquals("Mismatched data", expected, res);
}
channel.close(false);
}
}
}
示例2
@Test
public void testForwardingChannel() throws Exception {
try (ClientSession session = createNativeSession()) {
SshdSocketAddress local = new SshdSocketAddress("", 0);
SshdSocketAddress remote = new SshdSocketAddress(TEST_LOCALHOST, echoPort);
try (ChannelDirectTcpip channel = session.createDirectTcpipChannel(local, remote)) {
channel.open().verify(9L, TimeUnit.SECONDS);
String expected = getCurrentTestName();
byte[] bytes = expected.getBytes(StandardCharsets.UTF_8);
try (OutputStream output = channel.getInvertedIn();
InputStream input = channel.getInvertedOut()) {
output.write(bytes);
output.flush();
byte[] buf = new byte[bytes.length + Long.SIZE];
int n = input.read(buf);
String res = new String(buf, 0, n);
assertEquals("Mismatched data", expected, res);
}
channel.close(false);
}
}
}
示例3
Tunnel(ServerSocket ss, ChannelDirectTcpip tmp, int bufferSize) {
this.server = ss;
this.channel = tmp;
this.bufferSize = bufferSize;
}
示例4
/**
* Connect an existing {@link SshClient} to the server at <code>location</code> and authenticate using the given <code>credential</code>.
*
* @param adaptorName
* the adaptor where this method was called from.
* @param client
* the client to connect.
* @param location
* the server to connect to
* @param credential
* the credential to authenticate with.
* @param bufferSize
* the buffer size used for the (optional) SSH tunnels.
* @param timeout
* the timeout to use in connection setup (in milliseconds).
* @return the connected {@link ClientSession}
* @throws XenonException
* if the connection setup or authentication failed.
*/
public static SSHConnection connect(String adaptorName, SshClient client, String location, Credential credential, int bufferSize, long timeout)
throws XenonException {
if (credential == null) {
throw new IllegalArgumentException("Credential may not be null");
}
if (timeout <= 0) {
throw new IllegalArgumentException("Invalid timeout: " + timeout);
}
if (location == null) {
throw new IllegalArgumentException("Location may not be null");
}
SshdSocketAddress[] locations = extractLocations(adaptorName, location);
UserCredential[] creds = extractCredentials(adaptorName, locations, credential);
SSHConnection connection = new SSHConnection(client, locations.length - 1);
// Connect to the last location. This is either the destination (without tunneling) or the first hop.
ClientSession session = connectAndAuthenticate(adaptorName, client, locations[0].getHostName(), locations[0].getPort(), creds[0], timeout);
try {
// If we have more that one location we need to tunnel via another location.
for (int i = 1; i < locations.length; i++) {
ChannelDirectTcpip channel = session.createDirectTcpipChannel(null, locations[i]);
channel.open().await(timeout);
ServerSocket server = new ServerSocket(0);
int port = server.getLocalPort();
Tunnel tunnel = new Tunnel(server, channel, bufferSize);
tunnel.start();
connection.addHop(i - 1, session, tunnel);
session = connectAndAuthenticate(adaptorName, client, "localhost", port, creds[i], timeout);
}
} catch (IOException e) {
// Attempt to cleanup the mess
connection.close();
try {
session.close();
} catch (IOException e1) {
// ignored
}
throw new XenonException(adaptorName, "Failed to set up SSH forwarding", e);
} catch (XenonException xe) {
// Attempt to cleanup the mess
connection.close();
throw xe;
}
connection.setSession(session);
return connection;
}
示例5
@Override
public ChannelDirectTcpip createDirectTcpipChannel(SshdSocketAddress local, SshdSocketAddress remote) throws IOException {
throw new RuntimeException("Not implemented");
}