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

Ensure successful registration before key refresh #3736

Merged
merged 5 commits into from
Oct 26, 2023

Conversation

aitorvs
Copy link
Collaborator

@aitorvs aitorvs commented Oct 21, 2023

Task/Issue URL: https://app.asana.com/0/0/1205631225739207/f

Description

Improve re-key so that:

  • it happens once a day
  • just re-key when the screen is off
  • avoids re-keying attempts when device is idle

Steps to test this PR

Test force re-key

  • build internal from this branch
  • use force rekey in the netp dev settings
  • new private key should be registered

Test rekey

  • apply the following patch
Index: network-protection/network-protection-impl/src/main/java/com/duckduckgo/networkprotection/impl/rekey/NetPRekeyer.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/network-protection/network-protection-impl/src/main/java/com/duckduckgo/networkprotection/impl/rekey/NetPRekeyer.kt b/network-protection/network-protection-impl/src/main/java/com/duckduckgo/networkprotection/impl/rekey/NetPRekeyer.kt
--- a/network-protection/network-protection-impl/src/main/java/com/duckduckgo/networkprotection/impl/rekey/NetPRekeyer.kt	(revision e44ddfce33069ae2ee2227bddffb24cc89049381)
+++ b/network-protection/network-protection-impl/src/main/java/com/duckduckgo/networkprotection/impl/rekey/NetPRekeyer.kt	(date 1698157737720)
@@ -68,7 +68,7 @@
         val forceOrFalseInProductionBuilds = forceRekey.getAndResetValue()
 
         val millisSinceLastKeyUpdate = System.currentTimeMillis() - networkProtectionRepository.lastPrivateKeyUpdateTimeInMillis
