Reputation: 592
I want to transfer data from an android SQLite database to a remote mysql database securely. As I can't setup a VPN programmatically I want to transfer the data through an ssh tunnel. I have tried with jkraft.jsch. The forwarding runs without errors, but when I connect with mysql-connector with
conexionMySQL = DriverManager.getConnection("jdbc:mysql://localhost:53009/",user, password);
I get the following error:
java.sql.SQLException: Communication link failure: java.io.EOFException, underlying cause: null
** BEGIN NESTED EXCEPTION **
java.io.EOFException
STACKTRACE:
java.io.EOFException
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1395)
at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:1414)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:625)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:1808)
at com.mysql.jdbc.Connection.<init>(Connection.java:452)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:411)
at java.sql.DriverManager.getConnection(DriverManager.java:179)
at java.sql.DriverManager.getConnection(DriverManager.java:213)
at de.damian.android.common.Mysql$2.run(Mysql.java:151)
at java.lang.Thread.run(Thread.java:841)
** END NESTED EXCEPTION **
The code is:
public int connectSSH(int rport)
{
//
int assigned_port;
final int local_port = 53009;
// Remote host and port
final int remote_port = rport;
final String remote_host = "test.test.org";
try
{
jsch = new JSch();
// Create SSH session. Port 22 is your SSH port which.
// is open in your firewall setup.
session = jsch.getSession("xxx", remote_host, 22);
session.setPassword("xxxxxx");
// Additional SSH options. See your ssh_config manual for.
// more options. Set options according to your requirements.
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
config.put("Compression", "yes");
config.put("ConnectionAttempts", "2");
session.setConfig(config);
// Connect
session.connect();
// Create the tunnel through port forwarding.
// This is basically instructing jsch session to send
// data received from local_port in the local machine to
// remote_port of the remote_host.
// assigned_port is the port assigned by jsch for use,
// it may not always be the same as.
// local_port.
assigned_port = session.setPortForwardingL(local_port,
remote_host, remote_port);
}
catch (JSchException e)
{
Log.e(Level.SEVERE.toString(), e.getMessage(), e);
return 0;
}
if (assigned_port == 0)
{
Log.e(Level.SEVERE.toString(), "Port forwarding failed !");
return 0;
}
return assigned_port;
}
Upvotes: 1
Views: 1337
Reputation: 592
The answer is quite simple: setPortForwarding must use "localhost" as hostname as the remoteport is not open on the remote interface and you are already logged in in the remote host.
assigned_port = session.setPortForwardingL(local_port,
"localhost", remote_port);
Upvotes: 1