commit ec47151a0c5e9bf789c38a9f36820031241b54f4
Author: n8fr8 <nathan(a)guardianproject.info>
Date: Tue Apr 28 16:52:01 2020 -0400
major VPN refactor with Android Q updates for #263 #261 #151 #316 and #303
- integrate TorVPNService directly into OrbotService, so just one service now
- removed VPNEnableActivity, and just have VPN activated by service now
- changes address start on boot with VPN enable
- also improvements to managing PDNSD daemon
---
.../org/torproject/android/OnBootReceiver.java | 14 +--
.../org/torproject/android/OrbotMainActivity.java | 47 ++++----
.../torproject/android/ui/VPNEnableActivity.java | 120 ---------------------
.../android/ui/onboarding/OnboardingActivity.java | 2 -
.../torproject/android/service/OrbotService.java | 48 ++++++++-
.../android/service/TorServiceConstants.java | 11 +-
.../android/service/vpn/OrbotVpnManager.java | 45 ++++----
.../android/service/vpn/TorVpnService.java | 115 --------------------
8 files changed, 107 insertions(+), 295 deletions(-)
diff --git a/app/src/main/java/org/torproject/android/OnBootReceiver.java b/app/src/main/java/org/torproject/android/OnBootReceiver.java
index 5c7ae504..0bfb20d7 100644
--- a/app/src/main/java/org/torproject/android/OnBootReceiver.java
+++ b/app/src/main/java/org/torproject/android/OnBootReceiver.java
@@ -8,7 +8,6 @@ import android.os.Build;
import org.torproject.android.service.OrbotService;
import org.torproject.android.service.TorServiceConstants;
import org.torproject.android.service.util.Prefs;
-import org.torproject.android.ui.VPNEnableActivity;
public class OnBootReceiver extends BroadcastReceiver {
@@ -19,22 +18,11 @@ public class OnBootReceiver extends BroadcastReceiver {
if (Prefs.startOnBoot() && (!sReceivedBoot))
{
- if (Prefs.useVpn())
- startVpnService(context); //VPN will start Tor once it is done
- else
- startService(TorServiceConstants.ACTION_START, context);
-
+ startService(TorServiceConstants.ACTION_START_ON_BOOT, context);
sReceivedBoot = true;
}
}
-
- public void startVpnService (final Context context)
- {
- Intent intent = new Intent(context,VPNEnableActivity.class);
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- context.startActivity(intent);
- }
private void startService (String action, Context context)
{
diff --git a/app/src/main/java/org/torproject/android/OrbotMainActivity.java b/app/src/main/java/org/torproject/android/OrbotMainActivity.java
index ede16bad..7c4528d5 100644
--- a/app/src/main/java/org/torproject/android/OrbotMainActivity.java
+++ b/app/src/main/java/org/torproject/android/OrbotMainActivity.java
@@ -20,6 +20,7 @@ import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;
import android.net.Uri;
+import android.net.VpnService;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
@@ -58,7 +59,6 @@ import org.torproject.android.service.OrbotConstants;
import org.torproject.android.service.OrbotService;
import org.torproject.android.service.TorServiceConstants;
import org.torproject.android.service.util.Prefs;
-import org.torproject.android.service.vpn.TorVpnService;
import org.torproject.android.service.vpn.VpnConstants;
import org.torproject.android.service.vpn.VpnPrefs;
import org.torproject.android.settings.Languages;
@@ -66,7 +66,6 @@ import org.torproject.android.settings.LocaleHelper;
import org.torproject.android.settings.SettingsPreferences;
import org.torproject.android.ui.AppManagerActivity;
import org.torproject.android.ui.Rotate3dAnimation;
-import org.torproject.android.ui.VPNEnableActivity;
import org.torproject.android.ui.hiddenservices.ClientCookiesActivity;
import org.torproject.android.ui.hiddenservices.HiddenServicesActivity;
import org.torproject.android.ui.hiddenservices.backup.BackupUtils;
@@ -93,6 +92,9 @@ import static androidx.core.content.FileProvider.getUriForFile;
import static org.torproject.android.MainConstants.COUNTRY_CODES;
import static org.torproject.android.MainConstants.RESULT_CLOSE_ALL;
import static org.torproject.android.MainConstants.URL_TOR_CHECK;
+import static org.torproject.android.service.TorServiceConstants.ACTION_START;
+import static org.torproject.android.service.TorServiceConstants.ACTION_START_VPN;
+import static org.torproject.android.service.TorServiceConstants.ACTION_STOP_VPN;
import static org.torproject.android.service.vpn.VpnPrefs.PREFS_KEY_TORIFIED;
import static org.torproject.android.service.vpn.VpnUtils.getSharedPrefs;
@@ -338,7 +340,7 @@ public class OrbotMainActivity extends AppCompatActivity
//auto start VPN if VPN is enabled
if (useVPN) {
- startActivity(new Intent(OrbotMainActivity.this, VPNEnableActivity.class));
+ sendIntentToService(ACTION_START_VPN);
}
mBtnVPN.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@@ -548,8 +550,6 @@ public class OrbotMainActivity extends AppCompatActivity
private void doExit() {
stopTor();
- TorVpnService.stop(this);
-
// Kill all the wizard activities
setResult(RESULT_CLOSE_ALL);
finish();
@@ -578,17 +578,28 @@ public class OrbotMainActivity extends AppCompatActivity
}
private void refreshVPNApps() {
- TorVpnService.stop(this);
- startActivity(new Intent(OrbotMainActivity.this, VPNEnableActivity.class));
+ sendIntentToService(ACTION_STOP_VPN);
+ sendIntentToService(ACTION_START_VPN);
}
private void enableVPN(boolean enable) {
Prefs.putUseVpn(enable);
if (enable) {
- startActivityForResult(new Intent(OrbotMainActivity.this, VPNEnableActivity.class), REQUEST_VPN);
+
+ Intent intentVPN = VpnService.prepare(this);
+ if (intentVPN != null)
+ startActivityForResult(intentVPN,REQUEST_VPN);
+ else {
+ sendIntentToService(ACTION_START);
+ sendIntentToService(ACTION_START_VPN);
+ }
+
} else
- TorVpnService.stop(this);
+ {
+ //stop the VPN here
+ sendIntentToService(ACTION_STOP_VPN);
+ }
addAppShortcuts();
}
@@ -863,19 +874,19 @@ public class OrbotMainActivity extends AppCompatActivity
}
- } else if (request == REQUEST_VPN) {
- if (response == RESULT_OK) {
- TorVpnService.start(this);
- } else if (response == VPNEnableActivity.ACTIVITY_RESULT_VPN_DENIED) {
- mBtnVPN.setChecked(false);
- Prefs.putUseVpn(false);
- }
} else if (request == REQUEST_VPN_APPS_SELECT) {
if (response == RESULT_OK &&
torStatus.equals(TorServiceConstants.STATUS_ON))
refreshVPNApps();
}
+ else if (request == REQUEST_VPN && response == RESULT_OK) {
+ sendIntentToService(ACTION_START_VPN);
+ }
+ else if (request == REQUEST_VPN && response == RESULT_CANCELED) {
+ mBtnVPN.setChecked(false);
+ Prefs.putUseVpn(false);
+ }
IntentResult scanResult = IntentIntegrator.parseActivityResult(request, response, data);
if (scanResult != null) {
@@ -1041,7 +1052,7 @@ public class OrbotMainActivity extends AppCompatActivity
Intent resultIntent = lastStatusIntent;
if (resultIntent == null)
- resultIntent = new Intent(TorServiceConstants.ACTION_START);
+ resultIntent = new Intent(ACTION_START);
resultIntent.putExtra(
TorServiceConstants.EXTRA_STATUS,
@@ -1098,7 +1109,7 @@ public class OrbotMainActivity extends AppCompatActivity
* {@link OrbotService}
*/
private void startTor() {
- sendIntentToService(TorServiceConstants.ACTION_START);
+ sendIntentToService(ACTION_START);
mTxtOrbotLog.setText("");
}
diff --git a/app/src/main/java/org/torproject/android/ui/VPNEnableActivity.java b/app/src/main/java/org/torproject/android/ui/VPNEnableActivity.java
deleted file mode 100644
index ba12ebe5..00000000
--- a/app/src/main/java/org/torproject/android/ui/VPNEnableActivity.java
+++ /dev/null
@@ -1,120 +0,0 @@
-package org.torproject.android.ui;
-
-import android.content.Intent;
-import android.net.VpnService;
-import android.os.Build;
-import android.os.Bundle;
-import android.os.Handler;
-import android.util.Log;
-import android.view.Window;
-import androidx.appcompat.app.AppCompatActivity;
-import org.torproject.android.service.OrbotService;
-import org.torproject.android.service.TorServiceConstants;
-import org.torproject.android.service.util.Prefs;
-import org.torproject.android.service.vpn.TorVpnService;
-
-/**
- * To combat background service being stopped/swiped
- */
-public class VPNEnableActivity extends AppCompatActivity {
-
- private final static int REQUEST_VPN = 7777;
- private Intent intent = null;
- private boolean checkVpn = true;
- private Handler h = new Handler();
-
- @Override
- public void onCreate(Bundle icicle ) {
- requestWindowFeature(Window.FEATURE_NO_TITLE);
-
- super.onCreate( icicle );
-
- Log.d("VPNEnableActivity","prompting user to start Orbot VPN");
- }
-
- public void onResume() {
- super.onResume();
-
- if (checkVpn)
- {
- intent = VpnService.prepare(this);
-
- if (intent != null)
- promptStartVpnService();
- else
- startVpnService ();
-
- checkVpn = false;
- }
- }
-
- public void promptStartVpnService () {
- // todo no actual prompting happens here and this should be refactored
- startVpnService();
- }
-
- private void startVpnService ()
- {
- if (intent == null)
- {
- Prefs.putUseVpn(true);
-
- Log.d("VPNEnableActivity","VPN enabled, starting Tor...");
- TorVpnService.start(this);
-
- Handler h = new Handler();
- h.postDelayed(new Runnable () {
-
- public void run ()
- {
- sendIntentToService(TorServiceConstants.ACTION_START);
- finish();
- }
- }, 100);
-
-
- }
- else
- {
- Log.w("VPNEnableActivity","prompt for VPN");
- startActivityForResult(intent,REQUEST_VPN);
-
- }
-
- }
-
- public static final int ACTIVITY_RESULT_VPN_DENIED = 63;
-
- @Override
- protected void onActivityResult(int request, int response, Intent data) {
- super.onActivityResult(request, response, data);
-
- if (request == REQUEST_VPN && response == RESULT_OK) {
- TorVpnService.start(this);
- h.postDelayed(new Runnable () {
- @Override
- public void run () {
- sendIntentToService(TorServiceConstants.ACTION_START);
- finish();
- }
- }, 1000);
- }
- else if (request == REQUEST_VPN && response == RESULT_CANCELED) {
- setResult(ACTIVITY_RESULT_VPN_DENIED);
- finish();
- }
- }
-
-
- private void sendIntentToService(String action) {
- Intent intent = new Intent(this, OrbotService.class);
- intent.setAction(action);
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- startForegroundService(intent);
- }
- else
- {
- startService(intent);
- }
- }
-}
diff --git a/app/src/main/java/org/torproject/android/ui/onboarding/OnboardingActivity.java b/app/src/main/java/org/torproject/android/ui/onboarding/OnboardingActivity.java
index 8e6e0e0c..6380753d 100644
--- a/app/src/main/java/org/torproject/android/ui/onboarding/OnboardingActivity.java
+++ b/app/src/main/java/org/torproject/android/ui/onboarding/OnboardingActivity.java
@@ -15,7 +15,6 @@ import org.torproject.android.R;
import org.torproject.android.service.util.Prefs;
import org.torproject.android.settings.LocaleHelper;
import org.torproject.android.ui.AppManagerActivity;
-import org.torproject.android.ui.VPNEnableActivity;
import org.torproject.android.ui.hiddenservices.permissions.PermissionManager;
public class OnboardingActivity extends AppIntro {
@@ -69,7 +68,6 @@ public class OnboardingActivity extends AppIntro {
cs3.showButton(getString(R.string.action_vpn_choose), new View.OnClickListener() {
@Override
public void onClick(View v) {
- startActivity(new Intent(OnboardingActivity.this, VPNEnableActivity.class));
startActivityForResult(new Intent(OnboardingActivity.this, AppManagerActivity.class), 9999);
}
});
diff --git a/orbotservice/src/main/java/org/torproject/android/service/OrbotService.java b/orbotservice/src/main/java/org/torproject/android/service/OrbotService.java
index 57cff570..596771cf 100644
--- a/orbotservice/src/main/java/org/torproject/android/service/OrbotService.java
+++ b/orbotservice/src/main/java/org/torproject/android/service/OrbotService.java
@@ -26,6 +26,7 @@ import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
+import android.net.VpnService;
import android.os.Build;
import android.os.Debug;
import android.os.IBinder;
@@ -81,7 +82,7 @@ import java.util.concurrent.TimeoutException;
import static org.torproject.android.service.vpn.VpnUtils.getSharedPrefs;
-public class OrbotService extends Service implements TorServiceConstants, OrbotConstants
+public class OrbotService extends VpnService implements TorServiceConstants, OrbotConstants
{
public final static String BINARY_TOR_VERSION = org.torproject.android.binary.TorServiceConstants.BINARY_TOR_VERSION;
@@ -132,6 +133,8 @@ public class OrbotService extends Service implements TorServiceConstants, OrbotC
private static final Uri HS_CONTENT_URI = Uri.parse("content://org.torproject.android.ui.hiddenservices.providers/hs");
private static final Uri COOKIE_CONTENT_URI = Uri.parse("content://org.torproject.android.ui.hiddenservices.providers.cookie/cookie");
+ OrbotVpnManager mVpnManager;
+
public static final class HiddenService implements BaseColumns {
public static final String NAME = "name";
public static final String PORT = "port";
@@ -364,10 +367,37 @@ public class OrbotService extends Service implements TorServiceConstants, OrbotC
String action = mIntent.getAction();
if (action != null) {
- if (action.equals(ACTION_START)) {
+ if (action.equals(ACTION_START) || action.equals(ACTION_START_ON_BOOT)) {
startTor();
replyWithStatus(mIntent);
+ if (Prefs.useVpn())
+ {
+ //start VPN here
+ Intent vpnIntent = VpnService.prepare(OrbotService.this);
+ if (vpnIntent == null) //then we can run the VPN
+ {
+ mVpnManager.handleIntent(new Builder(), mIntent);
+ if (mPortSOCKS != -1 && mPortHTTP != -1)
+ sendCallbackPorts(mPortSOCKS, mPortHTTP, mPortDns, mPortTrans);
+ }
+ }
+
+ }
+ else if (action.equals(ACTION_START_VPN)) {
+ //start VPN here
+ Intent vpnIntent = VpnService.prepare(OrbotService.this);
+ if (vpnIntent == null) //then we can run the VPN
+ {
+ mVpnManager.handleIntent(new Builder(), mIntent);
+
+ if (mPortSOCKS != -1 && mPortHTTP != -1)
+ sendCallbackPorts(mPortSOCKS, mPortHTTP, mPortDns, mPortTrans);
+ }
+
+ }
+ else if (action.equals(ACTION_STOP_VPN)) {
+ mVpnManager.handleIntent(new Builder(),mIntent);
}
else if (action.equals(ACTION_STATUS)) {
replyWithStatus(mIntent);
@@ -509,7 +539,7 @@ public class OrbotService extends Service implements TorServiceConstants, OrbotC
appBinHome.mkdirs();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
- appCacheHome = getDataDir();//getDir(DIRECTORY_TOR_DATA, Application.MODE_PRIVATE);
+ appCacheHome = new File(getDataDir(),DIRECTORY_TOR_DATA);
}
else {
appCacheHome = getDir(DIRECTORY_TOR_DATA, Application.MODE_PRIVATE);
@@ -568,7 +598,14 @@ public class OrbotService extends Service implements TorServiceConstants, OrbotC
}
}).start();
-
+
+ try {
+ mVpnManager = new OrbotVpnManager(this);
+ } catch (IOException e) {
+ e.printStackTrace();
+ } catch (TimeoutException e) {
+ e.printStackTrace();
+ }
}
catch (Exception e)
{
@@ -1458,6 +1495,9 @@ public class OrbotService extends Service implements TorServiceConstants, OrbotC
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
+ if (Prefs.useVpn())
+ mVpnManager.handleIntent(new Builder(),intent);
+
}
protected void sendCallbackStatus(String currentStatus) {
diff --git a/orbotservice/src/main/java/org/torproject/android/service/TorServiceConstants.java b/orbotservice/src/main/java/org/torproject/android/service/TorServiceConstants.java
index 5776abe9..9220095a 100644
--- a/orbotservice/src/main/java/org/torproject/android/service/TorServiceConstants.java
+++ b/orbotservice/src/main/java/org/torproject/android/service/TorServiceConstants.java
@@ -8,7 +8,7 @@ import android.content.Intent;
public interface TorServiceConstants {
- String DIRECTORY_TOR_DATA = "data";
+ String DIRECTORY_TOR_DATA = "tordata";
String TOR_CONTROL_PORT_FILE = "control.txt";
String TOR_PID_FILE = "torpid";
@@ -38,6 +38,15 @@ public interface TorServiceConstants {
* A request to Orbot to transparently start Tor services
*/
String ACTION_START = "org.torproject.android.intent.action.START";
+ String ACTION_STOP = "org.torproject.android.intent.action.STOP";
+
+ String ACTION_START_VPN = "org.torproject.android.intent.action.START_VPN";
+ String ACTION_STOP_VPN = "org.torproject.android.intent.action.STOP_VPN";
+
+ String ACTION_START_ON_BOOT = "org.torproject.android.intent.action.START_BOOT";
+
+ int REQUEST_VPN = 7777;
+
/**
* {@link Intent} send by Orbot with {@code ON/OFF/STARTING/STOPPING} status
*/
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 49fb5f33..f5a3e8a9 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
@@ -55,6 +55,9 @@ import java.util.List;
import java.util.concurrent.TimeoutException;
import static org.torproject.android.service.TorServiceConstants.ACTION_START;
+import static org.torproject.android.service.TorServiceConstants.ACTION_START_VPN;
+import static org.torproject.android.service.TorServiceConstants.ACTION_STOP;
+import static org.torproject.android.service.TorServiceConstants.ACTION_STOP_VPN;
import static org.torproject.android.service.vpn.VpnUtils.getSharedPrefs;
import static org.torproject.android.service.vpn.VpnUtils.killProcess;
@@ -70,6 +73,7 @@ public class OrbotVpnManager implements Handler.Callback {
private int mTorSocks = -1;
private int mTorDns = -1;
+ private int pdnsdPort = 8091;
public static int sSocksProxyServerPort = -1;
public static String sSocksProxyLocalhost = null;
@@ -95,12 +99,6 @@ public class OrbotVpnManager implements Handler.Callback {
filePdnsd = CustomNativeLoader.loadNativeBinary(service.getApplicationContext(),PDNSD_BIN,new File(service.getFilesDir(),PDNSD_BIN));
- /**
- try {
- killProcess(filePdnsd, "-1");
- } catch (Exception e) {
- e.printStackTrace();
- }**/
Tun2Socks.init();
@@ -109,15 +107,16 @@ public class OrbotVpnManager implements Handler.Callback {
boolean isStarted = false;
- //public int onStartCommand(Intent intent, int flags, int startId) {
public int handleIntent(Builder builder, Intent intent) {
if (intent != null)
{
String action = intent.getAction();
- if (action.equals(TorVpnService.ACTION_START))
+ if (action.equals(ACTION_START_VPN)||action.equals(ACTION_START))
{
+ Log.d(TAG,"starting VPN");
+
isStarted = true;
// Stop the previous session by interrupting the thread.
@@ -137,18 +136,19 @@ public class OrbotVpnManager implements Handler.Callback {
}
+
}
- else if (action.equals(TorVpnService.ACTION_STOP))
+ else if (action.equals(ACTION_STOP_VPN))
{
isStarted = false;
- Log.d(TAG,"stop OrbotVPNService service!");
+ Log.d(TAG,"stopping VPN");
stopVPN();
}
else if (action.equals(TorServiceConstants.LOCAL_ACTION_PORTS))
{
- Log.d(TAG,"starting OrbotVPNService service!");
+ Log.d(TAG,"setting VPN ports");
int torSocks = intent.getIntExtra(OrbotService.EXTRA_SOCKS_PROXY_PORT,-1);
int torDns = intent.getIntExtra(OrbotService.EXTRA_DNS_PORT,-1);
@@ -347,7 +347,8 @@ public class OrbotVpnManager implements Handler.Callback {
//start PDNSD daemon pointing to actual DNS
if (filePdnsd != null) {
- int pdnsdPort = 8091;
+
+ pdnsdPort++;
startDNS(filePdnsd.getCanonicalPath(), localhost, mTorDns, virtualGateway, pdnsdPort);
final boolean localDnsTransparentProxy = true;
@@ -421,8 +422,6 @@ public class OrbotVpnManager implements Handler.Callback {
File fileConf = makePdnsdConf(mService, mService.getFilesDir(), torDnsHost, torDnsPort, pdnsdHost, pdnsdPort);
-
-
String[] cmdString = {pdnsPath,"-c",fileConf.toString(),"-g","-v2"};
ProcessBuilder pb = new ProcessBuilder(cmdString);
pb.redirectErrorStream(true);
@@ -444,16 +443,18 @@ public class OrbotVpnManager implements Handler.Callback {
}
+ File filePdnsPid;
+
private boolean stopDns ()
{
-
- File filePdnsPid = new File(mService.getFilesDir(),"pdnsd.pid");
- if (filePdnsPid.exists()) {
+ if (filePdnsPid != null && filePdnsPid.exists()) {
List<String> lines = null;
try {
lines = IOUtils.readLines(new FileReader(filePdnsPid));
String dnsPid = lines.get(0);
killProcess(dnsPid, "");
+ filePdnsPid.delete();
+ filePdnsPid = null;
} catch (Exception e) {
Log.e("OrbotVPN", "error killing dns process", e);
}
@@ -468,13 +469,13 @@ public class OrbotVpnManager implements Handler.Callback {
Log.d(TAG,"pdsnd conf:" + conf);
- File f = new File(fileDir,"pdnsd.conf");
+ File fPid = new File(fileDir,pdnsdPort + "pdnsd.conf");
- if (f.exists()) {
- f.delete();
+ if (fPid.exists()) {
+ fPid.delete();
}
- FileOutputStream fos = new FileOutputStream(f, false);
+ FileOutputStream fos = new FileOutputStream(fPid, false);
PrintStream ps = new PrintStream(fos);
ps.print(conf);
ps.close();
@@ -489,7 +490,7 @@ public class OrbotVpnManager implements Handler.Callback {
}
}
- return f;
+ return fPid;
}
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
deleted file mode 100644
index 688a8610..00000000
--- a/orbotservice/src/main/java/org/torproject/android/service/vpn/TorVpnService.java
+++ /dev/null
@@ -1,115 +0,0 @@
-package org.torproject.android.service.vpn;
-
-import android.app.Service;
-import android.content.BroadcastReceiver;
-import android.content.Context;
-import android.content.Intent;
-import android.content.IntentFilter;
-import android.net.VpnService;
-import androidx.localbroadcastmanager.content.LocalBroadcastManager;
-import android.util.Log;
-import org.torproject.android.service.OrbotService;
-import org.torproject.android.service.TorServiceConstants;
-import org.torproject.android.service.util.Prefs;
-
-import java.io.IOException;
-import java.util.concurrent.TimeoutException;
-
-/**
- * Created by n8fr8 on 9/26/16.
- */
-public class TorVpnService extends VpnService {
-
- public static final String TAG = "TorVpnService";
-
- public static final String ACTION_START = "start";
- public static final String ACTION_STOP = "stop";
-
- public static void start(Context context) {
- Intent intent = new Intent(context, TorVpnService.class);
- intent.setAction(ACTION_START);
- context.startService(intent);
- }
-
- public static void stop(Context context) {
- Intent intent = new Intent(context, TorVpnService.class);
- intent.setAction(ACTION_STOP);
- context.startService(intent);
- }
-
- OrbotVpnManager mVpnManager;
-
- @Override
- public void onCreate() {
- super.onCreate();
- try {
- mVpnManager = new OrbotVpnManager(this);
- } catch (IOException e) {
- e.printStackTrace();
- } catch (TimeoutException e) {
- e.printStackTrace();
- }
-
-
- }
-
- /* (non-Javadoc)
- * @see android.app.Service#onStart(android.content.Intent, int)
- */
- public int onStartCommand(Intent intent, int flags, int startId) {
-
- String action = ACTION_START;
- if (intent != null && intent.getAction() != null)
- action = intent.getAction();
-
- if (ACTION_START.equals(action)) {
-
- if (mLocalBroadcastReceiver == null) {
- mLocalBroadcastReceiver = new BroadcastReceiver() {
-
- @Override
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- if (action == null)
- return;
-
- if (action.equals(TorServiceConstants.LOCAL_ACTION_PORTS)) {
-
- mVpnManager.handleIntent(new Builder(), intent);
- }
- }
- };
- LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
- lbm.registerReceiver(mLocalBroadcastReceiver,
- new IntentFilter(TorServiceConstants.LOCAL_ACTION_PORTS));
- }
-
- } else if (ACTION_STOP.equals(action)) {
- Log.d(TAG, "clearing VPN Proxy");
- Prefs.putUseVpn(false);
- LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
- lbm.unregisterReceiver(mLocalBroadcastReceiver);
- mLocalBroadcastReceiver = null;
- }
-
- mVpnManager.handleIntent(new Builder(), intent);
-
- return Service.START_STICKY;
- }
-
- @Override
- public void onDestroy() {
- super.onDestroy();
-
- }
-
- /**
- * The state and log info from {@link OrbotService} are sent to the UI here in
- * the form of a local broadcast. Regular broadcasts can be sent by any app,
- * so local ones are used here so other apps cannot interfere with Orbot's
- * operation.
- */
- private BroadcastReceiver mLocalBroadcastReceiver = null;
-
-
-}