Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update common, Proxyguard support #415

Merged
merged 14 commits into from
Feb 23, 2024
Merged
3 changes: 3 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# This will allow us to get the correct file line numbers to the errors
-keepattributes SourceFile,LineNumberTable

-dontwarn com.google.errorprone.annotations.*
-dontwarn org.bouncycastle.jsse.**
-dontwarn org.conscrypt.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ public void testAppSettingsSerialization() throws SerializerService.UnknownForma
Settings settings = new Settings(true, true);
JSONObject jsonObject = _serializerService.serializeAppSettings(settings);
Settings deserializedSettings = _serializerService.deserializeAppSettings(jsonObject);
assertEquals(settings.forceTcp(), deserializedSettings.forceTcp());
assertEquals(settings.preferTcp(), deserializedSettings.preferTcp());
assertEquals(settings.useCustomTabs(), deserializedSettings.useCustomTabs());
settings = new Settings(false, false);
jsonObject = _serializerService.serializeAppSettings(settings);
deserializedSettings = _serializerService.deserializeAppSettings(jsonObject);
assertEquals(settings.forceTcp(), deserializedSettings.forceTcp());
assertEquals(settings.preferTcp(), deserializedSettings.preferTcp());
assertEquals(settings.useCustomTabs(), deserializedSettings.useCustomTabs());
}

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/nl/eduvpn/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
}

is MainViewModel.MainParentAction.ConnectWithConfig -> {
viewModel.parseConfigAndStartConnection(this, parentAction.config, parentAction.forceTCP)
viewModel.parseConfigAndStartConnection(this, parentAction.config, parentAction.preferTcp)
openFragment(ConnectionStatusFragment(), false)
}

