@@ -5,16 +5,15 @@ import NetworkExtension
5
5
public class SwiftOpenVPNFlutterPlugin : NSObject , FlutterPlugin {
6
6
private static var utils : VPNUtils ! = VPNUtils ( )
7
7
8
- private static var EVENT_CHANNEL_VPN_STAGE : String = " id.laskarmedia.openvpn_flutter/vpnstage "
9
- private static var METHOD_CHANNEL_VPN_CONTROL : String = " id.laskarmedia.openvpn_flutter/vpncontrol "
8
+ private static var EVENT_CHANNEL_VPN_STAGE = " id.laskarmedia.openvpn_flutter/vpnstage "
9
+ private static var METHOD_CHANNEL_VPN_CONTROL = " id.laskarmedia.openvpn_flutter/vpncontrol "
10
10
11
- public static var stage : FlutterEventSink ?
11
+ public static var stage : FlutterEventSink ?
12
12
private var initialized : Bool = false
13
13
14
14
public static func register( with registrar: FlutterPluginRegistrar ) {
15
15
let instance = SwiftOpenVPNFlutterPlugin ( )
16
16
instance. onRegister ( registrar)
17
-
18
17
}
19
18
20
19
public func onRegister( _ registrar: FlutterPluginRegistrar ) {
@@ -23,7 +22,7 @@ public class SwiftOpenVPNFlutterPlugin: NSObject, FlutterPlugin {
23
22
24
23
vpnStageE. setStreamHandler ( StageHandler ( ) )
25
24
vpnControlM. setMethodCallHandler ( { ( call: FlutterMethodCall , result: @escaping FlutterResult ) -> Void in
26
- switch call. method{
25
+ switch call. method {
27
26
case " status " :
28
27
SwiftOpenVPNFlutterPlugin . utils. getTraffictStats ( )
29
28
result ( UserDefaults . init ( suiteName: SwiftOpenVPNFlutterPlugin . utils. groupIdentifier) ? . string ( forKey: " connectionUpdate " ) )
@@ -36,15 +35,21 @@ public class SwiftOpenVPNFlutterPlugin: NSObject, FlutterPlugin {
36
35
let localizedDescription : String ? = ( call. arguments as? [ String : Any ] ) ? [ " localizedDescription " ] as? String
37
36
let groupIdentifier : String ? = ( call. arguments as? [ String : Any ] ) ? [ " groupIdentifier " ] as? String
38
37
if providerBundleIdentifier == nil {
39
- result ( FlutterError ( code: " -2 " , message: " providerBundleIdentifier content empty or null " , details: nil ) ) ;
38
+ result ( FlutterError ( code: " -2 " ,
39
+ message: " providerBundleIdentifier content empty or null " ,
40
+ details: nil ) ) ;
40
41
return ;
41
42
}
42
43
if localizedDescription == nil {
43
- result ( FlutterError ( code: " -3 " , message: " localizedDescription content empty or null " , details: nil ) ) ;
44
+ result ( FlutterError ( code: " -3 " ,
45
+ message: " localizedDescription content empty or null " ,
46
+ details: nil ) ) ;
44
47
return ;
45
48
}
46
49
if groupIdentifier == nil {
47
- result ( FlutterError ( code: " -4 " , message: " groupIdentifier content empty or null " , details: nil ) ) ;
50
+ result ( FlutterError ( code: " -4 " ,
51
+ message: " groupIdentifier content empty or null " ,
52
+ details: nil ) ) ;
48
53
return ;
49
54
}
50
55
SwiftOpenVPNFlutterPlugin . utils. groupIdentifier = groupIdentifier
@@ -54,7 +59,7 @@ public class SwiftOpenVPNFlutterPlugin: NSObject, FlutterPlugin {
54
59
if err == nil {
55
60
result ( SwiftOpenVPNFlutterPlugin . utils. currentStatus ( ) )
56
61
} else {
57
- result ( FlutterError ( code: " -4 " , message: err. debugDescription , details: err? . localizedDescription) ) ;
62
+ result ( FlutterError ( code: " -4 " , message: err? . localizedDescription , details: err? . localizedDescription) ) ;
58
63
}
59
64
}
60
65
self . initialized = true
@@ -64,28 +69,34 @@ public class SwiftOpenVPNFlutterPlugin: NSObject, FlutterPlugin {
64
69
break ;
65
70
case " connect " :
66
71
if !self . initialized {
67
- result ( FlutterError ( code: " -1 " , message: " VPNEngine need to be initialize " , details: nil ) ) ;
72
+ result ( FlutterError ( code: " -1 " ,
73
+ message: " VPNEngine need to be initialize " ,
74
+ details: nil ) ) ;
68
75
}
69
76
let config : String ? = ( call. arguments as? [ String : Any ] ) ? [ " config " ] as? String
70
77
let username : String ? = ( call. arguments as? [ String : Any ] ) ? [ " username " ] as? String
71
78
let password : String ? = ( call. arguments as? [ String : Any ] ) ? [ " password " ] as? String
72
79
if config == nil {
73
- result ( FlutterError ( code: " -2 " , message: " Config is empty or nulled " , details: " Config can't be nulled " ) )
80
+ result ( FlutterError ( code: " -2 " ,
81
+ message: " Config is empty or nulled " ,
82
+ details: " Config can't be nulled " ) )
74
83
return
75
84
}
85
+
76
86
SwiftOpenVPNFlutterPlugin . utils. configureVPN ( config: config, username: username, password: password, completion: { ( success: Error ? ) -> Void in
77
87
if ( success == nil ) {
78
88
result ( nil )
79
89
} else {
80
- result ( FlutterError ( code: " -5 " , message: success? . localizedDescription, details: success. debugDescription) )
90
+ result ( FlutterError ( code: " 99 " ,
91
+ message: " permission denied " ,
92
+ details: success? . localizedDescription) )
81
93
}
82
94
} )
83
95
break ;
84
96
case " dispose " :
85
97
self . initialized = false
86
98
default :
87
99
break ;
88
-
89
100
}
90
101
} )
91
102
}
@@ -181,7 +192,6 @@ class VPNUtils {
181
192
else {
182
193
return " disconnected "
183
194
}
184
- // return "DISCONNECTED"
185
195
}
186
196
187
197
func configureVPN( config: String ? , username : String ? , password : String ? , completion: @escaping ( _ error : Error ? ) -> Void ) {
@@ -211,9 +221,13 @@ class VPNUtils {
211
221
}
212
222
do {
213
223
if self . vpnStageObserver != nil {
214
- NotificationCenter . default. removeObserver ( self . vpnStageObserver!, name: NSNotification . Name. NEVPNStatusDidChange, object: nil )
224
+ NotificationCenter . default. removeObserver ( self . vpnStageObserver!,
225
+ name: NSNotification . Name. NEVPNStatusDidChange,
226
+ object: nil )
215
227
}
216
- self . vpnStageObserver = NotificationCenter . default. addObserver ( forName: NSNotification . Name. NEVPNStatusDidChange, object: nil , queue: nil ) { [ weak self] notification in
228
+ self . vpnStageObserver = NotificationCenter . default. addObserver ( forName: NSNotification . Name. NEVPNStatusDidChange,
229
+ object: nil ,
230
+ queue: nil ) { [ weak self] notification in
217
231
let nevpnconn = notification. object as! NEVPNConnection
218
232
let status = nevpnconn. status
219
233
self ? . onVpnStatusChanged ( notification: status)
@@ -235,6 +249,8 @@ class VPNUtils {
235
249
completion ( error) ;
236
250
}
237
251
} )
252
+ } else {
253
+ completion ( error) ;
238
254
}
239
255
} )
240
256
}
0 commit comments