Merge pull request #78 from sprig/http-proxy

Added http proxy option based on patch from https://code.google.com/p/connectbot/issues/detail?id=610
master
Martin Matuška 2013-10-30 00:21:40 -07:00
commit 5f70cc011c
7 changed files with 81 additions and 1 deletions

View File

@ -278,6 +278,11 @@
<!-- Summary for compression preference -->
<string name="hostpref_compression_summary">"This may help with slower networks"</string>
<!-- HTTP Proxy preference title -->
<string name="hostpref_httpproxy_title">"Use HTTP Proxy"</string>
<!-- Summary for compression preference -->
<string name="hostpref_httpproxy_summary">"The host:port of an HTTP proxy"</string>
<!-- Setting for whether we want a session to start up when we connect to a host -->
<string name="hostpref_wantsession_title">"Start shell session"</string>
<!-- Summary for field asking whether a shell session should be started up upon connection or not -->

View File

@ -72,6 +72,13 @@
android:title="@string/hostpref_compression_title"
android:summary="@string/hostpref_compression_summary"
/>
<EditTextPreference
android:key="httpproxy"
android:title="@string/hostpref_httpproxy_title"
android:summary="@string/hostpref_httpproxy_summary"
android:inputType="textUri"
/>
<CheckBoxPreference
android:key="wantsession"

View File

