Make AppExtension generic

- Make AppExtension a standalone util subspec
- Move OpenVPN tunnel provider to OpenVPN subspec
- Move Utils to Core subspec
- Depend OpenVPN on Core + AppExtension
This commit is contained in:
Davide De Rosa 2019-05-23 20:15:42 +02:00
parent 011a407edd
commit 9445b825d0
14 changed files with 278 additions and 166 deletions

View File

@ -12,7 +12,7 @@ Pod::Spec.new do |s|
s.ios.deployment_target = "11.0"
s.osx.deployment_target = "10.11"
s.default_subspecs = "Core", "OpenVPN", "AppExtension"
s.default_subspecs = "OpenVPN"
s.subspec "Core" do |p|
p.source_files = "TunnelKit/Sources/Core/**/*.{h,m,swift}"
@ -25,6 +25,15 @@ Pod::Spec.new do |s|
p.libraries = "resolv"
end
s.subspec "AppExtension" do |p|
p.source_files = "TunnelKit/Sources/AppExtension/**/*.swift"
p.frameworks = "NetworkExtension"
p.pod_target_xcconfig = { "APPLICATION_EXTENSION_API_ONLY" => "YES" }
p.dependency "SwiftyBeaver"
p.dependency "TunnelKit/Core"
end
s.subspec "OpenVPN" do |p|
p.source_files = "TunnelKit/Sources/OpenVPN/**/*.{h,m,swift}"
p.private_header_files = "TunnelKit/Sources/OpenVPN/**/*.h"
@ -34,14 +43,7 @@ Pod::Spec.new do |s|
"APPLICATION_EXTENSION_API_ONLY" => "YES" }
p.dependency "TunnelKit/Core"
end
s.subspec "AppExtension" do |p|
p.source_files = "TunnelKit/Sources/AppExtension/**/*.swift"
p.frameworks = "NetworkExtension"
p.pod_target_xcconfig = { "APPLICATION_EXTENSION_API_ONLY" => "YES" }
p.dependency "TunnelKit/OpenVPN"
p.dependency "TunnelKit/AppExtension"
end
s.subspec "LZO" do |p|

View File

