1
1
package com.openvpn
2
2
3
+ import android.content.ComponentName
4
+ import android.content.Context
5
+ import android.content.Intent
6
+ import android.content.ServiceConnection
7
+ import android.graphics.Bitmap.Config
8
+ import android.os.Handler
9
+ import android.os.IBinder
3
10
import com.facebook.react.bridge.Promise
4
11
import com.facebook.react.bridge.ReactApplicationContext
5
12
import com.facebook.react.bridge.ReadableMap
6
13
import com.facebook.react.module.annotations.ReactModule
14
+ import de.blinkt.openvpn.VpnProfile
15
+ import de.blinkt.openvpn.core.ConfigParser
16
+ import de.blinkt.openvpn.core.ConnectionStatus
17
+ import de.blinkt.openvpn.core.IOpenVPNServiceInternal
18
+ import de.blinkt.openvpn.core.OpenVPNService
19
+ import de.blinkt.openvpn.core.ProfileManager
20
+ import de.blinkt.openvpn.core.VPNLaunchHelper
21
+ import de.blinkt.openvpn.core.VpnStatus
22
+ import java.io.StringReader
23
+
7
24
8
25
@ReactModule(name = OpenvpnModule .NAME )
9
26
class OpenvpnModule (reactContext : ReactApplicationContext ) :
10
- NativeOpenvpnSpec (reactContext) {
27
+ NativeOpenvpnSpec (reactContext), VpnStatus .StateListener {
28
+
29
+ /* *
30
+ * Class for interacting with the main interface of the service.
31
+ */
32
+
33
+
34
+ private var mHandler: Handler ? = null
35
+ private var mService: IOpenVPNServiceInternal ? = null
36
+
37
+ private val mConnection: ServiceConnection = object : ServiceConnection {
38
+ override fun onServiceConnected (className : ComponentName , service : IBinder ) {
39
+ mService = (service) as IOpenVPNServiceInternal
40
+ }
41
+
42
+ override fun onServiceDisconnected (arg0 : ComponentName ) {
43
+ mService = null
44
+ }
45
+ }
46
+
47
+
48
+
49
+ private fun bindService () {
50
+ val intent = Intent (reactApplicationContext, OpenVPNService ::class .java)
51
+ intent.setAction(OpenVPNService .START_SERVICE )
52
+ reactApplicationContext.bindService(intent, mConnection, Context .BIND_AUTO_CREATE )
53
+ }
54
+
55
+
56
+ init {
57
+ bindService();
58
+
59
+ }
60
+
11
61
12
62
override fun getName (): String {
13
63
return NAME
@@ -22,7 +72,28 @@ class OpenvpnModule(reactContext: ReactApplicationContext) :
22
72
override fun onPrepared (promise : Promise ? ) {
23
73
}
24
74
75
+ /* *
76
+ * --> Here we skip LanchVPN activity and directly use ProfileManager + VPNLaunchHelper
77
+ * and start the VPN.
78
+ */
25
79
override fun connect (params : ReadableMap ? , promise : Promise ? ) {
80
+
81
+ val address: String = params?.getString(" address" ).toString()
82
+ val username: String = params?.getString(" username" ).toString()
83
+ val password: String = params?.getString(" password" ).toString()
84
+
85
+ val openVPNConfig: String = params?.getString(" openVPNConfig" ).toString()
86
+ val openVPNConfigLocalFile: String = params?.getString(" openVPNConfigLocalFile" ).toString()
87
+
88
+
89
+ val cParser: ConfigParser = ConfigParser ()
90
+
91
+
92
+ cParser.parseConfig(StringReader (' s' .toString()));
93
+ var openProfile: VpnProfile = cParser.convertProfile()
94
+
95
+
96
+
26
97
}
27
98
28
99
override fun getCurrentState (promise : Promise ? ) {
@@ -39,4 +110,18 @@ class OpenvpnModule(reactContext: ReactApplicationContext) :
39
110
companion object {
40
111
const val NAME = " Openvpn"
41
112
}
113
+
114
+ override fun updateState (
115
+ state : String? ,
116
+ logmessage : String? ,
117
+ localizedResId : Int ,
118
+ level : ConnectionStatus ? ,
119
+ Intent : Intent ?
120
+ ) {
121
+ TODO (" Not yet implemented" )
122
+ }
123
+
124
+ override fun setConnectedVPN (uuid : String? ) {
125
+ TODO (" Not yet implemented" )
126
+ }
42
127
}
0 commit comments