mirror of
https://github.com/kalarina-bit/Sparkledns.git
synced 2026-11-22 07:36:09 +00:00
- When the tunnel stops working, a notification says that DNS is no longer encrypted (new "Protection alerts" channel). - The service no longer brings up a tunnel for a server address that is not a valid HTTPS URL (always-on VPN or a restart by the system), and says so in a notification. - With "Block connections without VPN" on, a notification explains that the DNS server cannot be reached. - The tunnel is closed only after its reader thread has left. - Turning protection off from the quick settings tile needs an unlocked phone.
104 lines
3.6 KiB
Kotlin
104 lines
3.6 KiB
Kotlin
package cc.skysparkle.sparkledns
|
|
|
|
import android.annotation.SuppressLint
|
|
import android.app.PendingIntent
|
|
import android.content.ComponentName
|
|
import android.content.Context
|
|
import android.content.Intent
|
|
import android.net.VpnService
|
|
import android.os.Build
|
|
import android.service.quicksettings.Tile
|
|
import android.service.quicksettings.TileService
|
|
import android.util.Log
|
|
import kotlinx.coroutines.CoroutineScope
|
|
import kotlinx.coroutines.Dispatchers
|
|
import kotlinx.coroutines.MainScope
|
|
import kotlinx.coroutines.cancel
|
|
import kotlinx.coroutines.launch
|
|
import kotlinx.coroutines.withContext
|
|
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
|
|
|
class DnsTileService : TileService() {
|
|
|
|
companion object {
|
|
fun refresh(context: Context) {
|
|
runCatching {
|
|
TileService.requestListeningState(context, ComponentName(context, DnsTileService::class.java))
|
|
}
|
|
}
|
|
}
|
|
|
|
private var listening: CoroutineScope? = null
|
|
|
|
// Follows the VPN state while the panel is open, so the tile flips as soon as the tunnel is up
|
|
override fun onStartListening() {
|
|
listening?.cancel()
|
|
listening = MainScope().also { scope ->
|
|
scope.launch {
|
|
// The tile can start a fresh process: read settings from storage off the main thread,
|
|
// which also leaves them in memory for onClick
|
|
val prefs = withContext(Dispatchers.IO) { Prefs(applicationContext).also { it.providerId } }
|
|
DohVpnService.active.collect { updateTile(prefs) }
|
|
}
|
|
}
|
|
}
|
|
|
|
override fun onStopListening() {
|
|
listening?.cancel()
|
|
listening = null
|
|
}
|
|
|
|
override fun onClick() {
|
|
val running = DohVpnService.active.value != null
|
|
if (!running) {
|
|
val provider = Prefs(this).resolve()
|
|
val urlValid = provider.url.toHttpUrlOrNull()?.isHttps == true
|
|
// VPN consent or a broken custom URL can only be handled in the app
|
|
if (VpnService.prepare(this) != null || !urlValid) {
|
|
openApp()
|
|
return
|
|
}
|
|
}
|
|
// Turning protection off needs an unlocked phone, like other security toggles
|
|
if (running && isLocked) {
|
|
unlockAndRun { toggle(running) }
|
|
return
|
|
}
|
|
toggle(running)
|
|
}
|
|
|
|
private fun toggle(running: Boolean) {
|
|
val action = if (running) DohVpnService.ACTION_STOP else DohVpnService.ACTION_START
|
|
try {
|
|
startService(Intent(this, DohVpnService::class.java).setAction(action))
|
|
} catch (e: Exception) {
|
|
Log.w("SparkleDNS", "Tile could not reach the service", e)
|
|
openApp()
|
|
}
|
|
}
|
|
|
|
private fun updateTile(prefs: Prefs) {
|
|
val tile = qsTile ?: return
|
|
val active = DohVpnService.active.value
|
|
tile.state = if (active != null) Tile.STATE_ACTIVE else Tile.STATE_INACTIVE
|
|
tile.label = getString(R.string.app_name)
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
|
tile.subtitle = (active ?: prefs.resolve()).displayName(this)
|
|
}
|
|
tile.updateTile()
|
|
}
|
|
|
|
@SuppressLint("StartActivityAndCollapseDeprecated")
|
|
private fun openApp() {
|
|
val intent = Intent(this, MainActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
|
|
startActivityAndCollapse(
|
|
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)
|
|
)
|
|
} else {
|
|
@Suppress("DEPRECATION")
|
|
startActivityAndCollapse(intent)
|
|
}
|
|
}
|
|
}
|