@ -51,6 +51,12 @@
0E12B2A921456C0200B4BAE9 /* ControlChannel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E12B2A721456C0200B4BAE9 /* ControlChannel.swift */; };
0E12B2AB2145E01700B4BAE9 /* ControlChannelSerializer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E12B2AA2145E01700B4BAE9 /* ControlChannelSerializer.swift */; };
0E12B2AC2145E01700B4BAE9 /* ControlChannelSerializer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E12B2AA2145E01700B4BAE9 /* ControlChannelSerializer.swift */; };
0E23B3D02297192900304C30 /* NETCPLink.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E23B3CF2297192900304C30 /* NETCPLink.swift */; };
0E23B3D12297192900304C30 /* NETCPLink.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E23B3CF2297192900304C30 /* NETCPLink.swift */; };
0E23B3D32297193000304C30 /* NEUDPLink.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E23B3D22297193000304C30 /* NEUDPLink.swift */; };
0E23B3D42297193000304C30 /* NEUDPLink.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E23B3D22297193000304C30 /* NEUDPLink.swift */; };
0E23B3DE229749C600304C30 /* LinkProducer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E23B3DD229749C600304C30 /* LinkProducer.swift */; };
0E23B3DF229749C600304C30 /* LinkProducer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E23B3DD229749C600304C30 /* LinkProducer.swift */; };
0E245D6C2137F73600B012A2 /* CompressionFramingNative.h in Headers */ = {isa = PBXBuildFile; fileRef = 0E245D6B2137F73600B012A2 /* CompressionFramingNative.h */; };
0E39BCE8214B2AB60035E9DE /* ControlPacket.h in Headers */ = {isa = PBXBuildFile; fileRef = 0E39BCE6214B2AB60035E9DE /* ControlPacket.h */; };
0E39BCE9214B2AB60035E9DE /* ControlPacket.h in Headers */ = {isa = PBXBuildFile; fileRef = 0E39BCE6214B2AB60035E9DE /* ControlPacket.h */; };
@ -130,14 +136,14 @@
0EE2F9AD2291853D00F56F49 /* Session.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0EE2F9AB2291853D00F56F49 /* Session.swift */; };
0EE2F9EE22918DA100F56F49 /* ConnectionStrategy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0EE2F9DE22918DA100F56F49 /* ConnectionStrategy.swift */; };
0EE2F9EF22918DA100F56F49 /* ConnectionStrategy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0EE2F9DE22918DA100F56F49 /* ConnectionStrategy.swift */; };
0EE2F9F022918DA100F56F49 /* NETCPInterface.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0EE2F9E022918DA100F56F49 /* NETCPInterface.swift */; };
0EE2F9F122918DA100F56F49 /* NETCPInterface.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0EE2F9E022918DA100F56F49 /* NETCPInterface.swift */; };
0EE2F9F022918DA100F56F49 /* NETCPSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0EE2F9E022918DA100F56F49 /* NETCPSocket.swift */; };
0EE2F9F122918DA100F56F49 /* NETCPSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0EE2F9E022918DA100F56F49 /* NETCPSocket.swift */; };
0EE2F9F222918DA100F56F49 /* NETunnelInterface.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0EE2F9E122918DA100F56F49 /* NETunnelInterface.swift */; };
0EE2F9F322918DA100F56F49 /* NETunnelInterface.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0EE2F9E122918DA100F56F49 /* NETunnelInterface.swift */; };
0EE2F9F422918DA100F56F49 /* NWTCPConnectionState+Description.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0EE2F9E222918DA100F56F49 /* NWTCPConnectionState+Description.swift */; };
0EE2F9F522918DA100F56F49 /* NWTCPConnectionState+Description.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0EE2F9E222918DA100F56F49 /* NWTCPConnectionState+Description.swift */; };
0EE2F9F622918DA100F56F49 /* NEUDPInterface.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0EE2F9E322918DA100F56F49 /* NEUDPInterface.swift */; };
0EE2F9F722918DA100F56F49 /* NEUDPInterface.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0EE2F9E322918DA100F56F49 /* NEUDPInterface.swift */; };
0EE2F9F622918DA100F56F49 /* NEUDPSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0EE2F9E322918DA100F56F49 /* NEUDPSocket.swift */; };
0EE2F9F722918DA100F56F49 /* NEUDPSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0EE2F9E322918DA100F56F49 /* NEUDPSocket.swift */; };
0EE2F9F822918DA100F56F49 /* NWUDPSessionState+Description.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0EE2F9E422918DA100F56F49 /* NWUDPSessionState+Description.swift */; };
0EE2F9F922918DA100F56F49 /* NWUDPSessionState+Description.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0EE2F9E422918DA100F56F49 /* NWUDPSessionState+Description.swift */; };
0EE2F9FA22918DA100F56F49 /* DNSResolver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0EE2F9E522918DA100F56F49 /* DNSResolver.swift */; };
@ -330,6 +336,9 @@
0E12B2A721456C0200B4BAE9 /* ControlChannel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ControlChannel.swift; sourceTree = "<group>"; };
0E12B2AA2145E01700B4BAE9 /* ControlChannelSerializer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ControlChannelSerializer.swift; sourceTree = "<group>"; };
0E17D7F91F730D9F009EE129 /* TunnelKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = TunnelKit.framework; sourceTree = BUILT_PRODUCTS_DIR; };
0E23B3CF2297192900304C30 /* NETCPLink.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NETCPLink.swift; sourceTree = "<group>"; };
0E23B3D22297193000304C30 /* NEUDPLink.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NEUDPLink.swift; sourceTree = "<group>"; };
0E23B3DD229749C600304C30 /* LinkProducer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinkProducer.swift; sourceTree = "<group>"; };
0E245D6B2137F73600B012A2 /* CompressionFramingNative.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CompressionFramingNative.h; sourceTree = "<group>"; };
0E3251C51F95770D00C108D9 /* TunnelKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = TunnelKit.framework; sourceTree = BUILT_PRODUCTS_DIR; };
0E39BCE6214B2AB60035E9DE /* ControlPacket.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ControlPacket.h; sourceTree = "<group>"; };
@ -378,10 +387,10 @@
0EE2F9792291817300F56F49 /* Errors.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Errors.swift; sourceTree = "<group>"; };
0EE2F9AB2291853D00F56F49 /* Session.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Session.swift; sourceTree = "<group>"; };
0EE2F9DE22918DA100F56F49 /* ConnectionStrategy.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConnectionStrategy.swift; sourceTree = "<group>"; };
0EE2F9E022918DA100F56F49 /* NETCPInterface.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NETCPInterface.swift; sourceTree = "<group>"; };
0EE2F9E022918DA100F56F49 /* NETCPSocket.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NETCPSocket.swift; sourceTree = "<group>"; };
0EE2F9E122918DA100F56F49 /* NETunnelInterface.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NETunnelInterface.swift; sourceTree = "<group>"; };
0EE2F9E222918DA100F56F49 /* NWTCPConnectionState+Description.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NWTCPConnectionState+Description.swift"; sourceTree = "<group>"; };
0EE2F9E322918DA100F56F49 /* NEUDPInterface.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NEUDPInterface.swift; sourceTree = "<group>"; };
0EE2F9E322918DA100F56F49 /* NEUDPSocket.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NEUDPSocket.swift; sourceTree = "<group>"; };
0EE2F9E422918DA100F56F49 /* NWUDPSessionState+Description.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NWUDPSessionState+Description.swift"; sourceTree = "<group>"; };
0EE2F9E522918DA100F56F49 /* DNSResolver.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DNSResolver.swift; sourceTree = "<group>"; };
0EE2F9E622918DA100F56F49 /* TunnelKitProvider+Configuration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "TunnelKitProvider+Configuration.swift"; sourceTree = "<group>"; };
@ -602,6 +611,19 @@
path = Sources;
sourceTree = "<group>";
};
0E23B3CE2297190E00304C30 /* AppExtension */ = {
isa = PBXGroup;
children = (
0EE2F9DE22918DA100F56F49 /* ConnectionStrategy.swift */,
0E23B3CF2297192900304C30 /* NETCPLink.swift */,
0E23B3D22297193000304C30 /* NEUDPLink.swift */,
0EE2F9EA22918DA100F56F49 /* TunnelKitProvider.swift */,
0EE2F9E622918DA100F56F49 /* TunnelKitProvider+Configuration.swift */,
0EE2F9EC22918DA100F56F49 /* TunnelKitProvider+Interaction.swift */,
);
path = AppExtension;
sourceTree = "<group>";
};
0E58BEDC2240521F006FB157 /* LZO */ = {
isa = PBXGroup;
children = (
@ -657,6 +679,7 @@
0EB03E0A2290A338006D03A0 /* OpenVPN */ = {
isa = PBXGroup;
children = (
0E23B3CE2297190E00304C30 /* AppExtension */,
0EFEB43A2006D3C800F81029 /* Authenticator.swift */,
0E58BF522240FAA6006FB157 /* CompressionAlgorithm.swift */,
0E58BF4F2240F98E006FB157 /* CompressionAlgorithmNative.h */,
@ -713,16 +736,11 @@
isa = PBXGroup;
children = (
0EE2F9DF22918DA100F56F49 /* Transport */,
0EE2F9DE22918DA100F56F49 /* ConnectionStrategy.swift */,
0EE2F9E522918DA100F56F49 /* DNSResolver.swift */,
0EE2F9E922918DA100F56F49 /* GenericSocket.swift */,
0EE2F9ED22918DA100F56F49 /* InterfaceObserver.swift */,
0EE2F9E722918DA100F56F49 /* Keychain.swift */,
0E23B3DD229749C600304C30 /* LinkProducer.swift */,
0EE2F9E822918DA100F56F49 /* MemoryDestination.swift */,
0EE2F9EA22918DA100F56F49 /* TunnelKitProvider.swift */,
0EE2F9E622918DA100F56F49 /* TunnelKitProvider+Configuration.swift */,
0EE2F9EC22918DA100F56F49 /* TunnelKitProvider+Interaction.swift */,
0EE2F9EB22918DA100F56F49 /* Utils.swift */,
);
path = AppExtension;
sourceTree = "<group>";
@ -730,10 +748,10 @@
0EE2F9DF22918DA100F56F49 /* Transport */ = {
isa = PBXGroup;
children = (
0EE2F9E022918DA100F56F49 /* NETCPInterface.swift */,
0EE2F9E022918DA100F56F49 /* NETCPSocket.swift */,
0EE2F9E122918DA100F56F49 /* NETunnelInterface.swift */,
0EE2F9E322918DA100F56F49 /* NEUDPSocket.swift */,
0EE2F9E222918DA100F56F49 /* NWTCPConnectionState+Description.swift */,
0EE2F9E322918DA100F56F49 /* NEUDPInterface.swift */,
0EE2F9E422918DA100F56F49 /* NWUDPSessionState+Description.swift */,
);
path = Transport;
@ -750,6 +768,7 @@
0EFEB4432006D3C800F81029 /* Data+Manipulation.swift */,
0E411B992271F90700E0852C /* DNS.h */,
0E411B9A2271F90700E0852C /* DNS.m */,
0EE2F9E522918DA100F56F49 /* DNSResolver.swift */,
0E011F7C2196D97200BA59EE /* EndpointProtocol.swift */,
0EFEB4362006D3C800F81029 /* Errors.h */,
0EFEB44B2006D3C800F81029 /* Errors.m */,
@ -771,6 +790,7 @@
0EE2F9AB2291853D00F56F49 /* Session.swift */,
0E011F792196D93600BA59EE /* SocketType.swift */,
0EFEB42F2006D3C800F81029 /* TunnelInterface.swift */,
0EE2F9EB22918DA100F56F49 /* Utils.swift */,
0EFEB4412006D3C800F81029 /* ZeroingData.h */,
0EFEB4312006D3C800F81029 /* ZeroingData.m */,
0EFEB43B2006D3C800F81029 /* ZeroingData.swift */,
@ -1273,12 +1293,14 @@
0EE2F974229163C900F56F49 /* Proxy.swift in Sources */,
0EFEB4732006D3C800F81029 /* LinkInterface.swift in Sources */,
0EFEB4652006D3C800F81029 /* Authenticator.swift in Sources */,
0E23B3DE229749C600304C30 /* LinkProducer.swift in Sources */,
0EE2F9F222918DA100F56F49 /* NETunnelInterface.swift in Sources */,
0EE2F9FE22918DA100F56F49 /* Keychain.swift in Sources */,
0EE7A79820F6296F00B42E6A /* PacketMacros.m in Sources */,
0EFEB4562006D3C800F81029 /* SessionKey.swift in Sources */,
0E0B20422278A85C007A3CB9 /* RoutingTableEntry.m in Sources */,
0E58BF5922411FEF006FB157 /* LZO.m in Sources */,
0E23B3D32297193000304C30 /* NEUDPLink.swift in Sources */,
0E12B29E21449ADB00B4BAE9 /* NSRegularExpression+Shortcuts.swift in Sources */,
0EE2F9F822918DA100F56F49 /* NWUDPSessionState+Description.swift in Sources */,
0E749F5F2178885500BB2701 /* OpenVPNSession+PIA.swift in Sources */,
@ -1288,7 +1310,7 @@
0EFEB45D2006D3C800F81029 /* CryptoBox.m in Sources */,
0E3B15C92152B05E00984B17 /* CryptoCTR.m in Sources */,
0E011F7D2196D97200BA59EE /* EndpointProtocol.swift in Sources */,
0EE2F9F022918DA100F56F49 /* NETCPInterface.swift in Sources */,
0EE2F9F022918DA100F56F49 /* NETCPSocket.swift in Sources */,
0E0C2125212ED29D008AB282 /* OpenVPNError.swift in Sources */,
0E12B2A821456C0200B4BAE9 /* ControlChannel.swift in Sources */,
0EFEB4552006D3C800F81029 /* EncryptionBridge.swift in Sources */,
@ -1309,11 +1331,12 @@
0EE2FA0822918DA100F56F49 /* TunnelKitProvider+Interaction.swift in Sources */,
0EE2FA0422918DA100F56F49 /* TunnelKitProvider.swift in Sources */,
0EE2F9FA22918DA100F56F49 /* DNSResolver.swift in Sources */,
0EE2F9F622918DA100F56F49 /* NEUDPInterface.swift in Sources */,
0EE2F9F622918DA100F56F49 /* NEUDPSocket.swift in Sources */,
0E39BCEA214B2AB60035E9DE /* ControlPacket.m in Sources */,
0EE2F96E2291636B00F56F49 /* IPv4Settings.swift in Sources */,
0E12B2AB2145E01700B4BAE9 /* ControlChannelSerializer.swift in Sources */,
0EFEB4662006D3C800F81029 /* ZeroingData.swift in Sources */,
0E23B3D02297192900304C30 /* NETCPLink.swift in Sources */,
0EFEB4682006D3C800F81029 /* MSS.m in Sources */,
0EE2FA0622918DA100F56F49 /* Utils.swift in Sources */,
0E48AC662271ADA9009B1A98 /* PacketStream.m in Sources */,
@ -1352,12 +1375,14 @@
0EE2F975229163C900F56F49 /* Proxy.swift in Sources */,
0EFEB4A12006D7F300F81029 /* LinkInterface.swift in Sources */,
0EE7A79920F6296F00B42E6A /* PacketMacros.m in Sources */,
0E23B3DF229749C600304C30 /* LinkProducer.swift in Sources */,
0EE2F9F322918DA100F56F49 /* NETunnelInterface.swift in Sources */,
0EE2F9FF22918DA100F56F49 /* Keychain.swift in Sources */,
0E0B20432278A85C007A3CB9 /* RoutingTableEntry.m in Sources */,
0E58BF5A22411FEF006FB157 /* LZO.m in Sources */,
0E12B29F21449ADB00B4BAE9 /* NSRegularExpression+Shortcuts.swift in Sources */,
0EFEB4A02006D7F300F81029 /* ReplayProtector.m in Sources */,
0E23B3D42297193000304C30 /* NEUDPLink.swift in Sources */,
0E749F602178885500BB2701 /* OpenVPNSession+PIA.swift in Sources */,
0EE2F9F922918DA100F56F49 /* NWUDPSessionState+Description.swift in Sources */,
0EFEB4992006D7F300F81029 /* OpenVPNSession.swift in Sources */,
@ -1367,7 +1392,7 @@
0E3B15CA2152B05E00984B17 /* CryptoCTR.m in Sources */,
0E011F7E2196D97200BA59EE /* EndpointProtocol.swift in Sources */,
0E0C2126212ED29D008AB282 /* OpenVPNError.swift in Sources */,
0EE2F9F122918DA100F56F49 /* NETCPInterface.swift in Sources */,
0EE2F9F122918DA100F56F49 /* NETCPSocket.swift in Sources */,
0E12B2A921456C0200B4BAE9 /* ControlChannel.swift in Sources */,
0EFEB4982006D7F300F81029 /* ZeroingData.swift in Sources */,
0EFEB4A32006D7F300F81029 /* Errors.m in Sources */,
@ -1388,11 +1413,12 @@
0EE2FA0922918DA100F56F49 /* TunnelKitProvider+Interaction.swift in Sources */,
0EE2FA0522918DA100F56F49 /* TunnelKitProvider.swift in Sources */,
0EE2F9FB22918DA100F56F49 /* DNSResolver.swift in Sources */,
0EE2F9F722918DA100F56F49 /* NEUDPInterface.swift in Sources */,
0EE2F9F722918DA100F56F49 /* NEUDPSocket.swift in Sources */,
0E39BCEB214B2AB60035E9DE /* ControlPacket.m in Sources */,
0EE2F96F2291636B00F56F49 /* IPv4Settings.swift in Sources */,
0E12B2AC2145E01700B4BAE9 /* ControlChannelSerializer.swift in Sources */,
0E07598120F0060E00F38FD8 /* CryptoAEAD.m in Sources */,
0E23B3D12297192900304C30 /* NETCPLink.swift in Sources */,
0EFEB49C2006D7F300F81029 /* Data+Manipulation.swift in Sources */,
0EE2FA0722918DA100F56F49 /* Utils.swift in Sources */,
0E48AC672271ADA9009B1A98 /* PacketStream.m in Sources */,

View File

@ -36,10 +36,6 @@
import Foundation
protocol LinkProducer {
func link(withMTU mtu: Int) -> LinkInterface
}
protocol GenericSocketDelegate: class {
func socketDidTimeout(_ socket: GenericSocket)
@ -50,7 +46,7 @@ protocol GenericSocketDelegate: class {
func socketHasBetterPath(_ socket: GenericSocket)
}
protocol GenericSocket: LinkProducer {
protocol GenericSocket {
var remoteAddress: String? { get }
var hasBetterPath: Bool { get }

View File

@ -0,0 +1,30 @@
//
// LinkProducer.swift
// TunnelKit
//
// Created by Davide De Rosa on 5/23/19.
// Copyright (c) 2019 Davide De Rosa. All rights reserved.
//
// https://github.com/passepartoutvpn
//
// This file is part of TunnelKit.
//
// TunnelKit is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// TunnelKit is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with TunnelKit. If not, see <http://www.gnu.org/licenses/>.
//
import Foundation
protocol LinkProducer {
func link(withMTU mtu: Int) -> LinkInterface
}

View File

@ -37,14 +37,13 @@
import Foundation
import NetworkExtension
import SwiftyBeaver
import __TunnelKitOpenVPN
private let log = SwiftyBeaver.self
class NETCPSocket: NSObject, GenericSocket {
private static var linkContext = 0
private let impl: NWTCPConnection
let impl: NWTCPConnection
init(impl: NWTCPConnection) {
self.impl = impl
@ -104,10 +103,6 @@ class NETCPSocket: NSObject, GenericSocket {
return NETCPSocket(impl: NWTCPConnection(upgradeFor: impl))
}
func link(withMTU mtu: Int) -> LinkInterface {
return NETCPLink(impl: impl, mtu: mtu)
}
// MARK: Connection KVO (any queue)
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
@ -175,75 +170,6 @@ class NETCPSocket: NSObject, GenericSocket {
}
}
class NETCPLink: LinkInterface {
private let impl: NWTCPConnection
private let maxPacketSize: Int
init(impl: NWTCPConnection, mtu: Int, maxPacketSize: Int? = nil) {
self.impl = impl
self.mtu = mtu
self.maxPacketSize = maxPacketSize ?? (512 * 1024)
}
// MARK: LinkInterface
let isReliable: Bool = true
var remoteAddress: String? {
return (impl.remoteAddress as? NWHostEndpoint)?.hostname
}
let mtu: Int
var packetBufferSize: Int {
return maxPacketSize
}
func setReadHandler(queue: DispatchQueue, _ handler: @escaping ([Data]?, Error?) -> Void) {
loopReadPackets(queue, Data(), handler)
}
private func loopReadPackets(_ queue: DispatchQueue, _ buffer: Data, _ handler: @escaping ([Data]?, Error?) -> Void) {
// WARNING: runs in Network.framework queue
impl.readMinimumLength(2, maximumLength: packetBufferSize) { [weak self] (data, error) in
guard let _ = self else {
return
}
queue.sync {
guard (error == nil), let data = data else {
handler(nil, error)
return
}
var newBuffer = buffer
newBuffer.append(contentsOf: data)
var until = 0
let packets = PacketStream.packets(fromStream: newBuffer, until: &until)
newBuffer = newBuffer.subdata(in: until..<newBuffer.count)
self?.loopReadPackets(queue, newBuffer, handler)
handler(packets, nil)
}
}
}
func writePacket(_ packet: Data, completionHandler: ((Error?) -> Void)?) {
let stream = PacketStream.stream(fromPacket: packet)
impl.write(stream) { (error) in
completionHandler?(error)
}
}
func writePackets(_ packets: [Data], completionHandler: ((Error?) -> Void)?) {
let stream = PacketStream.stream(fromPackets: packets)
impl.write(stream) { (error) in
completionHandler?(error)
}
}
}
extension NETCPSocket {
override var description: String {
guard let hostEndpoint = impl.endpoint as? NWHostEndpoint else {

View File

@ -43,7 +43,7 @@ private let log = SwiftyBeaver.self
class NEUDPSocket: NSObject, GenericSocket {
private static var linkContext = 0
private let impl: NWUDPSession
let impl: NWUDPSession
init(impl: NWUDPSession) {
self.impl = impl
@ -103,10 +103,6 @@ class NEUDPSocket: NSObject, GenericSocket {
return NEUDPSocket(impl: NWUDPSession(upgradeFor: impl))
}
func link(withMTU mtu: Int) -> LinkInterface {
return NEUDPLink(impl: impl, mtu: mtu)
}
// MARK: Connection KVO (any queue)
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
@ -177,57 +173,6 @@ class NEUDPSocket: NSObject, GenericSocket {
}
}
class NEUDPLink: LinkInterface {
private let impl: NWUDPSession
private let maxDatagrams: Int
init(impl: NWUDPSession, mtu: Int, maxDatagrams: Int? = nil) {
self.impl = impl
self.mtu = mtu
self.maxDatagrams = maxDatagrams ?? 200
}
// MARK: LinkInterface
let isReliable: Bool = false
var remoteAddress: String? {
return (impl.resolvedEndpoint as? NWHostEndpoint)?.hostname
}
let mtu: Int
var packetBufferSize: Int {
return maxDatagrams
}
func setReadHandler(queue: DispatchQueue, _ handler: @escaping ([Data]?, Error?) -> Void) {
// WARNING: runs in Network.framework queue
impl.setReadHandler({ [weak self] (packets, error) in
guard let _ = self else {
return
}
queue.sync {
handler(packets, error)
}
}, maxDatagrams: maxDatagrams)
}
func writePacket(_ packet: Data, completionHandler: ((Error?) -> Void)?) {
impl.writeDatagram(packet) { (error) in
completionHandler?(error)
}
}
func writePackets(_ packets: [Data], completionHandler: ((Error?) -> Void)?) {
impl.writeMultipleDatagrams(packets) { (error) in
completionHandler?(error)
}
}
}
extension NEUDPSocket {
override var description: String {
guard let hostEndpoint = impl.endpoint as? NWHostEndpoint else {

View File

@ -0,0 +1,103 @@
//
// NETCPLink.swift
// TunnelKit
//
// Created by Davide De Rosa on 5/23/19.
// Copyright (c) 2019 Davide De Rosa. All rights reserved.
//
// https://github.com/passepartoutvpn
//
// This file is part of TunnelKit.
//
// TunnelKit is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// TunnelKit is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with TunnelKit. If not, see <http://www.gnu.org/licenses/>.
//
import Foundation
import NetworkExtension
import __TunnelKitOpenVPN
class NETCPLink: LinkInterface {
private let impl: NWTCPConnection
private let maxPacketSize: Int
init(impl: NWTCPConnection, mtu: Int, maxPacketSize: Int? = nil) {
self.impl = impl
self.mtu = mtu
self.maxPacketSize = maxPacketSize ?? (512 * 1024)
}
// MARK: LinkInterface
let isReliable: Bool = true
var remoteAddress: String? {
return (impl.remoteAddress as? NWHostEndpoint)?.hostname
}
let mtu: Int
var packetBufferSize: Int {
return maxPacketSize
}
func setReadHandler(queue: DispatchQueue, _ handler: @escaping ([Data]?, Error?) -> Void) {
loopReadPackets(queue, Data(), handler)
}
private func loopReadPackets(_ queue: DispatchQueue, _ buffer: Data, _ handler: @escaping ([Data]?, Error?) -> Void) {
// WARNING: runs in Network.framework queue
impl.readMinimumLength(2, maximumLength: packetBufferSize) { [weak self] (data, error) in
guard let _ = self else {
return
}
queue.sync {
guard (error == nil), let data = data else {
handler(nil, error)
return
}
var newBuffer = buffer
newBuffer.append(contentsOf: data)
var until = 0
let packets = PacketStream.packets(fromStream: newBuffer, until: &until)
newBuffer = newBuffer.subdata(in: until..<newBuffer.count)
self?.loopReadPackets(queue, newBuffer, handler)
handler(packets, nil)
}
}
}
func writePacket(_ packet: Data, completionHandler: ((Error?) -> Void)?) {
let stream = PacketStream.stream(fromPacket: packet)
impl.write(stream) { (error) in
completionHandler?(error)
}
}
func writePackets(_ packets: [Data], completionHandler: ((Error?) -> Void)?) {
let stream = PacketStream.stream(fromPackets: packets)
impl.write(stream) { (error) in
completionHandler?(error)
}
}
}
extension NETCPSocket: LinkProducer {
func link(withMTU mtu: Int) -> LinkInterface {
return NETCPLink(impl: impl, mtu: mtu)
}
}

View File

@ -0,0 +1,84 @@
//
// NEUDPLink.swift
// TunnelKit
//
// Created by Davide De Rosa on 5/23/19.
// Copyright (c) 2019 Davide De Rosa. All rights reserved.
//
// https://github.com/passepartoutvpn
//
// This file is part of TunnelKit.
//
// TunnelKit is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// TunnelKit is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with TunnelKit. If not, see <http://www.gnu.org/licenses/>.
//
import Foundation
import NetworkExtension
class NEUDPLink: LinkInterface {
private let impl: NWUDPSession
private let maxDatagrams: Int
init(impl: NWUDPSession, mtu: Int, maxDatagrams: Int? = nil) {
self.impl = impl
self.mtu = mtu
self.maxDatagrams = maxDatagrams ?? 200
}
// MARK: LinkInterface
let isReliable: Bool = false
var remoteAddress: String? {
return (impl.resolvedEndpoint as? NWHostEndpoint)?.hostname
}
let mtu: Int
var packetBufferSize: Int {
return maxDatagrams
}
func setReadHandler(queue: DispatchQueue, _ handler: @escaping ([Data]?, Error?) -> Void) {
// WARNING: runs in Network.framework queue
impl.setReadHandler({ [weak self] (packets, error) in
guard let _ = self else {
return
}
queue.sync {
handler(packets, error)
}
}, maxDatagrams: maxDatagrams)
}
func writePacket(_ packet: Data, completionHandler: ((Error?) -> Void)?) {
impl.writeDatagram(packet) { (error) in
completionHandler?(error)
}
}
func writePackets(_ packets: [Data], completionHandler: ((Error?) -> Void)?) {
impl.writeMultipleDatagrams(packets) { (error) in
completionHandler?(error)
}
}
}
extension NEUDPSocket: LinkProducer {
func link(withMTU mtu: Int) -> LinkInterface {
return NEUDPLink(impl: impl, mtu: mtu)
}
}

View File

@ -401,14 +401,14 @@ extension TunnelKitProvider: GenericSocketDelegate {
}
func socketDidBecomeActive(_ socket: GenericSocket) {
guard let session = session else {
guard let session = session, let producer = socket as? LinkProducer else {
return
}
if session.canRebindLink() {
session.rebindLink(socket.link(withMTU: cfg.mtu))
session.rebindLink(producer.link(withMTU: cfg.mtu))
reasserting = false
} else {
session.setLink(socket.link(withMTU: cfg.mtu))
session.setLink(producer.link(withMTU: cfg.mtu))
}
}