Expand Down Expand Up @@ -169,7 +169,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
.setItems(instancesWithNames.map { it.second }.toTypedArray()) { _, which ->
val selectedInstance = instancesWithNames[which]
selectedInstance.first.countryCode?.let { countryCode ->
viewModel.onCountrySelected(cookie, countryCode)
viewModel.onCountrySelected(cookie, selectedInstance.first.baseURI, countryCode)
}
}.show()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ data class ProfileWithoutIdMap(
Profile(
profileId = it.key,
displayName = it.value.displayName,
vpnProtocolList = it.value.supportedProtocols
)
} ?: emptyList()
}
Expand All @@ -32,7 +31,4 @@ data class ProfileWithoutIdMap(
data class ProfileWithoutId(
@SerialName("display_name")
val displayName: TranslatableString,

@SerialName("supported_protocols")
val supportedProtocols: List<Int>,
)
3 changes: 0 additions & 3 deletions app/src/main/java/nl/eduvpn/app/entity/Profile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,4 @@ data class Profile (

@SerialName("display_name")
val displayName: TranslatableString,

@SerialName("vpn_proto_list")
val vpnProtocolList: List<Int>,
) : Parcelable
13 changes: 12 additions & 1 deletion app/src/main/java/nl/eduvpn/app/entity/SerializedVpnConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,16 @@ data class SerializedVpnConfig(
val config: String,
val protocol: Int,
@SerialName("default_gateway")
val defaultGateway: Boolean
val defaultGateway: Boolean,
@SerialName("should_failover")
val shouldFailover: Boolean = false,
val proxy: ProxySettings? = null
)

@Serializable
data class ProxySettings(
@SerialName("source_port")
val sourcePort: Int,
val listen: String,
val peer: String
)
12 changes: 6 additions & 6 deletions app/src/main/java/nl/eduvpn/app/entity/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@
public class Settings {

public static final boolean USE_CUSTOM_TABS_DEFAULT_VALUE = true;
public static final boolean FORCE_TCP_DEFAULT_VALUE = false;
public static final boolean PREFER_TCP_DEFAULT_VALUE = false;

private boolean _useCustomTabs;
private boolean _forceTcp;
private boolean _preferTcp;

public Settings(boolean useCustomTabs, boolean forceTcp) {
public Settings(boolean useCustomTabs, boolean preferTcp) {
_useCustomTabs = useCustomTabs;
_forceTcp = forceTcp;
_preferTcp = preferTcp;
}

public boolean useCustomTabs() {
return _useCustomTabs;
}

public boolean forceTcp() {
return _forceTcp;
public boolean preferTcp() {
return _preferTcp;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ class ConnectionStatusFragment : BaseFragment<FragmentConnectionStatusBinding>()
}
viewModel.timer.observe(viewLifecycleOwner, updateCertExpiryObserver)
val vpnStatusObserver = { vpnStatus: VPNStatus ->
viewModel.notifyVpnStatus(vpnStatus)
binding.connectionStatus.setText(VPNConnectionService.vpnStatusToStringID(vpnStatus))
when (vpnStatus) {
VPNStatus.CONNECTED -> {
Expand Down
11 changes: 4 additions & 7 deletions app/src/main/java/nl/eduvpn/app/fragment/SettingsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ import nl.eduvpn.app.SettingsActivity
import nl.eduvpn.app.base.BaseFragment
import nl.eduvpn.app.databinding.FragmentSettingsBinding
import nl.eduvpn.app.entity.Settings
import nl.eduvpn.app.service.HistoryService
import nl.eduvpn.app.service.PreferencesService
import nl.eduvpn.app.viewmodel.SettingsViewModel
import javax.inject.Inject

/**
* Fragment which displays the available settings to the user.
Expand All @@ -53,9 +50,9 @@ class SettingsFragment : BaseFragment<FragmentSettingsBinding>() {
EduVPNApplication.get(view.context).component().inject(this)
val originalSettings = viewModel.appSettings
binding.useCustomTabsSwitch.isChecked = originalSettings.useCustomTabs()
binding.forceTcpSwitch.isChecked = originalSettings.forceTcp()
binding.preferTcpSwitch.isChecked = originalSettings.preferTcp()
binding.useCustomTabsSwitch.setOnClickListener { saveSettings() }
binding.forceTcpSwitch.setOnClickListener { saveSettings() }
binding.preferTcpSwitch.setOnClickListener { saveSettings() }
binding.licensesButton.setOnClickListener {
startActivity(
Intent(
Expand Down Expand Up @@ -114,7 +111,7 @@ class SettingsFragment : BaseFragment<FragmentSettingsBinding>() {

private fun saveSettings() {
val useCustomTabs = binding.useCustomTabsSwitch.isChecked
val forceTcp = binding.forceTcpSwitch.isChecked
viewModel.storeAppSettings(Settings(useCustomTabs, forceTcp))
val preferTcp = binding.preferTcpSwitch.isChecked
viewModel.storeAppSettings(Settings(useCustomTabs, preferTcp))
}
}
5 changes: 3 additions & 2 deletions app/src/main/java/nl/eduvpn/app/inject/ApplicationModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import nl.eduvpn.app.utils.Log
import okhttp3.Cache
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import org.eduvpn.common.Protocol
import java.io.IOException
import java.net.ConnectException
import java.net.SocketTimeoutException
Expand Down Expand Up @@ -147,8 +148,8 @@ class ApplicationModule(private val application: EduVPNApplication) {
wireGuardServiceProvider: Provider<WireGuardService>
): Optional<VPNService> {
return when (preferencesService.getCurrentProtocol()) {
org.eduvpn.common.Protocol.OpenVPN.nativeValue -> Optional.of(eduOpenVPNServiceProvider.get())
org.eduvpn.common.Protocol.WireGuard.nativeValue -> Optional.of(wireGuardServiceProvider.get())
Protocol.OpenVPN.nativeValue -> Optional.of(eduOpenVPNServiceProvider.get())
Protocol.WireGuard.nativeValue, Protocol.WireGuardWithProxyGuard.nativeValue -> Optional.of(wireGuardServiceProvider.get())
else -> Optional.empty()
}
}
Expand Down
71 changes: 47 additions & 24 deletions app/src/main/java/nl/eduvpn/app/service/BackendService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import nl.eduvpn.app.entity.CertExpiryTimes
import nl.eduvpn.app.entity.CurrentServer
import nl.eduvpn.app.entity.Instance
import nl.eduvpn.app.entity.Profile
import nl.eduvpn.app.entity.ProxySettings
import nl.eduvpn.app.entity.SerializedVpnConfig
import nl.eduvpn.app.entity.exception.CommonException
import nl.eduvpn.app.service.SerializerService.UnknownFormatException
Expand All @@ -22,6 +23,7 @@ import org.eduvpn.common.GoBackend
import org.eduvpn.common.GoBackend.Callback
import org.eduvpn.common.ServerType
import java.io.File
import java.io.FileDescriptor
import java.net.InetAddress
import java.net.NetworkInterface
import java.util.Collections
Expand All @@ -42,9 +44,9 @@ class BackendService(
}

enum class State(val nativeValue: Int) {
ASK_LOCATION(2),
OAUTH_STARTED(6),
ASK_PROFILE(9)
OAUTH_STARTED(3),
ASK_LOCATION(5),
ASK_PROFILE(6)
}

private val goBackend = GoBackend()
Expand All @@ -60,30 +62,26 @@ class BackendService(
selectProfiles: (List<Profile>) -> Unit,
selectCountry: (Int?) -> Unit,
connectWithConfig: (SerializedVpnConfig, Boolean) -> Unit,
showError: (Throwable) -> Unit
showError: (Throwable) -> Unit,
protectSocket: (Int) -> Unit
): String? {
onConfigReady = { config, forceTcp ->
connectWithConfig(config, forceTcp)
onConfigReady = { config, preferTcp ->
connectWithConfig(config, preferTcp)
}
GoBackend.callbackFunction = object : Callback {

// The library wants to get a token from our internal storage
override fun getToken(serverJson: String): String? {
// Find out the serverId
val parsedServer = serializerService.deserializeCurrentServer(serverJson)
parsedServer.getUniqueId()?.let { uniqueId ->
return preferencesService.getToken(uniqueId)
}
return null
override fun getToken(serverId: String): String? {
return preferencesService.getToken(serverId)
}

// The library wants to save a token in our internal storage
override fun setToken(serverJson: String, token: String?) {
// Find out the serverId
val parsedServer = serializerService.deserializeCurrentServer(serverJson)
parsedServer.getUniqueId()?.let { uniqueId ->
preferencesService.setToken(uniqueId, token)
}
override fun setToken(serverId: String, token: String?) {
preferencesService.setToken(serverId, token)
}

override fun onProxyFileDescriptor(fileDescriptor: Int) {
protectSocket(fileDescriptor)
}

// Called when the native state machine changes
Expand Down Expand Up @@ -246,19 +244,19 @@ class BackendService(
}

@kotlin.jvm.Throws(CommonException::class, UnknownFormatException::class)
suspend fun getConfig(instance: Instance, forceTCP: Boolean) = withContext(Dispatchers.IO) {
suspend fun getConfig(instance: Instance, preferTcp: Boolean) = withContext(Dispatchers.IO) {
val dataErrorTuple = goBackend.getProfiles(
instance.authorizationType.toNativeServerType().nativeValue,
instance.baseURI,
forceTCP,
preferTcp,
false
)

if (dataErrorTuple.isError) {
throw CommonException(dataErrorTuple.error)
}
val config = serializerService.deserializeSerializedVpnConfig(dataErrorTuple.data)
onConfigReady?.invoke(config, forceTCP)
onConfigReady?.invoke(config, preferTcp)
}

@kotlin.jvm.Throws(CommonException::class)
Expand All @@ -282,11 +280,11 @@ class BackendService(
}
}

fun selectCountry(cookie: Int?, countryCode: String) {
fun selectCountry(cookie: Int?, organizationId: String, countryCode: String) {
val errorString = if (cookie != null) {
goBackend.cookieReply(cookie, countryCode)
} else {
goBackend.selectCountry(countryCode)
goBackend.selectCountry(organizationId, countryCode)
}
if (errorString != null) {
throw CommonException(errorString)
Expand All @@ -313,6 +311,22 @@ class BackendService(
}
}

fun notifyConnecting() {
goBackend.notifyConnecting()
}

fun notifyConnected () {
goBackend.notifyConnected()
}

fun notifyDisconnecting() {
goBackend.notifyDisconnecting()
}

fun notifyDisconnected() {
goBackend.notifyDisconnected()
}

fun getLogFile() : File? {
val configDirectory = File(context.cacheDir, DIRECTORY_BACKEND_CONFIG_FILES)
val configFile = File(configDirectory, "log")
Expand Down Expand Up @@ -371,5 +385,14 @@ class BackendService(
}
}
}


@Throws(CommonException::class)
suspend fun startProxyguard(proxy: ProxySettings) = withContext(Dispatchers.IO) {
val result = goBackend.startProxyGuard(proxy.sourcePort, proxy.listen, proxy.peer)
if (!result.isNullOrEmpty()) {
throw CommonException(result)
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import androidx.lifecycle.LiveData;

import org.eduvpn.common.Protocol;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.StringReader;
Expand Down Expand Up @@ -200,12 +199,12 @@ public VpnProfile importConfig(String configString, String preferredName) {
*/
public void connect(@NonNull Activity activity, @NonNull VpnProfile vpnProfile) {
Log.i(TAG, "Initiating connection with profile:" + vpnProfile.getUUIDString());
boolean forceTcp = _preferencesService.getAppSettings().forceTcp();
Log.i(TAG, "Force TCP: " + forceTcp);
boolean preferTcp = _preferencesService.getAppSettings().preferTcp();
Log.i(TAG, "Prefet TCP: " + preferTcp);
// If force TCP is enabled, disable the UDP connections
for (Connection connection : vpnProfile.mConnections) {
if (connection.mUseUdp) {
connection.mEnabled = !forceTcp;
connection.mEnabled = !preferTcp;
}
}
// Make sure these changes are NOT saved, since we don't want the config changes to be permanent.
Expand Down
15 changes: 7 additions & 8 deletions app/src/main/java/nl/eduvpn/app/service/PreferencesService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,6 @@ class PreferencesService(
}
}
}

val editor = insecurePreferences.edit()
editor.putInt(KEY_STORAGE_VERSION, STORAGE_VERSION)
editor.commit()
if (Constants.DEBUG) {
Log.d(TAG, "Migrated over to storage version v4.")
}
}
if (version < 5) {
securePreferences.edit()
Expand All @@ -165,6 +158,12 @@ class PreferencesService(
Log.d(TAG, "Migrated over to storage version v5.")
}
}
val editor = insecurePreferences.edit()
editor.putInt(KEY_STORAGE_VERSION, STORAGE_VERSION)
editor.commit()
if (Constants.DEBUG) {
Log.d(TAG, "Migrated over to storage version v4.")
}
}

/**
Expand Down Expand Up @@ -235,7 +234,7 @@ class PreferencesService(
*/
fun getAppSettings(): Settings {
val defaultSettings =
Settings(Settings.USE_CUSTOM_TABS_DEFAULT_VALUE, Settings.FORCE_TCP_DEFAULT_VALUE)
Settings(Settings.USE_CUSTOM_TABS_DEFAULT_VALUE, Settings.PREFER_TCP_DEFAULT_VALUE)
val serializedSettings = getSharedPreferences().getString(KEY_APP_SETTINGS, null)
return if (serializedSettings == null) {
// Default settings.
Expand Down
Loading
Loading