Skip to content

Commit

Permalink
Merge branch 'when-phone-loses-connectivity-it-fails-to-reconnect-ios…
Browse files Browse the repository at this point in the history
…-889'
  • Loading branch information
buggmagnet committed Nov 6, 2024
2 parents 82f31fb + 7f84b85 commit d27c492
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 27 deletions.
2 changes: 1 addition & 1 deletion ios/MullvadVPN.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9260,7 +9260,7 @@
repositoryURL = "https://github.com/mullvad/wireguard-apple.git";
requirement = {
kind = revision;
revision = afb345188c187dddafae0f9e27c5466be11451c2;
revision = cc6d3e918691c82d13389ad0fdbe8f35b683a6fc;
};
};
/* End XCRemoteSwiftPackageReference section */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/mullvad/wireguard-apple.git",
"state" : {
"revision" : "afb345188c187dddafae0f9e27c5466be11451c2"
"revision" : "cc6d3e918691c82d13389ad0fdbe8f35b683a6fc"
}
}
],
Expand Down
3 changes: 2 additions & 1 deletion ios/PacketTunnel/WireGuardAdapter/WgAdapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ private extension TunnelAdapterConfiguration {
return TunnelConfiguration(
name: nil,
interface: interfaceConfig,
peers: peers
peers: peers,
pingableGateway: pingableGateway
)
}
}
Expand Down
8 changes: 6 additions & 2 deletions ios/PacketTunnelCore/Actor/ConfigurationBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,24 @@ public struct ConfigurationBuilder {
var endpoint: MullvadEndpoint?
var allowedIPs: [IPAddressRange]
var preSharedKey: PreSharedKey?
var pingableGateway: IPv4Address

public init(
privateKey: PrivateKey,
interfaceAddresses: [IPAddressRange],
dns: SelectedDNSServers? = nil,
endpoint: MullvadEndpoint? = nil,
allowedIPs: [IPAddressRange],
preSharedKey: PreSharedKey? = nil
preSharedKey: PreSharedKey? = nil,
pingableGateway: IPv4Address
) {
self.privateKey = privateKey
self.interfaceAddresses = interfaceAddresses
self.dns = dns
self.endpoint = endpoint
self.allowedIPs = allowedIPs
self.preSharedKey = preSharedKey
self.pingableGateway = pingableGateway
}

public func makeConfiguration() throws -> TunnelAdapterConfiguration {
Expand All @@ -51,7 +54,8 @@ public struct ConfigurationBuilder {
interfaceAddresses: interfaceAddresses,
dns: dnsServers,
peer: try peer,
allowedIPs: allowedIPs
allowedIPs: allowedIPs,
pingableGateway: pingableGateway
)
}

Expand Down
17 changes: 12 additions & 5 deletions ios/PacketTunnelCore/Actor/ConnectionConfigurationBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
//

import Foundation
import MullvadTypes
import Network
import WireGuardKitTypes

protocol Configuration {
Expand Down Expand Up @@ -69,7 +71,8 @@ private struct NormalConnectionConfiguration: Configuration {
endpoint: connectionData.connectedEndpoint,
allowedIPs: [
IPAddressRange(from: "\(connectionData.selectedRelays.exit.endpoint.ipv4Relay.ip)/32")!,
]
],
pingableGateway: IPv4Address(LocalNetworkIPs.gatewayAddress.rawValue)!
).makeConfiguration()
} else {
nil
Expand All @@ -84,7 +87,8 @@ private struct NormalConnectionConfiguration: Configuration {
allowedIPs: [
IPAddressRange(from: "0.0.0.0/0")!,
IPAddressRange(from: "::/0")!,
]
],
pingableGateway: IPv4Address(LocalNetworkIPs.gatewayAddress.rawValue)!
).makeConfiguration()

