mirror of
https://github.com/yggdrasil-network/crispa-android.git
synced 2025-01-22 16:06:30 +00:00
1. added IpV6 result in main activity
This commit is contained in:
parent
eb480c2677
commit
fbc59cd863
@ -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<RadioGroup>(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<TextView>(R.id.ip).setText(result)
|
||||
}
|
||||
else -> { // Note the block
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<Network> {
|
||||
@ -162,7 +163,6 @@ class YggdrasilTunService : VpnService() {
|
||||
}
|
||||
|
||||
fun stopVpn() {
|
||||
isRunning = false
|
||||
readCoroutine.cancel()
|
||||
writeCoroutine.cancel()
|
||||
tunInterface!!.close()
|
||||
|
@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<selector xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<item android:drawable="@color/pink" android:state_checked="true" />
|
||||
<item android:drawable="@color/dark_pink" android:state_pressed="true" />
|
||||
<item android:drawable="@color/transparent" />
|
||||
<item android:drawable="@color/dark_20" android:state_checked="true" />
|
||||
<item android:drawable="@color/dark_10" android:state_pressed="true" />
|
||||
<item android:drawable="@color/dark_5" />
|
||||
</selector>
|
@ -6,6 +6,38 @@
|
||||
android:layout_height="match_parent"
|
||||
android:background="@color/grey"
|
||||
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
|
||||
android:id="@+id/connectRadioGroup"
|
||||
|
@ -6,7 +6,7 @@
|
||||
<color name="white">#ffffff</color>
|
||||
<color name="green">#83cf03</color>
|
||||
<color name="grey">#343334</color>
|
||||
<color name="transparent">#555555</color>
|
||||
<color name="dark_pink">#666666</color>
|
||||
<color name="pink">#777777</color>
|
||||
<color name="dark_5">#555555</color>
|
||||
<color name="dark_10">#666666</color>
|
||||
<color name="dark_20">#777777</color>
|
||||
</resources>
|
||||
|
Loading…
x
Reference in New Issue
Block a user