Commit 8aa2c951 authored by Daniel Wolf's avatar Daniel Wolf
Browse files

Move cronet to Nebulo instead of the tunnel library, prepare to remove it in a build flavor

parent 52d9d079
......@@ -148,10 +148,12 @@ dependencies {
implementation 'com.frostnerd.utilskt:lifecycle:1.2.2'
implementation 'com.frostnerd.utilskt:preferences:1.5.31' // https://git.frostnerd.com/AndroidUtils/preferenceskt
implementation 'com.frostnerd.utilskt:navigationdraweractivity:1.4.3' // https://git.frostnerd.com/AndroidUtils/navigationdraweractivity
implementation 'com.frostnerd.utilskt:encrypteddnstunnelproxy:2.1.30' // https://git.frostnerd.com/AndroidUtils/encrypteddnstunnelproxy
implementation 'com.frostnerd.utilskt:encrypteddnstunnelproxy:2.1.31' // https://git.frostnerd.com/AndroidUtils/encrypteddnstunnelproxy
implementation 'com.frostnerd.utilskt:general:1.0.26' // https://git.frostnerd.com/AndroidUtils/generalkt
implementation 'com.frostnerd.utilskt:adapters:1.3.5' // https://git.frostnerd.com/AndroidUtils/Adapters
implementation 'com.google.android.gms:play-services-cronet:17.0.0'
implementation 'androidx.work:work-runtime:2.5.0'
implementation 'androidx.appcompat:appcompat:1.3.0-rc01'
implementation "androidx.preference:preference:1.1.1"
......
......@@ -16,13 +16,12 @@ import androidx.annotation.ColorInt
import androidx.appcompat.app.AlertDialog
import com.frostnerd.dnstunnelproxy.KnownDnsServers
import com.frostnerd.encrypteddnstunnelproxy.AbstractHttpsDNSHandle
import com.frostnerd.encrypteddnstunnelproxy.QuicEngine
import com.frostnerd.encrypteddnstunnelproxy.quic.AbstractQuicDnsHandle
import com.frostnerd.encrypteddnstunnelproxy.quic.QuicUpstreamAddress
import com.frostnerd.encrypteddnstunnelproxy.tls.AbstractTLSDnsHandle
import com.google.android.gms.net.CronetProviderInstaller
import kotlinx.android.synthetic.main.dialog_privacypolicy.view.*
import okhttp3.internal.toHexString
import org.chromium.net.CronetEngine
import java.util.*
......@@ -135,23 +134,8 @@ fun loadKnownDNSServers() {
KnownDnsServers
}
fun createQuicCronetEngineIfInstalled(context: Context, vararg addresses: QuicUpstreamAddress): CronetEngine? {
return if(CronetProviderInstaller.isInstalled()) try {
AbstractQuicDnsHandle.createEngine(context, *addresses)
} catch (ex:Throwable) { null }
else null
}
fun createHttpCronetEngineIfInstalled(context: Context): CronetEngine? {
return if(CronetProviderInstaller.isInstalled()) try {
val cacheDir = context.cacheDir.resolve("cronetcachehttp")
cacheDir.mkdir()
return CronetEngine.Builder(context)
.enableHttp2(true)
.enableBrotli(true)
.enableQuic(true)
.setStoragePath(cacheDir.path)
.build()
} catch (ex:Throwable) { null }
else null
fun createQuicEngineIfInstalled(context: Context, quicOnly:Boolean, vararg addresses: QuicUpstreamAddress): QuicEngine? {
return if (QuicEngineImpl.providerInstalled) {
QuicEngineImpl(context, quicOnly, *addresses)
} else null
}
\ No newline at end of file
package com.frostnerd.smokescreen
import android.content.Context
import com.frostnerd.encrypteddnstunnelproxy.QuicEngine
import com.frostnerd.encrypteddnstunnelproxy.quic.QuicUpstreamAddress
import com.google.android.gms.net.CronetProviderInstaller
import org.chromium.net.CronetEngine
import java.net.URL
import java.net.URLConnection
/*
* Copyright (C) 2021 Daniel Wolf (Ch4t4r)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* You can contact the developer at daniel.wolf@frostnerd.com.
*/
class QuicEngineImpl(context: Context, private val quicOnly:Boolean, vararg addresses:QuicUpstreamAddress):QuicEngine(context, *addresses) {
companion object {
var providerInstalled:Boolean = false
private set
fun installNetworkEngine(context: Context, onSuccess:(() -> Unit)? = null) {
try {
if(CronetProviderInstaller.isInstalled()) {
providerInstalled = true
onSuccess?.invoke()
} else {
CronetProviderInstaller.installProvider(context).addOnCompleteListener {
providerInstalled = true
onSuccess?.invoke()
}
}
} catch (ex:Throwable) {
ex.printStackTrace()
}
}
}
private var engine: CronetEngine? = null
init {
installNetworkEngine(context) {
engine = createEngine(context, *addresses)
}
}
private fun createEngine(context: Context, vararg addresses:QuicUpstreamAddress): CronetEngine {
val cacheDir = context.cacheDir.resolve("cronetcache")
cacheDir.mkdir()
return CronetEngine.Builder(context)
.apply {
addresses.forEach {
addQuicHint(it.host, it.port, 443)
}
}.enableHttp2(!quicOnly)
.enableBrotli(true)
.enableQuic(true)
.setStoragePath(cacheDir.path)
.build()
}
override fun openConnection(url: URL): URLConnection {
return engine!!.openConnection(url)
}
override fun shutdown() {
engine?.shutdown()
}
fun usable():Boolean {
return providerInstalled && engine != null
}
}
\ No newline at end of file
......@@ -144,7 +144,7 @@ class SmokeScreen : Application() {
log("Application created.")
handleFallbackDns()
loadKnownDNSServers()
AbstractQuicDnsHandle.installProvider(this) {}
QuicEngineImpl.installNetworkEngine(this)
}
private fun handleFallbackDns() {
......
......@@ -68,7 +68,8 @@ class NebuloAppIntro:AppIntro() {
scope.launch {
for(server in chunks[i]) {
if(!isActive) break
val testResult = DnsSpeedTest(this@NebuloAppIntro, server, log = {}, cronetEngine = createQuicCronetEngineIfInstalled(this@NebuloAppIntro)).runTest(3)
val testResult = DnsSpeedTest(this@NebuloAppIntro, server, log = {},
quicOnlyEngine = createQuicEngineIfInstalled(this@NebuloAppIntro, true)).runTest(3)
synchronized(speedTestResults) {
speedTestResults[server] = testResult
}
......
......@@ -195,8 +195,8 @@ class SpeedTestActivity : BaseActivity() {
if(wasStartedBefore) prepareList()
testJob = launchWithLifecycle {
prepareListJob?.join()
val engine = if(BuildConfig.SHOW_DOQ) createQuicCronetEngineIfInstalled(this@SpeedTestActivity) else null
val httpsEngine = createHttpCronetEngineIfInstalled(this@SpeedTestActivity)
val engine = if(BuildConfig.SHOW_DOQ) createQuicEngineIfInstalled(this@SpeedTestActivity, true) else null
val httpsEngine = createQuicEngineIfInstalled(this@SpeedTestActivity, false)
testRunning = true
wasStartedBefore = true
val testsLeft = testResults!!.shuffled()
......
......@@ -377,7 +377,7 @@ class MainFragment : Fragment() {
if(!context.getPreferences().compareDnsSpeedsAtLaunch) return
launchWithLifecycle {
val httpsEngine = createHttpCronetEngineIfInstalled(context)
val httpsEngine = createQuicEngineIfInstalled(context, false)
val fastServerAverage = (AbstractHttpsDNSHandle.suspendUntilKnownServersArePopulated(
1500
) {
......@@ -389,8 +389,8 @@ class MainFragment : Fragment() {
context,
it as DnsServerInformation<*>,
log = {},
cronetEngine = null, /* We do not need quic here*/
httpsCronetEngine = httpsEngine
quicOnlyEngine = null, /* We do not need quic here*/
httpsQuicEngine = httpsEngine
).runTest(4)
}.takeIf {
it.isNotEmpty()
......
......@@ -1246,7 +1246,7 @@ class DnsVpnService : VpnService(), Runnable, CoroutineScope {
var forwardingMode = VPNTunnelProxy.ForwardingMode.MIXED
val httpCronetEngine = createHttpCronetEngineIfInstalled(this)
val httpQuicEngine = createQuicEngineIfInstalled(this, false)
serverConfig.httpsConfiguration?.forEach {
forwardingMode = VPNTunnelProxy.ForwardingMode.NO_POLLABLE
val addresses = serverConfig.getIpAddressesFor(ipv4Enabled, ipv6Enabled, it)
......@@ -1264,7 +1264,7 @@ class DnsVpnService : VpnService(), Runnable, CoroutineScope {
}
},
mapQueryRefusedToHostBlock = getPreferences().mapQueryRefusedToHostBlock,
cronetEngine = httpCronetEngine
quicEngine = httpQuicEngine
)
handle.ipv4Enabled = ipv4Enabled
handle.ipv6Enabled = ipv6Enabled
......@@ -1294,8 +1294,8 @@ class DnsVpnService : VpnService(), Runnable, CoroutineScope {
if (defaultHandle == null) defaultHandle = handle
else handles.add(handle)
}
val cronetEngine = serverConfig.quicConfiguration?.let { createQuicCronetEngineIfInstalled(this, *it.toTypedArray()) }
if(serverConfig.quicConfiguration != null && cronetEngine == null) {
val quicEngine = serverConfig.quicConfiguration?.let { createQuicEngineIfInstalled(this, true, *it.toTypedArray()) }
if(serverConfig.quicConfiguration != null && quicEngine == null) {
showCronetErrorNotification()
destroy(true)
return
......@@ -1315,7 +1315,7 @@ class DnsVpnService : VpnService(), Runnable, CoroutineScope {
}
}
}, mapQueryRefusedToHostBlock = getPreferences().mapQueryRefusedToHostBlock,
cronetEngine!!
quicEngine!!
)
handle.ipv4Enabled = ipv4Enabled
handle.ipv6Enabled = ipv6Enabled
......
......@@ -3,9 +3,9 @@ package com.frostnerd.smokescreen.util.proxy
import com.frostnerd.dnstunnelproxy.AddressCreator
import com.frostnerd.dnstunnelproxy.UpstreamAddress
import com.frostnerd.encrypteddnstunnelproxy.AbstractHttpsDNSHandle
import com.frostnerd.encrypteddnstunnelproxy.QuicEngine
import com.frostnerd.encrypteddnstunnelproxy.ServerConfiguration
import com.frostnerd.vpntunnelproxy.FutureAnswer
import org.chromium.net.CronetEngine
import org.minidns.dnsmessage.DnsMessage
import java.net.InetAddress
......@@ -33,9 +33,9 @@ class ProxyHttpsHandler(
connectTimeout: Long,
val queryCountCallback: (() -> Unit)? = null,
val mapQueryRefusedToHostBlock:Boolean,
cronetEngine: CronetEngine? = null
quicEngine: QuicEngine? = null
) :
AbstractHttpsDNSHandle(serverConfigurations, connectTimeout, cronetEngine) {
AbstractHttpsDNSHandle(serverConfigurations, connectTimeout, quicEngine) {
override val handlesSpecificRequests: Boolean = ProxyBypassHandler.knownSearchDomains.isNotEmpty()
private val dummyUpstreamAddress = UpstreamAddress(AddressCreator.fromHostAddress("0.0.0.0"), 1)
......
......@@ -2,10 +2,10 @@ package com.frostnerd.smokescreen.util.proxy
import com.frostnerd.dnstunnelproxy.AddressCreator
import com.frostnerd.dnstunnelproxy.UpstreamAddress
import com.frostnerd.encrypteddnstunnelproxy.QuicEngine
import com.frostnerd.encrypteddnstunnelproxy.quic.AbstractQuicDnsHandle
import com.frostnerd.encrypteddnstunnelproxy.quic.QuicUpstreamAddress
import com.frostnerd.vpntunnelproxy.FutureAnswer
import org.chromium.net.CronetEngine
import org.minidns.dnsmessage.DnsMessage
import org.minidns.record.A
import org.minidns.record.AAAA
......@@ -36,7 +36,7 @@ class ProxyQuicHandler(
connectTimeout: Int,
val queryCountCallback: (() -> Unit)? = null,
val mapQueryRefusedToHostBlock:Boolean,
engine:CronetEngine
engine: QuicEngine
):AbstractQuicDnsHandle(upstreamAddresses, connectTimeout, engine) {
override val handlesSpecificRequests: Boolean = ProxyBypassHandler.knownSearchDomains.isNotEmpty()
private val dummyUpstreamAddress = UpstreamAddress(AddressCreator.fromHostAddress("0.0.0.0"), 1)
......
......@@ -5,18 +5,19 @@ import androidx.annotation.IntRange
import com.frostnerd.dnstunnelproxy.DnsServerInformation
import com.frostnerd.dnstunnelproxy.UpstreamAddress
import com.frostnerd.encrypteddnstunnelproxy.HttpsDnsServerInformation
import com.frostnerd.encrypteddnstunnelproxy.QuicEngine
import com.frostnerd.encrypteddnstunnelproxy.ServerConfiguration
import com.frostnerd.encrypteddnstunnelproxy.closeSilently
import com.frostnerd.encrypteddnstunnelproxy.quic.QuicUpstreamAddress
import com.frostnerd.encrypteddnstunnelproxy.tls.TLSUpstreamAddress
import com.frostnerd.smokescreen.createHttpCronetEngineIfInstalled
import com.frostnerd.smokescreen.QuicEngineImpl
import com.frostnerd.smokescreen.createQuicEngineIfInstalled
import com.frostnerd.smokescreen.type
import com.frostnerd.smokescreen.util.ServerType
import okhttp3.*
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody
import org.chromium.net.CronetEngine
import org.minidns.dnsmessage.DnsMessage
import org.minidns.dnsmessage.Question
import org.minidns.record.Record
......@@ -50,8 +51,8 @@ class DnsSpeedTest(context:Context,
val server: DnsServerInformation<*>,
private val connectTimeout: Int = 2500,
private val readTimeout:Int = 1500,
private val cronetEngine: CronetEngine?,
httpsCronetEngine: CronetEngine? = null,
private val quicOnlyEngine: QuicEngine?,
httpsQuicEngine: QuicEngine? = null,
val log:(line:String) -> Unit) {
private val httpClient by lazy(LazyThreadSafetyMode.NONE) {
OkHttpClient.Builder()
......@@ -65,8 +66,8 @@ class DnsSpeedTest(context:Context,
it.urlCreator.address
})
}
private val _httpsCronetEngine by lazy(LazyThreadSafetyMode.NONE) {
httpsCronetEngine ?: createHttpCronetEngineIfInstalled(context)
private val _quicEngine by lazy(LazyThreadSafetyMode.NONE) {
httpsQuicEngine ?: createQuicEngineIfInstalled(context, false)
}
companion object {
......@@ -94,7 +95,7 @@ class DnsSpeedTest(context:Context,
ServerType.DOH -> {
server as HttpsDnsServerInformation
server.serverConfigurations.values.forEach {
latencies += if(_httpsCronetEngine == null) {
latencies += if(_quicEngine == null) {
if(firstPass) testHttps(it)
testHttps(it) ?: 0
} else {
......@@ -107,7 +108,7 @@ class DnsSpeedTest(context:Context,
ServerType.DOQ -> {
@Suppress("UNCHECKED_CAST")
(server as DnsServerInformation<QuicUpstreamAddress>).servers.forEach {
if(cronetEngine != null) {
if(quicOnlyEngine != null) {
if(firstPass) testQuic(it.address)
latencies += testQuic(it.address) ?: 0
}
......@@ -117,7 +118,7 @@ class DnsSpeedTest(context:Context,
firstPass = false
}
if(server.type == ServerType.DOH) {
if(_httpsCronetEngine == null) httpClient.connectionPool.evictAll()
if(_quicEngine == null) httpClient.connectionPool.evictAll()
}
return when (strategy) {
Strategy.BEST_CASE -> {
......@@ -213,7 +214,7 @@ class DnsSpeedTest(context:Context,
var connection:HttpURLConnection? = null
var wasEstablished = false
try {
connection = _httpsCronetEngine!!.openConnection(url) as HttpURLConnection
connection = _quicEngine!!.openConnection(url) as HttpURLConnection
connection.connectTimeout = connectTimeout
if(config.requestHasBody) {
val body = config.bodyCreator?.createBody(msg, config.urlCreator.address) ?: return null
......@@ -307,7 +308,7 @@ class DnsSpeedTest(context:Context,
val msg = createTestDnsPacket()
try {
val start = System.currentTimeMillis()
connection = cronetEngine!!.openConnection(url) as HttpURLConnection
connection = quicOnlyEngine!!.openConnection(url) as HttpURLConnection
connection.connectTimeout = connectTimeout
connection.requestMethod = "POST"
connection.setRequestProperty("Content-Type", "application/dns-message")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment