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)
setContentView(R.layout.activity_dns_list)
setSupportActionBar(findViewById(R.id.toolbar))
findViewById<FloatingActionButton>(R.id.fab).setOnClickListener { view ->
findViewById<FloatingActionButton>(R.id.fab).setOnClickListener { _ ->
addNewDNS()
}
var extras = intent.extras
var dnsList = findViewById<ListView>(R.id.dnsList)
var adapter = SelectDNSInfoListAdapter(this, arrayListOf(), mutableSetOf())
dnsList.adapter = adapter
thread(start = true) {
try {
var cd = deserializeStringList2DNSInfoSet(
extras!!.getStringArrayList(MainActivity.DNS_LIST)!!
)
thread(start = true) {
try {
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<com.hbb20.CountryCodePicker>(R.id.ccp)
ccp.setCountryForNameCode(countryCode)
view.findViewById<com.hbb20.CountryCodePicker>(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<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()
}
}
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,9 +233,6 @@ 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)
@ -251,7 +246,6 @@ class MainActivity : AppCompatActivity() {
}
}
}
}
when (resultCode) {
STATUS_START -> {

View File

@ -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<FloatingActionButton>(R.id.fab).setOnClickListener { view ->
findViewById<FloatingActionButton>(R.id.fab).setOnClickListener { _ ->
addNewPeer()
}
var extras = intent.extras
@ -77,6 +78,7 @@ class PeerListActivity : AppCompatActivity() {
var ping = ping(pi.address, pi.port)
pi.ping = ping
}
try {
var json = downloadJson(PEER_LIST_URL)
var countries = CCPCountry.getLibraryMasterCountriesEnglish()
val mapType: Type = object :
@ -94,11 +96,11 @@ class PeerListActivity : AppCompatActivity() {
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
}
var ping = ping(address, url.port)
peerInfo.ping = ping
withContext(Dispatchers.Main) {
adapter.addItem(peerInfo)
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 }))
withContext(Dispatchers.Main) {
adapter.addAll(0, currentPeers)
@ -136,7 +141,7 @@ class PeerListActivity : AppCompatActivity() {
var ipInput = view.findViewById<TextView>(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<com.hbb20.CountryCodePicker>(R.id.ccp)
ccp.setCountryForNameCode(countryCode)
getPopupWindow(R.layout.spinner_item, resources.getStringArray(R.array.schemas), schemaInput);
view.findViewById<com.hbb20.CountryCodePicker>(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<String>,
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

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 ->{
@ -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,6 +271,7 @@ class YggdrasilTunService : VpnService() {
val networks = cm.allNetworks
for (network in networks) {
val linkProperties = cm.getLinkProperties(network)
if(linkProperties!=null) {
val routes = linkProperties.routes
for (route in routes) {
if (route.isDefaultRoute && route.gateway is Inet6Address) {
@ -278,6 +279,7 @@ class YggdrasilTunService : VpnService() {
}
}
}
}
return false
}

View File

@ -23,9 +23,10 @@ class DropDownAdapter(
) :
ArrayAdapter<String?>(context, textViewResourceId, objects), OnItemClickListener {
private val objects: Array<String>
private val popup: PopupWindow
private val editText: TextView
private val objects: Array<String> = 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
}
}

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()

View File

@ -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;