Improvements to NebulaVpnService (#79)

This commit is contained in:
John Maguire 2022-10-31 14:49:38 -04:00 committed by GitHub
parent 7bf9ee553e
commit 552d16bce2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 17 deletions

View File

@ -195,16 +195,11 @@ class MainActivity: FlutterActivity() {
private fun stopSite() {
val intent = Intent(this, NebulaVpnService::class.java)
intent.putExtra("COMMAND", "STOP")
intent.setAction(NebulaVpnService.ACTION_STOP)
//This is odd but stopService goes nowhere in my tests and this is correct
// according to the official example https://android.googlesource.com/platform/development/+/master/samples/ToyVpn/src/com/example/android/toyvpn/ToyVpnClient.java#116
// We can't stopService because we have to close the fd first. The service will call stopSelf when ready.
// See the official example: https://android.googlesource.com/platform/development/+/master/samples/ToyVpn/src/com/example/android/toyvpn/ToyVpnClient.java#116
startService(intent)
//TODO: why doesn't this work!?!?
// if (serviceIntent != null) {
// Log.e(TAG, "stopping ${serviceIntent.toString()}")
// stopService(serviceIntent)
// }
}
private fun activeListHostmap(call: MethodCall, result: MethodChannel.Result) {

View File

@ -18,6 +18,7 @@ class NebulaVpnService : VpnService() {
companion object {
private const val TAG = "NebulaVpnService"
const val ACTION_STOP = "net.defined.mobile_nebula.STOP"
const val MSG_REGISTER_CLIENT = 1
const val MSG_UNREGISTER_CLIENT = 2
const val MSG_IS_RUNNING = 3
@ -40,10 +41,10 @@ class NebulaVpnService : VpnService() {
private var nebula: mobileNebula.Nebula? = null
private var vpnInterface: ParcelFileDescriptor? = null
private var didSleep = false
private var networkCallback: NetworkCallback = NetworkCallback();
private var networkCallback: NetworkCallback = NetworkCallback()
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (intent?.getStringExtra("COMMAND") == "STOP") {
if (intent?.getAction() == ACTION_STOP) {
stopVpn()
return Service.START_NOT_STICKY
}
@ -103,7 +104,7 @@ class NebulaVpnService : VpnService() {
try {
vpnInterface = builder.establish()
nebula = mobileNebula.MobileNebula.newNebula(site!!.config, site!!.getKey(this), site!!.logFile, vpnInterface!!.fd.toLong())
nebula = mobileNebula.MobileNebula.newNebula(site!!.config, site!!.getKey(this), site!!.logFile, vpnInterface!!.detachFd().toLong())
} catch (e: Exception) {
Log.e(TAG, "Got an error $e")
@ -170,11 +171,22 @@ class NebulaVpnService : VpnService() {
}
private fun stopVpn() {
if (nebula == null) {
return stopSelf()
}
unregisterNetworkCallback()
nebula?.stop()
vpnInterface?.close()
nebula = null
running = false
announceExit(site?.id, null)
stopSelf()
}
override fun onRevoke() {
stopVpn()
//TODO: wait for the thread to exit
super.onRevoke()
}
override fun onDestroy() {