@ -47,6 +47,7 @@ public class HostBean extends AbstractBean {
private String delKey = HostDatabase.DELKEY_DEL;
private int fontSize = -1;
private boolean compression = false;
private String httpproxy = null;
private String encoding = HostDatabase.ENCODING_DEFAULT;
private boolean stayConnected = false;
private boolean wantX11Forward = false;
@ -187,6 +188,12 @@ public class HostBean extends AbstractBean {
public boolean getCompression() {
return compression;
}
public void setHttpproxy(String httpproxy) {
this.httpproxy = httpproxy;
}
public String getHttpproxy() {
return httpproxy;
}
public void setEncoding(String encoding) {
this.encoding = encoding;
@ -258,6 +265,7 @@ public class HostBean extends AbstractBean {
values.put(HostDatabase.FIELD_HOST_DELKEY, delKey);
values.put(HostDatabase.FIELD_HOST_FONTSIZE, fontSize);
values.put(HostDatabase.FIELD_HOST_COMPRESSION, Boolean.toString(compression));
values.put(HostDatabase.FIELD_HOST_HTTPPROXY, httpproxy);
values.put(HostDatabase.FIELD_HOST_ENCODING, encoding);
values.put(HostDatabase.FIELD_HOST_STAYCONNECTED, stayConnected);
values.put(HostDatabase.FIELD_HOST_WANTX11FORWARD, wantX11Forward);

View File

@ -289,6 +289,7 @@ public class TerminalBridge implements VDUDisplay {
// TODO make this more abstract so we don't litter on AbsTransport
transport.setCompression(host.getCompression());
transport.setHttpproxy(host.getHttpproxy());
transport.setUseAuthAgent(host.getUseAuthAgent());
transport.setEmulation(emulation);

View File

@ -131,6 +131,10 @@ public abstract class AbsTransport {
// do nothing
}
public void setHttpproxy(String httpproxy) {
// do nothing
}
public void setUseAuthAgent(String useAuthAgent) {
// do nothing
}

View File

@ -23,6 +23,8 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URL;
import java.net.MalformedURLException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
@ -63,6 +65,8 @@ import com.trilead.ssh2.LocalPortForwarder;
import com.trilead.ssh2.SCPClient;
import com.trilead.ssh2.ServerHostKeyVerifier;
import com.trilead.ssh2.Session;
import com.trilead.ssh2.HTTPProxyData;
import com.trilead.ssh2.HTTPProxyException;
import com.trilead.ssh2.crypto.PEMDecoder;
import com.trilead.ssh2.signature.DSAPrivateKey;
import com.trilead.ssh2.signature.DSAPublicKey;
@ -104,6 +108,7 @@ public class SSH extends AbsTransport implements ConnectionMonitor, InteractiveC
}
private boolean compression = false;
private String httpproxy = null;
private volatile boolean authenticated = false;
private volatile boolean connected = false;
private volatile boolean sessionOpen = false;
@ -421,6 +426,32 @@ public class SSH extends AbsTransport implements ConnectionMonitor, InteractiveC
Log.e(TAG, "Could not enable compression!", e);
}
if (httpproxy != null && httpproxy.length() > 0) {
Log.d(TAG, "Want HTTP Proxy: "+httpproxy, null);
try {
URL u;
if (httpproxy.startsWith("http://")) {
u = new URL(httpproxy);
} else {
u = new URL("http://" + httpproxy);
}
connection.setProxyData(new HTTPProxyData(
u.getHost(),
u.getPort(),
u.getUserInfo(),
u.getAuthority()));
bridge.outputLine("Connecting via proxy: "+httpproxy);
} catch (MalformedURLException e) {
Log.e(TAG, "Could not parse proxy "+httpproxy, e);
// Display the reason in the text.
bridge.outputLine("Bad proxy URL: "+httpproxy);
onDisconnect();
return;
}
}
try {
/* Uncomment when debugging SSH protocol:
DebugLogger logger = new DebugLogger() {
@ -454,6 +485,14 @@ public class SSH extends AbsTransport implements ConnectionMonitor, InteractiveC
connectionInfo.serverToClientCryptoAlgorithm,
connectionInfo.serverToClientMACAlgorithm));
}
} catch (HTTPProxyException e) {
Log.e(TAG, "Failed to connect to HTTP Proxy", e);
// Display the reason in the text.
bridge.outputLine("Failed to connect to HTTP Proxy.");
onDisconnect();
return;
} catch (IOException e) {
Log.e(TAG, "Problem in SSH connection thread during authentication", e);
@ -550,6 +589,8 @@ public class SSH extends AbsTransport implements ConnectionMonitor, InteractiveC
Map<String, String> options = new HashMap<String, String>();
options.put("compression", Boolean.toString(compression));
if (httpproxy != null)
options.put("httpproxy", httpproxy);
return options;
}
@ -558,6 +599,8 @@ public class SSH extends AbsTransport implements ConnectionMonitor, InteractiveC
public void setOptions(Map<String, String> options) {
if (options.containsKey("compression"))
compression = Boolean.parseBoolean(options.get("compression"));
if (options.containsKey("httpproxy"))
httpproxy = options.get("httpproxy");
}
public static String getProtocolName() {
@ -896,6 +939,11 @@ public class SSH extends AbsTransport implements ConnectionMonitor, InteractiveC
this.compression = compression;
}
@Override
public void setHttpproxy(String httpproxy) {
this.httpproxy = httpproxy;
}
public static String getFormatHint(Context context) {
return String.format("%s@%s:%s",
context.getString(R.string.format_username),

View File

@ -46,7 +46,7 @@ public class HostDatabase extends RobustSQLiteOpenHelper {
public final static String TAG = "ConnectBot.HostDatabase";
public final static String DB_NAME = "hosts";
public final static int DB_VERSION = 23;
public final static int DB_VERSION = 24;
public final static String TABLE_HOSTS = "hosts";
public final static String FIELD_HOST_NICKNAME = "nickname";
@ -66,6 +66,7 @@ public class HostDatabase extends RobustSQLiteOpenHelper {
public final static String FIELD_HOST_DELKEY = "delkey";
public final static String FIELD_HOST_FONTSIZE = "fontsize";
public final static String FIELD_HOST_COMPRESSION = "compression";
public final static String FIELD_HOST_HTTPPROXY = "httpproxy";
public final static String FIELD_HOST_ENCODING = "encoding";
public final static String FIELD_HOST_STAYCONNECTED = "stayconnected";
public final static String FIELD_HOST_WANTX11FORWARD = "wantx11forward";
@ -173,6 +174,7 @@ public class HostDatabase extends RobustSQLiteOpenHelper {
+ FIELD_HOST_FONTSIZE + " INTEGER, "
+ FIELD_HOST_WANTSESSION + " TEXT DEFAULT '" + Boolean.toString(true) + "', "
+ FIELD_HOST_COMPRESSION + " TEXT DEFAULT '" + Boolean.toString(false) + "', "
+ FIELD_HOST_HTTPPROXY + " TEXT, "
+ FIELD_HOST_ENCODING + " TEXT DEFAULT '" + ENCODING_DEFAULT + "', "
+ FIELD_HOST_STAYCONNECTED + " TEXT, "
+ FIELD_HOST_WANTX11FORWARD + " TEXT DEFAULT '" + Boolean.toString(false) + "', "
@ -274,6 +276,9 @@ public class HostDatabase extends RobustSQLiteOpenHelper {
+ " ADD COLUMN " + FIELD_HOST_X11HOST + " TEXT DEFAULT '" + X11HOST_DEFAULT + "'");
db.execSQL("ALTER TABLE " + TABLE_HOSTS
+ " ADD COLUMN " + FIELD_HOST_X11PORT + " INTEGER DEFAULT " + X11PORT_DEFAULT);
case 23:
db.execSQL("ALTER TABLE " + TABLE_HOSTS
+ " ADD COLUMN " + FIELD_HOST_HTTPPROXY + " TEXT");
}
}
@ -390,6 +395,7 @@ public class HostDatabase extends RobustSQLiteOpenHelper {
COL_DELKEY = c.getColumnIndexOrThrow(FIELD_HOST_DELKEY),
COL_FONTSIZE = c.getColumnIndexOrThrow(FIELD_HOST_FONTSIZE),
COL_COMPRESSION = c.getColumnIndexOrThrow(FIELD_HOST_COMPRESSION),
COL_HTTPPROXY = c.getColumnIndexOrThrow(FIELD_HOST_HTTPPROXY),
COL_ENCODING = c.getColumnIndexOrThrow(FIELD_HOST_ENCODING),
COL_STAYCONNECTED = c.getColumnIndexOrThrow(FIELD_HOST_STAYCONNECTED),
COL_WANTX11FORWARD = c.getColumnIndexOrThrow(FIELD_HOST_WANTX11FORWARD),
@ -415,6 +421,7 @@ public class HostDatabase extends RobustSQLiteOpenHelper {
host.setDelKey(c.getString(COL_DELKEY));
host.setFontSize(c.getInt(COL_FONTSIZE));
host.setCompression(Boolean.valueOf(c.getString(COL_COMPRESSION)));
host.setHttpproxy(c.getString(COL_HTTPPROXY));
host.setEncoding(c.getString(COL_ENCODING));
host.setStayConnected(Boolean.valueOf(c.getString(COL_STAYCONNECTED)));
host.setWantX11Forward(Boolean.valueOf(c.getString(COL_WANTX11FORWARD)));