3
0
Fork 0

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

View File

@ -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")
@ -115,7 +116,7 @@ class NebulaVpnService : VpnService() {
registerNetworkCallback() registerNetworkCallback()
//TODO: There is an open discussion around sleep killing tunnels or just changing mobile to tear down stale tunnels //TODO: There is an open discussion around sleep killing tunnels or just changing mobile to tear down stale tunnels
//registerSleep() //registerSleep()
nebula!!.start() nebula!!.start()
running = true running = true
sendSimple(MSG_IS_RUNNING, if (running) 1 else 0) sendSimple(MSG_IS_RUNNING, if (running) 1 else 0)
@ -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()
@ -240,7 +252,7 @@ class NebulaVpnService : VpnService() {
m.data.putString("data", res) m.data.putString("data", res)
msg.replyTo.send(m) msg.replyTo.send(m)
} }
private fun getHostInfo(msg: Message) { private fun getHostInfo(msg: Message) {
if (protect(msg)) { return } if (protect(msg)) { return }
@ -258,7 +270,7 @@ class NebulaVpnService : VpnService() {
m.data.putString("data", res) m.data.putString("data", res)
msg.replyTo.send(m) msg.replyTo.send(m)
} }
private fun closeTunnel(msg: Message) { private fun closeTunnel(msg: Message) {
if (protect(msg)) { return } if (protect(msg)) { return }
@ -300,4 +312,4 @@ class NebulaVpnService : VpnService() {
messenger = Messenger(IncomingHandler(this)) messenger = Messenger(IncomingHandler(this))
return messenger.binder return messenger.binder
} }
} }