return ConnectionConfiguration(
Expand Down Expand Up @@ -112,7 +116,8 @@ private struct EphemeralConnectionConfiguration: Configuration {
dns: settings.dnsServers,
endpoint: connectionData.connectedEndpoint,
allowedIPs: hop.configuration.allowedIPs,
preSharedKey: hop.configuration.preSharedKey
preSharedKey: hop.configuration.preSharedKey,
pingableGateway: IPv4Address(LocalNetworkIPs.gatewayAddress.rawValue)!
).makeConfiguration()

return ConnectionConfiguration(entryConfiguration: nil, exitConfiguration: exitConfiguration)
Expand All @@ -124,7 +129,8 @@ private struct EphemeralConnectionConfiguration: Configuration {
dns: settings.dnsServers,
endpoint: connectionData.connectedEndpoint,
allowedIPs: firstHop.configuration.allowedIPs,
preSharedKey: firstHop.configuration.preSharedKey
preSharedKey: firstHop.configuration.preSharedKey,
pingableGateway: IPv4Address(LocalNetworkIPs.gatewayAddress.rawValue)!
).makeConfiguration()

let exitConfiguration = try ConfigurationBuilder(
Expand All @@ -133,7 +139,8 @@ private struct EphemeralConnectionConfiguration: Configuration {
dns: settings.dnsServers,
endpoint: secondHop.relay.endpoint,
allowedIPs: secondHop.configuration.allowedIPs,
preSharedKey: secondHop.configuration.preSharedKey
preSharedKey: secondHop.configuration.preSharedKey,
pingableGateway: IPv4Address(LocalNetworkIPs.gatewayAddress.rawValue)!
).makeConfiguration()

return ConnectionConfiguration(entryConfiguration: entryConfiguration, exitConfiguration: exitConfiguration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ extension PacketTunnelActor {
let configurationBuilder = ConfigurationBuilder(
privateKey: PrivateKey(),
interfaceAddresses: [],
allowedIPs: []
allowedIPs: [],
pingableGateway: IPv4Address(LocalNetworkIPs.gatewayAddress.rawValue)!
)
var config = try configurationBuilder.makeConfiguration()
config.dns = [IPv4Address.loopback]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public struct TunnelAdapterConfiguration {
public var dns: [IPAddress]
public var peer: TunnelPeer?
public var allowedIPs: [IPAddressRange]
public var pingableGateway: IPv4Address
}

/// Struct describing a single peer.
Expand Down
4 changes: 2 additions & 2 deletions ios/PacketTunnelCore/Pinger/PingerProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public struct PingerSendResult {
public protocol PingerProtocol {
var onReply: ((PingerReply) -> Void)? { get set }

func openSocket(bindTo interfaceName: String?, destAddress: IPv4Address) throws
func closeSocket()
func startPinging(destAddress: IPv4Address) throws
func stopPinging()
func send() throws -> PingerSendResult
}
10 changes: 2 additions & 8 deletions ios/PacketTunnelCore/Pinger/TunnelPinger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@ public final class TunnelPinger: PingerProtocol {
self.logger = Logger(label: "TunnelPinger")
}

deinit {
pingProvider.closeICMP()
}

public func openSocket(bindTo interfaceName: String?, destAddress: IPv4Address) throws {
try pingProvider.openICMP(address: destAddress)
public func startPinging(destAddress: IPv4Address) throws {
stateLock.withLock {
self.destAddress = destAddress
}
Expand Down Expand Up @@ -64,10 +59,9 @@ public final class TunnelPinger: PingerProtocol {
}
}

public func closeSocket() {
public func stopPinging() {
stateLock.withLock {
self.destAddress = nil
pingProvider.closeICMP()
}
}

Expand Down
8 changes: 4 additions & 4 deletions ios/PacketTunnelCore/TunnelMonitor/TunnelMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,12 @@ public final class TunnelMonitor: TunnelMonitorProtocol {

private func startMonitoring() {
do {
guard let interfaceName = tunnelDeviceInfo.interfaceName, let probeAddress else {
logger.debug("Failed to obtain utun interface name or probe address.")
guard let probeAddress else {
logger.debug("Failed to obtain probe address.")
return
}

try pinger.openSocket(bindTo: interfaceName, destAddress: probeAddress)
try pinger.startPinging(destAddress: probeAddress)

state.connectionState = .connecting
startConnectivityCheckTimer()
Expand All @@ -314,7 +314,7 @@ public final class TunnelMonitor: TunnelMonitorProtocol {

private func stopMonitoring(resetRetryAttempt: Bool) {
stopConnectivityCheckTimer()
pinger.closeSocket()
pinger.stopPinging()

state.netStats = WgStats()
state.lastSeenRx = nil
Expand Down
4 changes: 2 additions & 2 deletions ios/PacketTunnelCoreTests/Mocks/PingerMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ class PingerMock: PingerProtocol {
self.decideOutcome = decideOutcome
}

func openSocket(bindTo interfaceName: String?, destAddress: IPv4Address) throws {
func startPinging(destAddress: IPv4Address) throws {
stateLock.withLock {
state.destAddress = destAddress
state.isSocketOpen = true
}
}

func closeSocket() {
func stopPinging() {
stateLock.withLock {
state.isSocketOpen = false
}
Expand Down

0 comments on commit d27c492

Please sign in to comment.