Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android): detect network and dns changes and send them to connlib #4163

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions kotlin/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@ object ConnlibSession {
): Long

external fun disconnect(connlibSession: Long): Boolean

external fun setDns(
connlibSession: Long,
dnsList: String,
): Boolean

external fun reconnect(connlibSession: Long): Boolean
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* Licensed under Apache 2.0 (C) 2024 Firezone, Inc. */
import android.net.ConnectivityManager
import android.net.LinkProperties
import android.net.Network
import com.google.gson.Gson
import dev.firezone.android.tunnel.ConnlibSession

class NetworkMonitor(private val connlibSessionPtr: Long) : ConnectivityManager.NetworkCallback() {
override fun onLinkPropertiesChanged(
network: Network,
linkProperties: LinkProperties,
) {
ConnlibSession.setDns(connlibSessionPtr, Gson().toJson(linkProperties.dnsServers))
ConnlibSession.reconnect(connlibSessionPtr)
super.onLinkPropertiesChanged(network, linkProperties)
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
/* Licensed under Apache 2.0 (C) 2024 Firezone, Inc. */
package dev.firezone.android.tunnel

import NetworkMonitor
import android.app.ActivityManager
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.net.NetworkRequest
import android.net.VpnService
import android.os.Binder
import android.os.Build
Expand All @@ -27,7 +31,6 @@ import dev.firezone.android.core.presentation.MainActivity
import dev.firezone.android.tunnel.callback.ConnlibCallback
import dev.firezone.android.tunnel.model.Cidr
import dev.firezone.android.tunnel.model.Resource
import dev.firezone.android.tunnel.util.DnsServersDetector
import java.nio.file.Files
import java.nio.file.Paths
import java.util.UUID
Expand All @@ -52,6 +55,7 @@ class TunnelService : VpnService() {
private var connlibSessionPtr: Long? = null
private var _tunnelResources: List<Resource> = emptyList()
private var _tunnelState: State = State.DOWN
private var networkCallback: NetworkMonitor? = null

var startedByUser: Boolean = false

Expand Down Expand Up @@ -136,16 +140,6 @@ class TunnelService : VpnService() {
return buildVpnService()
}

override fun getSystemDefaultResolvers(): Array<ByteArray> {
val found = DnsServersDetector(this@TunnelService).servers
Log.d(TAG, "getSystemDefaultResolvers: $found")
Firebase.crashlytics.log("getSystemDefaultResolvers: $found")

return found.map {
it.address
}.toTypedArray()
}

// Unexpected disconnect, most likely a 401. Clear the token and initiate a stop of the
// service.
override fun onDisconnect(error: String): Boolean {
Expand Down Expand Up @@ -188,7 +182,7 @@ class TunnelService : VpnService() {
Log.d(TAG, "disconnect")

// Connlib should call onDisconnect() when it's done, with no error.
connlibSessionPtr!!.let {
connlibSessionPtr?.let {
ConnlibSession.disconnect(it)
}

Expand All @@ -198,6 +192,8 @@ class TunnelService : VpnService() {
private fun shutdown() {
Log.d(TAG, "shutdown")

stopNetworkMonitoring()

connlibSessionPtr = null
stopSelf()
tunnelState = State.DOWN
Expand All @@ -223,6 +219,29 @@ class TunnelService : VpnService() {
logFilter = config.logFilter,
callback = callback,
)

startNetworkMonitoring()
}
}

private fun startNetworkMonitoring() {
networkCallback = NetworkMonitor(connlibSessionPtr!!)

val networkRequest =
NetworkRequest.Builder().addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)
.build()
val connectivityManager =
getSystemService(ConnectivityManager::class.java) as ConnectivityManager
connectivityManager.requestNetwork(networkRequest, networkCallback!!)
}

private fun stopNetworkMonitoring() {
networkCallback?.let {
val connectivityManager =
getSystemService(ConnectivityManager::class.java) as ConnectivityManager
connectivityManager.unregisterNetworkCallback(it)

networkCallback = null
}
}

Expand Down Expand Up @@ -312,7 +331,10 @@ class TunnelService : VpnService() {
addAddress(tunnelIpv6Address!!, 128)

updateAllowedDisallowedApplications("allowedApplications", ::addAllowedApplication)
updateAllowedDisallowedApplications("disallowedApplications", ::addDisallowedApplication)
updateAllowedDisallowedApplications(
"disallowedApplications",
::addDisallowedApplication,
)

setSession(SESSION_NAME)
setMtu(MTU)
Expand Down Expand Up @@ -354,14 +376,10 @@ class TunnelService : VpnService() {

val notificationBuilder = NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
val notification =
notificationBuilder.setOngoing(true)
.setSmallIcon(R.drawable.ic_firezone_logo)
.setContentTitle(NOTIFICATION_TITLE)
.setContentText(message)
notificationBuilder.setOngoing(true).setSmallIcon(R.drawable.ic_firezone_logo)
.setContentTitle(NOTIFICATION_TITLE).setContentText(message)
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.setContentIntent(configIntent())
.build()
.setCategory(Notification.CATEGORY_SERVICE).setContentIntent(configIntent()).build()

startForeground(STATUS_NOTIFICATION_ID, notification)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,5 @@ interface ConnlibCallback {
// The JNI doesn't support nullable types, so we need two method signatures
fun onDisconnect(error: String): Boolean

fun getSystemDefaultResolvers(): Array<ByteArray>

fun protectFileDescriptor(fileDescriptor: Int)
}

This file was deleted.