Skip to content

Commit

Permalink
Disable battery optimization (Doze) for the mitm addon
Browse files Browse the repository at this point in the history
It can interfere with the mitm service, causing connections to get stuck
until device is rebooted
  • Loading branch information
emanuele-f committed Jan 1, 2024
1 parent 74c2aac commit bb68ac6
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 5 deletions.
31 changes: 28 additions & 3 deletions app/src/main/java/com/emanuelef/remote_capture/MitmAddon.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with PCAPdroid. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2022 - Emanuele Faranda
* Copyright 2022-24 - Emanuele Faranda
*/

package com.emanuelef.remote_capture;
Expand All @@ -34,6 +34,7 @@
import android.os.Message;
import android.os.Messenger;
import android.os.ParcelFileDescriptor;
import android.os.PowerManager;
import android.os.RemoteException;

import androidx.annotation.NonNull;
Expand All @@ -48,8 +49,8 @@
import java.lang.ref.WeakReference;

public class MitmAddon {
public static final long PACKAGE_VERSION_CODE = 16;
public static final String PACKAGE_VERSION_NAME = "v0.16";
public static final long PACKAGE_VERSION_CODE = 17;
public static final String PACKAGE_VERSION_NAME = "v1.0";
public static final String REPOSITORY = "https://github.com/emanuele-f/PCAPdroid-mitm";
private static final String TAG = "MitmAddon";
private final Context mContext;
Expand Down Expand Up @@ -295,4 +296,28 @@ public boolean stopProxy() {
return false;
}
}

// NOTE: doze could be disabled by PCAPdroid itself, however this is moved to the addon to avoid
// any issues with the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS Google Play policies
public boolean disableDoze() {
if(mService == null)
return false;

Log.i(TAG, "Send disable doze");
Message msg = Message.obtain(null, MitmAPI.MSG_DISABLE_DOZE);
try {
mService.send(msg);
return true;
} catch (RemoteException e) {
e.printStackTrace();
return false;
}
}

public static boolean isDozeEnabled(Context context) {
final PowerManager manager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);

return (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) && (manager != null)
&& !manager.isIgnoringBatteryOptimizations(MitmAPI.PACKAGE_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with PCAPdroid. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2022 - Emanuele Faranda
* Copyright 2022-24 - Emanuele Faranda
*/

package com.emanuelef.remote_capture;
Expand Down Expand Up @@ -483,6 +483,11 @@ public void onMitmGetCaCertificateResult(@Nullable String ca_pem) {
return;
}

if (MitmAddon.isDozeEnabled(mContext)) {
Utils.showToastLong(mContext, R.string.mitm_doze_notice);
mAddon.disableDoze();
}

if(mThread != null)
mThread.interrupt();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with PCAPdroid. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2020-21 - Emanuele Faranda
* Copyright 2020-24 - Emanuele Faranda
*/

package com.emanuelef.remote_capture.activities;
Expand Down Expand Up @@ -55,6 +55,7 @@
import com.emanuelef.remote_capture.Billing;
import com.emanuelef.remote_capture.CaptureService;
import com.emanuelef.remote_capture.Log;
import com.emanuelef.remote_capture.MitmAddon;
import com.emanuelef.remote_capture.R;
import com.emanuelef.remote_capture.Utils;
import com.emanuelef.remote_capture.model.Prefs;
Expand Down Expand Up @@ -147,6 +148,7 @@ public boolean onMenuItemSelected(MenuItem item) {
String deviceInfo = Utils.getBuildInfo(this) + "\n\n" +
Prefs.asString(this);

// Private DNS
Utils.PrivateDnsMode dns_mode = CaptureService.getPrivateDnsMode();
if(dns_mode == null) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
Expand All @@ -164,6 +166,9 @@ public boolean onMenuItemSelected(MenuItem item) {
if(dns_mode != null)
deviceInfo += "\n" + "PrivateDnsMode: " + dns_mode;

// Mitm doze
deviceInfo += "\n" + "MitmBatteryOptimized: " + ((MitmAddon.isInstalled(this) && MitmAddon.isDozeEnabled(this)) ? "true" : "false");

LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.scrollable_dialog, null);
((TextView)view.findViewById(R.id.text)).setText(deviceInfo);
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/pcapdroid/mitm/MitmAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class MitmAPI {
public static final int MSG_START_MITM = 1;
public static final int MSG_GET_CA_CERTIFICATE = 2;
public static final int MSG_STOP_MITM = 3;
public static final int MSG_DISABLE_DOZE = 4;
public static final String MITM_CONFIG = "mitm_config";
public static final String CERTIFICATE_RESULT = "certificate";
public static final String SSLKEYLOG_RESULT = "sslkeylog";
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -493,4 +493,5 @@
<string name="decryption_info_no_rule">This connection will not be decrypted. Create a decryption rule to decrypt it</string>
<string name="tls_decryption_no_rules_notice">TLS decryption is only applied to connections that match the configured rules. Do you want to create decryption rules now?</string>
<string name="active_vpn_detected">Active VPN detected</string>
<string name="mitm_doze_notice">Battery optimization may interfere with the mitm addon</string>
</resources>

0 comments on commit bb68ac6

Please sign in to comment.