-
Notifications
You must be signed in to change notification settings - Fork 927
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Block malware in internal builds (#4979)
Task/Issue URL: https://app.asana.com/0/1203137811378537/1208212374550781/f ### Description Block malware for internal builds. ### Steps to test this PR _Test_ - [x] filter logcat with `Adding DNS` - [x] install from this branch, launch the app and enable VPN - [x] verify DNS added is 10.11.12.1 - [x] go to VPN settings and enable "block malware" toggle - [x] go back to VPN main screen - [x] verify DNS added is 10.11.12.2 - [x] verify network works as expected - [x] Set a custom DNS (eg. 1.1.1.1) in VPN settings -> DNS server - [x] verify DNS added is the custom DNS (eg. 1.1.1.1) - [x] configure back to DDG DNS - [x] verify DNS added is 10.11.12.2 - [x] verify network works as expected
- Loading branch information
Showing
9 changed files
with
225 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...main/java/com/duckduckgo/networkprotection/impl/configuration/InetAddressDnsOperations.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright (c) 2024 DuckDuckGo | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.duckduckgo.networkprotection.impl.configuration | ||
|
||
import java.net.InetAddress | ||
|
||
/** | ||
* The block malware DNS IP address is a <<1 bit-wise operations on the last octet based on the default DNS | ||
* This method assumes the [InetAddress] passed in as parameter is the default DNS. | ||
* | ||
* You should only | ||
*/ | ||
internal fun InetAddress.computeBlockMalwareDnsOrSame(): InetAddress { | ||
return kotlin.runCatching { | ||
// Perform <<1 operation on the last octet | ||
// Since byte is signed in Kotlin/Java, we mask it with 0xFF to treat it as unsigned | ||
val newLastOctet = (address.last().toInt() and 0xFF) shl 1 | ||
|
||
val newIPAddress = address | ||
// Update the last octet in the byte array | ||
newIPAddress[newIPAddress.size - 1] = (newLastOctet and 0xFF).toByte() // Ensure it stays within byte range | ||
|
||
InetAddress.getByAddress(newIPAddress) | ||
}.getOrNull() ?: this | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
...main/java/com/duckduckgo/networkprotection/internal/feature/BlockMalwareVpnSettingView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* Copyright (c) 2024 DuckDuckGo | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.duckduckgo.networkprotection.internal.feature | ||
|
||
import android.annotation.SuppressLint | ||
import android.content.Context | ||
import android.util.AttributeSet | ||
import android.view.View | ||
import android.widget.CompoundButton.OnCheckedChangeListener | ||
import android.widget.FrameLayout | ||
import com.duckduckgo.anvil.annotations.InjectWith | ||
import com.duckduckgo.anvil.annotations.PriorityKey | ||
import com.duckduckgo.app.di.AppCoroutineScope | ||
import com.duckduckgo.common.ui.viewbinding.viewBinding | ||
import com.duckduckgo.common.utils.DispatcherProvider | ||
import com.duckduckgo.di.scopes.ActivityScope | ||
import com.duckduckgo.di.scopes.ViewScope | ||
import com.duckduckgo.feature.toggles.api.Toggle | ||
import com.duckduckgo.mobile.android.vpn.VpnFeature | ||
import com.duckduckgo.mobile.android.vpn.VpnFeaturesRegistry | ||
import com.duckduckgo.networkprotection.impl.configuration.WgTunnelConfig | ||
import com.duckduckgo.networkprotection.impl.settings.NetPSettingsLocalConfig | ||
import com.duckduckgo.networkprotection.impl.settings.VpnSettingPlugin | ||
import com.duckduckgo.networkprotection.internal.databinding.VpnViewSettingsBlockMalwareBinding | ||
import com.squareup.anvil.annotations.ContributesMultibinding | ||
import dagger.android.support.AndroidSupportInjection | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.SupervisorJob | ||
import kotlinx.coroutines.cancel | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
|
||
@InjectWith(ViewScope::class) | ||
class BlockMalwareVpnSettingView @JvmOverloads constructor( | ||
context: Context, | ||
attrs: AttributeSet? = null, | ||
defStyle: Int = 0, | ||
) : FrameLayout(context, attrs, defStyle) { | ||
|
||
@Inject | ||
lateinit var dispatcherProvider: DispatcherProvider | ||
|
||
@Inject | ||
lateinit var netPSettingsLocalConfig: NetPSettingsLocalConfig | ||
|
||
@Inject | ||
lateinit var vpnFeaturesRegistry: VpnFeaturesRegistry | ||
|
||
@Inject | ||
@AppCoroutineScope | ||
lateinit var appCoroutineScope: CoroutineScope | ||
|
||
@Inject | ||
lateinit var wgTunnelConfig: WgTunnelConfig | ||
|
||
private var mainCoroutineScope: CoroutineScope? = null | ||
|
||
private val binding: VpnViewSettingsBlockMalwareBinding by viewBinding() | ||
|
||
private var didToggleSetting = false | ||
|
||
private val toggleListener = OnCheckedChangeListener { _, value -> | ||
mainCoroutineScope?.launch(dispatcherProvider.io()) { | ||
didToggleSetting = !didToggleSetting | ||
netPSettingsLocalConfig.blockMalware().setEnabled(Toggle.State(enable = value)) | ||
} | ||
} | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
override fun onAttachedToWindow() { | ||
AndroidSupportInjection.inject(this) | ||
super.onAttachedToWindow() | ||
|
||
@SuppressLint("NoHardcodedCoroutineDispatcher") | ||
mainCoroutineScope = CoroutineScope(SupervisorJob() + dispatcherProvider.main()) | ||
|
||
mainCoroutineScope?.launch(dispatcherProvider.io()) { | ||
val isEnabled = netPSettingsLocalConfig.blockMalware().isEnabled() | ||
withContext(dispatcherProvider.main()) { | ||
binding.blockMalware.quietlySetIsChecked(isEnabled, toggleListener) | ||
} | ||
} | ||
} | ||
|
||
override fun onDetachedFromWindow() { | ||
super.onDetachedFromWindow() | ||
if (didToggleSetting) { | ||
// appCoroutineScope to make sure it's not cancelled | ||
appCoroutineScope.launch(dispatcherProvider.io()) { | ||
// wgTunnelConfig.clearWgConfig() // force config re-fetch | ||
// VpnFeature hardcoded here as eventually we'll move this inside the netp-impl module | ||
vpnFeaturesRegistry.refreshFeature(VpnFeature { "NETP_VPN" }) | ||
} | ||
} | ||
mainCoroutineScope?.cancel() | ||
mainCoroutineScope = null | ||
} | ||
} | ||
|
||
@ContributesMultibinding(ActivityScope::class) | ||
@PriorityKey(BLOCK_MALWARE_PRIORITY) | ||
class BlockMalwareSettingViewPlugin @Inject constructor() : VpnSettingPlugin { | ||
override fun getView(context: Context): View? { | ||
return BlockMalwareVpnSettingView(context) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...ction/network-protection-internal/src/main/res/layout/vpn_view_settings_block_malware.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?xml version="1.0" encoding="utf-8"?><!-- | ||
~ Copyright (c) 2024 DuckDuckGo | ||
~ | ||
~ Licensed under the Apache License, Version 2.0 (the "License"); | ||
~ you may not use this file except in compliance with the License. | ||
~ You may obtain a copy of the License at | ||
~ | ||
~ http://www.apache.org/licenses/LICENSE-2.0 | ||
~ | ||
~ Unless required by applicable law or agreed to in writing, software | ||
~ distributed under the License is distributed on an "AS IS" BASIS, | ||
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
~ See the License for the specific language governing permissions and | ||
~ limitations under the License. | ||
--> | ||
|
||
<com.duckduckgo.common.ui.view.listitem.TwoLineListItem | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
android:id="@+id/block_malware" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
app:primaryText="@string/netpBlockMalwarePrimary" | ||
app:secondaryText="@string/netpBlockMalwareByline" | ||
app:showSwitch="true" | ||
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters