Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ class MainActivity: FlutterActivity() {
private val channelName = "tech.threefold.mycelium/tun"
private val vpnRequestCode = 0x0F

// these two variables are only used during VPN permission flow.
// these variables are only used during VPN permission flow.
private var vpnPermissionPeers: List<String>? = null
private var vpnPermissionSecretKey: ByteArray? = null
private var vpnPermissionDnsServers: List<String>? = null

private lateinit var channel : MethodChannel

Expand All @@ -47,8 +48,10 @@ class MainActivity: FlutterActivity() {
"startVpn" -> {
val peers = call.argument<List<String>>("peers")!!
val secretKey = call.argument<ByteArray>("secretKey")!!
val dnsServers = call.argument<List<String>>("dnsServers") ?: emptyList()
Log.d("tff", "peers = $peers")
val started = startVpn(peers, secretKey)
Log.d("tff", "dnsServers = $dnsServers")
val started = startVpn(peers, secretKey, dnsServers)
result.success(started)
}
"stopVpn" -> {
Expand Down Expand Up @@ -84,7 +87,9 @@ class MainActivity: FlutterActivity() {
if (requestCode == vpnRequestCode) {
if (resultCode == Activity.RESULT_OK) {
Log.i(tag, "VPN permission granted by the user")
startVpn(this.vpnPermissionPeers ?: emptyList(), this.vpnPermissionSecretKey ?: ByteArray(0))
startVpn(this.vpnPermissionPeers ?: emptyList(),
this.vpnPermissionSecretKey ?: ByteArray(0),
this.vpnPermissionDnsServers ?: emptyList())
} else {
// The user denied the VPN permission,
// TODO: handle this case as needed
Expand All @@ -94,19 +99,20 @@ class MainActivity: FlutterActivity() {
}
// checkAskVpnPermission will return true if we need to ask for permission,
// false otherwise.
private fun checkAskVpnPermission(peers: List<String>, secretKey: ByteArray): Boolean{
private fun checkAskVpnPermission(peers: List<String>, secretKey: ByteArray, dnsServers: List<String>): Boolean{
val intent = VpnService.prepare(this)
if (intent != null) {
this.vpnPermissionPeers = peers
this.vpnPermissionSecretKey = secretKey
this.vpnPermissionDnsServers = dnsServers
startActivityForResult(intent, vpnRequestCode)
return true
} else {
return false
}
}
private fun startVpn(peers: List<String>, secretKey: ByteArray): Boolean {
if (checkAskVpnPermission(peers, secretKey)) {
private fun startVpn(peers: List<String>, secretKey: ByteArray, dnsServers: List<String>): Boolean {
if (checkAskVpnPermission(peers, secretKey, dnsServers)) {
// need to ask for permission, so stop the flow here.
// permission handler will be handled by onActivityResult function
return false
Expand All @@ -116,6 +122,7 @@ class MainActivity: FlutterActivity() {
intent.action = TunService.ACTION_START
intent.putExtra("secret_key", secretKey)
intent.putStringArrayListExtra("peers", ArrayList(peers))
intent.putStringArrayListExtra("dns_servers", ArrayList(dnsServers))
startService(intent)

return true
Expand Down
17 changes: 15 additions & 2 deletions android/app/src/main/kotlin/tech/threefold/mycelium/TunService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ class TunService : VpnService(), CoroutineScope {
ACTION_START -> {
val secretKey = intent.getByteArrayExtra("secret_key") ?: ByteArray(0)
val peers = intent.getStringArrayListExtra("peers") ?: emptyList()
start(peers.toList(), secretKey)
val dnsServers = intent.getStringArrayListExtra("dns_servers") ?: emptyList()
start(peers.toList(), secretKey, dnsServers)
START_STICKY
}
else -> {
Expand All @@ -71,7 +72,7 @@ class TunService : VpnService(), CoroutineScope {
}


private fun start(peers: List<String>, secretKey: ByteArray): Int {
private fun start(peers: List<String>, secretKey: ByteArray, dnsServers: List<String>): Int {
if (!started.compareAndSet(false, true)) {
return 0
}
Expand All @@ -87,6 +88,18 @@ class TunService : VpnService(), CoroutineScope {
//.setBlocking(true)
//.setMtu(1400)
.setSession("mycelium")

// Add DNS servers if provided
if (dnsServers.isNotEmpty()) {
if (dnsServers.size >= 1) {
builder.addDnsServer(dnsServers[0])
Log.i(tag, "Added primary DNS server: ${dnsServers[0]}")
}
if (dnsServers.size >= 2) {
builder.addDnsServer(dnsServers[1])
Log.i(tag, "Added secondary DNS server: ${dnsServers[1]}")
}
}


parcel = builder.establish()
Expand Down
Loading