Skip to content

Commit

Permalink
Merge pull request #50 from mysteriumnetwork/refactor/init-tunnel
Browse files Browse the repository at this point in the history
Init Android tunnel + Permissions request
  • Loading branch information
KristijanMitrik authored Jan 9, 2025
2 parents 77d87c7 + 41a1347 commit e0fa625
Showing 1 changed file with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class WireguardDartPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,
private var activity: Activity? = null
private var config: com.wireguard.config.Config? = null
private var tunnel: WireguardTunnel? = null
private var tunnelName: String? = null
private var permissionsResultCallback: Result? = null
private var status: ConnectionStatus = ConnectionStatus.disconnected
set(value) {
field = value
Expand All @@ -59,6 +61,16 @@ class WireguardDartPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?): Boolean {
if (requestCode == PERMISSIONS_REQUEST_CODE) {
havePermission = resultCode == Activity.RESULT_OK
if (havePermission) {
permissionsResultCallback?.success(null)
} else {
permissionsResultCallback?.error(
"err_setup_tunnel",
"Permissions are not given",
null
)

}
}
return havePermission
}
Expand Down Expand Up @@ -128,13 +140,14 @@ class WireguardDartPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,
"nativeInit" -> result.success("")
"generateKeyPair" -> generateKeyPair(result)
"setupTunnel" -> setupTunnel(
call.argument<String>("bundleId").toString(),
call.argument<String>("tunnelName").toString(),
result
)

"checkTunnelConfiguration" -> {
checkTunnelConfiguration(result)
checkTunnelConfiguration(call.argument<String>("tunnelName").toString(), result)
}

"connect" -> connect(call.argument<String>("cfg").toString(), result)
"disconnect" -> disconnect(result)
"status" -> status(result)
Expand All @@ -143,9 +156,12 @@ class WireguardDartPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,
}
}

private fun checkTunnelConfiguration(result: MethodChannel.Result) {
private fun checkTunnelConfiguration(tunnelName: String, result: Result) {
val intent = GoBackend.VpnService.prepare(this.activity)
havePermission = intent == null
if (havePermission) {
initTunnel(tunnelName)
}
return result.success(havePermission)
}

Expand All @@ -159,21 +175,27 @@ class WireguardDartPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,
)
}

private fun setupTunnel(bundleId: String, tunnelName: String, result: Result) {
private fun setupTunnel(tunnelName: String, result: Result) {
scope.launch(Dispatchers.IO) {
if (Tunnel.isNameInvalid(tunnelName)) {
flutterError(result, "Tunnel name is invalid")
return@launch
}
permissionsResultCallback = result
checkPermission()
tunnel = WireguardTunnel(tunnelName) { state ->
status = ConnectionStatus.fromTunnelState(state)
}
status = ConnectionStatus.fromTunnelState(backend?.getState(tunnel!!))
result.success(null)
initTunnel(tunnelName)
}
}

private fun initTunnel(tunnelName: String) {
this.tunnelName = tunnelName
tunnel = WireguardTunnel(tunnelName) { state ->
status = ConnectionStatus.fromTunnelState(state)
}
status = ConnectionStatus.fromTunnelState(backend?.getState(tunnel!!))
}


private fun connect(cfg: String, result: Result) {
val tun = tunnel ?: run {
result.error("err_setup_tunnel", "Tunnel is not initialized", null)
Expand All @@ -188,7 +210,7 @@ class WireguardDartPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,
}
val inputStream = ByteArrayInputStream(cfg.toByteArray())
config = com.wireguard.config.Config.parse(inputStream)
futureBackend.await().setState(tun, Tunnel.State.UP, config);
futureBackend.await().setState(tun, Tunnel.State.UP, config)
flutterSuccess(result, "")
} catch (e: Throwable) {
Log.e(TAG, "Connect - Can't connect to tunnel: $e", e)
Expand Down

0 comments on commit e0fa625

Please sign in to comment.