diff --git a/app/src/main/java/io/github/chronosx88/yggdrasil/DNSListActivity.kt b/app/src/main/java/io/github/chronosx88/yggdrasil/DNSListActivity.kt index 0cd86a2..270246b 100644 --- a/app/src/main/java/io/github/chronosx88/yggdrasil/DNSListActivity.kt +++ b/app/src/main/java/io/github/chronosx88/yggdrasil/DNSListActivity.kt @@ -45,26 +45,24 @@ class DNSListActivity : AppCompatActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.activity_dns_list) setSupportActionBar(findViewById(R.id.toolbar)) - findViewById(R.id.fab).setOnClickListener { view -> + findViewById(R.id.fab).setOnClickListener { _ -> addNewDNS() } var extras = intent.extras var dnsList = findViewById(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 } for (dns in allDNS) { - if (cd.contains(dns)) { - continue - } var ping = ping(dns.address, 53) dns.ping = ping runOnUiThread( @@ -72,16 +70,25 @@ 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 + } + ) } + } + @Suppress("DEPRECATION") 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) { @@ -90,8 +97,7 @@ class DNSListActivity : AppCompatActivity() { this.resources.configuration.locale.country } - var ccp = view.findViewById(R.id.ccp) - ccp.setCountryForNameCode(countryCode) + view.findViewById(R.id.ccp).setCountryForNameCode(countryCode) val ab: AlertDialog.Builder = AlertDialog.Builder(this) ab.setCancelable(true).setView(view) var ad = ab.show() @@ -133,18 +139,9 @@ class DNSListActivity : AppCompatActivity() { val result = Intent(this, MainActivity::class.java) var adapter = findViewById(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 } } \ No newline at end of file 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 a1c8734..e525341 100644 --- a/app/src/main/java/io/github/chronosx88/yggdrasil/MainActivity.kt +++ b/app/src/main/java/io/github/chronosx88/yggdrasil/MainActivity.kt @@ -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(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(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(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() } } } diff --git a/app/src/main/java/io/github/chronosx88/yggdrasil/PeerListActivity.kt b/app/src/main/java/io/github/chronosx88/yggdrasil/PeerListActivity.kt index 67d0e86..4f74ab2 100644 --- a/app/src/main/java/io/github/chronosx88/yggdrasil/PeerListActivity.kt +++ b/app/src/main/java/io/github/chronosx88/yggdrasil/PeerListActivity.kt @@ -28,6 +28,7 @@ import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import java.io.ByteArrayOutputStream +import java.io.FileNotFoundException import java.lang.reflect.Type import java.net.InetAddress import java.net.URI @@ -60,7 +61,7 @@ class PeerListActivity : AppCompatActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.activity_peer_list) setSupportActionBar(findViewById(R.id.toolbar)) - findViewById(R.id.fab).setOnClickListener { view -> + findViewById(R.id.fab).setOnClickListener { _ -> addNewPeer() } var extras = intent.extras @@ -77,41 +78,45 @@ class PeerListActivity : AppCompatActivity() { 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>>() {}.type - val peersMap: Map> = Gson().fromJson(json, mapType) - for ((country, peers) in peersMap.entries) { - for ((peer, status) in peers) { - if (status.up) { - for (ccp in countries) { - if (ccp.name.toLowerCase() - .contains(country.replace(".md", "").replace("-", " ")) - ) { - var url = URI(peer) - try { - var address = InetAddress.getByName(url.host) - var peerInfo = - PeerInfo(url.scheme, address, url.port, ccp.nameCode) - if(cp.contains(peerInfo)){ - continue - } - var ping = ping(address, url.port) - peerInfo.ping = ping - withContext(Dispatchers.Main) { - adapter.addItem(peerInfo) - if(adapter.count % 5 == 0) { - adapter.sort() + try { + var json = downloadJson(PEER_LIST_URL) + var countries = CCPCountry.getLibraryMasterCountriesEnglish() + val mapType: Type = object : + TypeToken>>() {}.type + val peersMap: Map> = Gson().fromJson(json, mapType) + for ((country, peers) in peersMap.entries) { + for ((peer, status) in peers) { + if (status.up) { + for (ccp in countries) { + if (ccp.name.toLowerCase() + .contains(country.replace(".md", "").replace("-", " ")) + ) { + var url = URI(peer) + try { + var address = InetAddress.getByName(url.host) + var peerInfo = + PeerInfo(url.scheme, address, url.port, ccp.nameCode) + var ping = ping(address, url.port) + peerInfo.ping = ping + if(cp.contains(peerInfo)){ + continue } + withContext(Dispatchers.Main) { + adapter.addItem(peerInfo) + if(adapter.count % 5 == 0) { + adapter.sort() + } + } + } catch (e: Throwable){ + e.printStackTrace() } - } catch (e: Throwable){ - e.printStackTrace() } } } } } + } catch(e: FileNotFoundException){ + e.printStackTrace() } var currentPeers = ArrayList(cp.sortedWith(compareBy { it.ping })) withContext(Dispatchers.Main) { @@ -136,7 +141,7 @@ class PeerListActivity : AppCompatActivity() { var ipInput = view.findViewById(R.id.ipInput) ipInput.requestFocus() schemaInput.showSoftInputOnFocus = false - schemaInput.setOnFocusChangeListener { v, b -> + schemaInput.setOnFocusChangeListener { v, _ -> if(schemaInput.isFocused) { onClickSchemaList(v) } @@ -144,9 +149,8 @@ class PeerListActivity : AppCompatActivity() { schemaInput.setOnClickListener { v-> onClickSchemaList(v) } - getPopupWindow(R.layout.spinner_item, resources.getStringArray(R.array.schemas), schemaInput, getString(R.string.schema)); - var ccp = view.findViewById(R.id.ccp) - ccp.setCountryForNameCode(countryCode) + getPopupWindow(R.layout.spinner_item, resources.getStringArray(R.array.schemas), schemaInput); + view.findViewById(R.id.ccp).setCountryForNameCode(countryCode) val ab: AlertDialog.Builder = AlertDialog.Builder(this) ab.setCancelable(true).setView(view) var ad = ab.show() @@ -176,7 +180,7 @@ class PeerListActivity : AppCompatActivity() { } } - fun onClickSchemaList(v: View) { + private fun onClickSchemaList(v: View) { val height = -1 * v.height +30 getAddressListPopup()?.showAsDropDown(v, -5, height) } @@ -188,8 +192,7 @@ class PeerListActivity : AppCompatActivity() { private fun getPopupWindow( textViewResourceId: Int, objects: Array, - editText: TextView, - hint: String? + editText: TextView ): PopupWindow? { // initialize a pop up window type val popupWindow = PopupWindow(this) @@ -203,9 +206,6 @@ class PeerListActivity : AppCompatActivity() { listView.onItemClickListener = adapter // some other visual settings popupWindow.isFocusable = true - //popupWindow.setWidth(400); - val display: Display = - (this.getSystemService(Context.WINDOW_SERVICE) as WindowManager).getDefaultDisplay() popupWindow.width = 320 popupWindow.height = WindowManager.LayoutParams.WRAP_CONTENT // set the list view as pop up window content 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 f90025b..96e95ad 100644 --- a/app/src/main/java/io/github/chronosx88/yggdrasil/YggdrasilTunService.kt +++ b/app/src/main/java/io/github/chronosx88/yggdrasil/YggdrasilTunService.kt @@ -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 ->{ @@ -88,11 +88,11 @@ class YggdrasilTunService : VpnService() { address = ygg.addressString var builder = Builder() - .addAddress(address, 7) + .addAddress(address!!, 7) .allowFamily(OsConstants.AF_INET) + .allowBypass() .setMtu(MAX_PACKET_SIZE) if (dns.size > 0) { - builder.addDnsServer(address) for (d in dns) { builder.addDnsServer(d.address) } @@ -246,11 +246,11 @@ class YggdrasilTunService : VpnService() { private fun stopVpn(pi: PendingIntent?) { isClosed = true; + scope!!.coroutineContext.cancelChildren() tunInputStream!!.close() tunOutputStream!!.close() tunInterface!!.close() tunInterface = null - scope!!.coroutineContext.cancelChildren() Log.d(TAG,"Stop is running from service") ygg.stop() val intent: Intent = Intent() @@ -271,10 +271,12 @@ class YggdrasilTunService : VpnService() { val networks = cm.allNetworks for (network in networks) { val linkProperties = cm.getLinkProperties(network) - val routes = linkProperties.routes - for (route in routes) { - if (route.isDefaultRoute && route.gateway is Inet6Address) { - return true + if(linkProperties!=null) { + val routes = linkProperties.routes + for (route in routes) { + if (route.isDefaultRoute && route.gateway is Inet6Address) { + return true + } } } } diff --git a/app/src/main/java/io/github/chronosx88/yggdrasil/models/config/DropDownAdapter.kt b/app/src/main/java/io/github/chronosx88/yggdrasil/models/config/DropDownAdapter.kt index bb62127..f98fa33 100644 --- a/app/src/main/java/io/github/chronosx88/yggdrasil/models/config/DropDownAdapter.kt +++ b/app/src/main/java/io/github/chronosx88/yggdrasil/models/config/DropDownAdapter.kt @@ -23,9 +23,10 @@ class DropDownAdapter( ) : ArrayAdapter(context, textViewResourceId, objects), OnItemClickListener { - private val objects: Array - private val popup: PopupWindow - private val editText: TextView + private val objects: Array = objects + private val popup: PopupWindow = popup + private val editText: TextView = editText + override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup?): View? { return getCustomView(position, convertView, parent) } @@ -34,8 +35,8 @@ class DropDownAdapter( return getCustomView(position, convertView, parent) } - fun getCustomView(position: Int, convertView: View?, parent: ViewGroup?): View { - var convertView: View? = convertView + fun getCustomView(position: Int, view: View?, parent: ViewGroup?): View { + var convertView: View? = view if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.dropdown_item, parent, false) @@ -43,7 +44,7 @@ class DropDownAdapter( val sub = convertView?.findViewById(R.id.sub) as TextView val address = objects[position] sub.text = address - return convertView!! + return convertView } override fun onItemClick(arg0: AdapterView<*>?, v: View, arg2: Int, arg3: Long) { @@ -61,9 +62,4 @@ class DropDownAdapter( editText.text = address } - init { - this.objects = objects - this.popup = popup - this.editText = editText - } } \ No newline at end of file diff --git a/app/src/main/java/io/github/chronosx88/yggdrasil/models/config/SelectDNSInfoAdapter.kt b/app/src/main/java/io/github/chronosx88/yggdrasil/models/config/SelectDNSInfoAdapter.kt index 8cae5be..1b706cb 100644 --- a/app/src/main/java/io/github/chronosx88/yggdrasil/models/config/SelectDNSInfoAdapter.kt +++ b/app/src/main/java/io/github/chronosx88/yggdrasil/models/config/SelectDNSInfoAdapter.kt @@ -82,6 +82,13 @@ class SelectDNSInfoListAdapter( allDNS.add(index, peerInfo) } + fun addAll(index: Int, dnsInfo: ArrayList){ + currentDNS.addAll(dnsInfo) + allDNS.removeAll(dnsInfo) + allDNS.addAll(index, dnsInfo) + this.notifyDataSetChanged() + } + fun sort(){ allDNS = ArrayList(allDNS.sortedWith(compareBy { it.ping })) this.notifyDataSetChanged() diff --git a/yggdrasil/Makefile b/yggdrasil/Makefile index 0742211..8de93c7 100644 --- a/yggdrasil/Makefile +++ b/yggdrasil/Makefile @@ -4,7 +4,7 @@ all: -go get -u github.com/yggdrasil-network/yggdrasil-go; -cd $(GOPATH)/src/github.com/yggdrasil-network/yggdrasil-go; \ go get -v -d ./...; \ - go get -u github.com/vikulin/yggdrasil-extras@ab56805; \ + go get -u github.com/vikulin/yggdrasil-extras@268b006; \ ANDROID=true ./build; mv -f $(GOPATH)/src/github.com/yggdrasil-network/yggdrasil-go/yggdrasil.aar yggdrasil.aar; mv -f $(GOPATH)/src/github.com/yggdrasil-network/yggdrasil-go/yggdrasil-sources.jar yggdrasil-sources.jar;