1. added IpV6 result in main activity

This commit is contained in:
vadym 2020-06-13 03:47:19 -07:00
parent eb480c2677
commit fbc59cd863
5 changed files with 81 additions and 29 deletions

View File

@ -1,17 +1,24 @@
package io.github.chronosx88.yggdrasil package io.github.chronosx88.yggdrasil
import android.R.attr
import android.app.Activity import android.app.Activity
import android.app.PendingIntent
import android.content.Intent import android.content.Intent
import android.net.VpnService import android.net.VpnService
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
import android.widget.RadioGroup import android.widget.RadioGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
companion object { 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 private const val VPN_REQUEST_CODE = 0x0F
} }
@ -19,16 +26,15 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
val connectRadioGroup = findViewById<RadioGroup>(R.id.connectRadioGroup) val connectRadioGroup = findViewById<RadioGroup>(R.id.connectRadioGroup)
connectRadioGroup.setOnCheckedChangeListener( connectRadioGroup.setOnCheckedChangeListener { group, checkedId ->
RadioGroup.OnCheckedChangeListener { group, checkedId ->
when (checkedId) { when (checkedId) {
R.id.disconnectButton -> stopVpn() R.id.disconnectButton -> stopVpn()
R.id.connectButton -> startVpn() R.id.connectButton -> startVpn()
else -> { // Note the block else -> { // Note the block
//print("x is neither 1 nor 2")
}
} }
} }
})
} }
fun stopVpn(){ fun stopVpn(){
@ -52,7 +58,21 @@ class MainActivity : AppCompatActivity() {
super.onActivityResult(requestCode, resultCode, data) super.onActivityResult(requestCode, resultCode, data)
if (requestCode == VPN_REQUEST_CODE && resultCode== Activity.RESULT_OK){ if (requestCode == VPN_REQUEST_CODE && resultCode== Activity.RESULT_OK){
val intent = Intent(this, YggdrasilTunService::class.java) 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) startService(intent)
} }
when (resultCode) {
STATUS_START -> print("service started")
STATUS_FINISH -> {
val result: String = data!!.getStringExtra(IPv6)
findViewById<TextView>(R.id.ip).setText(result)
}
else -> { // Note the block
}
}
} }
} }

View File

@ -1,6 +1,6 @@
package io.github.chronosx88.yggdrasil package io.github.chronosx88.yggdrasil
import android.app.Service import android.app.PendingIntent
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.net.ConnectivityManager import android.net.ConnectivityManager
@ -20,7 +20,6 @@ import java.io.InputStream
import java.io.OutputStream import java.io.OutputStream
import java.nio.ByteBuffer import java.nio.ByteBuffer
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
import kotlin.experimental.or
class YggdrasilTunService : VpnService() { class YggdrasilTunService : VpnService() {
@ -29,7 +28,7 @@ class YggdrasilTunService : VpnService() {
private val MAX_PACKET_SIZE = Short.MAX_VALUE.toInt() private val MAX_PACKET_SIZE = Short.MAX_VALUE.toInt()
companion object { companion object {
private var isRunning: Boolean = false private const val TAG = "Yggdrasil-service"
} }
private var tunInterface: ParcelFileDescriptor? = null private var tunInterface: ParcelFileDescriptor? = null
private lateinit var yggConduitEndpoint: ConduitEndpoint private lateinit var yggConduitEndpoint: ConduitEndpoint
@ -42,16 +41,16 @@ class YggdrasilTunService : VpnService() {
if (intent?.getStringExtra("COMMAND") == "STOP") { if (intent?.getStringExtra("COMMAND") == "STOP") {
stopVpn() stopVpn()
} }
return Service.START_STICKY if (intent?.getStringExtra("COMMAND") == "START") {
val pi: PendingIntent = intent.getParcelableExtra(MainActivity.PARAM_PINTENT)
setupTunInterface(pi)
} }
override fun onCreate() { return super.onStartCommand(intent, flags, startId);
super.onCreate()
isRunning = true
setupTunInterface()
} }
private fun setupTunInterface() { private fun setupTunInterface(pi: PendingIntent) {
pi.send(MainActivity.STATUS_START);
val builder = Builder() val builder = Builder()
val ygg = Yggdrasil() val ygg = Yggdrasil()
var configJson = Mobile.generateConfigJSON() var configJson = Mobile.generateConfigJSON()
@ -86,6 +85,8 @@ class YggdrasilTunService : VpnService() {
writePacketsToTun() writePacketsToTun()
} }
} }
val intent: Intent = Intent().putExtra(MainActivity.IPv6, address)
pi.send(this, MainActivity.STATUS_FINISH, intent)
} }
private fun getNonVpnNetworks(Address: String): Array<Network> { private fun getNonVpnNetworks(Address: String): Array<Network> {
@ -162,7 +163,6 @@ class YggdrasilTunService : VpnService() {
} }
fun stopVpn() { fun stopVpn() {
isRunning = false
readCoroutine.cancel() readCoroutine.cancel()
writeCoroutine.cancel() writeCoroutine.cancel()
tunInterface!!.close() tunInterface!!.close()

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/pink" android:state_checked="true" /> <item android:drawable="@color/dark_20" android:state_checked="true" />
<item android:drawable="@color/dark_pink" android:state_pressed="true" /> <item android:drawable="@color/dark_10" android:state_pressed="true" />
<item android:drawable="@color/transparent" /> <item android:drawable="@color/dark_5" />
</selector> </selector>

View File

@ -6,6 +6,38 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="@color/grey" android:background="@color/grey"
tools:context=".MainActivity"> tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toTopOf="@+id/connectRadioGroup"
app:layout_constraintTop_toTopOf="parent"
android:orientation="vertical"
android:gravity="left">
<TextView
android:id="@+id/ipLabel"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/dark_10"
android:elevation="8dp"
android:gravity="center_vertical"
android:text="Your IP address:"
android:textColor="@color/white"
android:paddingLeft="20dp"/>
<TextView
android:id="@+id/ip"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="@color/dark_10"
android:elevation="8dp"
android:gravity="center_vertical"
android:text=""
android:textColor="@color/white"
android:paddingLeft="20dp"
app:layout_constraintBottom_toTopOf="@+id/connectRadioGroup"
app:layout_constraintTop_toTopOf="parent"/>
</LinearLayout>
<RadioGroup <RadioGroup
android:id="@+id/connectRadioGroup" android:id="@+id/connectRadioGroup"

View File

@ -6,7 +6,7 @@
<color name="white">#ffffff</color> <color name="white">#ffffff</color>
<color name="green">#83cf03</color> <color name="green">#83cf03</color>
<color name="grey">#343334</color> <color name="grey">#343334</color>
<color name="transparent">#555555</color> <color name="dark_5">#555555</color>
<color name="dark_pink">#666666</color> <color name="dark_10">#666666</color>
<color name="pink">#777777</color> <color name="dark_20">#777777</color>
</resources> </resources>