From 19a73f9c9cabb4f699f9497f1c9a8228ea154fea Mon Sep 17 00:00:00 2001 From: vadym Date: Mon, 29 Jun 2020 12:30:25 -0700 Subject: [PATCH] 1. removed wifi direct code --- app/src/main/AndroidManifest.xml | 4 - .../chronosx88/yggdrasil/MainActivity.kt | 135 ------------------ .../yggdrasil/YggdrasilTunService.kt | 28 ++-- 3 files changed, 14 insertions(+), 153 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 9840d0f..6d4dfb6 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -3,11 +3,7 @@ xmlns:tools="http://schemas.android.com/tools" package="io.github.chronosx88.yggdrasil"> - - - - () private var currentDNS = setOf() private val wirelessPeers = mutableListOf() private val intentFilter = IntentFilter() - private val peerListListener = WirelessPeerList() - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(findViewById(R.id.toolbar)) - /*p2p part*/ - // Indicates a change in the Wi-Fi P2P status. - intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION); - // Indicates a change in the list of available peers. - intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION); - // Indicates the state of Wi-Fi P2P connectivity has changed. - intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION); - // Indicates this device's details have changed. - intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION); - - isStarted = isYggServiceRunning(this) val switchOn = findViewById(R.id.switchOn) switchOn.isChecked = isStarted @@ -107,35 +87,6 @@ class MainActivity : AppCompatActivity() { wifiDirect.setOnCheckedChangeListener { _, isChecked -> - if (ActivityCompat.checkSelfPermission( - this, - Manifest.permission.ACCESS_FINE_LOCATION - ) != PackageManager.PERMISSION_GRANTED - ) { - // TODO: Consider calling - // ActivityCompat#requestPermissions - // here to request the missing permissions, and then overriding - // public void onRequestPermissionsResult(int requestCode, String[] permissions, - // int[] grantResults) - // to handle the case where the user grants the permission. See the documentation - // for ActivityCompat#requestPermissions for more details. - return@setOnCheckedChangeListener - } - mManager!!.discoverPeers(mChannel, object : WifiP2pManager.ActionListener { - override fun onSuccess() { - // Code for when the discovery initiation is successful goes here. - // No services have actually been discovered yet, so this method - // can often be left blank. Code for peer discovery goes in the - // onReceive method, detailed below. - //showToast("discover peers success") - } - - override fun onFailure(reasonCode: Int) { - // Code for when the discovery initiation fails goes here. - // Alert the user that something went wrong. - showToast("discover peers failed, code="+reasonCode) - } - }) } @@ -331,90 +282,4 @@ class MainActivity : AppCompatActivity() { return false } - inner class WiFiDirectBroadcastReceiver(mManager: WifiP2pManager, - mChannel: WifiP2pManager.Channel): BroadcastReceiver() { - override fun onReceive(context: Context?, intent: Intent?) { - val action = intent!!.action - if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION == action) { - // Determine if Wifi P2P mode is enabled or not, alert - // the Activity. - val state = intent!!.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1) - if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) { - this@MainActivity.setIsWifiP2pEnabled(true) - } else { - this@MainActivity.setIsWifiP2pEnabled(false) - } - } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION == action) { - // The peer list has changed! We should probably do something about - // that. - // Request available peers from the wifi p2p manager. This is an - // asynchronous call and the calling activity is notified with a - // callback on PeerListListener.onPeersAvailable() - if (mManager != null) { - if (ActivityCompat.checkSelfPermission( - this@MainActivity, - Manifest.permission.ACCESS_FINE_LOCATION - ) != PackageManager.PERMISSION_GRANTED - ) { - // TODO: Consider calling - // ActivityCompat#requestPermissions - // here to request the missing permissions, and then overriding - // public void onRequestPermissionsResult(int requestCode, String[] permissions, - // int[] grantResults) - // to handle the case where the user grants the permission. See the documentation - // for ActivityCompat#requestPermissions for more details. - return - } - mManager!!.requestPeers(mChannel, peerListListener); - } - Log.d(TAG, "P2P peers changed"); - } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION == action) { - - // Connection state changed! We should probably do something about - // that. - } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION == action) { - this@MainActivity.updateThisDevice( - intent!!.getParcelableExtra( - WifiP2pManager.EXTRA_WIFI_P2P_DEVICE - ) as WifiP2pDevice - ) - } - } - } - - private fun updateThisDevice(wifiP2pDevice: WifiP2pDevice) { - //showToast("update device:"+wifiP2pDevice.deviceName+" address:"+wifiP2pDevice.deviceAddress) - } - - private fun setIsWifiP2pEnabled(b: Boolean) { - //showToast("WifiP2pEnabled="+b) - } - - /** register the BroadcastReceiver with the intent values to be matched */ - override fun onResume() { - super.onResume() - mManager = getSystemService(Context.WIFI_P2P_SERVICE) as WifiP2pManager - mChannel = mManager!!.initialize(this, mainLooper, null); - receiver = WiFiDirectBroadcastReceiver(mManager!!, mChannel!!) - registerReceiver(receiver, intentFilter) - } - - override fun onPause() { - super.onPause() - unregisterReceiver(receiver) - } - - inner class WirelessPeerList:WifiP2pManager.PeerListListener{ - - override fun onPeersAvailable(peers: WifiP2pDeviceList?) { - // Out with the old, in with the new. - this@MainActivity.wirelessPeers.clear() - this@MainActivity.wirelessPeers.addAll(peers!!.deviceList); - // If an AdapterView is backed by this data, notify it // of the change. For instance, if you have a ListView of available // peers, trigger an update. - //((WiFiPeerListAdapter) getListAdapter()).notifyDataSetChanged(); if (peers.size() == 0) { Log.d(WiFiDirectActivity.TAG, "No devices found"); return; } } - //just show message - //showToast("available peers:"+this@MainActivity.wirelessPeers.size) - } - - } } 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 523107e..d1d8f24 100644 --- a/app/src/main/java/io/github/chronosx88/yggdrasil/YggdrasilTunService.kt +++ b/app/src/main/java/io/github/chronosx88/yggdrasil/YggdrasilTunService.kt @@ -22,6 +22,7 @@ import mobile.Yggdrasil import java.io.* import java.net.Inet6Address import java.nio.ByteBuffer +import kotlin.concurrent.thread class YggdrasilTunService : VpnService() { @@ -29,8 +30,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. */ - private val MAX_PACKET_SIZE = Short.MAX_VALUE.toInt() + /** Maximum packet size is constrained by the MTU, which is given as a signed short - 256 */ + private val MAX_PACKET_SIZE = Short.MAX_VALUE-256 companion object { private const val TAG = "Yggdrasil-service" @@ -159,17 +160,15 @@ class YggdrasilTunService : VpnService() { } private fun readPacketsFromTun(yggConduitEndpoint: ConduitEndpoint, buffer: ByteArray) { - // Read the outgoing packet from the input stream. - try{ - val length = tunInputStream!!.read(buffer) - if (length > 0) { - val byteBuffer = ByteBuffer.allocate(length) - byteBuffer.put(buffer, 0, length) - yggConduitEndpoint.send(byteBuffer.array()) + try { + // Read the outgoing packet from the input stream. + val length = tunInputStream?.read(buffer) ?: 1 + if (length > 0){ + yggConduitEndpoint.send(buffer.sliceArray(IntRange(0, length - 1))) } else { - Thread.sleep(10) + Thread.sleep(100) } - }catch(e: IOException){ + } catch (e: IOException) { e.printStackTrace() } } @@ -178,8 +177,8 @@ class YggdrasilTunService : VpnService() { val buffer = yggConduitEndpoint.recv() if(buffer!=null) { try { - tunOutputStream!!.write(buffer) - }catch(e: IOException){ + tunOutputStream?.write(buffer) + } catch (e: IOException) { e.printStackTrace() } } @@ -187,11 +186,12 @@ 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()