2. added new DNS functionality

This commit is contained in:
vadym 2020-07-02 10:19:02 -07:00
parent f8875455be
commit 21c70cb0f7
6 changed files with 113 additions and 17 deletions

View File

@ -1,15 +1,11 @@
package io.github.chronosx88.yggdrasil package io.github.chronosx88.yggdrasil
import android.app.Activity import android.app.Activity
import android.content.DialogInterface
import android.content.Intent import android.content.Intent
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.text.Html
import android.view.* import android.view.*
import android.widget.Button import android.widget.*
import android.widget.ListView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.floatingactionbutton.FloatingActionButton import com.google.android.material.floatingactionbutton.FloatingActionButton
@ -18,7 +14,7 @@ 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.deserializeStringList2DNSInfoSet
import io.github.chronosx88.yggdrasil.models.config.Utils.Companion.ping import io.github.chronosx88.yggdrasil.models.config.Utils.Companion.ping
import io.github.chronosx88.yggdrasil.models.config.Utils.Companion.serializeDNSInfoSet2StringList import io.github.chronosx88.yggdrasil.models.config.Utils.Companion.serializeDNSInfoSet2StringList
import kotlinx.coroutines.Runnable import kotlinx.coroutines.*
import java.net.InetAddress import java.net.InetAddress
import kotlin.concurrent.thread import kotlin.concurrent.thread
@ -50,7 +46,7 @@ class DNSListActivity : AppCompatActivity() {
setContentView(R.layout.activity_dns_list) setContentView(R.layout.activity_dns_list)
setSupportActionBar(findViewById(R.id.toolbar)) setSupportActionBar(findViewById(R.id.toolbar))
findViewById<FloatingActionButton>(R.id.fab).setOnClickListener { view -> findViewById<FloatingActionButton>(R.id.fab).setOnClickListener { view ->
addNewDNS()
} }
var extras = intent.extras var extras = intent.extras
var dnsList = findViewById<ListView>(R.id.dnsList) var dnsList = findViewById<ListView>(R.id.dnsList)
@ -86,6 +82,43 @@ class DNSListActivity : AppCompatActivity() {
} }
} }
private fun addNewDNS() {
val view: View = LayoutInflater.from(this).inflate(R.layout.new_dns_dialog, null)
val countryCode: String = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
this.resources.configuration.locales[0].country
} else {
this.resources.configuration.locale.country
}
var ccp = view.findViewById<com.hbb20.CountryCodePicker>(R.id.ccp)
ccp.setCountryForNameCode(countryCode)
val ab: AlertDialog.Builder = AlertDialog.Builder(this)
ab.setCancelable(true).setView(view)
var ad = ab.show()
var addButton = view.findViewById<Button>(R.id.add)
addButton.setOnClickListener{
var ipInput = view.findViewById<TextView>(R.id.ipInput)
var ccpInput = view.findViewById<com.hbb20.CountryCodePicker>(R.id.ccp)
var ip = ipInput.text.toString().toLowerCase()
var ccp = ccpInput.selectedCountryNameCode
GlobalScope.launch {
var di = DNSInfo(InetAddress.getByName("["+ip+"]"), ccp, "User DNS")
try {
var ping = ping(di.address, 53)
di.ping = ping
} catch(e: Throwable){
di.ping = Int.MAX_VALUE
}
withContext(Dispatchers.Main) {
var selectAdapter = (findViewById<ListView>(R.id.peerList).adapter as SelectDNSInfoListAdapter)
selectAdapter.addItem(0, di)
selectAdapter.notifyDataSetChanged()
ad.dismiss()
}
}
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean { override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present. // Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.save, menu) menuInflater.inflate(R.menu.save, menu)

View File

@ -28,10 +28,8 @@ import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
import java.io.IOException
import java.lang.reflect.Type import java.lang.reflect.Type
import java.net.InetAddress import java.net.InetAddress
import java.net.SocketTimeoutException
import java.net.URI import java.net.URI
import java.net.URL import java.net.URL
import java.nio.charset.Charset import java.nio.charset.Charset
@ -55,7 +53,7 @@ class PeerListActivity : AppCompatActivity() {
var isLoading = true; var isLoading = true;
var popupAddress: PopupWindow? = null var popup: PopupWindow? = null
var adapter: DropDownAdapter? = null var adapter: DropDownAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
@ -181,7 +179,7 @@ class PeerListActivity : AppCompatActivity() {
} }
private fun getAddressListPopup(): PopupWindow? { private fun getAddressListPopup(): PopupWindow? {
return popupAddress return popup
} }
private fun getPopupWindow( private fun getPopupWindow(
@ -209,7 +207,7 @@ class PeerListActivity : AppCompatActivity() {
popupWindow.height = WindowManager.LayoutParams.WRAP_CONTENT popupWindow.height = WindowManager.LayoutParams.WRAP_CONTENT
// set the list view as pop up window content // set the list view as pop up window content
popupWindow.contentView = listView popupWindow.contentView = listView
popupAddress = popupWindow popup = popupWindow
return popupWindow return popupWindow
} }

View File

@ -30,8 +30,6 @@ import mobile.Mobile
import mobile.Yggdrasil import mobile.Yggdrasil
import java.io.* import java.io.*
import java.net.Inet6Address import java.net.Inet6Address
import kotlin.concurrent.thread
class YggdrasilTunService : VpnService() { class YggdrasilTunService : VpnService() {
@ -39,7 +37,7 @@ class YggdrasilTunService : VpnService() {
private var isClosed = false private var isClosed = false
/** Maximum packet size is constrained by the MTU, which is given as a signed short - 256 */ /** Maximum packet size is constrained by the MTU, which is given as a signed short - 256 */
private val MAX_PACKET_SIZE = Short.MAX_VALUE-256 private val MAX_PACKET_SIZE = Short.MAX_VALUE/2
companion object { companion object {
private const val TAG = "Yggdrasil-service" private const val TAG = "Yggdrasil-service"

View File

@ -78,6 +78,10 @@ class SelectDNSInfoListAdapter(
allDNS.add(peerInfo) allDNS.add(peerInfo)
} }
fun addItem(index: Int, peerInfo: DNSInfo){
allDNS.add(index, peerInfo)
}
fun sort(){ fun sort(){
allDNS = ArrayList(allDNS.sortedWith(compareBy { it.ping })) allDNS = ArrayList(allDNS.sortedWith(compareBy { it.ping }))
this.notifyDataSetChanged() this.notifyDataSetChanged()

View File

@ -35,6 +35,6 @@
app:elevation="6dp" app:elevation="6dp"
app:fabSize="normal" app:fabSize="normal"
app:srcCompat="@android:drawable/ic_input_add" app:srcCompat="@android:drawable/ic_input_add"
android:visibility="gone"/> />
</androidx.coordinatorlayout.widget.CoordinatorLayout> </androidx.coordinatorlayout.widget.CoordinatorLayout>

View File

@ -0,0 +1,63 @@
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="10dp"
android:background="@color/grey">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/ip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_margin="10dp"
app:boxBackgroundMode="none"
android:background="@drawable/edit_text_rounded_corner"
android:textColorHint="@color/white"
style="@style/EditText.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/ipInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="0,1,2,3,4,5,6,7,8,9,:"
android:hint="IPv6"
android:inputType="textNoSuggestions"
android:textColor="@color/white"
android:textCursorDrawable="@null"
/>
</com.google.android.material.textfield.TextInputLayout>
<com.hbb20.CountryCodePicker
android:id="@+id/ccp"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_vertical"
android:layout_margin="10dp"
app:ccp_contentColor="@color/white"
app:ccp_showFullName="true"
app:ccp_showPhoneCode="false"
app:ccp_showNameCode="false"
app:ccpDialog_backgroundColor="@color/grey"
app:ccpDialog_textColor="@color/white"
app:layout_constraintEnd_toEndOf="@+id/ip"
app:layout_constraintTop_toBottomOf="@+id/ip"
android:background="@drawable/edit_text_rounded_corner"/>
<Button
android:id="@+id/add"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
app:layout_constraintEnd_toEndOf="@+id/schema"
app:layout_constraintTop_toBottomOf="@+id/ccp"
android:background="@drawable/button_selector"
app:backgroundTint="@null"
android:text="ADD"
android:textColor="@color/white"/>
</androidx.constraintlayout.widget.ConstraintLayout>