forked from core/mobile_nebula
Improvements to NebulaVpnService (#79)
This commit is contained in:
parent
7bf9ee553e
commit
552d16bce2
|
@ -195,16 +195,11 @@ class MainActivity: FlutterActivity() {
|
||||||
|
|
||||||
private fun stopSite() {
|
private fun stopSite() {
|
||||||
val intent = Intent(this, NebulaVpnService::class.java)
|
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
|
// We can't stopService because we have to close the fd first. The service will call stopSelf when ready.
|
||||||
// according to the official example https://android.googlesource.com/platform/development/+/master/samples/ToyVpn/src/com/example/android/toyvpn/ToyVpnClient.java#116
|
// See the official example: https://android.googlesource.com/platform/development/+/master/samples/ToyVpn/src/com/example/android/toyvpn/ToyVpnClient.java#116
|
||||||
startService(intent)
|
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) {
|
private fun activeListHostmap(call: MethodCall, result: MethodChannel.Result) {
|
||||||
|
|
|
@ -18,6 +18,7 @@ class NebulaVpnService : VpnService() {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val TAG = "NebulaVpnService"
|
private const val TAG = "NebulaVpnService"
|
||||||
|
const val ACTION_STOP = "net.defined.mobile_nebula.STOP"
|
||||||
const val MSG_REGISTER_CLIENT = 1
|
const val MSG_REGISTER_CLIENT = 1
|
||||||
const val MSG_UNREGISTER_CLIENT = 2
|
const val MSG_UNREGISTER_CLIENT = 2
|
||||||
const val MSG_IS_RUNNING = 3
|
const val MSG_IS_RUNNING = 3
|
||||||
|
@ -40,10 +41,10 @@ class NebulaVpnService : VpnService() {
|
||||||
private var nebula: mobileNebula.Nebula? = null
|
private var nebula: mobileNebula.Nebula? = null
|
||||||
private var vpnInterface: ParcelFileDescriptor? = null
|
private var vpnInterface: ParcelFileDescriptor? = null
|
||||||
private var didSleep = false
|
private var didSleep = false
|
||||||
private var networkCallback: NetworkCallback = NetworkCallback();
|
private var networkCallback: NetworkCallback = NetworkCallback()
|
||||||
|
|
||||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
if (intent?.getStringExtra("COMMAND") == "STOP") {
|
if (intent?.getAction() == ACTION_STOP) {
|
||||||
stopVpn()
|
stopVpn()
|
||||||
return Service.START_NOT_STICKY
|
return Service.START_NOT_STICKY
|
||||||
}
|
}
|
||||||
|
@ -103,7 +104,7 @@ class NebulaVpnService : VpnService() {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
vpnInterface = builder.establish()
|
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) {
|
} catch (e: Exception) {
|
||||||
Log.e(TAG, "Got an error $e")
|
Log.e(TAG, "Got an error $e")
|
||||||
|
@ -170,14 +171,25 @@ class NebulaVpnService : VpnService() {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun stopVpn() {
|
private fun stopVpn() {
|
||||||
|
if (nebula == null) {
|
||||||
|
return stopSelf()
|
||||||
|
}
|
||||||
|
|
||||||
unregisterNetworkCallback()
|
unregisterNetworkCallback()
|
||||||
nebula?.stop()
|
nebula?.stop()
|
||||||
vpnInterface?.close()
|
nebula = null
|
||||||
running = false
|
running = false
|
||||||
announceExit(site?.id, null)
|
announceExit(site?.id, null)
|
||||||
|
stopSelf()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onRevoke() {
|
||||||
|
stopVpn()
|
||||||
|
//TODO: wait for the thread to exit
|
||||||
|
super.onRevoke()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
stopVpn()
|
stopVpn()
|
||||||
//TODO: wait for the thread to exit
|
//TODO: wait for the thread to exit
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
|
|
Loading…
Reference in New Issue