isarjanfaza opened a new issue, #272:
URL: https://github.com/apache/mina-sshd/issues/272
my code is daynamic port forwardin but not work for use android
package com.topvpn.myvpn;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.EnumSet;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.util.Arrays;
import java.util.stream.Collectors;
import org.apache.sshd.client.SshClient;
import org.apache.sshd.client.channel.ClientChannel;
import org.apache.sshd.client.channel.ClientChannelEvent;
import org.apache.sshd.client.keyverifier.AcceptAllServerKeyVerifier;
import org.apache.sshd.client.session.ClientSession;
import org.apache.sshd.client.session.forward.DynamicPortForwardingTracker;
import org.apache.sshd.client.session.forward.ExplicitPortForwardingTracker;
import org.apache.sshd.common.channel.Channel;
import org.apache.sshd.common.forward.PortForwardingEventListener;
import org.apache.sshd.common.session.Session;
import org.apache.sshd.common.util.net.SshdSocketAddress;
import org.apache.sshd.server.SshServer;
import org.apache.sshd.server.channel.ChannelSessionFactory;
import org.apache.sshd.server.forward.AcceptAllForwardingFilter;
import org.apache.sshd.server.forward.DirectTcpipFactory;
import org.apache.sshd.server.forward.ForwardedTcpipFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class activity_ssh extends AppCompatActivity {
ClientChannel channel;
TextView shellOutput;
String host, username, password;
Integer port;
String command;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ssh);
// set output field
shellOutput = findViewById(R.id.textView);
// Get user credentials from indent
Intent intent = getIntent();
host = intent.getStringExtra("host");
port = Integer.parseInt(intent.getStringExtra("port"));
username = intent.getStringExtra("username");
password = intent.getStringExtra("password");
// Command which will be executed
command = "pwd\n";
// Setting user.com property manually
// since isn't set by default in android
String key = "user.home";
Context Syscontext;
Syscontext = getApplicationContext();
String val = Syscontext.getApplicationInfo().dataDir;
System.setProperty(key, val);
// Creating a client instance
SshClient client = SshClient.setUpDefaultClient();
client.setForwardingFilter(AcceptAllForwardingFilter.INSTANCE);
client.start();
// Starting new thread because network processes
// can interfere with UI if started in main thread
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
// Connection establishment and authentication
try (ClientSession session = client.connect(username,
host, port).verify(10000).getSession()) {
session.addPasswordIdentity(password);
session.auth().verify(50000);
System.out.println("Connection establihed");
// Create a channel to communicate
channel =
session.createChannel(Channel.CHANNEL_SHELL);
System.out.println("Starting shell");
ByteArrayOutputStream responseStream = new
ByteArrayOutputStream();
channel.setOut(responseStream);
// Open channel
channel.open().verify(5, TimeUnit.SECONDS);
try (OutputStream pipedIn = channel.getInvertedIn())
{
pipedIn.write(command.getBytes());
pipedIn.flush();
}
session.addPortForwardingEventListener(new
PortForwardingEventListener() {
@Override
public void establishedDynamicTunnel(Session
session, SshdSocketAddress local,
SshdSocketAddress boundAddress, Throwable reason) throws IOException {
// TODO Auto-generated method stub
PortForwardingEventListener.super.establishedDynamicTunnel(session, local,
boundAddress, reason);
System.out.println("Dynamic Forword Tunnel
is Ready");
}
});
SshdSocketAddress sshdSocketAddress = session
.startDynamicPortForwarding(new
SshdSocketAddress("localhost", 1212));
System.out.println("Host: " +
sshdSocketAddress.getHostName());
System.out.println("Port: " +
sshdSocketAddress.getPort());
Proxy proxy = new Proxy(Proxy.Type.SOCKS,
new
InetSocketAddress(sshdSocketAddress.getHostName(),
sshdSocketAddress.getPort()));
testRemoteURL(proxy);
// Close channel
channel.waitFor(EnumSet.of(ClientChannelEvent.CLOSED),
TimeUnit.SECONDS.toMillis(5));
// Output after converting to string type
String responseString = new
String(responseStream.toByteArray());
System.out.println(responseString);
shellOutput.setText(responseString);
} catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} finally {
client.stop();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
private static void testRemoteURL(final Proxy proxy) throws IOException {
URL url = new URL("http://test.javastack.org/");
HttpURLConnection connection = (HttpURLConnection) (proxy != null ?
url.openConnection(proxy) : url.openConnection());
//LOGGER.info("Get URL: {}", connection.getURL());
try (BufferedReader in = new BufferedReader(new
InputStreamReader(connection.getInputStream()))) {
String result = null;
if (android.os.Build.VERSION.SDK_INT >=
android.os.Build.VERSION_CODES.N) {
result = in.lines().collect(Collectors.joining("\n"));
}
System.out.println(result);
// LOGGER.info("Response from server: {}", result);
}
}
}
please help me???
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]