1. IP address state fix, code refactoring

This commit is contained in:
vadym 2020-06-28 06:00:51 -07:00
parent 63f777d2c4
commit 5bb9ddc2de
3 changed files with 38 additions and 28 deletions

View File

@ -52,7 +52,7 @@ class MainActivity : AppCompatActivity() {
@JvmStatic var isStarted = false
@JvmStatic var isCancelled = false
@JvmStatic var address = ""
}
private var currentPeers = setOf<PeerInfo>()
@ -133,6 +133,11 @@ class MainActivity : AppCompatActivity() {
intent.putStringArrayListExtra(DNS_LIST, serializeDNSInfoSet2StringList(currentDNS))
startActivityForResult(intent, DNS_LIST_CODE)
}
if(isStarted){
val ipLayout = findViewById<LinearLayout>(R.id.ipLayout)
ipLayout.visibility = View.VISIBLE
findViewById<TextView>(R.id.ip).text = address
}
}
private fun stopVpn(){
@ -242,8 +247,8 @@ class MainActivity : AppCompatActivity() {
isStarted = true
val ipLayout = findViewById<LinearLayout>(R.id.ipLayout)
ipLayout.visibility = View.VISIBLE
val result: String = data!!.getStringExtra(IPv6)
findViewById<TextView>(R.id.ip).text = result
address = data!!.getStringExtra(IPv6)
findViewById<TextView>(R.id.ip).text = address
}
STATUS_STOP -> {
isStarted = false

View File

@ -39,21 +39,22 @@ class YggdrasilTunService : VpnService() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (intent?.getStringExtra(MainActivity.COMMAND) == MainActivity.STOP) {
val pi: PendingIntent = intent.getParcelableExtra(MainActivity.PARAM_PINTENT)
stopVpn(pi)
}
if (intent?.getStringExtra(MainActivity.COMMAND) == MainActivity.START) {
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)
}
if (intent?.getStringExtra(MainActivity.COMMAND) == MainActivity.UPDATE_DNS) {
val pi: PendingIntent = intent.getParcelableExtra(MainActivity.PARAM_PINTENT)
val dns = deserializeStringList2DNSInfoSet(intent.getStringArrayListExtra(MainActivity.DNS))
setupIOStreams(dns)
when(intent?.getStringExtra(MainActivity.COMMAND)){
MainActivity.STOP ->{
val pi: PendingIntent? = intent.getParcelableExtra(MainActivity.PARAM_PINTENT)
stopVpn(pi)
}
MainActivity.START ->{
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)
}
MainActivity.UPDATE_DNS ->{
val dns = deserializeStringList2DNSInfoSet(intent.getStringArrayListExtra(MainActivity.DNS))
setupIOStreams(dns)
}
}
return super.onStartCommand(intent, flags, startId)
@ -82,11 +83,11 @@ class YggdrasilTunService : VpnService() {
}
private fun setupTunInterface(
pi: PendingIntent,
pi: PendingIntent?,
peers: Set<PeerInfo>,
dns: MutableSet<DNSInfo>
) {
pi.send(MainActivity.STATUS_START)
pi!!.send(MainActivity.STATUS_START)
var configJson = Mobile.generateConfigJSON()
val gson = Gson()
var config = gson.fromJson(String(configJson), Map::class.java).toMutableMap()
@ -173,7 +174,7 @@ class YggdrasilTunService : VpnService() {
}
}
private fun stopVpn(pi: PendingIntent) {
private fun stopVpn(pi: PendingIntent?) {
isClosed = true;
scope!!.coroutineContext.cancelChildren()
tunInputStream!!.close()
@ -183,7 +184,7 @@ class YggdrasilTunService : VpnService() {
Log.d(TAG,"Stop is running from service")
ygg.stop()
val intent: Intent = Intent()
pi.send(this, MainActivity.STATUS_STOP, intent)
pi!!.send(this, MainActivity.STATUS_STOP, intent)
stopSelf()
}

View File

@ -12,21 +12,25 @@ class Utils {
companion object {
@JvmStatic
fun deserializeStringList2PeerInfoSet(list: List<String>): MutableSet<PeerInfo> {
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))
if (list != null) {
for(s in list) {
out.add(gson.fromJson(s, PeerInfo::class.java))
}
}
return out
}
@JvmStatic
fun deserializeStringList2DNSInfoSet(list: List<String>): MutableSet<DNSInfo> {
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))
if (list != null) {
for(s in list) {
out.add(gson.fromJson(s, DNSInfo::class.java))
}
}
return out
}