commit 0123927ab563af9afd13924dd3f8a7eb8777ba36
Author: b <dsnake(a)protonmail.com>
Date: Thu Dec 9 18:42:42 2021 -0500
Interface for #540
- External apps can request orbot to spin up a v3 service
- When cancelled, apps are informed
- When service is created, apps get the .onion url
Also introdued a change where onion service domains are updated when a
fresh connection tor tor is achieved, so users don't have to restart app
---
.../org/torproject/android/OrbotMainActivity.java | 40 ++++++++++++++++++++--
.../torproject/android/service/OrbotService.java | 9 ++---
.../android/service/TorServiceConstants.java | 5 +++
3 files changed, 48 insertions(+), 6 deletions(-)
diff --git a/app/src/main/java/org/torproject/android/OrbotMainActivity.java b/app/src/main/java/org/torproject/android/OrbotMainActivity.java
index 3853f957..2310b3b8 100644
--- a/app/src/main/java/org/torproject/android/OrbotMainActivity.java
+++ b/app/src/main/java/org/torproject/android/OrbotMainActivity.java
@@ -8,6 +8,7 @@ import android.app.AlertDialog;
import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
+import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
@@ -16,6 +17,7 @@ import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
+import android.database.Cursor;
import android.net.Uri;
import android.net.VpnService;
import android.os.Build;
@@ -98,6 +100,7 @@ import static org.torproject.android.service.vpn.VpnPrefs.PREFS_KEY_TORIFIED;
public class OrbotMainActivity extends AppCompatActivity implements OrbotConstants {
private static final String INTENT_ACTION_REQUEST_V3_ONION_SERVICE = "org.torproject.android.REQUEST_V3_ONION_SERVICE";
+ private static final String INTENT_EXTRA_REQUESTED_V3_HOSTNAME = "org.torproject.android.REQUESTED_V3_HOSTNAME";
private static final String INTENT_ACTION_REQUEST_START_TOR = "org.torproject.android.START_TOR";
private static final int REQUEST_VPN = 8888;
private static final int REQUEST_SETTINGS = 0x9874;
@@ -133,6 +136,9 @@ public class OrbotMainActivity extends AppCompatActivity implements OrbotConstan
private String torStatus = null; //latest status reported from the tor service
private Intent lastStatusIntent; // the last ACTION_STATUS Intent received
+ // used when apps request a new v3 service
+ private long lastInsertedOnionServiceRowId = -1;
+
/**
* 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,
@@ -507,7 +513,8 @@ public class OrbotMainActivity extends AppCompatActivity implements OrbotConstan
fields.put(OnionServiceContentProvider.OnionService.CREATED_BY_USER, 0);
ContentResolver contentResolver = getContentResolver();
- contentResolver.insert(OnionServiceContentProvider.CONTENT_URI, fields);
+ lastInsertedOnionServiceRowId = ContentUris.parseId(contentResolver.insert(OnionServiceContentProvider.CONTENT_URI, fields));
+
if (torStatus.equals(TorServiceConstants.STATUS_OFF)) {
startTor();
@@ -538,7 +545,11 @@ public class OrbotMainActivity extends AppCompatActivity implements OrbotConstan
new AlertDialog.Builder(this)
.setMessage(getString(R.string.hidden_service_request, v3LocalPort))
.setPositiveButton(R.string.allow, (d, w) -> enableV3OnionService(v3LocalPort, v3onionPort, finalName))
- .setNegativeButton(R.string.deny, (d, w) -> d.dismiss())
+ .setNegativeButton(R.string.deny, (d, w) -> {
+ setResult(RESULT_CANCELED);
+ d.dismiss();
+ finish();
+ })
.show();
return;
@@ -804,6 +815,10 @@ public class OrbotMainActivity extends AppCompatActivity implements OrbotConstan
}
+ // if new onion hostnames are generated, update local DB
+ sendIntentToService(TorServiceConstants.ACTION_UPDATE_ONION_NAMES);
+
+
if (autoStartFromIntent) {
autoStartFromIntent = false;
@@ -858,6 +873,27 @@ public class OrbotMainActivity extends AppCompatActivity implements OrbotConstan
resetBandwidthStatTextviews();
break;
+
+ case TorServiceConstants.STATUS_V3_NAMES_UPDATED:
+ if (lastInsertedOnionServiceRowId == -1) break; // another app did not request an onion service
+ ContentResolver cr = getContentResolver();
+ String where = OnionServiceContentProvider.OnionService._ID + "=" + lastInsertedOnionServiceRowId;
+ Cursor v3Cursor = cr.query(OnionServiceContentProvider.CONTENT_URI, OnionServiceContentProvider.PROJECTION,
+ where, null, null);
+ if (v3Cursor == null || v3Cursor.getCount() != 1 || !v3Cursor.moveToFirst()) {
+ if (v3Cursor != null) v3Cursor.close();
+ setResult(RESULT_CANCELED);
+ finish();
+ return;
+ }
+ String hostname = v3Cursor.getString(v3Cursor.getColumnIndex(OnionServiceContentProvider.OnionService.DOMAIN));
+ v3Cursor.close();
+ if (TextUtils.isEmpty(hostname)) break;
+ Intent response = new Intent();
+ response.putExtra(INTENT_EXTRA_REQUESTED_V3_HOSTNAME, hostname);
+ setResult(RESULT_OK, response);
+ finish();
+ return;
}
}
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 1b15e0da..3cc7ab40 100644
--- a/orbotservice/src/main/java/org/torproject/android/service/OrbotService.java
+++ b/orbotservice/src/main/java/org/torproject/android/service/OrbotService.java
@@ -679,7 +679,6 @@ public class OrbotService extends VpnService implements TorServiceConstants, Orb
*/
private void startTor() {
try {
-
if (torServiceConnection != null && conn != null)
{
showConnectedToTorNetworkNotification();
@@ -701,7 +700,6 @@ public class OrbotService extends VpnService implements TorServiceConstants, Orb
}
startTorService();
-
if (Prefs.hostOnionServicesEnabled()) {
try {
updateV3OnionNames();
@@ -735,8 +733,8 @@ public class OrbotService extends VpnService implements TorServiceConstants, Orb
contentResolver.update(V3_ONION_SERVICES_CONTENT_URI, fields, OnionService._ID + "=" + id, null);
}
}
-
}
+ sendCallbackStatus(STATUS_V3_NAMES_UPDATED);
} catch (Exception e) {
e.printStackTrace();
}
@@ -1436,7 +1434,10 @@ public class OrbotService extends VpnService implements TorServiceConstants, Orb
}
} else if (action.equals(ACTION_STOP)) {
stopTorAsync();
- } else if (action.equals(ACTION_START_VPN)) {
+ } else if (action.equals(ACTION_UPDATE_ONION_NAMES)) {
+ updateV3OnionNames();
+ }
+ else if (action.equals(ACTION_START_VPN)) {
if (mVpnManager != null && (!mVpnManager.isStarted())) {
//start VPN here
Intent vpnIntent = VpnService.prepare(OrbotService.this);
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 02ab7429..b9b410ad 100644
--- a/orbotservice/src/main/java/org/torproject/android/service/TorServiceConstants.java
+++ b/orbotservice/src/main/java/org/torproject/android/service/TorServiceConstants.java
@@ -34,6 +34,8 @@ public interface TorServiceConstants {
String ACTION_START_VPN = "org.torproject.android.intent.action.START_VPN";
String ACTION_STOP_VPN = "org.torproject.android.intent.action.STOP_VPN";
+ String ACTION_UPDATE_ONION_NAMES = "org.torproject.android.intent.action.UPDATE_ONION_NAMES";
+
String ACTION_START_ON_BOOT = "org.torproject.android.intent.action.START_BOOT";
int REQUEST_VPN = 7777;
@@ -78,6 +80,9 @@ public interface TorServiceConstants {
* All tor-related services and daemons are stopped
*/
String STATUS_OFF = "OFF";
+
+ String STATUS_V3_NAMES_UPDATED = "V3_NAMES_UPDATED";
+
/**
* All tor-related services and daemons have completed starting
*/