From 729b44c127168d0c4e2e39124508080a07ca4d49 Mon Sep 17 00:00:00 2001 From: vadym Date: Mon, 29 Jun 2020 13:29:39 -0700 Subject: [PATCH] 1. added static IP --- .../chronosx88/yggdrasil/MainActivity.kt | 19 +++++---- .../yggdrasil/YggdrasilTunService.kt | 40 +++++++++++++++---- app/src/main/res/layout/activity_main.xml | 4 +- 3 files changed, 47 insertions(+), 16 deletions(-) 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 a361907..7758128 100644 --- a/app/src/main/java/io/github/chronosx88/yggdrasil/MainActivity.kt +++ b/app/src/main/java/io/github/chronosx88/yggdrasil/MainActivity.kt @@ -5,7 +5,6 @@ import android.app.ActivityManager import android.content.* import android.net.VpnService import android.net.wifi.p2p.WifiP2pDevice -import android.net.wifi.p2p.WifiP2pManager import android.os.Bundle import android.util.Log import android.view.Gravity @@ -28,6 +27,11 @@ import io.github.chronosx88.yggdrasil.models.config.Utils.Companion.serializePee class MainActivity : AppCompatActivity() { companion object { + const val STATIC_IP = "STATIC_IP" + const val signingPrivateKey = "signingPrivateKey" + const val signingPublicKey = "signingPublicKey" + const val encryptionPrivateKey = "encryptionPrivateKey" + const val encryptionPublicKey = "encryptionPublicKey" const val COMMAND = "COMMAND" const val STOP = "STOP" const val START = "START" @@ -57,9 +61,6 @@ class MainActivity : AppCompatActivity() { private var currentPeers = setOf() private var currentDNS = setOf() - private val wirelessPeers = mutableListOf() - private val intentFilter = IntentFilter() - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) @@ -67,9 +68,9 @@ class MainActivity : AppCompatActivity() { isStarted = isYggServiceRunning(this) val switchOn = findViewById(R.id.switchOn) switchOn.isChecked = isStarted - val wifiDirect = findViewById(R.id.wifiDirect) + switchOn.setOnCheckedChangeListener { _, isChecked -> - if(currentPeers.isEmpty() && !wifiDirect.isChecked){ + if(currentPeers.isEmpty()){ switchOn.isChecked = false return@setOnCheckedChangeListener } @@ -85,9 +86,11 @@ class MainActivity : AppCompatActivity() { } } - + val wifiDirect = findViewById(R.id.staticIP) wifiDirect.setOnCheckedChangeListener { _, isChecked -> + if(isChecked){ + } } val peersListView = findViewById(R.id.peers) @@ -187,6 +190,8 @@ class MainActivity : AppCompatActivity() { intent.putExtra(COMMAND, START) intent.putStringArrayListExtra(PEERS, serializePeerInfoSet2StringList(currentPeers)) intent.putStringArrayListExtra(DNS, serializeDNSInfoSet2StringList(currentDNS)) + intent.putExtra(STATIC_IP, findViewById(R.id.staticIP).isChecked) + startService(intent) } if (requestCode == VPN_REQUEST_CODE && resultCode== Activity.RESULT_CANCELED){ 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 d1d8f24..ab57079 100644 --- a/app/src/main/java/io/github/chronosx88/yggdrasil/YggdrasilTunService.kt +++ b/app/src/main/java/io/github/chronosx88/yggdrasil/YggdrasilTunService.kt @@ -4,11 +4,11 @@ import android.app.PendingIntent import android.content.Context import android.content.Intent import android.net.ConnectivityManager -import android.net.Network import android.net.VpnService import android.os.ParcelFileDescriptor import android.system.OsConstants import android.util.Log +import androidx.preference.PreferenceManager import com.google.gson.Gson import dummy.ConduitEndpoint import io.github.chronosx88.yggdrasil.models.DNSInfo @@ -21,8 +21,6 @@ import mobile.Mobile import mobile.Yggdrasil import java.io.* import java.net.Inet6Address -import java.nio.ByteBuffer -import kotlin.concurrent.thread class YggdrasilTunService : VpnService() { @@ -52,9 +50,10 @@ class YggdrasilTunService : VpnService() { MainActivity.START ->{ val peers = deserializeStringList2PeerInfoSet(intent.getStringArrayListExtra(MainActivity.PEERS)) val dns = deserializeStringList2DNSInfoSet(intent.getStringArrayListExtra(MainActivity.DNS)) + val staticIP: Boolean = intent.getBooleanExtra(MainActivity.STATIC_IP, false) val pi: PendingIntent = intent.getParcelableExtra(MainActivity.PARAM_PINTENT) ygg = Yggdrasil() - setupTunInterface(pi, peers, dns) + setupTunInterface(pi, peers, dns, staticIP) } MainActivity.UPDATE_DNS ->{ val dns = deserializeStringList2DNSInfoSet(intent.getStringArrayListExtra(MainActivity.DNS)) @@ -97,13 +96,15 @@ class YggdrasilTunService : VpnService() { private fun setupTunInterface( pi: PendingIntent?, peers: Set, - dns: MutableSet + dns: MutableSet, + staticIP: Boolean ) { pi!!.send(MainActivity.STATUS_START) var configJson = Mobile.generateConfigJSON() val gson = Gson() var config = gson.fromJson(String(configJson), Map::class.java).toMutableMap() - config = fixConfig(config, peers) + config = fixConfig(config, peers, staticIP) + configJson = gson.toJson(config).toByteArray() var yggConduitEndpoint = ygg.startJSON(configJson) @@ -128,7 +129,11 @@ class YggdrasilTunService : VpnService() { pi.send(this, MainActivity.STATUS_FINISH, intent) } - private fun fixConfig(config: MutableMap, peers: Set): MutableMap { + private fun fixConfig( + config: MutableMap, + peers: Set, + staticIP: Boolean + ): MutableMap { val whiteList = arrayListOf() whiteList.add("") @@ -138,6 +143,27 @@ class YggdrasilTunService : VpnService() { config["Listen"] = "" config["AdminListen"] = "tcp://localhost:9001" config["IfName"] = "tun0" + if(staticIP) { + val preferences = + PreferenceManager.getDefaultSharedPreferences(this.baseContext) + if(preferences.getString(MainActivity.signingPrivateKey, null)==null) { + val encryptionPublicKey = config["EncryptionPublicKey"].toString() + val encryptionPrivateKey = config["EncryptionPrivateKey"].toString() + val signingPublicKey = config["SigningPublicKey"].toString() + val signingPrivateKey = config["SigningPrivateKey"].toString() + preferences.edit() + .putString(MainActivity.signingPrivateKey, signingPrivateKey) + .putString(MainActivity.signingPublicKey, signingPublicKey) + .putString(MainActivity.encryptionPrivateKey, encryptionPrivateKey) + .putString(MainActivity.encryptionPublicKey, encryptionPublicKey).apply() + } else { + config["signingPrivateKey"] = preferences.getString(MainActivity.signingPrivateKey, null) + config["signingPublicKey"] = preferences.getString(MainActivity.signingPublicKey, null) + config["encryptionPrivateKey"] = preferences.getString(MainActivity.encryptionPrivateKey, null) + config["encryptionPublicKey"] = preferences.getString(MainActivity.encryptionPublicKey, null) + } + } + //config["EncryptionPublicKey"] = "b15633cf66e63a04f03e9d1a5b2ac6411af819cde9e74175cf574d5599b1296c" //config["EncryptionPrivateKey"] = "a39e2da3ccbb5afc3854574a2e3823e881d2d720754d6fdc877f57b252d3b521" //config["SigningPublicKey"] = "4f248483c094aea370fba86f1630ba5099cb230aa1337ab6ef6ff0b132be2c2b" diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 5fc60e9..11d6687 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -30,7 +30,7 @@ app:popupTheme="@style/AppTheme.PopupOverlay" />