-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added service to detect and block tor apps.
- Loading branch information
1 parent
f0136f7
commit 2c5b094
Showing
7 changed files
with
78 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
app/src/main/java/com/cgprograms/dnslock/TorMonitorService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} |