Make AppExtension entities public

This commit is contained in:
Davide De Rosa 2019-05-23 21:49:20 +02:00
parent 334613401b
commit 72ce14b676
12 changed files with 67 additions and 60 deletions

View File

@ -36,7 +36,7 @@
import Foundation
protocol GenericSocketDelegate: class {
public protocol GenericSocketDelegate: class {
func socketDidTimeout(_ socket: GenericSocket)
func socketDidBecomeActive(_ socket: GenericSocket)
@ -46,7 +46,7 @@ protocol GenericSocketDelegate: class {
func socketHasBetterPath(_ socket: GenericSocket)
}
protocol GenericSocket {
public protocol GenericSocket {
var remoteAddress: String? { get }
var hasBetterPath: Bool { get }

View File

@ -44,14 +44,14 @@ extension NSNotification.Name {
static let __InterfaceObserverDidDetectWifiChange = NSNotification.Name("__InterfaceObserverDidDetectWifiChange")
}
class InterfaceObserver: NSObject {
public class InterfaceObserver: NSObject {
private var queue: DispatchQueue?
private var timer: DispatchSourceTimer?
private var lastWifiName: String?
func start(queue: DispatchQueue) {
public func start(queue: DispatchQueue) {
self.queue = queue
let timer = DispatchSource.makeTimerSource(flags: DispatchSource.TimerFlags(rawValue: UInt(0)), queue: queue)
@ -64,7 +64,7 @@ class InterfaceObserver: NSObject {
self.timer = timer
}
func stop() {
public func stop() {
timer?.cancel()
timer = nil
queue = nil
@ -87,7 +87,7 @@ class InterfaceObserver: NSObject {
lastWifiName = currentWifiName
}
func currentWifiNetworkName() -> String? {
public func currentWifiNetworkName() -> String? {
#if os(iOS)
guard let interfaceNames = CNCopySupportedInterfaces() as? [CFString] else {
return nil

View File

@ -36,7 +36,6 @@
import Foundation
/// :nodoc:
public enum KeychainError: Error {
case add
@ -45,7 +44,6 @@ public enum KeychainError: Error {
case typeMismatch
}
/// :nodoc:
public class Keychain {
private let service: String?

View File

@ -25,6 +25,6 @@
import Foundation
protocol LinkProducer {
public protocol LinkProducer {
func link(withMTU mtu: Int) -> LinkInterface
}

View File

@ -37,39 +37,33 @@
import Foundation
import SwiftyBeaver
class MemoryDestination: BaseDestination, CustomStringConvertible {
public class MemoryDestination: BaseDestination, CustomStringConvertible {
private var buffer: [String] = []
var maxLines: Int?
public var maxLines: Int?
override init() {
public override init() {
super.init()
asynchronously = false
}
func start(with existing: [String]) {
public func start(with existing: [String]) {
execute(synchronously: true) {
self.buffer = existing
}
}
func flush(to url: URL) {
public func flush(to url: URL) {
execute(synchronously: true) {
let content = self.buffer.joined(separator: "\n")
try? content.write(to: url, atomically: true, encoding: .utf8)
}
}
var description: String {
return executeSynchronously {
return self.buffer.joined(separator: "\n")
}
}
// MARK: BaseDestination
// XXX: executed in SwiftyBeaver queue. DO NOT invoke execute* here (sync in sync would crash otherwise)
override func send(_ level: SwiftyBeaver.Level, msg: String, thread: String, file: String, function: String, line: Int, context: Any?) -> String? {
public override func send(_ level: SwiftyBeaver.Level, msg: String, thread: String, file: String, function: String, line: Int, context: Any?) -> String? {
guard let message = super.send(level, msg: msg, thread: thread, file: file, function: function, line: line) else {
return nil
}
@ -81,4 +75,12 @@ class MemoryDestination: BaseDestination, CustomStringConvertible {
}
return message
}
// MARK: CustomStringConvertible
public var description: String {
return executeSynchronously {
return self.buffer.joined(separator: "\n")
}
}
}

View File

@ -40,12 +40,12 @@ import SwiftyBeaver
private let log = SwiftyBeaver.self
class NETCPSocket: NSObject, GenericSocket {
public class NETCPSocket: NSObject, GenericSocket {
private static var linkContext = 0
let impl: NWTCPConnection
public let impl: NWTCPConnection
init(impl: NWTCPConnection) {
public init(impl: NWTCPConnection) {
self.impl = impl
isActive = false
isShutdown = false
@ -57,19 +57,19 @@ class NETCPSocket: NSObject, GenericSocket {
private var isActive: Bool
private(set) var isShutdown: Bool
public private(set) var isShutdown: Bool
var remoteAddress: String? {
public var remoteAddress: String? {
return (impl.remoteAddress as? NWHostEndpoint)?.hostname
}
var hasBetterPath: Bool {
public var hasBetterPath: Bool {
return impl.hasBetterPath
}
weak var delegate: GenericSocketDelegate?
public weak var delegate: GenericSocketDelegate?
func observe(queue: DispatchQueue, activeTimeout: Int) {
public func observe(queue: DispatchQueue, activeTimeout: Int) {
isActive = false
self.queue = queue
@ -86,17 +86,17 @@ class NETCPSocket: NSObject, GenericSocket {
impl.addObserver(self, forKeyPath: #keyPath(NWTCPConnection.hasBetterPath), options: .new, context: &NETCPSocket.linkContext)
}
func unobserve() {
public func unobserve() {
impl.removeObserver(self, forKeyPath: #keyPath(NWTCPConnection.state), context: &NETCPSocket.linkContext)
impl.removeObserver(self, forKeyPath: #keyPath(NWTCPConnection.hasBetterPath), context: &NETCPSocket.linkContext)
}
func shutdown() {
public func shutdown() {
impl.writeClose()
impl.cancel()
}
func upgraded() -> GenericSocket? {
public func upgraded() -> GenericSocket? {
guard impl.hasBetterPath else {
return nil
}
@ -105,7 +105,7 @@ class NETCPSocket: NSObject, GenericSocket {
// MARK: Connection KVO (any queue)
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
guard (context == &NETCPSocket.linkContext) else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
return
@ -170,8 +170,9 @@ class NETCPSocket: NSObject, GenericSocket {
}
}
/// :nodoc:
extension NETCPSocket {
override var description: String {
public override var description: String {
guard let hostEndpoint = impl.endpoint as? NWHostEndpoint else {
return impl.endpoint.maskedDescription
}

View File

@ -37,25 +37,25 @@
import Foundation
import NetworkExtension
class NETunnelInterface: TunnelInterface {
public class NETunnelInterface: TunnelInterface {
private weak var impl: NEPacketTunnelFlow?
private let protocolNumber: NSNumber
init(impl: NEPacketTunnelFlow, isIPv6: Bool) {
public init(impl: NEPacketTunnelFlow, isIPv6: Bool) {
self.impl = impl
protocolNumber = (isIPv6 ? AF_INET6 : AF_INET) as NSNumber
}
// MARK: TunnelInterface
var isPersistent: Bool {
public var isPersistent: Bool {
return false
}
// MARK: IOInterface
func setReadHandler(queue: DispatchQueue, _ handler: @escaping ([Data]?, Error?) -> Void) {
public func setReadHandler(queue: DispatchQueue, _ handler: @escaping ([Data]?, Error?) -> Void) {
loopReadPackets(queue, handler)
}
@ -70,12 +70,12 @@ class NETunnelInterface: TunnelInterface {
}
}
func writePacket(_ packet: Data, completionHandler: ((Error?) -> Void)?) {
public func writePacket(_ packet: Data, completionHandler: ((Error?) -> Void)?) {
impl?.writePackets([packet], withProtocols: [protocolNumber])
completionHandler?(nil)
}
func writePackets(_ packets: [Data], completionHandler: ((Error?) -> Void)?) {
public func writePackets(_ packets: [Data], completionHandler: ((Error?) -> Void)?) {
let protocols = [NSNumber](repeating: protocolNumber, count: packets.count)
impl?.writePackets(packets, withProtocols: protocols)
completionHandler?(nil)

View File

@ -40,12 +40,12 @@ import SwiftyBeaver
private let log = SwiftyBeaver.self
class NEUDPSocket: NSObject, GenericSocket {
public class NEUDPSocket: NSObject, GenericSocket {
private static var linkContext = 0
let impl: NWUDPSession
public let impl: NWUDPSession
init(impl: NWUDPSession) {
public init(impl: NWUDPSession) {
self.impl = impl
isActive = false
@ -58,19 +58,19 @@ class NEUDPSocket: NSObject, GenericSocket {
private var isActive: Bool
private(set) var isShutdown: Bool
public private(set) var isShutdown: Bool
var remoteAddress: String? {
public var remoteAddress: String? {
return (impl.resolvedEndpoint as? NWHostEndpoint)?.hostname
}
var hasBetterPath: Bool {
public var hasBetterPath: Bool {
return impl.hasBetterPath
}
weak var delegate: GenericSocketDelegate?
public weak var delegate: GenericSocketDelegate?
func observe(queue: DispatchQueue, activeTimeout: Int) {
public func observe(queue: DispatchQueue, activeTimeout: Int) {
isActive = false
self.queue = queue
@ -87,16 +87,16 @@ class NEUDPSocket: NSObject, GenericSocket {
impl.addObserver(self, forKeyPath: #keyPath(NWUDPSession.hasBetterPath), options: .new, context: &NEUDPSocket.linkContext)
}
func unobserve() {
public func unobserve() {
impl.removeObserver(self, forKeyPath: #keyPath(NWUDPSession.state), context: &NEUDPSocket.linkContext)
impl.removeObserver(self, forKeyPath: #keyPath(NWUDPSession.hasBetterPath), context: &NEUDPSocket.linkContext)
}
func shutdown() {
public func shutdown() {
impl.cancel()
}
func upgraded() -> GenericSocket? {
public func upgraded() -> GenericSocket? {
guard impl.hasBetterPath else {
return nil
}
@ -105,7 +105,7 @@ class NEUDPSocket: NSObject, GenericSocket {
// MARK: Connection KVO (any queue)
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
guard (context == &NEUDPSocket.linkContext) else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
return
@ -173,8 +173,9 @@ class NEUDPSocket: NSObject, GenericSocket {
}
}
/// :nodoc:
extension NEUDPSocket {
override var description: String {
public override var description: String {
guard let hostEndpoint = impl.endpoint as? NWHostEndpoint else {
return impl.endpoint.maskedDescription
}

View File

@ -36,7 +36,6 @@
import Foundation
/// :nodoc:
public class DNSResolver {
private static let queue = DispatchQueue(label: "DNSResolver")

View File

@ -96,8 +96,9 @@ class NETCPLink: LinkInterface {
}
}
/// :nodoc:
extension NETCPSocket: LinkProducer {
func link(withMTU mtu: Int) -> LinkInterface {
public func link(withMTU mtu: Int) -> LinkInterface {
return NETCPLink(impl: impl, mtu: mtu)
}
}

View File

@ -77,8 +77,9 @@ class NEUDPLink: LinkInterface {
}
}
/// :nodoc:
extension NEUDPSocket: LinkProducer {
func link(withMTU mtu: Int) -> LinkInterface {
public func link(withMTU mtu: Int) -> LinkInterface {
return NEUDPLink(impl: impl, mtu: mtu)
}
}

View File

@ -386,7 +386,8 @@ extension OpenVPNTunnelProvider: GenericSocketDelegate {
// MARK: GenericSocketDelegate (tunnel queue)
func socketDidTimeout(_ socket: GenericSocket) {
/// :nodoc:
public func socketDidTimeout(_ socket: GenericSocket) {
log.debug("Socket timed out waiting for activity, cancelling...")
reasserting = true
socket.shutdown()
@ -400,7 +401,8 @@ extension OpenVPNTunnelProvider: GenericSocketDelegate {
}
}
func socketDidBecomeActive(_ socket: GenericSocket) {
/// :nodoc:
public func socketDidBecomeActive(_ socket: GenericSocket) {
guard let session = session, let producer = socket as? LinkProducer else {
return
}
@ -412,7 +414,8 @@ extension OpenVPNTunnelProvider: GenericSocketDelegate {
}
}
func socket(_ socket: GenericSocket, didShutdownWithFailure failure: Bool) {
/// :nodoc:
public func socket(_ socket: GenericSocket, didShutdownWithFailure failure: Bool) {
guard let session = session else {
return
}
@ -463,7 +466,8 @@ extension OpenVPNTunnelProvider: GenericSocketDelegate {
disposeTunnel(error: shutdownError)
}
func socketHasBetterPath(_ socket: GenericSocket) {
/// :nodoc:
public func socketHasBetterPath(_ socket: GenericSocket) {
log.debug("Stopping tunnel due to a new better path")
logCurrentSSID()
session?.reconnect(error: ProviderError.networkChanged)