Skip to content

Commit 7eecc67

Browse files
committed
Get rid of swiftlint warnings and move files around
1 parent f42dcb3 commit 7eecc67

File tree

5 files changed

+224
-200
lines changed

5 files changed

+224
-200
lines changed

ios/MullvadVPN.xcodeproj/project.pbxproj

+8
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,8 @@
616616
A935594C2B4C2DA900D5D524 /* APIAvailabilityTestRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = A935594B2B4C2DA900D5D524 /* APIAvailabilityTestRequest.swift */; };
617617
A94D691A2ABAD66700413DD4 /* WireGuardKitTypes in Frameworks */ = {isa = PBXBuildFile; productRef = 58FE25E22AA72AE9003D1918 /* WireGuardKitTypes */; };
618618
A94D691B2ABAD66700413DD4 /* WireGuardKitTypes in Frameworks */ = {isa = PBXBuildFile; productRef = 58FE25E72AA7399D003D1918 /* WireGuardKitTypes */; };
619+
A95EEE362B722CD600A8A39B /* TunnelMonitorState.swift in Sources */ = {isa = PBXBuildFile; fileRef = A95EEE352B722CD600A8A39B /* TunnelMonitorState.swift */; };
620+
A95EEE382B722DFC00A8A39B /* PingStats.swift in Sources */ = {isa = PBXBuildFile; fileRef = A95EEE372B722DFC00A8A39B /* PingStats.swift */; };
619621
A970C89D2B29E38C000A7684 /* Socks5UsernamePasswordCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = A970C89C2B29E38C000A7684 /* Socks5UsernamePasswordCommand.swift */; };
620622
A97D25AE2B0BB18100946B2D /* ProtocolObfuscator.swift in Sources */ = {isa = PBXBuildFile; fileRef = A97D25AD2B0BB18100946B2D /* ProtocolObfuscator.swift */; };
621623
A97D25B02B0BB5C400946B2D /* ProtocolObfuscationStub.swift in Sources */ = {isa = PBXBuildFile; fileRef = A97D25AF2B0BB5C400946B2D /* ProtocolObfuscationStub.swift */; };
@@ -1779,6 +1781,8 @@
17791781
A935594B2B4C2DA900D5D524 /* APIAvailabilityTestRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIAvailabilityTestRequest.swift; sourceTree = "<group>"; };
17801782
A935594D2B4E919F00D5D524 /* Api.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Api.xcconfig; sourceTree = "<group>"; };
17811783
A9467E7E2A29DEFE000DC21F /* RelayCacheTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RelayCacheTests.swift; sourceTree = "<group>"; };
1784+
A95EEE352B722CD600A8A39B /* TunnelMonitorState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TunnelMonitorState.swift; sourceTree = "<group>"; };
1785+
A95EEE372B722DFC00A8A39B /* PingStats.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PingStats.swift; sourceTree = "<group>"; };
17821786
A970C89C2B29E38C000A7684 /* Socks5UsernamePasswordCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Socks5UsernamePasswordCommand.swift; sourceTree = "<group>"; };
17831787
A97D25AD2B0BB18100946B2D /* ProtocolObfuscator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProtocolObfuscator.swift; sourceTree = "<group>"; };
17841788
A97D25AF2B0BB5C400946B2D /* ProtocolObfuscationStub.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProtocolObfuscationStub.swift; sourceTree = "<group>"; };
@@ -3116,9 +3120,11 @@
31163120
isa = PBXGroup;
31173121
children = (
31183122
58225D252A84E8A10083D7F1 /* DefaultPathObserverProtocol.swift */,
3123+
A95EEE372B722DFC00A8A39B /* PingStats.swift */,
31193124
582403162A821FD700163DE8 /* TunnelDeviceInfoProtocol.swift */,
31203125
58FC040927B3EE03001C21F0 /* TunnelMonitor.swift */,
31213126
58C7A42C2A85067A0060C66F /* TunnelMonitorProtocol.swift */,
3127+
A95EEE352B722CD600A8A39B /* TunnelMonitorState.swift */,
31223128
7A6B4F582AB8412E00123853 /* TunnelMonitorTimings.swift */,
31233129
58A3BDAF28A1821A00C8C2C6 /* WgStats.swift */,
31243130
);
@@ -4658,6 +4664,7 @@
46584664
58C7A4592A863FB90060C66F /* WgStats.swift in Sources */,
46594665
7AD0AA1F2AD6C8B900119E10 /* URLRequestProxyProtocol.swift in Sources */,
46604666
7A6B4F592AB8412E00123853 /* TunnelMonitorTimings.swift in Sources */,
4667+
A95EEE362B722CD600A8A39B /* TunnelMonitorState.swift in Sources */,
46614668
58FE25DB2AA72A8F003D1918 /* StartOptions.swift in Sources */,
46624669
A97D25AE2B0BB18100946B2D /* ProtocolObfuscator.swift in Sources */,
46634670
583832212AC3174700EA2071 /* PacketTunnelActor+NetworkReachability.swift in Sources */,
@@ -4670,6 +4677,7 @@
46704677
5838322B2AC3EF9600EA2071 /* CommandChannel.swift in Sources */,
46714678
586C145A2AC4735F00245C01 /* PacketTunnelActor+Public.swift in Sources */,
46724679
58342C042AAB61FB003BA12D /* State+Extensions.swift in Sources */,
4680+
A95EEE382B722DFC00A8A39B /* PingStats.swift in Sources */,
46734681
583832272AC3193600EA2071 /* PacketTunnelActor+SleepCycle.swift in Sources */,
46744682
58FE25DC2AA72A8F003D1918 /* AnyTask.swift in Sources */,
46754683
58FE25D92AA72A8F003D1918 /* AutoCancellingTask.swift in Sources */,

