From da30d2898e59d34dc47b251a07376db70fcd3e96 Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Mon, 16 May 2016 19:58:24 -0700 Subject: [PATCH] beginning of communicating with the zero tier service --- ZeroTier One.xcodeproj/project.pbxproj | 4 + ZeroTier One/Info.plist | 5 + ZeroTier One/ServiceCom.swift | 97 +++++++++++++++++++ ZeroTier One/ShowNetworksViewController.swift | 6 ++ 4 files changed, 112 insertions(+) create mode 100644 ZeroTier One/ServiceCom.swift diff --git a/ZeroTier One.xcodeproj/project.pbxproj b/ZeroTier One.xcodeproj/project.pbxproj index 4fb0a76e2..d0441a5cb 100644 --- a/ZeroTier One.xcodeproj/project.pbxproj +++ b/ZeroTier One.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 9330F1351CEAB4C400687EC8 /* ServiceCom.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9330F1341CEAB4C400687EC8 /* ServiceCom.swift */; }; 93326BDC1CE7C816005CA2AC /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93326BDB1CE7C816005CA2AC /* AppDelegate.swift */; }; 93326BDE1CE7C816005CA2AC /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 93326BDD1CE7C816005CA2AC /* Assets.xcassets */; }; 93326BE11CE7C816005CA2AC /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 93326BDF1CE7C816005CA2AC /* MainMenu.xib */; }; @@ -17,6 +18,7 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 9330F1341CEAB4C400687EC8 /* ServiceCom.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ServiceCom.swift; sourceTree = ""; }; 93326BD81CE7C816005CA2AC /* ZeroTier One.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "ZeroTier One.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 93326BDB1CE7C816005CA2AC /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 93326BDD1CE7C816005CA2AC /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; @@ -61,6 +63,7 @@ 93326BDB1CE7C816005CA2AC /* AppDelegate.swift */, 93326BE81CE7D9B9005CA2AC /* JoinNetworkViewController.swift */, 93326BEC1CE7DA30005CA2AC /* ShowNetworksViewController.swift */, + 9330F1341CEAB4C400687EC8 /* ServiceCom.swift */, 93326BDD1CE7C816005CA2AC /* Assets.xcassets */, 93326BDF1CE7C816005CA2AC /* MainMenu.xib */, 93326BE21CE7C816005CA2AC /* Info.plist */, @@ -145,6 +148,7 @@ 93326BDC1CE7C816005CA2AC /* AppDelegate.swift in Sources */, 93326BEA1CE7D9B9005CA2AC /* JoinNetworkViewController.swift in Sources */, 93326BEE1CE7DA30005CA2AC /* ShowNetworksViewController.swift in Sources */, + 9330F1351CEAB4C400687EC8 /* ServiceCom.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/ZeroTier One/Info.plist b/ZeroTier One/Info.plist index f5d0dc948..7a6ab7230 100644 --- a/ZeroTier One/Info.plist +++ b/ZeroTier One/Info.plist @@ -2,6 +2,11 @@ + NSAppTransportSecurity + + NSAllowsArbitraryLoads + + LSUIElement CFBundleDevelopmentRegion diff --git a/ZeroTier One/ServiceCom.swift b/ZeroTier One/ServiceCom.swift new file mode 100644 index 000000000..a1bb0a3c2 --- /dev/null +++ b/ZeroTier One/ServiceCom.swift @@ -0,0 +1,97 @@ +// +// ServiceCom.swift +// ZeroTier One +// +// Created by Grant Limberg on 5/16/16. +// Copyright © 2016 ZeroTier, Inc. All rights reserved. +// + +import Cocoa + +class ServiceCom: NSObject { + static let baseURL = "http://localhost:9993" + static var key: NSString? = "ddeb3b1e6996b6b4f2d12d10" + + static func getNetworkList() { + + let urlString = baseURL + "/network?auth=\(ServiceCom.key!)" + + let url = NSURL(string: urlString) + + if let u = url { + let request = NSMutableURLRequest(URL: u) + let session = NSURLSession.sharedSession() + let task = session.dataTaskWithRequest(request) { (data, response, error) in + let httpResponse = response as! NSHTTPURLResponse + let status = httpResponse.statusCode + + if status == 200 { + do { + let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions()) + print("\(json)") + } + catch { + print("JSON Error: \(error)") + } + } + } + + task.resume() + } + else { + print("bad URL") + } + } + + + static func joinNetwork(network: String) { + let urlString = baseURL + "/network/\(network)?auth=\(ServiceCom.key!)" + let url = NSURL(string: urlString) + + if let u = url { + let request = NSMutableURLRequest(URL: u) + request.HTTPMethod = "POST" + + let session = NSURLSession.sharedSession() + let task = session.dataTaskWithRequest(request) { (data, response, error) in + let httpResponse = response as! NSHTTPURLResponse + let status = httpResponse.statusCode + + if status == 200 { + print("join ok") + } + else { + print("join error: \(status)") + } + } + + task.resume() + } + } + + static func leaveNetwork(network: String) { + let urlString = baseURL + "/network/\(network)?auth=\(ServiceCom.key!)" + let url = NSURL(string: urlString) + + if let u = url { + let request = NSMutableURLRequest(URL: u) + request.HTTPMethod = "DELETE" + + let session = NSURLSession.sharedSession() + let task = session.dataTaskWithRequest(request) { (data, response, error) in + let httpResponse = response as! NSHTTPURLResponse + let status = httpResponse.statusCode + + if status == 200 { + print("leave ok") + } + else { + print("leave error: \(status)") + } + } + + task.resume() + } + } + +} diff --git a/ZeroTier One/ShowNetworksViewController.swift b/ZeroTier One/ShowNetworksViewController.swift index 8bf84a1cb..0e8fd4b6b 100644 --- a/ZeroTier One/ShowNetworksViewController.swift +++ b/ZeroTier One/ShowNetworksViewController.swift @@ -16,5 +16,11 @@ class ShowNetworksViewController: NSViewController { super.viewDidLoad() // Do view setup here. } + + override func viewWillAppear() { + super.viewWillAppear() + + ServiceCom.getNetworkList() + } }