2016-05-17 02:58:24 +00:00
|
|
|
//
|
|
|
|
// 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"
|
2016-05-31 20:50:43 +00:00
|
|
|
|
|
|
|
private static func getKey() -> String {
|
|
|
|
struct Holder {
|
|
|
|
static var key: String? = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if Holder.key == nil {
|
|
|
|
do {
|
|
|
|
// Check the user's ZeroTier application support directory. If
|
|
|
|
// authtoken.secret exists, use it.
|
|
|
|
|
|
|
|
var appSupportDir = try NSFileManager.defaultManager().URLForDirectory(.ApplicationSupportDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: false)
|
|
|
|
NSLog("\(appSupportDir)")
|
|
|
|
|
|
|
|
|
|
|
|
appSupportDir = appSupportDir.URLByAppendingPathComponent("ZeroTier")
|
|
|
|
NSLog("\(appSupportDir)")
|
|
|
|
|
|
|
|
appSupportDir = appSupportDir.URLByAppendingPathComponent("One")
|
|
|
|
NSLog("\(appSupportDir)")
|
|
|
|
|
|
|
|
let authtokenURL = appSupportDir.URLByAppendingPathComponent("authtoken.secret")
|
|
|
|
|
|
|
|
NSLog("\(authtokenURL)")
|
|
|
|
|
|
|
|
if NSFileManager.defaultManager().fileExistsAtPath(authtokenURL.path!) {
|
|
|
|
Holder.key = try String(contentsOfURL: authtokenURL)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// TODO: Elevate priviledge to copy /Library/Application Support/ZeroTier/One/authtoken.secret to the user's local AppSupport directory
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
NSLog("Error getting app support dir: \(error)")
|
|
|
|
Holder.key = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if let k = Holder.key {
|
|
|
|
return k
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
2016-05-17 02:58:24 +00:00
|
|
|
|
2016-05-18 02:41:54 +00:00
|
|
|
static func getNetworkList(completionHandler: ([Network]) -> Void) {
|
2016-05-17 02:58:24 +00:00
|
|
|
|
2016-05-31 20:50:43 +00:00
|
|
|
let urlString = baseURL + "/network?auth=\(ServiceCom.getKey())"
|
2016-05-17 02:58:24 +00:00
|
|
|
|
|
|
|
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 {
|
2016-05-18 02:41:54 +00:00
|
|
|
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions()) as! [[String: AnyObject]]
|
|
|
|
|
|
|
|
var networks = [Network]()
|
|
|
|
for jobj in json {
|
|
|
|
networks.append(Network(jsonData: jobj))
|
|
|
|
}
|
|
|
|
|
|
|
|
completionHandler(networks)
|
2016-05-17 02:58:24 +00:00
|
|
|
}
|
|
|
|
catch {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
task.resume()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
print("bad URL")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static func joinNetwork(network: String) {
|
2016-05-31 20:50:43 +00:00
|
|
|
let urlString = baseURL + "/network/\(network)?auth=\(ServiceCom.getKey())"
|
2016-05-17 02:58:24 +00:00
|
|
|
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) {
|
2016-05-31 20:50:43 +00:00
|
|
|
let urlString = baseURL + "/network/\(network)?auth=\(ServiceCom.getKey())"
|
2016-05-17 02:58:24 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|