Skip to content

Commit

Permalink
Log same-ip on each connection attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrej Mihajlov committed Oct 12, 2023
1 parent b227392 commit d40a0ec
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 33 deletions.
31 changes: 0 additions & 31 deletions ios/PacketTunnel/PacketTunnelProvider/PacketTunnelProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
private var appMessageHandler: AppMessageHandler!
private var stateObserverTask: AnyTask?
private var deviceChecker: DeviceChecker!
private var isLoggedSameIP = false

override init() {
Self.configureLogging()
Expand Down Expand Up @@ -121,36 +120,6 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
}
}

extension PacketTunnelProvider {
override func setTunnelNetworkSettings(
_ tunnelNetworkSettings: NETunnelNetworkSettings?,
completionHandler: ((Error?) -> Void)? = nil
) {
if let networkSettings = tunnelNetworkSettings as? NEPacketTunnelNetworkSettings {
let ipv4Addresses = networkSettings.ipv4Settings?.addresses.compactMap { IPv4Address($0) } ?? []
let ipv6Addresses = networkSettings.ipv6Settings?.addresses.compactMap { IPv6Address($0) } ?? []
let allIPAddresses: [IPAddress] = ipv4Addresses + ipv6Addresses

if !allIPAddresses.isEmpty, !isLoggedSameIP {
isLoggedSameIP = true
logIfDeviceHasSameIP(than: allIPAddresses)
}
}

super.setTunnelNetworkSettings(tunnelNetworkSettings, completionHandler: completionHandler)
}

private func logIfDeviceHasSameIP(than addresses: [IPAddress]) {
let hasIPv4SameAddress = addresses.compactMap { $0 as? IPv4Address }
.contains { $0 == ApplicationConfiguration.sameIPv4 }
let hasIPv6SameAddress = addresses.compactMap { $0 as? IPv6Address }
.contains { $0 == ApplicationConfiguration.sameIPv6 }

let isUsingSameIP = (hasIPv4SameAddress || hasIPv6SameAddress) ? "" : "NOT "
providerLogger.debug("Same IP is \(isUsingSameIP)being used")
}
}

extension PacketTunnelProvider {
private static func configureLogging() {
var loggerBuilder = LoggerBuilder()
Expand Down
21 changes: 19 additions & 2 deletions ios/PacketTunnel/WireGuardAdapter/WgAdapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ import PacketTunnelCore
import WireGuardKit

struct WgAdapter: TunnelAdapterProtocol {
let logger = Logger(label: "WgAdapter")
let adapter: WireGuardAdapter

init(packetTunnelProvider: NEPacketTunnelProvider) {
let logger = Logger(label: "WireGuard")
let wgGoLogger = Logger(label: "WireGuard")

adapter = WireGuardAdapter(
with: packetTunnelProvider,
shouldHandleReasserting: false,
logHandler: { logLevel, string in
logger.log(level: logLevel.loggerLevel, "\(string)")
wgGoLogger.log(level: logLevel.loggerLevel, "\(string)")
}
)
}
Expand All @@ -35,11 +36,27 @@ struct WgAdapter: TunnelAdapterProtocol {
} catch WireGuardAdapterError.invalidState {
try await adapter.start(tunnelConfiguration: wgConfig)
}

let tunAddresses = wgConfig.interface.addresses.map { $0.address }
// TUN addresses can be empty when adapter is configured for blocked state.
if !tunAddresses.isEmpty {
logIfDeviceHasSameIP(than: tunAddresses)
}
}

func stop() async throws {
try await adapter.stop()
}

private func logIfDeviceHasSameIP(than addresses: [IPAddress]) {
let hasIPv4SameAddress = addresses.compactMap { $0 as? IPv4Address }
.contains { $0 == ApplicationConfiguration.sameIPv4 }
let hasIPv6SameAddress = addresses.compactMap { $0 as? IPv6Address }
.contains { $0 == ApplicationConfiguration.sameIPv6 }

let isUsingSameIP = (hasIPv4SameAddress || hasIPv6SameAddress) ? "" : "NOT "
logger.debug("Same IP is \(isUsingSameIP)being used")
}
}

extension WgAdapter: TunnelDeviceInfoProtocol {
Expand Down

0 comments on commit d40a0ec

Please sign in to comment.