Skip to content

Commit

Permalink
Show the correct port of the endpoint the device is connected to
Browse files Browse the repository at this point in the history
  • Loading branch information
buggmagnet committed Dec 4, 2023
1 parent 2d026de commit 56ac2e6
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ final class SimulatorTunnelProviderHost: SimulatorTunnelProviderDelegate {
relayConstraints: try SettingsManager.readSettings().relayConstraints,
networkReachability: .reachable,
connectionAttemptCount: 0,
transportLayer: .udp
transportLayer: .udp,
remotePort: selectedRelay.endpoint.ipv4Relay.port
)
)
} catch {
Expand Down
12 changes: 1 addition & 11 deletions ios/MullvadVPN/View controllers/Tunnel/TunnelControlView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,7 @@ final class TunnelControlView: UIView {
cityLabel.attributedText = attributedStringForLocation(string: model.city)
countryLabel.attributedText = attributedStringForLocation(string: model.country)
connectionPanel.connectedRelayName = model.connectedRelayName

if let tunnelRelay = model.tunnelStatus.state.relay {
var protocolLayer = ""
if case let .connected(state) = model.tunnelStatus.observedState {
protocolLayer = state.transportLayer == .tcp ? "TCP" : "UDP"
}
connectionPanel.dataSource = ConnectionPanelData(
inAddress: "\(tunnelRelay.endpoint.ipv4Relay) \(protocolLayer)",
outAddress: model.connectionPanel.outAddress
)
}
connectionPanel.dataSource = model.connectionPanel

updateSecureLabel(tunnelState: tunnelState)
updateActionButtons(tunnelState: tunnelState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,25 @@ struct TunnelControlViewModel {
}

func update(outgoingConnectionInfo: OutgoingConnectionInfo) -> TunnelControlViewModel {
TunnelControlViewModel(
let inPort = tunnelStatus.observedState.connectionState?.remotePort ?? 0

var connectionPanelData = ConnectionPanelData(inAddress: "")
if let tunnelRelay = tunnelStatus.state.relay {
var protocolLayer = ""
if case let .connected(state) = tunnelStatus.observedState {
protocolLayer = state.transportLayer == .tcp ? "TCP" : "UDP"
}

connectionPanelData = ConnectionPanelData(
inAddress: "\(tunnelRelay.endpoint.ipv4Relay.ip):\(inPort) \(protocolLayer)",
outAddress: outgoingConnectionInfo.outAddress
)
}

return TunnelControlViewModel(
tunnelStatus: tunnelStatus,
secureLabelText: secureLabelText,
connectionPanel: ConnectionPanelData(
inAddress: "\(tunnelStatus.state.relay?.endpoint.ipv4Relay.description ?? "no info")",
outAddress: outgoingConnectionInfo.outAddress
),
connectionPanel: connectionPanelData,
enableButtons: enableButtons,
city: city,
country: country,
Expand Down
4 changes: 4 additions & 0 deletions ios/PacketTunnelCore/Actor/ObservedState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public struct ObservedConnectionState: Equatable, Codable {
public var networkReachability: NetworkReachability
public var connectionAttemptCount: UInt
public var transportLayer: TransportLayer
public var remotePort: UInt16
public var lastKeyRotation: Date?

public var isNetworkReachable: Bool {
Expand All @@ -41,13 +42,15 @@ public struct ObservedConnectionState: Equatable, Codable {
networkReachability: NetworkReachability,
connectionAttemptCount: UInt,
transportLayer: TransportLayer,
remotePort: UInt16,
lastKeyRotation: Date? = nil
) {
self.selectedRelay = selectedRelay
self.relayConstraints = relayConstraints
self.networkReachability = networkReachability
self.connectionAttemptCount = connectionAttemptCount
self.transportLayer = transportLayer
self.remotePort = remotePort
self.lastKeyRotation = lastKeyRotation
}
}
Expand Down Expand Up @@ -89,6 +92,7 @@ extension ConnectionState {
networkReachability: networkReachability,
connectionAttemptCount: connectionAttemptCount,
transportLayer: transportLayer,
remotePort: remotePort,
lastKeyRotation: lastKeyRotation
)
}
Expand Down
6 changes: 4 additions & 2 deletions ios/PacketTunnelCore/Actor/PacketTunnelActor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,8 @@ extension PacketTunnelActor {
connectionAttemptCount: connectionState.connectionAttemptCount,
lastKeyRotation: connectionState.lastKeyRotation,
connectedEndpoint: obfuscatedEndpoint,
transportLayer: transportLayer
transportLayer: transportLayer,
remotePort: protocolObfuscator.remotePort
)
}

Expand Down Expand Up @@ -401,7 +402,8 @@ extension PacketTunnelActor {
connectionAttemptCount: 0,
lastKeyRotation: lastKeyRotation,
connectedEndpoint: selectedRelay.endpoint,
transportLayer: .udp
transportLayer: .udp,
remotePort: selectedRelay.endpoint.ipv4Relay.port
)
}

Expand Down
5 changes: 5 additions & 0 deletions ios/PacketTunnelCore/Actor/ProtocolObfuscator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import TunnelObfuscation
public protocol ProtocolObfuscation {
func obfuscate(_ endpoint: MullvadEndpoint, settings: Settings, retryAttempts: UInt) -> MullvadEndpoint
var transportLayer: TransportLayer? { get }
var remotePort: UInt16 { get }
}

public class ProtocolObfuscator<Obfuscator: TunnelObfuscation>: ProtocolObfuscation {
Expand All @@ -34,8 +35,11 @@ public class ProtocolObfuscator<Obfuscator: TunnelObfuscation>: ProtocolObfuscat
return tunnelObfuscator.transportLayer
}

private(set) public var remotePort: UInt16 = 0

public func obfuscate(_ endpoint: MullvadEndpoint, settings: Settings, retryAttempts: UInt = 0) -> MullvadEndpoint {
var obfuscatedEndpoint = endpoint
remotePort = endpoint.ipv4Relay.port
let shouldObfuscate = switch settings.obfuscation.state {
case .automatic:
retryAttempts % 4 == 2 || retryAttempts % 4 == 3
Expand All @@ -57,6 +61,7 @@ public class ProtocolObfuscator<Obfuscator: TunnelObfuscation>: ProtocolObfuscat
remoteAddress: obfuscatedEndpoint.ipv4Relay.ip,
tcpPort: tcpPort.portValue
)
remotePort = tcpPort.portValue
obfuscator.start()
tunnelObfuscator = obfuscator

Expand Down
3 changes: 3 additions & 0 deletions ios/PacketTunnelCore/Actor/State.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ struct ConnectionState {
public let connectedEndpoint: MullvadEndpoint
/// Via which transport protocol was the connection made to the relay
public let transportLayer: TransportLayer

/// The remote port that was chosen to connect to `connectedEndpoint`
public let remotePort: UInt16
}

/// Data associated with error state.
Expand Down
2 changes: 2 additions & 0 deletions ios/PacketTunnelCoreTests/Mocks/ProtocolObfuscationStub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import Foundation
@testable import PacketTunnelCore

struct ProtocolObfuscationStub: ProtocolObfuscation {
var remotePort: UInt16 { 42 }

func obfuscate(_ endpoint: MullvadEndpoint, settings: Settings, retryAttempts: UInt) -> MullvadEndpoint {
endpoint
}
Expand Down
3 changes: 3 additions & 0 deletions ios/TunnelObfuscation/UDPOverTCPObfuscator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public protocol TunnelObfuscation {
func start()
func stop()
var localUdpPort: UInt16 { get }
var remotePort: UInt16 { get }

var transportLayer: TransportLayer { get }
}
Expand All @@ -35,6 +36,8 @@ public final class UDPOverTCPObfuscator: TunnelObfuscation {
return stateLock.withLock { proxyHandle.port }
}

public var remotePort: UInt16 { tcpPort }

public var transportLayer: TransportLayer { .tcp }

/// Initialize tunnel obfuscator with remote server address and TCP port where udp2tcp is running.
Expand Down

0 comments on commit 56ac2e6

Please sign in to comment.