From 36f0b2c03dbf9fc28b3c33a2a07124a3adea31f7 Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Thu, 13 Oct 2022 18:44:44 +0200 Subject: [PATCH] Extend description of link remote with protocol --- Sources/TunnelKitCore/LinkInterface.swift | 5 ++++- Sources/TunnelKitOpenVPNAppExtension/NETCPLink.swift | 9 ++++++++- Sources/TunnelKitOpenVPNAppExtension/NEUDPLink.swift | 9 ++++++++- .../OpenVPNTunnelProvider.swift | 9 ++++++--- Sources/TunnelKitOpenVPNProtocol/OpenVPNSession.swift | 10 ++++++++-- 5 files changed, 34 insertions(+), 8 deletions(-) diff --git a/Sources/TunnelKitCore/LinkInterface.swift b/Sources/TunnelKitCore/LinkInterface.swift index 081a2f4..38f5396 100644 --- a/Sources/TunnelKitCore/LinkInterface.swift +++ b/Sources/TunnelKitCore/LinkInterface.swift @@ -38,13 +38,16 @@ import Foundation /// Represents a specific I/O interface meant to work at the link layer (e.g. TCP/IP). public protocol LinkInterface: IOInterface { - + /// When `true`, packets delivery is guaranteed. var isReliable: Bool { get } /// The literal address of the remote host. var remoteAddress: String? { get } + /// A literal describing the remote protocol. + var remoteProtocol: String? { get } + /// The number of packets that this interface is able to bufferize. var packetBufferSize: Int { get } diff --git a/Sources/TunnelKitOpenVPNAppExtension/NETCPLink.swift b/Sources/TunnelKitOpenVPNAppExtension/NETCPLink.swift index d99f78e..e2fe539 100644 --- a/Sources/TunnelKitOpenVPNAppExtension/NETCPLink.swift +++ b/Sources/TunnelKitOpenVPNAppExtension/NETCPLink.swift @@ -47,9 +47,16 @@ class NETCPLink: LinkInterface { let isReliable: Bool = true var remoteAddress: String? { - return (impl.remoteAddress as? NWHostEndpoint)?.hostname + (impl.remoteAddress as? NWHostEndpoint)?.hostname } + var remoteProtocol: String? { + guard let remote = impl.remoteAddress as? NWHostEndpoint else { + return nil + } + return "TCP:\(remote.port)" + } + var packetBufferSize: Int { return maxPacketSize } diff --git a/Sources/TunnelKitOpenVPNAppExtension/NEUDPLink.swift b/Sources/TunnelKitOpenVPNAppExtension/NEUDPLink.swift index 5d9174e..1da5f8b 100644 --- a/Sources/TunnelKitOpenVPNAppExtension/NEUDPLink.swift +++ b/Sources/TunnelKitOpenVPNAppExtension/NEUDPLink.swift @@ -46,9 +46,16 @@ class NEUDPLink: LinkInterface { let isReliable: Bool = false var remoteAddress: String? { - return (impl.resolvedEndpoint as? NWHostEndpoint)?.hostname + (impl.resolvedEndpoint as? NWHostEndpoint)?.hostname } + var remoteProtocol: String? { + guard let remote = impl.resolvedEndpoint as? NWHostEndpoint else { + return nil + } + return "UDP:\(remote.port)" + } + var packetBufferSize: Int { return maxDatagrams } diff --git a/Sources/TunnelKitOpenVPNAppExtension/OpenVPNTunnelProvider.swift b/Sources/TunnelKitOpenVPNAppExtension/OpenVPNTunnelProvider.swift index f1785a2..77415ed 100644 --- a/Sources/TunnelKitOpenVPNAppExtension/OpenVPNTunnelProvider.swift +++ b/Sources/TunnelKitOpenVPNAppExtension/OpenVPNTunnelProvider.swift @@ -480,11 +480,14 @@ extension OpenVPNTunnelProvider: OpenVPNSessionDelegate { // MARK: OpenVPNSessionDelegate (tunnel queue) - public func sessionDidStart(_ session: OpenVPNSession, remoteAddress: String, options: OpenVPN.Configuration) { + public func sessionDidStart(_ session: OpenVPNSession, remoteAddress: String, remoteProtocol: String?, options: OpenVPN.Configuration) { log.info("Session did start") - + log.info("\tAddress: \(remoteAddress.maskedDescription)") + if let proto = remoteProtocol { + log.info("\tProtocol: \(proto)") + } + log.info("Returned ifconfig parameters:") - log.info("\tRemote: \(remoteAddress.maskedDescription)") log.info("\tIPv4: \(options.ipv4?.description ?? "not configured")") log.info("\tIPv6: \(options.ipv6?.description ?? "not configured")") if let routingPolicies = options.routingPolicies { diff --git a/Sources/TunnelKitOpenVPNProtocol/OpenVPNSession.swift b/Sources/TunnelKitOpenVPNProtocol/OpenVPNSession.swift index ecbee7d..91bc371 100644 --- a/Sources/TunnelKitOpenVPNProtocol/OpenVPNSession.swift +++ b/Sources/TunnelKitOpenVPNProtocol/OpenVPNSession.swift @@ -50,9 +50,10 @@ public protocol OpenVPNSessionDelegate: AnyObject { Called after starting a session. - Parameter remoteAddress: The address of the VPN server. + - Parameter remoteProtocol: The protocol of the VPN server, if specified. - Parameter options: The pulled tunnel settings. */ - func sessionDidStart(_: OpenVPNSession, remoteAddress: String, options: OpenVPN.Configuration) + func sessionDidStart(_: OpenVPNSession, remoteAddress: String, remoteProtocol: String?, options: OpenVPN.Configuration) /** Called after stopping a session. @@ -981,7 +982,12 @@ public class OpenVPNSession: Session { guard let remoteAddress = link?.remoteAddress else { fatalError("Could not resolve link remote address") } - delegate?.sessionDidStart(self, remoteAddress: remoteAddress, options: reply.options) + delegate?.sessionDidStart( + self, + remoteAddress: remoteAddress, + remoteProtocol: link?.remoteProtocol, + options: reply.options + ) scheduleNextPing() }