1. corrected VPN service status on GUI

This commit is contained in:
vadym 2020-06-27 11:39:03 -07:00
parent bee63041f4
commit 484bc7c3d3
5 changed files with 92 additions and 94 deletions

View File

@ -14,7 +14,9 @@ import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.snackbar.Snackbar
import io.github.chronosx88.yggdrasil.models.DNSInfo
import io.github.chronosx88.yggdrasil.models.config.SelectDNSInfoListAdapter
import io.github.chronosx88.yggdrasil.models.config.Utils.Companion.deserializeStringList2DNSInfoSet
import io.github.chronosx88.yggdrasil.models.config.Utils.Companion.ping
import io.github.chronosx88.yggdrasil.models.config.Utils.Companion.serializeDNSInfoSet2StringList
import kotlinx.coroutines.*
import java.net.*
import kotlin.concurrent.thread
@ -54,7 +56,7 @@ class DNSListActivity : AppCompatActivity() {
dnsList.adapter = adapter
thread(start = true) {
try {
var cd = MainActivity.deserializeStringList2DNSInfoSet(
var cd = deserializeStringList2DNSInfoSet(
extras!!.getStringArrayList(MainActivity.DNS_LIST)!!
)
for (d in cd) {
@ -93,7 +95,7 @@ class DNSListActivity : AppCompatActivity() {
var adapter = findViewById<ListView>(R.id.dnsList).adapter as SelectDNSInfoListAdapter
val selectedDNS = adapter.getSelectedDNS()
if(selectedDNS.isNotEmpty()) {
result.putExtra(MainActivity.DNS_LIST, MainActivity.serializeDNSInfoSet2StringList(selectedDNS))
result.putExtra(MainActivity.DNS_LIST, serializeDNSInfoSet2StringList(selectedDNS))
setResult(Activity.RESULT_OK, result)
finish()
} else {

View File

@ -1,10 +1,9 @@
package io.github.chronosx88.yggdrasil
import android.app.Activity
import android.content.BroadcastReceiver
import android.app.ActivityManager
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.VpnService
import android.os.Bundle
import android.util.Log
@ -14,13 +13,17 @@ import android.view.MenuItem
import android.view.View
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.preference.PreferenceManager
import com.google.gson.Gson
import io.github.chronosx88.yggdrasil.models.DNSInfo
import io.github.chronosx88.yggdrasil.models.PeerInfo
import io.github.chronosx88.yggdrasil.models.config.DNSInfoListAdapter
import io.github.chronosx88.yggdrasil.models.config.PeerInfoListAdapter
import io.github.chronosx88.yggdrasil.models.config.Utils.Companion.deserializeStringList2DNSInfoSet
import io.github.chronosx88.yggdrasil.models.config.Utils.Companion.deserializeStringList2PeerInfoSet
import io.github.chronosx88.yggdrasil.models.config.Utils.Companion.deserializeStringSet2DNSInfoSet
import io.github.chronosx88.yggdrasil.models.config.Utils.Companion.deserializeStringSet2PeerInfoSet
import io.github.chronosx88.yggdrasil.models.config.Utils.Companion.serializeDNSInfoSet2StringList
import io.github.chronosx88.yggdrasil.models.config.Utils.Companion.serializePeerInfoSet2StringList
class MainActivity : AppCompatActivity() {
@ -48,65 +51,6 @@ class MainActivity : AppCompatActivity() {
@JvmStatic var isStarted = false
@JvmStatic
fun deserializeStringList2PeerInfoSet(list: List<String>): MutableSet<PeerInfo> {
var gson = Gson()
var out = mutableSetOf<PeerInfo>()
for(s in list) {
out.add(gson.fromJson(s, PeerInfo::class.java))
}
return out
}
@JvmStatic
fun deserializeStringList2DNSInfoSet(list: List<String>): MutableSet<DNSInfo> {
var gson = Gson()
var out = mutableSetOf<DNSInfo>()
for(s in list) {
out.add(gson.fromJson(s, DNSInfo::class.java))
}
return out
}
@JvmStatic
fun deserializeStringSet2PeerInfoSet(list: Set<String>): MutableSet<PeerInfo> {
var gson = Gson()
var out = mutableSetOf<PeerInfo>()
for(s in list) {
out.add(gson.fromJson(s, PeerInfo::class.java))
}
return out
}
@JvmStatic
fun deserializeStringSet2DNSInfoSet(list: Set<String>): MutableSet<DNSInfo> {
var gson = Gson()
var out = mutableSetOf<DNSInfo>()
for(s in list) {
out.add(gson.fromJson(s, DNSInfo::class.java))
}
return out
}
@JvmStatic
fun serializePeerInfoSet2StringList(list: Set<PeerInfo>): ArrayList<String> {
var gson = Gson()
var out = ArrayList<String>()
for(p in list) {
out.add(gson.toJson(p))
}
return out
}
@JvmStatic
fun serializeDNSInfoSet2StringList(list: Set<DNSInfo>): ArrayList<String> {
var gson = Gson()
var out = ArrayList<String>()
for(p in list) {
out.add(gson.toJson(p))
}
return out
}
}
private var currentPeers = setOf<PeerInfo>()
@ -114,9 +58,8 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
LocalBroadcastManager.getInstance(this).registerReceiver(ServiceEchoReceiver(), IntentFilter("pong"));
LocalBroadcastManager.getInstance(this).sendBroadcastSync(Intent("ping"));
setContentView(R.layout.activity_main)
isStarted = isYggServiceRunning(this)
val listView = findViewById<ListView>(R.id.peers)
//save to shared preferences
val preferences =
@ -294,10 +237,15 @@ class MainActivity : AppCompatActivity() {
toast.show()
}
private class ServiceEchoReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
isStarted = true
//TODO reimplement it
private fun isYggServiceRunning(context: Context): Boolean {
val manager =
context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
for (service in manager.getRunningServices(Int.MAX_VALUE)) {
if (YggdrasilTunService::class.java.getName() == service.service.className) {
return true
}
}
return false
}
}

View File

@ -18,7 +18,9 @@ import com.hbb20.CCPCountry
import io.github.chronosx88.yggdrasil.models.PeerInfo
import io.github.chronosx88.yggdrasil.models.Status
import io.github.chronosx88.yggdrasil.models.config.SelectPeerInfoListAdapter
import io.github.chronosx88.yggdrasil.models.config.Utils.Companion.deserializeStringList2PeerInfoSet
import io.github.chronosx88.yggdrasil.models.config.Utils.Companion.ping
import io.github.chronosx88.yggdrasil.models.config.Utils.Companion.serializePeerInfoSet2StringList
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
@ -60,7 +62,7 @@ class PeerListActivity : AppCompatActivity() {
GlobalScope.launch {
try {
var cp = MainActivity.deserializeStringList2PeerInfoSet(
var cp = deserializeStringList2PeerInfoSet(
extras!!.getStringArrayList(MainActivity.PEER_LIST)!!
)
for(pi in cp){
@ -125,7 +127,7 @@ class PeerListActivity : AppCompatActivity() {
var adapter = findViewById<ListView>(R.id.peerList).adapter as SelectPeerInfoListAdapter
val selectedPeers = adapter.getSelectedPeers()
if(selectedPeers.size>0) {
result.putExtra(MainActivity.PEER_LIST, MainActivity.serializePeerInfoSet2StringList(selectedPeers))
result.putExtra(MainActivity.PEER_LIST, serializePeerInfoSet2StringList(selectedPeers))
setResult(Activity.RESULT_OK, result)
finish()
} else {

View File

@ -1,19 +1,17 @@
package io.github.chronosx88.yggdrasil
import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.VpnService
import android.os.ParcelFileDescriptor
import android.system.OsConstants
import android.util.Log
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.google.gson.Gson
import dummy.ConduitEndpoint
import io.github.chronosx88.yggdrasil.models.DNSInfo
import io.github.chronosx88.yggdrasil.models.PeerInfo
import io.github.chronosx88.yggdrasil.models.config.Utils.Companion.deserializeStringList2DNSInfoSet
import io.github.chronosx88.yggdrasil.models.config.Utils.Companion.deserializeStringList2PeerInfoSet
import kotlinx.coroutines.*
import mobile.Mobile
import mobile.Yggdrasil
@ -46,13 +44,6 @@ class YggdrasilTunService : VpnService() {
private var tunOutputStream: OutputStream? = null
private var scope: CoroutineScope? = null
override fun onCreate() {
super.onCreate()
LocalBroadcastManager
.getInstance(this)
.registerReceiver(ServiceEchoReceiver(), IntentFilter("ping"))
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (intent?.getStringExtra(MainActivity.COMMAND) == MainActivity.STOP) {
@ -60,8 +51,8 @@ class YggdrasilTunService : VpnService() {
stopVpn(pi)
}
if (intent?.getStringExtra(MainActivity.COMMAND) == MainActivity.START) {
val peers = MainActivity.deserializeStringList2PeerInfoSet(intent.getStringArrayListExtra(MainActivity.PEERS))
val dns = MainActivity.deserializeStringList2DNSInfoSet(intent.getStringArrayListExtra(MainActivity.DNS))
val peers = deserializeStringList2PeerInfoSet(intent.getStringArrayListExtra(MainActivity.PEERS))
val dns = deserializeStringList2DNSInfoSet(intent.getStringArrayListExtra(MainActivity.DNS))
val pi: PendingIntent = intent.getParcelableExtra(MainActivity.PARAM_PINTENT)
ygg = Yggdrasil()
setupTunInterface(pi, peers, dns)
@ -194,12 +185,4 @@ class YggdrasilTunService : VpnService() {
super.onDestroy()
stopSelf()
}
private class ServiceEchoReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
LocalBroadcastManager
.getInstance(context!!)
.sendBroadcastSync(Intent("pong"))
}
}
}

View File

@ -1,5 +1,8 @@
package io.github.chronosx88.yggdrasil.models.config
import com.google.gson.Gson
import io.github.chronosx88.yggdrasil.models.DNSInfo
import io.github.chronosx88.yggdrasil.models.PeerInfo
import java.net.InetAddress
import java.net.InetSocketAddress
import java.net.Socket
@ -8,6 +11,66 @@ class Utils {
companion object {
@JvmStatic
fun deserializeStringList2PeerInfoSet(list: List<String>): MutableSet<PeerInfo> {
var gson = Gson()
var out = mutableSetOf<PeerInfo>()
for(s in list) {
out.add(gson.fromJson(s, PeerInfo::class.java))
}
return out
}
@JvmStatic
fun deserializeStringList2DNSInfoSet(list: List<String>): MutableSet<DNSInfo> {
var gson = Gson()
var out = mutableSetOf<DNSInfo>()
for(s in list) {
out.add(gson.fromJson(s, DNSInfo::class.java))
}
return out
}
@JvmStatic
fun deserializeStringSet2PeerInfoSet(list: Set<String>): MutableSet<PeerInfo> {
var gson = Gson()
var out = mutableSetOf<PeerInfo>()
for(s in list) {
out.add(gson.fromJson(s, PeerInfo::class.java))
}
return out
}
@JvmStatic
fun deserializeStringSet2DNSInfoSet(list: Set<String>): MutableSet<DNSInfo> {
var gson = Gson()
var out = mutableSetOf<DNSInfo>()
for(s in list) {
out.add(gson.fromJson(s, DNSInfo::class.java))
}
return out
}
@JvmStatic
fun serializePeerInfoSet2StringList(list: Set<PeerInfo>): ArrayList<String> {
var gson = Gson()
var out = ArrayList<String>()
for(p in list) {
out.add(gson.toJson(p))
}
return out
}
@JvmStatic
fun serializeDNSInfoSet2StringList(list: Set<DNSInfo>): ArrayList<String> {
var gson = Gson()
var out = ArrayList<String>()
for(p in list) {
out.add(gson.toJson(p))
}
return out
}
@JvmStatic
fun ping(address: InetAddress, port:Int): Int {
val start = System.currentTimeMillis()