Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor and rewrite the OrbotConstants
Browse files Browse the repository at this point in the history
meenbeese committed Jan 14, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 0fd1c1f commit ca4067c
Showing 8 changed files with 212 additions and 196 deletions.
6 changes: 3 additions & 3 deletions app/src/main/java/org/torproject/android/OrbotApp.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package org.torproject.android

import android.app.Application
import android.content.Intent
import android.content.res.Configuration

import org.torproject.android.core.Languages
import org.torproject.android.core.LocaleHelper
import org.torproject.android.service.OrbotConstants
import org.torproject.android.service.util.Prefs

import java.util.Locale

class OrbotApp : Application(), OrbotConstants {
class OrbotApp : Application() {


override fun onCreate() {
Original file line number Diff line number Diff line change
@@ -39,7 +39,7 @@ import org.torproject.android.service.vpn.TorifiedApp
import java.util.Arrays
import java.util.StringTokenizer

class AppManagerActivity : AppCompatActivity(), View.OnClickListener, OrbotConstants {
class AppManagerActivity : AppCompatActivity(), View.OnClickListener {
inner class TorifiedAppWrapper {
var header: String? = null
var subheader: String? = null
5 changes: 5 additions & 0 deletions orbotservice/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
apply plugin: 'com.android.library'
apply from: "../commons.gradle"
apply from : '../dependencies.gradle'
apply plugin: 'org.jetbrains.kotlin.android'

android {
defaultConfig {
@@ -31,6 +32,9 @@ android {
xmlReport false
}
namespace 'org.torproject.android.service'
kotlinOptions {
jvmTarget = '17'
}
}

dependencies {
@@ -52,4 +56,5 @@ dependencies {
)

implementation files('../libs/geoip.jar')
implementation 'androidx.core:core-ktx:1.10.1'
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
package org.torproject.android.service

import org.torproject.jni.TorService

object OrbotConstants {
const val TAG = "Orbot"

const val PREF_OR = "pref_or"
const val PREF_OR_PORT = "pref_or_port"
const val PREF_OR_NICKNAME = "pref_or_nickname"
const val PREF_REACHABLE_ADDRESSES = "pref_reachable_addresses"
const val PREF_REACHABLE_ADDRESSES_PORTS = "pref_reachable_addresses_ports"

const val PREF_TOR_SHARED_PREFS = "org.torproject.android_preferences"

const val PREF_SOCKS = "pref_socks"

const val PREF_HTTP = "pref_http"

const val PREF_ISOLATE_DEST = "pref_isolate_dest"
const val PREF_ISOLATE_PORT = "pref_isolate_port"
const val PREF_ISOLATE_PROTOCOL = "pref_isolate_protocol"

const val PREF_CONNECTION_PADDING = "pref_connection_padding"
const val PREF_REDUCED_CONNECTION_PADDING = "pref_reduced_connection_padding"
const val PREF_CIRCUIT_PADDING = "pref_circuit_padding"
const val PREF_REDUCED_CIRCUIT_PADDING = "pref_reduced_circuit_padding"

const val PREF_PREFER_IPV6 = "pref_prefer_ipv6"
const val PREF_DISABLE_IPV4 = "pref_disable_ipv4"


const val APP_TOR_KEY = "_app_tor"
const val APP_DATA_KEY = "_app_data"
const val APP_WIFI_KEY = "_app_wifi"


const val DIRECTORY_TOR_DATA = "tordata"

// geoip data file asset key
const val GEOIP_ASSET_KEY = "geoip"
const val GEOIP6_ASSET_KEY = "geoip6"

const val TOR_TRANSPROXY_PORT_DEFAULT = 9040

const val TOR_DNS_PORT_DEFAULT = 5400

const val HTTP_PROXY_PORT_DEFAULT = "8118" // like Privoxy!
const val SOCKS_PROXY_PORT_DEFAULT = "9050"

// control port
const val LOG_NOTICE_HEADER = "NOTICE: "
const val LOG_NOTICE_BOOTSTRAPPED = "Bootstrapped"

/**
* A request to Orbot to transparently start Tor services
*/
const val ACTION_START = TorService.ACTION_START
const val ACTION_STOP = "org.torproject.android.intent.action.STOP"

// needed when Orbot exits and tor is not running, but the notification is still active
const val ACTION_STOP_FOREGROUND_TASK = "org.torproject.android.intent.action.STOP_FOREGROUND_TASK"

const val ACTION_START_VPN = "org.torproject.android.intent.action.START_VPN"
const val ACTION_STOP_VPN = "org.torproject.android.intent.action.STOP_VPN"
const val ACTION_RESTART_VPN = "org.torproject.android.intent.action.RESTART_VPN"

const val ACTION_LOCAL_LOCALE_SET = "org.torproject.android.intent.LOCAL_LOCALE_SET"

const val ACTION_UPDATE_ONION_NAMES = "org.torproject.android.intent.action.UPDATE_ONION_NAMES"

/**
* [Intent] send by Orbot with `ON/OFF/STARTING/STOPPING` status
*/
const val ACTION_STATUS = TorService.ACTION_STATUS

/**
* `String` that contains a status constant: [.STATUS_ON],
* [.STATUS_OFF], [.STATUS_STARTING], or
* [.STATUS_STOPPING]
*/
const val EXTRA_STATUS = TorService.EXTRA_STATUS

/**
* A [String] `packageName` for Orbot to direct its status reply
* to, used in [.ACTION_START] [Intent]s sent to Orbot
*/
const val EXTRA_PACKAGE_NAME = TorService.EXTRA_PACKAGE_NAME

/**
* The SOCKS proxy settings in URL form.
*/
const val EXTRA_SOCKS_PROXY = "org.torproject.android.intent.extra.SOCKS_PROXY"
const val EXTRA_SOCKS_PROXY_HOST = "org.torproject.android.intent.extra.SOCKS_PROXY_HOST"
const val EXTRA_SOCKS_PROXY_PORT = "org.torproject.android.intent.extra.SOCKS_PROXY_PORT"

/**
* The HTTP proxy settings in URL form.
*/
const val EXTRA_HTTP_PROXY = "org.torproject.android.intent.extra.HTTP_PROXY"
const val EXTRA_HTTP_PROXY_HOST = "org.torproject.android.intent.extra.HTTP_PROXY_HOST"
const val EXTRA_HTTP_PROXY_PORT = "org.torproject.android.intent.extra.HTTP_PROXY_PORT"

const val EXTRA_DNS_PORT = "org.torproject.android.intent.extra.DNS_PORT"
const val EXTRA_TRANS_PORT = "org.torproject.android.intent.extra.TRANS_PORT"

/**
* When present, indicates with certainty that the system itself did *not* send the Intent.
* Effectively, the lack of this extra indicates that the VPN is being started by the system
* as a result of the user's always-on preference for the VPN.
* See: [Detect always-on | VPN | Android Developers](https://developer.android.com/guide/topics/connectivity/vpn#detect_always-on)
*/
const val EXTRA_NOT_SYSTEM = "org.torproject.android.intent.extra.NOT_SYSTEM"

const val LOCAL_ACTION_LOG = "log"
const val LOCAL_ACTION_STATUS = "status"
const val LOCAL_ACTION_BANDWIDTH = "bandwidth"
const val LOCAL_EXTRA_TOTAL_READ = "totalRead"
const val LOCAL_EXTRA_TOTAL_WRITTEN = "totalWritten"
const val LOCAL_EXTRA_LAST_WRITTEN = "lastWritten"
const val LOCAL_EXTRA_LAST_READ = "lastRead"
const val LOCAL_EXTRA_LOG = "log"
const val LOCAL_EXTRA_BOOTSTRAP_PERCENT = "percent"
const val LOCAL_ACTION_PORTS = "ports"
const val LOCAL_ACTION_V3_NAMES_UPDATED = "V3_NAMES_UPDATED"
const val LOCAL_ACTION_NOTIFICATION_START = "notification_start"
const val LOCAL_ACTION_SMART_CONNECT_EVENT = "smart"
const val LOCAL_EXTRA_SMART_STATUS = "status"
const val SMART_STATUS_NO_DIRECT = "no_direct"
const val SMART_STATUS_CIRCUMVENTION_ATTEMPT_FAILED = "bad_attempt_suggestion"


/**
* All tor-related services and daemons are stopped
*/
const val STATUS_OFF = TorService.STATUS_OFF

/**
* All tor-related services and daemons have completed starting
*/
const val STATUS_ON = TorService.STATUS_ON
const val STATUS_STARTING = TorService.STATUS_STARTING
const val STATUS_STOPPING = TorService.STATUS_STOPPING

/**
* The user has disabled the ability for background starts triggered by
* apps. Fallback to the old [Intent] action that brings up Orbot:
* [.ACTION_START]
*/
const val STATUS_STARTS_DISABLED = "STARTS_DISABLED"

// actions for internal command Intents
const val CMD_SET_EXIT = "setexit"
const val CMD_ACTIVE = "ACTIVE"
const val CMD_SNOWFLAKE_PROXY = "sf_proxy"

const val ONION_SERVICES_DIR = "v3_onion_services"
const val V3_CLIENT_AUTH_DIR = "v3_client_auth"

const val PREFS_DNS_PORT: String = "PREFS_DNS_PORT"

const val PREFS_KEY_TORIFIED: String = "PrefTord"

/**
* Include packages here to make the VPNService ignore these apps. This is to
* prevent tor over tor scenarios...
*/
@JvmField
val BYPASS_VPN_PACKAGES = mutableListOf(
"org.torproject.torbrowser_alpha",
"org.torproject.torbrowser",
"org.onionshare.android", // issue #618
"org.onionshare.android.fdroid",
"org.briarproject.briar.android", // https://github.com/guardianproject/orbot/issues/474
"im.cwtch.flwtch",
)

val VPN_SUGGESTED_APPS = mutableListOf(
"org.thoughtcrime.securesms", // Signal
"com.whatsapp",
"com.instagram.android",
"im.vector.app",
"org.telegram.messenger",
"com.twitter.android",
"com.facebook.orca",
"com.facebook.mlite",
"com.brave.browser",
"org.mozilla.focus",
)

const val ONION_EMOJI: String = "\uD83E\uDDC5"
}
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@

package org.torproject.android.service;

import static org.torproject.android.service.OrbotConstants.*;
import static org.torproject.jni.TorService.ACTION_ERROR;

import android.annotation.SuppressLint;
@@ -83,7 +84,7 @@


@SuppressWarnings("StringConcatenationInsideStringBufferAppend")
public class OrbotService extends VpnService implements OrbotConstants {
public class OrbotService extends VpnService {

public final static String BINARY_TOR_VERSION = TorService.VERSION_NAME;

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.torproject.android.service;

import static org.torproject.android.service.OrbotConstants.*;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@@ -10,7 +12,7 @@
import org.torproject.android.service.util.Prefs;


public class StartTorReceiver extends BroadcastReceiver implements OrbotConstants {
public class StartTorReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
@@ -24,7 +26,7 @@ public void onReceive(Context context, Intent intent) {
if (Prefs.allowBackgroundStarts()) {
Intent startTorIntent = new Intent(context, OrbotService.class).setAction(action).putExtra(OrbotConstants.EXTRA_NOT_SYSTEM, true);
if (packageName != null) {
startTorIntent.putExtra(OrbotService.EXTRA_PACKAGE_NAME, packageName);
startTorIntent.putExtra(OrbotConstants.EXTRA_PACKAGE_NAME, packageName);
}
ContextCompat.startForegroundService(context, startTorIntent);
} else if (!TextUtils.isEmpty(packageName)) {
Original file line number Diff line number Diff line change
@@ -16,6 +16,8 @@

package org.torproject.android.service.vpn;

import static org.torproject.android.service.OrbotConstants.*;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
@@ -51,7 +53,7 @@
import IPtProxy.IPtProxy;
import IPtProxy.PacketFlow;

public class OrbotVpnManager implements Handler.Callback, OrbotConstants {
public class OrbotVpnManager implements Handler.Callback {
private static final String TAG = "OrbotVpnManager";
boolean isStarted = false;
private final static String mSessionName = "OrbotVPN";
@@ -96,9 +98,9 @@ public void handleIntent(VpnService.Builder builder, Intent intent) {
}
case OrbotConstants.LOCAL_ACTION_PORTS -> {
Log.d(TAG, "setting VPN ports");
int torSocks = intent.getIntExtra(OrbotService.EXTRA_SOCKS_PROXY_PORT, -1);
int torSocks = intent.getIntExtra(OrbotConstants.EXTRA_SOCKS_PROXY_PORT, -1);
// int torHttp = intent.getIntExtra(OrbotService.EXTRA_HTTP_PROXY_PORT,-1);
int torDns = intent.getIntExtra(OrbotService.EXTRA_DNS_PORT, -1);
int torDns = intent.getIntExtra(OrbotConstants.EXTRA_DNS_PORT, -1);

//if running, we need to restart
if ((torSocks != -1 && torSocks != mTorSocks && torDns != -1 && torDns != mTorDns)) {

0 comments on commit ca4067c

Please sign in to comment.