Skip to content

Commit 2b0f3d0

Browse files
committed
feat: part 2 - openvpn android
1 parent 0a8d21a commit 2b0f3d0

File tree

1 file changed

+86
-1
lines changed

1 file changed

+86
-1
lines changed

android/src/main/java/com/openvpn/OpenvpnModule.kt

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,63 @@
11
package com.openvpn
22

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
310
import com.facebook.react.bridge.Promise
411
import com.facebook.react.bridge.ReactApplicationContext
512
import com.facebook.react.bridge.ReadableMap
613
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+
724

825
@ReactModule(name = OpenvpnModule.NAME)
926
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+
1161

1262
override fun getName(): String {
1363
return NAME
@@ -22,7 +72,28 @@ class OpenvpnModule(reactContext: ReactApplicationContext) :
2272
override fun onPrepared(promise: Promise?) {
2373
}
2474

75+
/**
76+
* --> Here we skip LanchVPN activity and directly use ProfileManager + VPNLaunchHelper
77+
* and start the VPN.
78+
*/
2579
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+
2697
}
2798

2899
override fun getCurrentState(promise: Promise?) {
@@ -39,4 +110,18 @@ class OpenvpnModule(reactContext: ReactApplicationContext) :
39110
companion object {
40111
const val NAME = "Openvpn"
41112
}
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+
}
42127
}

0 commit comments

Comments
 (0)