3. DNS dynamic list selection fixes

This commit is contained in:
vadym 2020-07-03 14:38:19 -07:00
parent 0201705c45
commit d403c0c15d
5 changed files with 43 additions and 41 deletions

View File

@ -52,11 +52,12 @@ class DNSListActivity : AppCompatActivity() {
var dnsList = findViewById<ListView>(R.id.dnsList)
var adapter = SelectDNSInfoListAdapter(this, arrayListOf(), mutableSetOf())
dnsList.adapter = adapter
var cd = deserializeStringList2DNSInfoSet(
extras!!.getStringArrayList(MainActivity.DNS_LIST)!!
)
thread(start = true) {
try {
var cd = deserializeStringList2DNSInfoSet(
extras!!.getStringArrayList(MainActivity.DNS_LIST)!!
)
for (d in cd) {
var ping = ping(d.address, 53)
d.ping = ping
@ -72,14 +73,22 @@ class DNSListActivity : AppCompatActivity() {
{
adapter.addItem(dns)
adapter.sort()
isLoading = false
}
)
}
} catch (e: Throwable) {
e.printStackTrace()
}
runOnUiThread(
Runnable
{
var currentDNS = ArrayList(cd.sortedWith(compareBy { it.ping }))
adapter.addAll(0, currentDNS)
isLoading = false
}
)
}
}
private fun addNewDNS() {
@ -133,18 +142,9 @@ class DNSListActivity : AppCompatActivity() {
val result = Intent(this, MainActivity::class.java)
var adapter = findViewById<ListView>(R.id.dnsList).adapter as SelectDNSInfoListAdapter
val selectedDNS = adapter.getSelectedDNS()
if(selectedDNS.isNotEmpty()) {
result.putExtra(MainActivity.DNS_LIST, serializeDNSInfoSet2StringList(selectedDNS))
setResult(Activity.RESULT_OK, result)
finish()
} else {
val text = "Select at least one DNS"
val duration = Toast.LENGTH_SHORT
val toast = Toast.makeText(applicationContext, text, duration)
toast.setGravity(Gravity.CENTER, 0, 0)
toast.show()
}
}
result.putExtra(MainActivity.DNS_LIST, serializeDNSInfoSet2StringList(selectedDNS))
setResult(Activity.RESULT_OK, result)
finish() }
return true
}
}

View File

