1. added duplication peer records fix

This commit is contained in:
vadym 2020-06-24 12:43:40 -07:00
parent 572d487e35
commit aec660bff0
6 changed files with 88 additions and 29 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

View File

@ -33,7 +33,7 @@ class MainActivity : AppCompatActivity() {
const val PEERS: String = "PEERS"
const val PEER_LIST_CODE = 1000
const val PEER_LIST = "PEERS_LIST"
const val CURRENT_PEERS = "CURRENT_PEER_INFO"
const val CURRENT_PEERS = "CURRENT_PEERS_v1.1"
const val START_VPN = "START_VPN"
private const val TAG="Yggdrasil"
private const val VPN_REQUEST_CODE = 0x0F
@ -124,7 +124,7 @@ class MainActivity : AppCompatActivity() {
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == VPN_REQUEST_CODE && resultCode== Activity.RESULT_OK){
if(currentPeers.size==0){
if(currentPeers.isEmpty()){
showToast("No peers selected!")
return
}
@ -143,7 +143,7 @@ class MainActivity : AppCompatActivity() {
showToast("No peers selected!")
} else {
this.currentPeers = deserializeStringList2PeerInfoSet(currentPeers)
val adapter = PeerInfoListAdapter(this, ArrayList(this.currentPeers))
val adapter = PeerInfoListAdapter(this, this.currentPeers.sortedWith(compareBy { it.ping }))
val listView = findViewById<ListView>(R.id.peers)
listView.adapter = adapter
@ -152,7 +152,7 @@ class MainActivity : AppCompatActivity() {
PreferenceManager.getDefaultSharedPreferences(this.baseContext)
preferences.edit().putStringSet(CURRENT_PEERS, HashSet(currentPeers)).apply()
if(isStarted){
//apply peer changes
//TODO implement UpdateConfig methon in native interface and apply peer changes
stopVpn()
val i = baseContext.packageManager
.getLaunchIntentForPackage(baseContext.packageName)
@ -161,7 +161,6 @@ class MainActivity : AppCompatActivity() {
i.putExtra(START_VPN, true)
finish()
startActivity(i)
}
}
}
@ -207,7 +206,7 @@ class MainActivity : AppCompatActivity() {
return true
}
fun showToast(text: String){
private fun showToast(text: String){
val duration = Toast.LENGTH_SHORT
val toast = Toast.makeText(applicationContext, text, duration)
toast.setGravity(Gravity.CENTER, 0, 0)

View File

@ -73,13 +73,19 @@ class PeerListActivity : AppCompatActivity() {
GlobalScope.launch {
try {
var cp = MainActivity.deserializeStringList2PeerInfoSet(
extras!!.getStringArrayList(MainActivity.PEER_LIST)!!
)
for(pi in cp){
var ping = ping(pi.address, pi.port)
pi.ping = ping
}
var json = downloadJson(PEER_LIST_URL)
var countries = CCPCountry.getLibraryMasterCountriesEnglish()
val mapType: Type = object :
TypeToken<Map<String?, Map<String, Status>>>() {}.type
val peersMap: Map<String, Map<String, Status>> = Gson().fromJson(json, mapType)
for ((country, peers) in peersMap.entries) {
println("$country:")
for ((peer, status) in peers) {
if (status.up) {
for (ccp in countries) {
@ -89,9 +95,12 @@ class PeerListActivity : AppCompatActivity() {
var url = URI(peer)
try {
var address = InetAddress.getByName(url.host)
var ping = ping(address, url.port)
var peerInfo =
PeerInfo(url.scheme, address, url.port, ccp.nameCode)
if(cp.contains(peerInfo)){
continue
}
var ping = ping(address, url.port)
peerInfo.ping = ping
adapter.addItem(peerInfo)
if(peerList.adapter.count % 5 == 0) {
@ -107,14 +116,9 @@ class PeerListActivity : AppCompatActivity() {
}
}
}
if (extras != null) {
var cp = MainActivity.deserializeStringList2PeerInfoSet(
extras.getStringArrayList(MainActivity.PEER_LIST)!!
)
var currentPeers = ArrayList(cp.sortedWith(compareBy { it.ping }))
withContext(Dispatchers.Main) {
adapter.addAll(0, currentPeers)
}
var currentPeers = ArrayList(cp.sortedWith(compareBy { it.ping }))
withContext(Dispatchers.Main) {
adapter.addAll(0, currentPeers)
}
} catch (e: Throwable){
e.printStackTrace()
@ -134,7 +138,7 @@ class PeerListActivity : AppCompatActivity() {
var adapter = findViewById<ListView>(R.id.peerList).adapter as SelectPeerInfoListAdapter
val selectedPeers = adapter.getSelectedPeers()
if(selectedPeers.size>0) {
result.putExtra(MainActivity.PEER_LIST, MainActivity.serializePeerInfoSet2StringList(adapter.getSelectedPeers()))
result.putExtra(MainActivity.PEER_LIST, MainActivity.serializePeerInfoSet2StringList(selectedPeers))
setResult(Activity.RESULT_OK, result)
finish()
} else {

View File

@ -150,7 +150,13 @@ class YggdrasilTunService : VpnService() {
private fun writePacketsToTun() {
if(tunOutputStream != null) {
val buffer = yggConduitEndpoint.recv()
tunOutputStream!!.write(buffer)
if(buffer!=null) {
try {
tunOutputStream!!.write(buffer)
}catch(e: IOException){
e.printStackTrace()
}
}
}
}
@ -162,11 +168,10 @@ class YggdrasilTunService : VpnService() {
tunOutputStream!!.close()
tunInterface!!.close()
tunInterface = null
//this hack due to https://github.com/yggdrasil-network/yggdrasil-go/issues/714 bug
ygg.startAutoconfigure()
ygg.stop()
val intent: Intent = Intent()
pi.send(this, MainActivity.STATUS_STOP, intent)
stopSelf()
}
override fun onDestroy() {

View File

@ -10,25 +10,27 @@ class PeerInfo {
constructor(schema: String, address: InetAddress, port: Int, countryCode: String){
this.schema = schema
this.address = address
var a = address.toString();
if(a.lastIndexOf('/')>0){
this.hostName = a.split("/")[0]
} else {
this.hostName = a.substring(1)
}
this.port = port
this.countryCode = countryCode
}
var schema: String
var address: InetAddress
var hostName: String
var port = 0
var countryCode: String
var ping: Int = Int.MAX_VALUE
override fun toString(): String {
var a = address.toString();
if(a.indexOf("/")>0){
return this.schema+"://"+a.split("/")[0]+":"+port
if(this.hostName.contains(":")) {
return this.schema + "://[" + this.hostName + "]:" + port
} else {
if(a.contains(":")) {
return this.schema + "://[" + a.substring(1) + "]:" + port
} else {
return this.schema + ":/" + a + ":" + port
}
return this.schema + "://" + this.hostName + ":" + port
}
}

View File

@ -46,6 +46,7 @@
</LinearLayout>
<LinearLayout
android:id="@+id/peerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
@ -93,4 +94,52 @@
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@drawable/info_panel_rounded_corner"
android:gravity="left"
android:paddingLeft="20dp"
android:orientation="vertical"
app:layout_constraintTop_toBottomOf="@id/peerLayout">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
android:orientation="horizontal">
<TextView
android:id="@+id/dnsLabel"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:background="@android:color/transparent"
android:elevation="8dp"
android:gravity="center_vertical"
android:text="DNS:"
android:textColor="@color/dark_30"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/editDNS"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="EDIT"
app:layout_constraintEnd_toEndOf="parent"
android:background="@android:color/transparent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<ListView
android:id="@+id/dnsList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:layout_marginBottom="10dp"
android:dividerHeight="0dp"
android:divider="@null"
app:layout_constraintTop_toTopOf="parent"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>