Add system HTTP proxy support to libsignal-net.

Co-authored-by: Cody Henthorne <cody@signal.org>
This commit is contained in:
andrew-signal
2025-04-11 18:58:07 -05:00
committed by Cody Henthorne
parent 8e880fe117
commit 74c6e76808
8 changed files with 101 additions and 18 deletions

View File

@@ -4,6 +4,7 @@ import android.app.Application
import android.app.Service
import android.content.Context
import android.content.Intent
import android.net.ProxyInfo
import android.os.IBinder
import androidx.annotation.VisibleForTesting
import androidx.core.app.NotificationCompat
@@ -86,16 +87,29 @@ class IncomingMessageObserver(private val context: Application, private val auth
private val lock: ReentrantLock = ReentrantLock()
private val connectionNecessarySemaphore = Semaphore(0)
private val networkConnectionListener = NetworkConnectionListener(context) { isNetworkUnavailable ->
lock.withLock {
AppDependencies.libsignalNetwork.onNetworkChange()
if (isNetworkUnavailable()) {
Log.w(TAG, "Lost network connection. Resetting the drained state.")
decryptionDrained = false
private var previousProxyInfo: ProxyInfo? = null
private val networkConnectionListener = NetworkConnectionListener(
context,
{ isNetworkUnavailable ->
lock.withLock {
AppDependencies.libsignalNetwork.onNetworkChange()
if (isNetworkUnavailable()) {
Log.w(TAG, "Lost network connection. Resetting the drained state.")
decryptionDrained = false
}
connectionNecessarySemaphore.release()
}
connectionNecessarySemaphore.release()
},
{ proxyInfo ->
if (proxyInfo != previousProxyInfo) {
val networkReset = AppDependencies.onSystemHttpProxyChange(proxyInfo?.host, proxyInfo?.port)
if (networkReset) {
Log.i(TAG, "System proxy configuration changed, network reset.")
}
}
previousProxyInfo = proxyInfo
}
}
)
private val messageContentProcessor = MessageContentProcessor(context)

View File

@@ -10,7 +10,9 @@ import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.ConnectivityManager
import android.net.LinkProperties
import android.net.Network
import android.net.ProxyInfo
import android.os.Build
import org.signal.core.util.logging.Log
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint
@@ -24,7 +26,7 @@ import org.thoughtcrime.securesms.util.ServiceUtil
* API 28+ only runs on lost networks, so it provides a conditional that's always true because that is guaranteed by the call site.
* Earlier versions use [NetworkConstraint.isMet] to query the current network state upon receiving the broadcast.
*/
class NetworkConnectionListener(private val context: Context, private val onNetworkLost: (() -> Boolean) -> Unit) {
class NetworkConnectionListener(private val context: Context, private val onNetworkLost: (() -> Boolean) -> Unit, private val onProxySettingsChanged: ((ProxyInfo?) -> Unit)) {
companion object {
private val TAG = Log.tag(NetworkConnectionListener::class.java)
}
@@ -55,6 +57,12 @@ class NetworkConnectionListener(private val context: Context, private val onNetw
Log.d(TAG, "ConnectivityManager.NetworkCallback onLost()")
onNetworkLost { true }
}
override fun onLinkPropertiesChanged(network: Network, linkProperties: LinkProperties) {
super.onLinkPropertiesChanged(network, linkProperties)
Log.d(TAG, "ConnectivityManager.NetworkCallback onLinkPropertiesChanged()")
onProxySettingsChanged(linkProperties.httpProxy)
}
}
private val connectionReceiver = object : BroadcastReceiver() {