ProtocolApi refactoring, move network classes to NetworkUtils.kt
This commit is contained in:
parent
8ec105bee0
commit
9738ada946
13 changed files with 213 additions and 122 deletions
|
|
@ -14,4 +14,5 @@ android {
|
|||
dependencies {
|
||||
compileOnly(project(":utils"))
|
||||
implementation(libs.androidx.annotation)
|
||||
implementation(libs.kotlinx.coroutines)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,8 @@ import androidx.annotation.RequiresApi
|
|||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
import java.util.zip.ZipFile
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import org.amnezia.vpn.util.InetNetwork
|
||||
import org.amnezia.vpn.util.Log
|
||||
import org.json.JSONObject
|
||||
|
||||
|
|
@ -21,8 +23,11 @@ const val VPN_SESSION_NAME = "AmneziaVPN"
|
|||
abstract class Protocol {
|
||||
|
||||
abstract val statistics: Statistics
|
||||
protected lateinit var state: MutableStateFlow<ProtocolState>
|
||||
|
||||
abstract fun initialize(context: Context)
|
||||
open fun initialize(context: Context, state: MutableStateFlow<ProtocolState>) {
|
||||
this.state = state
|
||||
}
|
||||
|
||||
abstract fun startVpn(config: JSONObject, vpnBuilder: Builder, protect: (Int) -> Boolean)
|
||||
|
||||
|
|
@ -30,11 +35,17 @@ abstract class Protocol {
|
|||
|
||||
protected open fun buildVpnInterface(config: ProtocolConfig, vpnBuilder: Builder) {
|
||||
vpnBuilder.setSession(VPN_SESSION_NAME)
|
||||
vpnBuilder.allowFamily(OsConstants.AF_INET)
|
||||
vpnBuilder.allowFamily(OsConstants.AF_INET6)
|
||||
|
||||
for (addr in config.addresses) vpnBuilder.addAddress(addr)
|
||||
|
||||
for (addr in config.dnsServers) vpnBuilder.addDnsServer(addr)
|
||||
// fix for Samsung android ignoring DNS servers outside the VPN route range
|
||||
if (Build.BRAND == "samsung") {
|
||||
for (addr in config.dnsServers) vpnBuilder.addRoute(InetNetwork(addr))
|
||||
}
|
||||
|
||||
config.searchDomain?.let { vpnBuilder.addSearchDomain(it) }
|
||||
|
||||
for (addr in config.routes) vpnBuilder.addRoute(addr)
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
|
||||
|
|
@ -43,6 +54,15 @@ abstract class Protocol {
|
|||
for (app in config.excludedApplications) vpnBuilder.addDisallowedApplication(app)
|
||||
|
||||
vpnBuilder.setMtu(config.mtu)
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
|
||||
config.httpProxy?.let { vpnBuilder.setHttpProxy(it) }
|
||||
|
||||
if (config.allowAllAF) {
|
||||
vpnBuilder.allowFamily(OsConstants.AF_INET)
|
||||
vpnBuilder.allowFamily(OsConstants.AF_INET6)
|
||||
}
|
||||
|
||||
vpnBuilder.setBlocking(config.blockingMode)
|
||||
vpnBuilder.setUnderlyingNetworks(null)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
|
||||
|
|
|
|||
|
|
@ -1,16 +1,18 @@
|
|||
package org.amnezia.vpn.protocol
|
||||
|
||||
import android.net.InetAddresses
|
||||
import android.os.Build
|
||||
import androidx.annotation.RequiresApi
|
||||
import android.net.ProxyInfo
|
||||
import java.net.InetAddress
|
||||
import org.amnezia.vpn.util.InetNetwork
|
||||
|
||||
open class ProtocolConfig protected constructor(
|
||||
val addresses: Set<InetNetwork>,
|
||||
val dnsServers: Set<InetAddress>,
|
||||
val searchDomain: String?,
|
||||
val routes: Set<InetNetwork>,
|
||||
val excludedRoutes: Set<InetNetwork>,
|
||||
val excludedApplications: Set<String>,
|
||||
val httpProxy: ProxyInfo?,
|
||||
val allowAllAF: Boolean,
|
||||
val blockingMode: Boolean,
|
||||
val mtu: Int
|
||||
) {
|
||||
|
|
@ -18,25 +20,37 @@ open class ProtocolConfig protected constructor(
|
|||
protected constructor(builder: Builder) : this(
|
||||
builder.addresses,
|
||||
builder.dnsServers,
|
||||
builder.searchDomain,
|
||||
builder.routes,
|
||||
builder.excludedRoutes,
|
||||
builder.excludedApplications,
|
||||
builder.httpProxy,
|
||||
builder.allowAllAF,
|
||||
builder.blockingMode,
|
||||
builder.mtu
|
||||
)
|
||||
|
||||
class Builder(blockingMode: Boolean) {
|
||||
open class Builder(blockingMode: Boolean) {
|
||||
internal val addresses: MutableSet<InetNetwork> = hashSetOf()
|
||||
internal val dnsServers: MutableSet<InetAddress> = hashSetOf()
|
||||
internal val routes: MutableSet<InetNetwork> = hashSetOf()
|
||||
internal val excludedRoutes: MutableSet<InetNetwork> = hashSetOf()
|
||||
internal val excludedApplications: MutableSet<String> = hashSetOf()
|
||||
|
||||
internal var searchDomain: String? = null
|
||||
private set
|
||||
|
||||
internal var httpProxy: ProxyInfo? = null
|
||||
private set
|
||||
|
||||
internal var allowAllAF: Boolean = false
|
||||
private set
|
||||
|
||||
internal var blockingMode: Boolean = blockingMode
|
||||
private set
|
||||
|
||||
internal var mtu: Int = 0
|
||||
private set
|
||||
open var mtu: Int = 0
|
||||
protected set
|
||||
|
||||
fun addAddress(addr: InetNetwork) = apply { this.addresses += addr }
|
||||
fun addAddresses(addresses: List<InetNetwork>) = apply { this.addresses += addresses }
|
||||
|
|
@ -44,18 +58,21 @@ open class ProtocolConfig protected constructor(
|
|||
fun addDnsServer(dnsServer: InetAddress) = apply { this.dnsServers += dnsServer }
|
||||
fun addDnsServers(dnsServers: List<InetAddress>) = apply { this.dnsServers += dnsServers }
|
||||
|
||||
fun setSearchDomain(domain: String) = apply { this.searchDomain = domain }
|
||||
|
||||
fun addRoute(route: InetNetwork) = apply { this.routes += route }
|
||||
fun addRoutes(routes: List<InetNetwork>) = apply { this.routes += routes }
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
|
||||
fun excludeRoute(route: InetNetwork) = apply { this.excludedRoutes += route }
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
|
||||
fun excludeRoutes(routes: List<InetNetwork>) = apply { this.excludedRoutes += routes }
|
||||
|
||||
fun excludeApplication(application: String) = apply { this.excludedApplications += application }
|
||||
fun excludeApplications(applications: List<String>) = apply { this.excludedApplications += applications }
|
||||
|
||||
fun setHttpProxy(httpProxy: ProxyInfo) = apply { this.httpProxy = httpProxy }
|
||||
|
||||
fun setAllowAllAF(allowAllAF: Boolean) = apply { this.allowAllAF = allowAllAF }
|
||||
|
||||
fun setBlockingMode(blockingMode: Boolean) = apply { this.blockingMode = blockingMode }
|
||||
|
||||
fun setMtu(mtu: Int) = apply { this.mtu = mtu }
|
||||
|
|
@ -72,7 +89,7 @@ open class ProtocolConfig protected constructor(
|
|||
if (errorMessage.isNotEmpty()) throw BadConfigException(errorMessage.toString())
|
||||
}
|
||||
|
||||
fun build(): ProtocolConfig = validate().run { ProtocolConfig(this@Builder) }
|
||||
open fun build(): ProtocolConfig = validate().run { ProtocolConfig(this@Builder) }
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
|
@ -80,43 +97,3 @@ open class ProtocolConfig protected constructor(
|
|||
Builder(blockingMode).apply(block).build()
|
||||
}
|
||||
}
|
||||
|
||||
data class InetNetwork(val address: InetAddress, val mask: Int) {
|
||||
|
||||
override fun toString(): String = "${address.hostAddress}/$mask"
|
||||
|
||||
companion object {
|
||||
fun parse(data: String): InetNetwork {
|
||||
val split = data.split("/")
|
||||
val address = parseInetAddress(split.first())
|
||||
val mask = split.last().toInt()
|
||||
return InetNetwork(address, mask)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
data class InetEndpoint(val address: InetAddress, val port: Int) {
|
||||
|
||||
override fun toString(): String = "${address.hostAddress}:$port"
|
||||
|
||||
companion object {
|
||||
fun parse(data: String): InetEndpoint {
|
||||
val split = data.split(":")
|
||||
val address = parseInetAddress(split.first())
|
||||
val port = split.last().toInt()
|
||||
return InetEndpoint(address, port)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun parseInetAddress(address: String): InetAddress = parseNumericAddressCompat(address)
|
||||
|
||||
private val parseNumericAddressCompat: (String) -> InetAddress =
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
InetAddresses::parseNumericAddress
|
||||
} else {
|
||||
val m = InetAddress::class.java.getMethod("parseNumericAddress", String::class.java)
|
||||
fun(address: String): InetAddress {
|
||||
return m.invoke(null, address) as InetAddress
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,5 +4,6 @@ enum class ProtocolState {
|
|||
CONNECTED,
|
||||
CONNECTING,
|
||||
DISCONNECTED,
|
||||
DISCONNECTING
|
||||
DISCONNECTING,
|
||||
UNKNOWN
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue