diff options
author | Jeroen Leenarts <jeroen.leenarts@gmail.com> | 2018-06-22 08:23:39 +0200 |
---|---|---|
committer | Jeroen Leenarts <jeroen.leenarts@gmail.com> | 2018-06-22 08:23:39 +0200 |
commit | f30f0d1a7bcdfa65058cfe0d0571c1add11953da (patch) | |
tree | 99c5ac92d36bac58fa9f0119ad817f56c24d7e11 /WireGuardNetworkExtension | |
parent | Generate fat archive (diff) | |
download | wireguard-apple-f30f0d1a7bcdfa65058cfe0d0571c1add11953da.tar.xz wireguard-apple-f30f0d1a7bcdfa65058cfe0d0571c1add11953da.zip |
Objective-C wrapper around WireguardGo.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'WireGuardNetworkExtension')
6 files changed, 160 insertions, 0 deletions
diff --git a/WireGuardNetworkExtension/Info.plist b/WireGuardNetworkExtension/Info.plist new file mode 100644 index 0000000..497cce6 --- /dev/null +++ b/WireGuardNetworkExtension/Info.plist @@ -0,0 +1,31 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> +<plist version="1.0"> +<dict> + <key>CFBundleDevelopmentRegion</key> + <string>$(DEVELOPMENT_LANGUAGE)</string> + <key>CFBundleDisplayName</key> + <string>WireGuardNetworkExtension</string> + <key>CFBundleExecutable</key> + <string>$(EXECUTABLE_NAME)</string> + <key>CFBundleIdentifier</key> + <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> + <key>CFBundleInfoDictionaryVersion</key> + <string>6.0</string> + <key>CFBundleName</key> + <string>$(PRODUCT_NAME)</string> + <key>CFBundlePackageType</key> + <string>XPC!</string> + <key>CFBundleShortVersionString</key> + <string>1.0</string> + <key>CFBundleVersion</key> + <string>1</string> + <key>NSExtension</key> + <dict> + <key>NSExtensionPointIdentifier</key> + <string>com.apple.networkextension.packet-tunnel</string> + <key>NSExtensionPrincipalClass</key> + <string>$(PRODUCT_MODULE_NAME).PacketTunnelProvider</string> + </dict> +</dict> +</plist> diff --git a/WireGuardNetworkExtension/PacketTunnelProvider.swift b/WireGuardNetworkExtension/PacketTunnelProvider.swift new file mode 100644 index 0000000..095a1b8 --- /dev/null +++ b/WireGuardNetworkExtension/PacketTunnelProvider.swift @@ -0,0 +1,38 @@ +// +// PacketTunnelProvider.swift +// WireGuardNetworkExtension +// +// Created by Jeroen Leenarts on 19-06-18. +// Copyright © 2018 Wireguard. All rights reserved. +// + +import NetworkExtension + +class PacketTunnelProvider: NEPacketTunnelProvider { + + override func startTunnel(options: [String: NSObject]?, completionHandler: @escaping (Error?) -> Void) { + // Add code here to start the process of connecting the tunnel. + + } + + override func stopTunnel(with reason: NEProviderStopReason, completionHandler: @escaping () -> Void) { + // Add code here to start the process of stopping the tunnel. + completionHandler() + } + + override func handleAppMessage(_ messageData: Data, completionHandler: ((Data?) -> Void)?) { + // Add code here to handle the message. + if let handler = completionHandler { + handler(messageData) + } + } + + override func sleep(completionHandler: @escaping () -> Void) { + // Add code here to get ready to sleep. + completionHandler() + } + + override func wake() { + // Add code here to wake up. + } +} diff --git a/WireGuardNetworkExtension/WireGuardGoWrapper.h b/WireGuardNetworkExtension/WireGuardGoWrapper.h new file mode 100644 index 0000000..0a1e6ce --- /dev/null +++ b/WireGuardNetworkExtension/WireGuardGoWrapper.h @@ -0,0 +1,16 @@ +// +// WireGuardGoWrapper.h +// WireGuardNetworkExtension +// +// Created by Jeroen Leenarts on 21-06-18. +// Copyright © 2018 Wireguard. All rights reserved. +// + +#import <Foundation/Foundation.h> + +@interface WireGuardGoWrapper : NSObject + +- (void) turnOnWithInterfaceName: (NSString *)interfaceName settingsString: (NSString *)settingsString; +- (void) turnOff; + +@end diff --git a/WireGuardNetworkExtension/WireGuardGoWrapper.m b/WireGuardNetworkExtension/WireGuardGoWrapper.m new file mode 100644 index 0000000..8852b7b --- /dev/null +++ b/WireGuardNetworkExtension/WireGuardGoWrapper.m @@ -0,0 +1,56 @@ +// +// WireGuardGoWrapper.m +// WireGuardNetworkExtension +// +// Created by Jeroen Leenarts on 21-06-18. +// Copyright © 2018 Wireguard. All rights reserved. +// + +#import "WireGuardGoWrapper.h" + +#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); + +@interface WireGuardGoWrapper () + +@property (nonatomic, assign) int handle; +@property (nonatomic, assign) BOOL isClosed; + +@end + +@implementation WireGuardGoWrapper + +- (void) turnOnWithInterfaceName: (NSString *)interfaceName settingsString: (NSString *)settingsString +{ + const char * ifName = [interfaceName UTF8String]; + const char * settings = [settingsString UTF8String]; + + self.handle = wgTurnOn((gostring_t){ .p = ifName, .n = interfaceName.length }, (gostring_t){ .p = settings, .n = settingsString.length }, do_read, do_write, (__bridge void *)(self)); +} + +- (void) turnOff +{ + self.isClosed = YES; + wgTurnOff(self.handle); +} + +@end + +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); + return wrapper.isClosed ? -1 : 0; +} + +static ssize_t do_write(const void *ctx, const unsigned char *buf, size_t len) +{ + WireGuardGoWrapper *wrapper = (__bridge WireGuardGoWrapper *)ctx; + printf("Writing from instance with ctx %p into buffer %p of length %zu\n", ctx, buf, len); + return len; +} diff --git a/WireGuardNetworkExtension/WireGuardNetworkExtension-Bridging-Header.h b/WireGuardNetworkExtension/WireGuardNetworkExtension-Bridging-Header.h new file mode 100644 index 0000000..cfbb258 --- /dev/null +++ b/WireGuardNetworkExtension/WireGuardNetworkExtension-Bridging-Header.h @@ -0,0 +1,5 @@ +// +// Use this file to import your target's public headers that you would like to expose to Swift. +// + +#import "WireGuardGoWrapper.h" diff --git a/WireGuardNetworkExtension/WireGuardNetworkExtension.entitlements b/WireGuardNetworkExtension/WireGuardNetworkExtension.entitlements new file mode 100644 index 0000000..43e2a4d --- /dev/null +++ b/WireGuardNetworkExtension/WireGuardNetworkExtension.entitlements @@ -0,0 +1,14 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> +<plist version="1.0"> +<dict> + <key>com.apple.security.application-groups</key> + <array> + <string>group.appforce1.com.wireguard.ios.WireGuard</string> + </array> + <key>keychain-access-groups</key> + <array> + <string>$(AppIdentifierPrefix)appforce1.com.wireguard.ios.WireGuard</string> + </array> +</dict> +</plist> |