1. added static IP

This commit is contained in:
vadym 2020-06-29 13:29:39 -07:00
parent 0377fe2565
commit 729b44c127
3 changed files with 47 additions and 16 deletions

View File

@ -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<PeerInfo>()
private var currentDNS = setOf<DNSInfo>()
private val wirelessPeers = mutableListOf<WifiP2pDevice>()
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<Switch>(R.id.switchOn)
switchOn.isChecked = isStarted
val wifiDirect = findViewById<Switch>(R.id.wifiDirect)
switchOn.setOnCheckedChangeListener { _, isChecked ->
if(currentPeers.isEmpty() && !wifiDirect.isChecked){
if(currentPeers.isEmpty()){
switchOn.isChecked = false
return@setOnCheckedChangeListener
}
@ -85,10 +86,12 @@ class MainActivity : AppCompatActivity() {
}
}
val wifiDirect = findViewById<Switch>(R.id.staticIP)
wifiDirect.setOnCheckedChangeListener { _, isChecked ->
if(isChecked){
}
}
val peersListView = findViewById<ListView>(R.id.peers)
//save to shared preferences
@ -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<Switch>(R.id.staticIP).isChecked)
startService(intent)
}
if (requestCode == VPN_REQUEST_CODE && resultCode== Activity.RESULT_CANCELED){

View File

@ -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<PeerInfo>,
dns: MutableSet<DNSInfo>
dns: MutableSet<DNSInfo>,
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<Any?, Any?>, peers: Set<PeerInfo>): MutableMap<Any?, Any?> {
private fun fixConfig(
config: MutableMap<Any?, Any?>,
peers: Set<PeerInfo>,
staticIP: Boolean
): MutableMap<Any?, Any?> {
val whiteList = arrayListOf<String>()
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"

View File

@ -30,7 +30,7 @@
app:popupTheme="@style/AppTheme.PopupOverlay" />
<Switch
android:id="@+id/wifiDirect"
android:id="@+id/staticIP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
@ -38,7 +38,7 @@
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/switchOn"
app:layout_constraintTop_toTopOf="parent"
android:text="Wifi-Direct"
android:text="Static IP"
android:textColor="@color/white"/>
<Switch