commit b2ec768d8d454bc9fcc170a13cf847d6b6c26025 Author: Nathan Freitas nathan@freitas.net Date: Mon Feb 23 12:29:21 2015 -0500
more work to get bridge VPN mode to work --- src/org/torproject/android/vpn/HttpProxy.java | 84 +++++++++++++------- .../torproject/android/vpn/OrbotVpnService.java | 4 - 2 files changed, 57 insertions(+), 31 deletions(-)
diff --git a/src/org/torproject/android/vpn/HttpProxy.java b/src/org/torproject/android/vpn/HttpProxy.java index d211342..ac62786 100644 --- a/src/org/torproject/android/vpn/HttpProxy.java +++ b/src/org/torproject/android/vpn/HttpProxy.java @@ -218,7 +218,6 @@ public class HttpProxy extends Thread while (true) { Socket client = server.accept(); - HttpProxy.vpnService.protect(client); ProxyThread t = new ProxyThread(client, fwdServer, fwdPort); t.setDebug(debugLevel, debugOut); t.setTimeout(ptTimeout); @@ -331,6 +330,7 @@ class ProxyThread extends Thread try { server = SocketChannel.open().socket(); + InetSocketAddress remoteHost = new InetSocketAddress(hostName, hostPort); if ((null != server) && (null != HttpProxy.vpnService)) { HttpProxy.vpnService.protect(server); @@ -338,18 +338,13 @@ class ProxyThread extends Thread if ((fwdServer.length() > 0) && (fwdPort > 0)) { - //server = new Socket(fwdServer, fwdPort); server.connect(new InetSocketAddress(fwdServer, fwdPort)); } else { - //server = new Socket(hostName, hostPort); - server.connect(new InetSocketAddress(hostName, hostPort)); + server.connect(remoteHost); } - - HttpProxy.vpnService.protect(server); - } catch (Exception e) { // tell the client there was an error String errMsg = "HTTP/1.0 500\nContent Type: text/plain\n\n" + @@ -360,31 +355,56 @@ class ProxyThread extends Thread if (server != null) { server.setSoTimeout(socketTimeout); + + BufferedInputStream serverIn = new BufferedInputStream(server.getInputStream()); BufferedOutputStream serverOut = new BufferedOutputStream(server.getOutputStream()); - // send the request out - serverOut.write(request, 0, requestLength); - serverOut.flush(); - - // and get the response; if we're not at a debug level that - // requires us to return the data in the response, just stream - // it back to the client to save ourselves from having to - // create and destroy an unnecessary byte array. Also, we - // should set the waitForDisconnect parameter to 'true', - // because some servers (like Google) don't always set the - // Content-Length header field, so we have to listen until - // they decide to disconnect (or the connection times out). - if (debugLevel > 1) + if (requestLength > 0) { - response = getHTTPData(serverIn, true); - responseLength = Array.getLength(response); - } else { - responseLength = streamHTTPData(serverIn, clientOut, true); + // send the request out + serverOut.write(request, 0, requestLength); + serverOut.flush(); + + // and get the response; if we're not at a debug level that + // requires us to return the data in the response, just stream + // it back to the client to save ourselves from having to + // create and destroy an unnecessary byte array. Also, we + // should set the waitForDisconnect parameter to 'true', + // because some servers (like Google) don't always set the + // Content-Length header field, so we have to listen until + // they decide to disconnect (or the connection times out). + if (debugLevel > 1) + { + response = getHTTPData(serverIn, true); + responseLength = Array.getLength(response); + } else { + responseLength = streamHTTPData(serverIn, clientOut, true); + } + + serverIn.close(); + serverOut.close(); + } + else + { + int i = 0; + byte[] buffer = new byte[4096]; + + int avail = clientIn.available(); + while (avail > 0 && (i = clientIn.read(buffer,0,avail))!=-1) + { + serverOut.write(buffer,0,i); + avail = clientIn.available(); + } + + while ((i = serverIn.read(buffer))!=-1) + clientOut.write(buffer,0,i); + + clientOut.close(); + clientIn.close(); + pSocket.close(); + return; } - - serverIn.close(); - serverOut.close(); } // send the response back to the client, if we haven't already @@ -488,6 +508,16 @@ class ProxyThread extends Thread debugOut.println("Error parsing response code " + rcString); } } + //CONNECT www.comodo.com:443 HTTP/1.1 + + else if (data.toLowerCase().startsWith("connect ")) + { + + String connectHost = data.substring(pos+1, data.indexOf(" ", pos+1)); + host.append(connectHost); + return 0; + + } } // get the rest of the header info diff --git a/src/org/torproject/android/vpn/OrbotVpnService.java b/src/org/torproject/android/vpn/OrbotVpnService.java index 365edab..f78f720 100644 --- a/src/org/torproject/android/vpn/OrbotVpnService.java +++ b/src/org/torproject/android/vpn/OrbotVpnService.java @@ -16,16 +16,12 @@
package org.torproject.android.vpn;
-import java.io.IOException; import java.net.InetAddress; -import java.net.UnknownHostException; import java.util.Locale;
import org.torproject.android.service.TorServiceConstants;
import android.annotation.TargetApi; -import android.app.Notification; -import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.net.VpnService;