Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev fix service leak fixes #553 #561

Merged
merged 8 commits into from
Dec 22, 2021
155 changes: 80 additions & 75 deletions app/src/main/java/org/torproject/android/OrbotMainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,18 @@ public class OrbotMainActivity extends AppCompatActivity implements OrbotConstan
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String status = intent.getStringExtra(TorServiceConstants.EXTRA_STATUS);
if (action == null)
return;

switch (action) {
case TorServiceConstants.LOCAL_ACTION_LOG: {
Message msg = mStatusUpdateHandler.obtainMessage(STATUS_UPDATE);
msg.obj = intent.getStringExtra(TorServiceConstants.LOCAL_EXTRA_LOG);
msg.getData().putString("status", intent.getStringExtra(TorServiceConstants.EXTRA_STATUS));

if (!TextUtils.isEmpty(status))
msg.getData().putString("status", status);

mStatusUpdateHandler.sendMessage(msg);

break;
Expand All @@ -173,7 +177,9 @@ public void onReceive(Context context, Intent intent) {
msg.getData().putLong("lastWritten", lastWritten);
msg.getData().putLong("totalWritten", totalWritten);
msg.getData().putLong("totalRead", totalRead);
msg.getData().putString("status", intent.getStringExtra(TorServiceConstants.EXTRA_STATUS));

if (!TextUtils.isEmpty(status))
msg.getData().putString("status", status);

mStatusUpdateHandler.sendMessage(msg);

Expand Down Expand Up @@ -203,7 +209,9 @@ public void onReceive(Context context, Intent intent) {
lastStatusIntent = intent;

Message msg = mStatusUpdateHandler.obtainMessage(STATUS_UPDATE);
msg.getData().putString("status", intent.getStringExtra(TorServiceConstants.EXTRA_STATUS));

if (!TextUtils.isEmpty(status))
msg.getData().putString("status", status);

mStatusUpdateHandler.sendMessage(msg);
break;
Expand All @@ -214,6 +222,9 @@ public void onReceive(Context context, Intent intent) {
msg.getData().putInt("socks", intent.getIntExtra(OrbotService.EXTRA_SOCKS_PROXY_PORT, -1));
msg.getData().putInt("http", intent.getIntExtra(OrbotService.EXTRA_HTTP_PROXY_PORT, -1));

if (!TextUtils.isEmpty(status))
msg.getData().putString("status", status);

mStatusUpdateHandler.sendMessage(msg);

break;
Expand Down Expand Up @@ -291,7 +302,7 @@ private void stopTor() {
if (mBtnVPN.isChecked()) sendIntentToService(ACTION_STOP_VPN);
sendIntentToService(ACTION_STOP);
}
else if (torStatus.equals(STATUS_STARTING)) {
else if (torStatus.equals(STATUS_STARTING)||torStatus.equals(STATUS_STOPPING)) {

if (!waitingToStop) {
waitingToStop = true;
Expand Down Expand Up @@ -324,6 +335,7 @@ private void doLayout() {

lblStatus = findViewById(R.id.lblStatus);
lblStatus.setOnClickListener(v -> mDrawer.openDrawer(GravityCompat.END));
lblStatus.setText(String.format("Tor v%s", OrbotService.BINARY_TOR_VERSION));

lblPorts = findViewById(R.id.lblPorts);

Expand Down Expand Up @@ -745,7 +757,7 @@ protected void onResume() {
requestTorStatus();

if (torStatus == null)
updateStatus("", TorServiceConstants.STATUS_STOPPING);
updateStatus("", STATUS_OFF);
else
updateStatus(null, torStatus);

Expand Down Expand Up @@ -807,7 +819,7 @@ private void showAlert(String title, String msg, boolean button) {
* Update the layout_main UI based on the status of {@link OrbotService}.
* {@code torServiceMsg} must never be {@code null}
*/
private synchronized void updateStatus(String torServiceMsg, String newTorStatus) {
private void updateStatus(String torServiceMsg, String newTorStatus) {

if (!TextUtils.isEmpty(torServiceMsg)) {
if (torServiceMsg.contains(TorServiceConstants.LOG_NOTICE_HEADER)) {
Expand All @@ -817,105 +829,98 @@ private synchronized void updateStatus(String torServiceMsg, String newTorStatus
mTxtOrbotLog.append(torServiceMsg + '\n');
}

if (torStatus == null || (newTorStatus != null && newTorStatus.equals(torStatus))) {
torStatus = newTorStatus;
return;
} else {
torStatus = newTorStatus;
}

if (torStatus == null) {
return;
}

switch (torStatus) {
case TorServiceConstants.STATUS_ON:
if (!TextUtils.isEmpty(newTorStatus)) {
if (torStatus == null || (newTorStatus != null && newTorStatus.equals(torStatus))) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inconsequential, but newTorStatus should never be null there because TextUtils.isEmpty returned false

torStatus = newTorStatus;
return;
} else {
torStatus = newTorStatus;
}

imgStatus.setImageResource(R.drawable.toron);

mBtnStart.setText(R.string.menu_stop);
mPulsator.stop();
switch (torStatus) {
case TorServiceConstants.STATUS_ON:

if (Prefs.beSnowflakeProxy())
{
lblStatus.setText(getString(R.string.status_activated)+"\n"
+ getString(R.string.snowflake_proxy_enabled));
imgStatus.setImageResource(R.drawable.toron);

SnowfallView sv = findViewById(R.id.snowflake_view);
sv.setVisibility(View.VISIBLE);
sv.restartFalling();
mBtnStart.setText(R.string.menu_stop);
mPulsator.stop();

}
else
{
lblStatus.setText(getString(R.string.status_activated));
SnowfallView sv = findViewById(R.id.snowflake_view);
sv.setVisibility(View.GONE);
sv.stopFalling();
if (Prefs.beSnowflakeProxy()) {
lblStatus.setText(getString(R.string.status_activated) + "\n"
+ getString(R.string.snowflake_proxy_enabled));

SnowfallView sv = findViewById(R.id.snowflake_view);
sv.setVisibility(View.VISIBLE);
sv.restartFalling();

}
} else {
lblStatus.setText(getString(R.string.status_activated));
SnowfallView sv = findViewById(R.id.snowflake_view);
sv.setVisibility(View.GONE);
sv.stopFalling();
}

// if new onion hostnames are generated, update local DB
sendIntentToService(TorServiceConstants.ACTION_UPDATE_ONION_NAMES);
// if new onion hostnames are generated, update local DB
sendIntentToService(TorServiceConstants.ACTION_UPDATE_ONION_NAMES);


if (autoStartFromIntent) {
autoStartFromIntent = false;
Intent resultIntent = lastStatusIntent;

if (autoStartFromIntent) {
autoStartFromIntent = false;
Intent resultIntent = lastStatusIntent;
if (resultIntent == null)
resultIntent = new Intent(ACTION_START);

if (resultIntent == null)
resultIntent = new Intent(ACTION_START);
resultIntent.putExtra(
TorServiceConstants.EXTRA_STATUS,
torStatus == null ? TorServiceConstants.STATUS_OFF : torStatus
);

resultIntent.putExtra(
TorServiceConstants.EXTRA_STATUS,
torStatus == null ? TorServiceConstants.STATUS_OFF : torStatus
);
setResult(RESULT_OK, resultIntent);

setResult(RESULT_OK, resultIntent);
finish();
}

finish();
}
break;

break;
case TorServiceConstants.STATUS_STARTING:

case TorServiceConstants.STATUS_STARTING:
imgStatus.setImageResource(R.drawable.torstarting);

imgStatus.setImageResource(R.drawable.torstarting);
if (torServiceMsg != null) {
if (torServiceMsg.contains(TorServiceConstants.LOG_NOTICE_BOOTSTRAPPED))
lblStatus.setText(torServiceMsg);
} else {
lblStatus.setText(getString(R.string.status_starting_up));
}

if (torServiceMsg != null) {
if (torServiceMsg.contains(TorServiceConstants.LOG_NOTICE_BOOTSTRAPPED))
lblStatus.setText(torServiceMsg);
} else {
lblStatus.setText(getString(R.string.status_starting_up));
}
mBtnStart.setText("...");

mBtnStart.setText("...");
break;

break;
case TorServiceConstants.STATUS_STOPPING:

case TorServiceConstants.STATUS_STOPPING:
if (torServiceMsg != null && torServiceMsg.contains(TorServiceConstants.LOG_NOTICE_HEADER))
lblStatus.setText(torServiceMsg);

if (torServiceMsg != null && torServiceMsg.contains(TorServiceConstants.LOG_NOTICE_HEADER))
imgStatus.setImageResource(R.drawable.torstarting);
lblStatus.setText(torServiceMsg);

imgStatus.setImageResource(R.drawable.torstarting);
lblStatus.setText(torServiceMsg);

break;
break;

case TorServiceConstants.STATUS_OFF:
case TorServiceConstants.STATUS_OFF:

imgStatus.setImageResource(R.drawable.toroff);
lblStatus.setText(String.format("Tor v%s", OrbotService.BINARY_TOR_VERSION));
mBtnStart.setText(R.string.menu_start);
mPulsator.start();
resetBandwidthStatTextviews();
imgStatus.setImageResource(R.drawable.toroff);
lblStatus.setText(String.format("Tor v%s", OrbotService.BINARY_TOR_VERSION));
mBtnStart.setText(R.string.menu_start);
mPulsator.start();
resetBandwidthStatTextviews();

break;
break;


}
}
}

Expand Down
2 changes: 1 addition & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ext {
guardian_jtorctl : "0.4.5.7",
ipt_proxy : "1.3.0",
portmapper : "2.0.5",
tor_android : "0.4.6.8"
tor_android : "0.4.6.9"
]

libs = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,17 +293,20 @@ else if (Prefs.beSnowflakeProxy())
disableSnowflakeProxy();

stopTor();
sendCallbackStatus(STATUS_OFF);

//stop the foreground priority and make sure to remove the persistent notification
stopForeground(true);

sendCallbackLogMessage(getString(R.string.status_disabled));

} catch (Exception e) {
logNotice("An error occurred stopping Tor: " + e.getMessage());
sendCallbackLogMessage(getString(R.string.something_bad_happened));
}
clearNotifications();
sendCallbackStatus(STATUS_OFF);

stopSelf();
}

private void stopTorOnError(String message) {
Expand Down Expand Up @@ -414,7 +417,6 @@ private void stopTor() throws Exception {
shouldUnbindTorService = false;
}

stopSelf();
}

private void requestTorRereadConfig() {
Expand Down Expand Up @@ -697,8 +699,6 @@ private void startTor() {
sendCallbackStatus(STATUS_STARTING);

showToolbarNotification(getString(R.string.status_starting_up), NOTIFY_ID, R.drawable.ic_stat_tor);
//sendCallbackLogMessage(getString(R.string.status_starting_up));
//logNotice(getString(R.string.status_starting_up));

ArrayList<String> customEnv = new ArrayList<>();

Expand All @@ -708,6 +708,7 @@ private void startTor() {
}

startTorService();

if (Prefs.hostOnionServicesEnabled()) {
try {
updateV3OnionNames();
Expand Down Expand Up @@ -750,7 +751,7 @@ private void updateV3OnionNames() throws SecurityException {
*/
String oldStatus = mCurrentStatus;
Intent intent = new Intent(LOCAL_ACTION_V3_NAMES_UPDATED);
intent.putExtra(EXTRA_STATUS, mCurrentStatus);
// intent.putExtra(EXTRA_STATUS, mCurrentStatus);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

mCurrentStatus = oldStatus;
Expand Down Expand Up @@ -831,6 +832,9 @@ public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
public void onServiceDisconnected(ComponentName componentName) {
if (Prefs.useDebugLogging())
Log.d(OrbotConstants.TAG, "TorService: onServiceDisconnected");

sendCallbackStatus(STATUS_OFF);

}

@Override
Expand All @@ -841,6 +845,9 @@ public void onNullBinding(ComponentName componentName) {
@Override
public void onBindingDied(ComponentName componentName) {
Log.w(OrbotConstants.TAG, "TorService: onBindingDied");

sendCallbackStatus(STATUS_OFF);

}
};

Expand Down Expand Up @@ -940,7 +947,6 @@ protected void sendCallbackBandwidth(long lastWritten, long lastRead, long total
intent.putExtra("totalRead", totalRead);
intent.putExtra("lastWritten", lastWritten);
intent.putExtra("lastRead", lastRead);
intent.putExtra(EXTRA_STATUS, mCurrentStatus);

LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
Expand All @@ -949,8 +955,6 @@ private void sendCallbackLogMessage(final String logMessage) {
mHandler.post(() -> {
Intent intent = new Intent(LOCAL_ACTION_LOG); // You can also include some extra data.
intent.putExtra(LOCAL_EXTRA_LOG, logMessage);
intent.putExtra(EXTRA_STATUS, mCurrentStatus);

LocalBroadcastManager.getInstance(OrbotService.this).sendBroadcast(intent);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
public class TorEventHandler implements EventHandler, TorServiceConstants {

private final static int BW_THRESDHOLD = 10000;
private final static int BW_THRESDHOLD = 0;
private final OrbotService mService;
private long lastRead = -1;
private long lastWritten = -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public interface TorServiceConstants {
/**
* {@link Intent} send by Orbot with {@code ON/OFF/STARTING/STOPPING} status
*/
String ACTION_STATUS = "org.torproject.android.intent.action.STATUS";
String ACTION_STATUS = "ORBOT.intent.action.STATUS";
/**
* {@code String} that contains a status constant: {@link #STATUS_ON},
* {@link #STATUS_OFF}, {@link #STATUS_STARTING}, or
Expand Down