Objective-C wrapper around WireguardGo.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jeroen Leenarts
2018-06-22 08:23:39 +02:00
parent 17529b300b
commit f30f0d1a7b
10 changed files with 393 additions and 9 deletions
+31
View File
@@ -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>
@@ -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.
}
}
@@ -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
@@ -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;
}
@@ -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"
@@ -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>