OpenVPN over Cloak for Android and iOS (#158)
OpenVPN over Cloak for Android and iOS
This commit is contained in:
parent
7f02fe4157
commit
780efc2477
94 changed files with 3212 additions and 1287 deletions
|
|
@ -14,6 +14,7 @@ import com.wireguard.android.util.SharedLibraryLoader
|
|||
import com.wireguard.config.*
|
||||
import com.wireguard.crypto.Key
|
||||
import org.json.JSONObject
|
||||
import java.util.Base64
|
||||
|
||||
import net.openvpn.ovpn3.ClientAPI_Config
|
||||
import net.openvpn.ovpn3.ClientAPI_EvalConfig
|
||||
|
|
@ -25,6 +26,7 @@ import net.openvpn.ovpn3.ClientAPI_OpenVPNClient
|
|||
import net.openvpn.ovpn3.ClientAPI_ProvideCreds
|
||||
import net.openvpn.ovpn3.ClientAPI_Status
|
||||
import net.openvpn.ovpn3.ClientAPI_TransportStats
|
||||
import java.lang.StringBuilder
|
||||
|
||||
class OpenVPNThreadv3(var service: VPNService): ClientAPI_OpenVPNClient(), Runnable {
|
||||
private val tag = "OpenVPNThreadv3"
|
||||
|
|
@ -59,7 +61,48 @@ class OpenVPNThreadv3(var service: VPNService): ClientAPI_OpenVPNClient(), Runna
|
|||
override fun run() {
|
||||
|
||||
val config: ClientAPI_Config = ClientAPI_Config()
|
||||
config.content = mService.getVpnConfig().getJSONObject("openvpn_config_data").getString("config")
|
||||
|
||||
val jsonVpnConfig = mService.getVpnConfig()
|
||||
val ovpnConfig = jsonVpnConfig.getJSONObject("openvpn_config_data").getString("config")
|
||||
|
||||
val resultingConfig = StringBuilder()
|
||||
resultingConfig.append(ovpnConfig)
|
||||
|
||||
if (jsonVpnConfig.getString("protocol") == "cloak") {
|
||||
val cloakConfigJson: JSONObject = jsonVpnConfig.getJSONObject("cloak_config_data")
|
||||
|
||||
if (cloakConfigJson.keySet().contains("NumConn")) {
|
||||
cloakConfigJson.put("NumConn", 1)
|
||||
}
|
||||
|
||||
if (cloakConfigJson.keySet().contains("ProxyMethod")) {
|
||||
cloakConfigJson.put("ProxyMethod", "openvpn")
|
||||
}
|
||||
|
||||
if (cloakConfigJson.keySet().contains("port")) {
|
||||
val portValue = cloakConfigJson.get("port")
|
||||
cloakConfigJson.remove("port")
|
||||
cloakConfigJson.put("RemotePort", portValue)
|
||||
}
|
||||
|
||||
if (cloakConfigJson.keySet().contains("remote")) {
|
||||
val hostValue = cloakConfigJson.get("remote")
|
||||
cloakConfigJson.remove("remote")
|
||||
cloakConfigJson.put("RemoteHost", hostValue)
|
||||
}
|
||||
|
||||
val cloakConfig = Base64.getEncoder().encodeToString(
|
||||
jsonVpnConfig.getJSONObject("cloak_config_data").toString().toByteArray()
|
||||
)
|
||||
|
||||
resultingConfig.append("\n<cloak>\n")
|
||||
resultingConfig.append(cloakConfig)
|
||||
resultingConfig.append("\n</cloak>\n")
|
||||
|
||||
config.setUsePluggableTransports(true)
|
||||
}
|
||||
|
||||
config.content = resultingConfig.toString()
|
||||
|
||||
eval_config(config)
|
||||
|
||||
|
|
@ -67,7 +110,6 @@ class OpenVPNThreadv3(var service: VPNService): ClientAPI_OpenVPNClient(), Runna
|
|||
if (status.getError()) {
|
||||
Log.i(tag, "connect() error: " + status.getError() + ": " + status.getMessage())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
override fun log(arg0: ClientAPI_LogInfo){
|
||||
|
|
|
|||
|
|
@ -176,7 +176,6 @@ class VPNService : BaseVpnService(), LocalDnsService.Interface {
|
|||
}
|
||||
|
||||
override fun onUnbind(intent: Intent?): Boolean {
|
||||
Log.v(tag, "Aman: onUnbind....................")
|
||||
if (!isUp) {
|
||||
// If the Qt Client got closed while we were not connected
|
||||
// we do not need to stay as a foreground service.
|
||||
|
|
@ -185,12 +184,17 @@ class VPNService : BaseVpnService(), LocalDnsService.Interface {
|
|||
return super.onUnbind(intent)
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
turnOff()
|
||||
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
/**
|
||||
* EntryPoint for the Service, gets Called when AndroidController.cpp
|
||||
* calles bindService. Returns the [VPNServiceBinder] so QT can send Requests to it.
|
||||
*/
|
||||
override fun onBind(intent: Intent): IBinder {
|
||||
Log.v(tag, "Aman: onBind....................")
|
||||
|
||||
when (mProtocol) {
|
||||
"shadowsocks" -> {
|
||||
|
|
@ -214,7 +218,6 @@ class VPNService : BaseVpnService(), LocalDnsService.Interface {
|
|||
* or from Booting the device and having "connect on boot" enabled.
|
||||
*/
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
Log.v(tag, "Aman: onStartCommand....................")
|
||||
this.intent = intent
|
||||
this.flags = flags
|
||||
this.startId = startId
|
||||
|
|
@ -278,6 +281,7 @@ class VPNService : BaseVpnService(), LocalDnsService.Interface {
|
|||
var isUp: Boolean = false
|
||||
get() {
|
||||
return when (mProtocol) {
|
||||
"cloak",
|
||||
"openvpn" -> {
|
||||
field
|
||||
}
|
||||
|
|
@ -303,6 +307,7 @@ class VPNService : BaseVpnService(), LocalDnsService.Interface {
|
|||
val deviceIpv4: String = ""
|
||||
|
||||
val status = when (mProtocol) {
|
||||
"cloak",
|
||||
"openvpn" -> {
|
||||
if (mOpenVPNThreadv3 == null) {
|
||||
Status(null, null, null, null)
|
||||
|
|
@ -373,7 +378,9 @@ class VPNService : BaseVpnService(), LocalDnsService.Interface {
|
|||
Log.i(tag, "Config: $mConfig")
|
||||
mProtocol = mConfig!!.getString("protocol")
|
||||
Log.i(tag, "Protocol: $mProtocol")
|
||||
|
||||
when (mProtocol) {
|
||||
"cloak",
|
||||
"openvpn" -> {
|
||||
startOpenVpn()
|
||||
}
|
||||
|
|
@ -447,8 +454,13 @@ class VPNService : BaseVpnService(), LocalDnsService.Interface {
|
|||
fun turnOff() {
|
||||
Log.v(tag, "Aman: turnOff....................")
|
||||
when (mProtocol) {
|
||||
"wireguard" -> wgTurnOff(currentTunnelHandle)
|
||||
"openvpn" -> ovpnTurnOff()
|
||||
"wireguard" -> {
|
||||
wgTurnOff(currentTunnelHandle)
|
||||
}
|
||||
"cloak",
|
||||
"openvpn" -> {
|
||||
ovpnTurnOff()
|
||||
}
|
||||
"shadowsocks" -> {
|
||||
stopRunner(false)
|
||||
stopTest()
|
||||
|
|
@ -457,6 +469,7 @@ class VPNService : BaseVpnService(), LocalDnsService.Interface {
|
|||
Log.e(tag, "No protocol")
|
||||
}
|
||||
}
|
||||
|
||||
currentTunnelHandle = -1
|
||||
stopForeground(true)
|
||||
isUp = false
|
||||
|
|
@ -688,6 +701,10 @@ class VPNService : BaseVpnService(), LocalDnsService.Interface {
|
|||
}
|
||||
|
||||
private fun startOpenVpn() {
|
||||
if (isUp || mOpenVPNThreadv3 != null) {
|
||||
ovpnTurnOff()
|
||||
}
|
||||
|
||||
mOpenVPNThreadv3 = OpenVPNThreadv3(this)
|
||||
|
||||
Thread({
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue