diff --git a/WireGuardNetworkExtension/PacketTunnelProvider.swift b/WireGuardNetworkExtension/PacketTunnelProvider.swift index a86bad2..a885a41 100644 --- a/WireGuardNetworkExtension/PacketTunnelProvider.swift +++ b/WireGuardNetworkExtension/PacketTunnelProvider.swift @@ -41,7 +41,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider { //TODO: Hardcoded values for addresses let ipv4Settings = NEIPv4Settings(addresses: ["10.50.10.171"], subnetMasks: ["255.255.224.0"]) //TODO: Hardcoded values for allowed ips - ipv4Settings.includedRoutes = [NEIPv4Route(destinationAddress: "0.0.0.0", subnetMask: "0.0.0.0")] + ipv4Settings.includedRoutes = [NEIPv4Route.default()] ipv4Settings.excludedRoutes = endpoints.split(separator: ",").compactMap { $0.split(separator: ":").first}.map {NEIPv4Route(destinationAddress: String($0), subnetMask: "255.255.255.255")} //TODO IPv6 settings @@ -58,12 +58,14 @@ class PacketTunnelProvider: NEPacketTunnelProvider { } setTunnelNetworkSettings(newSettings) { [weak self](error) in - completionHandler(error) + self?.wireGuardWrapper.packetFlow = self?.packetFlow self?.wireGuardWrapper.configured = true self?.wireGuardWrapper.startReadingPackets() + completionHandler(error) } } else { + self.wireGuardWrapper.packetFlow = self.packetFlow completionHandler(PacketTunnelProviderError.tunnelSetupFailed) wireGuardWrapper.configured = false } diff --git a/WireGuardNetworkExtension/WireGuardGoWrapper.h b/WireGuardNetworkExtension/WireGuardGoWrapper.h index 21ee8b4..09c7193 100644 --- a/WireGuardNetworkExtension/WireGuardGoWrapper.h +++ b/WireGuardNetworkExtension/WireGuardGoWrapper.h @@ -11,7 +11,7 @@ @interface WireGuardGoWrapper : NSObject -@property (nonatomic, weak) NEPacketTunnelFlow *packetFlow; +@property (nonatomic, strong) NEPacketTunnelFlow *packetFlow; @property (nonatomic, assign) BOOL configured; - (BOOL) turnOnWithInterfaceName: (NSString *)interfaceName settingsString: (NSString *)settingsString; diff --git a/WireGuardNetworkExtension/WireGuardGoWrapper.m b/WireGuardNetworkExtension/WireGuardGoWrapper.m index 7afe255..c4235b8 100644 --- a/WireGuardNetworkExtension/WireGuardGoWrapper.m +++ b/WireGuardNetworkExtension/WireGuardGoWrapper.m @@ -83,12 +83,14 @@ static void do_log(int level, const char *tag, const char *msg); os_log_debug([WireGuardGoWrapper log], "readPackets - read call - on thread \"%{public}@\" - %d", NSThread.currentThread.name, (int)NSThread.currentThread); [self.packetFlow readPacketsWithCompletionHandler:^(NSArray * _Nonnull packets, NSArray * _Nonnull protocols) { + [self.condition lock]; @synchronized(self.packets) { [self.packets addObjectsFromArray:packets]; [self.protocols addObjectsFromArray:protocols]; } os_log_debug([WireGuardGoWrapper log], "readPackets - signal - on thread \"%{public}@\" - %d", NSThread.currentThread.name, (int)NSThread.currentThread); [self.condition signal]; + [self.condition unlock]; [self readPackets]; }]; }); @@ -126,6 +128,7 @@ static ssize_t do_read(const void *ctx, const unsigned char *buf, size_t len) NSData * __block packet = nil; // NSNumber *protocol = nil; dispatch_sync(wrapper.dispatchQueue, ^{ + [wrapper.condition lock]; @synchronized(wrapper.packets) { if (wrapper.packets.count == 0) { os_log_debug([WireGuardGoWrapper log], "do_read - no packet - on thread \"%{public}@\" - %d", NSThread.currentThread.name, (int)NSThread.currentThread); @@ -143,7 +146,10 @@ static ssize_t do_read(const void *ctx, const unsigned char *buf, size_t len) if (packet == nil) { os_log_debug([WireGuardGoWrapper log], "do_read - wait - on thread \"%{public}@\" - %d", NSThread.currentThread.name, (int)NSThread.currentThread); [wrapper.condition wait]; + [wrapper.condition unlock]; return 0; + } else { + [wrapper.condition unlock]; } NSUInteger packetLength = [packet length];