ios/MullvadVPN/View controllers/RedeemVoucher/RedeemVoucherViewController.swift

+1-2
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ class RedeemVoucherViewController: UIViewController, UINavigationControllerDeleg
138138

139139
contentView.state = .logout
140140

141-
Task {
142-
[weak self] in
141+
Task { [weak self] in
143142
guard let self else { return }
144143
await interactor.logout()
145144
contentView.state = .initial
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// PingStats.swift
3+
// PacketTunnelCore
4+
//
5+
// Created by Marco Nikic on 2024-02-06.
6+
// Copyright © 2024 Mullvad VPN AB. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
/// Ping statistics.
12+
struct PingStats {
13+
/// Dictionary holding sequence and corresponding date when echo request took place.
14+
var requests = [UInt16: Date]()
15+
16+
/// Timestamp when last echo request was sent.
17+
var lastRequestDate: Date?
18+
19+
/// Timestamp when last echo reply was received.
20+
var lastReplyDate: Date?
21+
}

ios/PacketTunnelCore/TunnelMonitor/TunnelMonitor.swift

+2-198
Original file line numberDiff line numberDiff line change
@@ -14,188 +14,6 @@ import NetworkExtension
1414

1515
/// Tunnel monitor.
1616
public final class TunnelMonitor: TunnelMonitorProtocol {
17-
/// Connection state.
18-
private enum ConnectionState {
19-
/// Initialized and doing nothing.
20-
case stopped
21-
22-
/// Preparing to start.
23-
/// Intermediate state before receiving the first path update.
24-
case pendingStart
25-
26-
/// Establishing connection.
27-
case connecting
28-
29-
/// Connection is established.
30-
case connected
31-
32-
/// Delegate is recovering connection.
33-
/// Delegate has to call `start(probeAddress:)` to complete recovery and resume monitoring.
34-
case recovering
35-
36-
/// Waiting for network connectivity.
37-
case waitingConnectivity
38-
}
39-
40-
/// Tunnel monitor state.
41-
private struct State {
42-
/// Current connection state.
43-
var connectionState: ConnectionState = .stopped
44-
45-
/// Network counters.
46-
var netStats = WgStats()
47-
48-
/// Ping stats.
49-
var pingStats = PingStats()
50-
51-
/// Reference date used to determine if timeout has occurred.
52-
var timeoutReference = Date()
53-
54-
/// Last seen change in rx counter.
55-
var lastSeenRx: Date?
56-
57-
/// Last seen change in tx counter.
58-
var lastSeenTx: Date?
59-
60-
/// Whether periodic heartbeat is suspended.
61-
var isHeartbeatSuspended = false
62-
63-
/// Retry attempt.
64-
var retryAttempt: UInt32 = 0
65-
66-
// Timings and timeouts.
67-
let timings: TunnelMonitorTimings
68-
69-
func evaluateConnection(now: Date, pingTimeout: Duration) -> ConnectionEvaluation {
70-
switch connectionState {
71-
case .connecting:
72-
return handleConnectingState(now: now, pingTimeout: pingTimeout)
73-
case .connected:
74-
return handleConnectedState(now: now, pingTimeout: pingTimeout)
75-
default:
76-
return .ok
77-
}
78-
}
79-
80-
func getPingTimeout() -> Duration {
81-
switch connectionState {
82-
case .connecting:
83-
let multiplier = timings.establishTimeoutMultiplier.saturatingPow(retryAttempt)
84-
let nextTimeout = timings.initialEstablishTimeout * Double(multiplier)
85-
86-
if nextTimeout.isFinite, nextTimeout < timings.maxEstablishTimeout {
87-
return nextTimeout
88-
} else {
89-
return timings.maxEstablishTimeout
90-
}
91-
92-
case .pendingStart, .connected, .waitingConnectivity, .stopped, .recovering:
93-
return timings.pingTimeout
94-
}
95-
}
96-
97-
mutating func updateNetStats(newStats: WgStats, now: Date) {
98-
if newStats.bytesReceived > netStats.bytesReceived {
99-
lastSeenRx = now
100-
}
101-
102-
if newStats.bytesSent > netStats.bytesSent {
103-
lastSeenTx = now
104-
}
105-
106-
netStats = newStats
107-
}
108-
109-
mutating func updatePingStats(sendResult: PingerSendResult, now: Date) {
110-
pingStats.requests.updateValue(now, forKey: sendResult.sequenceNumber)
111-
pingStats.lastRequestDate = now
112-
}
113-
114-
mutating func setPingReplyReceived(_ sequenceNumber: UInt16, now: Date) -> Date? {
115-
guard let pingTimestamp = pingStats.requests.removeValue(forKey: sequenceNumber) else {
116-
return nil
117-
}
118-
119-
pingStats.lastReplyDate = now
120-
timeoutReference = now
121-
122-
return pingTimestamp
123-
}
124-
125-
private func handleConnectingState(now: Date, pingTimeout: Duration) -> ConnectionEvaluation {
126-
if now.timeIntervalSince(timeoutReference) >= pingTimeout {
127-
return .pingTimeout
128-
}
129-
130-
guard let lastRequestDate = pingStats.lastRequestDate else {
131-
return .sendInitialPing
132-
}
133-
134-
if now.timeIntervalSince(lastRequestDate) >= timings.pingDelay {
135-
return .sendNextPing
136-
}
137-
138-
return .ok
139-
}
140-
141-
private func handleConnectedState(now: Date, pingTimeout: Duration) -> ConnectionEvaluation {
142-
if now.timeIntervalSince(timeoutReference) >= pingTimeout, !isHeartbeatSuspended {
143-
return .pingTimeout
144-
}
145-
146-
guard let lastRequestDate = pingStats.lastRequestDate else {
147-
return .sendInitialPing
148-
}
149-
150-
let timeSinceLastPing = now.timeIntervalSince(lastRequestDate)
151-
if let lastReplyDate = pingStats.lastReplyDate,
152-
lastRequestDate.timeIntervalSince(lastReplyDate) >= timings.heartbeatReplyTimeout,
153-
timeSinceLastPing >= timings.pingDelay, !isHeartbeatSuspended {
154-
return .retryHeartbeatPing
155-
}
156-
157-
guard let lastSeenRx, let lastSeenTx else { return .ok }
158-
159-
let rxTimeElapsed = now.timeIntervalSince(lastSeenRx)
160-
let txTimeElapsed = now.timeIntervalSince(lastSeenTx)
161-
162-
if timeSinceLastPing >= timings.heartbeatPingInterval {
163-
// Send heartbeat if traffic is flowing.
164-
if rxTimeElapsed <= timings.trafficFlowTimeout || txTimeElapsed <= timings.trafficFlowTimeout {
165-
return .sendHeartbeatPing
166-
}
167-
168-
if !isHeartbeatSuspended {
169-
return .suspendHeartbeat
170-
}
171-
}
172-
173-
if timeSinceLastPing >= timings.pingDelay {
174-
if txTimeElapsed >= timings.trafficTimeout || rxTimeElapsed >= timings.trafficTimeout {
175-
return .trafficTimeout
176-
}
177-
178-
if lastSeenTx > lastSeenRx, rxTimeElapsed >= timings.inboundTrafficTimeout {
179-
return .inboundTrafficTimeout
180-
}
181-
}
182-
183-
return .ok
184-
}
185-
}
186-
187-
/// Ping statistics.
188-
private struct PingStats {
189-
/// Dictionary holding sequence and corresponding date when echo request took place.
190-
var requests = [UInt16: Date]()
191-
192-
/// Timestamp when last echo request was sent.
193-
var lastRequestDate: Date?
194-
195-
/// Timestamp when last echo reply was received.
196-
var lastReplyDate: Date?
197-
}
198-
19917
private let tunnelDeviceInfo: TunnelDeviceInfoProtocol
20018

20119
private let nslock = NSLock()
@@ -207,7 +25,7 @@ public final class TunnelMonitor: TunnelMonitorProtocol {
20725
private var isObservingDefaultPath = false
20826
private var timer: DispatchSourceTimer?
20927

210-
private var state: State
28+
private var state: TunnelMonitorState
21129
private var probeAddress: IPv4Address?
21230

21331
private let logger = Logger(label: "TunnelMonitor")
@@ -236,7 +54,7 @@ public final class TunnelMonitor: TunnelMonitorProtocol {
23654
self.tunnelDeviceInfo = tunnelDeviceInfo
23755

23856
self.timings = timings
239-
state = State(timings: timings)
57+
state = TunnelMonitorState(timings: timings)
24058

24159
self.pinger = pinger
24260
self.pinger.onReply = { [weak self] reply in
@@ -547,18 +365,6 @@ public final class TunnelMonitor: TunnelMonitorProtocol {
547365
}
548366
}
549367

550-
private enum ConnectionEvaluation {
551-
case ok
552-
case sendInitialPing
553-
case sendNextPing
554-
case sendHeartbeatPing
555-
case retryHeartbeatPing
556-
case suspendHeartbeat
557-
case inboundTrafficTimeout
558-
case trafficTimeout
559-
case pingTimeout
560-
}
561-
562368
private func getStats() -> WgStats? {
563369
do {
564370
return try tunnelDeviceInfo.getStats()
@@ -568,6 +374,4 @@ public final class TunnelMonitor: TunnelMonitorProtocol {
568374
return nil
569375
}
570376
}
571-
572-
// swiftlint:disable:next file_length
573377
}

0 commit comments

Comments
 (0)