Android™
Android™
SSH Through a SOCKS Proxy
See more SSH Examples
Demonstrates connecting to an SSH server through a SOCKS4 or SOCKS5 proxy. Set the SOCKS properties before calling Connect, and set SocksVersion to match the proxy server.
Background: SOCKS is a general-purpose TCP relay, so unlike an HTTP proxy it is designed from the start to carry arbitrary protocols — usually the smoother option for SSH. The version matters: SOCKS4 supports only a username with no password, while SOCKS5 supports full login/password authentication as well as IPv6 and remote DNS resolution. Note this is the inverse of dynamic port forwarding: here a SOCKS proxy is used to reach the SSH server, rather than SSH providing a SOCKS proxy.
Chilkat Android™ Downloads
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
private static final String TAG = "Chilkat";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean success = false;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Demonstrates connecting to an SSH server through a SOCKS4 or SOCKS5 proxy.
CkSsh ssh = new CkSsh();
// Set the SOCKS proxy properties before connecting. The hostname may be a domain name or an
// IP address.
ssh.put_SocksHostname("socks.example.com");
ssh.put_SocksPort(1080);
ssh.put_SocksUsername("mySocksLogin");
ssh.put_SocksPassword("mySocksPassword");
// Set the version to match the proxy server: 4 or 5.
// Note: SOCKS4 supports only a username, with no password. SOCKS5 supports full
// login/password authentication.
ssh.put_SocksVersion(5);
String hostname = "ssh.example.com";
int port = 22;
success = ssh.Connect(hostname,port);
if (success == false) {
Log.i(TAG, ssh.lastErrorText());
return;
}
Log.i(TAG, "Connected to the SSH server through the SOCKS proxy.");
// Normally you would not hard-code the password in source. You should instead obtain it
// from an interactive prompt, environment variable, or a secrets vault.
String password = "mySshPassword";
success = ssh.AuthenticatePw("mySshLogin",password);
if (success == false) {
Log.i(TAG, ssh.lastErrorText());
return;
}
// ... use the SSH connection normally ...
ssh.Disconnect();
}
static {
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}