Merge pull request #11 from vikulin/master

New DNS and new Peer forms
This commit is contained in:
ChronosX88 2020-07-07 11:37:34 +04:00 committed by GitHub
commit 0d589709fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 100 additions and 104 deletions

View File

@ -45,26 +45,24 @@ class DNSListActivity : AppCompatActivity() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
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 { _ ->
addNewDNS() addNewDNS()
} }
var extras = intent.extras var extras = intent.extras
var dnsList = findViewById<ListView>(R.id.dnsList) var dnsList = findViewById<ListView>(R.id.dnsList)
var adapter = SelectDNSInfoListAdapter(this, arrayListOf(), mutableSetOf()) var adapter = SelectDNSInfoListAdapter(this, arrayListOf(), mutableSetOf())
dnsList.adapter = adapter dnsList.adapter = adapter
thread(start = true) {
try {
var cd = deserializeStringList2DNSInfoSet( var cd = deserializeStringList2DNSInfoSet(
extras!!.getStringArrayList(MainActivity.DNS_LIST)!! extras!!.getStringArrayList(MainActivity.DNS_LIST)!!
) )
thread(start = true) {
try {
for (d in cd) { for (d in cd) {
var ping = ping(d.address, 53) var ping = ping(d.address, 53)
d.ping = ping d.ping = ping
} }
for (dns in allDNS) { for (dns in allDNS) {
if (cd.contains(dns)) {
continue
}
var ping = ping(dns.address, 53) var ping = ping(dns.address, 53)
dns.ping = ping dns.ping = ping
runOnUiThread( runOnUiThread(
@ -72,16 +70,25 @@ class DNSListActivity : AppCompatActivity() {
{ {
adapter.addItem(dns) adapter.addItem(dns)
adapter.sort() adapter.sort()
isLoading = false
} }
) )
} }
} catch (e: Throwable) { } catch (e: Throwable) {
e.printStackTrace() e.printStackTrace()
} }
runOnUiThread(
Runnable
{
var currentDNS = ArrayList(cd.sortedWith(compareBy { it.ping }))
adapter.addAll(0, currentDNS)
isLoading = false
} }
)
} }
}
@Suppress("DEPRECATION")
private fun addNewDNS() { private fun addNewDNS() {
val view: View = LayoutInflater.from(this).inflate(R.layout.new_dns_dialog, null) 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) { val countryCode: String = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
@ -90,8 +97,7 @@ class DNSListActivity : AppCompatActivity() {
this.resources.configuration.locale.country this.resources.configuration.locale.country
} }
var ccp = view.findViewById<com.hbb20.CountryCodePicker>(R.id.ccp) view.findViewById<com.hbb20.CountryCodePicker>(R.id.ccp).setCountryForNameCode(countryCode)
ccp.setCountryForNameCode(countryCode)
val ab: AlertDialog.Builder = AlertDialog.Builder(this) val ab: AlertDialog.Builder = AlertDialog.Builder(this)
ab.setCancelable(true).setView(view) ab.setCancelable(true).setView(view)
var ad = ab.show() var ad = ab.show()
@ -133,18 +139,9 @@ class DNSListActivity : AppCompatActivity() {
val result = Intent(this, MainActivity::class.java) val result = Intent(this, MainActivity::class.java)
var adapter = findViewById<ListView>(R.id.dnsList).adapter as SelectDNSInfoListAdapter var adapter = findViewById<ListView>(R.id.dnsList).adapter as SelectDNSInfoListAdapter
val selectedDNS = adapter.getSelectedDNS() val selectedDNS = adapter.getSelectedDNS()
if(selectedDNS.isNotEmpty()) {
result.putExtra(MainActivity.DNS_LIST, serializeDNSInfoSet2StringList(selectedDNS)) result.putExtra(MainActivity.DNS_LIST, serializeDNSInfoSet2StringList(selectedDNS))
setResult(Activity.RESULT_OK, result) setResult(Activity.RESULT_OK, result)
finish() 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()
}
}
return true return true
} }
} }

View File

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

View File

