diff --git a/Podfile.lock b/Podfile.lock index aaad4ec..e0cd145 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -63,4 +63,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: 0d0e9b16451896d624a83e7c8701984fa3e606e0 -COCOAPODS: 1.5.2 +COCOAPODS: 1.5.3 diff --git a/WireGuard.xcodeproj/project.pbxproj b/WireGuard.xcodeproj/project.pbxproj index d856bf1..694b167 100644 --- a/WireGuard.xcodeproj/project.pbxproj +++ b/WireGuard.xcodeproj/project.pbxproj @@ -35,6 +35,8 @@ 4A8AABD820B6A79100B6D8C1 /* UITableView+WireGuard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A8AABD720B6A79100B6D8C1 /* UITableView+WireGuard.swift */; }; 4AD095C820DC4190000E9CF5 /* libwg-go.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 4AD0900120DC4171000E9CF5 /* libwg-go.a */; }; 4AD095CC20DC42CD000E9CF5 /* WireGuardGoWrapper.m in Sources */ = {isa = PBXBuildFile; fileRef = 4AD095CB20DC42CD000E9CF5 /* WireGuardGoWrapper.m */; }; + 4AEAC32920F14B3B007B67AB /* Log.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AEAC32820F14B3B007B67AB /* Log.swift */; }; + 4AEAC32B20F14BA9007B67AB /* Log.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AEAC32A20F14BA9007B67AB /* Log.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -108,6 +110,8 @@ 4AD095C920DC42CD000E9CF5 /* WireGuardNetworkExtension-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "WireGuardNetworkExtension-Bridging-Header.h"; sourceTree = ""; }; 4AD095CA20DC42CD000E9CF5 /* WireGuardGoWrapper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WireGuardGoWrapper.h; sourceTree = ""; }; 4AD095CB20DC42CD000E9CF5 /* WireGuardGoWrapper.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = WireGuardGoWrapper.m; sourceTree = ""; }; + 4AEAC32820F14B3B007B67AB /* Log.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Log.swift; sourceTree = ""; }; + 4AEAC32A20F14BA9007B67AB /* Log.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Log.swift; sourceTree = ""; }; 861983CAE8FDC13BC83E7E04 /* Pods_WireGuard.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_WireGuard.framework; sourceTree = BUILT_PRODUCTS_DIR; }; /* End PBXFileReference section */ @@ -175,6 +179,7 @@ 4A4BACEC20B5F1C100F12B28 /* Assets.xcassets */, 4A4BACEE20B5F1C100F12B28 /* LaunchScreen.storyboard */, 4A4BACF120B5F1C100F12B28 /* Info.plist */, + 4AEAC32A20F14BA9007B67AB /* Log.swift */, ); path = WireGuard; sourceTree = ""; @@ -242,6 +247,7 @@ 4AD095CA20DC42CD000E9CF5 /* WireGuardGoWrapper.h */, 4AD095CB20DC42CD000E9CF5 /* WireGuardGoWrapper.m */, 4AD095C920DC42CD000E9CF5 /* WireGuardNetworkExtension-Bridging-Header.h */, + 4AEAC32820F14B3B007B67AB /* Log.swift */, ); path = WireGuardNetworkExtension; sourceTree = ""; @@ -514,6 +520,7 @@ files = ( 4A4BAD0C20B5F6AA00F12B28 /* AppCoordinator.swift in Sources */, 4A4BAD2220B6026900F12B28 /* Interface+CoreDataProperties.swift in Sources */, + 4AEAC32B20F14BA9007B67AB /* Log.swift in Sources */, 4A7F6EDE20B674CD00B260B7 /* Address+CoreDataProperties.swift in Sources */, 4A4BAD1320B5F82400F12B28 /* Identifyable.swift in Sources */, 4A4BAD1720B5F8DE00F12B28 /* WireGuard.xcdatamodeld in Sources */, @@ -546,6 +553,7 @@ files = ( 4AD095CC20DC42CD000E9CF5 /* WireGuardGoWrapper.m in Sources */, 4A61D82920D98CE2006C7A76 /* PacketTunnelProvider.swift in Sources */, + 4AEAC32920F14B3B007B67AB /* Log.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -791,6 +799,7 @@ CODE_SIGN_ENTITLEMENTS = WireGuardNetworkExtension/WireGuardNetworkExtension.entitlements; CODE_SIGN_STYLE = Automatic; DEVELOPMENT_TEAM = 67JZJ7TWU3; + ENABLE_BITCODE = NO; INFOPLIST_FILE = WireGuardNetworkExtension/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 11.4; LD_RUNPATH_SEARCH_PATHS = ( @@ -819,6 +828,7 @@ CODE_SIGN_ENTITLEMENTS = WireGuardNetworkExtension/WireGuardNetworkExtension.entitlements; CODE_SIGN_STYLE = Automatic; DEVELOPMENT_TEAM = 67JZJ7TWU3; + ENABLE_BITCODE = NO; INFOPLIST_FILE = WireGuardNetworkExtension/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 11.4; LD_RUNPATH_SEARCH_PATHS = ( diff --git a/WireGuard/Log.swift b/WireGuard/Log.swift new file mode 100644 index 0000000..3f63fb6 --- /dev/null +++ b/WireGuard/Log.swift @@ -0,0 +1,13 @@ +// +// Log.swift +// WireGuard +// +// Created by Jeroen Leenarts on 07-07-18. +// Copyright © 2018 WireGuard. All rights reserved. +// + +import os.log + +struct Log { + static var general = OSLog(subsystem: "com.wireguard.ios.WireGuard", category: "general") +} diff --git a/WireGuardNetworkExtension/Log.swift b/WireGuardNetworkExtension/Log.swift new file mode 100644 index 0000000..358874c --- /dev/null +++ b/WireGuardNetworkExtension/Log.swift @@ -0,0 +1,13 @@ +// +// Log.swift +// WireGuardNetworkExtension +// +// Created by Jeroen Leenarts on 07-07-18. +// Copyright © 2018 WireGuard. All rights reserved. +// + +import os.log + +struct Log { + static var general = OSLog(subsystem: "com.wireguard.ios.WireGuard.WireGuardNetworkExtension", category: "general") +} diff --git a/WireGuardNetworkExtension/PacketTunnelProvider.swift b/WireGuardNetworkExtension/PacketTunnelProvider.swift index 07fd8d3..14ddbb8 100644 --- a/WireGuardNetworkExtension/PacketTunnelProvider.swift +++ b/WireGuardNetworkExtension/PacketTunnelProvider.swift @@ -7,16 +7,31 @@ // import NetworkExtension +import os.log class PacketTunnelProvider: NEPacketTunnelProvider { + let wireGuardWrapper = WireGuardGoWrapper() + + private let tunnelQueue = DispatchQueue(label: PacketTunnelProvider.description()) + + //TODO create a way to transfer config into extension override func startTunnel(options: [String: NSObject]?, completionHandler: @escaping (Error?) -> Void) { + os_log("Starting tunnel", log: Log.general, type: .info) // Add code here to start the process of connecting the tunnel. + //TODO get a settings string in here. + tunnelQueue.sync { + wireGuardWrapper.turnOn(withInterfaceName: "TODO", settingsString: "TODO") + } } override func stopTunnel(with reason: NEProviderStopReason, completionHandler: @escaping () -> Void) { + os_log("Stopping tunnel", log: Log.general, type: .info) // Add code here to start the process of stopping the tunnel. + tunnelQueue.sync { + wireGuardWrapper.turnOff() + } completionHandler() } @@ -27,12 +42,21 @@ class PacketTunnelProvider: NEPacketTunnelProvider { } } - override func sleep(completionHandler: @escaping () -> Void) { - // Add code here to get ready to sleep. - completionHandler() + private func loopReadPackets(_ handler: @escaping ([Data]?, Error?) -> Void) { + packetFlow.readPackets { [weak self] (_, _) in + // TODO write packets into the tunnel + self?.loopReadPackets(handler) + } } - override func wake() { - // Add code here to wake up. + func writePacket(_ packet: Data, completionHandler: ((Error?) -> Void)?) { + packetFlow.writePackets([packet], withProtocols: [AF_INET] as [NSNumber]) + completionHandler?(nil) + } + + func writePackets(_ packets: [Data], completionHandler: ((Error?) -> Void)?) { + let protocols = [Int32](repeating: AF_INET, count: packets.count) as [NSNumber] + packetFlow.writePackets(packets, withProtocols: protocols) + completionHandler?(nil) } } diff --git a/WireGuardNetworkExtension/WireGuardGoWrapper.m b/WireGuardNetworkExtension/WireGuardGoWrapper.m index 4244fe9..0b854f1 100644 --- a/WireGuardNetworkExtension/WireGuardGoWrapper.m +++ b/WireGuardNetworkExtension/WireGuardGoWrapper.m @@ -8,12 +8,17 @@ #import "WireGuardGoWrapper.h" +#include #include "wireguard.h" /// Trampoline function static ssize_t do_read(const void *ctx, const unsigned char *buf, size_t len); /// Trampoline function static ssize_t do_write(const void *ctx, const unsigned char *buf, size_t len); +/// Trampoline function +static void do_log(int level, const char *tag, const char *msg); + + @interface WireGuardGoWrapper () @@ -26,6 +31,9 @@ static ssize_t do_write(const void *ctx, const unsigned char *buf, size_t len); - (void) turnOnWithInterfaceName: (NSString *)interfaceName settingsString: (NSString *)settingsString { + + wgSetLogger(do_log); + const char * ifName = [interfaceName UTF8String]; const char * settings = [settingsString UTF8String]; @@ -38,6 +46,16 @@ static ssize_t do_write(const void *ctx, const unsigned char *buf, size_t len); wgTurnOff(self.handle); } ++ (os_log_t)log { + static os_log_t subLog = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + subLog = os_log_create("com.wireguard.ios.WireGuard.WireGuardNetworkExtension", "WireGuard-Go"); + }); + + return subLog; +} + @end static ssize_t do_read(const void *ctx, const unsigned char *buf, size_t len) @@ -45,6 +63,7 @@ static ssize_t do_read(const void *ctx, const unsigned char *buf, size_t len) WireGuardGoWrapper *wrapper = (__bridge WireGuardGoWrapper *)ctx; printf("Reading from instance with ctx %p into buffer %p of length %zu\n", ctx, buf, len); sleep(1); + // TODO received data from tunnel, write to Packetflow return wrapper.isClosed ? -1 : 0; } @@ -54,3 +73,9 @@ static ssize_t do_write(const void *ctx, const unsigned char *buf, size_t len) printf("Writing from instance with ctx %p into buffer %p of length %zu\n", ctx, buf, len); return len; } + +static void do_log(int level, const char *tag, const char *msg) +{ + // TODO Get some details on the log level and distribute to matching log levels. + os_log([WireGuardGoWrapper log], "Log level %d for %s: %s", level, tag, msg); +}