@@ -44,6 +44,8 @@ class WireguardDartPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,
44
44
private var activity: Activity ? = null
45
45
private var config: com.wireguard.config.Config ? = null
46
46
private var tunnel: WireguardTunnel ? = null
47
+ private var tunnelName: String? = null
48
+ private var permissionsResultCallback: Result ? = null
47
49
private var status: ConnectionStatus = ConnectionStatus .disconnected
48
50
set(value) {
49
51
field = value
@@ -59,6 +61,16 @@ class WireguardDartPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,
59
61
override fun onActivityResult (requestCode : Int , resultCode : Int , data : Intent ? ): Boolean {
60
62
if (requestCode == PERMISSIONS_REQUEST_CODE ) {
61
63
havePermission = resultCode == Activity .RESULT_OK
64
+ if (havePermission) {
65
+ permissionsResultCallback?.success(null )
66
+ } else {
67
+ permissionsResultCallback?.error(
68
+ " err_setup_tunnel" ,
69
+ " Permissions are not given" ,
70
+ null
71
+ )
72
+
73
+ }
62
74
}
63
75
return havePermission
64
76
}
@@ -128,13 +140,14 @@ class WireguardDartPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,
128
140
" nativeInit" -> result.success(" " )
129
141
" generateKeyPair" -> generateKeyPair(result)
130
142
" setupTunnel" -> setupTunnel(
131
- call.argument<String >(" bundleId" ).toString(),
132
143
call.argument<String >(" tunnelName" ).toString(),
133
144
result
134
145
)
146
+
135
147
" checkTunnelConfiguration" -> {
136
- checkTunnelConfiguration(result)
148
+ checkTunnelConfiguration(call.argument< String >( " tunnelName " ).toString(), result)
137
149
}
150
+
138
151
" connect" -> connect(call.argument<String >(" cfg" ).toString(), result)
139
152
" disconnect" -> disconnect(result)
140
153
" status" -> status(result)
@@ -143,9 +156,12 @@ class WireguardDartPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,
143
156
}
144
157
}
145
158
146
- private fun checkTunnelConfiguration (result : MethodChannel . Result ) {
159
+ private fun checkTunnelConfiguration (tunnelName : String , result : Result ) {
147
160
val intent = GoBackend .VpnService .prepare(this .activity)
148
161
havePermission = intent == null
162
+ if (havePermission) {
163
+ initTunnel(tunnelName)
164
+ }
149
165
return result.success(havePermission)
150
166
}
151
167
@@ -159,21 +175,27 @@ class WireguardDartPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,
159
175
)
160
176
}
161
177
162
- private fun setupTunnel (bundleId : String , tunnelName : String , result : Result ) {
178
+ private fun setupTunnel (tunnelName : String , result : Result ) {
163
179
scope.launch(Dispatchers .IO ) {
164
180
if (Tunnel .isNameInvalid(tunnelName)) {
165
181
flutterError(result, " Tunnel name is invalid" )
166
182
return @launch
167
183
}
184
+ permissionsResultCallback = result
168
185
checkPermission()
169
- tunnel = WireguardTunnel (tunnelName) { state ->
170
- status = ConnectionStatus .fromTunnelState(state)
171
- }
172
- status = ConnectionStatus .fromTunnelState(backend?.getState(tunnel!! ))
173
- result.success(null )
186
+ initTunnel(tunnelName)
174
187
}
175
188
}
176
189
190
+ private fun initTunnel (tunnelName : String ) {
191
+ this .tunnelName = tunnelName
192
+ tunnel = WireguardTunnel (tunnelName) { state ->
193
+ status = ConnectionStatus .fromTunnelState(state)
194
+ }
195
+ status = ConnectionStatus .fromTunnelState(backend?.getState(tunnel!! ))
196
+ }
197
+
198
+
177
199
private fun connect (cfg : String , result : Result ) {
178
200
val tun = tunnel ? : run {
179
201
result.error(" err_setup_tunnel" , " Tunnel is not initialized" , null )
@@ -188,7 +210,7 @@ class WireguardDartPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,
188
210
}
189
211
val inputStream = ByteArrayInputStream (cfg.toByteArray())
190
212
config = com.wireguard.config.Config .parse(inputStream)
191
- futureBackend.await().setState(tun, Tunnel .State .UP , config);
213
+ futureBackend.await().setState(tun, Tunnel .State .UP , config)
192
214
flutterSuccess(result, " " )
193
215
} catch (e: Throwable ) {
194
216
Log .e(TAG , " Connect - Can't connect to tunnel: $e " , e)
0 commit comments