mirror of
https://github.com/kalarina-bit/Sparkledns.git
synced 2026-11-22 15:46:09 +00:00
Tell the user when protection stops and check the server address in the service
- 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.
This commit is contained in:
@@ -59,6 +59,15 @@ class DnsTileService : TileService() {
|
|||||||
return
|
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
|
val action = if (running) DohVpnService.ACTION_STOP else DohVpnService.ACTION_START
|
||||||
try {
|
try {
|
||||||
startService(Intent(this, DohVpnService::class.java).setAction(action))
|
startService(Intent(this, DohVpnService::class.java).setAction(action))
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import android.system.StructPollfd
|
|||||||
import android.util.Log
|
import android.util.Log
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
|
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
||||||
import java.io.FileInputStream
|
import java.io.FileInputStream
|
||||||
import java.io.FileOutputStream
|
import java.io.FileOutputStream
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
@@ -34,7 +35,11 @@ private const val WORKERS = 16
|
|||||||
private const val TAG = "SparkleDNS"
|
private const val TAG = "SparkleDNS"
|
||||||
private const val CHANNEL_ID = "vpn_status"
|
private const val CHANNEL_ID = "vpn_status"
|
||||||
private const val NOTIFICATION_ID = 1
|
private const val NOTIFICATION_ID = 1
|
||||||
|
// Separate channel with normal importance: losing protection must not go unnoticed
|
||||||
|
private const val ALERT_CHANNEL_ID = "vpn_alerts"
|
||||||
|
private const val ALERT_NOTIFICATION_ID = 2
|
||||||
private const val MAX_BAD_PACKETS = 100
|
private const val MAX_BAD_PACKETS = 100
|
||||||
|
private const val READER_JOIN_MS = 1000L
|
||||||
|
|
||||||
// Android's resolver retries after 5 s, so an older lookup no longer has anyone waiting for it
|
// Android's resolver retries after 5 s, so an older lookup no longer has anyone waiting for it
|
||||||
private const val QUEUE_TIMEOUT_MS = 5000L
|
private const val QUEUE_TIMEOUT_MS = 5000L
|
||||||
@@ -96,6 +101,16 @@ class DohVpnService : VpnService() {
|
|||||||
val provider = prefs.resolve()
|
val provider = prefs.resolve()
|
||||||
val excluded = prefs.excludedApps
|
val excluded = prefs.excludedApps
|
||||||
|
|
||||||
|
// The app checks the address before connecting, but always-on VPN and a restart by the
|
||||||
|
// system start the service directly. A tunnel with a broken URL would fail every lookup.
|
||||||
|
if (provider.url.toHttpUrlOrNull()?.isHttps != true) {
|
||||||
|
Log.e(TAG, "Server address is not a valid HTTPS URL, not starting")
|
||||||
|
stopSession()
|
||||||
|
showAlert(R.string.alert_bad_url_title, R.string.alert_bad_url_text)
|
||||||
|
stopSelf()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
val builder = Builder()
|
val builder = Builder()
|
||||||
.setSession(provider.displayName(this))
|
.setSession(provider.displayName(this))
|
||||||
.addAddress(TUN_ADDRESS, 32)
|
.addAddress(TUN_ADDRESS, 32)
|
||||||
@@ -138,6 +153,14 @@ class DohVpnService : VpnService() {
|
|||||||
_activeExcluded.value = excluded
|
_activeExcluded.value = excluded
|
||||||
showNotification(provider)
|
showNotification(provider)
|
||||||
DnsTileService.refresh(this)
|
DnsTileService.refresh(this)
|
||||||
|
getSystemService(NotificationManager::class.java)?.cancel(ALERT_NOTIFICATION_ID)
|
||||||
|
|
||||||
|
// With "Block connections without VPN" the app, excluded from its own tunnel, cannot reach
|
||||||
|
// the DoH server, so every lookup would fail
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && isLockdownEnabled) {
|
||||||
|
Log.w(TAG, "Lockdown mode is on, the DoH server is not reachable")
|
||||||
|
showAlert(R.string.alert_lockdown_title, R.string.alert_lockdown_text)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun stopSession() {
|
private fun stopSession() {
|
||||||
@@ -157,10 +180,34 @@ class DohVpnService : VpnService() {
|
|||||||
if (session !== failed) return@post
|
if (session !== failed) return@post
|
||||||
Log.e(TAG, "Tunnel stopped working, disconnecting")
|
Log.e(TAG, "Tunnel stopped working, disconnecting")
|
||||||
stopSession()
|
stopSession()
|
||||||
|
// DNS goes unencrypted from here on; the user has to know
|
||||||
|
showAlert(R.string.alert_stopped_title, R.string.alert_stopped_text)
|
||||||
stopSelf()
|
stopSelf()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun showAlert(titleRes: Int, textRes: Int) {
|
||||||
|
val manager = getSystemService(NotificationManager::class.java) ?: return
|
||||||
|
manager.createNotificationChannel(
|
||||||
|
NotificationChannel(ALERT_CHANNEL_ID, getString(R.string.alert_channel), NotificationManager.IMPORTANCE_DEFAULT)
|
||||||
|
)
|
||||||
|
val openApp = PendingIntent.getActivity(
|
||||||
|
this, 2,
|
||||||
|
Intent(this, MainActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),
|
||||||
|
PendingIntent.FLAG_IMMUTABLE,
|
||||||
|
)
|
||||||
|
val text = getString(textRes)
|
||||||
|
val notification = Notification.Builder(this, ALERT_CHANNEL_ID)
|
||||||
|
.setSmallIcon(R.drawable.ic_notification)
|
||||||
|
.setContentTitle(getString(titleRes))
|
||||||
|
.setContentText(text)
|
||||||
|
.setStyle(Notification.BigTextStyle().bigText(text))
|
||||||
|
.setContentIntent(openApp)
|
||||||
|
.setAutoCancel(true)
|
||||||
|
.build()
|
||||||
|
runCatching { manager.notify(ALERT_NOTIFICATION_ID, notification) }
|
||||||
|
}
|
||||||
|
|
||||||
private fun showNotification(provider: DohProvider) {
|
private fun showNotification(provider: DohProvider) {
|
||||||
val manager = getSystemService(NotificationManager::class.java) ?: return
|
val manager = getSystemService(NotificationManager::class.java) ?: return
|
||||||
manager.createNotificationChannel(
|
manager.createNotificationChannel(
|
||||||
@@ -196,7 +243,7 @@ class DohVpnService : VpnService() {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
|
||||||
startForeground(NOTIFICATION_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE)
|
startForeground(NOTIFICATION_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED)
|
||||||
} else {
|
} else {
|
||||||
startForeground(NOTIFICATION_ID, notification)
|
startForeground(NOTIFICATION_ID, notification)
|
||||||
}
|
}
|
||||||
@@ -229,6 +276,9 @@ private class Session(
|
|||||||
if (!alive.getAndSet(false)) return
|
if (!alive.getAndSet(false)) return
|
||||||
pool.shutdownNow()
|
pool.shutdownNow()
|
||||||
runCatching { wakeup[1].close() }
|
runCatching { wakeup[1].close() }
|
||||||
|
// The pipe wakes the reader at once; let it leave before its descriptor is closed and
|
||||||
|
// possibly reused by something else
|
||||||
|
if (Thread.currentThread() !== reader) runCatching { reader.join(READER_JOIN_MS) }
|
||||||
runCatching { tun.close() }
|
runCatching { tun.close() }
|
||||||
closer.execute {
|
closer.execute {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -131,4 +131,11 @@
|
|||||||
<string name="turn_on">Einschalten</string>
|
<string name="turn_on">Einschalten</string>
|
||||||
<string name="downloading">Wird geladen…</string>
|
<string name="downloading">Wird geladen…</string>
|
||||||
<string name="change_server">Server wechseln</string>
|
<string name="change_server">Server wechseln</string>
|
||||||
|
<string name="alert_channel">Schutzwarnungen</string>
|
||||||
|
<string name="alert_stopped_title">DNS-Schutz beendet</string>
|
||||||
|
<string name="alert_stopped_text">Der Tunnel funktioniert nicht mehr, DNS ist nicht mehr verschlüsselt. Öffne SparkleDNS, um dich erneut zu verbinden.</string>
|
||||||
|
<string name="alert_bad_url_title">DNS-Schutz nicht gestartet</string>
|
||||||
|
<string name="alert_bad_url_text">Die Serveradresse ist keine gültige HTTPS-URL. Öffne SparkleDNS und prüfe den Server.</string>
|
||||||
|
<string name="alert_lockdown_title">DNS-Server nicht erreichbar</string>
|
||||||
|
<string name="alert_lockdown_text">„Verbindungen ohne VPN blockieren“ ist aktiv, daher erreicht SparkleDNS den DNS-Server nicht und Anfragen schlagen fehl. Schalte es in den VPN-Einstellungen von Android aus.</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
@@ -131,4 +131,11 @@
|
|||||||
<string name="turn_on">オンにする</string>
|
<string name="turn_on">オンにする</string>
|
||||||
<string name="downloading">ダウンロード中…</string>
|
<string name="downloading">ダウンロード中…</string>
|
||||||
<string name="change_server">サーバーを変更</string>
|
<string name="change_server">サーバーを変更</string>
|
||||||
|
<string name="alert_channel">保護に関する警告</string>
|
||||||
|
<string name="alert_stopped_title">DNS 保護が停止しました</string>
|
||||||
|
<string name="alert_stopped_text">トンネルが動作しなくなったため、DNS は暗号化されていません。SparkleDNS を開いて再接続してください。</string>
|
||||||
|
<string name="alert_bad_url_title">DNS 保護を開始できません</string>
|
||||||
|
<string name="alert_bad_url_text">サーバーのアドレスが有効な HTTPS URL ではありません。SparkleDNS を開いてサーバーを確認してください。</string>
|
||||||
|
<string name="alert_lockdown_title">DNS サーバーに接続できません</string>
|
||||||
|
<string name="alert_lockdown_text">「VPN なしの接続をブロック」がオンのため、SparkleDNS は DNS サーバーに接続できず、名前解決に失敗します。Android の VPN 設定でオフにしてください。</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
@@ -131,4 +131,11 @@
|
|||||||
<string name="turn_on">Įjungti</string>
|
<string name="turn_on">Įjungti</string>
|
||||||
<string name="downloading">Atsisiunčiama…</string>
|
<string name="downloading">Atsisiunčiama…</string>
|
||||||
<string name="change_server">Keisti serverį</string>
|
<string name="change_server">Keisti serverį</string>
|
||||||
|
<string name="alert_channel">Apsaugos įspėjimai</string>
|
||||||
|
<string name="alert_stopped_title">DNS apsauga išjungta</string>
|
||||||
|
<string name="alert_stopped_text">Tunelis nustojo veikti, DNS nebešifruojamas. Atidarykite SparkleDNS, kad prisijungtumėte iš naujo.</string>
|
||||||
|
<string name="alert_bad_url_title">DNS apsauga nepaleista</string>
|
||||||
|
<string name="alert_bad_url_text">Serverio adresas nėra tinkamas HTTPS adresas. Atidarykite SparkleDNS ir patikrinkite serverį.</string>
|
||||||
|
<string name="alert_lockdown_title">DNS serveris nepasiekiamas</string>
|
||||||
|
<string name="alert_lockdown_text">Įjungta „Blokuoti ryšius be VPN“, todėl SparkleDNS negali pasiekti DNS serverio ir užklausos nepavyksta. Išjunkite tai Android VPN nustatymuose.</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
@@ -131,4 +131,11 @@
|
|||||||
<string name="turn_on">Включить</string>
|
<string name="turn_on">Включить</string>
|
||||||
<string name="downloading">Загрузка…</string>
|
<string name="downloading">Загрузка…</string>
|
||||||
<string name="change_server">Сменить сервер</string>
|
<string name="change_server">Сменить сервер</string>
|
||||||
|
<string name="alert_channel">Предупреждения о защите</string>
|
||||||
|
<string name="alert_stopped_title">Защита DNS отключена</string>
|
||||||
|
<string name="alert_stopped_text">Туннель перестал работать, DNS больше не шифруется. Откройте SparkleDNS, чтобы подключиться снова.</string>
|
||||||
|
<string name="alert_bad_url_title">Защита DNS не запущена</string>
|
||||||
|
<string name="alert_bad_url_text">Адрес сервера не является корректным HTTPS-адресом. Откройте SparkleDNS и проверьте сервер.</string>
|
||||||
|
<string name="alert_lockdown_title">DNS-сервер недоступен</string>
|
||||||
|
<string name="alert_lockdown_text">Включено «Блокировать соединения без VPN», поэтому SparkleDNS не может связаться с DNS-сервером и запросы не проходят. Отключите это в настройках VPN Android.</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
@@ -131,4 +131,11 @@
|
|||||||
<string name="turn_on">开启</string>
|
<string name="turn_on">开启</string>
|
||||||
<string name="downloading">正在下载…</string>
|
<string name="downloading">正在下载…</string>
|
||||||
<string name="change_server">更换服务器</string>
|
<string name="change_server">更换服务器</string>
|
||||||
|
<string name="alert_channel">保护警告</string>
|
||||||
|
<string name="alert_stopped_title">DNS 保护已停止</string>
|
||||||
|
<string name="alert_stopped_text">隧道已停止工作,DNS 不再加密。请打开 SparkleDNS 重新连接。</string>
|
||||||
|
<string name="alert_bad_url_title">DNS 保护未启动</string>
|
||||||
|
<string name="alert_bad_url_text">服务器地址不是有效的 HTTPS 网址。请打开 SparkleDNS 检查服务器。</string>
|
||||||
|
<string name="alert_lockdown_title">无法连接 DNS 服务器</string>
|
||||||
|
<string name="alert_lockdown_text">已开启“屏蔽未使用 VPN 的连接”,SparkleDNS 无法连接 DNS 服务器,解析会失败。请在 Android 的 VPN 设置中关闭该选项。</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
@@ -132,4 +132,11 @@
|
|||||||
<string name="turn_on">Turn on</string>
|
<string name="turn_on">Turn on</string>
|
||||||
<string name="downloading">Downloading…</string>
|
<string name="downloading">Downloading…</string>
|
||||||
<string name="change_server">Change server</string>
|
<string name="change_server">Change server</string>
|
||||||
|
<string name="alert_channel">Protection alerts</string>
|
||||||
|
<string name="alert_stopped_title">DNS protection stopped</string>
|
||||||
|
<string name="alert_stopped_text">The tunnel stopped working, so DNS is no longer encrypted. Open SparkleDNS to connect again.</string>
|
||||||
|
<string name="alert_bad_url_title">DNS protection not started</string>
|
||||||
|
<string name="alert_bad_url_text">The server address is not a valid HTTPS URL. Open SparkleDNS and check the server.</string>
|
||||||
|
<string name="alert_lockdown_title">DNS server unreachable</string>
|
||||||
|
<string name="alert_lockdown_text">\"Block connections without VPN\" is on, so SparkleDNS cannot reach the DNS server and lookups fail. Turn it off in Android VPN settings.</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
Reference in New Issue
Block a user