-        if (!forceOrFalseInProductionBuilds && millisSinceLastKeyUpdate < TimeUnit.DAYS.toMillis(1)) {
+        if (!forceOrFalseInProductionBuilds && millisSinceLastKeyUpdate < TimeUnit.SECONDS.toMillis(5)) {
             logcat { "Less than 24h passed, skip re-keying" }
             return
         }
Index: network-protection/network-protection-impl/src/main/java/com/duckduckgo/networkprotection/impl/rekey/NetPRekeyScheduler.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/network-protection/network-protection-impl/src/main/java/com/duckduckgo/networkprotection/impl/rekey/NetPRekeyScheduler.kt b/network-protection/network-protection-impl/src/main/java/com/duckduckgo/networkprotection/impl/rekey/NetPRekeyScheduler.kt
--- a/network-protection/network-protection-impl/src/main/java/com/duckduckgo/networkprotection/impl/rekey/NetPRekeyScheduler.kt	(revision e44ddfce33069ae2ee2227bddffb24cc89049381)
+++ b/network-protection/network-protection-impl/src/main/java/com/duckduckgo/networkprotection/impl/rekey/NetPRekeyScheduler.kt	(date 1698157737723)
@@ -42,7 +42,7 @@
         job += coroutineScope.launch(dispatcherProvider.io()) {
             while (isActive && vpnFeaturesRegistry.isFeatureRegistered(NetPVpnFeature.NETP_VPN)) {
                 logcat { "Start periodic re-keying attempts" }
-                delay(TimeUnit.HOURS.toMillis(1)) // try to re-key every 1h
+                delay(TimeUnit.SECONDS.toMillis(10)) // try to re-key every 1h
 
                 netPRekeyer.doRekey()
             }
  • build internal build
  • launch app and enable NetP
  • with mobile device screen ON, see Device not locked, skip re-keying logcat appear every 10 seconds or so
  • with mobile device screen OFF, see Restarting VPN after clearing client keys logcat appear every 10 seconds or so

@aitorvs
Copy link
Collaborator Author

aitorvs commented Oct 21, 2023

Current dependencies on/for this PR:

This comment was auto-generated by Graphite.

@aitorvs aitorvs mentioned this pull request Oct 21, 2023
@aitorvs aitorvs marked this pull request as draft October 21, 2023 16:58
@aitorvs aitorvs force-pushed the feature/aitor/netp/rekey_ branch 3 times, most recently from 3c4ec73 to 4514bee Compare October 21, 2023 17:00
@aitorvs aitorvs force-pushed the feature/aitor/netp/rekey_ branch 2 times, most recently from fb31b77 to 6967b32 Compare October 21, 2023 17:03
Comment on lines +105 to +108
suspend fun forceRekey() {
if (appBuildConfig.isInternalBuild()) {
forceRekey.set(true)
doRekey()
} else {
logcat(LogPriority.ERROR) { "Force re-key not allowed in production builds" }
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to keep the force rekey internal build functionality

@@ -52,7 +53,7 @@ interface NetPWaitlistManager {
@SingleInstanceIn(AppScope::class)
@ContributesBinding(AppScope::class)
class RealNetPWaitlistManager @Inject constructor(
private val service: WgVpnControllerService,
@ProtectedVpnControllerService private val service: WgVpnControllerService,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto, ie. this probably can also rather be @UnprotectedVpnControllerService at which point we delete the @ProtectedVpnControllerService altogether

@@ -215,7 +213,7 @@ class NetPInternalSettingsActivity : DuckDuckGoActivity() {

binding.forceRekey.setClickListener {
lifecycleScope.launch {
netPRekeyer.doRekey()
sendBroadcast(Intent(DebugRekeyReceiver.ACTION_FORCE_REKEY))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use a broadcast as an IPC mechanism, as rekeying needs to be received/executed by the :vpn process

@@ -32,7 +33,7 @@ import javax.inject.Inject
)
class WgServerInternalProvider @Inject constructor(
private val netPServerRepository: NetPServerRepository,
private val controllerService: WgVpnControllerService,
@ProtectedVpnControllerService private val controllerService: WgVpnControllerService,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

class DebugRekeyReceiver @Inject constructor(
private val context: Context,
private val rekeyer: RealNetPRekeyer,
@ProcessName private val processName: String,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just for logging

@aitorvs aitorvs force-pushed the feature/aitor/netp/rekey_ branch 3 times, most recently from 728ba37 to 1e99303 Compare October 21, 2023 18:39
@aitorvs aitorvs mentioned this pull request Oct 22, 2023
@aitorvs aitorvs force-pushed the feature/aitor/netp/rekey branch from 0ed4fd1 to 57c48bd Compare October 23, 2023 12:31
@aitorvs aitorvs force-pushed the feature/aitor/netp/rekey_ branch from 1e99303 to 573f67e Compare October 23, 2023 12:31
@aitorvs aitorvs changed the base branch from feature/aitor/netp/rekey to develop October 23, 2023 15:11
@aitorvs aitorvs force-pushed the feature/aitor/netp/rekey_ branch from 573f67e to 605400c Compare October 23, 2023 15:11
@aitorvs aitorvs marked this pull request as ready for review October 23, 2023 15:18
@aitorvs aitorvs force-pushed the feature/aitor/netp/rekey_ branch 3 times, most recently from c3ca5af to c897c06 Compare October 23, 2023 16:07
@aitorvs aitorvs force-pushed the feature/aitor/netp/rekey_ branch 3 times, most recently from a2ffe89 to cde8bfa Compare October 24, 2023 22:24
@aitorvs aitorvs force-pushed the feature/aitor/netp/rekey_ branch from cde8bfa to 8027aaa Compare October 26, 2023 14:57
@aitorvs aitorvs force-pushed the feature/aitor/netp/rekey_ branch from 8027aaa to 771b666 Compare October 26, 2023 16:12
@aitorvs aitorvs merged commit 5038292 into develop Oct 26, 2023
@aitorvs aitorvs deleted the feature/aitor/netp/rekey_ branch October 26, 2023 17:50
joshliebe pushed a commit that referenced this pull request Nov 7, 2023
Task/Issue URL: https://app.asana.com/0/0/1205631225739207/f

### Description
Improve re-key so that:
* it happens once a day
* just re-key when the screen is off
* avoids re-keying attempts when device is idle

### Steps to test this PR

_Test force re-key_
- [x] build internal from this branch
- [x] use force rekey in the netp dev settings
- [x] new private key should be registered

_Test rekey_
- apply the following patch
```diff
Index: network-protection/network-protection-impl/src/main/java/com/duckduckgo/networkprotection/impl/rekey/NetPRekeyer.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/network-protection/network-protection-impl/src/main/java/com/duckduckgo/networkprotection/impl/rekey/NetPRekeyer.kt b/network-protection/network-protection-impl/src/main/java/com/duckduckgo/networkprotection/impl/rekey/NetPRekeyer.kt
--- a/network-protection/network-protection-impl/src/main/java/com/duckduckgo/networkprotection/impl/rekey/NetPRekeyer.kt	(revision e44ddfce33069ae2ee2227bddffb24cc89049381)
+++ b/network-protection/network-protection-impl/src/main/java/com/duckduckgo/networkprotection/impl/rekey/NetPRekeyer.kt	(date 1698157737720)
@@ -68,7 +68,7 @@
         val forceOrFalseInProductionBuilds = forceRekey.getAndResetValue()
 
         val millisSinceLastKeyUpdate = System.currentTimeMillis() - networkProtectionRepository.lastPrivateKeyUpdateTimeInMillis
-        if (!forceOrFalseInProductionBuilds && millisSinceLastKeyUpdate < TimeUnit.DAYS.toMillis(1)) {
+        if (!forceOrFalseInProductionBuilds && millisSinceLastKeyUpdate < TimeUnit.SECONDS.toMillis(5)) {
             logcat { "Less than 24h passed, skip re-keying" }
             return
         }
Index: network-protection/network-protection-impl/src/main/java/com/duckduckgo/networkprotection/impl/rekey/NetPRekeyScheduler.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/network-protection/network-protection-impl/src/main/java/com/duckduckgo/networkprotection/impl/rekey/NetPRekeyScheduler.kt b/network-protection/network-protection-impl/src/main/java/com/duckduckgo/networkprotection/impl/rekey/NetPRekeyScheduler.kt
--- a/network-protection/network-protection-impl/src/main/java/com/duckduckgo/networkprotection/impl/rekey/NetPRekeyScheduler.kt	(revision e44ddfce33069ae2ee2227bddffb24cc89049381)
+++ b/network-protection/network-protection-impl/src/main/java/com/duckduckgo/networkprotection/impl/rekey/NetPRekeyScheduler.kt	(date 1698157737723)
@@ -42,7 +42,7 @@
         job += coroutineScope.launch(dispatcherProvider.io()) {
             while (isActive && vpnFeaturesRegistry.isFeatureRegistered(NetPVpnFeature.NETP_VPN)) {
                 logcat { "Start periodic re-keying attempts" }
-                delay(TimeUnit.HOURS.toMillis(1)) // try to re-key every 1h
+                delay(TimeUnit.SECONDS.toMillis(10)) // try to re-key every 1h
 
                 netPRekeyer.doRekey()
             }
```
- build internal build
- launch app and enable NetP
- [x] with mobile device screen ON, see `Device not locked, skip re-keying` logcat appear every 10 seconds or so
- [x] with mobile device screen OFF, see `Restarting VPN after clearing client keys` logcat appear every 10 seconds or so
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants