bump websocket version

This commit is contained in:
Erik 2016-02-27 19:43:21 -05:00
parent 16e0bf6319
commit 5976ef1752

View File

@ -109,7 +109,7 @@ public class WebSocket : NSObject, NSStreamDelegate {
public var headers = [String: String]() public var headers = [String: String]()
public var voipEnabled = false public var voipEnabled = false
public var selfSignedSSL = false public var selfSignedSSL = false
private var security: SSLSecurity? public var security: SSLSecurity?
public var enabledSSLCipherSuites: [SSLCipherSuite]? public var enabledSSLCipherSuites: [SSLCipherSuite]?
public var origin: String? public var origin: String?
public var isConnected :Bool { public var isConnected :Bool {
@ -167,8 +167,8 @@ public class WebSocket : NSObject, NSStreamDelegate {
public func disconnect(forceTimeout forceTimeout: NSTimeInterval? = nil) { public func disconnect(forceTimeout forceTimeout: NSTimeInterval? = nil) {
switch forceTimeout { switch forceTimeout {
case .Some(let seconds) where seconds > 0: case .Some(let seconds) where seconds > 0:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(seconds * Double(NSEC_PER_SEC))), queue) { [unowned self] in dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(seconds * Double(NSEC_PER_SEC))), queue) { [weak self] in
self.disconnectStream(nil) self?.disconnectStream(nil)
} }
fallthrough fallthrough
case .None: case .None:
@ -309,13 +309,12 @@ public class WebSocket : NSObject, NSStreamDelegate {
let bytes = UnsafePointer<UInt8>(data.bytes) let bytes = UnsafePointer<UInt8>(data.bytes)
var timeout = 5000000 //wait 5 seconds before giving up var timeout = 5000000 //wait 5 seconds before giving up
writeQueue.addOperationWithBlock { [weak self] in writeQueue.addOperationWithBlock { [weak self] in
guard let this = self else { return }
while !outStream.hasSpaceAvailable { while !outStream.hasSpaceAvailable {
usleep(100) //wait until the socket is ready usleep(100) //wait until the socket is ready
timeout -= 100 timeout -= 100
if timeout < 0 { if timeout < 0 {
this.cleanupStream() self?.cleanupStream()
this.doDisconnect(this.errorWithDetail("write wait timed out", code: 2)) self?.doDisconnect(self?.errorWithDetail("write wait timed out", code: 2))
return return
} else if outStream.streamError != nil { } else if outStream.streamError != nil {
return //disconnectStream will be called. return //disconnectStream will be called.
@ -352,7 +351,11 @@ public class WebSocket : NSObject, NSStreamDelegate {
} }
//disconnect the stream object //disconnect the stream object
private func disconnectStream(error: NSError?) { private func disconnectStream(error: NSError?) {
if error == nil {
writeQueue.waitUntilAllOperationsAreFinished() writeQueue.waitUntilAllOperationsAreFinished()
} else {
writeQueue.cancelAllOperations()
}
cleanupStream() cleanupStream()
doDisconnect(error) doDisconnect(error)
} }
@ -814,7 +817,7 @@ public class WebSocket : NSObject, NSStreamDelegate {
} }
private class SSLCert { public class SSLCert {
var certData: NSData? var certData: NSData?
var key: SecKeyRef? var key: SecKeyRef?
@ -825,7 +828,7 @@ private class SSLCert {
- returns: a representation security object to be used with - returns: a representation security object to be used with
*/ */
init(data: NSData) { public init(data: NSData) {
self.certData = data self.certData = data
} }
@ -836,13 +839,13 @@ private class SSLCert {
- returns: a representation security object to be used with - returns: a representation security object to be used with
*/ */
init(key: SecKeyRef) { public init(key: SecKeyRef) {
self.key = key self.key = key
} }
} }
private class SSLSecurity { public class SSLSecurity {
var validatedDN = true //should the domain name be validated? public var validatedDN = true //should the domain name be validated?
var isReady = false //is the key processing done? var isReady = false //is the key processing done?
var certificates: [NSData]? //the certificates var certificates: [NSData]? //the certificates
@ -856,10 +859,11 @@ private class SSLSecurity {
- returns: a representation security object to be used with - returns: a representation security object to be used with
*/ */
convenience init(usePublicKeys: Bool = false) { public convenience init(usePublicKeys: Bool = false) {
let paths = NSBundle.mainBundle().pathsForResourcesOfType("cer", inDirectory: ".") let paths = NSBundle.mainBundle().pathsForResourcesOfType("cer", inDirectory: ".")
let certs = paths.reduce([SSLCert]()) { (var certs: [SSLCert], path: String) -> [SSLCert] in let certs = paths.reduce([SSLCert]()) { (certs: [SSLCert], path: String) -> [SSLCert] in
var certs = certs
if let data = NSData(contentsOfFile: path) { if let data = NSData(contentsOfFile: path) {
certs.append(SSLCert(data: data)) certs.append(SSLCert(data: data))
} }
@ -877,12 +881,13 @@ private class SSLSecurity {
- returns: a representation security object to be used with - returns: a representation security object to be used with
*/ */
init(certs: [SSLCert], usePublicKeys: Bool) { public init(certs: [SSLCert], usePublicKeys: Bool) {
self.usePublicKeys = usePublicKeys self.usePublicKeys = usePublicKeys
if self.usePublicKeys { if self.usePublicKeys {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)) {
let pubKeys = certs.reduce([SecKeyRef]()) { (var pubKeys: [SecKeyRef], cert: SSLCert) -> [SecKeyRef] in let pubKeys = certs.reduce([SecKeyRef]()) { (pubKeys: [SecKeyRef], cert: SSLCert) -> [SecKeyRef] in
var pubKeys = pubKeys
if let data = cert.certData where cert.key == nil { if let data = cert.certData where cert.key == nil {
cert.key = self.extractPublicKey(data) cert.key = self.extractPublicKey(data)
} }
@ -896,7 +901,8 @@ private class SSLSecurity {
self.isReady = true self.isReady = true
} }
} else { } else {
let certificates = certs.reduce([NSData]()) { (var certificates: [NSData], cert: SSLCert) -> [NSData] in let certificates = certs.reduce([NSData]()) { (certificates: [NSData], cert: SSLCert) -> [NSData] in
var certificates = certificates
if let data = cert.certData { if let data = cert.certData {
certificates.append(data) certificates.append(data)
} }
@ -915,7 +921,7 @@ private class SSLSecurity {
- returns: if the key was successfully validated - returns: if the key was successfully validated
*/ */
func isValid(trust: SecTrustRef, domain: String?) -> Bool { public func isValid(trust: SecTrustRef, domain: String?) -> Bool {
var tries = 0 var tries = 0
while(!self.isReady) { while(!self.isReady) {
@ -1010,7 +1016,8 @@ private class SSLSecurity {
- returns: the certificate chain for the trust - returns: the certificate chain for the trust
*/ */
func certificateChainForTrust(trust: SecTrustRef) -> [NSData] { func certificateChainForTrust(trust: SecTrustRef) -> [NSData] {
let certificates = (0..<SecTrustGetCertificateCount(trust)).reduce([NSData]()) { (var certificates: [NSData], index: Int) -> [NSData] in let certificates = (0..<SecTrustGetCertificateCount(trust)).reduce([NSData]()) { (certificates: [NSData], index: Int) -> [NSData] in
var certificates = certificates
let cert = SecTrustGetCertificateAtIndex(trust, index) let cert = SecTrustGetCertificateAtIndex(trust, index)
certificates.append(SecCertificateCopyData(cert!)) certificates.append(SecCertificateCopyData(cert!))
return certificates return certificates
@ -1028,7 +1035,8 @@ private class SSLSecurity {
*/ */
func publicKeyChainForTrust(trust: SecTrustRef) -> [SecKeyRef] { func publicKeyChainForTrust(trust: SecTrustRef) -> [SecKeyRef] {
let policy = SecPolicyCreateBasicX509() let policy = SecPolicyCreateBasicX509()
let keys = (0..<SecTrustGetCertificateCount(trust)).reduce([SecKeyRef]()) { (var keys: [SecKeyRef], index: Int) -> [SecKeyRef] in let keys = (0..<SecTrustGetCertificateCount(trust)).reduce([SecKeyRef]()) { (keys: [SecKeyRef], index: Int) -> [SecKeyRef] in
var keys = keys
let cert = SecTrustGetCertificateAtIndex(trust, index) let cert = SecTrustGetCertificateAtIndex(trust, index)
if let key = extractPublicKeyFromCert(cert!, policy: policy) { if let key = extractPublicKeyFromCert(cert!, policy: policy) {
keys.append(key) keys.append(key)