@@ -23,65 +23,19 @@ extension PacketTunnelActor {
23
23
- Parameter lastKeyRotation: date when last key rotation took place.
24
24
*/
25
25
func cacheActiveKey( lastKeyRotation: Date ? ) {
26
- func mutateConnectionState( _ connState: inout ConnectionState ) -> Bool {
27
- switch connState. keyPolicy {
28
- case . useCurrent:
29
- if let currentKey = connState. currentKey {
30
- connState. lastKeyRotation = lastKeyRotation
31
-
32
- // Move currentKey into keyPolicy.
33
- connState. keyPolicy = . usePrior( currentKey, startKeySwitchTask ( ) )
34
- connState. currentKey = nil
35
-
36
- return true
37
- } else {
38
- return false
39
- }
40
-
41
- case . usePrior:
42
- // It's unlikely that we'll see subsequent key rotations happen frequently.
43
- return false
44
- }
45
- }
46
-
47
- switch state {
48
- case var . connecting( connState) :
49
- if mutateConnectionState ( & connState) {
50
- state = . connecting( connState)
51
- }
52
-
53
- case var . connected( connState) :
54
- if mutateConnectionState ( & connState) {
55
- state = . connected( connState)
56
- }
57
-
58
- case var . reconnecting( connState) :
59
- if mutateConnectionState ( & connState) {
60
- state = . reconnecting( connState)
61
- }
62
-
63
- case var . error( blockedState) :
64
- switch blockedState. keyPolicy {
65
- case . useCurrent:
66
- // Key policy is preserved between states and key rotation may still happen while in blocked state.
67
- // Therefore perform the key switch as normal with one exception that it shouldn't reconnect the tunnel
68
- // automatically.
69
- if let currentKey = blockedState. currentKey {
70
- blockedState. lastKeyRotation = lastKeyRotation
71
-
72
- // Move currentKey into keyPolicy.
73
- blockedState. keyPolicy = . usePrior( currentKey, startKeySwitchTask ( ) )
74
- blockedState. currentKey = nil
75
-
76
- state = . error( blockedState)
77
- }
78
-
79
- case . usePrior:
80
- break
81
- }
82
-
83
- case . initial, . disconnected, . disconnecting:
84
- break
26
+ state. mutateAssociatedData { stateData in
27
+ guard
28
+ stateData. keyPolicy == . useCurrent,
29
+ let currentKey = stateData. currentKey
30
+ else { return }
31
+ // Key policy is preserved between states and key rotation may still happen while in blocked state.
32
+ // Therefore perform the key switch as normal with one exception that it shouldn't reconnect the tunnel
33
+ // automatically.
34
+ stateData. lastKeyRotation = lastKeyRotation
35
+
36
+ // Move currentKey into keyPolicy.
37
+ stateData. keyPolicy = . usePrior( currentKey, startKeySwitchTask ( ) )
38
+ stateData. currentKey = nil
85
39
}
86
40
}
87
41
@@ -123,36 +77,10 @@ extension PacketTunnelActor {
123
77
- Returns: `true` if the tunnel should reconnect, otherwise `false`.
124
78
*/
125
79
private func switchToCurrentKeyInner( ) -> Bool {
126
- switch state {
127
- case var . connecting( connState) :
128
- if setCurrentKeyPolicy ( & connState. keyPolicy) {
129
- state = . connecting( connState)
130
- return true
131
- }
132
-
133
- case var . connected( connState) :
134
- if setCurrentKeyPolicy ( & connState. keyPolicy) {
135
- state = . connected( connState)
136
- return true
137
- }
138
-
139
- case var . reconnecting( connState) :
140
- if setCurrentKeyPolicy ( & connState. keyPolicy) {
141
- state = . reconnecting( connState)
142
- return true
143
- }
144
-
145
- case var . error( blockedState) :
146
- if setCurrentKeyPolicy ( & blockedState. keyPolicy) {
147
- state = . error( blockedState)
148
-
149
- // Prevent tunnel from reconnecting when in blocked state.
150
- return false
151
- }
152
-
153
- case . disconnected, . disconnecting, . initial:
154
- break
155
- }
80
+ let oldKeyPolicy = state. keyPolicy
81
+ state. mutateKeyPolicy ( setCurrentKeyPolicy)
82
+ // Prevent tunnel from reconnecting when in blocked state.
83
+ guard case . error = state else { return state. keyPolicy != oldKeyPolicy }
156
84
return false
157
85
}
158
86
@@ -162,14 +90,9 @@ extension PacketTunnelActor {
162
90
- Parameter keyPolicy: a reference to key policy held either in connection state or blocked state struct.
163
91
- Returns: `true` when the policy was modified, otherwise `false`.
164
92
*/
165
- private func setCurrentKeyPolicy( _ keyPolicy: inout KeyPolicy ) -> Bool {
166
- switch keyPolicy {
167
- case . useCurrent:
168
- return false
169
-
170
- case . usePrior:
93
+ private func setCurrentKeyPolicy( _ keyPolicy: inout KeyPolicy ) {
94
+ if case . usePrior = keyPolicy {
171
95
keyPolicy = . useCurrent
172
- return true
173
96
}
174
97
}
175
98
}
0 commit comments