@ -50,8 +50,6 @@ class MainActivity : AppCompatActivity() {
const val STATUS_FINISH = 8
const val STATUS_STOP = 9
const val IPv6: String = "IPv6"
const val PEERS: String = "PEERS"
const val DNS: String = "DNS"
const val PEER_LIST_CODE = 1000
const val DNS_LIST_CODE = 2000
const val PEER_LIST = "PEERS_LIST"
@ -173,7 +171,7 @@ class MainActivity : AppCompatActivity() {
val pi = createPendingResult(TASK_CODE, intent, 0)
intent.putExtra(PARAM_PINTENT, pi)
intent.putExtra(COMMAND, UPDATE_DNS)
intent.putStringArrayListExtra(DNS, serializeDNSInfoSet2StringList(currentDNS))
intent.putStringArrayListExtra(CURRENT_DNS, serializeDNSInfoSet2StringList(currentDNS))
startService(intent)
}
@ -197,8 +195,8 @@ class MainActivity : AppCompatActivity() {
val pi = createPendingResult(TASK_CODE, intent, 0)
intent.putExtra(PARAM_PINTENT, pi)
intent.putExtra(COMMAND, START)
intent.putStringArrayListExtra(PEERS, serializePeerInfoSet2StringList(currentPeers))
intent.putStringArrayListExtra(DNS, serializeDNSInfoSet2StringList(currentDNS))
intent.putStringArrayListExtra(CURRENT_PEERS, serializePeerInfoSet2StringList(currentPeers))
intent.putStringArrayListExtra(CURRENT_DNS, serializeDNSInfoSet2StringList(currentDNS))
intent.putExtra(STATIC_IP, findViewById<Switch>(R.id.staticIP).isChecked)
startService(intent)
@ -235,20 +233,16 @@ class MainActivity : AppCompatActivity() {
if (requestCode == DNS_LIST_CODE && resultCode== Activity.RESULT_OK){
if(data!!.extras!=null){
var currentDNS = data.extras!!.getStringArrayList(DNS_LIST)
if(currentDNS==null || currentDNS.size==0){
showToast("No DNS selected!")
} else {
this.currentDNS = deserializeStringList2DNSInfoSet(currentDNS)
val adapter = DNSInfoListAdapter(this, this.currentDNS.sortedWith(compareBy { it.ping }))
val listView = findViewById<ListView>(R.id.dns)
listView.adapter = adapter
//save to shared preferences
val preferences =
PreferenceManager.getDefaultSharedPreferences(this.baseContext)
preferences.edit().putStringSet(CURRENT_DNS, HashSet(currentDNS)).apply()
if(isStarted){
updateDNS()
}
this.currentDNS = deserializeStringList2DNSInfoSet(currentDNS)
val adapter = DNSInfoListAdapter(this, this.currentDNS.sortedWith(compareBy { it.ping }))
val listView = findViewById<ListView>(R.id.dns)
listView.adapter = adapter
//save to shared preferences
val preferences =
PreferenceManager.getDefaultSharedPreferences(this.baseContext)
preferences.edit().putStringSet(CURRENT_DNS, HashSet(currentDNS)).apply()
if(isStarted){
updateDNS()
}
}
}

View File

@ -181,7 +181,7 @@ class PeerListActivity : AppCompatActivity() {
}
}
fun onClickSchemaList(v: View) {
private fun onClickSchemaList(v: View) {
val height = -1 * v.height +30
getAddressListPopup()?.showAsDropDown(v, -5, height)
}

View File

@ -36,8 +36,8 @@ class YggdrasilTunService : VpnService() {
private lateinit var ygg: Yggdrasil
private var isClosed = false
/** Maximum packet size is constrained by the MTU, which is given as a signed short - 256 */
private val MAX_PACKET_SIZE = 65535
/** Maximum packet size is constrained by the MTU, which is given as a signed short/2 */
private val MAX_PACKET_SIZE = Short.MAX_VALUE/2
companion object {
private const val TAG = "Yggdrasil-service"
@ -65,15 +65,15 @@ class YggdrasilTunService : VpnService() {
startForeground(FOREGROUND_ID, foregroundNotification("Yggdrasil service stopped"))
}
MainActivity.START ->{
val peers = deserializeStringList2PeerInfoSet(intent.getStringArrayListExtra(MainActivity.PEERS))
val dns = deserializeStringList2DNSInfoSet(intent.getStringArrayListExtra(MainActivity.DNS))
val peers = deserializeStringList2PeerInfoSet(intent.getStringArrayListExtra(MainActivity.CURRENT_PEERS))
val dns = deserializeStringList2DNSInfoSet(intent.getStringArrayListExtra(MainActivity.CURRENT_DNS))
val staticIP: Boolean = intent.getBooleanExtra(MainActivity.STATIC_IP, false)
ygg = Yggdrasil()
setupTunInterface(pi, peers, dns, staticIP)
startForeground(FOREGROUND_ID, foregroundNotification("Yggdrasil service started"))
}
MainActivity.UPDATE_DNS ->{
val dns = deserializeStringList2DNSInfoSet(intent.getStringArrayListExtra(MainActivity.DNS))
val dns = deserializeStringList2DNSInfoSet(intent.getStringArrayListExtra(MainActivity.CURRENT_DNS))
setupIOStreams(dns)
}
MainActivity.UPDATE_PEERS ->{
@ -90,6 +90,7 @@ class YggdrasilTunService : VpnService() {
var builder = Builder()
.addAddress(address, 7)
.allowFamily(OsConstants.AF_INET)
.allowBypass()
.setMtu(MAX_PACKET_SIZE)
if (dns.size > 0) {
builder.addDnsServer(address)

View File

@ -82,6 +82,13 @@ class SelectDNSInfoListAdapter(
allDNS.add(index, peerInfo)
}
fun addAll(index: Int, dnsInfo: ArrayList<DNSInfo>){
currentDNS.addAll(dnsInfo)
allDNS.removeAll(dnsInfo)
allDNS.addAll(index, dnsInfo)
this.notifyDataSetChanged()
}
fun sort(){
allDNS = ArrayList(allDNS.sortedWith(compareBy { it.ping }))
this.notifyDataSetChanged()