commit 1db567698daab5e596fc473e11c5f50c44ed5d41
Author: Nathan Freitas <nathan(a)freitas.net>
Date: Thu Sep 29 12:00:57 2016 -0400
continue to migrate service and VPN code
---
.../org/torproject/android/service/TorService.java | 48 ++++----
.../android/service/transproxy/TorifiedApp.java | 122 +++++++++++++++++++++
.../android/service/vpn/OrbotVpnManager.java | 8 +-
.../android/service/vpn/TorVpnService.java | 44 ++------
4 files changed, 164 insertions(+), 58 deletions(-)
diff --git a/orbotservice/src/main/java/org/torproject/android/service/TorService.java b/orbotservice/src/main/java/org/torproject/android/service/TorService.java
index 452e094..b50b00f 100644
--- a/orbotservice/src/main/java/org/torproject/android/service/TorService.java
+++ b/orbotservice/src/main/java/org/torproject/android/service/TorService.java
@@ -42,6 +42,8 @@ import org.torproject.android.service.util.Prefs;
import org.torproject.android.service.util.TorResourceInstaller;
import org.torproject.android.service.util.TorServiceUtils;
import org.torproject.android.service.util.Utils;
+import org.torproject.android.service.vpn.OrbotVpnManager;
+import org.torproject.android.service.vpn.TorVpnService;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
@@ -314,7 +316,6 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
if (action.equals(ACTION_START)) {
replyWithStatus(mIntent);
startTor();
- // stopTor() is called when the Service is destroyed
}
else if (action.equals(ACTION_STATUS)) {
replyWithStatus(mIntent);
@@ -328,7 +329,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
} else if (action.equals(CMD_UPDATE_TRANS_PROXY)) {
processTransparentProxying();
} else if (action.equals(CMD_VPN)) {
- enableVpnProxy();
+ startVPNService();
} else if (action.equals(CMD_VPN_CLEAR)) {
clearVpnProxy();
} else if (action.equals(CMD_SET_EXIT)) {
@@ -745,7 +746,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
if (Prefs.bridgesEnabled())
if (Prefs.useVpn() && !mIsLollipop)
{
- //TODO customEnv.add("TOR_PT_PROXY=socks5://" + OrbotVpnManager.sSocksProxyLocalhost + ":" + OrbotVpnManager.sSocksProxyServerPort);
+ customEnv.add("TOR_PT_PROXY=socks5://" + OrbotVpnManager.sSocksProxyLocalhost + ":" + OrbotVpnManager.sSocksProxyServerPort);
}
// String baseDirectory = fileTor.getParent();
@@ -1179,23 +1180,6 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
}
- @TargetApi(14)
- public void clearVpnProxy ()
- {
- debug ("clearing VPN Proxy");
- Prefs.putUseVpn(false);
- processTransparentProxying();
-
- /**
- if (mVpnManager != null)
- {
- Intent intent = new Intent();
- intent.setAction("stop");
- mVpnManager.handleIntent(new Builder(), intent);
- mVpnManager = null;
- } **/
-
- }
@@ -1565,7 +1549,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
if (!mIsLollipop)
{
String proxyType = "socks5";
- //TODO VPN extraLines.append(proxyType + "Proxy" + ' ' + OrbotVpnManager.sSocksProxyLocalhost + ':' + OrbotVpnManager.sSocksProxyServerPort).append('\n');
+ extraLines.append(proxyType + "Proxy" + ' ' + OrbotVpnManager.sSocksProxyLocalhost + ':' + OrbotVpnManager.sSocksProxyServerPort).append('\n');
};
}
@@ -1978,4 +1962,26 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
return NOTIFY_ID;
}
+ private void startVPNService ()
+ {
+ Intent intentVpn = new Intent(this,TorVpnService.class);
+ intentVpn.setAction("start");
+ intentVpn.putExtra("torSocks",mPortSOCKS);
+ startService(intentVpn);
+ }
+
+
+ @TargetApi(14)
+ public void clearVpnProxy ()
+ {
+ debug ("clearing VPN Proxy");
+ Prefs.putUseVpn(false);
+ processTransparentProxying();
+
+ Intent intentVpn = new Intent(this,TorVpnService.class);
+ intentVpn.setAction("stop");
+ startService(intentVpn);
+
+ }
+
}
diff --git a/orbotservice/src/main/java/org/torproject/android/service/transproxy/TorifiedApp.java b/orbotservice/src/main/java/org/torproject/android/service/transproxy/TorifiedApp.java
index 1ca5a12..c786905 100644
--- a/orbotservice/src/main/java/org/torproject/android/service/transproxy/TorifiedApp.java
+++ b/orbotservice/src/main/java/org/torproject/android/service/transproxy/TorifiedApp.java
@@ -1,7 +1,21 @@
package org.torproject.android.service.transproxy;
+import android.content.Context;
+import android.content.SharedPreferences;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
+import org.torproject.android.service.OrbotConstants;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.StringTokenizer;
+
public class TorifiedApp implements Comparable {
private boolean enabled;
@@ -136,4 +150,112 @@ public class TorifiedApp implements Comparable {
public void setPackageName(String packageName) {
this.packageName = packageName;
}
+
+ public static ArrayList<TorifiedApp> getApps (Context context, SharedPreferences prefs)
+ {
+
+ String tordAppString = prefs.getString(OrbotConstants.PREFS_KEY_TORIFIED, "");
+ String[] tordApps;
+
+ StringTokenizer st = new StringTokenizer(tordAppString,"|");
+ tordApps = new String[st.countTokens()];
+ int tordIdx = 0;
+ while (st.hasMoreTokens())
+ {
+ tordApps[tordIdx++] = st.nextToken();
+ }
+
+ Arrays.sort(tordApps);
+
+ //else load the apps up
+ PackageManager pMgr = context.getPackageManager();
+
+ List<ApplicationInfo> lAppInfo = pMgr.getInstalledApplications(0);
+
+ Iterator<ApplicationInfo> itAppInfo = lAppInfo.iterator();
+
+ ArrayList<TorifiedApp> apps = new ArrayList<TorifiedApp>();
+
+ ApplicationInfo aInfo = null;
+
+ int appIdx = 0;
+ TorifiedApp app = null;
+
+ while (itAppInfo.hasNext())
+ {
+ aInfo = itAppInfo.next();
+
+ app = new TorifiedApp();
+
+ try {
+ PackageInfo pInfo = pMgr.getPackageInfo(aInfo.packageName, PackageManager.GET_PERMISSIONS);
+
+ if (pInfo != null && pInfo.requestedPermissions != null)
+ {
+ for (String permInfo:pInfo.requestedPermissions)
+ {
+ if (permInfo.equals("android.permission.INTERNET"))
+ {
+ app.setUsesInternet(true);
+
+ }
+ }
+
+ }
+
+
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ if ((aInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 1)
+ {
+ //System app
+ app.setUsesInternet(true);
+ }
+
+
+ if (!app.usesInternet())
+ continue;
+ else
+ {
+ apps.add(app);
+ }
+
+
+ app.setEnabled(aInfo.enabled);
+ app.setUid(aInfo.uid);
+ app.setUsername(pMgr.getNameForUid(app.getUid()));
+ app.setProcname(aInfo.processName);
+ app.setPackageName(aInfo.packageName);
+
+ try
+ {
+ app.setName(pMgr.getApplicationLabel(aInfo).toString());
+ }
+ catch (Exception e)
+ {
+ app.setName(aInfo.packageName);
+ }
+
+
+ //app.setIcon(pMgr.getApplicationIcon(aInfo));
+
+ // check if this application is allowed
+ if (Arrays.binarySearch(tordApps, app.getUsername()) >= 0) {
+ app.setTorified(true);
+ }
+ else
+ {
+ app.setTorified(false);
+ }
+
+ appIdx++;
+ }
+
+ Collections.sort(apps);
+
+ return apps;
+ }
}
diff --git a/orbotservice/src/main/java/org/torproject/android/service/vpn/OrbotVpnManager.java b/orbotservice/src/main/java/org/torproject/android/service/vpn/OrbotVpnManager.java
index b4aad92..f2e8d4c 100644
--- a/orbotservice/src/main/java/org/torproject/android/service/vpn/OrbotVpnManager.java
+++ b/orbotservice/src/main/java/org/torproject/android/service/vpn/OrbotVpnManager.java
@@ -35,11 +35,10 @@ import android.widget.Toast;
import com.runjva.sourceforge.jsocks.protocol.ProxyServer;
import com.runjva.sourceforge.jsocks.server.ServerAuthenticatorNone;
-import org.torproject.android.R;
+import org.torproject.android.service.R;
import org.torproject.android.service.TorServiceConstants;
import org.torproject.android.service.transproxy.TorifiedApp;
import org.torproject.android.service.util.TorServiceUtils;
-import org.torproject.android.ui.AppManager;
import java.io.File;
import java.io.FileNotFoundException;
@@ -66,8 +65,7 @@ public class OrbotVpnManager implements Handler.Callback {
public static int sSocksProxyServerPort = -1;
public static String sSocksProxyLocalhost = null;
private ProxyServer mSocksProxyServer;
-
-
+
private final static int VPN_MTU = 1500;
private final static boolean mIsLollipop = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
@@ -359,7 +357,7 @@ public class OrbotVpnManager implements Handler.Callback {
private void doLollipopAppRouting (Builder builder) throws NameNotFoundException
{
- ArrayList<TorifiedApp> apps = AppManager.getApps(mService, TorServiceUtils.getSharedPrefs(mService.getApplicationContext()));
+ ArrayList<TorifiedApp> apps = TorifiedApp.getApps(mService, TorServiceUtils.getSharedPrefs(mService.getApplicationContext()));
boolean perAppEnabled = false;
diff --git a/orbotservice/src/main/java/org/torproject/android/service/vpn/TorVpnService.java b/orbotservice/src/main/java/org/torproject/android/service/vpn/TorVpnService.java
index 1bd8173..f396860 100644
--- a/orbotservice/src/main/java/org/torproject/android/service/vpn/TorVpnService.java
+++ b/orbotservice/src/main/java/org/torproject/android/service/vpn/TorVpnService.java
@@ -19,44 +19,24 @@ import org.torproject.android.service.util.Prefs;
public class TorVpnService extends VpnService {
-
- /* (non-Javadoc)
- * @see android.app.Service#onStart(android.content.Intent, int)
- */
- public int onStartCommand(Intent intent, int flags, int startId) {
-
+ OrbotVpnManager mVpnManager;
- if (!TextUtils.isEmpty(intent.getAction()))
- {
- if (intent.getAction().equals("start"))
- enableVpnProxy();;
- }
+ @Override
+ public void onCreate() {
+ super.onCreate();
- return Service.START_STICKY;
+ mVpnManager = new OrbotVpnManager(this);
}
+ /* (non-Javadoc)
+ * @see android.app.Service#onStart(android.content.Intent, int)
+ */
+ public int onStartCommand(Intent intent, int flags, int startId) {
- public void enableVpnProxy () {
- // debug ("enabling VPN Proxy");
-
- OrbotVpnManager vpnManager = new OrbotVpnManager(this);
-
- int portSocks = 9050;
-
- Prefs.putUseVpn(true);
- // processTransparentProxying();
-
- //updateConfiguration("DNSPort",TOR_VPN_DNS_LISTEN_ADDRESS + ":" + TorServiceConstants.TOR_DNS_PORT_DEFAULT,false);
-
- // if (mVpnManager == null)
- // mVpnManager = new OrbotVpnManager (this);
-
- Intent intent = new Intent();
- intent.setAction("start");
- intent.putExtra("torSocks", portSocks);
-
- vpnManager.handleIntent(new Builder(),intent);
+ mVpnManager.handleIntent(new Builder(),intent);
+ return Service.START_STICKY;
}
+
}