Advertisement
Guest User

Service builder

a guest
Jun 16th, 2025
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.38 KB | None | 0 0
  1.     class SecureVpnServiceBuilder {
  2.         val upstreamDnsServers = mutableListOf<InetAddress>()
  3.    
  4.         fun configure(
  5.             context: Context,
  6.             builder: Builder,
  7.         ): VpnService.Builder {
  8.             log("Configuring secure service")
  9.             val dnsServers = getDnsServers(context)
  10.             log("Received dns servers: $dnsServers")
  11.    
  12.             var format: String = EmptyString
  13.    
  14.             for (prefix in TEST_NET_PREFIX_LIST) {
  15.                 try {
  16.                     builder.addAddress(prefix + TEST_NET_POSTFIX, TEST_NET_PREFIX_LENGTH)
  17.                 } catch (e: IllegalArgumentException) {
  18.                     continue
  19.                 }
  20.                 format = "$prefix.%d"
  21.                 break
  22.             }
  23.    
  24.             if (format.isEmpty()) {
  25.                 log("Cannot use test prefixes, using DNS servers")
  26.                 builder.addAddress(DEFAULT_ADDRESS, DEFAULT_PREFIX_LENGTH)
  27.             }
  28.    
  29.             upstreamDnsServers.clear()
  30.    
  31.             CLOUDFLARE_DNS.forEach { addrString ->
  32.                 val address = InetAddress.getByName(addrString)
  33.                 upstreamDnsServers.add(address)
  34.                 address.hostAddress?.let { builder.addRoute(it, 32) }
  35.                 builder.addDnsServer(address)
  36.             }
  37.    
  38.             builder.setBlocking(true)
  39.             builder.allowBypass()
  40.    
  41.             builder.allowFamily(OsConstants.AF_INET)
  42.             builder.allowFamily(OsConstants.AF_INET6)
  43.    
  44.             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) builder.setMetered(false)
  45.    
  46.             builder.addRoute("0.0.0.0", 0) // catch all ipv4
  47.             builder.addRoute("::", 0) // catch all ipv6
  48.             return builder
  49.         }
  50.    
  51.         private fun getDnsServers(context: Context): List<InetAddress> {
  52.             val result = hashSetOf<InetAddress>()
  53.             val connectivityManager = context.getSystemService(VpnService.CONNECTIVITY_SERVICE) as ConnectivityManager
  54.             val activeInfo = connectivityManager.activeNetworkInfo ?: return listOf()
  55.    
  56.             for (network in connectivityManager.allNetworks) {
  57.                 val networkInfo = connectivityManager.getNetworkInfo(network)
  58.                 networkInfo?.let {
  59.                     if (networkInfo.isConnected && networkInfo.type == activeInfo.type && networkInfo.subtype == activeInfo.subtype) {
  60.                         for (address in connectivityManager.getLinkProperties(network)?.dnsServers ?: listOf()) {
  61.                             result.add(address)
  62.                         }
  63.                     }
  64.                 }
  65.             }
  66.    
  67.             return result.toList()
  68.         }
  69.    
  70.         private fun log(text: String) {
  71.             Timber.i("$TAG$text")
  72.         }
  73.    
  74.         private companion object {
  75.             const val TAG = "SecureBuilder:: "
  76.             val TEST_NET_PREFIX_LIST =
  77.                 listOf(
  78.                     "192.0.2", // TEST-NET-1
  79.                     "198.51.100", // TEST-NET-2
  80.                     "203.0.113", // TEST-NET-3
  81.                 )
  82.             const val TEST_NET_POSTFIX = ".1"
  83.             const val TEST_NET_PREFIX_LENGTH = 24
  84.    
  85.             const val DEFAULT_ADDRESS = "192.168.50.1"
  86.             const val DEFAULT_PREFIX_LENGTH = 24
  87.    
  88.             val CLOUDFLARE_DNS = listOf("1.1.1.1", "1.0.0.1")
  89.         }
  90.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement