Skip to content

Commit

Permalink
Attempt to fix DeadObjectException when unbindService
Browse files Browse the repository at this point in the history
  • Loading branch information
Gedsh committed Jan 11, 2022
1 parent 5e4ec28 commit fda55b7
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,72 +42,79 @@ class ConnectionRecordsGetter @Inject constructor(

private val bound = AtomicBoolean(false)

@Volatile
private var serviceConnection: ServiceConnection? = null
private val serviceConnection = object : ServiceConnection {
override fun onServiceConnected(name: ComponentName, service: IBinder) {
if (service is VPNBinder) {
serviceVPN = WeakReference(service.service)
}
}

override fun onServiceDisconnected(name: ComponentName) {
if (bound.compareAndSet(true, false)) {
serviceVPN = null
}
}
}

@Volatile
private var serviceVPN: WeakReference<ServiceVPN?>? = null

fun getConnectionRawRecords(): List<ConnectionRecord?> {
if (serviceVPN == null && serviceConnection == null
&& bound.compareAndSet(false, true)
) {
if (bound.compareAndSet(false, true)) {
logi("ConnectionRecordsGetter bind to VPN service")
bindToVPNService()
}

if (!bound.get()) {
return emptyList()
}

lockConnectionRawRecordsListForRead(true)

val rawRecords = ArrayList<ConnectionRecord?>(
serviceVPN?.get()?.dnsQueryRawRecords ?: emptyList()
)
val rawRecords = try {
ArrayList<ConnectionRecord?>(
serviceVPN?.get()?.dnsQueryRawRecords ?: emptyList()
)
} catch (e: Exception) {
logw("ConnectionRecordsGetter getConnectionRawRecords", e)
emptyList()
}

lockConnectionRawRecordsListForRead(false)

return rawRecords
}

fun clearConnectionRawRecords() {
serviceVPN?.get()?.clearDnsQueryRawRecords()
try {
serviceVPN?.get()?.clearDnsQueryRawRecords()
} catch (e: java.lang.Exception) {
logw("ConnectionRecordsGetter clearConnectionRawRecords", e)
}
}

fun connectionRawRecordsNoMoreRequired() {
unbindVPNService()
}

private fun lockConnectionRawRecordsListForRead(lock: Boolean) {
serviceVPN?.get()?.lockDnsQueryRawRecordsListForRead(lock)
try {
serviceVPN?.get()?.lockDnsQueryRawRecordsListForRead(lock)
} catch (e: Exception) {
logw("ConnectionRecordsGetter lockConnectionRawRecordsListForRead", e)
}
}

@Synchronized
private fun bindToVPNService() {
serviceConnection = object : ServiceConnection {
override fun onServiceConnected(name: ComponentName, service: IBinder) {
if (service is VPNBinder) {
serviceVPN = WeakReference(service.service)
}
}

override fun onServiceDisconnected(name: ComponentName) {
serviceVPN = null
serviceConnection = null
}
}

val intent = Intent(context, ServiceVPN::class.java)
serviceConnection?.let {
serviceConnection.let {
context.bindService(intent, it, Context.BIND_IMPORTANT)
}
}

private fun unbindVPNService() {
if (bound.compareAndSet(true, false)) {
logi("ConnectionRecordsGetter unbind VPN service")

try {
serviceConnection?.let { context.unbindService(it) }
serviceConnection.let { context.unbindService(it) }
} catch (e: Exception) {
logw(
"ConnectionRecordsGetter unbindVPNService exception "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -762,10 +762,14 @@ public IBinder onBind(Intent intent) {

@Override
public boolean onUnbind(Intent intent) {

logi("ServiceVPN onUnbind");
return true;
}

return super.onUnbind(intent);
@Override
public void onRebind(Intent intent) {
logi("ServiceVPN onRebind");
super.onRebind(intent);
}

@Override
Expand Down Expand Up @@ -878,4 +882,30 @@ public void onLowMemory() {
clearDnsQueryRawRecords();
loge("ServiceVPN low memory");
}

@Override
public void onTaskRemoved(Intent rootIntent) {

try {
notificationManager.cancel(DEFAULT_NOTIFICATION_ID);
stopForeground(true);
} catch (Exception e) {
loge("VPNService onTaskRemoved", e);
}

stopSelf();

try {
Thread.sleep(100);
} catch (Exception ignored) {
} finally {
defaultPreferences.get().edit().putBoolean(VPN_SERVICE_ENABLED, true).apply();
ServiceVPNHelper.start(
"ModulesReceiver start VPN service after task removed",
this
);
}

super.onTaskRemoved(rootIntent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ public Allowed isAddressAllowed(ServiceVPN vpn, Packet packet) {
// https://android.googlesource.com/platform/system/core/+/master/include/private/android_filesystem_config.h
if ((!vpn.canFilter) && isSupported(packet.protocol)) {
packet.allowed = true;
} else if (packet.dport == DNS_OVER_TLS_PORT
&& vpnPreferences.getIgnoreSystemDNS()) {
logw("Block DNS over TLS " + packet);
} else if (vpnDnsSet.contains(packet.daddr)
&& packet.dport != PLAINTEXT_DNS_PORT
&& vpnPreferences.getIgnoreSystemDNS()) {
logw("Block DNS over HTTPS " + packet);
} else if ((packet.uid == vpnPreferences.getOwnUID()
|| vpnPreferences.getCompatibilityMode()
&& packet.uid == SPECIAL_UID_KERNEL
Expand All @@ -160,13 +167,6 @@ && isSupported(packet.protocol)) {
if (!vpnPreferences.getCompatibilityMode()) {
logw("Allowing self " + packet);
}
} else if (packet.dport == DNS_OVER_TLS_PORT
&& vpnPreferences.getIgnoreSystemDNS()) {
logw("Block DNS over TLS " + packet);
} else if (vpnDnsSet.contains(packet.daddr)
&& packet.dport != PLAINTEXT_DNS_PORT
&& vpnPreferences.getIgnoreSystemDNS()) {
logw("Block DNS over HTTPS " + packet);
} else if (vpnPreferences.getArpSpoofingDetection()
&& vpnPreferences.getBlockInternetWhenArpAttackDetected()
&& (ArpScanner.Companion.getArpAttackDetected()
Expand Down

0 comments on commit fda55b7

Please sign in to comment.