Skip to content

Commit

Permalink
Added service to detect and block tor apps.
Browse files Browse the repository at this point in the history
  • Loading branch information
CompuGenius-Programs committed Oct 28, 2024
1 parent f0136f7 commit 2c5b094
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 7 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId = "com.cgprograms.dnslock"
minSdk = 29
targetSdk = 34
versionCode = 5
versionName = "3.1.0"
versionCode = 6
versionName = "4.0.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
Binary file modified app/release/baselineProfiles/0/app-release.dm
Binary file not shown.
Binary file modified app/release/baselineProfiles/1/app-release.dm
Binary file not shown.
4 changes: 2 additions & 2 deletions app/release/output-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"type": "SINGLE",
"filters": [],
"attributes": [],
"versionCode": 5,
"versionName": "3.1.0",
"versionCode": 6,
"versionName": "4.0.0",
"outputFile": "app-release.apk"
}
],
Expand Down
11 changes: 8 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<uses-permission android:name="android.permission.MANAGE_DEVICE_ADMINS" />
<uses-permission android:name="android.permission.BIND_DEVICE_ADMIN" />
<uses-permission android:name="android.permission.MANAGE_PROFILE_AND_DEVICE_OWNERS" />
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"/>

<application
android:allowBackup="true"
Expand All @@ -24,8 +25,8 @@
<service
android:name=".WifiTileService"
android:exported="true"
android:label="Wi-Fi Toggle"
android:icon="@drawable/baseline_wifi_24"
android:label="Wi-Fi Toggle"
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
<intent-filter>
<action android:name="android.service.quicksettings.action.QS_TILE" />
Expand All @@ -38,13 +39,13 @@
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.service.quicksettings.action.QS_TILE_PREFERENCES"/>
<action android:name="android.service.quicksettings.action.QS_TILE_PREFERENCES" />
</intent-filter>
</activity>

<receiver
android:name=".MyDeviceAdminReceiver"
android:exported="true"
android:exported="false"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data
android:name="android.app.device_admin"
Expand All @@ -54,6 +55,10 @@
</intent-filter>
</receiver>

<service
android:name=".TorMonitorService"
android:exported="false" />

</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.content.Context
import android.content.Intent
import android.os.UserManager


class MyDeviceAdminReceiver : DeviceAdminReceiver() {
override fun onEnabled(context: Context, intent: Intent) {
super.onEnabled(context, intent)
Expand All @@ -16,6 +17,8 @@ class MyDeviceAdminReceiver : DeviceAdminReceiver() {

dpm.addUserRestriction(adminComponent, UserManager.DISALLOW_CONFIG_PRIVATE_DNS)
dpm.addUserRestriction(adminComponent, UserManager.DISALLOW_CONFIG_WIFI)

context.startService(Intent(context, TorMonitorService::class.java))
}

override fun onDisabled(context: Context, intent: Intent) {
Expand Down
63 changes: 63 additions & 0 deletions app/src/main/java/com/cgprograms/dnslock/TorMonitorService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.cgprograms.dnslock

import android.app.Service
import android.app.admin.DevicePolicyManager
import android.app.usage.UsageEvents
import android.app.usage.UsageStatsManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Handler
import android.os.IBinder
import android.os.Looper

class TorMonitorService : Service() {

private val handler = Handler(Looper.getMainLooper())
private val checkInterval: Long = 60000

private val checkTorRunnable = object : Runnable {
override fun run() {
checkTorUsage()
handler.postDelayed(this, checkInterval)
}
}

override fun onCreate() {
super.onCreate()
handler.post(checkTorRunnable)
}

override fun onDestroy() {
super.onDestroy()
handler.removeCallbacks(checkTorRunnable)
}

override fun onBind(intent: Intent?): IBinder? {
return null
}

private fun checkTorUsage() {
val usageStatsManager = getSystemService(Context.USAGE_STATS_SERVICE) as UsageStatsManager
val currentTime = System.currentTimeMillis()
val usageEvents = usageStatsManager.queryEvents(currentTime - checkInterval, currentTime)

while (usageEvents.hasNextEvent()) {
val event = UsageEvents.Event()
usageEvents.getNextEvent(event)

if (event.eventType == UsageEvents.Event.ACTIVITY_RESUMED) {
val packageName = event.packageName
if (packageName.contains("torproject") || packageName.contains("torbrowser")) {
val dpm = getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
val adminComponent = ComponentName(this, MyDeviceAdminReceiver::class.java)

dpm.setUninstallBlocked(adminComponent, packageName, true)
dpm.setPackagesSuspended(
adminComponent, arrayOf<String>(packageName), true
)
}
}
}
}
}

0 comments on commit 2c5b094

Please sign in to comment.