diff --git a/app/src/main/java/io/github/chronosx88/yggdrasil/MainActivity.kt b/app/src/main/java/io/github/chronosx88/yggdrasil/MainActivity.kt index 60b380a..0e9a83e 100644 --- a/app/src/main/java/io/github/chronosx88/yggdrasil/MainActivity.kt +++ b/app/src/main/java/io/github/chronosx88/yggdrasil/MainActivity.kt @@ -1,17 +1,24 @@ package io.github.chronosx88.yggdrasil +import android.R.attr import android.app.Activity +import android.app.PendingIntent import android.content.Intent import android.net.VpnService import android.os.Bundle import android.util.Log import android.widget.RadioGroup +import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { companion object { - private const val TAG="Yggdrasil"; + const val PARAM_PINTENT = "pendingIntent" + const val STATUS_START = 1 + const val STATUS_FINISH = 0 + const val IPv6: String = "IPv6" + private const val TAG="Yggdrasil" private const val VPN_REQUEST_CODE = 0x0F } @@ -19,16 +26,15 @@ class MainActivity : AppCompatActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val connectRadioGroup = findViewById(R.id.connectRadioGroup) - connectRadioGroup.setOnCheckedChangeListener( - RadioGroup.OnCheckedChangeListener { group, checkedId -> - when (checkedId) { - R.id.disconnectButton -> stopVpn() - R.id.connectButton -> startVpn() - else -> { // Note the block - //print("x is neither 1 nor 2") - } - } - }) + connectRadioGroup.setOnCheckedChangeListener { group, checkedId -> + when (checkedId) { + R.id.disconnectButton -> stopVpn() + R.id.connectButton -> startVpn() + else -> { // Note the block + + } + } + } } fun stopVpn(){ @@ -52,7 +58,21 @@ class MainActivity : AppCompatActivity() { super.onActivityResult(requestCode, resultCode, data) if (requestCode == VPN_REQUEST_CODE && resultCode== Activity.RESULT_OK){ val intent = Intent(this, YggdrasilTunService::class.java) + val TASK_CODE = 100 + var pi = createPendingResult(TASK_CODE, intent, 0); + intent.putExtra("COMMAND", "START") + intent.putExtra(PARAM_PINTENT, pi) startService(intent) } + when (resultCode) { + STATUS_START -> print("service started") + STATUS_FINISH -> { + val result: String = data!!.getStringExtra(IPv6) + findViewById(R.id.ip).setText(result) + } + else -> { // Note the block + + } + } } } diff --git a/app/src/main/java/io/github/chronosx88/yggdrasil/YggdrasilTunService.kt b/app/src/main/java/io/github/chronosx88/yggdrasil/YggdrasilTunService.kt index 736947b..aad8eca 100644 --- a/app/src/main/java/io/github/chronosx88/yggdrasil/YggdrasilTunService.kt +++ b/app/src/main/java/io/github/chronosx88/yggdrasil/YggdrasilTunService.kt @@ -1,6 +1,6 @@ package io.github.chronosx88.yggdrasil -import android.app.Service +import android.app.PendingIntent import android.content.Context import android.content.Intent import android.net.ConnectivityManager @@ -20,7 +20,6 @@ import java.io.InputStream import java.io.OutputStream import java.nio.ByteBuffer import kotlin.coroutines.CoroutineContext -import kotlin.experimental.or class YggdrasilTunService : VpnService() { @@ -29,7 +28,7 @@ class YggdrasilTunService : VpnService() { private val MAX_PACKET_SIZE = Short.MAX_VALUE.toInt() companion object { - private var isRunning: Boolean = false + private const val TAG = "Yggdrasil-service" } private var tunInterface: ParcelFileDescriptor? = null private lateinit var yggConduitEndpoint: ConduitEndpoint @@ -42,16 +41,16 @@ class YggdrasilTunService : VpnService() { if (intent?.getStringExtra("COMMAND") == "STOP") { stopVpn() } - return Service.START_STICKY + if (intent?.getStringExtra("COMMAND") == "START") { + val pi: PendingIntent = intent.getParcelableExtra(MainActivity.PARAM_PINTENT) + setupTunInterface(pi) + } + + return super.onStartCommand(intent, flags, startId); } - override fun onCreate() { - super.onCreate() - isRunning = true - setupTunInterface() - } - - private fun setupTunInterface() { + private fun setupTunInterface(pi: PendingIntent) { + pi.send(MainActivity.STATUS_START); val builder = Builder() val ygg = Yggdrasil() var configJson = Mobile.generateConfigJSON() @@ -86,6 +85,8 @@ class YggdrasilTunService : VpnService() { writePacketsToTun() } } + val intent: Intent = Intent().putExtra(MainActivity.IPv6, address) + pi.send(this, MainActivity.STATUS_FINISH, intent) } private fun getNonVpnNetworks(Address: String): Array { @@ -162,7 +163,6 @@ class YggdrasilTunService : VpnService() { } fun stopVpn() { - isRunning = false readCoroutine.cancel() writeCoroutine.cancel() tunInterface!!.close() diff --git a/app/src/main/res/drawable/toggle_widget_background.xml b/app/src/main/res/drawable/toggle_widget_background.xml index 3fad79f..f1c7b08 100644 --- a/app/src/main/res/drawable/toggle_widget_background.xml +++ b/app/src/main/res/drawable/toggle_widget_background.xml @@ -1,6 +1,6 @@ - - - + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 8ae190e..69295aa 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -6,6 +6,38 @@ android:layout_height="match_parent" android:background="@color/grey" tools:context=".MainActivity"> + + + + + + #ffffff #83cf03 #343334 - #555555 - #666666 - #777777 + #555555 + #666666 + #777777