@ -28,6 +28,7 @@ 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.FileNotFoundException
import java.lang.reflect.Type import java.lang.reflect.Type
import java.net.InetAddress import java.net.InetAddress
import java.net.URI import java.net.URI
@ -60,7 +61,7 @@ class PeerListActivity : AppCompatActivity() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_peer_list) setContentView(R.layout.activity_peer_list)
setSupportActionBar(findViewById(R.id.toolbar)) setSupportActionBar(findViewById(R.id.toolbar))
findViewById<FloatingActionButton>(R.id.fab).setOnClickListener { view -> findViewById<FloatingActionButton>(R.id.fab).setOnClickListener { _ ->
addNewPeer() addNewPeer()
} }
var extras = intent.extras var extras = intent.extras
@ -77,6 +78,7 @@ class PeerListActivity : AppCompatActivity() {
var ping = ping(pi.address, pi.port) var ping = ping(pi.address, pi.port)
pi.ping = ping pi.ping = ping
} }
try {
var json = downloadJson(PEER_LIST_URL) var json = downloadJson(PEER_LIST_URL)
var countries = CCPCountry.getLibraryMasterCountriesEnglish() var countries = CCPCountry.getLibraryMasterCountriesEnglish()
val mapType: Type = object : val mapType: Type = object :
@ -94,11 +96,11 @@ class PeerListActivity : AppCompatActivity() {
var address = InetAddress.getByName(url.host) var address = InetAddress.getByName(url.host)
var peerInfo = var peerInfo =
PeerInfo(url.scheme, address, url.port, ccp.nameCode) PeerInfo(url.scheme, address, url.port, ccp.nameCode)
var ping = ping(address, url.port)
peerInfo.ping = ping
if(cp.contains(peerInfo)){ if(cp.contains(peerInfo)){
continue continue
} }
var ping = ping(address, url.port)
peerInfo.ping = ping
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
adapter.addItem(peerInfo) adapter.addItem(peerInfo)
if(adapter.count % 5 == 0) { if(adapter.count % 5 == 0) {
@ -113,6 +115,9 @@ class PeerListActivity : AppCompatActivity() {
} }
} }
} }
} catch(e: FileNotFoundException){
e.printStackTrace()
}
var currentPeers = ArrayList(cp.sortedWith(compareBy { it.ping })) var currentPeers = ArrayList(cp.sortedWith(compareBy { it.ping }))
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
adapter.addAll(0, currentPeers) adapter.addAll(0, currentPeers)
@ -136,7 +141,7 @@ class PeerListActivity : AppCompatActivity() {
var ipInput = view.findViewById<TextView>(R.id.ipInput) var ipInput = view.findViewById<TextView>(R.id.ipInput)
ipInput.requestFocus() ipInput.requestFocus()
schemaInput.showSoftInputOnFocus = false schemaInput.showSoftInputOnFocus = false
schemaInput.setOnFocusChangeListener { v, b -> schemaInput.setOnFocusChangeListener { v, _ ->
if(schemaInput.isFocused) { if(schemaInput.isFocused) {
onClickSchemaList(v) onClickSchemaList(v)
} }
@ -144,9 +149,8 @@ class PeerListActivity : AppCompatActivity() {
schemaInput.setOnClickListener { v-> schemaInput.setOnClickListener { v->
onClickSchemaList(v) onClickSchemaList(v)
} }
getPopupWindow(R.layout.spinner_item, resources.getStringArray(R.array.schemas), schemaInput, getString(R.string.schema)); getPopupWindow(R.layout.spinner_item, resources.getStringArray(R.array.schemas), schemaInput);
var ccp = view.findViewById<com.hbb20.CountryCodePicker>(R.id.ccp) view.findViewById<com.hbb20.CountryCodePicker>(R.id.ccp).setCountryForNameCode(countryCode)
ccp.setCountryForNameCode(countryCode)
val ab: AlertDialog.Builder = AlertDialog.Builder(this) val ab: AlertDialog.Builder = AlertDialog.Builder(this)
ab.setCancelable(true).setView(view) ab.setCancelable(true).setView(view)
var ad = ab.show() 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 val height = -1 * v.height +30
getAddressListPopup()?.showAsDropDown(v, -5, height) getAddressListPopup()?.showAsDropDown(v, -5, height)
} }
@ -188,8 +192,7 @@ class PeerListActivity : AppCompatActivity() {
private fun getPopupWindow( private fun getPopupWindow(
textViewResourceId: Int, textViewResourceId: Int,
objects: Array<String>, objects: Array<String>,
editText: TextView, editText: TextView
hint: String?
): PopupWindow? { ): PopupWindow? {
// initialize a pop up window type // initialize a pop up window type
val popupWindow = PopupWindow(this) val popupWindow = PopupWindow(this)
@ -203,9 +206,6 @@ class PeerListActivity : AppCompatActivity() {
listView.onItemClickListener = adapter listView.onItemClickListener = adapter
// some other visual settings // some other visual settings
popupWindow.isFocusable = true popupWindow.isFocusable = true
//popupWindow.setWidth(400);
val display: Display =
(this.getSystemService(Context.WINDOW_SERVICE) as WindowManager).getDefaultDisplay()
popupWindow.width = 320 popupWindow.width = 320
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

View File

@ -36,8 +36,8 @@ class YggdrasilTunService : VpnService() {
private lateinit var ygg: Yggdrasil private lateinit var ygg: Yggdrasil
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/2 */
private val MAX_PACKET_SIZE = 65535 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"
@ -65,15 +65,15 @@ class YggdrasilTunService : VpnService() {
startForeground(FOREGROUND_ID, foregroundNotification("Yggdrasil service stopped")) startForeground(FOREGROUND_ID, foregroundNotification("Yggdrasil service stopped"))
} }
MainActivity.START ->{ MainActivity.START ->{
val peers = deserializeStringList2PeerInfoSet(intent.getStringArrayListExtra(MainActivity.PEERS)) val peers = deserializeStringList2PeerInfoSet(intent.getStringArrayListExtra(MainActivity.CURRENT_PEERS))
val dns = deserializeStringList2DNSInfoSet(intent.getStringArrayListExtra(MainActivity.DNS)) val dns = deserializeStringList2DNSInfoSet(intent.getStringArrayListExtra(MainActivity.CURRENT_DNS))
val staticIP: Boolean = intent.getBooleanExtra(MainActivity.STATIC_IP, false) val staticIP: Boolean = intent.getBooleanExtra(MainActivity.STATIC_IP, false)
ygg = Yggdrasil() ygg = Yggdrasil()
setupTunInterface(pi, peers, dns, staticIP) setupTunInterface(pi, peers, dns, staticIP)
startForeground(FOREGROUND_ID, foregroundNotification("Yggdrasil service started")) startForeground(FOREGROUND_ID, foregroundNotification("Yggdrasil service started"))
} }
MainActivity.UPDATE_DNS ->{ MainActivity.UPDATE_DNS ->{
val dns = deserializeStringList2DNSInfoSet(intent.getStringArrayListExtra(MainActivity.DNS)) val dns = deserializeStringList2DNSInfoSet(intent.getStringArrayListExtra(MainActivity.CURRENT_DNS))
setupIOStreams(dns) setupIOStreams(dns)
} }
MainActivity.UPDATE_PEERS ->{ MainActivity.UPDATE_PEERS ->{
@ -88,11 +88,11 @@ class YggdrasilTunService : VpnService() {
address = ygg.addressString address = ygg.addressString
var builder = Builder() var builder = Builder()
.addAddress(address, 7) .addAddress(address!!, 7)
.allowFamily(OsConstants.AF_INET) .allowFamily(OsConstants.AF_INET)
.allowBypass()
.setMtu(MAX_PACKET_SIZE) .setMtu(MAX_PACKET_SIZE)
if (dns.size > 0) { if (dns.size > 0) {
builder.addDnsServer(address)
for (d in dns) { for (d in dns) {
builder.addDnsServer(d.address) builder.addDnsServer(d.address)
} }
@ -246,11 +246,11 @@ class YggdrasilTunService : VpnService() {
private fun stopVpn(pi: PendingIntent?) { private fun stopVpn(pi: PendingIntent?) {
isClosed = true; isClosed = true;
scope!!.coroutineContext.cancelChildren()
tunInputStream!!.close() tunInputStream!!.close()
tunOutputStream!!.close() tunOutputStream!!.close()
tunInterface!!.close() tunInterface!!.close()
tunInterface = null tunInterface = null
scope!!.coroutineContext.cancelChildren()
Log.d(TAG,"Stop is running from service") Log.d(TAG,"Stop is running from service")
ygg.stop() ygg.stop()
val intent: Intent = Intent() val intent: Intent = Intent()
@ -271,6 +271,7 @@ class YggdrasilTunService : VpnService() {
val networks = cm.allNetworks val networks = cm.allNetworks
for (network in networks) { for (network in networks) {
val linkProperties = cm.getLinkProperties(network) val linkProperties = cm.getLinkProperties(network)
if(linkProperties!=null) {
val routes = linkProperties.routes val routes = linkProperties.routes
for (route in routes) { for (route in routes) {
if (route.isDefaultRoute && route.gateway is Inet6Address) { if (route.isDefaultRoute && route.gateway is Inet6Address) {
@ -278,6 +279,7 @@ class YggdrasilTunService : VpnService() {
} }
} }
} }
}
return false return false
} }

View File

@ -23,9 +23,10 @@ class DropDownAdapter(
) : ) :
ArrayAdapter<String?>(context, textViewResourceId, objects), OnItemClickListener { ArrayAdapter<String?>(context, textViewResourceId, objects), OnItemClickListener {
private val objects: Array<String> private val objects: Array<String> = objects
private val popup: PopupWindow private val popup: PopupWindow = popup
private val editText: TextView private val editText: TextView = editText
override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup?): View? { override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup?): View? {
return getCustomView(position, convertView, parent) return getCustomView(position, convertView, parent)
} }
@ -34,8 +35,8 @@ class DropDownAdapter(
return getCustomView(position, convertView, parent) return getCustomView(position, convertView, parent)
} }
fun getCustomView(position: Int, convertView: View?, parent: ViewGroup?): View { fun getCustomView(position: Int, view: View?, parent: ViewGroup?): View {
var convertView: View? = convertView var convertView: View? = view
if (convertView == null) { if (convertView == null) {
convertView = convertView =
LayoutInflater.from(context).inflate(R.layout.dropdown_item, parent, false) 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 sub = convertView?.findViewById(R.id.sub) as TextView
val address = objects[position] val address = objects[position]
sub.text = address sub.text = address
return convertView!! return convertView
} }
override fun onItemClick(arg0: AdapterView<*>?, v: View, arg2: Int, arg3: Long) { override fun onItemClick(arg0: AdapterView<*>?, v: View, arg2: Int, arg3: Long) {
@ -61,9 +62,4 @@ class DropDownAdapter(
editText.text = address editText.text = address
} }
init {
this.objects = objects
this.popup = popup
this.editText = editText
}
} }

View File

@ -82,6 +82,13 @@ class SelectDNSInfoListAdapter(
allDNS.add(index, peerInfo) 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(){ fun sort(){
allDNS = ArrayList(allDNS.sortedWith(compareBy { it.ping })) allDNS = ArrayList(allDNS.sortedWith(compareBy { it.ping }))
this.notifyDataSetChanged() this.notifyDataSetChanged()

View File

@ -4,7 +4,7 @@ all:
-go get -u github.com/yggdrasil-network/yggdrasil-go; -go get -u github.com/yggdrasil-network/yggdrasil-go;
-cd $(GOPATH)/src/github.com/yggdrasil-network/yggdrasil-go; \ -cd $(GOPATH)/src/github.com/yggdrasil-network/yggdrasil-go; \
go get -v -d ./...; \ 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; 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.aar yggdrasil.aar;
mv -f $(GOPATH)/src/github.com/yggdrasil-network/yggdrasil-go/yggdrasil-sources.jar yggdrasil-sources.jar; mv -f $(GOPATH)/src/github.com/yggdrasil-network/yggdrasil-go/yggdrasil-sources.jar yggdrasil-sources.jar;