This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
zhangyang-libzt/docs/osx_zt_sdk.md

104 lines
3.4 KiB
Markdown
Raw Normal View History

2016-06-14 17:00:44 -07:00
OSX + ZeroTier SDK
2016-06-14 16:01:19 -07:00
====
Welcome!
2016-07-14 08:40:40 -07:00
Imagine a flat, encrypted, no-configuration LAN for all of the instances of your OSX app.
2016-06-14 16:01:19 -07:00
2016-07-18 17:34:18 -07:00
This short tutorial will show you how to enable ZeroTier functionality for your OSX app with little to no code modification. Check out our [ZeroTier SDK](https://www.zerotier.com/blog) page for more info on how the integration works.
2016-06-14 16:01:19 -07:00
***
2016-08-03 15:22:51 -07:00
## Via Traditional Linking (Everything bundled)
- Use this if you'd like everything included in a single easy-to-use library.
2016-08-03 15:22:51 -07:00
```
make osx_shared_lib`
g++ app.cpp -o app libztosx.so
./app
2016-08-03 15:22:51 -07:00
```
## Via Traditional Linking (Service+Intercept model)
2016-06-22 11:35:12 -07:00
- Use this model if you'd like multiple applications to talk to the same ZeroTierSDK service instance. Often the *intercept-model* is used when you don't have access to the source of an app and you'd like to re-direct its network calls.
2016-06-22 11:35:12 -07:00
Example:
gcc app.c -o app libztintercept.so
2016-07-14 08:40:40 -07:00
export ZT_NC_NETWORK=/tmp/sdk-test-home/nc_8056c2e21c000001
2016-06-22 11:35:12 -07:00
Start service
./zerotier-sdk-service -d -p8000 /tmp/sdk-test-home &
2016-06-22 11:35:12 -07:00
Run application
./app
2016-06-22 11:35:12 -07:00
2016-08-03 15:22:51 -07:00
## Via App Framework in XCode
2016-06-22 11:35:12 -07:00
2016-07-14 08:40:40 -07:00
***
**Step 1: Build OSX framework**
2016-06-22 11:35:12 -07:00
- `make osx_app_framework`
- This will output to `build/osx_app_framework/Release/ZeroTierSDK_OSX.framework`
2016-06-22 11:35:12 -07:00
2016-07-14 08:40:40 -07:00
**Step 2: Integrate SDK into project**
2016-06-22 11:43:29 -07:00
2016-07-14 08:40:40 -07:00
- Add the resultant framework package to your project
- Add `src` directory to *Build Settings -> Header Search Paths*
- Add `build/osx_app_framework/Release/` to *Build Settings -> Framework Search Paths*
- Add `ZeroTierSDK.frameworkOSX` to *General->Embedded Binaries*
2016-07-14 08:40:40 -07:00
- Add `src/SDK_XcodeWrapper.cpp` and `src/SDK_XcodeWrapper.hpp` to your project:
- Set `src/SDK_Apple-Bridging-Header.h` as your bridging-header in *Build Settings -> Objective-C Bridging-header*
2016-06-22 11:35:12 -07:00
2016-07-14 08:40:40 -07:00
**Step 3: Start the ZeroTier service**
2016-06-22 11:48:10 -07:00
Set up the ZeroTier service thread:
2016-07-14 08:40:40 -07:00
```
var service_thread : NSThread!
func zt_start_service() {
let path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
start_service(".") // "." path will tell ZeroTier to write its config data to the same directory as the binary
2016-07-14 08:40:40 -07:00
}
```
and then start it:
```
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), {
self.service_thread = NSThread(target:self, selector:"zt_start_service", object:nil)
self.service_thread.start()
});
```
**Step 4: Pick an API**
2016-06-22 11:48:10 -07:00
2016-07-18 18:27:23 -07:00
The following APIs are available for this integration:
2016-07-18 16:52:10 -07:00
- `Direct Call`: Consult [src/SDK_Apple-Bridging-Header.h](../../../../src/SDK_Apple-Bridging-Header.h).
- `Hook of BSD-like sockets`: Use BSD-like sockets as you normally would.
- `Proxy of NSStream`: Create NSStream. Configure stream for SOCKS5 Proxy (127.0.0.1:PORT). Start Proxy. Use stream.
2016-06-22 11:48:10 -07:00
2016-07-14 08:40:40 -07:00
**Step 5: Join a network!**
2016-08-25 17:27:09 -07:00
Simply call `zt_join_network("nwid")`
2016-06-22 11:48:10 -07:00
2016-07-14 08:40:40 -07:00
***
**NSStream and SOCKS Proxy:**
2016-06-14 16:01:19 -07:00
As an example, here's how one would configure a NSStream object to redirect all network activity to the ZeroTier SOCKS proxy server:
```
// BEGIN proxy configuration
let myDict:NSDictionary = [NSStreamSOCKSProxyHostKey : "0.0.0.0",
NSStreamSOCKSProxyPortKey : 1337,
NSStreamSOCKSProxyVersionKey : NSStreamSOCKSProxyVersion5]
inputStream!.setProperty(myDict, forKey: NSStreamSOCKSProxyConfigurationKey)
outputStream!.setProperty(myDict, forKey: NSStreamSOCKSProxyConfigurationKey)
// END proxy configuration
```
2016-07-14 08:40:40 -07:00